




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This book is the digital signal processing in python
Typology: Papers
1 / 126
This page cannot be seen from the preview
Don't miss anything!





























































































Version 0.9.
Copyright © 2014 Allen B. Downey.
Green Tea Press 9 Washburn Ave Needham MA 02492
Permission is granted to copy, distribute, and/or modify this document under the terms of the Creative Commons Attribution-NonCommercial 3.0 Unported License, which is available at http://creativecommons.org/ licenses/by-nc/3.0/.
The premise of this book (and the other books in the Think X series) is that if you know how to program, you can use that skill to learn other things. I am writing this book because I think the conventional approach to digital signal processing is backward: most books (and the classes that use them) present the material bottom-up, starting with mathematical abstractions like pha- sors.
With a programming-based approach, I can go top-down, which means I can present the most important ideas right away. By the end of the first chapter, you can decompose a sound into its harmonics, modify the har- monics, and generate new sounds.
The code and sound samples used in this book are available from https: //github.com/AllenDowney/ThinkDSP. Git is a version control system that allows you to keep track of the files that make up a project. A collection of files under Git’s control is called a repository. GitHub is a hosting service that provides storage for Git repositories and a convenient web interface.
The GitHub homepage for my repository provides several ways to work with the code:
0.1. Using the code vii
If you have a suggestion or correction, please send email to [email protected]. If I make a change based on your feed- back, I will add you to the contributor list (unless you ask to be omitted).
If you include at least part of the sentence the error appears in, that makes it easy for me to search. Page and section numbers are fine, too, but not as easy to work with. Thanks!
viii Chapter 0. Preface
A signal is a representation of a quantity that varies in time, or space, or both. That definition is pretty abstract, so let’s start with a concrete exam- ple: sound. Sound is variation in air pressure. A sound signal represents variations in air pressure over time.
A microphone is a device that measures these variations and generates an electrical signal that represents sound. A speaker is a device that takes an electrical signal and produces sound. Microphones and speakers are called transducers because they transduce, or convert, signals from one form to another.
This book is about signal processing, which includes processes for synthe- sizing, transforming, and analyzing signals. I will focus on sound signals, but the same methods apply to electronic signals, mechanical vibration, and signals in many other domains.
They also apply to signals that vary in space rather than time, like eleva- tion along a hiking trail. And they apply to signals in more than one di- mension, like an image, which you can think of as a signal that varies in two-dimensional space. Or a movie, which is a signal that varies in two- dimensional space and time.
But we start with simple one-dimensional sound.
The code for this chapter is in sounds.py, which is in the repository for this book (see Section 0.1).
2 Chapter 1. Sounds and signals
0.000 0.001 0.002 0.003time (s) 0.004 0.005 0.
Figure 1.1: Segment from a recording of a tuning fork.
We’ll start with periodic signals , which are signals that repeat themselves after some period of time. For example, if you strike a tuning fork, it vi- brates and generates sound. If you record that sound and plot the trans- duced signal, it looks like Figure 1.1.^1
This signal is similar to a sinusoid, which means it has the same shape as the trigonometric sine function.
You can see that this signal is periodic. I chose the duration to show three full periods, also known as cycles. The duration of each cycle is about 2. ms.
The frequency of a signal is the number of cycles per second, which is the inverse of the period. The units of frequency are cycles per second, or Hertz , abbreviated “Hz”.
The frequency of this signal is about 439 Hz, slightly lower than 440 Hz, which is the standard tuning pitch for orchestral music. The musical name of this note is A, or more specifically, A4. If you are not familiar with “scientific pitch notation”, the numerical suffix indicates which octave the note is in. A4 is the A above middle C. A5 is one octave higher. See http://en.wikipedia.org/wiki/Scientific_pitch_notation.
A tuning fork generates a sinusoid because the vibration of the tines is a
(^1) I got this recording from http://www.freesound.org/people/zippi1/sounds/ 18871/.
4 Chapter 1. Sounds and signals
(^00 2000 4000) frequency (Hz) 6000 8000 10000 12000
500
1000
1500
2000
2500
3000
3500
4000
amplitude density
Figure 1.3: Spectrum of a segment from the violin recording.
For example, Figure 1.3 shows the spectrum of the violin recording in Fig- ure 1.2. The x-axis is the range of frequencies that make up the signal. The y-axis shows the strength of each frequency component.
The lowest frequency component is called the fundamental frequency. The fundamental frequency of this signal is near 440 Hz (actually a little lower, or “flat”).
In this signal the fundamental frequency has the largest amplitude, so it is also the dominant frequency. Normally the perceived pitch of a sound is determined by the fundamental frequency, even if it is not dominant.
The other spikes in the spectrum are at frequencies 880, 1320, 1760, and 2200, which are integer multiples of the fundamental. These components are called harmonics because they are musically harmonious with the fun- damental:
These harmonics make up the notes of an A major chord, although not all in the same octave. Some of them are only approximate because the notes
(^3) If you are not familiar with musical intervals like "major fifth”, see https://en. wikipedia.org/wiki/Interval_(music).
1.3. Signals 5
that make up Western music have been adjusted for equal temperament (see http://en.wikipedia.org/wiki/Equal_temperament).
Given the harmonics and their amplitudes, you can reconstruct the signal by adding up sinusoids. Next we’ll see how.
I wrote a Python module called thinkdsp that contains classes and functions for working with signals and spectrums. 4 You can download it from http: //think-dsp.com/thinkdsp.py.
To represent signals, thinkdsp provides a class called Signal, which is the parent class for several signal types, including Sinusoid, which represents both sine and cosine signals.
thinkdsp provides functions to create sine and cosine signals:
cos_sig = thinkdsp.CosSignal(freq=440, amp=1.0, offset=0) sin_sig = thinkdsp.SinSignal(freq=880, amp=0.5, offset=0)
freq is frequency in Hz. amp is amplitude in unspecified units where 1.0 is generally the largest amplitude we can play.
offset is a phase offset in radians. Phase offset determines where in the period the signal starts (that is, when t=0). For example, a cosine signal with offset=0 starts at cos 0, which is 1. With offset=pi/2 it starts at cos π /2, which is 0. A sine signal with offset=0 also starts at 0. In fact, a cosine signal with offset=pi/2 is identical to a sine signal with offset=0.
Signals have an add method, so you can use the + operator to add them:
mix = sin_sig + cos_sig
The result is a SumSignal, which represents the sum of two or more signals.
A Signal is basically a Python representation of a mathematical function. Most signals are defined for all values of t, from negative infinity to infinity.
You can’t do much with a Signal until you evaluate it. In this context, “eval- uate” means taking a sequence of ts and computing the corresponding val- ues of the signal, which I call ys. I encapsulate ts and ys in an object called a Wave.
(^4) In Latin the plural of “spectrum” is “spectra”, but since I am not writing in Latin, I generally use standard English plurals.
1.4. Reading and writing Waves 7
period = mix.period segment = wave.segment(start=0, duration=period*3)
period is a property of a Signal; it returns the period in seconds.
start and duration are in seconds. This example copies the first three pe- riods from mix. The result is a Wave object.
If we plot segment, it looks like Figure 1.4. This signal contains two fre- quency components, so it is more complicated than the signal from the tun- ing fork, but less complicated than the violin.
thinkdsp provides read_wave, which reads a WAV file and returns a Wave:
violin_wave = thinkdsp.read_wave('violin1.wav')
And Wave provides write, which writes a WAV file:
wave.write(filename='example1.wav')
You can listen to the Wave with any media player that plays WAV files. On UNIX systems, I use aplay, which is simple, robust, and included in many Linux distributions.
thinkdsp also provides play_wave, which runs the media player as a sub- process:
thinkdsp.play_wave(filename='example1.wav', player='aplay')
It uses aplay by default, but you can provide another player.
Wave provides make_spectrum, which returns a Spectrum:
spectrum = wave.make_spectrum()
And Spectrum provides plot:
spectrum.plot() thinkplot.show()
thinkplot is a module I wrote to provide wrappers around some of the functions in pyplot. You can download it from http://think-dsp.com/ thinkplot.py. It is also included in the Git repository for this book (see Section 0.1).
Spectrum provides three methods that modify the spectrum:
8 Chapter 1. Sounds and signals
This example attenuates all frequencies above 600 by 99%:
spectrum.low_pass(cutoff=600, factor=0.01)
Finally, you can convert a Spectrum back to a Wave:
wave = spectrum.make_wave()
At this point you know how to use many of the classes and functions in thinkdsp, and you are ready to do the exercises at the end of the chapter. In Chapter 2.4 I explain more about how these classes are implemented.
Before you begin this exercises, you should download the code for this book, following the instructions in Section 0.1.
Exercise 1.1 If you have IPython, load chap01.ipynb, read through it, and run the examples. You can also view this notebook at http://tinyurl.com/ thinkdsp01.
Go to http://freesound.org and download a sound sample that include music, speech, or other sounds that have a well-defined pitch. Select a seg- ment with duration 0.5 to 2 seconds 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 harmon- ics. 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?
Exercise 1.2 Synthesize a wave by creating a spectrum with arbitrary har- monics, inverting it, and listening. What happens as you add frequency components that are not multiples of the fundamental?