Simple Wave Generation in Python (and SciPy)

[153Armstrong] did a short post on how easy it is to generate waveforms using Python. We agree it is simple, but actually, it isn’t so much Python per se, it is some pretty cool libraries (SciPy, in particular) that do all the hard work. That may be splitting hairs, but it is worth nothing that SciPy (pronounced “Sigh Pie”) also does other handy tricks like Fourier transforms, too. You can see a video of his results, below.

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

from Hackaday http://ift.tt/2tgMsqV
via IFTTT