ThinkDSP
This notebook contains solutions to exercises in Chapter 1: Sounds and Signals
Copyright 2015 Allen Downey
Exercise
Go to http://freesound.org and download a sound sample that includes music, speech, or other sounds that have a well-defined pitch. Select a roughly half-second segment where the pitch is constant. Compute and plot the spectrum of the segment you selected. What connection can you make between the timbre of the sound and the harmonic structure you see in the spectrum?
Use high_pass
, low_pass
, and band_stop
to filter out some of the harmonics. Then convert the spectrum back to a wave and listen to it. How does the sound relate to the changes you made in the spectrum?
Solution
I chose this recording (or synthesis?) of a trumpet section http://www.freesound.org/people/Dublie/sounds/170255/
As always, thanks to the people who contributed these recordings!
Here's what the whole wave looks like:
By trial and error, I selected a segment with a constant pitch (although I believe it is a chord played by at least two horns).
Here's what the segment looks like:
And here's an even shorter segment so you can see the waveform:
Here's what the spectrum looks like:
It has lots of frequency components. Let's zoom in on the fundamental and dominant frequencies:
peaks
prints the highest points in the spectrum and their frequencies, in descending order:
The dominant peak is at 870 Hz. It's not easy to dig out the fundamental, but with peaks at 507, 347, and 253 Hz, we can infer a fundamental at roughly 85 Hz, with harmonics at 170, 255, 340, 425, and 510 Hz.
85 Hz is close to F2 at 87 Hz. The pitch we perceive is usually the fundamental, even when it is not dominant. When you listen to this segment, what pitch(es) do you perceive?
Next we can filter out the high frequencies:
And here's what it sounds like:
The following interaction allows you to select a segment and apply different filters. If you set the cutoff to 3400 Hz, you can simulate what the sample would sound like over an old (not digital) phone line.
Exercise
Synthesize a compound signal by creating SinSignal and CosSignal objects and adding them up. Evaluate the signal to get a Wave, and listen to it. Compute its Spectrum and plot it. What happens if you add frequency components that are not multiples of the fundamental?
Solution
Here are some arbitrary components I chose. It makes an interesting waveform!
We can use the signal to make a wave:
And here's what it sounds like:
The components are all multiples of 200 Hz, so they make a coherent sounding tone.
Here's what the spectrum looks like:
If we add a component that is not a multiple of 200 Hz, we hear it as a distinct pitch.
Exercise
Write a function called stretch
that takes a Wave and a stretch factor and speeds up or slows down the wave by modifying ts
and framerate
. Hint: it should only take two lines of code.
Solution
I'll use the trumpet example again:
Here's my implementation of stretch
And here's what it sounds like if we speed it up by a factor of 2.
Here's what it looks like (to confirm that the ts
got updated correctly).
I think it sounds better speeded up. In fact, I wonder if we are playing the original at the right speed.