Discrete time processing, Papers of Electrical Engineering

This book is the digital signal processing in python

Typology: Papers

2020/2021

Uploaded on 04/21/2021

g-mahima
g-mahima 🇮🇳

1 document

1 / 126

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Think DSP
Digital Signal Processing in Python
Version 0.9.8
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Discrete time processing and more Papers Electrical Engineering in PDF only on Docsity!

Think DSP

Digital Signal Processing in Python

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/.

Preface

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.

0.1 Using the code

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:

  • You can create a copy of my repository on GitHub by pressing the Fork button. If you don’t already have a GitHub account, you’ll need to create one. After forking, you’ll have your own repository on GitHub that you can use to keep track of code you write while working on this book. Then you can clone the repo, which means that you make a copy of the files on your computer.
  • Or you could clone my repository. You don’t need a GitHub account to do this, but you won’t be able to write your changes back to GitHub.

0.1. Using the code vii

Contributor List

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!

  • Before I started writing, my thoughts about this book benefited from con- versations with Boulos Harb at Google and Aurelio Ramos, formerly at Har- monix Music Systems.
  • During the Fall 2013 semester, Nathan Lintz and Ian Daniher worked with me on an independent study project and helped me with the first draft of this book.
  • On Reddit’s DSP forum, the anonymous user RamjetSoundwave helped me fix a problem with my implementation of Brownian Noise. And andodli found a typo.
  • In Spring 2015 I had the pleasure of teaching this material along with Prof. Oscar Mur-Miranda and Prof. Siddhartan Govindasamy. Both made many suggestions and corrections.

viii Chapter 0. Preface

Chapter 1

Sounds and signals

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.

1.1 Periodic signals

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:

  • 880 is the frequency of A5, one octave higher than the fundamental.
  • 1320 is approximately E6, which is a major fifth^3 above A5.
  • 1760 is A6, two octaves above the fundamental.
  • 2200 is approximately C]7, which is a major third above A6.

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.

1.3 Signals

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.

1.4 Reading and writing Waves

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.

1.5 Spectrums

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

  • low_pass applies a low-pass filter, which means that components above a given cutoff frequency are attenuated (that is, reduced in mag- nitude) by a factor.
  • high_pass applies a high-pass filter, which means that it attenuates components below the cutoff.
  • band_stop attenuates components in the band of frequencies between two cutoffs.

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.

1.6 Exercises

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?