This notebook contains code examples from Chapter 11: Modulation and sampling
Copyright 2015 Allen Downey
Special thanks to my colleague Siddhartan Govindasamy; the sequence of topics in this notebook is based on material he developed for Signals and Systems at Olin College, which he and Oscar Mur-Miranda and I co-taught in Spring 2015.
# Get thinkdsp.py import os if not os.path.exists('thinkdsp.py'): !wget https://github.com/AllenDowney/ThinkDSP/raw/master/code/thinkdsp.py
import numpy as np import matplotlib.pyplot as plt from thinkdsp import decorate
To demonstrate the effect of convolution with impulses, I'll load a short beep sound.
if not os.path.exists('253887__themusicalnomad__positive-beeps.wav'): !wget https://github.com/AllenDowney/ThinkDSP/raw/master/code/253887__themusicalnomad__positive-beeps.wav
from thinkdsp import read_wave wave = read_wave('253887__themusicalnomad__positive-beeps.wav') wave.normalize() wave.plot()
Here's what it sounds like.
And here's a sequence of 4 impulses with diminishing amplitudes:
from thinkdsp import Impulses imp_sig = Impulses([0.005, 0.3, 0.6, 0.9], amps=[1, 0.5, 0.25, 0.1]) impulses = imp_sig.make_wave(start=0, duration=1.0, framerate=wave.framerate) impulses.plot() decorate(xlabel='Time (s)')
If we convolve the wave with the impulses, we get 4 shifted, scaled copies of the original sound.
convolved = wave.convolve(impulses) convolved.plot()
And here's what it sounds like.
The previous example gives some insight into how AM works.
First I'll load a recording that sounds like AM radio.
if not os.path.exists('105977__wcfl10__favorite-station.wav'): !wget https://github.com/AllenDowney/ThinkDSP/raw/master/code/105977__wcfl10__favorite-station.wav
wave = read_wave('105977__wcfl10__favorite-station.wav') wave.unbias() wave.normalize() wave.make_audio()
Here's what the spectrum looks like:
spectrum = wave.make_spectrum() spectrum.plot() decorate(xlabel='Frequency (Hz)')
For the following examples, it will be more useful to look at the full spectrum, which includes the negative frequencies. Since we are starting with a real signal, the spectrum is always symmetric.
spectrum = wave.make_spectrum(full=True) spectrum.plot() decorate(xlabel='Frequency (Hz)')
Amplitude modulation works my multiplying the input signal by a "carrier wave", which is a cosine, 10 kHz in this example.
from thinkdsp import CosSignal carrier_sig = CosSignal(freq=10000) carrier_wave = carrier_sig.make_wave(duration=wave.duration, framerate=wave.framerate)
* operator performs elementwise multiplication.
modulated = wave * carrier_wave
The result sounds pretty horrible.