

Period 2*pi which is falling from 0 to pi and rising from Return a periodic band-limited triangle wave with # Faster, but runs out of memory and dies # TODO: Maybe choose between these based on number of harmonics? # Sum all multiple sine waves up to the Nyquist frequency Raise ValueError( "Sampling frequency must be constant") > t = linspace(0, 1, num = 1000, endpoint = False) Produces the same phase and amplitude as. Period 2*pi which is falling from 0 to 2*pi and rising at Return a periodic band-limited sawtooth wave with
#Harmonics of a bipolar square wave generator#
# TODO: De-duplicate all this code into one generator function and then make these into wrapper functions Triangle can be made by integrating squareīut I'm trying to avoid leaky integration, etc.įrom numpy import asarray, zeros, pi, sin, cos, amax, diff, arange, outer Square can be made by integrating bipolar BLIT Sawtooth can be made by integrating BLIT minus a DC value to prevent integrator wandering off These use additive synthesis, so they are slow, but exact. Intent is mathematical perfection over performance Learn more about bidirectional Unicode charactersīandlimited versions of. To review, open the file in an editor that reveals hidden Unicode characters. This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. Would it specify a filter object? and then harmonics are scaled by its frequency response? And filter=None should be possible which represents a brickwall! Then it will click for sweeps or go right up to Nyquist for non-sweeps if that's what's desired.Īlso should have a phase= option. So it needs some kind of filter= argument for specifying the roll-off filter. The transition will cause a glitch with infinite harmonics that will alias, and post-filtering won't help. No, you can't cut off the sweep frequencies so that one sample has a frequency and the next does not and then filter after the fact.
#Harmonics of a bipolar square wave how to#
With sweeps or chirps, there is also the problem of harmonics just disappearing suddenly when they hit fs/2 and producing sudden amplitude changes, which can be fixed with a roll-off filter, but which roll-off filter? It should probably be a parameter? But how to specify it as a parameter? I could also just cut it off suddenly, and let the user do their own filter, or oversample and decimate, if they want to. They can generate BLITs, which can then be integrated to form the others, but this accumulates numerical error and requires leaky integration.

Or the discrete summation methods would be more precise? Perhaps, but they cannot generate sawtooth/triangle/square directly, because the harmonic fall-off is at a different rate. Something like BLEPs might be better for this? (Probably not they are not as accurate due to truncation of the sinc.) The extra spikes are the aliased harmonics, which are even more obvious when log-scaled:

Usage: t = linspace(0, 1, num = 1000, endpoint = False)īand-limited version in blue, naïve version in red (equivalent to sampling the ideal mathematical function without putting it through an antialiasing filter first) It currently requires a constant sampling frequency (no chirps) and So the wave is generated by additive synthesis.Īt low frequencies, the difference from the naive version is minimal,Īnd the computational cost of the additive synthesis is high.Īt high frequencies, the difference is very obvious, and theĬomputational cost of additive synthesis is not too bad. Main goal is numerical accuracy for doing simulations, Meant to be called the same way as ,īut without the aliasing effects produced by the existing "naïve" Returns periodic band-limited square, triangle, or sawtooth waveform with
