ThinkDSP
This notebook contains code examples from Chapter 2: Harmonics
Copyright 2015 Allen Downey
Waveforms and harmonics
Create a triangle signal and plot a 3 period segment.
Make a wave and play it.
Compute its spectrum and plot it.
Make a square signal and plot a 3 period segment.
Make a wave and play it.
Compute its spectrum and plot it.
Create a sawtooth signal and plot a 3 period segment.
Make a wave and play it.
Compute its spectrum and plot it.
Aliasing
Make a cosine signal at 4500 Hz, make a wave at framerate 10 kHz, and plot 5 periods.
Make a cosine signal at 5500 Hz, make a wave at framerate 10 kHz, and plot the same duration.
With framerate 10 kHz, the folding frequency is 5 kHz, so a 4500 Hz signal and a 5500 Hz signal look exactly the same.
Make a triangle signal and plot the spectrum. See how the harmonics get folded.
Amplitude and phase
Make a sawtooth wave.
Play it.
Extract the wave array and compute the real FFT (which is just an FFT optimized for real inputs).
Compute the frequencies that match up with the elements of the FFT.
Plot the magnitudes vs the frequencies.
Plot the phases vs the frequencies.
What does phase sound like?
Shuffle the phases.
Put the shuffled phases back into the spectrum. Each element in hs
is a complex number with magitude and phase , with which we can compute
Convert the spectrum back to a wave (which uses irfft).
Play the wave with the shuffled phases.
For comparison, here's the original wave again.
Although the two signals have different waveforms, they have the same frequency components with the same amplitudes. They differ only in phase.
Aliasing interaction
The following interaction explores the effect of aliasing on the harmonics of a sawtooth signal.