ThinkDSP

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.

License: Creative Commons Attribution 4.0 International

In [1]:
# Get thinkdsp.py

import os

if not os.path.exists('thinkdsp.py'):
    !wget https://github.com/AllenDowney/ThinkDSP/raw/master/code/thinkdsp.py
In [2]:
import numpy as np
import matplotlib.pyplot as plt

from thinkdsp import decorate

Convolution with impulses

To demonstrate the effect of convolution with impulses, I'll load a short beep sound.

In [3]:
if not os.path.exists('253887__themusicalnomad__positive-beeps.wav'):
    !wget https://github.com/AllenDowney/ThinkDSP/raw/master/code/253887__themusicalnomad__positive-beeps.wav
In [4]:
from thinkdsp import read_wave

wave = read_wave('253887__themusicalnomad__positive-beeps.wav')
wave.normalize()
wave.plot()

Here's what it sounds like.

In [5]:
wave.make_audio()
Out[5]:

And here's a sequence of 4 impulses with diminishing amplitudes:

In [6]:
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.

In [7]:
convolved = wave.convolve(impulses)
convolved.plot()

And here's what it sounds like.

In [8]:
convolved.make_audio()
Out[8]:

Amplitude modulation (AM)

The previous example gives some insight into how AM works.

First I'll load a recording that sounds like AM radio.

In [9]:
if not os.path.exists('105977__wcfl10__favorite-station.wav'):
    !wget https://github.com/AllenDowney/ThinkDSP/raw/master/code/105977__wcfl10__favorite-station.wav
In [10]:
wave = read_wave('105977__wcfl10__favorite-station.wav')
wave.unbias()
wave.normalize()
wave.make_audio()
Out[10]:

Here's what the spectrum looks like:

In [11]:
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.

In [12]:
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.

In [13]:
from thinkdsp import CosSignal

carrier_sig = CosSignal(freq=10000)
carrier_wave = carrier_sig.make_wave(duration=wave.duration, framerate=wave.framerate)

The * operator performs elementwise multiplication.

In [14]:
modulated = wave * carrier_wave

The result sounds pretty horrible.

In [15]:
modulated.make_audio()
Out[15]: