Callysto.ca Banner

Open in Callysto

Creating synthetic sounds

Sound on a computer is represented as a list of numbers. These numbers are translated into an electrical signal that is sent to the computer’s speaker (or headphone) to set up vibrations in the air that our ears can hear.

An important parameter is the number of samples per second that are sent to the computer. Typical choices are 8000 per second for low quality sound, then 44100 or 48000 per second for higher quality (this is CD quality sound). This number is called the sample rate, usually denoted Fs in our code.

Note that some computers have poor quality speakers, so you may not hear the difference.

To set up the Jupyter notebook to deal with sound, we run the following two toolboxes.

from numpy import *    ## for numerical functions
from IPython.display import Audio  ## to output audio

Example 1 - noise

Let’s just create a list of random numbers, and play it out. It should sound like random noise. We use a sample rate of 8000.

Don’t forget to press “play” on the audio tool that appears.

Fs = 8000
signal = random.randn(Fs)

Audio(data=signal, rate=Fs)

Example 2 - pure tone

A sine wave sounds pleasant to our ears. It is easy to create using the mathematical functions available in Python.

440 Hz represents A above middle C on the piano.

Fs, Len, f1 = 8000, 5, 440  ## sample rate, length in second, frequency
t = linspace(0,Len,Fs*Len)  ## time variable
signal = sin(2*pi*f1*t) 

Audio(data=signal, rate=Fs)

Example 3 - tone with harmonics

A sine wave sounds pleasant to our ears. We add harmonics to get some interesting, tonal sounds. We use the power function (seventh power) to get something nice.

Fs, Len, f1 = 8000, 5, 440  ## sample rate, length in second, frequency
t = linspace(0,Len,Fs*Len)
signal = sin(2*pi*f1*t)**7  ## the 7th power introduces harmonics

Audio(data=signal, rate=Fs)

Example 4 - two tones

The sum of two sine waves, with close frequencies, creates beats that we can hear.

Fs, Len, f1, f2 = 8000, 5, 440, 442  ## rate, length, frequencies
t = linspace(0,Len,Fs*Len)
f1 = 440.0
f2 = 442.0
signal = sin(2*pi*f1*t) + sin(2*pi*f2*t)

Audio(data=signal, rate=Fs)

Example 4 - chirp or sweep

Finally, we do a high resolution sound (44100 sample rate, or CD quality) to create a tone that runs from low to high frequencies.

Fs = 44100
Len = 10
t = linspace(0,Len,Fs*Len)
f1 = 1000.0
signal = sin(2*pi*f1*t**2) ## t**2 creates the increasing freq's.

Audio(data=signal, rate=Fs)

Callysto.ca License