The code is simple and one of the commenters pointed out an even more efficient way to write the data to a WAV file. The basic idea is to create an array of samples in a buffer using some features of SciPy’s NumPy component.
Most regular waveforms are easy to create using an algorithm. For example, sine waves can be generally described as: y=amplitude * sine(radian_frequency*t+phase_shift)
Where y is the value of the wave at time t. The amplitude is the peak value (so 5 will give you +/-5 V) and the radian frequency is twice the value of pi times the frequency in Hertz. [153Armstrong] shows simple formulae for sine waves, symmetric and asymmetric square waves, and a sawtooth wave, using generators provided by the SciPy package. The code is on GitHub and he also links to the generators available in SciPy.
We’ve seen SciPy in some Hackaday contest entries before. You can think of it like Matlab for Python. Just keep in mind, it isn’t an inherent part of Python. If you use another language, you could use a similar library to get the same effect. And if you’re doing this in hardware, you’ll probably want to use look-up tables, to keep things fast and simple.
Filed under: software hacks