ThinkDSP
This notebook contains solutions to exercises in Chapter 2: Harmonics
Copyright 2015 Allen Downey
Exercise
A sawtooth signal has a waveform that ramps up linearly from -1 to 1, then drops to -1 and repeats. See http://en.wikipedia.org/wiki/Sawtooth_wave
Write a class called SawtoothSignal
that extends Signal
and provides evaluate
to evaluate a sawtooth signal.
Compute the spectrum of a sawtooth wave. How does the harmonic structure compare to triangle and square waves?
Solution
My solution is basically a simplified version of TriangleSignal.
Here's what it sounds like:
And here's what the spectrum looks like:
Compared to a square wave, the sawtooth drops off similarly, but it includes both even and odd harmonics. Notice that I had to cut the amplitude of the square wave to make them comparable.
Compared to a triangle wave, the sawtooth doesn't drop off as fast.
Specifically, the harmonics of the triangle wave drop off in proportion to , while the sawtooth drops off like .
Exercise
Make a square signal at 1100 Hz and make a wave that samples it at 10000 frames per second. If you plot the spectrum, you can see that most of the harmonics are aliased. When you listen to the wave, can you hear the aliased harmonics?
Solution
Here's the square wave:
Here's what the spectrum looks like:
The fundamental and the first harmonic are in the right place, but the second harmonic, which should be 5500 Hz, is aliased to 4500 Hz. The third, which should be 7700 Hz, is aliased to 2300 Hz, and so on.
When you listen to the wave, you can hear these aliased harmonics as a low tone.
The tone I hear most prominently is at 300 Hz. If you listen to this 300 Hz sine wave, you might hear what I mean.
Exercise
If you have a spectrum object, spectrum
, and print the first few values of spectrum.fs
, you’ll see that they start at zero. So spectrum.hs[0]
is the magnitude of the component with frequency 0. But what does that mean?
Try this experiment:
Make a triangle signal with frequency 440 and make a Wave with duration 0.01 seconds. Plot the waveform.
Make a Spectrum object and print
spectrum.hs[0]
. What is the amplitude and phase of this component?Set
spectrum.hs[0] = 100
. Make a Wave from the modified Spectrum and plot it. What effect does this operation have on the waveform?
Solution
Here's the triangle wave:
The first element of the spectrum is a complex number close to zero.
If we add to the zero-frequency component, it has the effect of adding a vertical offset to the wave.
The zero-frequency component is the total of all the values in the signal, as we'll see when we get into the details of the DFT. If the signal is unbiased, the zero-frequency component is 0. In the context of electrical signals, the zero-frequency term is called the DC offset; that is, a direct current offset added to an AC signal.
Exercise
Write a function that takes a Spectrum as a parameter and modifies it by dividing each element of hs by the corresponding frequency from fs. Test your function using one of the WAV files in the repository or any Wave object.
Compute the Spectrum and plot it.
Modify the Spectrum using your function and plot it again.
Make a Wave from the modified Spectrum and listen to it. What effect does this operation have on the signal?
Solution
Here's my version of the function:
Here's a triangle wave:
Here's what the before and after look like. I scaled the after picture to make it visible on the same scale.
The filter clobbers the harmonics, so it acts like a low pass filter.
Here's what it sounds like:
The triangle wave now sounds almost like a sine wave.
Exercise
The triangle and square waves have odd harmonics only; the sawtooth wave has both even and odd harmonics. The harmonics of the square and sawtooth waves drop off in proportion to ; the harmonics of the triangle wave drop off like . Can you find a waveform that has even and odd harmonics that drop off like ?
Hint: There are two ways you could approach this: you could construct the signal you want by adding up sinusoids, or you could start with a signal that is similar to what you want and modify it.
Solution
One option is to start with a sawtooth wave, which has all of the harmonics we need:
Here's what the spectrum looks like. The harmonics drop off like .
If we apply the filter we wrote in the previous exercise, we can make the harmonics drop off like .
Here's what it sounds like:
And here's what the waveform looks like.
It's an interesting shape, but not easy to see what its functional form is.
Another approach is to add up a series of cosine signals with the right frequencies and amplitudes.
Here's what the spectrum looks like:
Here's what it sounds like:
And here's what the waveform looks like.
If those look to you like parabolas, you might be right. thinkdsp
provides ParabolicSignal
, which computes parabolic waveforms.
Here's what the waveform looks like:
A parabolic signal has even and odd harmonics which drop off like :