Signal Processing

Download as pdf or txt
Download as pdf or txt
You are on page 1of 367

J U H A V I E R I N E N , J Ø R N O L AV J E N S E N

SIGNAL PROCESSING

UNIVERSITY OF TROMSØ
Copyright © 2024 Juha Vierinen, Jørn Olav Jensen

published as lecture notes.

http://kaira.uit.no/fys2006

August 2024
Contents

Syllabus 7

Introduction 9

Python 15

Complex Algebra 23

Signals and Systems 33

Elementary Signals 49

Sinusoidal Signals 55

Fourier Series 75

Programming Assignment 1 109

Fourier Transform 111

Discrete-time Signals 135


4

Linear Time-invariant Systems 165

Frequency Response 195

Programming Assignment 2 213

Discrete-time Fourier Transform (DTFT) 219

Ideal and Tapered Filters 229

Time-frequency Uncertainty Principle 245

Discrete Fourier Transform 257

Spectral Analysis 275

Arbitrary Frequency Response Filters 289

Programming Assignment 3 303

Z-transform 313

Infinite Impulse Response Filters 339

Bibliography 363
5

I’d like to thank the following people1 for corrections and suggestions that 1
or their aliases, as some may have
chosen not to use their real names
have improved these lecture notes over the years: Björn Gustavsson, Patrick when participating in the course
Guio, Mikkel Isak Gaup, Adrian Sletten32 , Rikke Bjarnesen Andresen, commentary on Perusall.

Jostein Henriksen7 , Daniel Nordahl Jørgensen, Ivan Mikheev, Oskar


Marthinussen, Iver Martinsen, Marit Breimo, Sigurd Haugse, Ragnar
Helgaas, Sondre Thomassen, Sofie Svenøe, Sigurd Yngvar Ekern, Vetle
Hofsøy-Woie, Øyvind Alexander Larssen, Erlend Thorkildsen, Amalie
Gjelsvik, Tommy Ryan, Eivind Dragset, Attiqa Abrar, Daniel Breiland
Teigen, Sigrid Holm, Åse Fauske, Yvonne Johansen, Frank Martin Fossland,
Runar Folke-Olsen, Morten Paulsen, Teodor Skotnes, Martin Stave16 ,
Kristine Rein3 , Anna Odh2 , Vegar Einarsen1 , Christian Salomonsen5 ,
Jonas Riise1 , Jørn Jensen63 , Anton Zyranov1 , Nikolai Anfeltmo1 , Håkon
Johansen10 , Kian Sadeghi3 , Johannes Bjørnhaug2 , Ines Seeliger2 , Dana
King3 , Emil Jettli1 , Sigurd Hanssen1 , Liza Liz1 , Sebastian Iversen1 , Daniel
Johansen1 , Tobias W. Tobiassen1 , and Johanna Mankova Buseth1 . I’d also
like to thank countless others whose names I have forgotten to mention. A
number indicates how many corrections were found by each person. This is a
very rough estimate based on my somewhat incomplete bookkeeping, which I
only started midway through this project. All mistakes are purely mine.
Syllabus
8

Date Topic Special notes


19.08. Intro, Python, Complex
Algebra
20.08. Signals and Systems, Ele-
mentary Signals
26.08. Sinusoidal Signals
27.08. Fourier Series 1
02.09. Fourier Series 2
03.09. Fourier Transform 1
09.09. Fourier Transform 2 Programming Assignment
1, Jørn Olav Jensen
10.09. Discrete-time Signals Jørn Olav Jensen
16.09. Sampling
19.09. Linear Time-Invariant Sys- Note unusual time!
tems
23.09. Frequency Response
24.09. Discrete-time Fourier Trans-
form
30.09. No lecture, Høstferie
01.10. No lecture, Høstferie Programming assignment 2
07.10. Ideal and tapered filters
08.10. Time-frequency uncertainty
principle
14.10. Discrete Fourier Transform
15.10. Spectral Analysis
21.10. Arbitrary Frequency Re- Note unusual time!
sponse Filter
22.10 Laplace Transform
28.10. Z-Transform and Finite
Impulse Response Filters
31.10. Z-Transform and Infinite
Impulse Response Filters
04.11.
05.11.
11.11. Programming assignment 3
12.11.
18.11.
19.11.
25.11. Exam prep
26.11. Exam prep
28.11. Final Exam
Introduction

Many aspects of the life of a modern human depend on the


theory of signal processing. Here are a few examples. When you are
listening to a digital audio recording of music, displaying an image
on your computer, or using a wireless internet connection, you are
relying on mathematical concepts that are taught in a basic course on
signal processing. These same concepts are encountered when, e.g.,
solving differential equations, applying deep learning techniques,
measuring the gravitational wave signature of colliding neutron
stars with a laser interferometer, or making an image of a black hole
using a worldwide network of radio telescopes. The theory of signals
and systems is a collection of applied mathematical tools that have Figure 1: Light can be viewed as a
superposition of electromagnetic
important applications in science, technology, and engineering. The waves with different amplitudes,
term signal processing and the terminology of signal processing have phases, and frequencies. This can be
investigated in practice with the help
origins in electrical engineering. of a prism or a diffraction grating. My
hope is that after taking this course,
you will metaphorically be able to
“see” arbitrary signals as a sum of
Let’s look more closely at the audio example. When you
periodic harmonic functions or spectral
listen to a recording of music, you are probably relying on a digital components.
representation of the acoustic signal that is compressed. With the help
of signal processing mathematics, the audio signal is encoded in a
special way, which allows the size of the file to be greatly reduced.
This lowers the minimum internet speed required to stream the
audio without annoying interruptions and allows 10 to 100 times
more audio files to be stored on a digital storage device than without
compression.
Audio compression algorithms often rely on the fact that the
frequency domain or spectral representation of a signal is far more
sparse than the time domain representation. This means that we can
approximate the original audio signal using a sum of a relatively
small number of periodic sinusoidal signal components. Let’s say
that the original audio signal is x (t), which represents air pressure as
a function of time. Using the Fourier series we can use the following
10

type of sum to approximate x (t):


N
x (t) ≈ ∑ An sin(ωn t + ϕn ) , (1)
n =1

with each sinusoidal signal only described by three parameters: an


amplitude An ∈ R, a phase ϕn ∈ R, and an angular frequency
ωn ∈ R.
If the value of N in Equation 1 is sufficiently small, then the
amount of information required to represent the audio signal as
a sum of sinusoids can be significantly less than what would be re-
quired if the signal was just sampled with a fixed sample spacing
and the amplitude of the waveform at each sampled point would be
stored.
Here’s an example. Let’s say that you want to store 1024 samples2
of audio signal. With the signal represented using 44100 samples
Figure 2: Leonhard Euler. Credit: Jakob
per second, this would be approximately 0.023 seconds of signal. Handmann (1753)
You’d need to store 1024 numbers without compression. With 16 2
One sample is a number representing
bits per sample, this audio signal would require 16384 bits of storage the audio signal amplitude at one
instant of time
space. However, if the signal is sufficiently well represented using
N = 10 sinusoidal components, you’d only need to store 30 numbers
to retain the information about the phase, amplitude and frequency
of each sinusoidal component. This is only approximately 3% of the
original data, or 480 bits of storage space at 16 bits per number3 . 3
One "bit" of information is an answer
Most of the videos, images, and music that you access over the to a yes or no question. One bit would
allow you to say that a number is either
internet is compressed in this manner. Without spectral compression 0 or 1. A two bit signal would allow
techniques, the internet would most likely need to have at least you to say that a number is either 0, 1, 2,
or 3.
10 times more capacity than it currently has, in order to be able to
provide us with the same amount of entertainment. For example, the
MP3 audio compression standard and the JPEG image compression
standard both utilize spectral compression methods.

We often use more general complex-valued sinusoidal


functions instead of real-valued ones when describing the theory of
signal processing. These are defined as follows:

Aeiϕ eiωt = A[cos(ωt + ϕ) + i sin(ωt + ϕ)] . (2)

Just like a real-valued sinusoid, the complex sinusoid is also rep-


resented using an amplitude A ∈ R, phase ϕ ∈ R, and angular
frequency ω ∈ R. Note that the above equation is just an application
of Euler’s formula4 , which is probably the most frequently occurring
equation in signal processing. If you master this equation, you’ll have
covered already most of this course! 4
The famous physicist Richard Feyn-
If you are not familiar with complex algebra, Equation 2 might man had this to say about Euler’s
formula: “We summarize with this, the
at first seem intractable. But don’t worry. If you have a hard time most remarkable formula in mathe-
matics. This is our jewel.” (Feynman
Lectures on Physics, Vol I). He had
clearly benefited greatly from this
formula.
11

grasping the function Aeiϕ eiωt , just think of it as a periodic function


like A cos(ωt + ϕ), but with a real and imaginary sinusoidal compo-
nent. The reason for using the complex sinusoidal signal instead of
the real-valued one is that most of the mathematical calculations are
greatly simplified.
=

A general frequency domain decomposition of signals is +


known as the Fourier transform. This is just an extended version +
of the sum of sinusoidal signals given in Equation 1. The reverse and
+
forward Fourier transforms allow transforming a signal between a
time domain and a frequency domain representation: +
Z ∞ +
1
x (t) = x̂ (ω )eiωt dω (3)
2π −∞ +
Z ∞
−iωt
x̂ (ω ) = x (t)e dt . (4)
−∞

The Fourier transform allows nearly any function to be expressed as Figure 3: Spectral decomposition of
an infinite sum of complex sinusoidal functions, or in other words, a narrow pulse that consists of seven
sinusoidal signals.
an integral. This is one of the main theoretical foundations of sig-
nal processing. In this equation, the term x (t) is the time domain
representation of the signal, and x̂ (ω ) is the frequency domain rep-
resentation of the signal. The function x̂ (ω ) simply tells you what
is the phase ϕ and amplitude A of each spectral component of the
signal, assuming that it consists of infinitely many sinusoidal compo-
nents.
Figure 3 shows a sketch of the basic idea behind the Fourier de-
composition of signals. By adding together the seven individual
sinusoidal signals on the bottom of the figure, we obtain the more
pulse-like signal shown on the top of the figure. Conversely, the
pulse-like signal on the top can therefore be decomposed into seven
sinusoidal signals. This is in essence the idea behind the forward
Figure 4: Jean-Baptiste Joseph Fourier
and reverse Fourier transform. Had we continued to sum together
higher and higher frequency sinusoidal signals in the same manner,
we would have ended up with an infinitely narrow spike δ(t), which
is called the Dirac delta function. This special function has many
important theoretical uses in signal processing.
The Fourier transform is named after Jean-Baptiste Joseph Fourier,
who is shown in Figure 4. He was the first to come up with the idea
of expressing a function as a sum of sinusoidal signals, or in other
words, spectral components. This occurred in the 1820s while he was
studying heat conduction. Using sinusoidal valued spectral compo-
nents to represent the distribution of heat within a body allowed him
to analytically solve the heat equation, because solving differential
equations is relatively straightforward for sinusoidal functions. Even
though this brilliant mathematical discovery occurred 200 years ago,
12

we are still coming up with new ways to apply it5 . I would say that 5
For example, your smartphone relies
the ability to divide nearly any function you encounter into a super- on the Fourier transform in several
ways for enabling transfer of data over
position of simple elementary sinusoidal waves (the concept of the radio waves, and for displaying pictures
Fourier transform) is the most useful idea you will learn from a basic and videos, and playing audio files.
course on signal processing.
If you are not yet completely sure what the equations in this chap-
ter mean, that is perfectly fine. Learning what the equations are and

Dependent variable x (t)


how to apply them in practice is the purpose of this course. If you
are already familiar with complex sinusoidal signals and the Fourier
transform, then you may be able to skip much of the introductory
material and spend more time on the application examples.

Signal processing is an interdisciplinary field, where


Independent variable t
mathematics, physics, computer science, and engineer-
ing intersect. The theoretical aspects of signal processing are Figure 5: Continuous-time signal.
essentially just applied mathematics, which often have already been

Dependent variable x [n]


introduced hundreds of years ago. However, applying the theory to
solve real world problems involves programming or building electri-
cal circuits. This aspect of signal processing is technology driven, and
is continuously evolving as technology advances. I believe that it is
important to be knowledgeable of both of these aspects in order to
successfully apply signal processing to real world problems.
These lecture notes aim to teach theoretical and applied aspects
Independent variable n
of signal processing through mathematical derivations and program-
ming examples. The mathematics we will use is not rigorous or Figure 6: Discrete-time signal.
generalized in a sense that it would appeal to a mathematician. Simi-
larly, we will not emphasize aspects of proper software engineering.
Instead, the aim is to provide programming examples that are as
simple and as easy to understand as possible.
My hope is that these notes will take an approach to signal pro-
cessing which is appealing to an undergraduate level physics or
engineering student. I do not expect you to have ever encountered
a Fourier transform before, to be familiar with complex algebra, or
even be proficient in programming.
It is natural to divide the topic of signal processing into two main
categories: continuous-time and discrete-time. The former deals with
signals that are continuous functions. The latter deals with signals
that are sequences of numbers, which is more natural for signals that
reside on digital computers and that are used for digital signal pro-
cessing. The latter type of signal processing has gained in importance
for quite a long time due to the exponential increase in computing
and storage capacity, which has made it possible to perform more
and more signal processing tasks digitally. The theory for continuous-
time signals lays the theoretical foundation for also the discrete-time
13

signal processing, so it is still important to teach this part.

This compendium covers the topics in the syllabus of the under-


graduate signal processing course “FYS-2006” held at University
of Tromsø. While many other universities will have a separate “sig-
nals and systems” course that covers continuous-time signals, and a
separate course dedicated to “digital signal processing”, this course
combines both topics in one course.
These lecture notes are complete in terms of the topics that are
covered in the course “FYS-2006”. You are not required to obtain
any additional reading material. The reason why I went through the
trouble of typing out these notes is that there currently are no signal
processing textbooks that cover continuous-time and discrete-time
signal processing, and use the Python programming language for
examples. If you wish to deepen your knowledge, there are several
published textbooks that you can use to complement this material.
One book that I recommend is “Applied Digital Signal Processing:
Theory and Practice” by D. G. Manolakis and V. K. Ingle.

You might be asking yourself: why should I learn about


signal processing? This may be in the minds of, especially those for
who have this as a mandatory course, and this is the primary reason
that you are attending it. I believe that the elementary concepts
taught in a basic course on this topic will provide you with the
tools to be a good scientist. You will encounter the mathematical
tools introduced in this course throughout science, technology, and
engineering, where knowledge of these tools can often be seen as
prerequisite background knowledge that allows you to understand
more advanced concepts.
Perhaps the most powerful practical tool you will learn during
this course is the Fast Fourier Transform (FFT), which is a specific
algorithm used to compute a discrete Fourier transformation. It
will allow you to perform a wide range of different calculations
efficiently, using techniques that are not trivial unless you understand
the basic concepts of signal processing. The FFT algorithm can be
used e.g., for spectral analysis, evaluation of a convolution operation,
to efficiently solve certain types of matrix operations, or to solve
differential equations. Any time when processing signals later on in
your life, there is a good chance that you will find the FFT algorithm
to be highly useful.
If you go on to work with e.g. machine learning, this course
will teach you the concept of convolution, which is a mathematical
Figure 7: Simplified diagram of a
operation that describes an arbitrary linear time-invariant system, convolutional neural network, where
and thus plays a major role in signal processing. You may have heard a convolution operation is one of the
fundamental components. Figure
adapted from [Maier et al., 2019].
14

of convolutional neural networks, right? This course will not teach


you anything about statistics or neural networks, but it will teach
you the concept of a convolution sum, which is often a fundamental
building block of image processing or deep learning algorithms6 . 6
Andreas Maier, Christopher Syben,
Tobias Lasser, and Christian Riess. A
gentle introduction to deep learning in
medical image processing. Zeitschrift für
Medizinische Physik, 29(2):86–101, 2019
Python

Figure 8: The Python programming


language is an open source language
We’ll demonstrate various signal processing topics using
that is increasingly popular for data
programming examples. Why Python and not some other language? science and signal processing.
Here are three justifications:

• It is free for everyone to use,

• there are a wide range of efficient numerical libraries available,

• and Python is nowadays the language that students are most


familiar with.

All the programming examples we’ll show in these lecture notes


will support Python 3. We will restrict ourselves to a bare minimum Figure 9: The NumPy package imple-
number of library dependencies. The main libraries you will need ments a large collection of numerical
routines that can be used for signal
are: numpy, scipy, and matplotlib. These should be readily available processing.
for nearly every operating system.
If you don’t know how to obtain Python for your computer, you
could try out the Anaconda distribution of Python. It provides a
relatively complete set of the most commonly used Python mod-
ules. You can obtain this distribution by visiting the anaconda web
Figure 10: The SciPy package contains
page: http://anaconda.org and downloading the installer for your a number of signal processing routines
platform. for Python.
The Anaconda package manager will allow you to install NumPy,
SciPy, and Matplotlib. Anaconda also comes with a Python devel-
opment environment as well. If you don’t have your mind set on
a specific programming editor yet, you could try out the Spyder
development environment that comes with Anaconda.
If you are using Linux, like I am most of the time, it is probably Figure 11: Matplotlib implements basic
plotting routines for Python.
easiest to just obtain Python using the system package manager. On
Ubuntu, you would install the necessary packages with the following
command:
sudo apt − g e t i n s t a l l python3 python3 −numpy python3 − s c i p y python3
−imageio python3 − m a t p l o t l i b ipython3

Listing 1: Installing Python on Ubuntu Linux


16

After installing Python and the required modules, I recommend


that you write a short test program to make sure that all the modules
are installed correctly. To test your Python installation, open up your
programming editor and write a small Python program:
import m a t p l o t l i b . pyplot as p l t Figure 12: The Anaconda distribution is
import numpy as np currently one of the most popular dis-
import s c i p y . c o n s t a n t s as c tributions of the Python programming
language, offering nearly all existing
p r i n t ( " Hello World ! " ) open source modules.
# T e s t numpy and s c i p y .
p r i n t ( np . p i )
p r i n t ( c . pi )

s a m p l e _ r a t e = 4 4 1 0 0 . 0 # 44100 samples per second .


t = np . arange ( 1 0 0 ) /s a m p l e _ r a t e
c s i n = np . exp ( 1 j * 2 . 0 * np . p i * 4 4 0 . 0 * t ) # A 440 Hz s i g n a l .

p l t . p l o t ( t * 1 e3 , c s i n . r e a l , c o l o r = " blue " , l a b e l = r " $\mathrm { Re } \ { z


( t ) \} $ " )
p l t . p l o t ( t * 1 e3 , c s i n . imag , c o l o r = " red " , l a b e l = r " $\mathrm { Im } \ { z (
t ) \} $ " )

plt . x l a b e l ( " Time (ms) " )


plt . y l a b e l ( r " $z ( t ) =e ^{ i \omega t } $ " )
plt . legend ( )
plt . show ( )

Listing 2: 001_hello_world/hello.py

This test program prints out the standard hello world message, and
uses NumPy and SciPy to print the approximate value of π. The
program then plots a complex sinusoidal signal with the help of
NumPy and Matplotlib. If you can run this program without getting
any errors, you are all set. The output should look something like
this:

Figure 13: Output of the hello.py test


program.

Code examples that are spread throughout these lecture notes can be
downloaded from GitHub: https://github.com/jvierine/signal_
17

processing.git. The easiest way to access the code is to just visit this
page with your web browser. In order to download the source code
for the examples in these lecture notes, you can also use git on the
command line:
g i t c l o n e h t t p s :// github . com/ j v i e r i n e / s i g n a l _ p r o c e s s i n g

Listing 3: Obtaining the source code for the programming examples


with git

If you are using Anaconda, you will have to install git (if you don’t
have it already) by typing in
Figure 14: If you are not familiar with
conda i n s t a l l −c anaconda g i t git, you can easily download the code
from the GitHub webpage as a zip file
Listing 4: Obtaining the source code for the programming examples instead of using the git clone command.
with git

Then you can proceed with the command shown above.

Figure 15: Program examples from


these lecture notes are available on
GitHub.
18

Exercises: Python

These exercises are intended to get you started with programming


with Python and the NumPy and Matplotlib libraries. If you are
already familiar with these topics, you may want to skip these exer-
cises.

1. Obtain the source code for the examples by cloning the GitHub
repository for this course.

2. The Hello World program shown in Listing 2 plots a complex


sinusoidal signal.

a) Go on the NumPy website http://numpy.org, and find the


documentation of the functions included in the package.
b) Use this documentation to determine what numpy.arange, and
numpy.exp do.

c) Modify the code so that it plots a circle on the complex plane,


by plotting the real part of the complex sinusoidal signal csin
on the x-axis, and the imaginary part on the y-axis.
d) Explain why the real and imaginary component draw a circle
on the complex plane.

3. Use Python to calculate and print out the value of eiπ + 1. Note that
i in Python, is denoted with 1j.

4. The use of built-in NumPy functions will let you program effi-
ciently. The program shown in Listing 5 demonstrates the use of
NumPy functions to evaluate the Mandelbrot set with a slight
twist.
import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . c o n s t a n t s as c

x = np . l i n s p a c e ( − 2 . 5 , 1 . 5 , num=2000 , dtype=np . f l o a t 3 2 )
y = np . l i n s p a c e ( − 2 , 2 , num=2000 , dtype=np . f l o a t 3 2 )
cx , cy = np . meshgrid ( x , y )
c = cx + 1 j * cy

# F i g u r e out what i s t h e d a t a t y p e o f v a r i a b l e c
# t o make s ur e i t ’ s complex64 .
p r i n t ( c . dtype )

z = np . z e r o s ( c . shape , dtype=np . complex64 )

f o r _ i n range ( 1 2 ) :
z = z **2 + c

z [ np . i s n a n ( z ) ] = 0 . 0
z [ np . i s i n f ( z ) ] = 0 . 0
19

plt . imshow ( np . angle ( z ) , e x t e n t = [ − 2 . 5 , 1 . 5 , −2 , 2 ] , cmap= " hsv " )


plt . colorbar ( )
plt . x l a b e l ( r " $\mathrm { Re } \ { c \} $ " )
plt . y l a b e l ( r " $\mathrm { Im } \ { c \} $ " )
plt . show ( )

Listing 5: 001_hello_world/mystery.py

a) Run the program shown in Listing 5. You should see a plot like
the one shown in Figure 16.
b) Describe what each line of the program does by adding com-
ments to the code.
c) It is possible to specify the data type of any NumPy array
using the dtype attribute. There are two complex-valued
datatypes available in NumPy: complex64 and complex128.
What are the pros and cons of using the complex64 datatype
instead of the complex128 datatype?

Figure 16: The Mandelbrot set example


demonstrates the use of NumPy array
functions and complex numbers. The
plot shows the phase of complex
number z12 after 12 iterations of zn+1 ←
z2n + c, starting with z0 = 0.
20

Suggested solutions: Python

1. If you have git installed, run: git clone https://github.com/jvierine/signal_processing


in the terminal. If you don’t have git, you can follow the link and
there should be a download option available on GitHub.

2. a) Go here http://numpy.org to read documentation.


b) Reading the documentation you’ll find that np.arange creates a
NumPy array of evenly spaced numbers from 0 to the provided
number minus 1. In the case of the code here, the np.arange
function will sequence the numbers from 0 to 99 and return a
NumPy array containing these numbers. See documentation:
https://numpy.org/doc/stable/reference/generated/numpy.
arange.html. The documentation for np.exp can be found here
https://numpy.org/doc/stable/reference/generated/numpy.
exp.html. The function simply computes e x , where x can be
real or complex, or even a NumPy array. If so, a NumPy array
is returned.
c) Listing 6 now plots a circle.
import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . c o n s t a n t s as c

p r i n t ( " Hello World ! " )


# T e s t NumPy and SciPy .
p r i n t ( np . p i )
p r i n t ( c . pi )

s a m p l e _ r a t e = 4 4 1 0 0 . 0 # 44100 samples per second .


t = np . arange ( 1 0 1 ) /s a m p l e _ r a t e # <−− Added 101 t o o b t a i n
the f u l l c i r c l e .

c s i n = np . exp ( 1 j * 2 . 0 * np . p i * 4 4 0 . 0 * t ) # A 440 Hz s i g n a l .

# Make a x i s a s p e c t r a t i o equal f o r v i s u a l l y p l e a s i n g output


.
p l t . axes ( ) . s e t _ a s p e c t ( ’ equal ’ )

# P l o t a c i r c l e using t h e r e a l and imaginary p a r t o f e ^( i


*2* pi *440* t ) .
p l t . p l o t ( c s i n . r e a l , c s i n . imag , c o l o r = " blue " , l a b e l = " Unit
circle " )

p l t . x l a b e l ( r " $\cos (2\ p i 4 4 0 t ) $ " )


p l t . y l a b e l ( r " $\ s i n (2\ p i 4 4 0 t ) $ " )
p l t . legend ( )
# C a l l t h i s i f needed .
# p l t . show ( )

Listing 6: Code is modified to plot a circle

d) The unit circle can be described by the equation x2 + y2 = 1,


which is satisfied by x = cos(t) and y = sin(t).

Figure 17: Output of Listing 6


21

3. Listing 7 shows how to compute and print eiπ + 1 in Python. The


resulting output is 1.2246467991473532e-16j, which is not zero.
The reason for this is due to floating point errors. The value is very
close to 0, but not exactly 0.
import numpy as np

p r i n t ( np . exp ( 1 j * np . p i ) +1)

Listing 7: Computing eiπ + 1

4. The following is a solution for Exercise 4.

a) Running the code generates the plot shown in the exercise.


b) Adding comments in Python can be done using #, Listing 8
shows some comments.
# Import some l i b r a r i e s .
import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . c o n s t a n t s as c

# P a r t i t i o n t h e i n t e r v a l ( − 2 . 5 , 1 . 5 ) or ( − 2 , 2 ) i n t o 2000
numbers .
# Separated by t h e same d i s t a n c e , s t o r e t h e s e numbers as
32− b i t f l o a t i n g p o i n t numbers .
x = np . l i n s p a c e ( − 2 . 5 , 1 . 5 , num=2000 , dtype=np . f l o a t 3 2 )
y = np . l i n s p a c e ( − 2 , 2 , num=2000 , dtype=np . f l o a t 3 2 )

# C r e a t e a g r i d using x and y as t h e x− and y− a x i s .


cx , cy = np . meshgrid ( x , y )
# ^cx i s a copy o f x s t o r e d as a matrix with y number o f
rows ,
# same i s t r u e f o r cy , but opposite , t h e r e i s a copy o f y
and t h e r e i s x number o f rows .

# C r e a t e a complex plane g r i d using cx and cy .


c = cx + 1 j * cy

# F i g u r e out what i s t h e d a t a t y p e o f v a r i a b l e c
# t o make s ur e i t i s complex64 .
p r i n t ( c . dtype )

# C r e a t e a matrix o f z e r o s having t h e same dimension as c .


z = np . z e r o s ( c . shape , dtype=np . complex64 )

# Compute t h e sequence z_ { n+1} = z_ { n }^2 + c f o r n =


0 ,... ,11.
f o r _ i n range ( 1 2 ) :
z = z **2 + c

# S e t i n f i n i t i e s and NaN ( not a number ) t o 0 . 0 .


z [ np . i s n a n ( z ) ] = 0 . 0
z [ np . i s i n f ( z ) ] = 0 . 0

# P l o t t h e r e s u l t using a c o l o r g r a d i e n t .
p l t . imshow ( np . angle ( z ) , e x t e n t = [ − 2 . 5 , 1 . 5 , −2 , 2 ] , cmap= "
hsv " )
22

plt . colorbar ( )
plt . x l a b e l ( r " $\mathrm { Re } \ { c \} $ " )
plt . y l a b e l ( r " $\mathrm { Im } \ { c \} $ " )
plt . show ( )

Listing 8: Commented Python code

c) One can specify the datatype of a NumPy array. The data types
supported can be found in the documentation. These include:
complex64, complex128, float32, int32, among others. Pros
and cons with the complex64 and complex128 include: size and
accuracy. The complex64 takes less memory, but is less accurate,
while complex128 can store more accurate numbers, but takes
more memory.
Complex Algebra
Im

Complex algebra is found throughout signal processing. x = Re{z} z

In this chapter, we’ll briefly review the basics of this topic. Of pri-
mary importance is Euler’s formula, which will be used extensively
y = Im{z}

|
|z
throughout this course.

=
A
Euler’s formula relates an arbitrary complex number θ = ∠z Re
z ∈ C to an exponential function of the natural number e
as follows:

z = x + iy = Aeiθ = A[cos(θ ) + i sin(θ )] . (5)


Figure 18: The polar representation of a
This formula is useful, as it provides a relationship between the complex number z = x + iy = Aeiθ .

Cartesian and polar representation of a complex number.


p
In this formula, A = |z| = x2 + y2 is the absolute value of the
complex number z. This is sometimes called the magnitude or modulus
of z.
The angle θ can be obtained with simple geometry θ = tan−1 (y/x ).
The angle is also sometimes called the argument of z. We’ll use the
following notation to denote the argument of a complex number:
∠z = θ = tan−1 (y/x ). It is worth pointing out here is that it is
possible to add an integer multiple of 2π to θ and still get the same
complex number:
Aeiθ = Aei(θ +2πk) (6)
This is due to the fact that ei2πk = 1, where k ∈ Z is an arbitrary
integer.
The term i in Equation 5 is the imaginary number, which has

the following properties: i = −1 and i2 = −1. In engineering
and programming, the symbol j is also often used for the imaginary
number instead of i. I’ll use i, but you can use whichever notation
you prefer yourself.
The geometric representation of a complex number is shown
in Figure 18, which shows the real and imaginary components of
a complex number in a two-dimensional coordinate system– the
complex plane.
24

The complex exponential obey the same exponentiation rules as


the real exponential function:

e z1 e z2 = e z1 + z2 . (7)

for all complex numbers z1 , z2 ∈ C, moreover (ez1 )z2 = ez1 z2 .

The conjugate z ∗ of complex number z is defined as:

z∗ = x − iy (8)
= A[cos(θ ) − i sin(θ )] (9)
= A[cos(−θ ) + i sin(−θ )] (10)
−iθ
= Ae . (11)

The conjugation operation flips the sign of the imaginary component.


The geometric interpretation of the complex conjugate is shown
in Figure 18. We’ll use the superscript star notation to denote the
conjugation operator.

The complex conjugate can be used to obtain the magni-



tude of the complex number: |z| = zz∗ as zz∗ = ( x + iy)( x −
iy) = x2 + y2 or zz∗ = |z|eiθ |z|e−iθ = |z|2 .

A complex conjugate can be used to select the real and


imaginary components of a complex number as follows:
1
Re{z} = (z + z∗ ) = x, (12)
2
1
Im{z} = (z − z∗ ) = y . (13)
2i
These formulas are often encountered when dealing with spectral
representations of real-valued signals, which always come as conju-
gate symmetric pairs z and z∗ .

The use of a sum of a complex number and it’s conjugate


can be used to relate the exponent function to a cosine
and sine function. Using Euler’s formula for z = eiθ and Equa-
tions 12 and 13, we can obtain:

1  iθ 
cos(θ ) = e + e−iθ (14)
2
1  iθ 
sin(θ ) = e − e−iθ . (15)
2i
These relations are sometimes called the inverse Euler relations. You’ll
encounter these formulas when converting a cos or sin function into
two complex exponent functions. The first step of a signal processing
25

related calculation involving real-valued signals is often making this


conversion, as functions of the form Aeiθ are significantly easier to
deal with.

Complex multiplication can be viewed as multiplication


of magnitudes and summation of phases. Let’s express two
complex numbers in polar form as z1 = A1 eiθ1 and z2 = A2 eiθ2 .
We can now see that multiplication with complex numbers has an
intuitive interpretation.

z1 z2 = A1 eiθ1 A2 eiθ2 = A1 A2 e|i(θ{z


1 + θ2 ) = A eiθ3 .
} 3 (16)
| {z }
A3 eiθ3

When multiplying two numbers, the resulting angle is a sum of the


two angles θ3 = θ1 + θ2 , which can be also seen as a rotation of the
point indicated by a complex number z1 by angle θ2 on the complex
plane. The new magnitude is the magnitudes of the two numbers
multiplied together A3 = A1 A2 .

Figure 19: A spiral is formed by evaluat-


ing zn with integer values of n between
0 and 41. In this case z = 0.92ei2π/20 .
The parametric curve eiθ with θ ∈ R
draws a circle in the complex plane,
which is depicted with a gray color. The
code that generated this plot can be
found in 006_spiral/spiral.py.

Raising a complex number to the nth power can be seen as


exponential scaling and rotation. Consider a complex number

z = Aeiθ (17)

where A ∈ R≥0 and θ ∈ R. If we raise this to the nth power, we get:

zn = An eiθn = An [cos(θn) + i sin(θn)] . (18)


26

Scaling and rotation is demonstrated in Figure 19.

Here are some Python examples of complex number opera-


tions.
import numpy as np

# I n i t i a l i z e a complex number v a r i a b l e z .
z = 1.0 + 0.5 j
# Get t h e r e a l component o f z
z . real
# and imaginary component o f z .
z . imag
# The a b s o l u t e value o f z .
np . abs ( z )

# What i s i ^{ −1}?
z2 = 1 j * * ( − 1 )

# C r e a t e complex numbers using t h e complex e x p o n e n t i a l f u n c t i o n .


z1 = np . exp ( 1 j * np . p i )
z2 = np . exp ( 1 j * np . p i / 2 . 0 )

# Complex m u l t i p l y .
z3 = z1 * z2
# Complex c o n j u g a t i o n .
z_squared = z1 * np . c o n j ( z1 )

# Determine phase angle .


t h e t a 3 = np . angle ( z3 )

Listing 9: 008_complex_ops/ops_example.py
27

Exercises: Complex Algebra

1. Prove eiπ + 1 = 0 using Euler’s formula.

2. Use Euler’s formula to write 1/i into polar form Aeiϕ with A, ϕ ∈
R. What is the phase angle ϕ?

3. Show that ii is real-valued using Euler’s formula. Use Python to


calculate this value and verify that it is real.

4. Use Euler’s formula to show that de Moivre’s formula is valid for


n ∈ Z:
[cos( x ) + i sin( x )]n = cos(nx ) + i sin(nx )

5. Using Euler’s formula, it is possible to determine the nth root of


unity. What this means is that we look for all unique values of z
which satisfy the following equation where n is a positive integer:

zn = 1. (19)

You probably already know the answer in the case of n = 2, for


which there are two solutions: z0 = 1 and z1 = −1 as both 12 = 1
and (−1)2 = 1.
Provide a general formula that gives n unique values of z ∈ C that
satisfy Equation 19. How many unique solutions of z are there
for each value of n? Hint: remember that ei2πk = 1 where k is an
integer.

6. Consider the equation z5 = 1, where z ∈ C.

a) Find the five solutions wk for k = 0, 1, 2, 3, 4, to the equation


z5 = 1.
b) Create a Python program to plot the solutions to this equation
in the complex plane. Add lines to your plot which connect the
point wk to wk−1 mod 5. What is the shape drawn?
c) Add the unit circle to the plot you found in b).

7. Use the inverse Euler formula to convert (1 − i )e−iωt + (1 + i )eiωt


into the following form:

A cos(ωt + ϕ)

with A ∈ R. What is A and what is ϕ?

8. Prove, using Euler’s formula that:

cos(3θ ) = cos3 (θ ) − 3 cos(θ ) sin2 (θ ).


28

9. Use Euler’s formula to prove the following trigonometric identity:

cos(α + β) = cos(α) cos( β) − sin(α) sin( β) .

10. Another definition of ez (the complex exponential) is by an


extension of the Taylor series expansion of e x (the real exponential),
as follows
∞ k
z
ez := ∑ , for z ∈ C.
k =0
k!
That is, ez is taken to mean this infinite series. Use this Taylor
series expansion definition to show that:

eiθ = cos(θ ) + i sin(θ ). (20)

To do this, compute the Taylor series expansion of eiθ .


29

Suggested solutions: Complex Algebra

1. Euler’s formula:
eiθ = cos θ + i sin θ,
so for θ = π,
eπi = cos(π ) + i sin(π ) = −1,
as cos(π ) = −1 and sin(π ) = 0, hence eπi + 1 = 0.

2. Multiply by i in the numerator and denominator to obtain:

1 i
= 2 = −i.
i i

Using Euler’s formula eiθ = cos θ + i sin θ we take θ = 2 , this gives
a polar representation of −i as:
3π i
−i = e 2 .

Remark: Multiplication by i rotates any vector in the complex


plane by π/2 counterclockwise and (1/i )(i ) = 1, so 1/i rotates by
π/2 clockwise, so our result makes sense.

3. Use Euler’s formula: eiθ = cos θ + i sin θ, with θ = π/2. Then we


π
can write e 2 i = i giving us:
π 2
ii = (e 2 i )i = e 2 i = e− 2 ∈ R.
π π

Thus, ii is a real number.

4. Let x be any real number and n be an integer. Using Equation 7


n times
z }| {
. . . e}a = e a + a + . . . + a = e an therefore:
we have (e a )n = e|a e a{z
n times

[cos( x ) + i sin( x )]n = (eix )n = eixn = cos(nx ) + i sin(nx ),

as desired.

5. From the hint we have that ei2πk = 1, which can be combined with
the fact that (e a )n = e an , giving that the solutions to zn = 1 are of
the form:
2πik
wk = e n , k = 0, . . . , n − 1.
A simple check to see that this is what we want is that wk satisfy:
2πik
( wk )n = ( e n )n = e2πik .

This gives 1 since e2πik = 1 for all k ∈ Z. This gives n unique


solutions to the equation, zn = 1 as expected by the fundamental
theorem of algebra.
30

6. Referring to the previous problem.

a) Have that wk = e2πik/5 for k = 0, 1, 2, 3, 4.


b-c) See Listing 10. The figure formed by the fifth roots of unity is
a regular pentagon.

import m a t p l o t l i b . pyplot as p l t
import numpy as np

# Simple f u n c t i o n t o compute t h e kth order r o o t o f u n i t y o f


degree m.
def r o o t _ u n i t y ( k : i n t , m: i n t ) −> f l o a t :
r e t u r n np . exp ( 2 * np . p i * k * 1 j /m)

# Loop from 0 t o 5 , r e c a l l t h a t t h e % o p e r a t o r computes t h e


remainder , g i v i n g 0 −4 , then 0 again .
r o o t s = np . a r r a y ( [ r o o t _ u n i t y ( k % 5 , 5 ) f o r k i n range ( 6 ) ] )

# P a r t i t i o n an i n t e r v a l o f one period .
t = np . l i n s p a c e ( 0 , 2 * np . pi , num=50)

# Used t o p l o t t h e u n i t c i r c l e .
x = np . cos ( t )
y = np . s i n ( t )

# E x t r a c t t h e r e a l and imaginary components .


z1 = r o o t s . r e a l
z2 = r o o t s . imag

plt . figure ( f i g s i z e =(4 , 4) )


ax = p l t . gca ( )
p l t . p l o t ( z1 , z2 )
plt . plot (x , y)
p l t . g r i d ( True )
plt . xlabel ( "x" )
plt . ylabel ( "y" )
p l t . t i t l e ( " F i f t h roots of unity " )
ax . s e t _ a s p e c t ( " equal " )
# C a l l t h i s i f needed .
# p l t . show ( )

Listing 10: Solution for Exercise 3.6

7. Have that:
1 iθ
cos(θ ) = (e + e−iθ ),
2
1
sin(θ ) = (eiθ − e−iθ ).
2i
Using that 1/i = −i from Exercise 2 we get:
1
(1 − i )e−iωt + (1 + i )eiωt = (eiωt + e−iωt ) − (eiωt − e−iωt ).
i
By the inverse Euler relations we may write this as:

(1 − i )e−iωt + (1 + i )eiωt = 2 cos(ωt) − 2 sin(ωt).


31

Next step is to write this as a pure cosine. That is, determine A


and ϕ such that:

A cos(ωt + ϕ) = 2 cos(ωt) − 2 sin(ωt).

Using cos(α + β) = cos(α) cos( β) − sin(α) sin( β) this can be


rewritten as:

A cos(ωt) cos(ϕ) − A sin(ωt) sin(ϕ) = 2 cos(ωt) − 2 sin(ωt).

Therefore, A and ϕ must satisfy:

A cos(ϕ) = 2,
A sin(ϕ) = 2.
√ √ √
In other words A = 22 + 22 = 8 = 2 2 and tan(ϕ) = 1. With ϕ
in the first quadrant, this gives ϕ = π/4. Hence:
√  π
(1 − i )e−iωt + (1 + i )eiωt = 2 2 cos ωt + .
4

8. Replace the cosine to obtain:

1 3iθ 1
cos(3θ ) = (e + e−3iθ ) = ((eiθ )3 + (e−iθ )3 ).
2 2
When expanded the first term is:

(cos θ + i sin θ )3 = cos3 θ + 3i cos2 θ sin θ − 3 cos θ sin2 θ − i sin3 θ,

and the second term:

(cos θ − i sin θ )3 = cos3 θ − 3i cos2 θ sin θ − 3 cos θ sin2 θ + i sin3 θ.

Adding these together then gives:

cos(3θ ) = cos3 θ − 3 cos θ sin2 θ.

9. Let z1 = eαi and z2 = e βi be complex numbers. Then:

z1 z2 = eαi e βi = e(α+ β)i = cos(α + β) + i sin(α + β).

In addition, we have that:

eαi e βi = (cos α + i sin α)(cos β + i sin β) = (cos α cos β − sin α sin β) + i (sin α cos β + cos α sin β).

Taking the real and imaginary parts we get that:

cos(α + β) = cos α cos β − sin α sin β,


sin(α + β) = sin α cos β + cos α sin β.
32

10. Consider the complex exponential of the form eiθ , then by this
new definition we have:

(iθ )k
eiθ = ∑ k!
.
k =0

Using this definition we have:



(iθ )k (iθ )2 (iθ )3 (iθ )4
eiθ = ∑ k!
= 1 + iθ +
2!
+
3!
+
4!
+....
k =0

Using that i2 = −1 we get:

θ2 θ3 θ4
eiθ = 1 + iθ − −i + +...
2! 3! 4!
Grouping terms with and without i this can be written as:

θ2 θ4 θ3 θ5
eiθ = (1 − + + . . .) + i (θ − + − . . . ).
2! 4! 3! 5!
The first terms correspond to the Taylor series for cos θ and the
other sin θ, hence we get Euler’s formula:

eiθ = cos θ + i sin θ.


Signals and Systems

What is a signal and what is a system? By a signal, we mean an


information carrying mathematical function. Any function that has a
value as a function of one or more variables is in essence a signal. By
a system, we denote a mathematical operation that modifies a signal.
A system consists of the precise mathematical description of how
a signal fed into a system is modified by the system to produce an
output signal.
The definition of signals and systems are merely abstract concepts,
which have gained acceptance in the engineering community. In
the context of mathematics, signals could also be called functions or
vectors. Systems would be called functions or operators. In computer
science, signals are often treated as arrays of numbers in the memory
of a computer and systems are algorithms and computer programs

Dependent variable x (t)


that operate on these arrays.

Signals

A signal is a mathematical function x (t), which describes the value


of a dependent variable x, as a function of an independent variable t. Independent variable t
An independent variable “sweeps” through all possible values. The
Figure 20: Continuous-time signal.
dependent variable is the variable that changes as a function of the
independent variable and conveys information.
Dependent variable x [n]

Here’s an example. When describing electric potential as a func-


tion of time V (t), time t is the independent variable and the electric
potential V (t) as a function of time is the dependent variable. Time
by itself does not convey information, but electric potential as a
function of time does.
Physical “real world” signals are modeled as continuous-time
signals. Examples include, amongst countless others: Independent variable n

• temperature, Figure 21: Discrete-time signal.

• density,
34

• pressure as a function of time and space (sound, seismic waves),

• electric field as a function of time and position (electromagnetic


waves), or

• electrical current in a circuit.

Physics relies on differential calculus, with integration and differ-


entiation as elementary operators. As we will later see, differential
calculus can be studied through methods of signal processing, espe-
cially the spectral techniques and the Fourier transform are useful
tools that can be applied in differential calculus.
This course will focus primarily on one-dimensional signals. These
signals are complex-valued functions x (t) ∈ C of a real-valued
argument t ∈ R. This will naturally also cover the special case, where
the signal is real-valued x (t) ∈ R ⊂ C.
By convention, we will refer to the independent variable of a sig-
nal as time, even though this variable doesn’t necessarily have to
indicate time. It can represent anything. For example, the indepen-
dent variable can just as well be, e.g., distance.
Signals can be continuous or discrete. Following a commonly
adopted practice, we will use round brackets for continuous-time
signals (e.g., x (t)) and square brackets for discrete-time signals (e.g.,
x [n]).
In the case of discrete-time signals, the sample index n ∈ Z is
unitless. A discrete-time signal is merely a sequence of numbers. The
only way to associate meaning to this sequence of numbers is the a
priori knowledge of how the signal was discretized. This allows us to,
e.g., map the nth sample to a real-valued time.
An example of a continuous-time and a discrete-time signal is
shown in Figures 20 and 21. When plotting signals graphically, it
is customary (but not mandatory) to use the horizontal axis for the
independent variable, and the dependent variable on the vertical axis.
To summarize, the two main types of signals that this course deals
with are one-dimensional continuous-time x (t) and one-dimensional
discrete-time signals x [n]. Continuous-time signals are mappings
from the real axis (time) to the set of complex numbers:

x:R→C (21)

Discrete-time signals are mappings from the set of integers to the set
of complex numbers:
x:Z→C (22)
Signal processing of higher dimensional signals are essentially func-
tions of the form
x : RN → C (23)
35

or in the case of discrete-time:

x : ZN → C (24)

The first experimental detection of gravitational waves


using the Laser Inteferometer Gravitational-Wave Observatory (LIGO)
is shown in Figure 22. This signal is an example of a one-dimensional
signal. The signal is thought to be caused by two black holes with
masses around 30 solar masses merging together, 1.3 billion light-
years from Earth. The independent variable on the x-axis is time,
and the dependent variable is strain (stretching) of space that occurs
due to a gravitational wave passing through the instrument. This is
measured by comparing the relative lengths of two 4 km long laser
interferometer arms.

Figure 22: Two independent one di-


mensional signals measured by LIGO
depicting strain. This is stretching
of space due to a gravitational wave
passing through two geographically
separated laser interferometer obser-
vatories. One in Hanford, WA (left),
and one in Livingston, LA (right). The
figure is from: B. P. Abbott et al. (LIGO
Scientific Collaboration and Virgo Col-
laboration) Phys. Rev. Lett. 116, 061102
– Published 11 February 2016.

Signals can be of arbitrary dimension. For example, an image


is a 2d signal, where the dependent variable is intensity I ( x, y) mea-
sured as a function of two independent spatial variables x and y that
indicate distance from origin along two orthogonal axes. An example
of a discrete-time two-dimensional signal is shown in Figure 23. It
represents an image of the emission from hot gas in the event hori-
zon of a black hole around the M87 galaxy. The image is obtained
using a technique called very long baseline interferometry, which
utilizes measurements from radio telescopes around the world. These
measurements are combined to simulate a large telescope with the
resolution equivalent to a telescope approximately the size of Earth. 7 7
The Fourier transform, which is one
Even higher dimensional signals can be used. Consider for exam- of the central themes in this course, is
a key part of the mathematics of very
ple a video. It is a signal that contains the intensity of an image as long baseline interferometric imaging.
a function of time. You can think of a moving picture (movie) as a
36

three-dimensional signal: I ( x, y, t), where the intensity of the image is


a function of two-dimensional position as well as time.
While we will focus primarily on one dimensional signals in
order to keep things simple, many of the concepts we discuss can
be generalized to multiple dimensions. Two-dimensional signal
processing is called image processing, and it shares many of the
same basic concepts with one dimensional signal processing. I will
try to occasionally give examples of signal processing with higher
dimensional signals.

Figure 23: An example of a 2d signal:


A Very Long Baseline Interferometric
radio image of the black hole at the
center of galaxy M87. The intensity
depicts what is thought to be hot gas
outside the event horizon of the black
hole. Credit: Event Horizon Telescope
collaboration et al. 2019.

Digital signals on a computer, are also quantized. This means


that there is only a finite number of possible values for the dependent
value of a signal. This type of signal is called a quantized signal. We
will not discuss quantization in this course.

Systems

A signal processing system can be represented graphically as


a block diagram, which describes signals going into a system, and
signals going out of a system. An example is shown in Figure 24

signal in x (t) signal out y(t) Figure 24: A simple signal processing
System T {·} system block diagram.
37

A graphical representation is useful for understanding a signal


processing system, especially if it is a complicated one, which in-
cludes many systems and signals.
The following block diagram is a real-world example of the block
diagram of the EISCAT Svalbard radar transmitter subsystem.

Figure 25: An example of a more


complicated block diagram. From:
Wannberg et.al., 1997.

The mathematical description of a system (what goes on in the


box) is a general transformation of a signal. The mathematical nota-
tion for a continuous-time system in general is8 : 8
Here T {·} is a mathematical descrip-
tion of how the input signal is modified
by the signal processing system. This is
y(t) = T { x (t)} (25)
also called an operator

and for discrete-time system:

y[n] = T { x [n]} (26)

An example of a system could be a function that delays the input


signal by a delay τ:
y ( t ) = x ( t − τ ). (27)
Another example is a linear amplifier, which multiplies the amplitude
of the signal with a constant α ∈ C:

y(t) = αx (t). (28)

You will often encounter both of these types of systems.

For example, we can use these two basic systems to make


a simplified model of a radar. Imagine that you have a radar.
This radar sends out a waveform that is described by a waveform
x (t). Let’s say that the time it takes an electromagnetic wave to travel
from the radar transmitter to a point-like radar target and back is
τ seconds. The received radar echo will be a delayed version of the
38

transmitted signal, which is delayed by τ. In addition to this, the


signal will be scaled, as a radar echo is typically much weaker than
the transmitted signal.
Therefore, a very simple system that describes a radar echo is the
following:
y(t) = αx (t − τ ) (29)
where y(t) is the received radar echo signal.
We can further expand this concept to write the equation that de-
scribes a radar echo for a situation where there can be a continuous
radar echo at time delays between 0 and T:
Z T
y(t) = α(τ ) x (t − τ )dτ. (30)
0

In this case, α(τ ) describes the radar echo amplitude as a function


of propagation delay. This type of equation is called a convolution
equation. You will encounter this type of equation very often in
signal processing, and find that the Fourier transform is a very useful
tool when dealing with convolution equations.

Differential operators can also be seen as systems. Con-


sider the first time-derivative of the continuous-time signal x (t):
d
y(t) = x ( t ). (31)
dt
d
The system definition is the time derivative operator dt .

Systems are classified based on their mathematical prop-


x1 ( t ) × c2

erties. The most important two are: linearity and time-invariance. A


system which is both linear and time-invariant is called linear time-
invariant (LTI). Such systems have beneficial mathematical properties + T {·} y1 ( t )

that make analysis and design of such systems very straightforward.


We’ll later on prove that LTI systems are fully characterized by some-
thing known as an impulse response. This is an important concept in x2 ( t ) × c1

signal processing.
x1 ( t ) T {·} × c2

Linear system

A system is linear, if a linear combination of inputs fed into the + y2 ( t )


system yields the same as the linear combination of outputs:

T {c1 x1 (t) + c2 x2 (t)} = c1 T { x1 (t)} + c2 T { x2 (t)} (32)


x2 ( t ) T {·} × c1

for arbitrary constants c1 ∈ C and c2 ∈ C and arbitrary input


signals x1 (t) and x2 (t). This property is highly useful, and appears Figure 26: In order for the system
specified by T {·} to be linear, y1 (t) =
throughout signal processing.
y2 (t) must be satisfied.
39

An example of a linear system is a system that scales the input


signal by a constant factor α:

y(t) = αx (t). (33)

If |α| > 1, the signal is amplified. This type of system would typically
be called an amplifier. If 0 < |α| < 1, the system would be called an
attenuator, as the output signal amplitude would be attenuated.
It is quite easy to determine that the test for linearity is passed for
this system:

α[c1 x1 (t) + c2 x2 (t)] = c1 [αx1 (t)] + c2 [αx2 (t)]. (34)

An example of a non-linear system is the following system,


which obtains the absolute value of x (t):
x (t) x (t)
y(t) = | x (t)| (35)

It is quite clear that this does not pass the test for linearity:
T {·} D{·}
|c1 x1 (t) + c2 x2 (t)| ̸= c1 | x1 (t)| + c2 | x2 (t)|, (36)

for all possible values c1 , c2 , x1 (t), and x2 (t).


D{·} T {·}

Time-invariant system
y2 ( t ) y1 ( t )
Let us first define a delay system D{ x (t)} = x (t − τ ). Let us assume
that the output of a system is y(t) = T { x (t)}. This system is time- Figure 27: In order for the system
invariant if: specified by T {·} to be time-invariant,
T {D{ x (t)}} = D{T { x (t)}} (37) y1 (t) = y2 (t) must be satisfied.

In other words, it does not matter if the signal is delayed before or


after the system T {·}. To check time-invariance, we need to verify
Equation 37.
The test for time-invariance is illustrated as a block diagram in
Figure 27. If you want, you could think of a time-invariant system as
a system that satisfies:
[T , D] x (t) = 0,
where [T , D] = T D − DT is the commutator of two operators.
Thus, a time-invariant system T is a system that commutes with the
time-delay operator [T , D] = 0.

An example of a time-invariant system is the system that


returns the absolute value of the input signal y(t) = | x (t)|. We can
see this by evaluating:

T {D{ x (t)}} = T { x (t − τ )} = | x (t − τ )| (38)


D{T { x (t)}} = D{| x (t)|} = | x (t − τ )| (39)
40

Time-invariance holds as the outputs are equal. It doesn’t matter


if a time-delay is applied to the signal before or after obtaining the
absolute value of the input signal.

An example of a case that is not time-invariant is y(t) =


T { x (t)} = t + x (t). We can immediately see that the system directly
depends on t, not only through, the input. The formal test also shows
that time-invariance is not met:

T {D{ x (t)}} = T { x (t − τ )} = t + x (t − τ ) (40)


D{T { x (t)}} = D{t + x (t)} = (t − τ ) + x (t − τ ) (41) y(t)
y(t)
4 yd (t)
In this case, T {D{ x (t)}} ̸= D{T { x (t)}} so the system is not time-
invariant. 2

x (t)
Example: Overdriven amplifier −2 2

A simple model of a distorted guitar amplifier system would be the −2


following clipping amplifier system, which is specified as follows:
 −4
 − β when αx (t) < − β

yd (t) = αx (t) when |αx (t)| ≤ β (42)
 Figure 28: The system function of a
 β when αx (t) > β linear amplifier y(t) and a clipping
amplifier system yd (t).
This is a very approximative model of an overdriven guitar amplifier.
This type of system is often encountered in guitar music from the 50s
and onwards. I’m sure that once you later implement this in practice,
you’ll recognize the sound that this system makes.
What does this system do? It amplifies the signal, but only up to a
certain point. Beyond a certain absolute value of the input, the output
maintains a constant positive or negative value. This type of behavior
is also sometimes called clipping, and the effect is also sometimes
called distortion, as the input signal amplitude is not linearly scaled,
but it is rather distorted.
Many real world amplifier systems have this type of saturation
behavior. What this often means in practice is that the system is
Figure 29: A video discussing an
linear when the input absolute amplitude is less than some critical overdriven guitar amplifier can be
value, but beyond this, linearity no longer holds. found here: https://youtu.be/I30Mn_
-yYF8.
Figure 28 illustrates what yd (t) would look like as a function of
x (t). It is compared with a normal amplifier system y(t) = αx (t),
which does not clip.
41

Exercises: Signals and Systems

1. Consider the following signal V (t), which describes the electric


potential of a circuit as a function of time. The instantaneous
power flowing through this circuit is given by the following equa-
tion:
P(t) = R−1 [V (t)]2 .
It is possible to consider the function R−1 [·]2 that converts electric
potential into power as a system in signal processing terminology.

a) Is this system linear?


b) Is this system time-invariant?

Prove your result.

2. A discrete-time system is defined as:

1 4
5 k∑
y[n] = x [ n − k ]. (43)
=0

a) Explain in words what this system does to the input signal


x [ n ].
b) Is this system linear?
c) Is this system time-invariant?

Use the tests for linearity and time-invariance to justify your


result.

3. A time scaling system adjusts the scaling of the independent


variable: y(t) = x (αt), when x (t) is the signal fed into the system
and y(t) is the output.

a) What is the effect on the signal when 0 < α < 1?


b) What about α > 1?
c) Is this system linear?
d) Time-invariant?
d
4. Prove that the time derivative operator y(t) = T { x (t)} = dt x ( t ) is
a linear time-invariant system.

5. The guitar amplifier system given in Equation 42 is not a linear


system. Prove this. Hint: you can use proof by counterexample, by
finding a case where the requirement of linearity is not met.

6. Is the guitar amplifier system given in Equation 42 a time-invariant


system? Prove your result by using the formal test for time-
invariance.
42

7. The code in Listing 11 implements a linear amplifier system for an


audio signal:
y(t) = αx (t) (44)

where the input signal is x (t), the output signal is y(t), and the
amplification is α. You can find the code and audio file on GitHub.
import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . i o . w a v f i l e as s i o

# A m p l i f i e r gain .
alpha = 1 0 . 0

# A f u n c t i o n t o compress s i g n a l x ( s i g n a l p r o c e s s i n g system . )
def amplify ( x , alpha ) :
r e t u r n alpha * x

# Read wav f i l e ( read only one s t e r e o channel . )


#
# G u i t a r _ c l e a n . wav c o p y r i g h t
# O r i g i n a l author : LG downloaded from freesound . org ,
# O r i g i n a l f i l e name : G u i t a r c l e a n r i f . wav
wav = s i o . read ( " g u i t a r _ c l e a n . wav" )
s a m p l e _ r a t e = wav [ 0 ]
# Read only one s t e r e o channel .
x = wav [ 1 ] [ : , 0 ]

# C r e a t e time v e c t o r ( independent v a r i a b l e . )
time_vec = np . arange ( l e n ( x ) ) / f l o a t ( s a m p l e _ r a t e )

# P l o t o r i g i n a l and a m p l i f i e d .
p l t . p l o t ( time_vec , amplify ( x , alpha ) , l a b e l = " Amplified " )
p l t . p l o t ( time_vec , x , l a b e l = " O r i g i n a l " )
p l t . legend ( )
p l t . x l a b e l ( " Time $ t $ " )
p l t . y l a b e l ( " R e l a t i v e a i r p r e s s u r e $y ( t ) $ " )
p l t . show ( )

out = amplify ( x , alpha )

# S c a l e maximum a b s o l u t e amplitude t o 0 . 9 ,
# because 1 . 0 i s t h e maximum allowed by t h e f i l e . wav f i l e
format .
# Note t h a t t h i s w i l l not allow you t o hear t h e audio s i g n a l
# amplitude i n c r e a s i n g .
out = 0 . 9 * out/np . max ( np . abs ( out ) )
# Write compressed output t o wav f i l e .

# Patch from J o s t e i n and Adrian ( c a s t t o 32 b i t f l o a t . )


s i o . w r i t e ( " guitar_amp . wav" , sample_rate , np . a r r a y ( out , dtype=
np . f l o a t 3 2 ) )

Listing 11: 003_guitar/amplifier.py

Run this code and verify that the audio signal stored in file
guitar_amp.wav is amplified. You will need to do this by inspect-
ing a before and after amplification plot of the audio signal, as the
43

example code will normalize the amplitude of the signal before


storing it to file.
You can use this script as a basis for experimenting with audio
signal processing later on during this course.

8. Change the code in Listing 11 is such a way that it implements a


clipping amplifier, as shown in Equation: 42. Figure out suitable
values for α and β that make the guitar sound distorted.
44

Suggested solutions: Signals and Systems

1. a) To check linearity of a system, we check Equation 32:

T {c1 V1 (t) + c2 V2 (t)} = R−1 [c1 V1 (t) + c2 V2 (t)],


c1 T {V1 (t)} + c2 T {V2 (t)} = c1 R−1 [V1 (t)]2 + c2 R−1 [V2 (t)]2 ,

these are not equal, so the system is not a linear system.


b) To check time-invariance for this system, verify Equation 37:

T {D(V (t))} = T {V (t − τ )} = R−1 [V (t − τ )]2 ,


D{T {V (t)}} = D{ R−1 [V (t)]2 } = R−1 [V (t − τ )]2 .

Here we get that the outputs are equal, hence the system is
time-invariant.

2. Consider the discrete-time system defined as:

1 4
5 k∑
y[n] = x [ n − k ].
=0

a) This system averages x [n] with four of its past values.

b) Have that:

1 4
5 m∑
T {c1 x1 [n] + c2 x2 [n]} = [c1 x1 [n − m] + c2 x2 [n − m]],
=0
4 4 4
1 1 1
c1 T { x1 [n]} + c2 T { x2 [n]} = c1
5 ∑ x1 [n − j] + c2 5 ∑ x2 [n − k] = 5 ∑ [c1 x1 [n − m] + c2 x2 [n − m]].
j =0 k =0 m =0

In the last step, the two sums were combined into one and the
index was renamed to m. From this we can see that the output are
equal, so the system is linear.

c) To check time-invariance we calculate:

1 4
5 k∑
T {D{ x [n]}} = T { x [n − τ ]} = x [(n − τ ) − k],
=0
( )
1 4 1 4
5 k∑ 5 k∑
D{T { x [n]}} = D x [ n − k ] = x [(n − k ) − τ ].
=0 =0

These are equal upon rearranging the parenthesis, hence T {·} is


time-invariant.

3. Consider a time-scaling system of the form y(t) = x (αt), where


x (t) is the input signal to the system.
45

a) If 0 < α < 1, the output signal gets stretched, becoming wider.


In effect, it reduces the frequency of the original signal. To see
this compare sin(t) with sin(0.5t) as an example.
b) If α > 1, the original frequencies are increased by a factor of
α, giving more oscillations in the signal. Again, compare sin(t)
with sin(2t) as an example.
c) Let’s check if the system is linear. Have that:

T {c1 x1 (t) + c2 x2 (t)} = c1 x1 (αt) + c2 x2 (αt),


c1 T { x1 (t)} + c2 T { x2 (t)} = c1 x1 (αt) + c2 x2 (αt),

so the system is linear as these are equal.


d) Checking time-invariance:

T {D{ x (t)}} = T { x (t − τ )} = x (α(t − τ )),


D{T { x (t)}} = D{ x (αt)} = x (αt − τ ).

The system is not time-invariant as these do not satisfy Equa-


tion 37.

d
4. Consider the derivative as a system T { x (t)} = dt x ( t ). This system
is time-invariant and linear. It is linear since:
d d
T {c1 x1 (t) + c2 x2 (t)} = c1 x1 ( t ) + c2 x2 ( t ),
dt dt
d d
c1 T { x1 (t)} + c2 T { x2 (t)} = c1 x1 (t) + c2 x2 (t),
dt dt
are equal, which follows from the linearity of the differential
operator.
For time-invariance, we check the condition in Equation 37:

d
T {D{ x (t)}} = T { x (t − τ )} = x (t − τ ),
  dt
d d
D{T { x (t)}} = D x ( t ) = x ( t − τ ).
dt dt

Showing that Equation 37 holds, hence the system is time-invariant.

5. The guitar amplifier is a system of the form:



− β,

 αx (t) < − β,
y(t) = αx (t), |αx (t)| ≤ β,


αx (t) > β.

β,

The system is not linear. To show this, find a counter example. The
linearity should hold for all signals x (t). In particular, consider
46

two signals for which αx1 (t) = β and αx2 (t) = β. For these signals
we get:
T {αx1 (t)} + T {αx2 (t)} = β + β = 2β,
while:

T {αx1 (t) + αx2 (t)} = T { β + β} = T {2β} = β,

as 2β > β by the last condition, hence we get T {2β} = β. Thus,


we get a contradiction in that 2β = β for all β, hence the system is
non-linear.

6. For the guitar amplifier of the form:



 − β when
 αx (t) < − β,
yd (t) = αx (t) when |αx (t)| ≤ β,

 β when αx (t) > β.

Have that:

− β,

 for αx (t − τ ) < − β,
T {D{ x (t)}} = T { x (t − τ )} = αx (t − τ ), for |αx (t − τ )| ≤ β,


for αx (t − τ ) > β.

β,
  

 − β,
for αx (t) < − β,  − β, for αx (t − τ ) < − β,
 
  
D{T { x (t)}} = D αx (t), for |αx (t)| ≤ β, = αx (t − τ ), for |αx (t − τ )| ≤ β,

   
 
 
β, for αx (t) > β. β, for αx (t − τ ) > β,
 

so the system is time-invariant.

7. Running Listing 11 and looking at the resulting plot, one can see
that the signal has been amplified.
import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . i o . w a v f i l e as s i o

# A m p l i f i e r gain .
alpha = 1 0 . 0

def amplify ( x : np . ndarray , alpha : f l o a t ) −> np . ndarray :


" " "A f u n c t i o n t o compress s i g n a l x ( s i g n a l p r o c e s s i n g
system ) . " " "
r e t u r n ( alpha * x )

# g u i t a r _ c l e a n . wav c o p y r i g h t
# O r i g i n a l author : LG downloaded from freesound . org ,
# O r i g i n a l f i l e name : G u i t a r c l e a n r i f . wav

# The read f u n c t i o n r e t u r n s two arguments ,


47

# t h e s e being t h e sample r a t e and t h e a c t u a l data .


wav = s i o . read ( " g u i t a r _ c l e a n . wav" )
# Read only one s t e r e o channel .
s a m p l e _ r a t e = wav [ 0 ]
# Returned i s a 2− dimensional NumPy a r r a y corresponding
# t o t h e l e f t and r i g h t channel .
x = wav [ 1 ] [ : , 0 ]

# C r e a t e time v e c t o r ( independent v a r i a b l e ) .
time_vec = np . arange ( l e n ( x ) ) / f l o a t ( s a m p l e _ r a t e )

# P l o t o r i g i n a l and a m p l i f i e d .
p l t . p l o t ( time_vec , amplify ( x , alpha ) , l a b e l = " Amplified " )
p l t . p l o t ( time_vec , x , l a b e l = " O r i g i n a l " )
p l t . legend ( )
p l t . x l a b e l ( " Time $ t $ " )
p l t . y l a b e l ( " R e l a t i v e a i r p r e s s u r e $y ( t ) $ " )
# C a l l t h i s i f needed .
# p l t . show ( )

# Amplify t h e s i g n a l .
out = amplify ( x , alpha )

# S c a l e maximum a b s o l u t e amplitude t o 0 . 9 , because 1 . 0 i s


# t h e maximum allowed by t h e f i l e format .
out = 0 . 9 * out / np . max ( np . abs ( out ) )

# Write compressed output t o wav f i l e .


s i o . w r i t e ( " guitar_amp . wav" , sample_rate , np . a r r a y ( out , dtype=
np . f l o a t 3 2 ) )

Listing 12: Solution for Exercise 4.7

The output from this system is shown below.


48

8. Running Listing 13 applies a distortion effect to the signal.


import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . i o . w a v f i l e as s i o

alpha = 2 . 0 # A m p l i f i e r gain .
beta = 0.08 # Cut o f f .

def d i s t o r t i o n ( x : np . ndarray , alpha : f l o a t , b e t a : f l o a t ) −> np


. ndarray :
" " "A f u n c t i o n t o apply d i s t o r t i o n t o a s i g n a l x . " " "
y = np . z e r o s ( x . s i z e )

p r i n t ( x . dtype )

# C r e a t e masks based on t h e c o n d i t i o n s .
mask1 = alpha * x < − b e t a
mask2 = np . abs ( alpha * x ) <= b e t a
mask3 = alpha * x > b e t a

# The masks a r e a r r a y s t h a t a r e True/ F a l s e , a s s i g n v a l u e s


based on t h e True/ F a l s e value .
y [ mask1 ] = − b e t a
y [ mask2 ] = alpha * x [ mask2 ]
y [ mask3 ] = b e t a

return y

# Read wav f i l e ( read only one s t e r e o channel ) .


wav = s i o . read ( " g u i t a r _ c l e a n . wav" )
s a m p l e _ r a t e = wav [ 0 ]
# Read only one s t e r e o channel .
x = wav [ 1 ] [ : , 0 ]

# C r e a t e time v e c t o r ( independent v a r i a b l e ) .
time_vec = np . arange ( x . s i z e ) / f l o a t ( s a m p l e _ r a t e )

# Apply d i s t o r t i o n t o t h e s i g n a l .
out = d i s t o r t i o n ( x , alpha , b e t a )

# P l o t o r i g i n a l and a m p l i f i e d .
p l t . p l o t ( time_vec , out , l a b e l = " D i s t o r t e d " )
p l t . p l o t ( time_vec , x , l a b e l = " O r i g i n a l " )
p l t . legend ( )
p l t . x l a b e l ( " Time $ t $ " )
p l t . y l a b e l ( " R e l a t i v e a i r p r e s s u r e $y ( t ) $ " )
# C a l l t h i s i f needed .
# plt . plot ( )

# S c a l e maximum a b s o l u t e amplitude t o 0 . 9 , because 1 . 0 i s t h e


# maximum allowed by t h e f i l e format .
out = 0 . 9 * out/np . max ( np . abs ( out ) )
# Write compressed output t o wav f i l e .
s i o . w r i t e ( " g u i t a r _ d i s t . wav" , sample_rate , np . a r r a y ( out , dtype=
np . f l o a t 3 2 ) )

Listing 13: Solution for Exercise 4.8


Elementary Signals

There are two important elementary signals which you will


encounter throughout signal processing: the unit impulse δ(t) and the
unit step function u(t). Both of these functions have continuous-time
and discrete-time counterparts.

The unit impulse function δ ( t ) is a special distribution function9 , 9


The unit impulse function is also
which is infinitely narrow, non-zero valued only at t = 0. And when known as the Dirac delta function, after
physicist Paul Dirac, who used this
you integrate over this function, the result will be 1. function for modeling the distribu-
It is possible to define δ(t) in several ways as a limit of functions tion of electrical point-charges (e.g.,
electrons and protons)
with a well-defined area and width σ. I’ll use the Gaussian density
function:
2
1 − t
δσ (t) = √ e 2σ2 . (45)
2πσ2
δσ1 (t)
The Gaussian density function is shown in Figure 30 for two different
standard deviations σ1 and σ2 . The smaller the value of σ, the more δσ2 (t)
narrow the function is. t
As a result of how the function is defined, an integral over δσ (t) is −σ2 σ1 σ1 σ2
always unity. This is perhaps not a surprise, as this function is used
Figure 30: A Gaussian density function
in statistics as a probability density function:
becomes a unit impulse δ(t) when
Z ∞ the width parameter approaches zero
δσ (t)dt = 1 . (46) σ → 0.
−∞

The Dirac delta function δ(t) can be thought of as the limit when the
standard deviation of this distribution approaches zero: δ(t − τ )
Z ∞ Z ∞
lim δσ (t)dt = δ(t)dt = 1 (47)
σ →0 − ∞ −∞
t
It is hopefully not difficult to convince yourself that the integral
τ
evaluates to unity even as σ → 0. But what does the function δ(t)
look like? It is infinitely narrow, with only one non-zero value at Figure 31: A unit impulse is typically
depicted with a vertical arrow. A unit
zero: ( impulse δ(t − τ ) is centered at t = τ.
∞ when t = 0 This is the only value of t where the
δ(t) = (48)
0 when t ̸= 0 unit impulse is non-zero.
50

When plotting this function, it is customary to utilize an up arrow, as


shown in Figure 31. The figure also demonstrates how it is possible a
x (t)
to shift the location of the peak by subtracting a constant τ from the
argument of the function. δ(t − τ )
t
The main application of the unit impulse function in signal pro- τ
cessing is selecting or “sampling” a value of a signal. This is also
sometimes referred to as the sifting property. Consider a function
x (t) which has the value x (τ ) = a. If we integrate over the function Figure 32: The unit impulse “selects”
x (t)δ(t − τ ), we obtain: the value of a continuous-time signal
x (τ ) = a.
Z ∞
x (t)δ(t − τ )dt = x (τ ) = a . (49)
−∞
δ[n]
This type of integral will often appear when relating a continuous- 1
time theory to a discrete-time theory. If you are still having a difficult
time convincing yourself why the integral above results in the value
it does, try thinking about it through the limit:
Z ∞ n

lim x (t)δσ (t − τ )dt = x (τ ) = a . (50) −3 −2 −1 1 2 3 4 5 6


σ →0 − ∞
Figure 33: The discrete-time unit
The Dirac delta function is for the most part, used in this course impulse.
inside an integral, so that dealing with a singularity (∞) is avoided.
u(t)
The discrete-time unit impulse δ [ n ] is defined as: 1
(
1 n=0
δ[n] = . (51)
0 n ̸= 0

The discrete-time unit impulse signal is shown in Figure 33. In most 0


cases, the discrete-time unit impulse serves a similar function as its t
continuous-time equivalent.
Figure 34: The unit step function u(t)
shown in blue transitions from 0 to 1 at
The unit step function can be defined as follows10 : the origin.
10
This function is also called the
(
0 t<0 Heaviside-function, after Oliver Heavi-
u(t) = . (52)
1 t≥0 side, who used this function to model
signals used in telecommunications.
It is a step-like function, which abruptly transitions to 1 at zero. The
following figure depicts the unit step function. Notice that there is a
u(t) − u(t − L)
discontinuity at t = 0. 1
The unit step function can be used to create a rectangular function
of a certain length. Here are two ways that this can be done:

rect(t, L) = u(t) − u(t − L)


= u(t)u( L − t) . 0 L
t
Figure 35 shows a plot of the rectangular function. This type of
Figure 35: A rectangular function that
function appears, e.g., when dealing with ideal filters that only select
is obtained using a unit step function
spectral components that lie within a specific band of frequencies. u ( t ) − u ( t − L ).
51

The discrete-time unit step u [ n ] function is defined as:


(
0 n<0 u[n]
u[n] = . (53) 1
1 n≥0

The discrete-time unit step function is shown in Figure 36.


n
−3 −2 −1 1 2 3 4 5 6

Figure 36: A discrete-time unit step


function.
52

Exercises: Elementary Signals

The following exercises cover typical integrals with elementary


signals, which you’ll encounter in signal processing.

1. Simplify the following integrals:

a) Z ∞
δ(t)eiωt dt =?
−∞

b) Z ∞
δ(t − τ )eiωt dt =?
−∞

c) Z ∞
δ(ω )e−iωt dω =?
−∞

d) Z ∞
δ(ω − ω0 )e−iωt dω =?
−∞

2. Assume L > 0. Show that the integral:


Z ∞
[u(t + L) − u(t − L)]eiωt dt
−∞

evaluates to:
α(eiωL − e−iωL )

What is α?

3. Consider the following integral:


Z ∞
sin(t − c)δ(t)dt.
−∞

For which value of c do we have:


Z ∞
sin(t − c)δ(t)dt = 0?
−∞

4. Evaluate the integral:


Z ∞
u(t)u( L − t) cos(2πωt)dt,
−∞

assuming L > 0.
53

Suggested solutions: Elementary Signals

1. By definition and known properties for the Dirac delta function,


we have: Z ∞
x (t)δ(t − τ )dt = x (τ ).
−∞

a) Using the above, we get:


Z ∞
δ(t)eiωt dt = eiω ·0 = 1.
−∞
b) Similarly, we get:
Z ∞
δ(t − τ )eiωt dt = eiωτ .
−∞
c) If the variable is ω, we have:
Z ∞
δ(ω )e−iωt dω = 1,
−∞
by the same reasoning as a).
d) Z ∞
δ(ω − ω0 )e−iωt dω = e−iω0 t .
−∞

2. Consider the integral:


Z ∞
[u(t + L) − u(t − L)]eiωt dt.
−∞
This corresponds to a rectangular function with height 1 and
width 2L centered around 0, when L > 0. Thus, the integral can be
simplified to:
Z ∞ Z L
iωt
[u(t + L) − u(t − L)]e dt = [u(t + L) − u(t − L)]eiωt dt.
−∞ −L
The function u(t + L) − u(t − L) ≡ 1 on the interval [− L, L], hence:
1 iωt L
Z L Z L  
1 iωL
iωt
[u(t + L) − u(t − L)]e dt = iωt
e dt = e = (e − e−iωL ).
−L −L iω −L iω
1
Therefore, α = iω .

3. Using the properties of the Dirac delta function, we have:


Z ∞
sin(t − c)δ(t)dt = sin(0 − c) = sin(−c) = − sin(c).
−∞
This evaluates to 0 for c = πn where n ∈ Z. Thus, c has to be
c = πn.

4. The function u(t)u( L − t) corresponds to a rectangular function


of length L and is 0 whenever t < 0 and t > L and 1 if 0 < t < L,
therefore the integral can be written as:
Z ∞ Z L
1 1
u(t)u( L − t) cos(2πωt)dt = cos(2πωt)dt = [sin(2πωt)]tt=
=
L
0 = sin(2πωL).
−∞ 0 2πω 2πω
Sinusoidal Signals

A complex sinusoidal signal is the elementary building


block of the Fourier decomposition of a signal. This signal
is fully determined by three parameters: amplitude A ∈ R≥0 , phase
eiωt
ϕ ∈ R (rad), and angular frequency ω ∈ R (rad/s). Re(eiωt )
Im(eiωt )
i (ωt+ϕ) iϕ iωt iωt
z(t) = Ae = Ae e = Xe . (54)

It is possible to combine phase and amplitude as one complex con-


stant X = Aeiϕ ∈ C. This constant contains information about the t
amplitude and phase of the signal. Here X is often referred to as a
phasor, whenever A, ω and ϕ have no time-dependence.
Using Euler’s formula, we can express the complex sinusoidal
signal as a function of cos and sin: 2π
T=
ω
Aei(ωt+ϕ) = A[cos(ωt + ϕ) + i sin(ωt + ϕ)]. (55)

With this formulation, it is apparent that the real and imaginary com- Figure 37: A complex sinusoidal
signal is the basic building block (basis
ponents are identical sinusoidal signals, which are 90 degrees out of
function) in a Fourier decomposition
phase with each other, because we know that cos(θ ) = sin(θ + π/2). of a signal. The fundamental period
Figure 37 shows an example of a complex sinusoidal signal. T = 2π/ω of this signal is also shown.

You can also think of a complex sinusoidal signal as a point on the


complex plane, which rotates around in a circle around the origin. Im
This is sometimes referred to as the rotating phasor. The radius of the
z(τ ) = Xeiωτ z(0) = X = Aeiϕ
circle is the amplitude of the signal A = | X |. At t = 0, the point is at
a phase angle of ϕ. This phase angle as a function of time is given by |X|
ϕ + ωt and determines the position of the point on the circle at a given ϕ + ωτ
time. If ω > 0, then the point moves counterclockwise around the ϕ Re
circle when the value of t increases. If ω < 0, then the point moves ϕ − ωτ
in the clockwise direction around the circle as the value of t increases.
Figure 38 depicts this concept.
This might at first seem like an unnecessarily complicated way to
express a sinusoidal signal, but this representation often simplifies z(τ ) = Xe−iωτ

the mathematics. For example, it is in most cases much easier to


algebraically work with the function 12 (eiϕ eiωt + e−iϕ e−iωt ) than it is Figure 38: A complex sinusoidal signal
with cos(ωt + ϕ), even though they are the same thing. on the complex plane can be seen as
a point moving around a circle. For
positive frequencies, the point rotates
around the circle in counterclockwise
direction. For negative frequencies, the
rotation is clockwise.
56

You’ll also see, later on, that a complex sinusoidal signal is an


eigenfunction for any linear time-invariant operator. It allows us to
characterize the properties of a linear time-invariant system just by
simply investigating what effect the system has on a complex sinu-
soidal signal. If we spend the effort to learn how to use a complex
sinusoidal signal, then we’ll know everything there is to know about
linear time-invariant systems!

Real-valued sinusoidal signals can be related to complex


sinusoidal signals. Using the inverse Euler relationship, we can
express a real-valued sinusoidal signal of some amplitude, angular
frequency, and phase. By taking the real or imaginary part of a
complex sinusoidal signal, we obtain a cosine or sine signal.
Using the earlier definition of a complex sinusoidal signal, we find
that:
1 h iωt i
Re{z(t)} = Xe + X ∗ e−iωt = A cos(ωt + ϕ) (56)
2
1 h iωt i
Im{z(t)} = Xe − X ∗ e−iωt = A sin(ωt + ϕ) (57)
2i
This tells us that a real-valued sinusoidal signal with angular fre-
quency ω is a sum of a positive and negative frequency complex
sinusoidal signal with angular frequencies ±ω. Note that the sign of
the phase angles of the two complex sinusoidal signals are also ±ϕ.

Angular frequency ω of a complex sinusoidal signal, determines


how many radians the phase of the signal advances per unit of time.
If the unit of time is seconds, the angular frequency has units of
radians per second (rad/s).
It is possible to relate angular frequency to frequency f in units
of cycles per time unit, i.e., how many times does the point whose
position is indicated by the complex sinusoidal signal rotate around
the circle per unit of time:

ω = 2π f (58)

If the unit of time is seconds, then the unit of frequency f is hertz


(Hz or 1/s). Sometimes cycles per second is indicated as the unit for
frequency.
I will try to consistently use the name angular frequency and sym-
bol ω when referring to a frequency that is in units of radians per
unit of t. I will use the name frequency and the symbol f when refer-
ring to the frequency that is in units of cycles per unit of t. In some
cases, when I am talking about frequency in general, and it does not
matter what the units are, I will drop the word angular and just talk
about frequency.
57

It is unfortunate that these two competing units exist. This occa-


sionally causes confusion even for people who have worked with sig-
nals for many years! It is not unusual to find a factor of 2π missing
or unnecessarily added in a textbook that is discussing the frequency
of a signal.
Throughout these lecture notes, we will most of the time call the
independent variable t of a one dimensional signal time and assign it
units of seconds. However, the physical units of t do not necessarily
have to be seconds, and t does not have to denote time.
For example, if the units of t were distance in meters, then we
would have an angular frequency ω that is in units of radians per
meter, and frequency f that is in units of cycles per meter. In physics,
a spatial frequency is typically called a wavenumber. It is customary
to use the symbol k for angular wavenumber and the Greek letter ν
for linear wavenumber. These types of spatial frequencies are often
encountered when dealing with propagating waves. There is an
example at the end of this chapter on this topic.

Complex and real-valued sinusoidal signals are periodic. The


definition of a periodic function requires that the function repeats
after a certain time period T for all values of t:

z(t) = z(t + nT ). (59)

where n ∈ Z and T ∈ R>0 .


The smallest possible positive non-zero value of T is called the
fundamental period of the function.
In the case of complex sinusoidal signals, the definition of periodic-
ity yields:

Aei(ωt+ϕ) = Aei(ω (t+T )+ϕ) = Aei(ωt+ωT +ϕ) (60)

By definition, we know that a complex sinusoidal signal is 2π-


periodic, in other words

Aeiθ = Aei(θ +2πk) (61)

for k ∈ Z. By setting θ = ωt + ϕ, this implies that ωT = 2πk.


In order to look for the smallest value of T, we inspect the case
where k = 1, from which it follows that the fundamental period is:

2π 1
T= = . (62)
ω f

The fundamental period for a complex sinusoidal signal is shown in


Figure 37.
58

Multiple complex sinusoidal signals with the same fre-


quency but possibly different phases and amplitudes can
be added together to form one complex sinusoidal signal.
This is sometimes referred to as the phasor summation11 property. 11
The signal Aeiϕ eiωt is called a phasor
Let us assume that we have N complex sinusoidal signals. Each if A, ϕ, and ω have no time depen-
dence.
one of these signals has the same angular frequency ω but different
phases ϕn and amplitudes An . If we add these together, we get one
complex sinusoidal signal with an amplitude A and phase ϕ:
N
∑ An ei(ωt+ϕn ) = Aei(ωt+ϕ) (63)
n =1

This is relatively easy to see. Let’s define Xn = An eiϕn , which allows


us to rewrite the sum in the following form:
N N
∑ An ei(ωt+ϕn ) = ∑ Xn eiωt (64)
n =1 n =1
!
N
= ∑ Xn eiωt (65)
n =1
| {z }
X
iωt
= Xe (66)
= | X |ei∠X eiωt (67)
iϕ iωt
= Ae e (68)

Here X = ∑n Xn is just a sum of constant valued complex numbers


that contain information about the phases and amplitudes of the
individual complex sinusoids.

Multiplying two complex sinusoidal signals together is


equivalent to a shift in frequency and phase. Consider two
complex sinusoidal signals with angular frequencies ω1 and ω2 , and
phase angles ϕ1 and ϕ2 . For the sake of simplicity, we’ll assume that
both signals are unit amplitude:

z1 (t) = ei(ω1 t+ϕ1 ) (69)


z2 (t) = ei(ω2 t+ϕ2 ) (70)

Multiplying together these two signals z3 (t) = z1 (t)z2 (t) we get:

ei(ω3 t+ϕ3 ) = ei[(ω1 +ω2 )t+(ϕ1 +ϕ2 )] (71)

The resulting signal z3 (t) is a complex sinusoid, at a new frequency:


ω3 = ω1 + ω2 and phase ϕ3 = ϕ1 + ϕ2 .
This property is one of the elementary operations in signal process-
ing. For example, we’ll use multiplication of two complex sinusoidals
59

signals later on as part of the proof of the Shannon-Nyquist sampling


theorem.
The frequency shift property is also widely used in radio engineer-
ing and telecommunications to shift high frequency signals to lower
frequencies and vice versa. This operation is called downconversion
or upconversion in radio engineering terminology.

It is possible to extend the concept of a complex sinu-


soidal signal into multiple dimensions. For example, a
two-dimensional complex sinusoidal signal would be of the form:

z( x, y) = Aeiϕ eikx eiℓy . (72)

Here k and ℓ are the angular frequencies in the x and y directions.


In the last example12 of this chapter, we’ll show how to use a 12
I couldn’t resist using this nice
example, even though this course is
two-dimensional complex sinusoidal signal to derive the plane wave
about 1d signals.
solution of electromagnetic waves propagating in free space from
Maxwell’s equations.

A time-shift is equivalent to a phase shift for a complex


sinusoidal signal. Let’s investigate a time-shift system, which
shifts time by a constant τ: δ(t) δ(t − τ )

y(t) = T { x (t)} = x (t − τ ), (73)

which means that the output signal at time t is the same as the input
signal at time t − τ. One could also say that the output signal is 0 τ
delayed by a time constant τ. t
For example, if x (t) = δ(t), then the location of the only non-zero
Figure 39: Time-shifted unit impulse.
valued peak in the input signal will be at t = 0. If we delay the signal
The time-shift system y(t) = x (t − τ )
by τ, then y(t) = δ(t − τ ) and the peak will now occur at t = τ. will delay the input signal by τ.
What does the time-delay system do to a complex sinusoidal
signal? If the input signal is x (t) = ei(ωt+ϕ) , the output becomes:

y(t) = T {ei(ωt+ϕ) } = ei[ω (t−τ )+ϕ] (74)



= ei(ωt+ϕ−ωτ ) (75) 2 −ωτ

= e−iωτ x (t) (76) π

π
The output of the time-shift system for a complex sinusoidal signal 2

is the input signal multiplied by e−iωτ . In other words, phase shifted ω

by −ωτ radians. There is a linear dependence of phase shift with −10 −5 5 10

frequency, as shown in Figure 40.


Because of the 2π-periodicity of the complex sinusoidal signal, the
phase shift introduced by a certain time delay is not unique. There
are infinitely many time shifts that can cause a certain phase shift at a
fixed frequency. Figure 40: Phase shift introduced to a
complex sinusoidal signal by a time-
shift system. The phase shift caused
by a constant time shift is linearly
dependent on angular frequency.
60

Let’s say that a complex sinusoidal signal is phase shifted by phase


θ. Due to the 2π periodicity, we have to consider all values θ + 2πk
with k ∈ Z. If we attribute this phase shift only to a time-delay, then
the following relationship holds

ei(θ +2πk) = e−iωτ . (77)

This means that all the time delays that could have caused a phase
shift θ are:
τ = −ω −1 (θ + 2πk). (78)

This problem is sometimes called phase-time ambiguity.


Sometimes it is possible to solve this ambiguity by making the
assumption that the true value of the time-shift is within a certain
interval τmin < τ < τmax and find a unique solution for τ.
Another possible solution to the phase-time ambiguity is to mea-
sure the rate-of-change of the phase as a function of frequency:


= −τ. (79)

This type of measurement is used by devices called network analyz-
ers for measuring the length of a cable. This principle is also used in
the radio astronomical method called very long baseline interferom-
etry (VLBI) to determine the relative time delay between a point-like
radio emission source and two different radio telescopes.

Example: Very long baseline interferometry


Figure 41: In radio astronomy, the
relative time delay between two Earth-
A point-like radio star emits a signal across a broad range of frequen-
based radio telescopes and a point like
cies. This means that we can observe the signal originating from the radio emission source is determining
radio star with multiple angular frequencies ω. We can denote the the rate of change of the relative phase
as a function of frequency. Figure:
signal originating from the star at a certain frequency with: NASA GFSC.

z(t, ω ) = A(ω )eiϕ(ω ) eiωt (80)

Here A(ω ) ∈ R≥0 is the amplitude and ϕ(ω ) is the phase of the
signal at angular frequency ω. Because the signal is generated by
an enormous collection of charged particles (primarily electrons)
in an accelerating motion, the amplitudes and phases are random
variables.
If we measure the signal originating from the radio star at two
different places with a radio telescope simultaneously, the time of
arrival of the signal between the two telescopes will differ by τ due to
geometry (see Figure 41). It is this value of τ that is often of interest,
when, e.g., estimating the orientation of Earth relative to the stars.
61

Let’s denote the signal received with telescope 1 with z1 and the
signal received with telescope 2 with z2 :

z1 (t, ω ) = A(ω )eiϕ(ω ) eiωt (81)


z2 (t, ω ) = A(ω )eiϕ(ω ) eiω (t−τ ) (82)

We’ll assume that z1 is the same signal as z2 , with only one exception,
the signal z2 is delayed by a time constant τ due to the receiver being
further away from the source of the signal13 . 13
For radio signals, we need the radio
In order to determine the value τ, we can use the following opera- source to be in the Fraunhofer far-field
in order for this to be valid.
tion:
z1 (t, ω )z2∗ (t, ω ) = | A(ω )|2 eiωτ (83)

The random phase ϕ(ω ) cancels out with this operation, as well as
the oscillating term ωt. The phase of this cross-product only depends
on frequency and time shift τ:

∠z1 (t, ω )z2∗ (t, ω ) = θ (ω ) = ωτ (84)

If we measure the cross-product z1 (t, ω )z2∗ (t, ω ) across a sufficiently


wide range of frequencies, we can estimate τ using:


=τ (85)

This is a very simplified description of how the very long baseline
interferometry technique used in radio astronomy and geodesy
allows us to precisely measure the relative difference in time of
arrival of a signal originating from a point-like radio source when
measured with two different radio receivers.

Python example: shifting in frequency

The following Python example demonstrates in practice Equation 71


which tells us that when multiplying two complex sinusoidal signals
together, the resulting signal will have a frequency corresponding to
the sum of the two frequencies of the multiplied signals.
import m a t p l o t l i b . pyplot as p l t
import numpy as np

# Time v e c t o r 0 t o 1 seconds , 1000 Hz sampler r a t e .


t = np . arange ( 1 0 0 0 ) / f l o a t ( 1 0 0 0 . 0 )
# Frequency o f 10 Hz .
om0 = 2 . 0 * np . p i * 1 0 . 0
# Frequency o f 5 Hz .
om1 = 2 . 0 * np . p i * 5 . 0
# C r e a t e complex s i n u s o i d a l s i g n a l .
z = np . exp ( 1 j * om0 * t )
# S h i f t z i n frequency by m u l t i p l y i n g with another
# complex s i n u s o i d a l s i g n a l o f frequency om1 .
62

z _ s h i f t e d = z * np . exp ( 1 j * om1 * t )

# Plot signals .
p l t . subplot (211)
p l t . p l o t ( t , z . r e a l , l a b e l = " Real " )
p l t . p l o t ( t , z . imag , l a b e l = " Imag " )
plt . t i t l e ( " Original signal " )
p l t . x l a b e l ( " Time ( s ) " )
p l t . legend ( )
p l t . subplot (212)
p l t . p l o t ( t , z _ s h i f t e d . r e a l , l a b e l = " Real " )
p l t . p l o t ( t , z _ s h i f t e d . imag , l a b e l = " Imag " )
p l t . t i t l e ( " Frequency s h i f t e d s i g n a l " )
p l t . legend ( )
p l t . x l a b e l ( " Time ( s ) " )
p l t . show ( )

Listing 14: 005_mixing/mixing.py

Example: Electromagnetic waves

This example will show that complex sinusoidal signals are a useful
mathematical tool also for solving differential equations by deriving
a plane wave solution of electromagnetic waves f rom Maxwell’s
equations.
Don’t worry if you are not familiar with these equations. I mainly
want you to pay attention to how the differential equation is solved
for a plane wave using a two-dimensional complex sinusoidal signal
⃗E( x, t) = ŷE0 eikx eiωt . Because the exponential function is easy to
differentiate, it is easy to apply this type of function for solving
differential equations.
Assuming that there are no currents (⃗J = 0) or space charges
(ρ = 0), Maxwell’s equations are as follows:

∇ · ⃗E = 0 (86)
∇ · ⃗B = 0 (87)
∂⃗B
∇ × ⃗E = − (88)
∂t
∂⃗E
∇ × ⃗B = ϵ0 µ0 (89)
∂t
If we curl Faraday’s law, we get:

∇ × (∇ × ⃗E) = − ∇ × ⃗B (90)
∂t
If we then use the vector identity ∇ × (∇ × ⃗E) = ∇(∇ · ⃗E) − ∇2 ⃗E and
include Gauss’ law for electric field ∇ · ⃗E = 0 in the case when there
are no charges, we get

∇2 ⃗E = ∇ × ⃗B (91)
∂t
63

If we now insert the Maxwell–Ampere’s law (Equation 89) into


Faraday’s law (Equation 88), we get:

∂2 ⃗E 1
− ∇2 ⃗E = 0. (92)
∂t2 µ 0 ϵ0
This type of equation is called a wave equation. You can also derive
the same equation for the magnetic field, but we are not going to do
that here.
Now let us assume that a solution to the wave equation is of the
form:
⃗E( x, t) = E0 ŷe−ikx eiωt (93)
Here E0 ∈ C is a complex number that determines the amplitude and
phase of the electric field. The term ŷ is a unit vector in the y-axis
direction. In other words, a plane wave, where the electric field in
the y-z plane is constant-valued. The electric field only changes as a
function of position in the x-axis as a function of time t.

Figure 42: A plane wave solution


to Maxwell’s equations, describ-
ing propagation of electromag-
netic waves in space and time. The
script that produced this plot is
007_2dwave/em_plane_wave.py.

It is easy to differentiate a complex exponential function twice in


space and time. Remember that the Laplacian operator, which is a
linear system, is defined as:

∂2 ∂2 ∂2
∇2 = + + (94)
∂x2 ∂y2 ∂z2
Because the electric field is constant in the y-z plane, only the second
derivative in the x-direction remains. Using the two-dimensional
64

plane wave formulation of Equation 93, we can see that

∂2 ⃗E
= −ω 2 ⃗E( x, t) (95)
∂t2
∇2 ⃗E = −k2 ⃗E( x, t). (96)

And therefore, the wave equation becomes:

∂2 ⃗E 1
2
− ∇2 ⃗E = 0 (97)
∂t µ 0 ϵ0
(c2 k2 − ω 2 )⃗E( x, t) = 0 (98)

I’ve taken the liberty of changing the constant c = (µ0 ϵ0 )−1/2 for
reasons that will become apparent soon. The constant µ0 is perme-
ability of free space, and ϵ0 is permittivity of free space. The value of
c ≈ 3 · 108 m/s.
In order for the two-dimensional complex sinusoidal signal to be
consistent with Maxwell’s equations, either E( x, t) = 0 or c2 k2 − ω 2 =
0. The trivial solution E( x, t) = 0 is not very interesting, as there
would be no electric field. The latter describes a solution with a
non-zero electric field, as long as the following relationship:
ω
k=± (99)
c
is satisfied. This means that the following two-dimensional signal is a
solution to the Maxwell’s equations:

⃗E( x, t) = E0 ŷe−i(ω/c)x eiωt (100)


−i2πλ−1 x
= E0 ŷe ei2π f t (101)

Here, I’ve used the relation 2π f = ω. I’ve also used the wavelength
of the electromagnetic wave, which is λ = c/ f . In other words,
we have used a two-dimensional complex sinusoidal signal and
the Maxwell’s equations to describe how electromagnetic waves
propagate in space and time.
I am showing the real part of the plane wave solution in Figure 42.
In this case, the frequency is chosen to be f = 2.4 GHz, which is the
frequency typically used by microwave ovens and wireless internet
(which you are probably using right now). Try to figure out what is
the velocity that a region of constant phase propagates in the positive
x-axis direction at.
65

Exercises: Sinusoidal Signals

Hint: You can often write a Python program to verify your result if
you are not sure if it is correct!

1. You may have heard somebody say that noise-cancelling head-


phones rely on playing an audio signal where each spectral compo-
nent of the signal is perfectly “out of phase” with the input signal.
Consider a noise source that is sinusoidal and described by the
following equation:

xnoise (t) = cos(ωt + ϕ). (102)

Your noise-cancelling system can generate a signal xcancel (t) with


the same amplitude and frequency, but a different phase:

xcancel (t) = cos(ωt + ϕc ) (103)

What value of ϕc results in perfect cancellation

xcancel (t) + xnoise (t) = 0? (104)

2. Prove that:

A cos(ωt + ϕ) = a cos(ωt) + b sin(ωt). (105)

How is A and ϕ related with a and b? Do this by converting the


cosine and sine signals to complex sinusoidal signals (Equations
56 and 57). Use the phasor summation property found in Equation
68.
The representation of the form a cos(ωt) + b sin(ωt) is often pre-
ferred over A cos(ωt + ϕ) when estimating the phase and ampli-
tude of a real-valued sinusoidal signal from noisy measurements,
because we get a set of linear equations with two unknown con-
stants a and b.

3. Consider the following signal, which consists of two sinusoidal


signals multiplied with one another:

x (t) = cos(ω1 t) sin(ω2 t). (106)

Show that it is possible to express this signal in the form:

x (t) = a sin(ω3 t) + b sin(ω4 t). (107)

What is the value of a, b, ω3 , and ω4 ?


66

4. The electromagnetic wave example demonstrated that a two-


dimensional complex sinusoidal signal of the form:

⃗E( x, t) = ŷE0 eikx eiωt (108)

is a solution to the Maxwell’s equation in free space.


Your task is to show that a real-valued solution Re{⃗E( x, t)} with
⃗E( x, t) ̸= 0 is also a solution to the wave equation, so that we know
that it is possible to have solutions with a real-valued electric field.
In other words, show that Equation 98 holds also in this case.

5. This is a follow-up question to the previous one. Let’s say that we


had an arbitrary number of solutions to the wave equation with Figure 43: Light can be seen as a
different angular frequencies ωn and phasors En : superposition of electromagnetic waves
with different amplitudes, phases,
⃗E( x, t) = ŷ ∑ En ei(ωn /c)x eiωn t (109)
and frequencies. This can be easily
visualized in practice with the help of a
n
prism or a diffraction grating.
Would this still satisfy the Maxwell’s equations (i.e., the wave
equation)? Can electromagnetic waves be composed of a super-
position of two-dimensional plane waves of different frequencies
(wavelengths, or colors), which each individually satisfy the wave
equation?

6. Copy the example program in Listing 14 that performs mixing


and run it. The example shows how to shift a complex exponential
signal with a certain frequency to another frequency. Modify the
script in such a way that the resulting complex exponential signal
z_shifted has a frequency of 42 Hz. Use the plot to verify that the
frequency of the signal is approximately 42 Hz.

7. Go back to the guitar amplifier program 003_guitar/amplifier.py.


We will now add one more effect. We’ll modify the program so
that it implements a modulation effect.

a) Multiply the clean guitar signal with a 5 Hz sinusoidal signal.


Plot the signal.
# M ul ti pl y with s i n u s o i d .
x_mod = x * np . cos ( 2 . 0 * np . p i * time_vec * 5 . 0 )
# Plot result .
p l t . p l o t ( time_vec , x_mod )
p l t . show ( )

b) Write the output into an audio file and play the result. What
does the output sound like?
# Write compressed output t o wav f i l e .
s i o . w r i t e ( " g u i t a r _ l e s l i e . wav" , sample_rate , x_mod )
67

c) Change the frequency of the modulating sinusoid from 5 Hz


to 2.5 Hz. Predict what the audio will sound like. Play the
resulting audio to confirm your hypothesis.

If you want to reflect on what is going on here, think of the origi-


nal audio signal x (t) as something that consists of a sum of sinu-
soidal signal components14 14
We’ll introduce this properly later on

x (t) = ∑ An cos(ωn t + ϕn ) (110)


n

When multiplying this signal with a sinusoidal signal of frequency


ω, we get a modulated audio signal y(t):

y(t) = x (t) cos(ωt) (111)


" #
y(t) = ∑ An cos(ωn t + ϕn ) cos(ωt). (112)
n

Each spectral component of a signal x (t) gets the same treatment


as you already investigated in Exercise 3.
68

Suggested solutions: Sinusoidal Signals

1. If xnoise (t) = cos(ωt + ϕ) and xcancel (t) = cos(ωt + ϕc ) and:

xnoise (t) + xcancel (t) = 0,

then we must have:

cos(ωt + ϕ) + cos(ωt + ϕc ) = 0,

for all t. The phase doesn’t depend on t, so it is the same for every
t ∈ R, in particular for t = 0 so:

cos(ϕ) = − cos(ϕc ),
cos(ϕ) = cos(π − ϕc ),
ϕ + 2πk = π − ϕc + 2πl,

for k, l ∈ Z, which gives ϕc = π − ϕ + 2π (l − k) = π − ϕ + 2πm,


where m ∈ Z. Take m = 0 to obtain a simple solution, which gives
ϕc = π − ϕ. Hence, the canceling signal must be of the form:

xcancel (t) = cos(ωt + (π − ϕ)).

A simple test program to verify this is shown in Listing 15 with an


arbitrary choice of ω, ϕ and m. If the code is run, two signals of
opposite amplitude are shown, which cancel out when added, as
hoped.
import m a t p l o t l i b . pyplot as p l t
import numpy as np

N = 1000 # Number o f sample p o i n t s .


om = 2 . 6 # Angular frequency .
phi = 3 . 1 # Phase .
m = 0 # Integer .
# Compute t h e phase needed t o c a n c e l t h e s i g n a l .
p h i _ c a n c e l = np . p i − phi + 2 * np . p i *m

# P a r t i t i o n t h e t a x i s with a range from


# 0 t o 4 p i with N samples .
t = np . l i n s p a c e ( s t a r t =0 , s t o p =4 * np . pi , num=N)

# Original signal .
x = np . cos (om * t + phi )

# Noise c a n c e l i n g s i g n a l .
y = np . cos (om * t + p h i _ c a n c e l )

plt . plot ( t , x )
plt . plot ( t , y)
# C a l l t h i s i f needed .
# p l t . show ( )

Listing 15: Noise canceling signal


69

2. Have that:
a iωt b
a cos(ωt) + b sin(ωt) = (e + e−iωt ) + (eiωt − e−iωt ),
2 2i
or written out:
a iωt a −iωt b b
a cos(ωt) + b sin(ωt) = e + e + eiωt − e−iωt .
2 2 2i 2i
Using the phasor summation property we have:
a b 1
X= ∑ Xn = 2 + 2i =
2
( a − bi ),
n

and
a b 1
Y= ∑ Ym = 2 − 2i =
2
( a + bi ).
m
Then we have that:
r
1 2 1p 2
|X| = ( a + b2 ) = a + b2 ,
4 2
r
1 2 1p 2
|Y | = ( a + b2 ) = a + b2 ,
4 2
and
−b


∠X = arctan = ϕ,
a
 
b
∠Y = arctan .
a
Then the whole expression can be rewritten as:
1p 2 1p 2
a cos(ωt) + b sin(ωt) = a + b2 eiϕ eiωt + a + b2 e−iϕ e−iωt ,
2 2
1p 2 1p 2
= a + b2 ei(ωt+ϕ) + a + b2 e−i(ωt+ϕ) ,
2   2
p 1 i(ωt+ϕ) 
= a2 + b2 e + e−i(ωt+ϕ) ,
2
p
= a2 + b2 cos(ωt + ϕ),
= A cos(ωt + ϕ).

Here A = a2 + b2 and ϕ = − arctan(b/a), where arctan(b/a)
should give the angle corresponding to which quadrant the point
( a, b) lies in the xy-plane.

3. Consider the signal:

x (t) = cos(ω1 t) sin(ω2 t),

which we can write as:


1 iω1 t 1 1
x (t) = (e + e−iω1 t ) (eiω2 t − e−iω2 t ) = [ei(ω1 +ω2 )t − ei(ω1 −ω2 )t + ei(−ω1 +ω2 )t − ei(−ω1 −ω2 )t ].
2 2i 4i
70

Collecting terms we get:

1 i ( ω1 + ω2 ) t 1
x (t) = [e − e−i(ω1 +ω2 )t ] + [ei(−ω1 +ω2 )t − e−i(−ω1 +ω2 )t ],
4i 4i
1 1
= sin((ω1 + ω2 )t) + sin((ω2 − ω1 )t).
2 2
This yields the values a = 1/2 and b = 1/2, while ω3 = ω1 + ω2
and ω4 = ω2 − ω1 .

4. The plane wave:


E( x, t) = E0 ei(kx+ωt) ∂y
is a solution to the wave equation. Consider the real part Re{ E( x, t)}:

1 1
Re{ E( x, t)} = [ E( x, t) + E∗ ( x, t)] = E0 (ei(kx+ωt) + e−i(kx+ωt) )∂y = E0 cos(kx + ωt)∂y .
2 2
Recall that the wave equation is:

1
∂2t E − ∇2 E = 0,
µ 0 ϵ0

which gives:
 
2 1 2 1 2 2
−ω E0 cos(kx + ωt)∂y + k E0 cos(kx + ωt)∂y = E0 k − ω cos(kx + ωt)∂y = 0,
µ 0 ϵ0 µ 0 ϵ0

since k2 c2 − ω 2 = 0, as c = 1/µ0 ϵ0 . In conclusion, the signal:


p

Re{ E( x, t)} = E0 cos(kx + ωt)∂y ,

is a solution to the wave equation.

5. If we have an arbitrary number of solutions to the wave equation,


each with different phasors En and different angular frequencies
ωn . Then a superposition:

E( x, t) = ∑ En ei(ωn /c)x eiωn t ∂y ,


n

is also a solution. This follows by the linearity of the wave equa-


tion.

6. The code from the notes has been modified such that the output
signal has a frequency of approximately 42.0 Hz.
import m a t p l o t l i b . pyplot as p l t
import numpy as np

# Time v e c t o r 0 t o 1 seconds , 1000 Hz sample r a t e .


t = np . arange ( 1 0 0 0 ) / 1 0 0 0 . 0

# Frequency o f 1 0 . 0 Hz and 3 2 . 0 Hz .
om0 = 2 . 0 * np . p i * 1 0 . 0
om1 = 2 . 0 * np . p i * 3 2 . 0
71

# C r e a t e complex s i n u s o i d a l s i g n a l .
z = np . exp ( 1 j * om0 * t )

# S h i f t z i n frequency by m u l t i p l y i n g with another


# complex s i n u s o i d a l s i g n a l o f frequency om1 ,
# t h e r e s u l t has a frequency o f 4 2 . 0 Hz = 1 0 . 0 Hz + 3 2 . 0 Hz .
z _ s h i f t e d = z * np . exp ( 1 j * om1 * t )

p l t . subplot (211)
p l t . p l o t ( t , z . r e a l , l a b e l = " Real " , c o l o r = " d a r k v i o l e t " )
p l t . p l o t ( t , z . imag , l a b e l = " Imag " , c o l o r = " lime " )
plt . t i t l e ( " Original signal " )
p l t . x l a b e l ( " Time ( s ) " )
p l t . legend ( )
p l t . subplot (212)
p l t . p l o t ( t , z _ s h i f t e d . r e a l , l a b e l = " Real " , c o l o r = " d a r k v i o l e t " )
p l t . p l o t ( t , z _ s h i f t e d . imag , l a b e l = " Imag " , c o l o r = " lime " )
p l t . t i t l e ( "New s i g n a l " )
p l t . x l a b e l ( " Time ( s ) " )
p l t . legend ( )
plt . tight_layout ()
# C a l l t h i s i f needed .
# p l t . show ( )

Listing 16: Adding frequencies

Running the code in Listing 16 yields the following output:

Figure 44: Signal with 42 Hz frequency

7. The following code is a modification that multiplies the signal


with a periodic 5.0 Hz signal:

a) Listing 17 provides a suggested solution.


72

import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . i o . w a v f i l e as s i o

wav = s i o . read ( " g u i t a r _ c l e a n . wav" )


s a m p l e _ r a t e = wav [ 0 ] # Sample r a t e .
x = wav [ 1 ] [ : , 0 ] # Read only one s t e r e o channel .

# C r e a t e time v e c t o r ( independent v a r i a b l e ) .
time_vec = np . arange ( l e n ( x ) ) / f l o a t ( s a m p l e _ r a t e )

# M ul ti pl y t h e s i g n a l with a c o s i n e wave
# with frequency f = 5 . 0 Hz .
out = x * np . cos ( 2 . 0 * np . p i * time_vec * 5 . 0 )

# P l o t o r i g i n a l and modified .
p l t . p l o t ( time_vec , out , l a b e l = " Output 5 . 0 Hz" , c o l o r = "
darkviolet " )
p l t . p l o t ( time_vec , x , l a b e l = " O r i g i n a l " , c o l o r = " lime " )
p l t . legend ( )
p l t . x l a b e l ( " Time $ t $ " )
p l t . y l a b e l ( " R e l a t i v e a i r p r e s s u r e $y ( t ) $ " )
# C a l l t h i s i f needed .
# p l t . show ( )

Listing 17: Modifying signals

Running Listing 17 produces Figure 45.

Figure 45: Original and modified guitar


signal
73

b) Adding the following code to Listing 17 will save the result as


an audio file.
out = 0 . 9 * out/np . max ( np . abs ( out ) )

# M ul ti pl y t h e s i g n a l s .
out2 = x * n . cos ( 2 . 0 * n . p i * time_vec * 2 . 5 )

# Write compressed output t o wav f i l e .


s i o . w r i t e ( " g u i t a r _ l e s l i e . wav" , sample_rate , np . a r r a y ( out ,
dtype=np . f l o a t 3 2 ) )

Listing 18: Saving an audiofile

Listening to the audio file, we hear that the guitar sound has a
wavy oscillating effect.
c) Based on the previous task, the result of multiplying with a 2.5
Hz signal will result in the same effect, but the oscillation will
be slower. After testing this by running Listing 18 this is exactly
what happens.
Fourier Series

This chapter will introduce the Fourier series, which is used to express
a periodic signal as a sum of sinusoidal signals. We will explore the
following topics:

• Signals as a sum of sinusoids.

• Periodic signals, fundamental frequency and fundamental period.

• Expressing a periodic function as a sum of complex sinusoidal x̂ (ω )


c2
signals (Fourier series synthesis).

• Determining the phase and amplitude of each Fourier series ···


cN
coefficient (Fourier series analysis). c1

• Derivation of the continuous-time Fourier transform. ω

ω1 ω2 ωN
Here is a signal represented as a sum of complex sinu-
soidal signals:
N Figure 46: A spectral representation
x (t) = ∑ ck eiωk t . (113) of a signal consisting of N complex
k =1 sinusoidal signals. It is not a coinci-
dence that I’m using the same arrow
The symbol ck = Ak eiϕk ∈ C is used for complex-valued constants symbol here that I used earlier when
that contain information about the amplitudes Ak ∈ R≥0 and phases introducing the Dirac delta function.

ϕk ∈ R of the complex exponential signals with angular frequencies


ωk ∈ R.
It is possible to plot the complex coefficients ck as a function of
angular frequency ω as shown in Figure 46, which depicts a function
of the form:
N
x̂ (ω ) = ∑ ck δ ( ω − ωk ) . (114)
k =1

This is a spectral or frequency domain representation of the signal


described in Equation 113. In this case, one can think of the spectrum
as consisting of infinitely narrow spectral lines, each defined by
complex constants ck that are located at angular frequencies ωk . We
will derive Equation 114 later when discussing the continuous-time
Fourier transform.
76

A spectral representation of a cosine signal consists of two


frequency components. One with a positive and one with a negative
A −iϕ A iϕ
frequency. This is a direct consequence of Euler’s formula. 2e 2e

Consider the following signal:

x (t) = A cos(ωt + ϕ) , (115) ω


ω −1 ω1
where A ∈ R≥0 . We can use Euler’s formula to obtain the spectral
representation:
A iϕ iωt A −iϕ −iωt Figure 47: A spectral representation
x (t) = e e + e e (116)
2 2 of a cosine signal consists of two
iω1 t frequency components: 12 Aeiϕ eiωt and
= c1 e + c−1 eiω−1 t (117) 1 −iϕ e−iωt . Here A is a non-negative
2 Ae
= ∑ ck e iωk t
. (118) real-valued amplitude. Blue denotes the
k ∈{−1,1} real and the red denotes the imaginary
component of ck .
The second line is simply representing the first line in the format of
Equation 113.
Note that there is symmetry within the frequency components.
The pairing of frequencies exists as ω1 = −ω−1 . The complex
constants are also conjugate symmetric c−1 = c1∗ . We’ll later on
see that this type of conjugate symmetry exists for the frequency
components of all real-valued signals.

A Fourier series is a spectral representation of a signal


where each frequency is a multiple of some common base
frequency ω:
N N
x (t) = ∑ ck eiωk t = ∑ ck eiℓk ωt . (119)
k =1 k =1

Here ℓk ∈ Z is integer valued. If we compare Equation 119 with the


first definition of a spectral representation in Equation 113, we can
see that all angular frequencies are integer multiples of a positive
valued angular base frequency ω ∈ R≥0 :

ωk = ℓk ω . (120)

The largest possible value ω for a set of frequencies {ω1 , ω2 , · · · , ω N }


is called the fundamental angular frequency of the signal.
The fundamental period T of the signal is related with the funda-
mental angular frequency ω as follows:


T= . (121)
ω

The definition of periodicity for a function is that the following


condition is satisfied:
x (t) = x (t + T ) . (122)
77

It is relatively easy to show that periodicity follows from the def-


inition of the Fourier series, by inspecting the periodicity of each
frequency component separately:
x (t)
T T
ck eiℓk ωt = ck eiℓk ω (t+T ) = ck ei(ℓk ωt+2π ℓk ) = ck eiℓk ωt . (123)

We have used the definition of T in Equation 121. Because all fre-


quency components are periodic signals with period T, it follows that
the sum of all frequency components is periodic as well.
A consequence of Equation 120 is that the ratios of all pairs of
t
angular frequencies ωk and ωℓ in Equation 113 need to be rational
numbers. In other words, it must be possible to express them in the
following form
ωk n
= ∈Q (124)
ωℓ m

in order for the signal to be periodic. Here n and m are arbitrary inte-
Figure 48: Periodic function ei2π9t −
gers and k and ℓ are all unique combinations of the frequency indices.
ei2π15t . Blue denotes the real and red
If the ratio of two real numbers ω1 , ω2 ∈ R is a rational number denotes the imaginary component of
ω1 /ω2 ∈ Q, the numbers ω1 and ω2 are called commensurable. All the signal.

angular frequencies for a Fourier series need to be commensurable


relative to one another in order for the signal to be periodic.
How does one determine the fundamental frequency ω? It is
possible to apply Euclid’s algorithm15 ω for the set of frequencies ωk . 15
Note that this is sometimes referred to
This algorithm will only produce a result in a finite number of steps as the greatest common divisor, but this is
only defined for integers.
if the set of numbers {ωk } are commensurable, so it cannot be easily
used as a test of periodicity.

Here is an example. Let us assume that a signal is defined as

x (t) = c1 eiω1 t + c2 eiω2 t = ei2π9t − ei2π15t . (125)

Is this signal periodic? If so, what is its fundamental frequency ω and


fundamental period T?
In order for this signal to be periodic, ω1 and ω2 must be commen-
surable. In other words, ω1 /ω2 ∈ Q. This is the case, as it is clear
that ω1 /ω2 = 3/5 is a rational number.
This signal has two unique angular frequencies: ω1 = 2π9 and
ω2 = 2π15. Let’s use Euclid’s algorithm16 on these two numbers to 16
The general principle behind the
determine the fundamental angular frequency ω. We start with the algorithm is that if ω1 = ω ℓ1 and
ω2 = ω ℓ2 with ℓ1 and ℓ2 integers, then
two numbers: (2π9, 2π15). Then subtract the smallest number from ω1 − ω2 = ω (ℓ1 − ℓ2 ) is still divisible
the largest number 2π15 − 2π9 = 2π6 and replace the larger number with ω.

with the remainder: (2π9, 2π6). We repeat this process until both
78

numbers are the same. All the iterations are shown below:

(2π15, 2π9)
(2π15 − 2π9, 2π9) = (2π6, 2π9)
(2π6, 2π9 − 2π6) = (2π6, 2π3)
(2π6 − 2π3, 2π3) = (2π3, 2π3).

And we thus find that the fundamental frequency is ω = 2π3 and,


using Equation 121, we find that T = 1/3.

The Fourier series is a spectral representation of a peri-


odic function and is defined as

N 2π kt
x N (t) = ∑ c k ei T . (126)
k =− N
Figure 49: Jean-Baptiste Joseph Fourier

The fundamental period of a periodic signal is related to the funda-


mental angular frequency as follows: T = 2π/ω. We can turn this
around and express the fundamental angular frequency as a function
of the fundamental period ω = 2π/T, as we have done above.
This Fourier series is named after Jean-Baptiste Joseph Fourier
(1768–1830). He introduced this representation for the purpose
of solving the heat equation with periodic boundary conditions
using a series of sinusoidal characteristic solutions. Perhaps the
most revolutionary outcome of his study was that a wide range
of functions can be represented as a sum of elementary sinusoidal
functions.
Given a set of Fourier series coefficients ck , we can synthesize a
signal x N (t). This procedure is called synthesis. This formula shown
in Equation 126 is also called the Fourier series synthesis equation.
The inverse operation, determining coefficients ck ∈ C from a
certain periodic signal x (t), is called analysis. We’ll slowly work our
way to this formula.
We will not study the convergence of the Fourier series, which
means investigating if lim N →∞ x N (t) = x (t). This is outside the scope
of this course.

In order to find the analysis formula, we’ll first need to


introduce the concept of basis functions and an inner

product. A Fourier series basis function is defined as ψk (t) = ei T kt .
We can use this to write the Fourier series in the following form:

N 2π kt
N
x N (t) = ∑ c k ei T = ∑ ck ψk (t) . (127)
k =− N k =− N
79

The terms ψk (t) can be seen as a set of orthogonal basis functions for
a periodic function with a period T. What does it mean that basis
functions ψk (t) are orthogonal? We’ll first need to define an inner
product.
For periodic functions a(t) and b(t) with period T, an inner prod-
uct is defined as:
Z t0 + T Z
⟨ a(t), b(t)⟩ = a(t)b∗ (t)dt = a(t)b∗ (t)dt , (128)
t0 T

where t0 is an arbitrary value of time, and T is the fundamental


period of the signal. Because of the periodicity of the signals, we can
evaluate the integral at any offset t0 we wish to. The right-hand side
is just an alternative way to denote the equation in the middle, i.e.,
that we integrate over one period of the periodic function.
When taking the inner product between two basis functions, we
obtain:
Z
⟨ψℓ (t), ψk (t)⟩ = ψℓ (t)ψk∗ (t)dt (129)
T
Z
2π (ℓ− k ) t
= ei T dt . (130)
T
Let’s break this down into two cases. First let’s look at ℓ = k. We
know that ei(2πt/T )(ℓ−ℓ) = ei·0 = 1. It is easy to see that:
Z T
⟨ψℓ (t), ψℓ (t)⟩ = dt (131)
0
=T. (132)
I’ve arbitrarily chosen to use t0 = 0 when evaluating the integral (see
the definition of the inner product in Equation 128). I would have
obtained the same result with any value of t0 .
Let’s look at the other case where ℓ ̸= k. This means that the
frequencies of the complex sinusoidal signals are different. We know
that ℓ − k is a non-zero integer. Evaluating the inner product in this
case gives us:
Z T
2π t (ℓ− k )
⟨ψℓ (t), ψk (t)⟩ = ei T dt (133)
0
T
T 2π
= ei T t(ℓ−k) (134)
2πi (ℓ − k ) t =0
T  2π
i T T (ℓ−k ) 2π

= e − ei T 0(ℓ−k) (135)
2πi (ℓ − k)
T
= (1 − 1) (136)
2πi (ℓ − k)
=0. (137)
We can combine these two results conveniently as:
⟨ψℓ (t), ψk (t)⟩ = Tδℓ,k . (138)
80

The integral thus evaluates to T when ℓ = k and zero otherwise.


We have used the Kronecker delta function δk,ℓ for convenience. It is
defined as: (
1 when ℓ = k
δk,ℓ = . (139)
0 when ℓ ̸= k

We now have what we need to introduce the analysis formula.

The Fourier series analysis formula is defined as:

1 1 1
Z Z
2π kt
ck = ⟨ x (t), ψk (t)⟩ = x (t)ψk∗ (t)dt = x ( t ) e −i T dt . (140)
T T T T T

This formula can be used to determine what the Fourier series co-
efficients are for a periodic function x (t). The proof for the analysis
formula relies on the orthogonality of basis functions. We’ll make the
assumption that there exists a Fourier series representation for the
signal x (t) in the form of an infinite sum and that this sum converges
uniformly17 : 17
The mathematician A.N. Kolmogorov
∞ pointed out in his 1923 paper several
i 2π
x (t) = ∑ cℓ e T ℓt . (141) examples of functions for which the
ℓ=−∞ Fourier series diverges. The topic
Fourier series convergence is beyond
If this is the case, then the inner product, i.e., the analysis equation, the scope of this course.
has the following result: Andrey Kolmogoroff. Une série de
Fourier-Lebesgue divergente presque
∞ partout. Fundamenta mathematicae, 1(4):
!
1 1
Z
2π 2π

T
⟨ x (t), ψk (t)⟩ = ∑ cℓ ei T ℓt e−i T kt dt
T T ℓ=−
(142) 324–328, 1923


1
Z
2π ℓ t 2π kt
=
T ∑ c ℓ ei T e −i T dt (143)
ℓ=−∞ T

1
Z
2π (ℓ− k ) t
=
T ∑ cℓ
T
ei T dt (144)
ℓ=−∞

1
=
T ∑ cℓ Tδℓ,k (145)
ℓ=−∞
= ck . (146)

This proves that we can go back and forth between the Fourier syn-
thesis and analysis formula, as long as there exists a Fourier series
representation that converges uniformly for the periodic signal x (t).

We now have the synthesis and analysis formulas for a


Fourier series. The synthesis formula allows us to obtain the time
domain representation of a periodic signal using Fourier coefficients:

N 2π kt
x N (t) = ∑ c k ei T . (147)
k =− N
81

The inverse operation, i.e., obtaining the Fourier coefficients ck from a


periodic function x (t), is as follows:

1
Z
2π kt
ck = x ( t ) e −i T dt . (148)
T T

What about real-valued signals? We can use Euler’s formula


to look at the real part of the Fourier series synthesis formula. I’ll
assume again that x (t) is a periodic function that can be represented
using an infinite sum of complex sinusoidal signals:
∞ 2π ℓ t
x (t) = ∑ c ℓ ei T . (149)
ℓ=−∞

The real part of the signal x (t) is:

1
Re{ x (t)} = ( x (t) + x ∗ (t)) (150)
2
1 ∞ 2π 1 ∞ ∗ −i 2π ℓt
= ∑
2 ℓ=−∞
c ℓ ei T ℓt + ∑ cℓ e T
2 ℓ=−
(151)

1 1 ∞ 2π 2π
= (c0 + c0∗ ) + ∑ [(cℓ + c∗−ℓ )ei T ℓt + (c∗ℓ + c−ℓ )e−i T ℓt ]
2 2 ℓ=1
(152)
∞  

= A0 + ∑ Aℓ cos T
ℓt + ϕℓ . (153)
ℓ=1

For the last part, I’ve introduced new variables: A0 = 21 (c0 + c0∗ ) ∈ R,
Aℓ = |cℓ + c∗−ℓ | ∈ R≥0 and ϕℓ = ∠(cℓ + c∗−ℓ ) ∈ R.
When analyzing Fourier series coefficients for a real-valued func-
tion, you will find that they come in conjugate symmetric pairs,
which will allow you to write the Fourier series synthesis equation
in the form shown on the last line of Equation 153. Also, the Fourier
series synthesis equation is sometimes introduced in this form in
older texts.

The zero frequency or the direct current (DC) frequency


component c 0 often has a special meaning. In the case of real-valued
signals, this is the only frequency component that does not have a
positive and negative frequency conjugate symmetric pairing. The
DC component also has the meaning that it is the mean value of the
signal:
1
Z
c0 = x (t)dt . (154)
T T

A partial sum Fourier series can be used to approximate


a periodic signal. This is especially useful, if one knows that the
82

signal is band-limited, and one knows a priori that higher order terms
(high frequency components) are not present in the signal, or they x (t)
have such low magnitudes that they are not significant.
A partial sum synthesis formula only uses a subset of the Fourier T
coefficients:
xS (t) = ∑ ck ei2πkt/T , (155)
k∈S

where S is a set of Fourier series coefficient indices.


A partial sum approximation of this type is often used in audio,
image, and video compression, which rely on storing only a small
t
subset of Fourier series coefficients, which typically have the largest
magnitudes, leaving out the frequency components with small magni-
Figure 50: The Dirac comb signal, an
tudes. infinitely long train of unit impulses
spaced apart by T. The Dirac comb
is a periodic function with a funda-
Example: Let’s represent the so-called Dirac comb using a mental period T. The Dirac comb is
Fourier series. The Dirac comb is a signal that consists of a train of used to model sampling values of a
unit impulse functions spaced T apart from one another. It is defined continuous-time signal spaced evenly
apart to provide an idealized model for
as: discretizing a continuous-time signal.

x (t) = ∑ δ(t − kT ) . (156)
k =−∞

This signal is shown in Figure 50. We’ll encounter this signal when
introducing the model for discretizing a continuous-time signal.
The fundamental period for this signal is T. Therefore, one would
expect that we can form a Fourier series representation of this signal.
What are the values of the coefficients ck ? Let’s find out using the
T
analysis formula.
x7 ( t )
We’ll evaluate the analysis integral formula from t = − T/2 to
t = T/2, as this is convenient in the case of this signal. The analysis
formula evaluates to: 2π 7t 2π 7t
= T −1 ( e i T + e −i T )
1 T/2
Z
−i 2π
T kt + T −1 ( e i 2π
T 6t −i 2π
T 6t
ck = ⟨ x (t), ψk (t)⟩ = δ(t)e dt (157) +e )
T − T/2 2π −i 2π
+ T −1 (ei T 5t +e T 5t )
1 2π −i 2π
= . (158) + T −1 (ei T 4t +e T 4t )
T 2π −i 2π
+ T −1 (ei T 3t +e T 3t )
The signal can thus be represented as the following Fourier series: 2π 2t 2π 2t
+ T −1 ( e i T + e −i T )
2π −i 2π
1 N 2π kt
+ T −1 ( e i T t +e T t )
x N (t) =
T ∑ ei T . (159) + T −1
k =− N

Take a look at Figure 51. This is an approximation of the Dirac comb


Figure 51: A Fourier series representa-
x7 (t) using 15 frequency components. I hope it is not too difficult 2π
tion x7 (t) = T1 ∑7k=−7 ei T kt of a Dirac
for you to convince yourself that as we increase the value of N, the comb signal x (t) = ∑∞ k =−∞ δ ( t − kT )
spikes will become sharper and sharper. with a period T. Two periods of the
signal are shown. The Fourier series
You may also be able to convince yourself that this signal is real-
representation of the signal is made
valued, by investigating the pairing of positive and negative fre- using seven sinusoidal signals.
83

quency terms:

N
1 2π kt
x N (t) =
T ∑ ei T (160)
k =− N
N
1 1 2π 2π
=
T
+
T ∑ (ei T kt + e−i T kt ) (161)
k =1
N  
1 2 2π
=
T
+
T ∑ cos
T
kt . (162)
k =1

This is how the positive and negative frequency terms are organized
in Figure 51. For real-valued Fourier series representations, this sort
of grouping of positive and negative frequency coefficients always
occurs.

Example: Fourier series representation for a square wave.


A square wave is a classic example of a periodic function that can
be evaluated using a Fourier series. This type of signal is often used, x (t)
T
e.g., in the pulse width modulation scheme of adjusting mean power
P
flowing through a circuit. This type of on-off pulsing scheme is
also used in avalanche rescue beacons used to radiolocate victims
buried under snow. The rectangular function is shown in Figure 52.
A measurement of power emitted by an avalanche rescue beacon as a
function of time is shown in Figure 53.
t
Consider a general square wave signal with period T, pulse length
1 2 3 4
P and amplitude 1. Between 0 ≤ t < T, this signal is mathematically
described with: Figure 52: A classic example of a
commonly encountered periodic
( function, the square wave signal. This
1 when 0≤t<P type of signal is encountered, e.g., with
x (t) = . (163)
0 otherwise pulse width modulation in electrical
engineering.

By analytically evaluating the Fourier series analysis integral one has:

Z T
1
ck = x (t)e−i2πkt/T dt , (164)
T 0

we can obtain an equation for coefficients ck . In the case of k = 0, the


integral evaluates to:

1 T
Z
c0 = x (t)ei·0 dt (165)
T 0
1 P
Z
= dt (166)
T 0
P Figure 53: A measurement of power
= . (167) as a function of time transmitted by
T
an avalanche rescue locator beacon.
Approximately a 0.1 second pulse is
emitted every second.
84

For other values of k ̸= 0:

1 T
Z

ck = x (t)e−i T tk dt (168)
T 0
1 P −i 2π tk
Z
= e T dt (169)
T 0
1 −i 2π tk P
= − e T (170)
2iπk t =0
1  −i 2π kP 
=− e T −1 (171)
2iπk
1 −i π kP  i π kP 
e T − e−i T kP
π
= e T (172)
2iπk
1 −i π kP π 
= e T sin kP . (173)
πk T
We have used: the inverse Euler’s formula for the sin(·) signal, the
fact that e−i T kP ei T kP = 1, and that sin(−θ ) = − sin(θ ).
π π

Now we can evaluate the synthesis formula to form a Fourier


series approximation of this function:

N Figure 54: Fourier series approx-


i 2π −i 2π
x N ( t ) = c0 + ∑ ck e T kt + c−k e T kt (174) imation of a square wave eval-
uated with the Python script
k =1
009_square_wave/square_wave.py.
N

P sin πT kP  −i π kP i 2π kt 2π

= +∑ e T e T + ei T kP e−i T kt
π
(175)
T k =1 πk
N 2 sin π kP
  
P 2π π
= +∑ T
cos kt − kP . (176)
T k =1 πk T T

Let’s see what this function looks like. I’ve written a little Python pro-
gram to visualize the Fourier series approximation of this function.
The Python code is found in Listing 19. The output of this program is
shown in Figure 54.
import m a t p l o t l i b . pyplot as p l t
import numpy as np

P = 0.1
T = 1.0

sample_rate = 1000.0
t = np . arange ( i n t ( s a m p l e _ r a t e * T ) ) /s a m p l e _ r a t e

zn = np . z e r o s ( l e n ( t ) , dtype=np . complex64 )

N = 101

ks = np . arange ( −N, N+1)


cks = np . z e r o s ( 2 *N+1 , dtype=np . complex64 )
ki = 0

f o r k i n range ( −N, N+1) :


i f k == 0 :
zn += P/T
85

cks [ k i ] = P/T
else :
k2 = f l o a t ( k )
ck = ( 1 . 0 / ( np . p i * k2 ) ) * ( np . exp ( −1 j * ( np . p i/T ) * k2 * P ) ) * np .
s i n ( np . p i * k2 * P/T )
cks [ k i ] = ck
zn += ck * np . exp ( 1 j * ( 2 . 0 * np . p i/T ) * k2 * t )
k i += 1

# Plot the absolute values of the Fourier s e r i e s c o e f f i c i e n t s .


p l t . p l o t ( 2 . 0 * np . p i * ks/T , np . abs ( cks ) )
p l t . show ( )

# C r e a t e a square wave s i g n a l .
square_wave = np . z e r o s ( l e n ( t ) , dtype=np . f l o a t 6 4 )
square_wave [ ( 0 < t ) & ( t < P ) ] = 1 . 0

plt . figure ( figsize =(0.8*6 , 0.8*4) )


p l t . p l o t ( t , square_wave , l a b e l = " Square wave " , c o l o r = " b l a c k " )
p l t . p l o t ( t , zn . r e a l , l a b e l = " F o u r i e r s e r i e s approximation ( Real ) "
, c o l o r = " blue " )
p l t . p l o t ( t , zn . imag , l a b e l = " F o u r i e r s e r i e s approximation ( Imag ) "
, c o l o r = " red " )
p l t . legend ( )
p l t . x l a b e l ( " Time ( t ) " )
plt . ylabel ( " s ( t ) " )
plt . tight_layout ()
p l t . s a v e f i g ( " square_wave . png " )
p l t . show ( )

Listing 19: 009_square_wave/square_wave.py

d
The time derivative operator dt has a very simple form for
a Fourier series representation of a signal.
We know that a Fourier series is a sum of complex sinusoidal
signals. We also know that it is easy to differentiate this signal:
d iωt
ce = iωceiωt , (177)
dt
where c ∈ C is a complex constant.
It is also relatively easy to see that the nth time derivative is:
dn iωt
ce = (iω )n ceiωt . (178)
dtn
Thus, if x (t) has a Fourier series representation
∞ 2π kt
x (t) = ∑ c k ei T , (179)
k =−∞
then the nth time derivative has the following Fourier series represen-
tation:
∞ n
dn

n 2πk 2π
x (t) = ∑ i ck ei T kt (180)
dtn k =−∞
T
∞ 2π kt

(n)
= c k ei T . (181)
k =−∞
86

It is still a Fourier series, but with each Fourier coefficient modified in


the following way:

2πk n
 
(n)
ck := i n ck . (182)
T

Differentiation can be seen as a filtering operation on the signal.


A filter is something that modifies the amplitude and phase of the
Fourier series coefficients in some way. For example, a low-pass
filtering operation would reduce the amplitudes of high frequency
coefficients relative to the amplitudes of the low frequency coeffi-
cients. After a low-pass filtering operation, the ratio of low frequency
coefficient amplitudes to the high frequency coefficient amplitudes
would be increased. A high pass filter on the other hand would do
the opposite.
One thing to ask yourself is how does differentiation adjust the
amplitude of each Fourier coefficient? When taking the nth time
derivative, what happens to the complex amplitudes at low angular
frequencies (|2πk/T | < 1)? What about high angular frequencies
(|2πk/T | > 1)? Hint, inspect how the coefficients defined in Equation
182 are scaled.
The answer is that the magnitude of the low frequency Fourier
coefficients are attenuated, and high frequency ones are amplified.
The DC component is completely removed as 2πk/T = 0 for k =
0. Differentiation can therefore be viewed as a high-pass filtering
operation.

The time shift operation x ( t − τ ) applied to a Fourier series


representation is also pretty handy. Let’s recall how a time shift
affects a complex sinusoidal signal:

x (t) = ceiωt . (183)

If we apply a delay by τ to this signal y(t) = x (t − τ ), we obtain:

y(t) = ceiω (t−τ ) = e−iωτ ceiωt = e−iωτ x (t) . (184)

This means that a time delay by τ corresponds to a phase shift by


−ωτ for a complex sinusoidal signal. We can apply this to a Fourier
series, which is a sum of complex sinusoidal signals.
If the Fourier series representation of a signal is:

∞ 2π kt
x (t) = ∑ c k ei T , (185)
k =−∞
87

then the Fourier series representation of a time shifted signal is:


∞  2πτ k
 2π kt
x (t − τ ) = ∑ c k e −i T ei T (186)
k =−∞
∞ 2π kt
= ∑ c′k ei T (187)
k =−∞

where c′k is rotated on the complex plane in the clockwise direction


by an angle of 2πkτ/T with respect to ck .
This means that if you know the Fourier series representation of
a signal, you can easily modify the complex amplitude ck of each
Fourier coefficient to obtain the coefficients of the time shifted signal.

What if a signal is not periodic, can we still form a spec-


tral representation for a signal? Yes, we can. This investigation
leads to the continuous-time Fourier transform.
Let’s derive the continuous-time Fourier transform from the
Fourier series. Pretty much all we’ll need to do is to study what
happens when the fundamental period of the signal approaches
infinity: T → ∞. We’ll arrive with the result that introduces the
Fourier transform, and the inverse Fourier transform, introducing, for
the first time, this general spectral representation for continuous-time
signals.
We start the derivation of the continuous-time Fourier transform
with the definition of a Fourier series for a function with period T.
We’ll denote by ∆ω the spacing between frequency components. The
term ∆ω also denotes the fundamental angular frequency of the
periodic signal:

x T (t) = ∑ ck eik∆ωt . (188)
k =−∞

We can now add the definition of ck corresponding to the kth Fourier


series coefficient using the synthesis equation:
∞  Z T/2 
1
x T (t) = ∑ x T (t)e −ik∆ωt
dt eik∆ωt . (189)
k =−∞
T − T/2

We then note that 1/T = ∆ω/2π. This is the relationship between


the fundamental period of the signal and the fundamental angular
frequency that we discussed in the beginning of this chapter.
We now obtain:
1 ∞
Z T/2 
x T (t) = ∑ −T/2 xT (t)e
2π k=−
−ik∆ωt
dt eik∆ωt ∆ω (190)


1
=
2π ∑ x̂ T (k∆ω )eik∆ωt ∆ω . (191)
k=−∞
88

In this form, the equation is a Riemann sum approximation of an


integral of a continuous function x̂ T (ω )e−iωt .

Figure 55: A depiction of how the


Fourier series can be seen as a Riemann
c0 sum approximation of an integral of a
continuous function.

c −1 c1
c −2 c2
c −3 c3
c −4 c4

··· ∆ω ···
ω
−4∆ω−3∆ω−2∆ω −∆ω ∆ω 2∆ω 3∆ω 4∆ω

We now inspect the case where T → ∞. When the period of the


function x T (t) approaches infinity, we drop the subscript T in the
notation and denote it with x (t). The Riemann sum then approaches
the following integral equation (∆ω → dω, k∆ω → ω):

1
lim x T (t) = lim
T →∞ T →∞ 2π
∑ x̂ T (k∆ω )eik∆ωt ∆ω (192)
k =−∞
Z ∞ Z ∞ 
1
= x (t)e−iωt dt eiωt dω (193)
2π −∞ −∞
Z ∞
1
= x̂ (ω )eiωt dω (194)
2π −∞
= x (t) . (195)

This integral is known as the inverse Fourier transform. It allows


us to go from a continuous function x̂ (ω ), which is the continuous
spectral representation of the signal, to the time domain signal x (t).
The innermost integral that allows us to obtain x̂ (ω ) from the
continuous-time signal x (t) is:
Z ∞
x̂ (ω ) = x (t)e−iωt dt . (196)
−∞

This is the forward Fourier transform.

The forward Fourier transform is defined as:


Z ∞
x̂ (ω ) = x (t)e−iωt dt . (197)
−∞

The inverse Fourier transform is defined as:


89

Z ∞
1
x (t) = x̂ (ω )eiωt dω . (198)
2π −∞

Just as the Fourier series, the continuous-time Fourier transform is in


most practical cases invertible.
We’ll use the following notation to indicate Fourier transform
pairs:
F
x (t) ←
→ x̂ (ω ) . (199)
We’ll return to the Fourier transform later and show that the Fourier
series is a special case of the Fourier transform!

The spectrum of an audio signal

The Python program in Listing 20 shows how to use the Fast Fourier
Transform (FFT) command in Python to analyze an audio signal for
its spectral contents. The output of this program is shown in Figure
56.
The FFT algorithm is used to evaluate a discrete-time Fourier
transform. This operation is mathematically defined as follows:
N −1 2π
c[k] = ∑ x [t]e−i N kt . (200)
t =0

Here c[k] is the complex amplitude corresponding to a spectral


component with angular frequency ωk = 2πk/( Ts N ), where Ts =
1/ f s is the sample-spacing, which is the inverse of the sample-rate of
the signal.
You can think of an FFT as a discrete-time equivalent of the
Fourier series analysis equation, which finds the Fourier series coef-
ficients for a periodic discrete-time signal. We will cover the discrete
Fourier transform and discrete-time signals in more detail later on, I
just wanted to already expose you to the FFT algorithm at this point.
import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . i o . w a v f i l e as s i o

wav = s i o . read ( " g u i t a r _ c l e a n . wav" )


# F i g u r e out what t h e sample − r a t e i s .
s a m p l e _ r a t e = wav [ 0 ]
# Read t h e s i g n a l f o r only one s t e r e o channel ,
# s e l e c t samples i n s i d e t h e f i l e .
x = wav [ 1 ] [ 5 0 0 0 0 : 6 0 0 0 0 , 0 ]

# How many samples t o p l o t and a n a l y s e .


N_samples = 1024

# C a l c u l a t e s p e c t r a l components using FFT


# ( d i s c r e t e −time F o u r i e r s e r i e s . )
90

c_k = np . f f t . f f t ( x [ : N_samples ] )

# Time i n seconds .
time_vec = np . arange ( N_samples ) / f l o a t ( s a m p l e _ r a t e )

# C r e a t e a v e c t o r with f r e q u e n c i e s i n kHz .
f r e q _ v e c = np . f f t . f f t s h i f t ( np . f f t . f f t f r e q ( N_samples , d=1.0/
s a m p l e _ r a t e ) ) /1 e3
# What i s magnitude squared o f each s p e c t r a l component i n dB .
power_dB = np . f f t . f f t s h i f t ( 1 0 . 0 * np . l o g 1 0 ( np . abs ( c_k ) * * 2 . 0 ) )

# P l o t time domain s i g n a l .
plt . figure ( f i g s i z e =(8 , 4) )
p l t . subplot (121)
p l t . p l o t ( 1 e3 * time_vec , x [ : 1 0 2 4 ] )
p l t . x l a b e l ( " Time (ms) " )
p l t . ylabel ( " Relative a i r pressure ( u n i t l e s s ) " )
p l t . t i t l e ( " Time domain s i g n a l " )

# P l o t frequency domain s i g n a l .
p l t . subplot (122)
p l t . p l o t ( f r e q _ v e c , power_dB )
p l t . t i t l e ( " S p e c t r a l components " )
p l t . x l a b e l ( " Frequency ( kHz ) " )
p l t . y l a b e l ( " Power ( dB ) " )
plt . tight_layout ()
p l t . s a v e f i g ( " audio_spec . png " )
p l t . show ( )

Listing 20: 013_audio_spec/audio_spec.py

Figure 56: The spectral components of


an audio signal (guitar string plucked).
Left: time domain signal x (t), Right:
The magnitude of the spectral compo-
nents in decibel scale: 10 log10 (|ck |2 ).
91

Exercises: Fourier Series

1. A signal is defined as:

x (t) = 7 sin(3πt + 0.2π ) + 3 cos(7πt + 0.5π ) (201)

The independent variable t is in units of seconds.

a) Express this signal in the form of Equation 113. What are


the values of the coefficients ck ∈ C and angular frequencies
ωk ∈ R?
b) The coefficients ck , satisfy c−k = c∗k , why?
c) Show that this signal is periodic by showing that the frequen-
cies of the individual frequency components are commensu-
rable.
d) Use Euclid’s algorithm to find the fundamental angular fre-
quency ω. What is the fundamental frequency in units of hertz
and rad/s?
e) What is the fundamental period T of this signal in seconds?
f) We delay the signal x (t) by 0.5 seconds y(t) = x (t − 12 ). Write
y(t) in the following format:

y(t) = 7 sin(3πt + ϕ0 ) + 3 cos(7πt + ϕ1 ) (202)

What are the values ϕ0 and ϕ1 ?



g) We create a new signal z(t) = x (t) + ei 2t+13 . Why is the signal
z(t) no longer periodic with a finite period? Is the signal z(t)
real-valued?

2. A signal is defined as:

x (t) = e−i(6πt+0.3) + 4ei(60πt+0.42) + 4e−i(60πt+0.42) + ei(6πt+0.3) (203)

The independent variable t is in units of seconds.

a) Why is this signal real-valued?


b) Is this signal periodic? If so, what is the fundamental angular
frequency ω and the fundamental period T?

3. The Fourier series coefficients for a pulsed signal x (t) with a


fundamental period T = 1 is described as:
(
1
10 when k=0
ck = 1 −i 10 k
π π
 (204)
πk e sin 10 k otherwise

See Equations 167 and 173 for the derivation of the Fourier coeffi-
cients.
92

a) Plot the partial sum x N (t) of the Fourier series using N = 101.
You can use Listing 19 to help you out.
b) What are the Fourier series coefficients for a signal delayed in
time by 0.2 seconds. Plot the partial sum to verify.
d
c) What are the Fourier series coefficients for y(t) = dt x ( t )? Make
a plot to verify.
93

Suggested solutions: Fourier Series

1. Let x (t) be the signal:

x (t) = 7 sin(3πt + 0.2π ) + 3 cos(7πt + 0.5π ).

a) Have that:

1 iθ
sin θ = (e − e−iθ ),
2i
1
cos θ = (eiθ + e−iθ ).
2
This gives:

7 i(3πt+0.2π ) 3
x (t) = (e − e−i(3πt+0.2π ) ) + (ei(7πt+0.5π ) + e−i(7πt+0.5π ) ),
2i
    2    
7 i0.2π i3πt 7 −i0.2π −i3πt 3 i0.5π i7πt 3 −i0.5π −i7πt
= e e − e e + e e + e e ,
2i 2i 2 2

giving:
 
3 −i0.5π 7 7 3
ck = e , − e−i0.2π , ei0.2π , ei0.5π ,
2 2i 2i 2

for which the corresponding angular frequencies are:

ωk = (−7π, −3π, 3π, 7π ).

b) For any real signal, the Fourier coefficients satisfy c−k = c∗k and
x (t) is real.
c) The signal is periodic if ωi /ω j ∈ Q for every pair i, j ∈
{−7, −3, 3, 7} with i ̸= j. In this case every ωi is an integer
multiple of π, so every ratio is a rational number as all the π
cancel.
d) Using Euclid’s algorithm we have:

(3π, 7π ),
(3π, 7π − 3π ),
(3π, 4π ),
(3π, 4π − 3π ),
(3π, π ),
(3π − π, π ),
(2π, π ),
(2π − π, π ),
(π, π ).
94

Hence, the fundamental angular frequency is ω = π rad/s. In


units of hertz, we have that ω = 2π f , so

ω π 1
f = = = .
2π 2π 2

Thus, the fundamental frequency is f = 1/2 Hz.

e) If the fundamental angular frequency is ω = π, then the


fundamental period is related by T = 2π/ω, hence

2π 2π
T= = = 2 seconds.
ω π

f) Define a new signal y(t) = x (t − 12 ). That is, we delay the signal


x (t) by 12 . We get:
       
1 1
y(t) = 7 sin 3π t − + 0.2π + 3 cos 7π t − + 0.5π .
2 2

Simplifying gives:

y(t) = 7 sin(3πt − 1.3π ) + 3 cos(7πt − 3π ).

Hence, ϕ0 = −1.3π and ϕ1 = −3π.



g) Define another signal z(t) = x (t) + ei 2+13 . This new signal is
not periodic as the frequencies are not commensurable, since

we have a factor of 2, the signal is also not real-valued since
we don’t have a corresponding complex conjugate pair.

2. Let x (t) be a signal defined as:

x (t) = e−i(6πt+0.3) + 4ei(60πt+0.42) + 4e−i(60πt+0.42) + ei(6πt+0.3) ,

where t is measured in seconds.

a) This signal is real-valued since we have two pairs, each with


the same frequency, but different signs. Thus, the signal can be
written as a real signal for which it takes the following form:

x (t) = 8 cos(60πt + 0.42) + 2 cos(6πt + 0.3).

b) In this case, we have the angular frequencies of ω1 = 60π and


ω2 = 6π for which ω1 /ω2 = 60π/6π = 10. We get a rational
number, so the signal is periodic.

c) To find the fundamental angular frequency, we use Euclid’s


95

algorithm. This gives:

(60π, 6π ),
(54π, 6π ),
(48π, 6π ),
(42π, 6π ),
(36π, 6π ),
(30π, 6π ),
(24π, 6π ),
(18π, 6π ),
(12π, 6π ),
(6π, 6π ),

therefore, the fundamental angular frequency is 6π in units of


radians per second.
d) The fundamental period is:

2π 2π 1
T= = = ,
ω 6π 3
1
hence T = 3 in units of seconds.

3. Let the Fourier series coefficients of a periodic signal x (t) with


fundamental period T = 1 be:

1, k = 0,
ck = 10
 1 e−i 10
πk π

sin k , otherwise.
πk 10

a) Using Python, we can implement a simple program for the


partial sum with N = 101. Listing 21 shows a way.
import m a t p l o t l i b . pyplot as p l t
import numpy as np

T = 1 # Period .
sample_rate = 1000.0 # Sample r a t e .
N = 101 # Number o f terms .

# P a r t i t i o n the t −a x is .
t = np . arange ( i n t ( s a m p l e _ r a t e * T ) ) /s a m p l e _ r a t e

# A l l o c a t e an a r r a y f o r t h e s i g n a l .
xn = np . z e r o s ( l e n ( t ) , dtype=np . complex64 )

def ck ( k : i n t ) −> f l o a t :
" " " Function f o r t h e F o u r i e r c o e f f i c i e n t s . " " "
i f k == 0 :
r e t u r n 1/10
else :
96

r e t u r n 1/(np . p i * k ) * np . exp ( −1 j * np . p i * k /10) * np . s i n ( np


. p i * k /10)

# Loop through t h e i n d i c e s and add t h e s i g n a l terms .


f o r i i n range ( −N, N+1) :
xn += ck ( i ) * np . exp ( 1 j * 2 * np . p i * i * t /T )

p l t . p l o t ( t , xn . r e a l , c o l o r = " blue " )


p l t . p l o t ( t , xn . imag , c o l o r = " red " )
p l t . x l a b e l ( " Time ( t ) " )
p l t . y l a b e l ( " $x_ {N} ( t ) $ " )
# I f needed .
# plt . plot ( )

Listing 21: Suggested solution to a)

b) The Fourier coefficients of a delayed signal are related to the


2πkτ
undelayed signal by c′k = e−i T ck . The script in Listing 21 can
be modified to account for delay. The delayed version is shown
in Listing 22.
import m a t p l o t l i b . pyplot as p l t
import numpy as np

T = 1 # Period .
sample_rate = 1000.0 # Sample r a t e .
N = 101 # Number o f terms .
tau = 0 . 2 # Delay .

# P a r t i t i o n the t −a x is .
t = np . arange ( i n t ( s a m p l e _ r a t e * T ) ) /s a m p l e _ r a t e

# A l l o c a t e an a r r a y f o r t h e s i g n a l .
xn = np . z e r o s ( l e n ( t ) , dtype=np . complex64 )

def ck ( k : i n t ) −> f l o a t :
" " " Function f o r t h e F o u r i e r c o e f f i c i e n t s . " " "
i f k == 0 :
r e t u r n 1/10
else :
r e t u r n 1/(np . p i * k ) * np . exp ( −1 j * np . p i * k /10) * np . s i n ( np
. p i * k /10)

# Compute t h e delayed s i g n a l .
f o r i i n range ( −N, N+1) :
xn += ck ( i ) * np . exp ( 1 j * 2 * np . p i * i * ( t −tau ) /T )

p l t . p l o t ( t , xn . r e a l , c o l o r = " blue " )


p l t . p l o t ( t , xn . imag , c o l o r = " red " )
p l t . x l a b e l ( " Time ( t ) " )
p l t . y l a b e l ( " $x_ {N} ( t ) $ " )
# I f needed .
# plt . plot ( )

Listing 22: Suggested solution to b)


97

d
c) Define y(t) = dt x (t). If ck are the Fourier coefficients for x (t),
then the Fourier coefficients for y(t) is:

2πk
dk (t) = i c .
T k
Using Python we have Listing 23.
import m a t p l o t l i b . pyplot as p l t
import numpy as np

T = 1 # Period .
sample_rate = 1000.0 # Sample r a t e .
N = 101 # Number o f terms .
tau = 0 . 0 # Delay .

# P a r t i t i o n the t −a x is .
t = np . arange ( i n t ( s a m p l e _ r a t e * T ) ) /s a m p l e _ r a t e

# A l l o c a t e an a r r a y f o r t h e s i g n a l .
xn = np . z e r o s ( l e n ( t ) , dtype=np . complex64 )

def ck ( k : i n t ) −> f l o a t :
" " " Function f o r t h e F o u r i e r c o e f f i c i e n t s . " " "
i f k == 0 :
r e t u r n 1/10
else :
r e t u r n 1/(np . p i * k ) * np . exp ( −1 j * np . p i * k /10) * np . s i n ( np
. p i * k /10)

# Derivative signal .
f o r i i n range ( −N, N+1) :
xn += ( 1 j * 2 * np . p i * i /T ) * ck ( i ) * np . exp ( 1 j * 2 * np . p i * i * ( t −tau
) /T )

# Plot the r e s u l t .
p l t . p l o t ( t , xn . r e a l , c o l o r = " blue " )
p l t . p l o t ( t , xn . imag , c o l o r = " red " )
p l t . x l a b e l ( " Time ( t ) " )
p l t . y l a b e l ( " $y_ {N} ( t ) $ " )
# I f needed .
# plt . plot ( )

Listing 23: Suggested solution to c)


98

Audio Compression Example [Optional]

In this application example, I’ll show you how to estimate the spec-
tral components of an audio signal and how this can be used for
audio compression. I won’t go very deep into either of these topics.
The main idea is to expose you to these concepts.

Example: audio compression algorithms often use a sparse


spectral representation of an audio signal. The sounds
produced by musical instruments when playing a single note are
often good examples of periodic signals that occur in our daily life.
Of course, real signals are typically not exactly periodic, but for short
intervals this approximation is good.
A good example of this is the signal shown in Figure 56, which
depicts the sound signal (instantaneous relative air pressure at the
microphone) emitted by a guitar when one string is plucked.
A Fourier series approximation of an audio signal is often used in
audio compression, as it is often possible to approximate an audio
signal relatively accurately with relatively few non-zero Fourier series
coefficients. This is the basic idea behind the MP3 audio compression
algorithm18 . 18
Yes, there is more to it. Note, that real
The Python code in Listing 24 demonstrates audio compression world compression algorithms use a
lot more tricks, such as psychoacous-
in practice. Understanding the program may require you to be tics – not storing spectral components
somewhat familiar with analyzing what computer programs do. that humans cannot hear very well in
music. These are outside the scope of
If you are just starting with programming, feel free to skip going this basic course on signal processing
through this program example. though. However, it is already possible
The audio compression example program relies on the discrete to achieve significant compression just
using the simple idea of representing
Fourier transform (FFT), which we haven’t covered yet. Just think the signal only using the N largest am-
of the numpy.fft.fft operation as the analysis step of the Fourier plitude spectral components. I would
wager that most of the compression
series, which gives you the phase and amplitude of each frequency is achieved with the sparse spectral
component ck . We’ll cover the discrete Fourier transform in more representation.
detail at a later part of this course.
import numpy as np
import s c i p y . i o . w a v f i l e as sw

# Remove low power s p e c t r a l components .


def compress_snippet ( c l i p , c o m p r e s s i o n _ r a t i o = 0 . 9 9 ) :
# Length o f sound c l i p .
L = len ( c l i p )
# Half l e n g t h ( r e a l −valued s i g n a l s have c o n j u g a t e symmetric
spectra ) .
L2 = i n t ( L/2)

# Compress audio .

# DFT using t h e FFT algorithm , only use


# h a l f o f spectrum because i t i s symmetric .
99

c l i p _ f f t = np . f f t . f f t ( c l i p ) [ : L2 ]
# Remove t h e DC component .
c l i p _ f f t [0] = 0.0

# Store original ( for plotting ) .


o r i g _ f f t = np . copy ( c l i p _ f f t )

# S o r t s p e c t r a l components by magnitude ,
# s m a l l e s t s p e c t r a l components f i r s t .
idx = np . a r g s o r t ( np . abs ( c l i p _ f f t ) )

# To s t o r e t h e compressed f i l e , one s a v e s t h e s p e c t r a l
# i n d e x e s o f t h e s t r o n g e s t frequency components
# and t h e s p a r s e frequency domain
# representation of the s i g n a l .
i d x s = np . a r r a y ( idx [ i n t ( L2 * c o m p r e s s i o n _ r a t i o ) : L2 ] , dtype= "
uint16 " )

# Convert t o 8− b i t i n t e g e r format t o save space .


s p e c _ r e = np . r e a l ( c l i p _ f f t [ i d x s ] )
spec_im = np . imag ( c l i p _ f f t [ i d x s ] )
max_amp = np . max ( [ np . max ( s p e c _ r e ) , np . max ( spec_im ) ] )
s c a l e = np . a r r a y ( max_amp , dtype=np . f l o a t 3 2 )
s p e c _ r e = np . a r r a y ( 1 2 8 . 0 * s p e c _ r e /max_amp , dtype=np . i n t 8 )
spec_im = np . a r r a y ( 1 2 8 . 0 * spec_im/max_amp , dtype=np . i n t 8 )

# S t o r e compressed audio as b i n a r y f i l e s on d i s k .
i d x s . t o f i l e ( " compressed_idx . bin " )
s c a l e . t o f i l e ( " compressed_scale . bin " )
s p e c _ r e . t o f i l e ( " compressed_spec_re . bin " )
spec_im . t o f i l e ( " compressed_spec_im . bin " )

# Decompress audio .

# Load compressed audio s i g n a l from b i n a r y f i l e s .


i d x s = np . f r o m f i l e ( " compressed_idx . bin " , dtype= " u i n t 1 6 " )

# Use 8− b i t as they t a k e up l e s s space .


s p e c _ r e = np . f r o m f i l e ( " compressed_spec_re . bin " , dtype=np .
int8 )
spec_im = np . f r o m f i l e ( " compressed_spec_im . bin " , dtype=np .
int8 )
# Store scale .
s c a l e = np . f r o m f i l e ( " compressed_scale . bin " , dtype=np . f l o a t 3 2
)

spec = np . a r r a y ( s p e c _ r e + spec_im * 1 j , dtype=np . complex64 ) *


s c a l e /128.0
comp_fft = np . z e r o s ( L , dtype=np . complex64 )

# I n s e r t s p a r s e s p e c t r a l components i n t o spectrum .
comp_fft [ i d x s ] = spec

# I n s e r t c o n j u g a t e symmetric s p a r s e
# s p e c t r a l components i n t o spectrum .
comp_fft [ L− i d x s ] = np . c o n j ( spec )

# I n v e r s e DFT t o g e t t h e time domain s i g n a l , based on


# the sparse s p e c t r a l representation
# i . e . , t h e r e c o n s t r u c t e d audio s i g n a l .
# Discard t h e imaginary component .
100

comp_clip = np . f f t . i f f t ( comp_fft ) . r e a l

# C a l c u l a t e a c t u a l compression r a t i o based on s t o r a g e s i z e .
o r i g i n a l _ s i z e = 1 6 . 0 * L # Uncompressed s i z e ( 1 6 b i t s per
sample ) .
# Sparse 8− b i t s p e c t r a , 16− b i t s p e c t r a l i n d i c e s , 32− b i t
scale factor .
compressed_size = 8 . 0 * 2 . 0 * l e n ( i d x s ) + 1 6 . 0 * l e n ( i d x s ) + 3 2 . 0
r e a l _ c o m p _ r a t i o = compressed_size/ o r i g i n a l _ s i z e

r e t u r n comp_clip , comp_fft [ : L2 ] , o r i g _ f f t , r e a l _ c o m p _ r a t i o

# Read wav f i l e .
# This i s t h e audio s i g n a l t o be compressed .
t s = sw . read ( " o r i g i n a l . wav" )
sr , c l i p = t s # Get t h e sample r a t e and t h e data from t h e f i l e .
i f l e n ( c l i p . shape ) == 2 : # I f s t e r e o , only use one channel .
p r i n t ( " Using only one s t e r e o channel . " )
c l i p = c l i p [ : , 0 ] # E x t r a c t only one channel .

# Compress and decompress audio f i l e .


# Compression_ratio = 0 . 9 5 means 95% r e d u c t i o n i n
# f i l e size .
cr = 0.9

# Compress f u l l l e n g t h o f t h e c l i p .
W = 100000
n_window = i n t ( np . f l o o r ( l e n ( c l i p ) /W) )
comp_clip = np . z e r o s ( l e n ( c l i p ) )
idx = np . arange (W)

f o r i i n range ( n_window ) :
s n i p p e t = c l i p [ i *W + idx ]
c , c _ f f t , o _ f f t , r e a l _ c o m p _ r a t i o = compress_snippet ( sn i pp et ,
compression_ratio=cr )
p r i n t ( f " Achieved compression { ( 1 0 0 . 0 * ( 1 . 0 − r e a l _ c o m p _ r a t i o )
) : 1 . 2 f } %" )
comp_clip [ i *W + idx ] = c

# Save r e s u l t as wav f i l e so t h a t we can e a s i l y l i s t e n t o t h e


# audio q u a l i t y a f t e r decompression o f t h e compressed s i g n a l .

# S c a l e numbers t o 0 . . 1 s c a l e .
comp_clip = comp_clip / ( 1 . 5 * np . max ( comp_clip ) )
sw . w r i t e ( " compressed_signal . wav" , 4 4 1 0 0 , comp_clip )

Listing 24: 010_audio_compression/audio_compression.py

The algorithm discards weak spectral components of the signal


and stores the strong ones. This can result in significant savings in
storage space. In this example, the amount of storage required is only
5% of the original audio signal!
101

Image Compression Example [Optional]

The Fourier series can be generalized to two-dimensional


signals I : R 2 → C. The two-dimensional Fourier series is often
used in image processing and image compression.
The Fourier series representation for a two-dimensional periodic
function with period Tx in the x direction and Ty in the y direction is:
2π i 2π
Ty my
I ( x, y) = ∑ cn,m ei Tx nx e . (205)
n,m∈Z

The analysis procedure to obtain the coefficients cn,m ∈ C is:


Z Tx Z Ty
1 2π −i 2π
Ty my
cn,m = I ( x, y)e−i Tx nx e dxdy . (206)
Tx Ty 0 0

Many of the familiar results for 1d signals, such as the time shifting
property, or differentiation, can be extended to 2d or higher dimen-
sional periodic functions.

In this programming example I’ll briefly demonstrate to


you the concept of spectral image compression. It is possible
to extend the idea of compression of a one dimensional signal, as
demonstrated in the audio compression example, by only storing
strong frequency components in 2D images. This is essentially the
idea behind the JPEG image compression algorithm. The Python
code in Listing 25 demonstrates image compression and produces
the image shown in Figure 57. The program uses the 2D discrete-
Fourier transform function, numpy.fft.fft2, to calculate the phases
and amplitudes of the spectral components, cn,m , of the image. It
then uses the 2D inverse discrete Fourier transform to FFT synthesize
an image from only 5% of the spectral components. The output of
the image compression example is shown in Figure 57. It shows the
original and compressed image that is formed using a sparse Fourier
series representation of the image. The magnitudes of the spectral
components for the compressed and original image are also shown.
The same words of warning that I gave with the audio compres-
sion example apply to this program example. If you are only begin-
ning with Python, you may have a hard time with understanding this
program. For now, you can think of the 2D FFT and inverse FFT as
an analysis and synthesis step for a 2D periodic function. But don’t
let a warning or lack of experience prevent you from exploring!
import imageio . v3 as s i
import m a t p l o t l i b . pyplot as p l t
import numpy as np
102

Figure 57: Image compression example.


Top left: original image, Top right, the
magnitude squared of the spectral
components in dB scale. Bottom left:
compressed image formed using
only 5% of the strongest spectral
components. Bottom right: the spectral
magnitude squared of the spectral
components used to form the image on
the bottom left.

# 2d f f t image , remove c o m p r e s s i o n _ r a t i o * ( width * h e i g h t )


# s p e c t r a l components .
#
def compress_image ( image , c o m p r e s s i o n _ r a t i o = 0 . 9 5 ) :
# 2D DFT .
i m a g e _ f f t = np . f f t . f f t 2 ( image )
L = image . shape [ 0 ] * image . shape [ 1 ]

# Convert t o 1d array ,
# s o r t s p e c t r a l components by magnitude ,
# s m a l l e s t s p e c t r a l components f i r s t .
image_fft = image_fft . f l a t t e n ( )
idx = np . a r g s o r t ( np . abs ( i m a g e _ f f t ) . f l a t t e n ( ) )

# Remove weakest s p e c t r a l components , t h e s e don ’ t


# need t o be s t o r e d , so t h i s reduces data s t o r a g e
requirement .
# Ensure t h a t a t l e a s t two s p e c t r a l components a r e l e f t .
max_idx = np . min ( [ L−1 , i n t ( L * c o m p r e s s i o n _ r a t i o ) ] )
i m a g e _ f f t [ idx [ 0 : max_idx ] ] = 0 . 0
i m a g e _ f f t . shape = image . shape

# I n v e r s e 2D DFT t o g e t compressed image .


# Take r e a l component t o ensure s i g n a l i s r e a l .
comp_image = np . f f t . i f f t 2 ( i m a g e _ f f t ) . r e a l

r e t u r n ( ( comp_image , i m a g e _ f f t ) )
103

# Read image . Assume t h e image i s p e r f e c t .


image = s i . imread ( " husky . j p g " , mode= " F " )

# How much do we compress t h e image . 0 i s no compression ,


# 1 . 0 i s n e a r l y 100% compression
# (we s t i l l have a t l e a s t one s p e c t r a l component . )
compression_ratio = 0.95

plt . figure ( f i g s i z e =(12 , 12) )


# Show o r i g i n a l image .
p l t . subplot (221)
p l t . imshow ( image , cmap= " gray " , vmin =0 , vmax=255)

p l t . t i t l e ( " O r i g i n a l Image " )

# Show 2D f o u r i e r t r a n s f o r m o f image .
i m a g e _ f f t = np . f f t . f f t s h i f t ( np . f f t . f f t 2 ( image ) )
x _ f r e q = np . l i n s p a c e ( −np . pi , np . pi , num= i m a g e _ f f t . shape [ 0 ] )
y _ f r e q = np . l i n s p a c e ( −np . pi , np . pi , num= i m a g e _ f f t . shape [ 1 ] )
p l t . subplot (222)
image_fft_dB = 1 0 . 0 * np . l o g 1 0 ( np . abs ( i m a g e _ f f t ) * * 2 . 0 )
dB_min = np . min ( image_fft_dB )
dB_max = np . max ( image_fft_dB )
p l t . pcolormesh ( y_freq , x _ f r e q , image_fft_dB )
p l t . x l a b e l ( r " $\omega_0$ " )
p l t . y l a b e l ( r " $\omega_1$ " )
p l t . xlim ( [ − np . pi , np . p i ] )
p l t . ylim ( [ − np . pi , np . p i ] )

plt . colorbar ( )
p l t . t i t l e ( r " $10 \l o g _ { 1 0 } | F (\ hat {\omega } _0 , \ hat {\omega } _1 ) |^2$ " )

# Compress image using 2D FFT .


p l t . subplot (224)
comp_imag , comp_fft = compress_image ( image , c o m p r e s s i o n _ r a t i o =
compression_ratio )
p l t . pcolormesh ( y_freq , x _ f r e q , 1 0 . 0 * np . l o g 1 0 ( np . abs ( np . f f t .
f f t s h i f t ( comp_fft ) + 1e −9) * * 2 . 0 ) , vmin=dB_min , vmax=dB_max )
p l t . x l a b e l ( r " $\omega_0$ " )
p l t . y l a b e l ( r " $\omega_1$ " )
p l t . xlim ( [ − np . pi , np . p i ] )
p l t . ylim ( [ − np . pi , np . p i ] )
plt . colorbar ( )
p l t . t i t l e ( " 2D FFT with s m a l l e s t s p e c t r a l components removed " )

p l t . subplot (223)
p l t . imshow ( comp_imag , cmap= " gray " , vmin =0 , vmax=255)
p l t . t i t l e ( " %1.1 f p e r c e n t compressed image " % ( 1 0 0 . 0 *
compression_ratio ) )
plt . tight_layout ()
p l t . s a v e f i g ( " image_compression . png " )
p l t . show ( )

Listing 25: 011_image_compression/image_compression.py


104

Variable star lightcurve fitting [Optional]

Example: The study of astronomical light curves of vari-


able stars is an example of a practical application of the
Fourier series.
Variable stars, called Cepheids, have intensities I (t) that vary
periodically as a function of time. An example of a light curve is
shown in Figure 58. Such variable stars pulsate in luminosity in a
regularly repeating manner. The repeating luminosity waveform is
often very non-sinusoidal, necessitating a Fourier series analysis.
Measurements of the intensity of a Cepheid variable star are made
at periods of time when telescope time is available and astronomical Figure 58: Measurements of the in-
tensity of a variable star (red) and
seeing allows observations to be made. The time spacing between a Fourier series model of the inten-
measurements is therefore by no means regular. sity of the star as a function of time
(blue). Illustration Credit: NASA, ESA
When analyzing the periodic waveform of this star, a search is and Z. Levay (STScI). Science Credit:
made to determine the period T of the intensity waveform, and the NASA, ESA, the Hubble Heritage
Fourier series coefficients ck . This is done by using an exhaustive Team (STScI/AURA) and the American
Association of Variable Star Observers.
search for plausible values of T. The model function is a Fourier
series synthesis equation:

N 2π kt
IN (t) = ∑ c k ei T . (207)
k =− N

The relationship between absolute brightness and the pulsation


frequency (fundamental frequency) is thought to be relatively stable.
This makes these types of variable stars useful for measuring the
astronomical distances.
The Fourier series can be used to express periodic functions. One
real life use case is estimating the functional form for a periodic
star, which varies in brightness periodically. In this exercise, we will
determine the Fourier series coefficients for brightness observations
of a real Cepheid star. We will then plot the Fourier series for the
periodic light curve of this star.
To estimate the periodic waveform for a Cepheid variable star
brightness measurements m(t), we will use something called the
maximum likelihood method, which estimates the Fourier series
coefficients. This involves expressing the Fourier series as a matrix
105

vector operation:

m = Ax (208)
   
m ( t0 )  i 2π k t 2π k t 2π k t c0
ei ei

 m(t )  e T 00 T 1 0 ... T N 0
  c1 
 
1   i 2π i 2π i 2π
  e T k 0 t1 e T k 1 t1 ... e T k N t1

 m ( t2 )  =    c2  .
  
.. .. .. .. (209)
 ..   .   .. 
    
. . .
 .  2π 2π k t 2π k t  . 
ei T k0 t M ei T 1 M ... ei T N M
m(t M ) cN

Here ck is a Fourier series coefficient and T is the fundamental period.


Because there are discrete measurements, the measurement function
is only known at discrete points m(tℓ ). In order to estimate the
Fourier series coefficients, we use the linear least-squares estimator:

x̂ = ( A H A)−1 A H m . (210)

The vector x̂ will now contain the most probable Fourier series
coefficients that explain the measurements m(tℓ ) of the periodic
function.
Because this course does not deal with statistics, we have imple-
mented the code that figures out the Fourier series coefficients that
fit the measurements. Your task is to edit the code below, and to
evaluate the Fourier series model, given the coefficients ck , which are
in the array variable named c_k. Code is shown in Listing 26, which
already does most of the work. Complete the last for-loop, and you
should get the following plot:

import m a t p l o t l i b . pyplot as p l t
import numpy as np
106

# Load data , download data from :


# h t t p :// k a i r a . u i t . no/ f y s 2 0 0 6 / l c b 1 . dat
d = np . l o a d t x t ( " l c b 1 . dat " )

# Measurement ti me s .
m_error = d [ : , 2 ]

# S e l e c t f i n d measurements t h a t don ’ t have l a r g e e r r o r s .


good_idx = np . where ( np . abs ( m_error ) < 1 . 0 ) [ 0 ]

# Measurement time ( u n i t s o f days ) .


m_t = d [ good_idx , 0 ]

# Magnitude measurements ( r e l a t i v e magnitude o f s t a r ) .


m_mag = d [ good_idx , 1 ]

# Fundamental period .
T = 13.124349

# Time , modulo period .


m_modulo_t = np . mod( m_t , T )

p l t . p l o t ( m_modulo_t , m_mag, " . " )

# Number o f F o u r i e r s e r i e s c o e f f i c i e n t s .
N = 10
N_meas = l e n (m_mag)

# Frequency i n d i c e s .
k_idx = np . arange ( −N, N+1)

# Theory matrix .
A = np . z e r o s ( [ N_meas , l e n ( k_idx ) ] , dtype=np . complex64 )

f o r ki , k i n enumerate ( k_idx ) :
# Setup t h e o r y matrix row .
A [ : , k i ] = np . exp ( 1 j * ( 2 . 0 * np . p i/T ) * k * m_modulo_t )

# Find maximum l i k e l i h o o d e s t i m a t e f o r F o u r i e r S e r i e s
c o e f f i c i e n t s c_k .
S = np . l i n a l g . inv ( np . dot ( np . t r a n s p o s e ( np . c o n j (A) ) , A) )
c_k = np . dot ( np . dot ( S , np . t r a n s p o s e ( np . c o n j (A) ) ) , m_mag)
p r i n t ( c_k . shape )
# E v a l u a t e t h e F o u r i e r S e r i e s f o r t h e maximum l i k e l i h o o d
estimate of
# c o e f f i c i e n t s c_k .
N_model = 100
model_t = np . l i n s p a c e ( 0 , T , num=N_model )
model = np . z e r o s ( N_model , dtype=np . complex64 )

# Sum t o g e t h e r s i g n a l f o r a l l F o u r i e r s e r i e s c o e f f i c i e n t s a_k .
f o r ki , k i n enumerate ( k_idx ) :
# F i g u r e out what t o put here .
# a r r a y " model_t " c o n t a i n s time
# a r r a y " c_k " c o n t a i n s t h e F o u r i e r s e r i e s c o e f f i c i e n t s .
# We want t h e a r r a y " model " t o c o n t a i n t h e F o u r i e r S e r i e s
# vector
model += # . . . F i g u r e out what t o put here .

p l t . p l o t ( model_t , model . r e a l , l a b e l = " Model " )


107

plt . p l o t ( m_modulo_t , m_mag, " x " , l a b e l = " Measurements " )


plt . t i t l e ( " F o u r i e r s e r i e s model f o r Cepheid v a r i a b l e s t a r " )
plt . x l a b e l ( " Time ( days ) " )
plt . y l a b e l ( " R e l a t i v e Magnitude " )
plt . legend ( )
plt . show ( )

Listing 26: 012_variable_star/variable_star.py


Programming Assignment 1

Perform the following tasks. Write a report describing your results,


which is at most two pages long. The report is free form, but it
must be delivered in PDF format. Include your code and plots in
the report. The report should be delivered by the deadline to the
following location using your candidate ID number: http://kaira.
uit.no/juha/upload/
x (t)
The Dirac comb is a periodic signal, which is defined as follows:
∞ T
x (t) = ∑ δ(t − kT ) (211)
k =−∞

The signal is shown in Figure 59.

a) Implement a program that calculates a partial sum approximation


N 2π kt

t
x N (t) = c k ei T (212)
k =− N
Figure 59: The Dirac comb signal with
of a Dirac comb with a fundamental period of T = 0.55 seconds. period T.
Use N = 50 in Equation 212 to define the number of complex
exponential signals to include in the sum. Evaluate the signal from
t = 0 to t = 4 seconds at 10 kHz sample rate.
Here’s some partial code, which almost does the job, but has
several things wrong.
import m a t p l o t l i b . pyplot as p l t
import numpy as np
# Define t h e sample r a t e (Hz) .
sample_rate = 10000.0
# C r e a t e time a r r a y 0 t o 1 seconds .
t = np . arange ( 1 . 0 * s a m p l e _ r a t e ) /s a m p l e _ r a t e
# I n i t i a l i z e empty v e c t o r t o hold F o u r i e r s e r i e s .
s i g = np . z e r o s ( l e n ( t ) , dtype=np . complex64 )
N = 10
# Add t o g e t h e r complex s i n u s o i d s m u l t i p l i e d with F o u r i e r
series coefficients .
f o r k i n range ( −N, N) :
print (k)
s i g += . . . # Complete t h i s l i n e .

plt . plot ( t , sig . real )


110

p l t . p l o t ( t , s i g . imag )
p l t . x l a b e l ( " Time ( s ) " )
p l t . show ( )

If implemented correctly, the Fourier series should be real-valued.


The imaginary part should only deviate from zero by only a very
small number due to numerical errors in finite precision accuracy
of floating point numbers.

b) Figure out what modification you need to make to the Fourier


series coefficients ck in order to delay the signal by 0.2 seconds.

c) Plot and verify that the coefficients obtained in b) produce the


correct delay.
Fourier Transform

This chapter discusses the continuous-time Fourier transform. We’ll


cover the following topics:

• Important Fourier transform pairs

• Plancherel’s theorem and Parseval’s theorem

• Convolution theorem

• Impulse response and frequency response of LTI systems

• Fourier transform of a time shifted signal

The forward Fourier transform is defined as:

Z ∞
x̂ (ω ) = x (t)e−iωt dt (213)
−∞

and the inverse Fourier transform is defined as:

Z ∞
1
x (t) = x̂ (ω )eiωt dω . (214)
2π −∞

We’ll use the following notation to indicate Fourier transform pairs:

F
x (t) ←
→ x̂ (ω ) . (215)

The left denotes the time domain representation of the signal and
the right-hand side denotes the frequency domain representation of
the same signal. I’ve used a hat to signify that the symbol x̂ (ω ) is a
frequency domain representation of a signal.
The existence or non-existence of a Fourier transform or an inverse
Fourier transform is rarely something that is encountered in signal
processing applications. Exploring this topic rigorously is outside the
scope of this course.
112

Selected Fourier transforms

It is impossible to exhaustively cover all the possible Fourier trans-


form pairs here. We’ll only give examples of commonly encountered
Fourier transforms and provide solution strategies for using these as
basic building blocks to evaluate the Fourier transform for more com-
plicated signals. Refer to a formula book19 , 20 for a more complete 19
The wikipedia article also contains
table of Fourier transform pairs. a nice table of Fourier transform
pairs:https://en.wikipedia.org/wiki/
Fourier_transform.
Linear combination
20
David W Kammler. A first course in
Fourier analysis. Cambridge University
Press, 2007
The Fourier transform of a linear combination of signals in time
domain is related to a linear combination of Fourier transforms in
frequency domain:
F
y ( t ) = c1 x1 ( t ) + c2 x2 ( t ) ←
→ ŷ(ω ) = c1 x̂1 (ω ) + c2 x̂2 (ω ) . (216)
This can be shown as follows:
Z ∞
ŷ(ω ) = [c1 x1 (t) + c2 x2 (t)]e−iωt dt (217)
−∞
Z ∞ Z ∞
= c1 x1 (t)e−iωt dt + c2 x2 (t)e−iωt dt (218)
−∞ −∞
x (t) = δ(t + τ )
= c1 x̂1 (ω ) + c2 x̂2 (ω ) . (219)
This may seem trivial, but keep this property in mind, as it can
be used to Fourier transform more complicated signals that are a
superposition of simple individual signals. t
−τ
Fourier transform of δ(t + τ ) x̂ (ω ) = eiωτ

The time shifted unit impulse signal is a complex sinusoidal signal in ω


frequency domain:
F
→ x̂ (ω ) = eiωτ .
x (t) = δ(t + τ ) ← (220)
Figure 60: Unit impulse signal δ(t +
This is also relatively easy to show:
τ ) is non-zero only when t = −τ.
Z ∞
In frequency domain, the Fourier
x̂ (ω ) = x (t)e−iωt dt (221) transform is a complex sinusoidal
−∞
Z ∞ signal with ω the independent variable.
= δ(t + τ )e−iωt dt (222) x (t) = eiω0 τ
−∞
iωτ
=e . (223) t

Fourier transform of eiω0 t


The converse of the previous is a complex sinusoidal signal eiω0 t with x̂ (ω ) = 2πδ(ω − ω0 )

frequency ω0 . This has the following Fourier transform pair:


F
x (t) = eiω0 t ←
→ x̂ (ω ) = 2πδ(ω − ω0 ) . (224)
ω
ω0

Figure 61: A complex sinusoidal signal


of frequency ω0 is a unit impulse signal
δ(ω − ω0 ) in frequency domain.
113

We show this by inverse Fourier transforming x̂ (ω ):

1 ∞ Z
x (t) = x̂ (ω )eiωt dω (225)
2π −∞
Z ∞
1
= 2πδ(ω − ω0 )eiωt dω (226)
2π −∞
= eiω0 t . (227)

Rectangular function
One often encountered Fourier transform pair is that of the rectangu-
lar function21 of length T: 21
Also often called the boxcar function.

   
T T F 1 iω T T
x (t) = u t + −u t− ←
→ x̂ (ω ) = (e 2 − e−iω 2 ) .
2 2 iω
(228)
This can be derived as follows:
Z ∞     
T T
x̂ (ω ) = u t+ −u t− e−iωt dt (229)
−∞ 2 2
Z T/2
= e−iωt dt (230)
− T/2
T T
 
T/2 x (t) = u t + 2 −u t− 2
e−iωt
= − (231)
iω t=− T/2
T T
eiω 2 − e−iω 2
= . (232)
iω t
− T2 T
We can use Euler sin( x ) = 1 ix
2i ( e − e−ix ) to express this as follows: 2
2 sin(ωT/2)
x̂ (ω ) = ω
 
2 sin ω T2
x̂ (ω ) = . (233)
ω ω

The function sinc(ω ) = sin(πω )/(πω ) is often used to express this


function. This is convenient, as numerical implementations (such as Figure 62: The Fourier transform of a
numpy.sinc) of the function deal with the special case when ω = 0, boxcar function.
which can be investigated using L’Hôpital’s rule:

f (ω ) f ′ (ω )
lim = lim ′ . (234)
ω →c g(ω ) ω →c g (ω )

Which in this case is:

2 sin(ωT/2)
lim = lim T cos(ωT/2) = T . (235)
ω →0 ω ω →0

We’ll be using L’Hôpital’s rule elsewhere in signal processing to


resolve similar situations.
114

Sinc function
A related Fourier transform pair is the one where the frequency
domain representation is a rectangular function. This is used, e.g.,
when calculating ideal filters. This result is also used in Shannon’s
sampling theorem to obtain an ideal reconstruction filter for band
limited signals that only have spectral components in the band
| ω | < ωb .

sin(ωb t) F
x (t) = ←
→ x̂ (ω ) = u(ω + ωb ) − u(ω − ωb ) . (236)
πt

This relationship can be shown by inverse Fourier transforming x̂ (ω ):

1 ∞
Z
x (t) = [u(ω + ωb ) − u(ω − ωb )] eiωt dω (237)
2π −∞
1
Z ω
b
= eiωt dω (238)
2π −ωb
ωb
1 eiωt x (t) = 1
sin(ωb t)
= (239) πt
2π it ω =−ωb
eitωb − e−itωb
= (240) t
2πit
sin(ωb t)
= . (241)
πt x̂ (ω ) = u (ω + ωb ) − u (ω − ωb )
1
Fourier transforming the sinc-function is not as trivial.
0.5
Gaussian
ω
The Fourier transform of a Gaussian density function is another − ωb ωb
Gaussian density function:
Figure 63: A sinc function in time do-
main is a boxcar function in frequency
r
−αt2 F π − ω2
x (t) = e ←
→ e 4α . (242) domain.
α

Note that the width of the time domain Gaussian is inversely propor-
tional to the width of the frequency domain Gaussian.
If we rewrite the above into a form which resembles a Gaussian
density function normalized to unity at zero, with a width parameter
σt and σω for the time domain and frequency domain width, we get:

− t2 ω2
2σt2 F −
x (t) ∝ e → x̂ (ω ) ∝ e 2
← 2σω , (243)

which implies that:


1
= σω . (244)
σt
This is a very nice demonstration of the inverse relationship between
the width of a signal in time domain and frequency domain. When
115

we discuss filters later on, we’ll use this to show that the length 2
x (t) = e−αt
of a filter in time domain is inversely proportional to the spectral
resolution (width of the filter response).
This Fourier transform pair can be derived as follows:
Z ∞
2 t
x̂ (ω ) = e−αt e−iωt dt (245)
−∞
Z ∞ 6 q
2 −iωt exp −ω 2 /4α

e−αt
π
x̂ (ω ) =
= dt . (246) α
−∞ 4
2 2 2 2
We perform a little trick and multiply with 1 = using ec ω e−c ω 2
a procedure called completing the square, so that we can get to a
ω
variable substitution that allows us to easily integrate the function
inside the integral:
Figure 64: The Fourier transform of a
Z ∞
c2 ω 2 −αt2 −iωt−c2 ω 2 Gaussian density function is another
x̂ (ω ) = e e dt . (247) Gaussian density function. The width
−∞
of the frequency domain and time
√ domain density functions are inversely
We now substitute a variable α′ = α, next we then need to find a
proportional.
constant c so that:

−t2 α′2 − iωt − c2 ω 2 = −(tα′ + cω )2 . (248)


i
Furthermore, we find that c = 2α′ .
Z ∞ 2
ω2 ′ i
x̂ (ω ) = e− 4α e−(tα + 2α′ ω ) dt . (249)
−∞

Now we perform a variable substitution: u = tα′ + 2αi ′ ω, which gives


us: α1′ du = dt.
We now need to refer to a table of integrals to obtain a solution to
the integral:
Z ∞ √
2
e−u du = π (250)
−∞

and thus
1 ω2 ∞ 2
Z
x̂ (ω ) = √ e− 4α e−u du (251)
α −∞
r
π − ω2
= e 4α . (252)
α
The plots in Figure 64 show the time domain and frequency domain x (t) = e− βt u(t)
Fourier transform pairs for two different values of α, which demon-
strates the inverse relationship between the width of the function in
time and frequency domain.
t
Exponentially decaying signal x (t) = e− βt u(t)
| x̂ (ω )|
When β ∈ R>0 the signal e− βt u(t) decays exponentially. This type of
signal is often encountered in electrical circuits (e.g., a low pass filter)

Figure 65: The Fourier transform of an


exponentially decaying signal.
116

and in control systems:

F 1
x (t) = e− βt u(t) ←
→ x̂ (ω ) = . (253)
β + iω

This can be derived as follows:


Z ∞
x̂ (ω ) = e− βt u(t)e−iωt dt (254)
−∞
Z ∞
= e−( β+iω )t dt (255)
0

1
= − e−( β+iω )t (256)
β + iω t =0
1
= . (257)
β + iω

The above requires that β ∈ R>0 .

Real-valued signals
There is a special conjugate symmetry for signals that are real-valued
either in time domain or frequency domain.
If the time domain signal is real-valued, then the frequency do-
main representation is conjugate symmetric.

F
→ x̂ (−ω ) = x̂ ∗ (ω ) .
x (t) ∈ R ← (258)

The same also applies the other way around. If the spectral rep-
resentation is real-valued, then the time domain representation is
conjugate symmetric:

F
x (−t) = x ∗ (t) ←
→ x̂ (ω ) ∈ R . (259)

This can be shown as follows:

Proof.
Z ∞
∗
x̂ ∗ (ω ) = x (t)e−iωt dt (260)
−∞
Z ∞
= x ∗ (t)eiωt dt (261)
−∞
Z ∞
= x (t)eiωt dt (262)
−∞
= x̂ (−ω ) . (263)

Because x (t) ∈ R, we have that x ∗ (t) = x (t). A similar proof exists


for the real-valued spectral representation.
117

Fourier series
A Fourier series can be used to represent a periodic function as a
sum of frequency components. Because we know that the Fourier

transform of eiω t is 2πδ(ω − ω ′ ), we can easily determine the Fourier
transform of an arbitrary periodic function by using its Fourier series:
∞ ∞
F
x (t) = ∑ ck eikω0 t ←
→ x̂ (ω ) = 2π ∑ ck δ(ω − kω0 ) . (264)
k =−∞ k =−∞

The derivation is as follows:


Z ∞ ∞
x̂ (ω ) =
−∞ k=−∞
∑ ck eikω0 t e−iωt dt (265)

∞ Z ∞ x̂ (ω )
= ∑ ck
−∞
eikω0 t e−iωt dt (266) c0
k =−∞

c −1 c1
= 2π ∑ ck δ(ω − kω0 ) . (267)
c −2 c2
k =−∞
c −3 c3
This is simply the continuous-frequency representation of Fourier c −4 c4
series spectral components. A periodic function has non-zero spectral
components only at frequencies that are multiples of ω0 . ··· ω0 ···
The inverse Fourier transform of the spectrum x̂ (ω ) is: ω
Z ∞
1
x (t) = x̂ (ω )eiωt dω (268)
2π −∞ Figure 66: The Fourier transform of a
∞ Z ∞ Fourier series. The spectral represen-
= ∑ ck
−∞
δ(ω − kω0 )eiωt dω (269) tation is a set of spectral lines spaced
apart by ω0 , the fundamental angular
k =−∞
frequency of the periodic signal. A


set of spectral lines spaced apart by a
= ck eikω0 t . (270)
constant value is a spectral signature of
k =−∞
a time-periodic signal.
The fact that the continuous-time Fourier transform and the Fourier
series are related in this way shouldn’t be too surprising, as we
earlier derived the Fourier transform from the Fourier series repre-
sentation for a periodic function with an infinitely long fundamental
period.

Time scaling system


Figure 67: A time-frequency-power
Let’s look at a time scaling system:
image of a violin being played. The
x-axis represents time and the y-axis
y(t) = x ( at) . (271) represents frequency. Only positive
frequencies are shown. At any given
What effect does this system have on the frequency domain represen- moment, a violin is playing approxi-
tation of this signal? mately a time-periodic signal (a single
note). The spectrum is a forest of
We assume that a signal x (t) has a Fourier transform: spectral lines spaced apart by the funda-
mental angular frequency of the signal.
F
x (t) ←
→ x̂ (ω ) . (272) Credits: Wikipedia.
118

If we scale the time parameter x ( at), then the spectral representation


is scaled inversely in frequency:

F 1 ω
y(t) = x ( at) ←
→ ŷ(ω ) = x̂ . (273)
| a| a

What does scaling in time mean?

• Compressing a signal in time with | a| > 1 will stretch the signal in


frequency.

• Stretching a signal in time with | a| < 1 will compress the signal in


frequency.

Proof. The proof is as follows. Let’s investigate the case where a > 0
and Fourier transform y(t):
Z ∞
ŷ(ω ) = y(t)e−iωt dt (274)
−∞
Z ∞
= x ( at)e−iωt dt . (275)
−∞

We do a variable substitution t′ = at, from which it follows that


dt = 1a dt′ and

1 ∞
Z
ω ′
ŷ(ω ) = x (t′ )e−i a t dt′ (276)
a −∞
1 ω
= x̂ . (277)
a a

Plancherel’s theorem
Plancherel’s theorem states that for any two signals f (t) and g(t)
with Fourier transforms F̂ (ω ) and Ĝ (ω ) the following integral is
satisfied:
Z ∞ Z ∞
∗ 1
f (t) g (t)dt = F̂ (ω ) Ĝ ∗ (ω )dω . (278)
−∞ 2π −∞

This is useful, as we can relate the amount of signal power in time


domain to the total amount of power in frequency domain. This the-
orem has many applications in, e.g., the theory of random processes,
radio astronomical signal processing, and radar signal processing.

Proof. The proof is as follows. Consider first the following signal,


which is represented using the Dirac delta function in frequency
domain:
x̂ (ω ) = δ(ω − ω ′ ) . (279)
119

This has a time domain-representation, which is obtained using the


inverse Fourier transform:
1 ∞
Z
x (t) = δ(ω − ω ′ )eiωt dω (280)
2π −∞
1 iω ′ t
= e . (281)

Let us now consider the Fourier transform representations for these
two signals:

1 ∞ Z
f (t) = F (ω )eiωt dω (282)
2π −∞
Z ∞
1 ′
g(t) = G (ω ′ )eiω t dω ′ . (283)
2π −∞
If we multiply the Fourier representations of these two and integrate
over time:
Z ∞ Z ∞ Z ∞  Z ∞ 
1 ′ −iω ′ t
f (t) g∗ (t)dt = F ( ω ) e iωt
dω G ∗
( ω ) e dω ′
dt
−∞ (2π )2 −∞ −∞ −∞
(284)
Z ∞ Z ∞ Z ∞
1 ′
= F (ω ) G ∗ (ω ′ ) ei(ω −ω )t dtdω ′ dω
(2π )2 −∞ −∞ −∞
(285)
Z ∞ Z ∞
1
= F (ω ) G ∗ (ω ′ )δ(ω − ω ′ )dω ′ dω (286)
2π −∞ −∞
Z ∞
1
= F (ω ) G ∗ (ω )dω . (287)
2π −∞

The key to this proof is that:


Z ∞ Z ∞
i (ω −ω ′ )t ′
e dt = e−iω t eiωt dt = 2πδ(ω − ω ′ ) . (288)
−∞ −∞

Parseval’s theorem is simply a corollary of Plancherel’s theorem. If


we state that f (t) = g(t) = x (t), then Plancherel’s theorem becomes:
Z ∞ Z ∞
1
| x (t)|2 dt = | x̂ (ω )|2 dω . (289)
−∞ 2π −∞

In physics this means that the total energy of the signal in time
domain and frequency domain is equivalent, up to a predefined fixed
scaling constant.

Convolution theorem
The convolution theorem states that a convolution operation in
time domain is a multiplication operation in frequency domain. A
120

convolution sum of signals x1 (t) and x2 (t) is defined as:


Z ∞
x1 ( t ) ∗ x2 ( t ) : = x1 (τ ) x2 (t − τ )dτ . (290)
−∞

The star symbol (∗) is often used in shorthand to express this opera-
tion.
An often used property in signal processing is that convolution in
time domain is multiplication in frequency domain.

F
y ( t ) = x1 ( t ) ∗ x2 ( t ) ←
→ ŷ(ω ) = x̂1 (ω ) x̂2 (ω ) . (291)

Proof.
Z ∞
ŷ(ω ) = y(t)e−iωt dt (292)
−∞
Z ∞
Z ∞ 
= x1 (τ ) x2 (t − τ )dτ e−iωt dt (293)
−∞ −∞
Z ∞ Z ∞ 
= x1 ( τ ) x2 (t − τ )e−iωt dt dτ . (294)
−∞ −∞

Next, we perform substitution of variables m = t − τ. From this, it


follows that t = m + τ and dm = dt. After substitution, we get:
Z ∞ Z ∞ 
−iωm
ŷ(ω ) = x1 ( τ ) x2 ( m ) e dm e−iωτ dτ (295)
−∞ −∞
Z ∞
= x̂2 (ω ) x1 (τ )e−iωτ dτ (296)
−∞
= x̂1 (ω ) x̂2 (ω ) . (297)

The converse of the previous is that multiplication in


time domain corresponds to convolution in frequency
domain:

F 1
y ( t ) = x1 ( t ) x2 ( t ) ←
→ ŷ(ω ) = x̂ (ω ) ∗ x̂2 (ω ) . (298)
2π 1

Proof. We start by Fourier transforming x1 (t) x2 (t):


Z ∞
ŷ(ω ) = x1 (t) x2 (t)e−iωt dt . (299)
−∞

We then substitute x2 (t) with its Fourier transform:


Z ∞
1 ′
x2 ( t ) = x̂2 (ω ′ )eiω t dω ′ , (300)
2π −∞

which gives us:


Z ∞  Z ∞ 
1 ′ iω ′ t ′
ŷ(ω ) = x1 ( t ) x̂2 (ω )e dω e−iωt dt . (301)
−∞ 2π −∞
121

We then swap the order of integration and combine all terms with t
inside the innermost integral:
Z ∞ Z ∞ 
1 ′ iω ′ t −iωt
ŷ(ω ) = x̂2 (ω ) x1 ( t ) e e dt dω ′ (302)
2π −∞ −∞
Z ∞ Z ∞ 
1 ′
= x̂2 (ω ′ ) x1 (t)e−i(ω −ω )t dt dω ′ (303)
2π −∞ −∞
Z ∞
1
x̂2 (ω ′ ) x̂1 ω − ω ′ dω ′

= (304)
2π −∞
1
= x̂ (ω ) ∗ x̂2 (ω ) . (305)
2π 1

This property is also very useful. We will use it next to inspect the
spectrum of an amplitude modulated signal. This property will also
be used when deriving Shannon’s sampling theorem.

Time shifted signal

Assuming that the Fourier transform of x (t) is x̂ (ω ), the Fourier


transform of a time shifted signal can be obtained using the following
formula:
F
→ ŷ(ω ) = e−iωt0 x̂ (ω ) .
y ( t ) = x ( t − t0 ) ← (306)

Proof. Left as an exercise.

Alternative definitions and notation


There are several notations and slightly different definitions of a
Fourier transform that one may encounter in the literature. These
mainly vary in how the frequency parameter of the spectral represen-
tation is scaled and what symbols are used to denote frequency and
the imaginary number. The underlying mathematical concepts are
the same.
The spectral representation is sometimes written with an explicit
function parameter iω instead of ω. Sometimes the hat-notation is
not used:
x̂ (ω ) = x̂ (iω ) = x (ω ) . (307)

Commonly used symbols for frequency include ω, ν, ξ, λ, k, l, and f .



Also, as usual, the imaginary number i = −1 is sometimes denoted
with j.
The most common alternative form is the one where the frequency
parameter is given scaled to units of cycles per unit of t:
ω
f = . (308)

122

If t is in units of time in seconds, then the units of f would be 1/s or


hertz. With this frequency parameterization, the transform is unitary
(it is norm preserving):
Z ∞
x̂1 ( f ) = x (t)e−i2π f t dt (309)
−∞
Z ∞
x (t) = x̂1 ( f )ei2π f t d f . (310)
−∞

Another commonly used definition for the Fourier transform is one


where the forward and reverse transform are scaled symmetrically:

1 ∞ Z
x̂2 (ω ) = √ x (t)e−iωt dt (311)
2π −∞
Z ∞
1
x (t) = √ x̂2 (ω )eiωt dω . (312)
2π −∞
Sometimes, the sign convention for the forward and reverse trans-
form is changed:
Z ∞
x̂3 (ω ) = x (t)eiωt dt (313)
−∞
Z ∞
1
x (t) = x̂3 (ω )e−iωt dω . (314)
2π −∞

All of these alternative Fourier transform definitions have the same


general properties, but the exact equations can differ in scaling con-
stants or in sign of the frequencies. For example, if we were to define
the Fourier transform as in equation 311, the Fourier transform of
x (t) = δ(t + τ ) would be x̂ (ω ) = √1 eiωτ , which is slightly different

from the result we obtained in Equation 220.
123

Exercises: Fourier Transform

1. Find the Fourier transforms of these signals:

a) x (t) = δ(t + 2) + δ(t) + δ(t − 2)


Hint: linearity and time shift properties.
sin(100π [t−2])
b) x (t) = π ( t −2)
Hint: time shift property.
c) x (t) = e−t u(t) − e−t u(t − 4)
Hint: look at the derivation of the Fourier transform of e− βt u(t).

Note that there are more than one way to solve the Fourier trans-
forms!

2. Find the inverse Fourier transform for the following:

a) x̂ (ω ) = 2 + cos(ω )
1 1
b) x̂ (ω ) = 1+iω − 2+iω
c) x̂ (ω ) = iδ(ω − 100π ) − iδ(ω + 100π ).

3. Use known Fourier transform pairs together with the properties of


the Fourier transform to complete the following Fourier transform
pairs:
F
a) x (t) = u(t + 3)u(3 − t) ←
→ x̂ =?
F
b) x (t) = sin(4πt) sin(50πt) ←
→ x̂ =?
sin(4πt) F
c) x (t) = πt sin(50πt) ←
→ x̂ =?
F
→ x̂ = cos2 (ω )
d) x (t) =? ←

4. Let x̂ (ω ) be the Fourier transform of x (t). Prove that if y(t) =


x (t − τ ), then ŷ(ω ) = e−iωτ x̂ (ω ).
124

Suggested solutions: Fourier Transform

1. a) If
x ( t ) = δ ( t + 2) + δ ( t ) + δ ( t − 2),
then by linearity we have that:

x̂ (ω ) = F { x (t)} = F {δ(t + 2)} + F {δ(t)} + F {δ(t − 2)},

now, by using time shift properties we get:

x̂ (ω ) = F { x (t)} = e2iω + 1 + e−2iω ,

by Equation 220.
b) Can rewrite the signal using the sinc function as:

sin(100π (t − 2))
x (t) = = 100sinc(100(t − 2)).
π ( t − 2)
Then by Equation 306 and Equation 236 the Fourier transform
is:
1
x̂ (ω ) = 100e−2iω F {sinc(100t)} = 100e−2iω [u(ω + 100π ) − u(ω − 100π )],
100
giving:

x̂ (ω ) = e−2iω [u(ω + 100π ) − u(ω − 100π )].

c) Take:
x ( t ) = e − t u ( t ) − e − t u ( t − 4),
then by linearity, the Fourier transform is:

x̂ (ω ) = F { x (t)} = F {e−t u(t)} − F {e−t u(t − 4)}.

The last term can be computed as follows:


Z ∞ Z ∞ Z ∞
−t −t −iωt −t −iωt
F {e u(t − 4)} = e u ( t − 4) e dt = e e dt = e−(1+ωi)t dt,
−∞ 4 4

Note that we cannot use the exponential decay formula here, as


the term is e−t u(t − 4) which is not the same as e−(t−4) u(t − 4).
This last expression can be solved with exponential decay, but
that’s not the case here, so we integrate manually, which gives:
 ∞
1 1
F {e−t u(t − 4)} = − e−(1+iω )t = e−4(1+iω ) .
1 + iω 4 1 + iω

The first term is easily found as it is an exponential decay with


β = 1, giving:
1 1 1  
x̂ (ω ) = − e−4(1+iω ) = 1 − e−4(1+iω ) .
1 + iω 1 + iω 1 + iω
125

2. a) Let x̂ (ω ) = 2 + cos ω, then the inverse Fourier transform is:

x (t) = F −1 { x̂ (ω )} = F −1 {2} + F −1 {cos ω },


1
= 2δ(t) + (F −1 {eiω } + F −1 {e−iω }),
2
1
= 2δ(t) + (δ(t + 1) + δ(t − 1)),
2

as F −1 {eiωτ } = δ(t + τ ).
1 1
b) For x̂ (ω ) = 1+iω − 2+iω , then the inverse Fourier transform is:
   
−1 −1 1 −1 1
x (t) = F { x̂ (ω )} = F −F ,
1 + iω 2 + iω
= e−t u(t) − e−2t u(t),

as F {e− βt u(t)} = 1
β+iω .
c) Let x̂ (ω ) = iδ(ω − 100π ) − iδ(ω + 100π ), then the inverse
Fourier transform is:

x (t) = F −1 { x̂ (ω )} = i F −1 {δ(ω − 100π )} − i F −1 {δ(ω + 100π )},


i 100πit i −100πit
= e − e ,
2π 2π
1 iπ/2 100πit
= (e e + e−iπ/2 e−100πit ),

1  π
= cos 100πt + .
π 2

3. a) Let x (t) = u(t + 3)u(3 − t), then x (t) is a rectangular function


with corners at t = ±3 and height 1. Therefore, we have:
Z ∞ Z 3  3
1 −iωt
x̂ (ω ) = F { x (t)} = u(t + 3)u(3 − t)e−iωt dt = e−iωt dt = e .
−∞ −3 −iω t=−3

Evaluating this yields:

1 −3iω 1 3iω 2 sin(3ω )


x̂ (ω ) = − (e − e3iω ) = (e − e−3iω ) = .
iω iω ω

b) Consider the signal x (t) given as:

x (t) = sin(4πt) sin(50πt),

then:
1 4πit
sin(4πt) = (e − e−4πit ),
2i
1
sin(50πt) = (e50πit − e−50πit ),
2i
126

which gives:
1  4πit  1   1  i54πt 
x (t) = e − e−4πit e50πit − e−50πit = − e − e−i46πt − ei46πt + e−i54πt ,
2i 2i 4
Next, we evaluate the transform:
Z ∞ Z ∞ 
1 
x̂ (ω ) = x (t)e−iωt dt = − ei54πt − e−i46πt − ei46πt + e−i54πt e−iωt dt,
−∞ 4 −∞

and obtain:
π
x̂ (ω ) = − [δ(ω − 54π ) − δ(ω + 46π ) − δ(ω − 46π ) + δ(ω + 54π )].
2

c) Let
sin(4πt)
x (t) = sin(50πt),
πt
then, to compute the Fourier transform we apply the convolu-
tion theorem, have that:
 
sin(4πt)
F = [u(ω + 4π ) − u(ω − 4π )] = x̂1 (ω ),
πt
F {sin(50πt)} = −iπ [δ(ω − 50π ) − δ(ω + 50π )] = x̂2 (ω ),

then in frequency domain we get:


1 ∞Z
x̂1 (ω ) ∗ x̂2 (ω ) = x̂ (τ ) x̂2 (ω − τ )dτ,
2π −∞ 1
Z ∞
1
= [u(τ + 4π ) − u(τ − 4π )][−πi (δ(ω − τ − 50π ) − δ(ω − τ + 50π ))]dτ,
2π −∞
Z ∞
1
= [u(τ + 4π ) − u(τ − 4π )][δ((ω − 50π ) − τ ) − δ((ω + 50π ) − τ )]dτ,
2i −∞
The integral consists of four terms, these being:
Z ∞
u(τ + 4π )δ(ω − 50π − τ )dτ = u(ω − 50π + 4π ) = u(ω − 46π ),
−∞
Z ∞
u(τ + 4π )δ(ω + 50π − τ )dτ = u(ω + 50π + 4π ) = u(ω + 54π ),
−∞
Z ∞
u(τ − 4π )δ(ω − 50π − τ )dτ = u(ω − 50π − 4π ) = u(ω − 54π ),
−∞
Z ∞
u(τ − 4π )δ(ω + 50π − τ )dτ = u(ω + 50π − 4π ) = u(ω + 46π ),
−∞

combining all these we get:


1
x̂ (ω ) = [u(ω − 46π ) − u(ω + 54π ) − u(ω − 54π ) + u(ω + 46π )].
2i

d) If x̂ (ω ) = cos2 (ω ), then we find x (t) by using the inverse


Fourier transform:
Z ∞
1
x (t) = cos2 (ω )eiωt dω.
2π −∞
127

Firstly, we rewrite our function as:


 
1 iω  2 1  2iω 
−iω
2
cos (ω ) = e +e = e + e−2iω + 2 ,
2 4

Substituting this we obtain:


Z ∞
1 1  2iω  1 1
x (t) = e + e−2iω + 2 eiωt dω = δ(t + 2) + 2δ(t) + δ(t − 2).
2π −∞ 4 4 4

4. Let x̂ (ω ) be the Fourier transform of x (t). Define y(t) = x (t − τ ),


then the Fourier transform of y(t) is by definition:
Z ∞ Z ∞
ŷ(ω ) = y(t)e−iωt dt = x (t − τ )e−iωt dt.
−∞ −∞

Let u = t − τ, then du = dt, thus:


Z ∞ Z ∞
ŷ(ω ) = x (u)e−iω (u+τ ) du = e−iωτ x (u)e−iωu du = e−iωτ x̂ (ω ),
−∞ −∞
R∞
as x̂ (ω ) = −∞ x (t)e−iωt dt.
128

Amplitude modulated radio [Optional]


Mixer
One application of the convolution theorem is explaining how am-
plitude modulated (AM) radio works. This is a nice and simple x (t) y(t) = x (t)c(t)

example, but it also demonstrates a more abstract concept of up- and


downconversion of signals, which is used e.g., in radio hardware22 .
Upconversion means that a signal is shifted in frequency up to a
c(t) = cos(ωc t)
higher frequency range. Downconversion means the opposite of that.
The underlying principle behind amplitude modulation is very Figure 68: A simple block diagram
simple. An audio signal x (t) is multiplied23 with a carrier signal representation of an AM radio signal
generator system, which multiplies an
c(t) = cos(ωc t), which is a high frequency sinusoidal signal:
audio signal x (t) with a carrier wave
c(t) = cos(ωc t).
y(t) = x (t)c(t) (315) 22
This same principle is used by nearly
= x (t) cos(ωc t) . (316) all radio devices, such as the radios
found in your smartphone. The only
difference is that the signal x (t) does
This is depicted in the block diagram shown in Figure 68. not represent an audio waveform, but a
Here ωc = 2π f c is the carrier angular frequency. A typical carrier scheme that encodes digital information
into a 1d waveform.
frequency f c for AM terrestrial radio transmissions is f c ∈ [0.1, 30] 23
In the early days of radio, this mul-
MHz. tiplication was implemented using
vacuum tubes. It can also be imple-
These types of radio transmissions that provide global radio
mented using a transistor or a diode
broadcasts still exist. Because signals bounce between the Earth’s circuit. Nowadays, it is more and more
ionosphere and ground in this frequency range, signals transmitted common to modulate signals with dig-
ital signal processing. Mathematically,
from one location in the world can in optimal conditions travel to the the operation is the same for all cases.
other side of the Earth.
We’ll now study what the spectrum ŷ(ω ) of signal y(t) looks like.
The key to understanding what ŷ(ω ) looks like is that the spectrum
of the signal x (t) is band-limited. This means that x̂ (ω ) = 0 when
|ω | > ωmax . For example, in the case of audio, we know that there
are no audible spectral components at frequencies above 20 kHz.
Therefore, in order to transfer an audio signal, we do not need to
include higher frequency components.
In Figure 69, the magnitude spectrum of the real-valued audio sig-
nal | x̂ (ω )| is shown with a red line. The plot represents a magnitude
spectrum for an arbitrary real-valued signal that occupies angular
frequencies between ±ωmax . Because the audio signal is real-valued
x (t) ∈ R, the magnitude spectrum is symmetric around the y-axis.
The spectrum of the signal c(t) can be determined by first repre-
senting the signal as a sum of two complex sinusoids (using Euler’s
formula):
1 1
c(t) = cos(ωc t) = eiωc t + e−iωc t . (317)
2 2
We know that the spectrum of a complex sinusoid is a frequency
shifted Dirac-delta, and hence, the Fourier transform of c(t) is

ĉ(ω ) = πδ(ω − ωc ) + πδ(ω + ωc ) . (318)


129

Figure 69: A spectral representation of


signals x (t) and c(t).
| x̂ (ω )|
|ĉ(ω )|

ω
−ωmax ωmax

We also know from the convolution theorem, that multiplying two


signals in time domain results in convolution in frequency domain:

F 1
y(t) = x (t)c(t) ←
→ ŷ(ω ) = x̂ (ω ) ∗ ĉ(ω ) . (319)

We can now inspect what the spectrum ŷ(ω ) looks like by evaluating
the convolution:
1 ∞ Z
ŷ(ω ) = x̂ (ω ′ )ĉ(ω − ω ′ )dω ′ (320)
2π −∞
1 ∞
Z
x̂ (ω ′ ) δ(ω − ωc − ω ′ ) + δ(ω + ωc − ω ′ ) dω ′
 
= (321)
2 −∞
1 1
= x̂ (ω − ωc ) + x̂ (ω + ωc ) . (322)
2 2
In this convolution, the Dirac delta functions act as frequency shifters,
which shift the function x̂ (ω ) up and down in frequency by ωc .
Figure 70 shows a plot of the spectrum of the original signal x (t),
and the upconverted signal y(t).

Figure 70: The spectrum of the am-


plitude modulated signal consists
| x̂ (ω )| of two copies of the spectrum of the
|ŷ(ω )| original signal, shifted up and down
by ωc in frequency and scaled by 12 in
amplitude.

ω
−ωc − ωmax −ωc + ωmax −ωmax ωmax ωc − ωmax ωc + ωmax

In order to reconstruct the audio signal x ( t ) from the


modulated signal y ( t ) , the signal is first multiplied with the
carrier signal:
w(t) = y(t)c(t) . (323)
130
y(t) = x (t) cos(ωc t)

This operation again is a frequency shift, just like in the modula-


tion step. We can again use the convolution theorem to obtain the
c(t) = cos(ωc t)
spectrum of the intermediate signal w(t):

w(t)
1
ŵ(ω ) = ŷ(ω ) ∗ ĉ(ω ) (324)
2π H(ω )
1 1
= ŷ(ω + ωc ) + ŷ(ω − ωc ) (325)
2 2
1
= [ x̂ (ω + ωc + ωc ) + x̂ (ω − ωc + ωc )] (326)
4
1 v(t)
+ [ x̂ (ω + ωc − ωc ) + x̂ (ω − ωc − ωc )] (327)
4
1 1 1 Figure 71: Demodulation or conversion
= x̂ (ω − 2ωc ) + x̂ (ω + 2ωc ) + x̂ (ω ) . (328) of a high frequency signal y(t) into an
4 4 2 audio signal v(t).
There are now three copies of the spectrum x̂ (ω ). One centered at
ω = 0 and two centered at twice the carrier frequency ω = ±2ωc as
shown in Figure 72.

Figure 72: Spectra of an AM demodu-


lated signal.
|ŵ(ω )|

H(ω )

ω
−2ωc −ωmax ωmax 2ωc

We almost have what we need, but we need to get rid of the high
frequency components. This can be done with the help of a low-pass
filter: (
2, |ω | ≤ ωmax
H(ω ) = . (329)
0, otherwise
which also corrects the scale back to the original. The frequency
response of such a filter is shown in both Figure 72 and 73 with red.
The output signal spectrum is then:

v̂(ω ) = H(ω )ŵ(ω ) (330)


= x̂ (ω ) . (331)

And therefore v(t) = x (t). Multiplying with a cosine signal and


low-pass filtering has shifted the signal back to DC and allowed us to
reconstruct the original signal x (t).
131

Figure 73: The original signal spectrum


x̂ (ω ) reconstructed from the high
v̂(ω ) = x̂ (ω ) frequency signal.

H(ω )

ω
−ωmax ωmax

RC-circuit [Optional]

Consider the following electrical circuit:


R

vin C vout

This is a very simple analogue filter implemented using a resistor


and a capacitor. This type of circuit is often used in, e.g., audio
electronics to remove high-pitched signals.
This system can be modeled as a linear time-invariant system.
In this case, vin (t) = x (t) and vout (t) = y(t). This system has the
following impulse response:
1 − 1 t
h(t) = e RC u(t) = αe−αt u(t) (332)
RC
where the real-valued constant α > 0.
In order to determine the frequency response, we evaluate the
Fourier transform of the impulse response:
Z ∞
H(ω ) = αe−αt u(t)e−iωt dt (333)
−∞
Z ∞
=α e−(α+iω )t dt (334)
0

e−(α+iω )t
=α (335)
−(α + iω )
t =0
α
= . (336)
α + iω
In order to find the magnitude response, we’ll need to establish that
α
H∗ (ω ) = , (337)
α − iω
132

which is relatively easy to show24 . 24


For example using:
The magnitude response is then: α α(α − iω ) α2 αω
= 2 2
= 2 −i 2
q α + iω α +ω α + ω2 α + ω2
|H(ω )| = H(ω )H∗ (ω ) (338)
α
= √ . (339)
α + ω2
2

The phase response is25 : 25


recall that tan−1 (− x ) = − tan−1 ( x )

Im(H(ω ))
∠H(ω ) = tan−1 (340)
Re(H(ω ))
ω
= − tan−1 . (341)
α
The magnitude and phase responses are plotted below:

|H(ω )| π
2 ∠H(ω )
1

π
4
√1
2
ω
−α α

− π4

ω
− π2
−α α

Note that the “half-power” point, where |H(ω )|2 = 12 corresponds to


ω = ±α. This half power width of a filter is often referred to as the
full width half maximum (FWHM). At this “half-power” point, the
phase is − tan−1 (±1) = ∓ π4 . For our example filter, the output power
has dropped to half when the frequency is:
1
ω = 2π f = (342)
RC
or
1
f = . (343)
2πRC
For example, if R = 1 kΩ and C = 100 pF, the cutoff frequency of the
filter is 1.6 MHz.
When looking at the Taylor series expansion of the phase around
zero, recalling that
d α
tan−1 (−ω/α) = 2 , (344)
dω ω + α2
we get
1
∠H(ω ) ≈ − ω = − RCω . (345)
α
133

This is shown as a dashed blue line in the figure above.


If we compare this with the equation for delay of a sinusoid x (t) =
Aeiωt , which we can derive from

x (t − t0 ) = Aeiω (t−t0 ) = Aeiωt e−iωt0 = Aeiωt eiφ (346)

to be −ωt0 = − RCω, we see that the filter corresponds to approxi-


mately a t0 = RC second time delay. This approximation is valid for
low values of |ω | < 12 α. In the case of R = 1 kΩ and C = 100 pF, the
time delay is approximately: 0.1 µs.
Discrete-time Signals x ( t ), x [ n ]
Ts

In this chapter, we introduce discrete-time signals. These are the type


of signals that are used for digital signal processing. We’ll cover the
following topics:
Figure 74: A continuous-time signal
• Discretization x (t), and a discrete-time signal x [n].
Sample-spacing Ts is related to sample-
rate as follows: Ts = 1/ f s .
• Aliasing

• Oversampling and undersampling

• Reconstruction

• Shannon-Nyquist sampling theorem

We’ll need to know about complex sinusoidal signals and the fre-
quency domain representation of signals (the Fourier transform).

Sampling x (t) C-to-D x [n] = x (nTs )


Ts
A continuous-time signal can be converted into a discrete-time signal
Figure 75: An ideal continuous-time to
by sampling it. This can be idealized as a system that performs the Discrete-time (C-to-D) converter.
following operation:
x [n] = x (nTs ) . (347)

In this process, a continuous function x (t) ∈ C is converted into an


ordered sequence of numbers x [n] ∈ C, where n ∈ Z indicates the
order of the numbers.
The reason why we consider complex-valued signals in addition
to real-valued signals is that there are many applications where the
signals are naturally complex-valued. Radio signal processing is one
example.
The sample-spacing Ts determines how densely samples of the
continuous-time signal are taken. Sample-spacing is related with the
sample-rate or sampling-rate f s as follows:

1
fs = . (348)
Ts
136

Sample-rate typically is given in units of samples per second or hertz,


but other units are also used in various applications. For example,
the resolution of a digital image is often given in units of dots per
inch (DPI).

Sampling a complex sinusoidal signal


In order to understand sampling, you need to understand how
sampling affects a complex sinusoidal signal:

x (t) = Aeiϕ eiω0 t . (349)


x̂ (ω )
Because we can use the Fourier transform to represent signals as
Aeiϕ
a superposition of complex sinusoidal signals, knowledge of what
happens to a fixed frequency signal will tell us how discretization
affect signals more generally.
So what happens to a complex sinusoidal signal when we sample
it?

x [n] = x (nTs ) (350)


iϕ iω0 nTs ω
= Ae e (351) ω0
= Aeiϕ eiω̂0 n . (352)

What happens is that we get a discrete-time complex sinusoidal


signal. This signal has a special type of frequency:

ω̂0 = ω0 Ts . (353)

The angular frequency ω̂0 is called a discrete-time angular frequency.


It indicates how many radians per sample the phase of the complex
sinusoidal signal advances. With a discrete-time signal, time is only
implied from the sample spacing that was used to discretize the
signal, which is why no time unit is attached to ω̂0 . There’s a hat
on ω̂0 to signify that this is not the same kind of frequency as the Im
continuous-time angular frequency ω, which has units of radians per x [1]
second.
x [0]
Figure 76 shows an example of how the values of the discrete-time
complex sinusoidal signal behave with increasing sample index, x [2] ω̂0
ω̂0
obtaining values on a circle of radius A in the complex plane. The ϕ Re
amount of angle per sample that the signal advances for each sample ω̂0

on the circle is given by the discrete-time angular frequency ω̂0 .

x [3]
Frequency aliases
What happens if ω̂ gets larger? What if it is so large that the phasor
rotates around the circle on the complex plane one or more times Figure 76: A discretized complex
every sample? This phenomenon is called aliasing. sinusoidal signal x [n] = Aeiϕ eiω̂0 n with
phase increments of ω̂0 radians per
sample. The initial phase for sample
n = 0 is given by ϕ.
137

Here’s a more formal way of defining aliasing. The complex sinu-


soidal signal eiω̂0 n is not unique. There are infinitely many possible
frequency increments that result in the same signal:

x [n] = Aeiϕ ei(ω̂0 +2πk)n (354)


iϕ i ω̂0 n
= Ae e (355)

where k ∈ Z. What this means is that we can select any one of the
discrete-time angular frequencies:

ω̂k = ω̂0 + 2πk . (356)

and our discrete-time complex sinusoidal signal will be identical.


There is no way of telling just from the signal itself what the true x̂ ( f )
discrete-time angular frequency is. Frequencies ω̂k are called aliases.
There are infinitely many such aliases.
What continuous-time frequencies do these aliases correspond
to? We can work this out by looking at the continuous-time signal of
frequency ω0 :
x (t) = Aeiϕ eiω0 t . (357)
When discretized, each of the following signals are identical:
f
iϕ iω0 Ts n
x [n] = Ae e (358) f0 − 2 fs f0 − fs f0 f0 + fs
iϕ i (ω0 Ts +2πk )n
= Ae e (359) Figure 77: Each one of these signals
= Aeiϕ ei(ω0 +2πk/Ts )Ts n (360) Aeiϕ ei2π ( f0 +k f s )t would result in the
same discrete-time complex sinusoidal
= Aeiϕ ei(ω0 +2πk f s )Ts n (361) signal when discretized with sample-
rate f s . Note that we’re showing
= Aeiϕ eiωk Ts n . (362) the spectrum with continuous-time
frequency in units of hertz (cycles per
We’ve used the relationship between sample-rate f s = 1/Ts and second) instead of radians per second.
sample spacing here. The aliases map to continuous-time angular
frequency as follows:

ωk = ω0 + 2πk f s (363)

Any one of these continuous-time angular frequencies ωk would


result in an identical discrete-time complex sinusoidal signal when
discretized with sample-rate f s . Dividing by 2π, we get the aliasing
behavior in units of hertz (samples per second)26 : 26
remember that angular frequency
(radians per second) and frequency
fk = f0 + k fs . (364) (cycles per second) are related as
follows ω = 2π f .

The phenomena of aliasing is problematic. We cannot uniquely


distinguish a discrete-time complex sinusoidal signal x [n] = Aeiω̂n
which continuous-time signal it corresponds to. Conversely, there
are infinitely many continuous-time sinusoidal signals x (t) = Aeiωt
that will result in the same discrete-time signal. In order to rule out
Ts
138 A

the wrong solutions, we need a priori information about the spectral t


occupancy of the continuous-time signal. −4 −2 2 4
Solving the problem of aliasing, by ensuring that there is a
uniquely one-to-one mapping between continuous-time and discrete-
time frequencies for all the spectral components of a signal, leads to −A
the Shannon-Nyquist sampling theorem, which is introduced at the
Ts
end of this chapter. A

Real-valued signals
t
The concept of aliasing of complex sinusoidal signals allows us to −4 −2 2 4
inspect aliasing of real-valued sinusoidal signals, which come in pairs
of positive and negative frequency spectral components. Understand-
ing aliasing for real-valued signals is slightly more complicated than −A
understanding complex-valued signals, because of the symmetric
pairing of the positive and negative frequency components. Figure 78: Two different complex
sinusoidal signals that result in the
The Fourier transform of a real-valued signal x (t) ∈ R is conjugate
same discrete-time signal.
symmetric
x̂ (ω ) = x̂ ∗ (−ω ) . (365)

This means that real-valued signals are composed of conjugate sym-


metric pairs of positive and negative frequency spectral components: x̂ (ω )

A iϕ iωt A −iϕ
2e
A iϕ
2e
A cos(ωt + ϕ) = (e e + e−iϕ e−iωt ) . (366)
2

In order to understand how aliasing affects real-valued signals, we


need to investigate how pairs of positive and negative frequency
spectral components are affected.
A conjugate symmetric pairing of positive and negative frequen-
cies means that there is no distinction between a positive and a
ω
negative frequency signal27 . For example, let’s say we have a signal − ω0 ω0
with angular frequency ω = −0.1:
Figure 79: A real-valued signal consists
A of a positive and negative frequency
A cos(−0.1t + ϕ) = (eiϕ e−i0.1t + e−iϕ ei0.1t ) . (367) spectral component, which are conju-
2
gate symmetric.
This is the same thing as a cosine signal with frequency 0.1, but a 27
For complex sinusoidal signals e−iωt
and eiωt are two unique signals.
sign flipped phase:

A −iϕ i0.1t
(e e + eiϕ e−i0.1t ) = A cos(0.1t − ϕ) (368)
2
= A cos(−0.1t + ϕ) . (369)

This means that you can always represent a real-valued sinusoidal


signal using only positive valued frequencies, but it does not apply
for complex-valued signals.
139

In the case of real-valued signals, the frequency alias correspond-


ing to a negative frequency is called a folded alias. For complex sinu-
soidal signals, a folded alias does not exist, as positive and negative
frequency components are not necessarily paired.

Example: Sinusoidal signal with f 0 = 1 Hz and f s = 10 Hz


Consider the following sinusoidal signal with a frequency of f 0 = 1
Hz and phase ϕ:
Figure 80: Example of signal alias-
x (t) = A cos(2π f 0 t + ϕ) (370) ing for f 0 ∈ {−19, −9, 1, 11, 21}
x (t) = A cos(2πt + ϕ) . (371) Hz and f s = 10 Hz. All signals
alias identically. Python code:
014_sampling/aliasing_example.py.
When discretized with a sample-rate f s = 10 Hz or Ts = 1/ f s = 0.1 s
this becomes:

x [n] = x (nTs ) (372)


= A cos(2πnTs + ϕ), (373)
= A cos(0.2πn + ϕ) . (374)

The discrete-time angular frequency is ω̂0 = 0.2π. For any k ∈ Z this


signal is identical to:

x [n] = A cos([0.2π + 2πk]n + ϕ) . (375)

In discrete-time angular frequencies (radians per sample), the aliases


are:

ω̂k = (0.2π + 2πk) (376)


= . . . , −3.8π, −1.8π, 0.2π, 2.2π, 4.2π, . . . (377)

In continuous-time frequency (Hz), the aliases would be:

fk = f0 + fs k (378)
= . . . , −19, −9, 1, 11, 21, 31, . . . (379)

In other words, a discretized cosine signal produced from any of


the frequencies f k would be identical to a 1 Hz signal, when using
10 Hz sample rate. The negative solutions are called folded aliases,
which can be interpreted as cosine signals with positive frequencies
9, 19, · · · but with a sign flipped phase.
Figure 82 shows part of the discrete-frequency spectrum where
there are three aliases of the cosine signal. There are infinitely many
aliases, but only three are shown here. It is customary to call the
region of the spectrum where |ω̂ | < π or | f | < f s /2, the principal
spectrum. This region of the spectrums contains the smallest discrete-
time angular frequency aliases.
140

Figure 81: The following figure illus-


trates what the continuous-time and
Ts
discrete-time signals look like. They are
A all identical in discrete-time, because
the continuous-time signals intersect
at the sampling points. The red line
shows ω0 = 0.2π f s (1 Hz), the green
t line shows ω1 = −1.8π f s (-9 Hz), and
the blue line shows ω2 = 2.2π f s (11
−4 −3 −2 −1 1 2 3 4 Hz). One can also see the phase-flip
on the negative frequency folded alias,
shown in green. The blue and red
continuous-time signals are both sam-
−A
pled at identical phases, but the green
not.

Figure 82: Spectral aliases of the


− fs − f2s fs
2 fs components plotted as a function of
discrete-time angular frequency ω̂
Folded Alias

Folded Alias

(radians per sample). The pairing


of positive and negative frequencies
Alias

Alias

Alias

Alias

indicates that this signal is real-valued,


and that the ω̂−1 = −1.8π alias
corresponds to a cosine signal with a
positive discrete-time angular frequency
of 1.8π and a flipped phase. Frequency
in units of hertz is shown on the top
axis.

ω̂
−2π −π π 2π
141

How could we be able to determine which one of the frequen-


cies f k is the true sinusoidal frequency component represented by
the discrete-time signal? We can only do this if we know that the
continuous-time frequencies of the spectral components of the signal
lie within a certain range.
For example, if we knew that there are no spectral components
with frequencies higher than 5 Hz present in the continuous-time
signal (| f | < 5) Hz, then we could determine that the discrete-time
signal is due to a 1 Hz continuous-time signal, because this is the
only possible mapping between a discrete-time frequency and a
continuous-time frequency.

Principal spectrum
The principal spectrum (also called normalized angular frequency) is
the range of discrete-time angular frequencies between:

−π < ω̂ < π . (380)

It is the region for the lowest frequency discrete-time angular fre-


quency representation for discrete-time spectral components that the
signal consists of. Unaliased continuous-time frequencies between

− f s /2 < f < f s /2 (381)

in units of hertz are mapped to this range of discrete-time angular


frequencies.
By finding a suitable value for k, it is possible to find an alias of
any discrete-time angular frequency ω̂, which lands in the range
−π < ω̂ + 2πk < π. This is called the principal alias. Every spectral
component of a signal has an alias in the principal spectrum.

For example, consider a signal of frequency f = 123 Hz:


1 i2π f t
x (t) = cos(2π f t) = (e + e−i2π f t ) , (382)
2
which is sampled using a sample-rate of f s = 100. The discrete-time
1
angular frequency is ω̂ = ±2π · 123 · 100 = ±2.46π. This is outside
the principal spectrum. If we shift this by ∓2π, we obtain principal
aliases: ω̂ = ±0.46π.

Folding
Figure 83 shows an illustration by Texas Instruments, which nicely
describes the concept of folding of spectral components. Figure 83: Folding.
Folding implies that at frequencies between [ f s /2 + k f s , f s + k f s ]
with k = 0, 1, 2, . . ., the order of the spectral components are flipped.
142

Note that this only occurs if the signal is real-valued, as folding relies
on a pairing of positive and negative frequency spectral components.

The following example demonstrates folding using three


real-valued sinusoidal signals. Let us consider three sinu-
soidal signals:

x1 (t) = A cos(2π f 0 t), (383)


x2 (t) = A cos(2π f 1 t), (384)
x3 (t) = A cos(2π f 2 t), (385)

with frequencies:

f 0 = 0.1 f s , (386)
f 1 = 0.2 f s , (387)
f 2 = 0.3 f s . (388)

When we discretize these signals, we obtain:

x0 [n] = A cos(2π f 0 n/ f s ) = A cos(0.2πn), (389)


x1 [n] = A cos(2π f 1 n/ f s ) = A cos(0.4πn), (390)
x2 [n] = A cos(2π f 2 n/ f s ) = A cos(0.6πn). (391)

These signals have spectral components with discrete-time angular


frequencies within the principal spectrum:

ω̂0 = ±0.2π, (392)


ω̂1 = ±0.4π, (393)
ω̂2 = ±0.6π. (394)

If we now take three more sinusoidal signals, with frequencies:

f 3 = 0.7 f s , (395)
f 4 = 0.8 f s , (396)
f 5 = 0.9 f s . (397)

When discretized we obtain the following principal aliases:

x3 [n] = A cos(2π f 3 n/ f s ) = A cos(1.4πn) = A cos(−0.6πn), (398)


x4 [n] = A cos(2π f 4 n/ f s ) = A cos(1.6πn) = A cos(−0.4πn), (399)
x5 [n] = A cos(2π f 5 n/ f s ) = A cos(1.8πn) = A cos(−0.2πn) . (400)

The frequencies f 3 , f 4 , and f 5 have the following aliases within the


principal spectrum:

ω̂3 = ∓0.6π (401)


ω̂4 = ∓0.4π (402)
ω̂5 = ∓0.2π . (403)
143

If we inspect the ordering of the frequencies, we can see that | f 0 | <


| f 1 | < | f 2 |. This agrees with |ω̂0 | < |ω̂1 | < |ω̂2 |. However, when we Ts Signal
compare the ordering | f 3 | < | f 4 | < | f 5 |, we can see that the order is A Samples
reversed with respect to |ω̂5 | < |ω̂4 | < |ω̂3 |. This is due to folding.

Nyquist oversampling criterion t


−4 −2 2 4
Let us assume that our continuous-time signal has non-zero spectral
components with frequencies only within the range | f | < 12 f s . This
means that it has a Fourier transform representation of the form: −A
Z π fs
1
x (t) = x̂ (ω )eiωt dω . (404)
2π −π f s

Alias
Alias

Alias
Alias
True

True
When such a signal is discretized, all the discrete-time spectral
components of the signal land on the principal spectrum between
−π < ω̂ < π.
The band limited nature of the continuous-time signal also guar-
antees that no high frequency components will have low frequency
aliases in the principal spectrum. In this case, one can be certain
that there is a one-to-one mapping between discrete-time frequency ω̂
and continuous-time frequency. And therefore, no information is −2π −π π 2π
lost when discretizing the signal. In this case, the signal is said to be
oversampled. Figure 84: Oversampling f s > 2 f 0 .

To avoid aliasing of spectral component frequencies f > f s /2 into


the principal spectrum, the sample rate f s has to be at least twice the
Ts
Signal
frequency of the highest frequency component of the signal: A Samples
f s > 2 f max . (405) Low frequency alias

It can be also expressed as


t
1 −4 −2 2 4
f max < fs . (406)
2
This is called the Nyquist oversampling criterion. It is a special case
of the more general Shannon-Nyquist sampling theorem. If this −A
criterion is not satisfied, then the signal is said to be undersampled.
When deriving the Shannon-Nyquist sampling theorem, we will
Alias

Alias

Alias

Alias

see that it is in some cases possible to retain information even when


True

True

the signal is undersampled. Undersampling is a technique that is


often used for sampling radio signals.

For example, the Nyquist frequency f max of a real-valued signal


sampled at f s = 44.1 kHz is f max = 22.05 kHz. The sample rate
44.1 kHz is often used when digitizing audio, because audio signals
ω̂
within the human hearing range are between 0 < f < 20 kHz and
−2π −π π 2π
signals conveying audio signals only contain spectral components
within this range. Figure 85: Undersampling f s < f 0 .
When the sample rate is smaller than
the frequency of the sinusoid there
is less than one sample per sinusoid
cycle. The discrete-time frequency
ω̂ > 2π and a low-frequency alias of
the high frequency sinusoid exists in
the principal spectrum.
144

Nyquist zones
A i ω̂
2e
0
If spectral components of a real-valued continuous-time signal are
A −i ω̂0
only located between k f s /2 ≤ | f | < (k + 1) f s /2 for only one value of 2e

k and if there are no non-zero spectral components elsewhere, then N4 N3 N2 N1 N0 N0 N1 N2 N3 N4


one can still recover the continuous-time signal from the discrete-time
representation of the signal, using the a priori information that the
signals are within this range of frequencies. This is the more general
sampling criterion for real-valued signals.
These regions, which are shown in Figure 86, are called Nyquist
ω̂
zones. In each of these cases, all the low frequency aliases of the
−3π −2π −π π 2π 3π
signal will be confined with the principal spectrum ±π. Because no
signals from outside the specific Nyquist zone are present, there is no Figure 86: Nyquist zones.
chance of two continuous-time spectral components mapping to the
same normalized angular frequency within the principal spectrum.
When k = 0, this case is called an oversampled signal. This is the Ts
Signal
A Samples
special case that the Nyquist oversampling criterion applies to. When
Low frequency alias
k = 1, the signal sampling is called folded undersampled signal. In
this case, the aliased signal in the principal spectrum is flipped in
t
frequency and phase relative to the continuous-time spectrum. When
−4 −2 2 4
k = 2, the signal is undersampled. These different cases are shown in
Figures 84, 87, and 85.
There are many cases, especially with modern software defined
−A
radios, where undersampling or folded undersampling at Nyquist
zones k > 0 are used. This violates the simple f s > 0.5 f max Nyquist
oversampling criterion, but is still perfectly fine, as long as the origi-
nal continuous-time signal has spectral components confined within Alias

Alias

Alias

Alias
True

True
a Nyquist zone.

Complex-valued signals
For complex-valued signals, the principal spectrum is the same as it
is for real-valued signals. In discrete-time angular frequency it is
−π < ω̂ < π and in continuous-time frequency − 12 f s < f < 21 f s .
However, because there are no conjugate symmetric negative fre- ω̂

quency pairs, both negative and positive frequencies can be used in −2π −π π 2π

this frequency band. Therefore, the effective bandwidth is twice that


Figure 87: Folded undersampling
of real-valued signals sampled at the sample rate f s , as both positive f 0 < f s < 2 f 0 , There are between 1
and negative frequency components can be used independently to and 2 samples for each cycle of the
continuous-time sinusoid. The low
encode information. frequency alias is phase flipped.
To ensure a one-to-one relationship between the discrete-time
signal and the continuous-time signal, the sample rate f s has to be
at least the difference between the largest and smallest frequency
component
f s > f max − f min . (407)
145

if the frequency domain representation of the complex-valued signal


is within a continuous range of frequencies between f min and f max .
This avoids having two continuous-time spectral components with
different frequencies aliasing onto the same discrete-time frequency
within the principal spectrum28 . This is another form of the Shannon- 28
The most general sampling criterion
Nyquist sampling criterion, which applies only to complex-valued involves investigating the relationship
between the frequency domain repre-
signals. sentation of the continuous-time signal
Figure 88 below illustrates aliasing with complex sinusoidal sig- and the frequency domain represen-
f tation of the discretized signal, which
nals. Here, a complex sinusoidal signal with frequency 1.2 2s that is we will discuss later when deriving the
f
sampled with frequency f s aliases to −0.8 2s in the principal spec- Shannon sampling theorem.
trum.

An example of aliasing of two complex sinusoidal signals


is shown below. If a continuous-time signal consists of two complex

f = −0.8 2s , ω̂ = −0.8π

f = 1.2 2s , ω̂ = 1.2π
sinusoidal signals:

z(t) = a1 ei2π f1 t + a2 ei2π f2 t , (408)

f
with frequencies f 1 = 1.2 f s /2 and f 2 = 0.8 f s /2.
When this signal is sampled with a sample-rate of f s , the Nyquist
oversampling criterion for real-valued signals is violated, because ω̂
f 1 > f s /2. However, this is a complex-valued signal, and we only −π π
need f max − f min < f s to hold in the more general case for complex-
Figure 88: Aliasing of a complex
valued signals. sinusoidal spectral component into
This condition is not violated. To see that everything is fine, we the principal spectrum. No conjugate
symmetric negative frequency spectral
translate both of the sinusoidal signals into the principal spectrum: components complicate the aliasing of
the signal into the principal spectrum.
z[n] = a1 ei1.2πn + a2 ei0.8πn (409)
−i0.8πn i0.8πn
= a1 e + a2 e . (410)

We see that the signals land at ω̂ = ±0.8π in the principal spectrum.


The two spectral components are distinct, and if we have sufficient
information about the lowest and highest frequency component
of the signal (i.e., we know f min and f max ), we can still perfectly
reconstruct the signal.

2D aliasing
Consider a two-dimensional signal I ( x, y) ∈ R, which is discretized
as follows:

I [n, m] = I (nTx , mTy ) . (411)

Here Tx and Ty are sample spacing in the x and y direction, and n,


and m are sample indices in the two dimensions.
146

A 2D signal has a spectral representation, which is given by 2D


complex exponential signals:
Z ∞ Z ∞
1
I ( x, y) = Î (ω1 , ω2 )ei(ω1 x+ω2 y) dω1 dω2 . (412)
(2π )2 −∞ −∞

To study aliasing behavior in 2D, it is sufficient to study aliasing


effects on individual spectral components:

z( x, y) = aei(ω1 x+ω2 y) , (413)

When discretizing the 2D complex exponential spectral compo-


nent, one can obtain aliasing behavior when sampling through
2π-periodicity:

z[n, m] = aei(ω1 Tx n+ω2 Ty m) (414)


i ([ω1 Tx +2πk ]n+[ω2 Ty +2π ℓ]m)
= ae (415)
= aei([ω̂1 +2πk]n+[ω̂2 +2π ℓ]m) . (416)

Here k, l ∈ Z.
Figure 89 demonstrates aliasing in 2D. This is discretized in such
a way that the high spatial frequency brick pattern on the side of the
building becomes undersampled, and a low frequency alias is seen.

Reconstruction
Figure 89: Example of aliasing behavior
Reconstruction involves transforming a discrete-time signal into a in a 2D image. Above: original, Below:
continuous-time signal. This type of operation is done, e.g., when aliased. When scaling an image (reduc-
translating an audio signal into air pressure variations to create ing its size), it is important to apply an
anti-aliasing filter that removes high
sound. frequency spectral components from
In order to convert a discrete-time signal into a continuous-time the high resolution image before it
is scaled down to a lower resolution.
signal, some form of interpolation is needed in order to fill in the Otherwise, there is a risk that high
space between the samples. This is the function of a reconstruction frequency periodic structures will ap-
filter, or a discrete-time to continuous-time (D-to-C) system: pear as low frequency structures in the
scaled image, as shown in the lower
image. Anti-aliasing filtering is a stan-
x (t) = R{ x [n]} . (417)
dard feature of most image processing
libraries, and one rarely sees the type of
The ingredients that are needed to reconstruct the signal are the
aliasing shown in the bottom figure in
samples x [n], information about the sampling rate Ts = 1/ f s , and practice.
information about the Nyquist zone that the signal is within. x [n] D-to-C x (t)
Ts
This is equivalent to knowing the true values of the discrete-
time frequencies ω̂ of all the discretized spectral components, i.e., Figure 90: A Discrete-time to
being able to rule out all the aliases using a priori knowledge of the Continuous-time (D-to-C) converter.
frequency band that the signal lies in.
There are several ways to perform this reconstruction. If the
analytical form of the signal is known, as in the case of a sinusoid,
the signal can be generated analytically based on the phase and
amplitude of the signal. In other cases, some form of interpolation is
needed.
147

Interpolation filters
In this section, we’ll only cover reconstruction of oversampled
signals29 that satisfy the Nyquist-Shannon sampling criterion 29
It is possible to derive interpolation
filters for reconstruction of undersam-
f s > 2 f max , i.e., the spectral components of the signal are between
pled signals in the same way, but this is
− f s /2 < f < f s /2. beyond the scope of this course.
We can convert a discrete-time signal into a continuous-time signal
using an interpolation filter of the form:
Original signal

x (t) = ∑ x [n] p(t − nTs ) . (418) Samples
Reconstruction
n=−∞
p(t)
Theoretically, this interpolation filter can use the discrete-time sam- A
ples to obtain the continuous-time reconstruction.

The zero-order hold filter is the simplest interpolation filter. t


It obtains a constant value for the duration of a sample. The filter in −4 −2 2 4
this case has the following mathematical definition:
( )
−A
1 when − T2s < t ≤ T2s
p(t) = . (419)
0 otherwise
Figure 91: Zero-order hold interpola-
tion.
The generated continuous-time signal looks like the one shown in
Figure 91.
The zero-order hold filter only needs one sample at a time. How- Original signal
ever, the continuous-time signal has large steps. The zero-order hold Samples
Reconstruction
filter approaches the continuous-time signal only when Ts → 0 – i.e.,
p(t)
when the sample rate is infinitely large.
A

A Linear interpolation filter is defined as follows:


( )
|t| t
1 − Ts when − Ts < t < Ts
p(t) = . (420) −4 −2 2 4
0 otherwise

It applies a linear interpolation between two consecutive samples.


−A
The generated continuous-time signal is depicted in Figure 92.
Figure 92: Linear interpolation filter.
The ideal reconstruction filter is based on the Shannon-
Nyquist sampling theorem. It guarantees theoretically that a continuous-
time real-valued signal can be perfectly reconstructed from discrete-
time samples for signals occupying the band − f s /2 < f < f s /2, if
f s > 2 f max . Here f max is the maximum frequency component within
the signal. We’ll prove this theorem at the end of this chapter.
In order to reconstruct the signal perfectly in a mathematical sense,
an ideal reconstruction filter must be used. Something approximately
resembling a truncated or tapered version of an ideal reconstruction
filter is typically used in real world applications.
148

The form of this ideal reconstruction filter can be derived from the
inverse Fourier transform of a rectangular shaped window that in
frequency domain is an ideal low pass filter:
Original signal
Samples
Reconstruction
(
Ts when |ω | ≤ π f s
P(ω ) = . (421) p(t)
0 otherwise
A

Here P(ω ) is the Fourier transform of the reconstruction filter, ω is


angular frequency. The resulting “ideal” reconstruction filter is: t
−4 −2 2 4
Z ∞
1
p(t) = P(ω )eiωt dω (422)
2π −∞
  −A
Ts π
= sin t . (423)
πt Ts Figure 93: Ideal reconstruction filter.

This filter is infinitely long, which implies that all samples need to
be used in order for the reconstructed signal to be mathematically
perfect.

Shannon’s sampling theorem

Shannon’s sampling theorem is a fundamental theorem in informa-


tion theory and signal processing30 . It relates the sample-rate of a
discrete-time signal to the bandwidth of a continuous-time signal
in frequency domain. The full form of the theory is not just: “use
a sample-rate that is at least twice as large as the largest frequency
component in the continuous-time signal”. There are other solutions,
Figure 94: Claude Elwood Shannon.
which also guarantee that no information about the signal is lost.
Photo: Bell Labs.
The most general way to investigate if information is lost when dis- 30
This theorem was discovered by
cretizing a signal is to explore the relationship between the Fourier Edmund Whittaker 33 years before
Claude Shannon, and thus sometimes
transform of the signal, and the Fourier transform of the discretized the sampling theory is called the
signal. Whittaker-Shannon sampling theorem.
We will assume that information is contained in the shape of the Edmund Taylor Whittaker. On the
functions which are represented by
signal x (t). This signal is assumed to be band-limited in frequency the expansions of the interpolation-
domain. This means that the spectrum is zero valued outside a theory. Proceedings of the Royal Society
certain range of frequencies: x̂ (ω ) = 0 when ω < ωmin or ω > ωmax . of Edinburgh, 35:181–194, 1915; and
Claude Elwood Shannon. A mathemat-
Is it possible to form a discrete-time signal x [n] from x (t) in such a ical theory of communication. The Bell
way that x [n] contains sufficient information to allow us to perfectly system technical journal, 27(3):379–423,
1948
reconstruct x (t)? In other words, can we perfectly reconstruct a
continuous-time signal x (t) from its discrete-time representation?
This is the question that Shannon studied, and his sampling theorem
provides an answer to.
149

Sampling

In the idealized case, sampling of a signal is selection of samples


from a continuous-time signal:

x [n] = x (nTs ) (424)

where Ts is the sample spacing. This is the inverse of the sample-rate


Ts = f s−1 . We can express this sampling process using the Dirac delta
function, where a delayed Dirac delta “samples” the value of the
signal x (t) at time nTs :
Z ∞
x [n] = x (t)δ(t − nTs )dt = x (nTs ). (425)
−∞

We can use a train of Dirac delta functions (Dirac comb)



s(t) = ∑ δ(t − nTs ), (426)
n=−∞ Figure 95: Sir Eduard Whittaker. Photo:
National Portrait Gallery.
to represent the sampled signal in continuous-time:

xs (t) = x (t)s(t) (427)



= x (t) ∑ δ(t − nTs ) (428)
n=−∞

= ∑ x [n]δ(t − nTs ) (429)
n=−∞

The sampled signal xs (t) is a continuous-time signal, which has the


same information content as the array of samples x [n]. This means
that xs (t) can be formed just by knowledge of the discrete-time signal
x [ n ].

Fourier series representation for Dirac comb

The Dirac comb



s(t) = ∑ δ(t − kTs ), (430)
k =−∞

is a periodic function with period Ts . Therefore, it is possible to


express it as a Fourier series:

∞ 2π
s(t) = ∑ ck ei Ts kt . (431)
k =−∞

The coefficients of the Fourier series can be obtained using the


Fourier series synthesis equation, by integrating over one period
150

of the function:

Z Ts /2
" #
1 2π
ck =
Ts − Ts /2
∑ δ(t − nTs ) e−i Ts kt dt (432)
n=−∞
1 Ts /2
Z

= δ(t)e−i Ts kt dt (433)
Ts −Ts /2
1
= e − i ·0 (434)
Ts
1
= (435)
Ts
We also note that 2π/Ts = ωs , which is the sample-rate expressed
as an angular frequency. The Fourier series representation of the
Dirac-comb is therefore:

1
s(t) =
Ts ∑ eikωs t . (436)
k =−∞

This Fourier series representation can now be substituted to represent


the sampling operation as:

xs (t) = x (t)s(t) (437)



!
1
= x (t)
Ts ∑ eikωs t (438)
k =−∞

Spectral representation of xs (t) and x (t)


We then Fourier transform xs (t) to study the relationship between
the sample-rate and the bandwidth of the signal. We use the prop-
erty that multiplication in time domain is convolution in frequency
domain. The Fourier transform of x (t) is x̂ (ω ). The Fourier trans-
form of s(t) is ŝ(ω ). We also make use of the fact that the Fourier
transform of eikωs t is 2πδ(ω − kωs ):
1
x̂s (ω ) = x̂ (ω ) ∗ ŝ(ω ) (439)

Z ∞
1
= x̂ (ω ′ )ŝ(ω − ω ′ )dω ′ (440)
2π −∞

Z ∞
!
1 1
=
2π −∞
x̂ (ω )′
Ts ∑ 2πδ(ω − kωs − ω ) ′
dω ′ (441)
k =−∞
∞ Z ∞
1
=
Ts ∑ x̂ (ω ′ )δ(ω − kωs − ω ′ )dω ′ (442)
k =−∞ −∞

1
=
Ts ∑ x̂ (ω − kωs ). (443)
k =−∞

In other words, the Fourier transform of the sampled function xs (t)


is infinitely many copies of x̂ (ω ), each shifted by kωs and scaled in
amplitude by T1s .
151

We illustrate this using the following two figures. The first one
depicts the magnitude spectrum of the original continuous-time
signal x (t):

| x̂ (ω )|

ω
− ωs −ωs /2 ωs /2 ωs

The sampled signal xs (t) has a periodic spectrum x̂s (ω ) = x̂s (ω +


kωs ), which has a period ωs :

| x̂s (ω )|

··· ···
ω
− ωs −ωs /2 ωs /2 ωs

This probably already provides some idea of what the sample-rate


should be, in order to avoid losing information. The most straight-
forward answer is that if the spectrum of the original signal x̂ (ω ) is
confined within the limits of ±ωs /2, then no overlap between the
different copies of x̂ (ω ) occurs.
There are other solutions that avoid aliasing for real-valued signals.
The original signal x̂ (ω ) might also be confined in frequency to one
of the intervals nωs /2 ≤ |ω | < (n + 1)ωs /2 with n ∈ Z. This
property is widely used when sampling radio signals that are at a
much higher frequency than the sample rate. This type of sampling
strategy is called undersampling.

Aliasing
If the different copies of x̂ (ω ) in x̂s (ω ) overlap, then aliasing occurs.
For example, if the spectrum of x̂ (ω ) extends over the limits ±ωs /2,
then there are regions where x̂s (ω ) is occupied by two or more
spectral components of x̂ (ω ) at the same time.
152

If the frequency extent of the original signal x̂ (ω ) is larger than ωs ,


then aliasing occurs as shown below:

ŝ(ω + kωs )
x̂s (ω )

··· ···
ω
− ωs −ωs /2 ωs /2 ωs

In this case, positive and negative frequency components of the


original spectrum x̂s (ω ) overlap and are added together when |ω | >
ωs /2, making it impossible to determine what the original value of
x̂ (ω ) is within the overlapping region.

Complex-valued signal

For a complex-valued signal x (t) ∈ C, a sufficient condition for


avoiding x̂ (ω ) overlapping with itself in x̂s (ω ) (aliasing) is that
ωs > ωmax − ωmin .
Aliasing with complex signals is illustrated with the following
plots. Consider a signal, which spans between frequencies 0.11ωs and
1.1ωs . In this case, the signal crosses ωs /2, which violates aliasing
rules for real-valued signals. However, ωs > ωmax − ωmin .

x̂ (ω ) ∈ C

ω
− ωs −ωs /2 ωs /2 ωs

The spectrum of the sampled signal xs (t) has a periodic spectrum


x̂s (ω ) = x̂s (ω + kωs ). Notice that there is no overlap between the
copies of the spectrum:
153

| x̂s (ω )|

··· ···
ω
− ωs −ωs /2 ωs /2 ωs

This means that the values of x̂s (ω ) between, e.g., 0.11ωs and 1.1ωs
fully determine the frequency domain representation of the original
signal x̂ (ω ).

Sampling criteria

The Shannon-Nyquist sampling theorem requires that it should be


possible to reconstruct x̂ (ω ) from x̂s (ω ). The commonly described
criterion that requires the sample-rate to be higher than twice the
largest frequency component of the signal:

f s > 2 f max (444)

is just one possible solution, which assumes that the spectral com-
ponents within the original signal x̂ (ω ) are confined to | f | < f s /2.
As we discussed above, complex-valued signals and under sampled
signals result in different sampling criteria.

Reconstruction

This reconstruction strategy only applies to a real-valued signal


x (t) ∈ R within the principal spectrum. It should be easy to deter-
mine how to create a reconstruction filter for, e.g., a complex-valued
signal occupying the band ω ∈ [ωmin , ωmax ] after following this
derivation.
In order to reconstruct signal x (t) from x [n], we first form xs (t)
using x [n]:

xs (t) = ∑ x [n]δ(t − nTs ) (445)
n=−∞

We then use an ideal low-pass filter specified in frequency domain as:


(
Ts |ω | ≤ ω2s
H(ω ) = (446)
0 otherwise
154

When we apply this filter x̂s (ω ) to reconstruct x̂ (ω ):



1
H(ω ) x̂s (ω ) = H(ω )
Ts ∑ x̂ (ω − kωs ) (447)
k =−∞

= x̂ (ω ) (448)
This operation is shown in the Figure below:

| x̂s (ω )|
H(ω )

··· ···
ω
− ωs −ωs /2 ωs /2 ωs

| x̂ (ω )|

ω
− ωs −ωs /2 ωs /2 ωs

This is in essence the proof of Shannon’s sampling theorem. To


obtain x (t), one can use an inverse Fourier transform:
Z ∞
1
x (t) = x̂ (ω )eiωt dω. (449)
2π −∞
If x̂s (ω ) contains overlapping copies of x̂ (ω ), then the original spec-
trum x̂ (ω ), and therefore the original signal x (t), cannot be recon-
structed.
For a comple- valued signal, we would need to apply an ideal
band-pass filter, which only retains signals between ωmin and ωmax ,
instead of the ideal low-pass filter. The same applies to an undersam-
pled signal.

Ideal reconstruction filter


Recall, from the beginning of this course, that we introduced the
ideal reconstruction filter without deriving it. Now we can derive it.
It is the inverse Fourier transform of the ideal low-pass filter
(
Ts |ω | ≤ ω2s
H(ω ) = (450)
0 otherwise
155

which is
1 ∞ Z
h(t) = H(ω )eiωt dω (451)
2π −∞
Z ωs /2
1
= Ts eiωt dω (452)
2π −ωs /2
ω /2
Ts eiωt s
= (453)
2π it ω =−ωs /2
Ts  i ωs t ωs

= e 2 − e −i 2 t (454)
2tπi
Ts ω 
s
= sin t (455)
πt 2
Keeping in mind that ωs /2 = π/Ts we obtain the familiar result:
 
Ts π
h(t) = sin t (456)
πt Ts

When applying this reconstruction filter in time domain, one con-


volves the sampled signal to obtain the reconstructed continuous-
time signal:

x (t) = h(t) ∗ xs (t) (457)


Z ∞
= xs (τ )h(t − τ )dτ (458)
−∞

Z ∞
!
=
−∞
∑ x [n]δ(τ − nTs ) h(t − τ )dτ (459)
n=−∞
∞ Z ∞
= ∑ x [n]
−∞
δ(τ − nTs )h(t − τ )dτ (460)
n=−∞
 
Ts ( t − nTs )
∞ sin π
= ∑ x [n] . (461)
Ts ( t − nTs )
π
n=−∞

This reconstruction formula now perfectly reproduces x (t) from


samples x [n], provided that ωs ≥ 2ωmax .
For an undersampled real-valued signal, or a complex-valued
band limited signal, the ideal reconstruction filter would be different.
In this case, it would correspond to the impulse response of an ideal
band-pass filter.
156

Exercises: Discrete-time Signals

1. The signal x (t) = Aeiω0 t is discretized using an idealized


continuous-to-discrete time converter x [n] = x (nTs ) = Aeiω̂k n .
Here A = 1 and ω0 = 2π10042 radians per second. The sample
rate is f s = 100 samples per second and Ts = 1/ f s .

a) What are all the possible values of ω̂k that result in an identical
discrete-time signal x [n]. Use normalized angular frequency
with units of radians per sample. The possible values ω̂k are
called frequency aliases. Here k ∈ Z is an integer index to the
different solutions.
b) What are the principal aliases in normalized angular frequency
with units radians per sample? Principal aliases are values of
ω̂k that lie in the interval −π < ω̂k < π.
c) Draw the spectrum of this signal with normalized angular
frequency in units of radians per sample on the horizontal
axis. Use the range −3π < ω̂ < 3π. How many frequency
components are there?
d) There are infinitely many continuous-time signals xk (t) =
Aeiωk t that will result in the same x [n]. What are all the pos-
sible continuous-time angular frequencies ωk ? What is the
smallest possible absolute value of |ωk |?
e) What is the smallest possible sample-rate f s at which the signal
x (t) can be sampled at, while still retaining enough informa-
tion to allow the signal to be reconstructed?

2. Given the discrete-time sinusoid y1 [n] = 2 cos(0.67πn) + cos(0.33πn).

a) Draw the spectrum for y1 [n]. Show aliases in the normalized


angular frequency range −3π < ω̂ < 3π (radians per sample).
Label the principal spectrum showing the phase, amplitude
and normalized angular frequency of each frequency compo-
nent.
b) Show how the spectrum changes if the signal changes to
y2 [n] = 2 cos(2.67πn) + cos(0.33πn). Use the same frequency
range as in a).

3. If you’ve ever seen an old western movie with a stagecoach wheel


seemingly rotating in the wrong direction, this exercise will help
you understand how this optical illusion occurs. You have done
a video recording of a disc with a red mark on the rim. The disc
started a clockwise rotation with slowly increasing rotational
speed 0 ≤ vrot ≤ 2880 rotations per minute (rpm). The sampling
rate of your camera was f s = 24 frames per second (fps). Assume
157

that each frame is captured instantaneously, i.e., the exposure time


is infinitely short. On the recording, you can see that the red spot
seems to rotate differently for certain speeds and speed ranges.

a) At what rotational speed (in rpm) does the red spot appear to
be standing still?
b) Around a certain speed, the red spot appears to start to ro-
tate counter-clockwise, and for what speed range will this
phenomenon occur?
c) Do you know another word for the counter-clockwise behav-
ior?
d) What behavior does the red spot have beyond the speed found
in a)?

4. The absolute values of the frequency domain representation of a


signal is shown in the figure below. We know that ω0 < ω1 and:

 0
 when ω ≥ ω1
| x̂ (ω )| = 0 when ω ≤ ω0 , (462)

 > 0 otherwise

| x̂ (ω )|

ω
ω0 ω1

R∞
a) x (t) = (2π )−1 −∞ x̂ (ω )eiωt dω. Why is x (t) not a real-valued
signal?
b) What is the minimum sample-rate (in units of samples per
second) required to retain all information about the signal?
c) Sketch a plot of | x̂s (ω )| for the sample rate you found in b).
x̂s (ω ) is defined in Equation 439. Why don’t the frequency
shifted copies of | x̂ (ω )| overlap?
d) What reconstruction filter would be needed in order to per-
fectly reconstruct x (t) from the discretized signal x [n], using
the sample-rate that you found?

5. The absolute values of the frequency domain representation of a


signal is shown in the figure below. We know that x̂ (ω ) = x̂ ∗ (−ω )
and that:
(
>0 when ω0 < | ω | < ω1
| x̂ (ω )| = , (463)
0 otherwise
158

We also know that ω0 = 2π30 and ω1 = 2π40 (radians per second).


This signal is undersampled using a sample-rate of f s = 20 hertz
(samples per second).

| x̂ (ω )|

ω
− ω1 − ω0 ω0 ω1

R∞
a) x (t) = (2π )−1 −∞ x̂ (ω )eiωt dω. Is the signal x (t) a real-valued
signal?
b) Sketch a plot of | x̂s (ω )|. Recall that x̂s (ω ) is defined in Equa-
tion 439. Why don’t the frequency shifted copies of | x̂ (ω )|
overlap?
c) The sample rate f s = 20 hertz is sufficient for retaining all
information about x (t). Why?
d) What is the frequency domain definition of the perfect recon-
struction filter that reproduces the original continuous-time
signal x (t) from a sampled signal x [n]?
159

Suggested solutions: Discrete-time Signals

1. The signal x (t) = Aeiω0 t is discretized using an ideal continuous-


to-discrete time converter x [n] = x (nTs ) = Aeiω̂k n . Let A = 1 and
ω0 = 2π10042 rad/s. Let the sample rate be f s = 100 samples per
second and Ts = 1/ f s .

a) Have ω̂0 = ω0 Ts and ω̂k is:


1 5021
ω̂k = ω̂0 + 2πk = ω0 + 2πk = π + 2πk,
fs 25

for k ∈ Z.
b) The principal aliases lie in −π < ω̂k < π. Pick a k ∈ Z such
that −π < ω̂k < π. For instance, k = −100 works:
5021
−π < π − 2π100 < π,
25
giving a value of ω̂ ≈ 2.6389 rad/sample.
c) To draw the spectrum for x (t) we can use the code in Listing
27. The boundaries for −3π and 3π are shown as vertical lines
in green. The principal alias is shown in blue while the other
aliases are shown in red. There are a total of three spectral
components on this the interval (−3π, 3π ).
import m a t p l o t l i b . pyplot as p l t
import numpy as np

f s = 100 # Sample r a t e i n Hz .
om0 = 2 * np . p i * 1 0 0 4 2

def omk( k ) :
" " " Function t o compute t h e d i f f e r e n t a l i a s e s . " " "
r e t u r n om0/ f s + 2 * np . p i * k

# P l o t on t h e i n t e r v a l ( −3 * np . pi , 3 * np . p i ) .
om = np . l i n s p a c e ( −3 * np . pi , 3 * np . pi , num=1000)

# P l o t t h e components .
p l t . v l i n e s (omk( − 1 0 0 ) , ymin =0 , ymax=1 , c o l o r = " blue " , l a b e l = "
P r i n c i p a l value " )
p l t . v l i n e s (omk( − 9 9 ) , ymin =0 , ymax=1 , c o l o r = " red " , l a b e l = "
Alias " )
p l t . v l i n e s (omk( − 1 0 1 ) , ymin =0 , ymax=1 , c o l o r = " red " )
p l t . v l i n e s ( −3 * np . pi , ymin =0 , ymax=1 , c o l o r = " green " , l a b e l = r
" $ ( −3\ pi , 3 \ p i ) $ " )
p l t . v l i n e s ( 3 * np . pi , ymin =0 , ymax=1 , c o l o r = " green " )
p l t . x l a b e l ( r " $\hat {\omega } $ " )
p l t . y l a b e l ( r " $\hat { x } ( \ omega ) $ " )
p l t . g r i d ( True )
p l t . legend ( )
p l t . show ( )

Listing 27: Suggested solution to c)


160

The spectrum x̂ (ω ) is shown in Figure 96.


d) The possible angular frequencies that result in the same discriti-
zation is found by:
ωk = ω0 + 2πk f s .
Figure 96: Spectrum of x (t), we see
Choosing different values for k and search for a value that 3 spectral components in the range
satisfy the requirement of being the smallest, |ωk | is found to (−3π, 3π )

be k = −100. This value gives ωk = 2π42.


e) Here the signal is complex, so the signal can be sampled at
ω0
f s > f max − f min = 2π by the Shannon sampling theorem, which
correspond to f s > 10042 Hz.

2. Given the continuous-time sinusoid y1 [n] = 2 cos(0.67πn) +


cos(0.33πn).

a) Using Euler’s formula to first rewrite y1 [n] on polar form:


1 1
y1 [n] = ei0.67πn + e−i0.67πn + ei0.33πn + e−i0.33πn
2 2 y[n]
and then plot the spectrum as shown in figure on the side. The e−i2.67π e−i0.67π ei1.33π
1
blue nodes are all aliases of each other, the same goes for the e−i1.33π ei0.67π ei2.67π
red ones. The light blue and scarlet colored nodes are folded
aliases. 1 −i2.33π 1 −i0.33π 1 i1.67π
2e 2e 2e
b) For the changed signal y2 [n] = 2 cos(2.67πn) + cos(0.33πn), 1 −i1.67π
2e
1 i0.33π
2e
1 i2.33π
2e
the spectrum will stay unchanged because cos(2.67πn) =
cos(0.67πn).
ω̂
3. A sampling rate of 24 fps equals 1440 frames per minute. −3π −2π −π π 2π 3π

a) When the disc speed equals the camera’s sampling rate at 1440
rpm, the red spot will be recorded at the same position for
every frame, and thus appears to be standing still.
b) When the disc speed gets larger than the Shannon-Nyquist
sampling theorem, vrot > f s /2 > 720 rpm, aliasing starts
to occur. Within the speed range of 720 < vrot < 1440 rpm,
the phase of the alias has the opposite sign than the phase of
the real rotation, and so the red spot seems to rotate counter-
clockwise with decreasing speed as vrot increases.
c) Aliasing in the interval with opposite phase sign is called
folding.
d) When 1440 < vrot < 2880, there is no folding, and the corre-
sponding alias of the red spot will have speed 0 < vrot < 1440
rpm.
R∞
4. Let x (t) = (2π )−1 −∞ x̂ (ω )eiωt dω.
161

a) By the definition of | x̂ (ω )| and the Figure we see that there is


no pairing for the frequencies, meaning that x̂ ∗ (ω ) ̸= x̂ (−ω ),
so x (t) can’t be real.
b) For complex signals, we must sample at a rate that satisfy
1
f s > f max − f min = 2π (ω1 − ω0 ) in units of Hz.
c) By the Shannon sampling theorem the sample rate f s from
above is sufficient to retain all the information regarding the
signal, thus the spectrum has no overlap. This is reflected in
the drawing of | x̂s (ω )| where the copies are shown in red.

| x̂s (ω )|

ω
ω0 ω1

d) In this case, the reconstruction filter would be an ideal band-


pass filter of the form:

T , ω ≤ ω ≤ ω ,
s 1 2
H(ω ) =
0, otherwise.

Have xs (t) as:



xs (t) = ∑ x [n]δ(t − nTs ),
n=−∞

from the samples. To reconstruct the signal, simply multiply


x̂ (ω ) by the reconstruction filter in frequency domain. The
frequency domain representation of xs (t) was shown to be:

1
x̂s (ω ) =
Ts ∑ x̂ (ω − nωs ).
n=−∞

Where ωs = 2π f s . Thus, multiplying in frequency domain


yields:

1
H(ω ) x̂s (ω ) = Ts
Ts ∑ x̂ (ω − nωs ) = x̂ (ω ),
n=−∞

here the sum is killed by H(ω ) as it is 0 outside ω1 ≤ ω ≤ ω2 .


Hence, we retain all the information necessary to reconstruct
the signal as:
Z ∞
x (t) = (2π )−1 x̂ (ω )eiωt dω.
−∞
162

R∞
5. As before, let x (t) = (2π )−1 −∞ x̂ (ω )eiωt dω and:

> 0 when ω0 < | ω | < ω1 ,
| x̂ (ω )| =
0, otherwise,

with ω0 = 2π30 and ω1 = 2π40 radians per second. Here we use a


sample rate of f s = 20 Hz or ωs = 2π20 radians per sample.

a) We know that x̂ (ω ) = x̂ ∗ (−ω ), so the signal is real-valued.


b) Draw | x̂s (ω )| with the different aliases shown in red:

| x̂s (ω )|

ω
− ω1 − ω0 ω0 ω1

The frequency shifted copies do not overlap due to the sample-


rate satisfying the Shannon-sampling theorem, but here the
signal is undersampled, as it is confined in another interval
than the typical +ωs /2. Recall that the goal of sampling is
exactly to avoid the shifted copies overlapping, which can be
done with the provided sample-rate if we use a different area
as to avoid aliasing.
c) The condition f s > 2 f max is violated in this case. This condition
applies to real signals, but assumes the frequencies fall in the
principal spectrum of | f | < f s /2 which is not the case here.
However, the sample rate satisfy:
ωs ωs
n ≤ | ω | ≤ ( n + 1) ,
2 2
or
n2π10 ≤ |ω | ≤ (n + 1)2π10,
which is satisfied for n = 3, as this gives:

2π30 ≤ |ω | ≤ 2π40,

this bounds the spectrum and avoids aliasing, hence this is


sufficient to retain all the information in the signal.
d) Define a filter H(ω ) as an ideal band-pass filter:

 T , ω ≤ |ω | ≤ ω ,
s 0 1
H(ω ) =
0, otherwise.
163

Then:

1
H(ω ) x̂s (ω ) = Ts
Ts ∑ x̂ (ω − kωs ) = x̂ (ω ).
k =−∞

Due to H(ω ) being 0 outside ω0 ≤ |ω | ≤ ω1 the sum goes


away giving back the original frequency representation of the
signal. Note that the absolute value is needed here to get both
the positive and negative frequencies as the signal is real, then
applying the Fourier transform to this signal yields the original
signal x (t).
Linear Time-invariant Systems

Linear time-invariant systems (LTI) are an important class of systems


that can be analyzed easily in frequency domain. An LTI system is
equivalent to convolution of the impulse response of the system. The
focus of this chapter is to learn more about what these concepts are.

Example: Running average filter

Here’s an example of a discrete-time system that you might use to


smooth a noisy signal:

1 7
y[n] = ∑ x [n − k] .
15 k=−
(464)
7

What does this system do? It averages together 15 neighboring values


of the input signal x [n]. Figure 97 shows a demonstration of this
system in action. The blue line indicates a noisy input signal x [n],
and the orange line depicts the output y[n] of the running mean filter
given in Equation 464. As you might expect, the output of the system
is a smoother version of the input signal.

Figure 97: A running average filter


is often used to smooth a noisy sig-
nal. You can find the Python code
used to produce this example in
016_smoothing/smoothing.py.
166

Finite impulse response filter


x [n] y[n]
The previous system shown in Equation 464 is a special case of a LTI
δ[n] h[n]
more general type of discrete-time LTI system, known as a Finite
Impulse Response (FIR) filter. This type of signal is often used in Figure 98: Discrete-time LTI systems are
digital signal processing. An FIR filter is defined as follows: characterized by an impulse response
h[n], which is the response of the LTI
system to a unit impulse signal.
N
y[n] = ∑ bk x [ n − k ] . (465)
k =− M

The coefficients bk ∈ C here are constant valued coefficients. As the


name implies, there is a finite number of non-zero coefficients bk . In
the case of the running average filter in Equation 464, there would be
15 coefficients, which are all bk = 1/15.

General discrete-time LTI system

What if we allow there to be infinitely many coefficients for the


system given in Equation 465? We get the following:
∞ ∞
y[n] = ∑ bk x [ n − k ] = ∑ b[k] x [n − k] . (466)
k =−∞ k =−∞

This turns out to be the general representation for arbitrary discrete-


time LTI systems! In this case, it makes sense to think of the infinitely
h[n]
many coefficients bk as a signal b[k].

For discrete-time LTI systems in general, the output of a system
is given by a discrete-time convolution sum of the input signal x [n]
with an impulse response h[n]:
n
∞ −10 −7 7 10
y[n] = T { x [n]} = ∑ h[k] x [n − k] . (467)
k =−∞ Figure 99: The impulse response of the
15 point running mean filter described
The impulse response is defined as follows: in Equation 464.

h[n] = T {δ[n]} . (468)

It is hopefully easy to see that Equation 467 is valid for all LTI sys-
tems.
A linear system T {·}31 must by definition satisfy the following 31
If linearity applies for two input
relation: signals T {α1 s1 [n] + α2 s2 [n]} =
∞ ∞ α1 T {s1 [n]} + α2 T {s2 [n]}, it also ap-
( )
T ∑ αk sk [n] = ∑ αk T {sk [n]} . (469) plies for linear combinations of three or
more signals.
k =−∞ k =−∞

Here αk ∈ C are arbitrary constants and sk [n] are arbitrary signals.


Time-invariance of T {·}, on the other hand, implies that for any
time shift k in the input, the output is correspondingly time shifted.
167

This is valid for any input signal x [n]:

y[n − k] = T { x [n − k]} , (470)

if
y[n] = T { x [n]} . (471)
It is possible to represent any signal x [n] with the help of time-shifted
unit impulse signals32 : 32
Recall that the discrete-time unit
impulse is defined as:

∑ x [k]δ[n − k] .

x [n] = (473) δ[n] =
1 when n=0
.
k =−∞ 0 otherwise
(472)
Linearity implies that: It is the discrete-time equivalent of a
Dirac delta function. The unit impulse
∞ ∞
( )
is shown in Figure 100.
T { x [n]} = T ∑ x [k]δ[n − k] = ∑ x [k]T {δ[n − k]} . (474)
k =−∞ k =−∞
δ[n]
1
The equation above is the same as Equation 469 with sk [n] = δ[n − k]
and αk = x [k]. Due to time-invariance, we can relate the impulse
response h[n] delayed by k to the term on the right-hand side above.
If a unit impulse fed into the system is n
−3 −2 −1 1 2 3 4 5 6
h[n] = T {δ[n]}, (475)
δ [ n − n0 ]
1
then a time-shifted unit impulse corresponds to a time-shifted
output:
h[n − k ] = T {δ[n − k ]} . (476)
n
Therefore, the output of an LTI system for an arbitrary input signal
n0
x [n] can be expressed using the impulse response h[n] as follows:
∞ Figure 100: Discrete-time unit impulse
y[n] = ∑ x [ k ] h [ n − k ]. (477) signal δ[n] and a time-shifted version
δ[n − n0 ] centered at n = n0 .
k =−∞

This type of equation is known as a discrete-time convolution sum.


We have now shown that any discrete-time LTI system can be rep-
resented with such a convolution sum. Note that Equation 477 isn’t
quite yet the same as Equation 467. We will later show that the convo-
lution sum is commutative, i.e., that:
∞ ∞
∑ x [k]h[n − k] = ∑ h [ k ] x [ n − k ]. (478)
k =−∞ k =−∞

Which completes the proof.

Example: Impulse response of an FIR filter


An FIR filter (Equation 465) has the following impulse response:
N
h[n] = ∑ bk δ [ n − k ] . (479)
k =− M
168

The signal h[n] contains the values of the filter coefficients h[n] = δ[n] h[n]
bn . Since there are a finite number of coefficients bk , the impulse FIR filter (bk )

response h[n] has non-zero values only in a finite range of samples.


Figure 101: The impulse response of
This is also where the name “finite impulse response” comes from. an FIR filter is a signal that contains
the coefficients. For a finite number of
coefficients bk , the length of the non-
Impulse response zero portion of the impulse response is
finite, and hence the name FIR.
Linear time-invariant (LTI) systems are fully characterized by an
impulse response h(t). This impulse response is obtained by feeding
a unit impulse into the LTI system:

h(t) = T {δ(t)} . (480)

Using an impulse response, it is possible to represent the output of


any LTI system as a convolution between the impulse response and
the input signal.
Z ∞
y(t) = T { x (t)} = h(t) ∗ x (t) = h(τ ) x (t − τ )dτ . (481)
−∞

Let’s prove this. We’ll first need to represent an arbitrary signal as a


sum of unit impulses:
Z ∞
x (t) = x (τ )δ(t − τ )dτ . (482)
−∞

One way to think of this integral is that the unit impulse δ(t − τ )
“selects” the value of x (τ ) where τ = t. Another way to think of
this is that the Dirac delta functions form a set of basis functions for δ(t) h(t)
LTI
representing the signal x (t).
You may recall that linearity of the system T {·} implies that: Figure 102: A linear time-invariant
system is characterized by an impulse
T {c1 δ(t − τ1 ) + c2 δ(t − τ2 )} = c1 T {δ(t − τ1 )} + c2 T {δ(t − τ2 )} . response.
(483)
Here I’ve used δ(t − τ1 ) and δ(t − τ2 ) as two different input signals.
The terms c1 , c2 ∈ C are arbitrary complex-valued constants.
Linearity must therefore also apply for the linear combination of
an arbitrary number of inputs:
( )
T ∑ xn δ(t − τn ) = ∑ xn T {δ(t − τn )} . (484)
n n

Linearity can be extended even further into a continuous linear


combination:
Z ∞  Z ∞
T x (τ )δ(t − τ )dτ = x (τ )T {δ(t − τ )}dτ . (485)
−∞ −∞

We can simplify the right-hand side and get:


Z ∞
x (τ )T {δ(t − τ )}dτ = T { x (t)} . (486)
−∞
169

In order to simplify the left-hand side, we have to rely on the prop-


erty of time-invariance. That is:

h(t) = T {δ(t)} ⇒ h(t − τ ) = T {δ(t − τ )} . (487)

This now completes our proof:


Z ∞
y(t) = x (τ )h(t − τ )dτ = T { x (t)} . (488)
−∞

The output of a linear time-invariant system y(t) = T { x (t)} is a


convolution of the system’s impulse response h(t) = T {δ(t)} with
the input signal x (t) fed into the system.

Convolution

A convolution operation is defined for both continuous-time and


discrete-time signals. As we just saw, a convolution operation can be
interpreted as an LTI system applied to a signal.
The continuous-time convolution is defined as an integral:

Z ∞
a(t) ∗ b(t) = a(τ )b(t − τ )dτ . (489)
−∞

The discrete-time convolution is defined as a sum:


a[n] ∗ b[n] = ∑ a[k]b[n − k] . (490)
k =−∞

Properties of a convolution

The properties of the convolution operation are shown in Table 1.


The same properties also exist for the continuous-time convolution
operation.

Property Equation Table 1: Table of convolution properties


identity x [n] ∗ δ[n] = x [n]
commutative a[n] ∗ b[n] = b[n] ∗ a[n]
associative ( a[n] ∗ b[n]) ∗ c[n] = a[n] ∗ (b[n] ∗ c[n])
distributive a[n] ∗ (b[n] + c[n]) = a[n] ∗ b[n] + a[n] ∗ c[n]
The proof of the identity property is clear, and the commutative
property is left as an exercise. The associative property takes a bit
more work, and the proof is as follows:
170

Proof. Using the definition of the convolution operation twice:


!
( a[n] ∗ b[n]) ∗ c[n] = ∑ ∑ a[ℓ]b[k − ℓ] c[n − k] (491)
k ℓ
| {z }
a[k ]∗b[k ]

= ∑ ∑ a[ℓ]b[k − ℓ]c[n − k] (492)


k ℓ

For the other ordering:


 
a[n] ∗ (b[n] ∗ c[n]) = ∑ a[ℓ] ∑ b[m]c[(n − m) −ℓ] (493)
ℓ m
| {z }
b[n]∗c[n]

= ∑ ∑ a[ℓ]b[m]c[(n − m) − ℓ] . (494)
ℓ m

If we now substitute: m = k − ℓ, then n − m − ℓ = n − k, which gives


! x [n] x [n]

a[n] ∗ (b[n] ∗ c[n]) = ∑ ∑ a[ℓ]b[k − ℓ] c[n − k] , (495)


k ℓ LTI (h1 [n]) LTI (h3 [n])
| {z }
a[k ]∗b[k ]
LTI (h2 [n]) y[n]
which is the same as ( a[n] ∗ b[n]) ∗ c[n], which completes the proof.

A consequence of the associative property is that if we have two y[n]


LTI systems that are applied to an input signal x [n] in series, we can
Figure 103: A consequence of the
come up with a single LTI system that is equivalent to two chained associative property of convolution
LTI systems: is that two LTI systems characterized
with h1 [n] and h2 [n] can be combined
as a single LTI system with impulse
y[n] = ( x [n] ∗ h1 [n]) ∗ h2 [n] = x [n] ∗ h3 [n] . (496)
response h3 [n] = h1 [n] ∗ h2 [n].

Here h3 [n] = h1 [n] ∗ h2 [n]. This is depicted in Figure 103. This


property can be extended to an arbitrary number of systems that are x1 [ n ] x2 [ n ] x1 [ n ] + x2 [ n ]
connected together in series.
LTI (h[n]) LTI (h[n]) LTI (h[n])
Distributive
The convolution is distributive: + y[n]

a[n] ∗ (b[n] + c[n]) = a[n] ∗ b[n] + a[n] ∗ c[n] . (497) y[n]

Proof. Figure 104: A consequence of the


∞ distributive property is linearity.
a[n] ∗ (b[n] + c[n]) = ∑ a[k](b[n − k] + c[n − k ]) (498)
k =−∞
∞ ∞
= ∑ a[k]b[n − k] + ∑ a[k]c[n − k] . (499)
k =−∞ k =−∞
171

An example application of this property is shown in Figure 104.


Signals x1 [n] and x2 [n] fed into an identical LTI systems separately
and then added together is equivalent to the sum of the signals fed
into a single LTI system.

Example: Convolution animations

Animations of the convolution operation for various example systems


can be found here: http://kaira.uit.no/juha/fir_animation/. The
code for creating such animations can be obtained from GitHub:
https://github.com/jvierine/signal_processing/tree/master/
017_fir_animation.

Applications of convolution

This chapter only scratches the practical and theoretical uses of con-
volution and linear time-invariant systems. I’ll use two examples to
illustrate the application of a one-dimensional convolution equation.
Be aware that these are by no means the only types of situations
where you will encounter a convolution. Chances are quite high that
a signal processing system you can think of is an LTI system, or can
be approximated as one. Convolution can be found everywhere!

Example: Radar and sonar equation


The convolution equation is often used to model radar and sonar
measurements. In this case, the impulse response signal h[n] repre-
sents the signal that is scattered as a function of distance. The signal
Figure 105: EISCAT Svalbard Radar.
x [n] represents what a radar or sonar transmits. The signal received
The radar echo from the D-region of the
by a radar or sonar receiver m[n] is then modeled as: ionosphere can be modeled using the
equation shown in Equation 501. Photo:
m[n] = h[n] ∗ x [n] (500) Craig Heinselman
M
= ∑ h [r ] x [ n − r ]. . (501)
r =0
In this case, the index r ∈ N represents round-trip propagation time
between the transmitter and the receiver. The larger the distance
between the transmitter and the receiver, the larger the delay. Assum-
ing that the sample rate is f s in units of hertz or ( 1s ), and the group
velocity of the transmitter wave v g ( ms ), the round-trip range is given
by:
vg r
R= . (502)
2 fs
In the case of radar, the group velocity for electromagnetic waves is
v g ≈ 3 · 108 ( ms ). For sonar, it is the speed of acoustic waves within the
medium. In the case of air, this is v g ≈ 343 ( ms ).
172

To see how the convolution equation is the radar equation, we can


use an illustration, as shown in Figure 106.

Figure 106: A range-time diagram


depicting the relationship between
r
a transmitted signal and a scattered
Range (samples)

signal.
··· ··· ···
Propagation velocity (r = n)
h[5] h[5] h[5] ···

h[4] h[4] h[4] ···

h[3] h[3] h[3] ···

h[2] h[2] h[2] ···

h[1] h[1] h[1] ···

h[0] h[0] h[0] ···

x[0] x[1] x[2] x[3] x[4] ··· Time (samples) n

m[0] m[1] m[2] m[3] m[4] ···


PM
m[n] = r=0 h[r]x[n − r]

If the probing signal is a unit impulse x [n] = δ[n] (a very short


radar pulse), then the measurement directly provides the scattering
amplitude as a function of range:
M
m[n] = ∑ h [r ] δ [ n − r ] = h [r ] . (503)
r =0
This type of radar is called a pulsed radar. In terms of radar signal
processing, this is the easiest case, as no signal processing is needed!
A radar measurement in this case is equivalent to measuring the
“impulse response” of the region that the radar is probing.
Here is a fascinating example of a blind child that has learned to
click his tongue (emitting a δ[n]-like impulse sound) to probe the
acoustic scattering of his surroundings https://www.youtube.com/
watch?v=fnH7AIwhpik. While this may seem like a superhuman feat,
this is not unlike measuring the distance between yourself and a
mountain side by shouting “echo” very loudly and counting how
long it takes for you to hear a strong echo. I am sure that many of
you have done this already. Figure 107: The acoustics of a space are
If the transmitted waveform x [n] is more complicated than a determined by acoustic waves scattered
from various obstacles at various
unit impulse, the measurements need to be filtered in some way to propagation delays. This can be quite
reconstruct the scattered signal as a function of range h[n]. This is precisely modeled using a convolution,
achieved by designing a filter λ[n], which has the following property assuming that nothing is moving.

λ[n] ∗ x [n] ≈ δ[n]. After applying the filter λ[n] to the measured signal,
one obtains the echo as a function of range:
λ[n] ∗ m[n] = λ[n] ∗ h[n] ∗ x [n] = (λ[n] ∗ x [n]) ∗ h[n] ≈ h[n] . (504)
173

Design of pairs of signals x [n] and filters λ[n] is a topic of radar


signal processing. An example of a good pairing of a radar transmit
signal and a receiver filter is shown in the following convolution
animation: http://kaira.uit.no/juha/fir_animation/ex12.gif.
This is the so-called 13-bit Barker code and the inverse filter that
corresponds to it. The convolution of these two signals is the unit
impulse.

Example: Reverb effect

Convolution is encountered when modeling the acoustics of a room


or a space in audio signal processing. The basic underlying principle
is the same as for radar and sonar.
A sound source x [n] is reflected or scattered from different sur-
faces within the room at various delays r. Each delay r corresponds
to the round-trip propagation delay of the acoustic signal. This is
what creates the “sound” of a room. This is also what is the cause
for an acoustic “echo” from a mountain side that you may have
experienced in real life!
How much signal is scattered from range r is determined by
the impulse response h[r ]. The resulting signal is a convolution Impulse response
1.5
m[n] = h[n] ∗ x [n]. In this case, performing the convolution results in a
1.0
reverb effect in acoustic signal processing.
0.5
By accurately modeling the impulse response of a room or a large h[n] 0.0
concert hall with an impulse response h[n], it is possible to modify an
0.5
audio signal x [n] to sound like it was played in that space.
1.0
One way to characterize h[n] for a room is to simply measure it.
0.0 0.5 1.0 1.5 2.0 2.5 3.0
One emits a very short discrete unit impulse δ[n] using a high quality Time (s)

speaker, and then measures the echoes for this pulse to obtain:
Figure 108: An impulse response that
models the acoustics of a large space,
M with echoes at up to 3 seconds time
h[n] = ∑ h [r ] δ [ n − r ] . (505) delay.
r =0

Another possibility is to model the impulse response h[n] by making


assumptions about the delays of the reflecting surfaces in a room or
space.
The following snippet of code demonstrates modeling of the
acoustics of a room using a modeled impulse response for a room.
# Reverb e f f e c t f o r sound
#
import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . i o . w a v f i l e as sw
import s c i p y . s i g n a l as s s
174

def reverb_model ( room_length_std = 3 0 . 0 , # Room wall t o wall


d i s t a n c e standard d e v i a t i o n i n meters .
echo_magnitude = 0 . 5 , # How much i s r e f l e c t e d
from a wall .
n_echoes =10 , # Number o f echoes .
n_walls =10 , # Number o f s c a t t e r i n g
surfaces .
s r =44100 , # Sample− r a t e .
c_sound = 3 4 0 . 0 ) : # Speed o f sound (m/s ) .
"""
S i m p l i f i e d model o f t h e a c o u s t i c s o f a room .
Model t h e r e f l e c t i o n s from r e v e r b e r a t i o n s between s u r f a c e s
i n a room .
"""
e ch o _l e n = i n t ( s r * 2 * room_length_std * n_echoes/c_sound )
h = np . z e r o s ( echo_len , dtype=np . f l o a t 3 2 )
h[0] = 1.0

f o r _ i n range ( n_walls ) :
w a l l _ d i s t = np . abs ( np . random . rand ( ) * room_length_std )
f o r i i n range ( n_echoes ) :
idx = i n t ( s r * ( i +1) * w a l l _ d i s t /c_sound )
i f idx < ec h o_ l en :
h [ idx ] = np . random . rand ( ) * echo_magnitude * * ( i
+1.0)

return h

# Read audio f i l e ( . wav format ) .


t s = sw . read ( " 7na . wav" )
sr , c l i p = t s
i f l e n ( c l i p . shape ) == 2 : # I f s t e r e o , only use one channel .
p r i n t ( " Using only one s t e r e o channel . Read on . " )
clip = clip [ : , 0]

# Make s ur e t h e c l i p i s f l o a t 3 2 .
c l i p = np . a r r a y ( c l i p , dtype=np . f l o a t 3 2 )

# This i s t h e impulse response t h a t determines


# t h e a c o u s t i c s o f a room .
h = reverb_model ( room_length_std = 1 5 . 0 , n_walls =100 ,
echo_magnitude = 0 . 5 )

omhat = np . l i n s p a c e ( −np . pi , np . pi , num=10000)


H = np . z e r o s ( l e n ( omhat ) , dtype=np . complex64 )

f o r i i n range ( l e n ( h ) ) :
H += h [ i ] * np . exp ( 1 j * omhat * i )

p l t . p l o t ( omhat , np . abs (H) * * 2 . 0 )


p l t . show ( )

# P l o t t h e impulse response
# i f t h e impulse response i s s h o r t , use stem p l o t
# o t h e r w i s e use normal p l o t ( i t i s much f a s t e r ) .
time_vec = np . arange ( l e n ( h ) ) / f l o a t ( s r )

i f len (h) < 100:


175

p l t . stem ( time_vec , h )
else :
p l t . p l o t ( time_vec , h )

plt . xlim ( [ − 0 . 1 * np . max ( time_vec ) , 1 . 1 * np . max ( time_vec ) ] )


plt . t i t l e ( " Impulse response " )
plt . x l a b e l ( " Time ( s ) " )
plt . ylabel ( "h[n] " )
plt . show ( )

# P l o t t h e audio .
time_vec = np . arange ( l e n ( c l i p ) )
t i d x = np . arange ( 0 , i n t ( np . min ( [ 4 4 1 0 0 , l e n ( c l i p ) ] ) ) )
p l t . subplot (211)
p l t . p l o t ( time_vec [ t i d x ] , c l i p [ t i d x ] )
p l t . t i t l e ( " O r i g i n a l audio " )
p l t . x l a b e l ( " Time ( s ) " )
p l t . y l a b e l ( " Amplitude " )

# Convolve t h e c l i p with t h e impulse response


# s c i p y . s i g n a l . convolve .
e c h o _ c l i p = s s . convolve ( c l i p , h , mode= " f u l l " )

# P l o t t h e audio .
p l t . subplot (212)
p l t . t i t l e ( " Convolution output " )
# S c a l e numbers t o 0 . . 1 s c a l e .
p l t . p l o t ( time_vec [ t i d x ] , e c h o _ c l i p [ t i d x ] )
p l t . x l a b e l ( " Time ( s ) " )
p l t . y l a b e l ( " Amplitude " )
plt . tight_layout ()
p l t . show ( )

# Normalize t o u n i t y .
e c h o _ c l i p = e c h o _ c l i p /(np . max ( np . abs ( e c h o _ c l i p ) ) )

p r i n t ( " Saving r e v e r b . wav" )


# Save as . wav f i l e with 4 4 . 1 kHz sample r a t e .
sw . w r i t e ( " r e v e r b . wav" , sr , np . a r r a y ( 2 0 e3 * e c h o _ c l i p , dtype=np .
int16 ) )

Listing 28: 018_reverb/reverb.py


176

Exercises: Linear Time-invariant Systems

1. Prove that the convolution operation is commutative. That is,


show that a[n] ∗ b[n] = b[n] ∗ a[n]. In addition, show that this holds
true for the continuous-time convolution operation.

2. A running average system is defined as:

L −1
1
y[n] = T { x [n]} =
L ∑ x [n − k] (506)
k =0

a) Show that the running average system is a linear time invariant


system using the test for linearity and time invariance.
b) What is the impulse response h[n] = T {δ[n]} of the running
average system?
c) How many non-zero values does the impulse response h[n]
have?
d) We feed a discrete-time complex sinusoidal signal x [n] = eiω̂0 n
into the system. Show that the output is of the form y[n] =
Aeiϕ eiω̂0 n , in other words, a discrete-time complex sinusoidal
signal with the same frequency as the input signal.
e) Continue with task d). Let us assume that L = 4. What is the
amplitude of the output signal A when we have a normalized
angular frequency of:
i. ω̂0 = 0 radians per sample?
ii. ω̂0 = π radians per sample?
iii. ω̂0 = 0.5π radians per sample?
f) Show that system y2 [n] = T {T { x [n]}} is also an LTI system.
g) What is the impulse response h2 [n] = T {T {δ[n]}}? Sketch a
plot of the non-zero values h2 [n].

3. Consider the following running average filter:

L −1
1
y[n] =
L ∑ x [ n − k ]. (507)
k =0

a) Let x [n] be the signal:





 1.2, n = 0,


4.3,


 n = 1,
x [n] = 4.7, n = 2,


3.3, n = 3,






2.9, n = 4.
177

Compute the resulting signal y[n] from using the filter in 507
by hand, using L = 3. Values of n for which n ∈
/ {0, 1, 2, 3, 4}
are treated as 0.
b) Implement a function running_average_filter(L, x) that
computes the running average filter for a given L and signal
x. Apply the filter on the signal x from the previous exercise
to verify that you get the same answer (up to some rounding
errors).
c) The code shown in Listing 29 makes plots of a noisy signal
along with the filtered signal, using the running average filter
from Equation 507. Add the function you wrote from the
previous exercise to the code, and verify that the noisy signal
has been smoothened by the filter. It should look similar to
Figure 97.
import sys

import m a t p l o t l i b . pyplot as p l t
import numpy as np

def r u n n i n g _ a v e r a g e _ f i l t e r ( L : i n t , x : np . ndarray ) −> np .


ndarray :
" " " Simple implementation o f a running average
filter . """

y = np . z e r o s _ l i k e ( x )

# TODO: implement me !

return y

i f __name__ == " __main__ " :

i f l e n ( sys . argv ) ! = 2 :
p r i n t ( f " Usage : L " )
e x i t ( −1)

L = i n t ( sys . argv [ 1 ] ) i f sys . argv [ 1 ] . i s d e c i m a l ( ) e l s e


None

i f L i s None :
p r i n t ( " You need t o s p e c i f y t h e L value as an
integer ! " )
e x i t ( −1)

N = 500 # Length o f t h e example s i g n a l s .


t = np . arange (N)

# Random n o i s e s i g n a l , w i l l change with each run o f t h e


program .
x = np . cumsum ( np . random . randn (N) )

# Apply t h e running average f i l t e r .


y = r u n n i n g _ a v e r a g e _ f i l t e r ( L=3 , x=x )
178

plt . p l o t ( t , x , l a b e l = " O r i g i n a l " , c o l o r = " red " )


plt . p l o t ( t , y , l a b e l = " F i l t e r " , c o l o r = " blue " )
plt . legend ( )
plt . show ( )

Listing 29: Given code for exercise 3c

d) Download 7na.wav from the course GitHub examples, https:


//github.com/jvierine/signal_processing/tree/master/
018_reverb. Apply the running average filter in Equation 507
to the audio using L = 4, 25, 50, 100. Can you detect with your
ear what happens to the low and high frequency components
of the signal as a result of the operation?

4. Obtain the demonstration code that implements a reverb effect


using a convolution operation:

y [ n ] = h [ n ] ∗ x [ n ], (508)

where x [n] is the input signal, h[n] is the impulse response of a


room, and y[n] is the output signal with the reverb effect. Down-
load 7na.wav from the course GitHub examples, https://github.
com/jvierine/signal_processing/tree/master/018_reverb or
alternatively use your own audio file.

a) Run the example code and verify that the filtered signal indeed
sounds like it is played in a large room by playing 7na.wav and
the file reverb.wav produced by the script.
b) Find the part in the code where a convolution between the
audio signal and the impulse response is evaluated. Plot the
impulse response of the FIR filter applied to the input signal.
c) Figure out how to increase and reduce the amount of reverb,
i.e., to make it sound like the audio signal is played in a large
or small room with many surfaces that reflect sound waves.
What does the impulse response for a large room and a small
room look like?
d) Now try to create an impulse response of an echo from a
distance of 1000 meters using the following impulse response
h[n] = δ[n] + 0.5δ[n − n0 ]. Assuming that the propagation
velocity of sound is 343 ms , determine a suitable value for n0 .
179

Suggested solutions: Linear Time-invariant Systems

1. Claim: a[n] ∗ b[n] = b[n] ∗ a[n].

Proof. By definition, the convolution of two discrete-time signals is


defined as:

a[n] ∗ b[n] = ∑ a [ k ] b [ n − k ].
k =−∞

Change variables by setting l = n − k, so that k = n − l, then:


∞ ∞
a[n] ∗ b[n] = ∑ a[n − l ]b[l ] = ∑ b [ l ] a [ n − l ].
l =−∞ l =−∞

The last sum is the definition of b[n] ∗ a[n], just with the sum index
named l, hence a[n] ∗ b[n] = b[n] ∗ a[n].

The same approach can be used to prove the commutativity of the


continuous-time convolution. Again, substitution of variables, but
with integrals. Here is the proof:

Proof.
Z ∞
a(t) ∗ b(t) = a(τ )b(t − τ )dτ,
−∞
Z −∞
=− a(t − u)b(u)du,

Z ∞
= b(u) a(t − u)du,
−∞
= b ( t ) ∗ a ( t ),

where u = t − τ, giving du = −dτ. Note that the minus sign is used


to swap the limits.

2. Consider the running average system, defined as:


L −1
1
y[n] = T { x [n]} =
L ∑ x [ n − k ].
k =0

a) Consider two discrete-time signals x1 [n] and x2 [n] with arbi-


trary constants c1 , c2 , then:
L −1
1
T {c1 x1 [n] + c2 x2 [n]} =
L ∑ [c1 x1 [n − j] + c2 x2 [n − j]],
j =0
L −1 L −1
1 1
c1 T { x1 [n]} + c2 T { x2 [n]} = c1
L ∑ x1 [ n − k ] + c2 L ∑ x2 [ n − l ],
k =0 l =0
180

which are equal.


For time-invariance we have:
L −1
1
T {D{ x [n]}} = T { x [n − τ ]} =
L ∑ x [ n − τ − k ],
k =0
( )
L −1 L −1
1 1
D{T { x [n]}} = D
L ∑ x [n − k] =
L ∑ x [ n − τ − k ],
k =0 k =0

both are equal, so the system is time-invariant.


b) The impulse response can be determined by h[n] = T {δ[n]},
doing this yields:
L −1
1
h[n] =
L ∑ δ [ n − k ].
k =0

The impulse response function is then:



 1 , n = 0, . . . , L − 1,
h[n] = L
0, otherwise.

c) The impulse response has L nonzero values, all of them being


1/L.
d) Let x [n] = eiω̂0 n , then if we feed this into our system we get
(using the formula for a geometric sum):

1 L−1 iω̂0 (n−k) 1 iω̂0 n L−1 −iω̂0 k 1 iω̂0 n 1 − (e−iω̂0 ) L


 

L k∑ ∑e
y[n] = e = e = e .
=0
L k =0
L 1 − e−iω̂0

The output signal can then be rewritten as:


!
1 e−iω̂0 L/2 eiω̂0 L/2 − e−iω̂0 L/2
y[n] = eiω̂0 n −iω̂ /2 ,
L e 0 eiω̂0 /2 − e−iω̂0 /2

then use the definition of sin(θ ) for complex values to obtain:


1 iω̂0 n −iω̂0 ( L−1)/2 sin(ω̂0 L/2)
y[n] = e e .
L sin(ω̂0 /2)
Later, we’ll introduce this as a filter that contains two parts, one
part being a Dirichlet kernel which dictates how the magnitude
of the frequencies are filtered, the other part is a time-delay,
which tells us how the filter delays the signal. We can see this
somewhat already, as we have:
 
1 −iω̂0 ( L−1)/2 sin(ω̂0 L/2)
y[n] = e x [ n ].
L sin(ω̂0 /2)
Here we see that:
1 sin(ω̂0 L/2)
DL (ω̂0 ) = ,
L sin(ω̂0 /2)
181

changes the magnitude, while:

e−iω̂0 ( L−1)/2

delays the signal.


e) Take L = 4, then:

1 −iω̂0 3/2 sin(2ω̂0 )


y[n] = e x [ n ].
4 sin(ω̂0 /2)

Next, consider the cases of input frequencies:

ω̂ = 0,
ω̂ = π,
ω̂ = 0.5π,

from our discussion above, we know that the amplitude is


dependent on the DL (ω̂0 ) function only. In these cases the
amplitude of the output is:

1 sin(2ω̂0 ) 1 2 cos(2ω̂0 )
A = lim = lim 1 = 1,
ω̂0 →0 4 sin(ω̂0 /2) ω̂0 →0 4 cos( ω̂0 /2)
2
1 sin(2π )
A= = 0,
4 sin(π/2)
1 sin(2π/2)
A= = 0.
4 sin(π/4)

We have used L’Hôpital’s rule to determine the behavior of


the filter at ω0 = 0, otherwise, just plug in the value for ω0 .
The filter seems to behave as a low-pass filter, passing low
frequencies and removing high frequencies.
f) The system y[n] = T { x [n]} is an LTI system and can be written
as y[n] = h[n] ∗ x [n], where h[n] is the impulse response function
as defined above. Then the system can be described as:

y2 [n] = T {T { x [n]}} = T {h[n] ∗ x [n]} = h[n] ∗ (h[n] ∗ x [n]) = (h[n] ∗ h[n]) ∗ x [n].

We’ve used associativity of convolution in the final step. Then


the system can be described as y2 [n] = h2 [n] ∗ x [n], where
h2 [n] = h[n] ∗ h[n]. Hence, y2 [n] is an LTI system, as all LTI
system are fully determined by convolution with an impulse
response.
g) Have that h2 [n] = T {T {δ[n]}} = h[n] ∗ h[n], so:

h2 [ n ] = ∑ h [ k ] h [ n − k ] = h [0] h [ n ] + h [1] h [ n − 1] + h [2] h [ n − 2] + h [3] h [ n − 3] + h [4] h [ n − 4].
k =−∞
182

Terms with n < 0 are dropped as these will be 0 due to h[n] =


0 for n < 0. Let’s evaluate the values of h2 [n] with L = 4:

1
h2 [0] = h[0]h[0] + h[1]h[−1] + h[2]h[−2] + h[3]h[−3] + h[4]h[−4] = ,
L2
2
h2 [1] = h[0]h[1] + h[1]h[0] + h[2]h[−1] + h[3]h[−2] + h[4][−3] = ,
L2
3
h2 [2] = h[0]h[2] + h[1]h[1] + h[2]h[0] + h[3]h[−1] + h[4]h[−2] = 2 ,
L
4
h2 [3] = h[0]h[3] + h[1]h[2] + h[2]h[1] + h[3]h[0] + h[4]h[−1] = 2 ,
L
3
h2 [4] = h [0] h [4] + h [1] h [3] + h [2] h [2] + h [3] h [1] + h [4] h [0] = 2 ,
L
2
h2 [5] = h [0] h [5] + h [1] h [4] + h [2] h [3] + h [3] h [2] + h [4] h [1] = 2 ,
L
1
h2 [6] = h [0] h [6] + h [1] h [5] + h [2] h [4] + h [3] h [3] + h [4] h [2] = 2 ,
L

while the rest are 0. Thus:



1


 16 , n = 0, 6,

 2
 16 , n = 1, 5,



h2 [n] = 163
, n = 2, 4,

4

16 , n = 3,






0, otherwise.

To draw the impulse response function, we write a little pro-


gram to do it. The program is shown in Listing 30. The impulse
response is 0 for all values of n not shown.

import m a t p l o t l i b . pyplot as p l t
import numpy as np

L = 4
h = np . r e p e a t (1/L , L ) # Add 1/L i n t o an a r r a y L t im es .

# Compute t h e c o n v o l u t i o n .
h2 = np . convolve ( h , h , mode= " f u l l " )

p l t . stem ( h2 )
p l t . x l a b e l ( " Samples $ ( n ) $ " )
p l t . y l a b e l ( " $h_ { 2 } [ n ] $ " )
p l t . t i t l e ( " Impulse response " )
# C a l l t h i s i f needed .

Listing 30: Simple convolution

The output of the program is shown in Figure 109.

3. a) The output signal y[n] will have the same length as x [n], thus,

Figure 109: Impulse response for y2 [n]


183

computing each value is done as follows:

1
y [0] = ( x [0 − 0] + x [0 − 1] + x [0 − 2]),
1
1
y [1] = ( x [1 − 0] + x [1 − 1] + x [1 − 2]),
2
1
y [2] = ( x [2 − 0] + x [2 − 1] + x [2 − 2]),
3
1
y [3] = ( x [3 − 0] + x [3 − 1] + x [3 − 2]),
3
1
y [4] = ( x [4 − 0] + x [4 − 1] + x [4 − 2]),
3
Note that the values of factor in front varies. The reason is
that the operation is to average, so we must divide by the total
number of terms that are counted. Remove all the negative
index terms (which are treated as 0), we get:

1
y [0] = ( x [0 − 0]) = x [0] = 1.2,
1
1 1
y [1] = ( x [1 − 0] + x [1 − 1]) = ( x [1] + x [0]) = 2.75,
2 2
1 1
y [2] = ( x [2 − 0] + x [2 − 1] + x [2 − 2]) = ( x [2] + x [1] + x [0]) = 3.40,
3 3
1 1
y [3] = ( x [3 − 0] + x [3 − 1] + x [3 − 2]) = ( x [3] + x [2] + x [1]) = 4.10,
3 3
1 1
y [4] = ( x [4 − 0] + x [4 − 1] + x [4 − 2]) = ( x [4] + x [3] + x [2]) = 3.63,
3 3
so the filtered signal y[n] is then:



 1.2, n = 0,


2.75, n = 1,



y[n] = 3.40, n = 2,


4.10, n = 3,





3.63, n = 4.

b) Listing 31 shows a Python implementation that implements the


running average filter from Equation 507. Running this code on
the signal x [n] gives the same result as the computation done in
the previous exercise.
import numpy as np

def r u n n i n g _ a v e r a g e _ f i l t e r ( L : i n t , x : np . ndarray ) −> np .


ndarray :
" " " Simple implementation o f a running average
filter . """

y = np . z e r o s _ l i k e ( x )
184

f o r n i n range ( l e n ( y ) ) :

# Determine i f t h e index i s n e g a t i v e .
# I f yes , p i c k 0 as t h e s t a r t i n g point , o t h e r w i s e
# use n − L + 1 . Add 1 t o avoid t h e n = L c a s e .
s t a r t = max ( 0 , n − L + 1 )

# Use +1 s i n c e n i s never a c t u a l l y t h e f i n a l index


of y .
y [ n ] = np . mean ( x [ s t a r t : n + 1 ] )

return y

i f __name__ == " __main__ " :

x = np . a r r a y ( [ 1 . 2 , 4 . 3 , 4 . 7 , 3 . 3 , 2 . 9 ] , dtype=np .
float32 )

print ( running_average_filter (3 , x ) )

Listing 31: Solution for exercise 3b

c) Listing 32 shows the previous function added to the code from


Listing 29
import sys

import m a t p l o t l i b . pyplot as p l t
import numpy as np

def r u n n i n g _ a v e r a g e _ f i l t e r ( L : i n t , x : np . ndarray ) −> np .


ndarray :
" " " Simple implementation o f a running average
filter . """

y = np . z e r o s _ l i k e ( x )

f o r n i n range ( l e n ( y ) ) :

# Determine i f t h e index i s n e g a t i v e .
# I f yes , p i c k 0 as t h e s t a r t i n g point , o t h e r w i s e
# use n − L + 1 . Add 1 t o avoid t h e n = L c a s e .
s t a r t = max ( 0 , n − L + 1 )

y [ n ] = np . mean ( x [ s t a r t : n + 1 ] ) # Use +1 s i n c e n i s
never a c t u a l l y t h e f i n a l index o f y .

return y

i f __name__ == " __main__ " :

i f l e n ( sys . argv ) ! = 2 :
p r i n t ( " Usage : <L> " )
exit (1)

L = i n t ( sys . argv [ 1 ] ) i f sys . argv [ 1 ] . i s d e c i m a l ( ) e l s e


None
185

i f L i s None :
p r i n t ( " L should be an i n t e g e r ! " )
exit (1)

N = 500 # Length o f t h e example s i g n a l s .


t = np . arange (N)

# Random n o i s e s i g n a l , w i l l change with each run o f t h e


program .
x = np . cumsum ( np . random . randn (N) )

# Apply t h e running average f i l t e r .


y = r u n n i n g _ a v e r a g e _ f i l t e r ( L=L , x=x )

p l t . p l o t ( t , x , l a b e l = " O r i g i n a l " , c o l o r = " red " )


p l t . p l o t ( t , y , l a b e l = " Running average f i l t e r e d " , c o l o r =
" blue " )
p l t . x l a b e l ( " Samples $n$ " )
p l t . legend ( )
# # p l t . show ( ) # C a l l t h i s i f needed .

Listing 32: Solution for exercise 3c

The plot generated is shown in Figure 110. Note that the plot is
random, so yours will look different when the code is run.
d) The code in Listing 33 implements the running average filter
and applies the running average filter to the input file. The
code is written so that the user can run the code by providing
the filename and L on the command line.
Figure 110: Noisy signal that has been
import sys smoothened with a running average
filter using L = 25.
import numpy as np
from s c i p y . i o . w a v f i l e import read , w r i t e

def r e a d _ w a v _ f i l e ( f i l e n a m e : s t r ) −> t u p l e [ f l o a t , np . ndarray


]:
" " " Function t o read a . wav f i l e . Returns t h e sample
r a t e and a s i n g l e channel with t h e audio .
"""

# Read t h e f i l e , then e x t r a c t t h e n e c e s s a r y i n f o r m a t i o n
.
w a v _ f i l e = read ( f i l e n a m e )
sr , s i g n a l = w a v _ f i l e

# Check i f t h e audio i s s t e r e o or mono .


i f l e n ( s i g n a l . shape ) == 2 :
# Use only 1 channel i f s t e r e o .
signal = signal [ : , 0]

r e t u r n sr , s i g n a l

def w r i t e _ w a v _ f i l e ( f i l e n a m e : s t r , s r : f l o a t , s i g n a l : np .
ndarray ) −> None :
" " " Function t o w r i t e t o a . wav f i l e . Arguments c o n s i s t s
186

o f t h e f i l e n a m e t o w r i t e to , t h e sample r a t e and t h e
actual
data t o w r i t e i n t o t h e f i l e . " " "

# Use t h e f l o a t i n g p o i n t f i l e format f o r . wav f i l e s .


# This r e q u i r e s t h e data t o be cropped between −1 and
1.
# c l i p p e d _ s i g n a l = np . c l i p ( s i g n a l , a_min = −1.0 , a_max
= 1 . 0 , dtype=np . f l o a t 3 2 )
c l i p p e d _ s i g n a l = 0 . 9 * s i g n a l /np . max ( np . abs ( s i g n a l ) )

w r i t e ( filename , sr , c l i p p e d _ s i g n a l . a s t y p e ( np . f l o a t 3 2 ) )

def r u n n i n g _ a v e r a g e _ f i l t e r ( L : i n t , x : np . ndarray ) −> np .


ndarray :
" " " Simple implementation o f a running average
filter . """

y = np . z e r o s _ l i k e ( x )

f o r n i n range ( l e n ( y ) ) :
s t a r t = max ( 0 , n − L + 1 )

y [ n ] = np . mean ( x [ s t a r t : n + 1 ] )

return y

i f __name__ == " __main__ " :

i f l e n ( sys . argv ) ! = 3 :
p r i n t ( " Usage : <filename > <L> " )
exit (1)

_ , filename , l = sys . argv


p r i n t ( f " F i l t e r i n g f i l e : ’ { f i l e n a m e } ’ using L = { l } " )

# V e r i f y t h a t t h e in pu t f o r t h e program i s okay .
L = i n t ( l ) i f l . i s d e c i m a l ( ) e l s e None

i f L i s None :
p r i n t ( " I n v a l i d s i z e o f running average f i l t e r . " )
exit (1)

# Read t h e f i l e from d i s k .
sr , s i g n a l = r e a d _ w a v _ f i l e ( f i l e n a m e )

# F i l t e r t h e audio s i g n a l using a running average


# low−pass f i l t e r with t h e u s er s p e c i f i e d L .
filtered_signal = running_average_filter (L, signal )

# Write t h e r e s u l t t o d i s k .
w r i t e _ w a v _ f i l e ( f " { f i l e n a m e . r e p l a c e ( ’ . wav ’ , ’ ’) } _ {L}
_ f i l t e r e d . wav" , sr , f i l t e r e d _ s i g n a l )

Listing 33: Suggested solution for exercise 3d

The effect on the audio can be heard between the original and
for large L. The higher frequencies have been reduced, so they
187

are less prevalent. This is not surprising, as the filter we are


using is a low-pass filter.

4. The code for this exercise is based on the reverb demonstration


code.

a) By running reverb.py and listening to reverb.wav, one can


hear the reverb effect having been applied to the audio-signal.
b) Running the code in Listing 34 yields the figure shown in Fig-
ure 111. Each peak shown represents an impulse that has been
reflected by some wall, except the first peak, which represents
the original signal.
import sys

import m a t p l o t l i b . pyplot as p l t
import numpy as np
from s c i p y . i o . w a v f i l e import read , w r i t e
Figure 111: Impulse response for the
# This code i s based on t h e r e v e r b example . reverb LTI system.

def r e a d _ w a v _ f i l e ( f i l e n a m e : s t r ) −> t u p l e [ i n t , np . ndarray ] :


" " " Function t o read a . wav f i l e . Returns t h e sample
r a t e and a s i n g l e channel with t h e audio .
"""

# Read t h e f i l e , then e x t r a c t t h e n e c e s s a r y i n f o r m a t i o n
.
w a v _ f i l e = read ( f i l e n a m e )
sr , s i g n a l = w a v _ f i l e

# Check i f t h e audio i s s t e r e o or mono .


i f l e n ( s i g n a l . shape ) == 2 :
# Use only 1 channel i f s t e r e o .
signal = signal [ : , 0]

r e t u r n sr , s i g n a l

def w r i t e _ w a v _ f i l e ( f i l e n a m e : s t r , s r : f l o a t , s i g n a l : np .
ndarray ) −> None :
" " " Function t o w r i t e t o a . wav f i l e . Arguments c o n s i s t s
o f t h e f i l e n a m e t o w r i t e to , t h e sample r a t e and
t h e a c t u a l data t o w r i t e i n t o t h e f i l e . " " "

# Use t h e f l o a t i n g p o i n t f i l e format f o r . wav f i l e s .


# This r e q u i r e s t h e data t o be cropped between −1 and
1.
# c l i p p e d _ s i g n a l = np . c l i p ( s i g n a l , a_min = −1.0 , a_max
= 1 . 0 , dtype=np . f l o a t 3 2 )
c l i p p e d _ s i g n a l = 0 . 9 * s i g n a l /np . max ( np . abs ( s i g n a l ) )

w r i t e ( filename , sr , c l i p p e d _ s i g n a l . a s t y p e ( np . f l o a t 3 2 ) )

def reverb_model ( room_length_std = 3 0 . 0 , # Room wall t o wall


d i s t a n c e standard d e v i a t i o n i n meters .
188

echo_magnitude = 0 . 5 , # How much i s


r e f l e c t e d from a wall .
n_echoes =10 , # Number o f echoes .
n_walls =10 , # Number o f
scattering surfaces .
s r =44100 , # Sample− r a t e .
c_sound = 3 4 0 . 0 ) : # Speed o f sound (m
/s ) .
" " " S i m p l i f i e d model o f t h e a c o u s t i c s o f a room .
Model t h e r e f l e c t i o n s from r e v e r b e r a t i o n s between
s u r f a c e s i n a room .
"""

e ch o _l e n = i n t ( 2 * s r * room_length_std * n_echoes/c_sound )
h = np . z e r o s ( echo_len , dtype=np . f l o a t 3 2 )
h [ 0 ] = 1 . 0 # This r e p r e s e n t s t h e i n i t i a l impulse ( t h e
original signal ) .

# Add each echo f o r t h e given number o f w a l l s .


f o r _ i n range ( n_walls ) :
# Generate random wall d i s t a n c e .
w a l l _ d i s t = np . abs ( np . random . rand ( ) * room_length_std
)

f o r i i n range ( n_echoes ) :
idx = i n t ( s r * ( i + 1 ) * w a l l _ d i s t /c_sound )

i f idx < ec h o_ l en :
# Echoes decay . This model makes each echo
decay as x ^( i + 1 ) , where x < 1
# and i i s t h e index f o r t h e i t h echo .
h [ idx ] = np . random . rand ( ) * echo_magnitude * * (
i + 1.0)

return h

i f __name__ == " __main__ " :

room_length_std = 7 5 . 0 # Standard d e v i a t i o n f o r t h e
room l e n g t h .
echo_magnitude = 0 . 5 # Must be l e s s than 1 .
n_echoes = 10 # Must be g r e a t e r than 0 .
n_walls = 10 # Number o f w a l l s f o r t h e echo t o bounce
off .
c_sound = 3 4 0 . 0 # Speed o f sound .

i f l e n ( sys . argv ) ! = 2 :
p r i n t ( " Usage : <filename > " )
exit (1)

_ , f i l e n a m e = sys . argv

p r i n t ( f " Reading . wav f i l e named { f i l e n a m e } " )


sr , x = r e a d _ w a v _ f i l e ( f i l e n a m e )

p r i n t ( " Computing t h e r e v e r b model . . . " )


h = reverb_model ( room_length_std , echo_magnitude ,
n_echoes , n_walls , sr , c_sound )

# Convolution happens here . The impulse response i s


189

convolved with
# t h e audio s i g n a l ( c a l l e d x here ) , t o s p i t out t h e
reverb e f f e c t s i g n a l .
p r i n t ( " Computing t h e c o n v o l u t i o n . . . " )
y = np . convolve ( x , h , mode= " f u l l " )

p r i n t ( " Saving r e v e r b . wav f i l e . " )


w r i t e _ w a v _ f i l e ( " r e v e r b . wav" , sr , y )

# P l o t t h e impulse response .
p l t . p l o t ( h , c o l o r = " darkblue " )
p l t . x l a b e l ( " Index [ n ] " )
p l t . y l a b e l ( " $h [ n ] $ " )
p l t . t i t l e ( r f " Impulse response f o r r e v e r b system . " )
plt . text (9.0 , 0.7 ,
rf """
Room l e n g t h $\sigma$ = { room_length_std } m
Echo magnitude : { echo_magnitude }
Echos : { n_echoes }
Walls : { n_walls }
Speed o f sound : { c_sound } m/s
""" )
# p l t . show ( ) # I f needed

Listing 34: Suggested solution for exercise 4b

c) By changing room_length_std one can increase the reverb


effect. Making this parameter larger means the sound will
travel further and reflect at a later time. This gives the effect
of a larger room. The impulse responses are similar, but the
impulse response for a larger room has more peaks at higher
indices. This makes sense, as the smaller room will not have
any reflections on these indices as they do not exist for the
small room. That is, there are no walls to reflect off of at such
distances. Running Listing 35 gives Figure 112, where we can
see that the larger room has more peaks at higher indices than
the smaller room. You can also see that the smaller room has
more peaks at lower indices, which is as expected as these
reflections come earlier, due to the signal having less travel
distance, than in the large room.

import numpy as np
import m a t p l o t l i b . pyplot as p l t

def reverb_model ( room_length_std = 3 0 . 0 , # Room wall t o wall Figure 112: Impulse response for the
d i s t a n c e standard d e v i a t i o n i n meters . reverb LTI system.
echo_magnitude = 0 . 5 , # How much i s
r e f l e c t e d from a wall .
n_echoes =10 , # Number o f echoes .
n_walls =10 , # Number o f
scattering surfaces .
s r =44100 , # Sample− r a t e .
c_sound = 3 4 0 . 0 ) : # Speed o f sound (m
/s ) .
190

" " " S i m p l i f i e d model o f t h e a c o u s t i c s o f a room .


Model t h e r e f l e c t i o n s from r e v e r b e r a t i o n s between
s u r f a c e s i n a room .
"""

e ch o _l e n = i n t ( 2 * s r * room_length_std * n_echoes/c_sound )
h = np . z e r o s ( echo_len , dtype=np . f l o a t 3 2 )
h [ 0 ] = 1 . 0 # This r e p r e s e n t s t h e i n i t i a l impulse ( t h e
original signal ) .

# Add each echo f o r t h e given number o f w a l l s .


f o r _ i n range ( n_walls ) :
# Generate random wall d i s t a n c e .
w a l l _ d i s t = np . abs ( np . random . rand ( ) * room_length_std
)

f o r i i n range ( n_echoes ) :
idx = i n t ( s r * ( i + 1 ) * w a l l _ d i s t /c_sound )

i f idx < ec h o_ l en :
# Echoes decay . This model makes each echo
decay as x ^( i + 1 ) , where x < 1
# and i i s t h e index f o r t h e i t h echo .
h [ idx ] = np . random . rand ( ) * echo_magnitude * * (
i + 1.0)

return h

h1 = reverb_model ( 2 . 0 , 0 . 5 , 1 0 , 1 0 , 4 4 1 0 0 , 3 4 0 )
h2 = reverb_model ( 7 5 . 0 , 0 . 5 , 1 0 , 1 0 , 4 4 1 0 0 , 3 4 0 )

# h1 w i l l be s h o r t e r than h2 when t h e room i s l a r g e r .


l e n g t h = min ( l e n ( h1 ) , l e n ( h2 ) )

t = np . arange ( l e n g t h )

p l t . p l o t ( t , h1 [ : l e n g t h ] , l a b e l = " Small room " , c o l o r = " blue " )


p l t . p l o t ( t , h2 [ : l e n g t h ] , l a b e l = " Large room " , c o l o r = " red " )
p l t . x l a b e l ( " Samples $n$ " )
p l t . legend ( )
p l t . t i t l e ( " Impulse response comparison . " )
# p l t . show ( ) # Run i f needed .

Listing 35: Suggested solution for exercise 4b

d) The impulse response h[n] = δ[n] + 0.5δ[n − n0 ] represents a


system where the original signal is unchanged, this can be seen
from the δ[n] term. In addition, there is the 0.5δ[n − n0 ] term,
which represents the original signal being delayed and reduced
by a factor of 0.5 in amplitude. We want this to represent a
delay of an echo from a 1000 meters distance. To do this, we
must find n0 so that the distance traveled by the echo is 1000
meters. Assuming a constant velocity for the audio signal, the
total time it takes for the signal to travel, bounce off a wall and
come back is then:
2s
t=
v
191

where s = 1000 m and v = 343 m/s (the assumed speed of


sound). The relation between time in seconds and samples for
a discretized signal is then:

t = Ts n.

Solving for n0 , we obtain:

2s 2s f s
n0 = = ,
Ts v v
where f s is the sample rate. Plugging in the numbers yields:

2s f s (2)(1000 m)(44100) Hz
n0 = = ≈ 257142.85,
v 343 m/s
or n0 = 257143 to round to the nearest integer. The implemen-
tation of this reverb effect is shown in Listing 36. The delay
should occur at

t = Ts n = n/ f s = 257143/44100 Hz = 5.83 seconds.

If you listen to the audio produced by the code, you should


be able to hear the delay coming in at around 5.83 seconds, as
hoped.
import sys

import numpy as np
from s c i p y . i o . w a v f i l e import read , w r i t e

# This code i s based on t h e r e v e r b example .

def r e a d _ w a v _ f i l e ( f i l e n a m e : s t r ) −> t u p l e [ i n t , np . ndarray ] :


" " " Function t o read a . wav f i l e . Returns t h e sample
r a t e and a s i n g l e channel with t h e audio .
"""

# Read t h e f i l e , then e x t r a c t t h e n e c e s s a r y i n f o r m a t i o n
.
w a v _ f i l e = read ( f i l e n a m e )
sr , s i g n a l = w a v _ f i l e

# Check i f t h e audio i s s t e r e o or mono .


i f l e n ( s i g n a l . shape ) == 2 :
# Use only 1 channel i f s t e r e o .
signal = signal [ : , 0]

r e t u r n sr , s i g n a l

def w r i t e _ w a v _ f i l e ( f i l e n a m e : s t r , s r : f l o a t , s i g n a l : np .
ndarray ) −> None :
" " " Function t o w r i t e t o a . wav f i l e . Arguments c o n s i s t s
o f t h e f i l e n a m e t o w r i t e to , t h e sample r a t e and
t h e a c t u a l data t o w r i t e i n t o t h e f i l e . " " "
192

# Use t h e f l o a t i n g p o i n t f i l e format f o r . wav f i l e s .


# This r e q u i r e s t h e data t o be cropped between −1 and
1.
# c l i p p e d _ s i g n a l = np . c l i p ( s i g n a l , a_min = −1.0 , a_max
= 1 . 0 , dtype=np . f l o a t 3 2 )
c l i p p e d _ s i g n a l = 0 . 9 * s i g n a l /np . max ( np . abs ( s i g n a l ) )

w r i t e ( filename , sr , c l i p p e d _ s i g n a l . a s t y p e ( np . f l o a t 3 2 ) )

def reverb_1000_meters_model ( room_length_std = 1 0 0 0 . 0 , #


Room wall t o wall d i s t a n c e standard d e v i a t i o n i n meters
.
echo_magnitude = 0 . 5 , # How
much i s r e f l e c t e d from a wall .
s r =44100 , #
Sample− r a t e .
c_sound = 3 4 3 . 0 ) : #
Speed o f sound (m/s ) .
" " " Implementation o f t h e d e l t a [ n ] + 0 . 5 d e l t a [ n − n0 ]
system .
"""

# Formula f o r computing n0 , s e e pdf f o r t h e computation


.
n0 = i n t ( ( 2 * s r * room_length_std ) /c_sound )
h = np . z e r o s ( 2 * n0 , dtype=np . f l o a t 3 2 )

h [ 0 ] = 1 . 0 # This r e p r e s e n t s t h e i n i t i a l impulse ( t h e
original signal ) .

# This i s t h e r e f l e c t i o n coming from a wall a t a 1000


meter d i s t a n c e .
# Note t h a t t h e amplitude i s halved .
h [ n0 ] = echo_magnitude

return h

i f __name__ == " __main__ " :

room_length_std = 1 0 0 0 . 0 # Standard d e v i a t i o n f o r t h e
room l e n g t h .
echo_magnitude = 0 . 5 # Must be l e s s than 1 .
c_sound = 3 4 3 . 0 # Speed o f sound .

i f l e n ( sys . argv ) ! = 2 :
p r i n t ( " Usage : <filename > " )
exit (1)

_ , f i l e n a m e = sys . argv

p r i n t ( f " Reading . wav f i l e named { f i l e n a m e } " )


sr , x = r e a d _ w a v _ f i l e ( f i l e n a m e )

p r i n t ( " Computing t h e r e v e r b model . . . " )


h = reverb_1000_meters_model ( room_length_std ,
echo_magnitude , sr , c_sound )

# Convolution happens here . The impulse response i s


193

convolved with
# t h e audio s i g n a l ( c a l l e d x here ) , t o s p i t out t h e
reverb e f f e c t s i g n a l .
p r i n t ( " Computing t h e c o n v o l u t i o n . . . " )
y = np . convolve ( x , h , mode= " f u l l " )

p r i n t ( " Saving 1000 m_reverb . wav f i l e . " )


w r i t e _ w a v _ f i l e ( " 1000 m_reverb . wav" , sr , y )

Listing 36: Solution for exercise 4


Frequency Response

In this chapter, we will investigate the response of an LTI system to


a complex sinusoidal input. This investigation leads to the concept
of frequency response of an LTI system. The frequency response al- x (t) = Aeiϕ eiωt y(t) = H(ω ) x (t)
LTI
lows you to determine how an LTI system modifies each frequency h(t)
component of a signal fed into the system.
Every LTI system T {·} can be expressed as a convolution of an Figure 113: The frequency response of a
continuous-time LTI system is obtained
input signal x (t) with the impulse response h(t) = T {δ(t)} of the LTI by investigating what the system does
system: to a complex sinusoidal signal.

y(t) = T { x (t)} (509)


= x (t) ∗ h(t) (510)
Z ∞
= h(τ ) x (t − τ )dτ . (511)
−∞

If we feed a complex sinusoidal signal x (t) = Aeiϕ eiωt to an LTI


system, we get33 : 33
An eigenfunction x (t) of an operator
Z ∞ T {·} is defined as a function that has
the following property:
y(t) = h(τ ) x (t − τ )dτ (513)
−∞ T { x (t)} = λx (t) , (512)
Z ∞
iϕ iω (t−τ )
= h(τ ) Ae e dτ (514) where λ ∈ C is a constant. This
−∞ means that applying an operator on
Z ∞
= h(τ )e−iωτ Aeiϕ eiωt dτ (515) an eigenfunction of that operator is
−∞ equivalent to multiplication of this
Z ∞   eigenfunction by a constant.
= h(τ )e−iωτ dτ Aeiϕ eiωt (516) The derivation leading up to Equation
−∞ 517 shows that complex sinusoidal sig-
| {z } | {z } nals x (t) = Aeiϕ eiωt are eigenfunctions
H(ω ) x (t)
of LTI systems. The same property also
= H(ω ) x (t) . (517) applies to discrete-time LTI systems
and discrete-time complex sinusoidal
The output of the system is the input signal, multiplied by a complex signals.

function H(ω ) ∈ C, which depends on the angular frequency ω of


the input signal. The term H(ω ) is called the frequency response of
the LTI system:
Z ∞
H(ω ) = h(τ )e−iωτ dτ . (518)
−∞

From the definition, we can see that it is the same thing as a Fourier
transform of the impulse response h(t).
196

Arbitrary signals

Let’s consider an arbitrary continuous-time signal that is represented


as a sum34 of complex sinusoidal signals using the inverse Fourier 34
You can think of the inverse Fourier
transform: transform as stating that the signal
x (t) consists of a linear combination
Z ∞ of infinitely many complex sinusoidal
1 signals.
x (t) = x̂ (ω )eiωt dω . (519)
2π −∞

Here x̂ (ω ) is the Fourier transform of x (t) – it represents the complex


amplitude of spectral components of the signal as a function of
angular frequency ω.
The convolution theorem35 states that convolution in time domain 35
the convolution theorem was dis-
is multiplication in frequency domain: cussed in the continuous-time Fourier
transform chapter

F
y(t) = h(t) ∗ x (t) ←
→ ŷ(ω ) = H(ω ) x̂ (ω ) . (520)

The frequency response therefore tells us how each frequency compo-


nent of the input signal is modified by the LTI system. Therefore, we
can view an LTI system as a filter. How each frequency component
of the input signal is modified, is determined by the Fourier trans-
form of the impulse response of the LTI system, which we will call
frequency response H(ω ).

Aeiϕ eiω̂n LTI H(ω̂ ) Aeiϕ eiω̂n


h[n]

Figure 114: The frequency response of a


Discrete-time frequency response discrete-time LTI system is obtained by
investigating what the system does to a
complex sinusoidal signal.
A discrete-time LTI system T {·} is also described by a convolution of
the input signal with the impulse response h[n] of the LTI system:

y[n] = T { x [n]} (521)


= x [n] ∗ h[n] (522)

36
The normalized angular frequency is
= ∑ h[k] x [n − k] . (523) defined as ω̂ = ωTs (radians per sam-
ple), where Ts (seconds) is the sample
k =−∞
spacing, and ω is the continuous-time
angular frequency (radians per second).
Remember that it is possible to convert
When inspecting the case where the input is a complex sinusoidal normalized angular frequency to fre-
signal x [n] = Aeiϕ eiω̂n , with a normalized angular frequency ω̂ and quency in units of cycles per second
using the formula ω = 2π f . Which
phase ϕ, we get36 : results in f = ω̂ f s /2π, where the
sample-rate is f s = 1/Ts . For example.
this means that ω̂ = π corresponds to a
frequency f = f s /2.
197

y[n] = ∑ h[k] x [n − k] (524)


k
= ∑ h[k] Aeiϕ eiω̂ (n−k) (525)
k
= ∑ h[k]e−iω̂k Aeiϕ eiω̂n (526)
k
!
 
= ∑ h[k]e −i ω̂k
Aeiϕ eiω̂n (527)
k | {z }
| {z } x [n]
H(ω̂ )

= H (ω̂ ) x [n] . (528)


Im
Here the complex-valued function H (ω̂ ) ∈ C, denotes the discrete-
time frequency response, which is defined as:
Re{H (ω̂ )} H (ω̂ )

H (ω̂ ) = ∑ h[k]e −i ω̂k
. (529)
k =−∞
Im{H (ω̂ )}

)|
(ω̂
We’ll later on show that this is a Fourier transform, the discrete-time

|H
Fourier transform of a discrete-time signal h[n].
∠H (ω̂ ) Re

Magnitude and phase response

The frequency response H (ω̂ ) ∈ C is a complex-valued function.


Figure 115: The frequency response
It can be expressed in either Cartesian or polar form using Euler’s is complex-valued, and it represents a
formula: phase shift ∠H (ω̂ ) and a magnitude
scaling |H (ω̂ ) | introduced by the LTI
H (ω̂ ) = Re {H (ω̂ )} + iIm {H (ω̂ )} = |H (ω̂ )| ei∠H(ω̂ ) . (530) system to a complex sinusoidal signal
of normalized angular frequency ω̂.

This is shown in Figure 115.


When an LTI system is applied to a complex sinusoidal signal
x [n] = Aeiϕ eiω̂n , the effect is scaling the absolute value and shifting
the phase of this signal:
|H (ω̂ ) |
1
y[n] = x [n] ∗ h[n] (531)
= H (ω̂ ) Aeiϕ eiω̂n (532) 0.5
i (ϕ+∠H(ω̂ )) i ω̂n
= ( A|H (ω̂ ) |) e| {z }e (533)
| {z } ′ ω̂
A′ eiϕ
′ −π π
= A′ eiϕ eiω̂n . (534) ∠H (ω̂ )
2
The output signal is still a complex sinusoidal signal with the ω̂
same frequency, but the amplitude A is scaled by a factor of |H (ω̂ ) |, −π π
and the phase is shifted by a factor ∠H (ω̂ ). The magnitude of the −2
frequency response |H (ω̂ ) | is called the magnitude response:
q Figure 116: An example frequency
|H (ω̂ ) | = Re{H (ω̂ )}2 + Im{H (ω̂ )}2 , (535) response. Top: The magnitude response
of the LTI system |H (ω̂ ) |. Bottom: The
phase response of the system ∠H (ω̂ ).
198

and the phase ∠H (ω̂ ) is called the phase response

Im{H (ω̂ )}
 
−1
∠H (ω̂ ) = tan . (536)
Re{H (ω̂ )}

of the LTI system.


Figure 116 shows an example frequency response for a low-pass
filter, which reduces the absolute value of complex sinusoidal signals
with high frequencies near ω̂ = ±π. The filter also introduces a
phase shift, which is described by a linear slope. We’ll later see that
a linear slope means that the system applies a time-shift to the input
signal.

Example: Low-pass filter

Let’s find the frequency response of the finite impulse response (FIR)
filter, which has an impulse response defined as:


 1 n=0
 h[n]
 2 n=1 2
h[n] = . (537)

 1 n=2

0 otherwise

1

Another way we can specify the impulse response is as a sum of unit n


impulses: 5 10
h[n] = δ[n] + 2δ[n − 1] + δ[n − 2] (538)
Figure 117: The impulse response h[n]
The impulse response of the filter is shown in Figure 117. of a discrete-time low-pass filter.

The frequency response can be determined using Equation 529:


|H (ω̂ ) |
2 4
H (ω̂ ) = ∑ h[k]e−iω̂k (539)
k =0 2
= h[0]e−iω̂0 + h[1]e−iω̂1 + h[2]e−iω̂2 (540)
ω̂
= 1 + 2e−iω̂ + e−i2ω̂ . (541)
−π π
∠H (ω̂ )
We can tidy this up a bit by multiplying the equation with 1 = 2
e−iω̂ eiω̂ so that we get a conjugate symmetric pairing of complex ex- ω̂
ponential functions, which allows us to represent H (ω̂ ) conveniently −π π
in polar form: −2
 
H (ω̂ ) = e−iω̂ eiω̂ + 2 + e−iω̂ (542)
Figure 118: Magnitude and phase
−i ω̂ response of low pass filter {h[n]}2n=0 =
= [2 + 2 cos(ω̂ )] e|{z} . (543)
| {z } {1, 2, 1}.
magnitude phase

The magnitude response is |H (ω̂ ) | = 2 + 2 cos(ω̂ ) and the phase


response is ∠H (ω̂ ) = −ω̂. These are shown in Figure 118.
199

Based on the magnitude response, we can see that the filter is a


low-pass filter. It reduces the magnitude of high frequency spectral
components of the input signal. At frequency ω̂ = ±π, the output
signal is completely attenuated, as |H(±π )| = 0.
The phase response has a linear slope ∠H (ω̂ ) = −ω̂. This implies
that the output signal is delayed relative to the input by a constant
value. Recall that a delay to a complex sinusoidal signal x [n] =
Aeiϕ eiω̂n is:

x [n − n0 ] = Aeiϕ eiω̂ (n−n0 ) = e−iω̂n0 x [n] . (544)


h[n]
This means that a delay by one sample n0 = 1 will correspond to the
1
phase response of ∠H (ω̂ ) = −ω̂.

Example: High-pass filter n


5 10
A first difference system is defined as follows:

y [ n ] = x [ n ] − x [ n − 1] . (545) −1

This is equivalent to a FIR filter with an impulse response h[n] =


δ[n] − δ[n − 1]. This is shown in Figure 119. Figure 119: The impulse response of a
Using Equation 529, we obtain the frequency response: high-pass filter h[n] = δ[n] − δ[n − 1].

H (ω̂ ) = ∑ h[k]e−iω̂k (546) 2 |H (ω̂ ) |


k
1
= 1 − e−iω̂ . (547) ω̂
−π π
If we multiply this with 1 = e −i ω̂2 e i ω̂2
, we obtain: π
  2 ∠H (ω̂ )
H (ω̂ ) = e−i 2 ei 2 − e −i 2 .
ω̂ ω̂ ω̂ ω̂
(548)
−π π
− π2
We know that 1 iα
2i ( e − e−iα ) = sin(α), therefore:
  Figure 120: Top: The magnitude
ω̂
H (ω̂ ) = e−i 2 2i sin
ω̂
. (549) response of the high-pass filter. Bottom:
2 The phase response of the same filter.
π
We also know that i = ei 2 , so


ω̂ −i 1 (ω̂ −π )
H (ω̂ ) = 2 sin e 2 . (550)
2
In order to inspect the magnitude and phase response, we need to
obtain the frequency response in the following form:

H (ω̂ ) = |H (ω̂ ) |ei∠H(ω̂ ) . (551)

We can do this using a phase shift by π to flip the phase −1 = e−iπ .


This allows us to take into account the fact that sin(ω̂/2) is negative
when −π < ω̂ < 0. The frequency response can now be written as:
1
(
2 sin ω̂2 e−i 2 (ω̂ −π ) when

0 < ω̂ < π
H (ω̂ ) =  − i 1 ( ω̂ + π ) . (552)
ω̂
2 sin 2 e 2 when −π < ω̂ ≤ 0
200

The magnitude response is thus:


 
ω̂
|H (ω̂ ) | = 2 sin , (553)
2
and the phase response is:
(
− 21 ω̂ + π
2 when 0 < ω̂ < π
∠H (ω̂ ) = . (554)
− 21 ω̂ − π
2 when −π < ω̂ ≤ 0
The magnitude and phase response is shown in Figure 120. It com-
pletely removes the zero-frequency component of the signal, and h[n]
reduces the amplitudes of low frequency spectral components. High 1
frequency spectral components of the input signal will be amplified.
For input signal spectral components with frequencies at ω̂ = ±π,
n
the filter output signal spectral components will have twice the origi-
−4 −2 2 4
nal magnitude.
This type of filter is called a high-pass filter, as it attenuates low
frequency spectral components relative to high frequency spectral −1
components.

Figure 121: The impulse response of a


Example: band-pass filter simple band-pass filter.

Let’s now investigate the frequency response of the following filter:

y[n] = 0.5x [n + 1] − 0.5x [n − 1] . (555)

The impulse response h[n] = 0.5δ[n + 1] − 0.5δ[n − 1] of this system is


shown in Figure 121.
Using Equation 529, we obtain the frequency response:

H (ω̂ ) = ∑ h[k]e−iω̂k (556)


k
= 0.5eiω̂ − 0.5e−iω̂ (557)
= i sin(ω̂ ) . (558)

If we wanted to investigate this analytically, we could use the type of


analysis that did in the previous example. We’ll write a little Python
script instead to plot the magnitude and phase response. This is
shown in Listing 37.
The plot produced is shown in Figure 122. From the magnitude Figure 122: The frequency response
of the band-pass filter h[n] = 0.5δ[n +
response plot, we can see that this filter reduces the magnitudes of 1] − 0.5δ[n − 1]. Top: the magnitude
high and low frequency spectral components of the input signal response. Bottom: The phase response.
near ω̂ = 0 and ω̂ = ±π. However, the spectral components with
frequencies near ω̂ = ± π2 are unchanged in magnitude. The phase
response is flat, except for a discontinuity at 0, where the phase
switches from − π2 to π2 to account for the sign of the sin function.
This type of filter that only lets through spectral components of the
signal within a range of frequencies is called a band-pass filter.
201

import m a t p l o t l i b . pyplot as p l t
import numpy as np

# A sweep o f −\ p i t o \ p i i n normalized angular frequency .


omhat = np . l i n s p a c e ( −np . pi , np . pi , num=500)
H = 0 . 5 * np . exp ( 1 j * omhat ) − 0 . 5 * np . exp ( −1 j * omhat )

plt . figure ( figsize =(0.7*6 , 0.7*8) )


plt . subplot (211)
plt . p l o t ( omhat , np . abs (H) )
plt . t i t l e ( " Magnitude response " )
plt . y l a b e l ( r " $|\\mathcal {H} ( \ hat {\omega } ) |$ " )
plt . x l a b e l ( r " $\hat {\omega } $ " )
plt . subplot (212)
plt . t i t l e ( " Phase response " )
plt . p l o t ( omhat , np . angle (H) )
plt . x l a b e l ( r " $\hat {\omega } $ " )
plt . y l a b e l ( r " $\\angle\\mathcal {H} ( \ hat {\omega } ) $ " )
plt . tight_layout ()
plt . s a v e f i g ( " b p f _ f r e s p . png " )
plt . show ( )

Listing 37: 019_frequency_response/example_bpf.py

1.5
h[n]
Example: Delay filter 1
0.5
A delay system is defined as: n
n0

y [ n ] = x [ n − n0 ] . (559) Figure 123: The impulse response of a


time-shift system.
This has a unit impulse h[n] = δ[n − n0 ] (shown in Figure 123). Using
Equation 529, we obtain the following frequency response: 2
1.5 |H (ω̂ ) |
H (ω̂ ) = ∑ h[k]e−iω̂k (560) 1
0.5
k ω̂
= ∑ δ[k − n0 ]e−iω̂k (561) −π π
k n0 π ∠H (ω̂ )
−i ω̂n0 ω̂
=e . (562)
−π π
− n0 π
The magnitude response is |H (ω̂ ) | = 1 and the phase response
is ∠H (ω̂ ) = −ω̂n0 . This is shown in Figure 124. This filter does Figure 124: The frequency response of a
time-delay filter.
not modify the magnitude of any spectral components. However, it
applies a phase shift to spectral components, which results in a time
delay.
This may seem trivial now, but the frequency domain represen-
tation for a time-shift will later be useful; when defining more ad-
vanced, fractional sample, time-shift filters for discrete-time signals.
In this case, n0 ∈ R is not an integer. We’ll need to define the inverse
discrete-time Fourier transform in order to derive the filter impulse
response in this case.
202

Example: Continuous-time low-pass filter h(t)


1
T
Consider a continuous-time LTI system, which uniformly averages
the input signal around a region of length T:
T t
1
Z
2
y(t) = x (t − τ )dτ . (563) − T2 T
T − T2 2

|H(ω )|
This has the following an impulse response:

1
h(t) = [u(t + T/2) − u(t − T/2)] . (564)
T
ω
This is the familiar rectangular function. Using Equation 518, we − 2π 2π
T T
obtain the following frequency response37 : ∠H(ω )|
π

2 sin(ωT/2)
H(ω ) = . (565)
ωT
Note that this is a continuous-time frequency response. Our fre- ω
quency variable is angular frequency (radians per second). The − 2π 2π
T T
frequency response is a sinc-function, as shown in Figure 125. This
is a low-pass filter, as this filter reduces the magnitude of spectral Figure 125: Top: the impulse response
of an averaging filter. Middle: The
components of the input signal x (t) with higher frequencies.
magnitude response. Bottom: The
phase response.

Example: Time symmetric running average filter 37


See the Fourier transform chapter for
derivation of Equation 565.

Let’s now look at a discrete-time version of the previous continuous-


time filter, the time symmetric running average system. This system
averages M = 2N + 1 values of the input signal x [n]:

N h[n]
1
y[n] =
M ∑ x [n − k] . (566) 1
M
k =− N

We can easily see that this is an LTI system with an impulse re- n
sponse: −N −N + 1 · · · ··· N−1 N

N
1
h[n] =
M ∑ δ[n − k] . (567) Figure 126: The impulse response of a
discrete-time running average filter.
k =− N

The impulse response h[n] of the system is shown in Figure 126. This
type of filter is often used to smooth noisy signals. It will also later
on play an important role in determining the frequency response of a
discrete Fourier transform.
Using Equation 529, we can find the frequency response of this
filter:
N
1 −iω̂k
H (ω̂ ) = ∑ e . (568)
k =− N
M
203

Let’s set c = 1/M, α = eiω̂ , and S = H (ω̂ ). By doing some algebraic


manipulations, we can find a closed form solution:

S = cα− N + cα− N +1 + · · · + c + · · · + cα N −1 + cα N (569)


− N +1 − N +2 N N +1
αS = cα + cα + · · · + c + · · · + cα + cα (570)
−N N +1
S − αS = cα − cα (571)
−N N +1
S (1 − α ) = c ( α −α ) (572)
α − N − α N +1
S=c . (573)
1−α

This is known as the closed form solution to a geometric series. When


substituting back the values of c, α, and S we obtain:

1 e−iω̂N − eiω̂ ( N +1)


H (ω̂ ) = . (574)
M 1 − eiω̂

We can once again use the trick of multiplying the complex expo-
nentials with 1 = eiω̂/2 e−iω̂/2 on the numerator and denominator.
This allows us to create conjugate pairings of complex exponentials:

1 e−iω̂N − eiω̂ ( N +1)


H (ω̂ ) = (575)
M 1 − eiω̂
1 (e − i ω̂N − eiω̂ ( N +1) )[eiω̂/2 e−iω̂/2 ]
= (576) 1 D7 (ω̂ )
M (1 − eiω̂ )[eiω̂/2 e−iω̂/2 ]
 −iω̂ ( N +1/2)
1eiω̂/2
 (e − eiω̂ ( N +1/2) )
= (577)
Meiω̂/2
 (e−iω̂/2 − eiω̂/2 ) ω̂
−π −ω̂0
1 e−iω̂ ( N +1/2) − eiω̂ ( N +1/2) ω̂0 π
= , (578)
M e−iω̂/2 − eiω̂/2 1 D21 (ω̂ )

which then allows us to use sin(θ ) = 1 iθ


2i ( e − e−iθ ) to relate the
symmetric pairs to sin functions: ω̂
−π π
−
12i sin(ω̂ ( N + 1/2))
H (ω̂ ) = = D M (ω̂ ) . (579)
−
M2i sin(ω̂/2)
Figure 127: The Dirichlet kernel D7 (ω̂ )
and D21 (ω̂ ). The first zero-crossings
The function D M (ω̂ ) is defined as (recall that M = 2N + 1):
±ω̂0 are depicted for D7 (ω̂ ), which
indicate the bandwidth of the filter.
1 sin(ω̂M/2)
D M (ω̂ ) = . (580)
M sin(ω̂/2)

It is sometimes referred to as the periodic sinc function, and some-


times as the Dirichlet kernel38 . 38
Note that there are several con-
Equation 580 may at first appear problematic at ω̂ = 0, which flicting definitions of the Dirichlet
kernel. I am using the one consis-
results in D M (0) = 0/0. This can be resolved using L’Hôpital’s rule: tent with the Scipy implementation
scipy.special.diric(x,n).
f (x) f ′ (x)
lim = lim ′ . (581)
x →c g( x ) x →c g ( x )
204

From this it follows that:

D M (0) = 1 . (582)

Figure 127 shows a plot of the Dirichlet kernel for M = 7 and M =


21, which shows that the width of the main lobe becomes more
narrow as M is increased. The location of the first zero-crossing of | D21 (ω̂ )|
1
D M (ω̂0 ) = 0 is at ω̂0 = 2π/M. This is a demonstration of time-
frequency ambiguity, i.e., the inverse relationship between the width of
a signal in time domain and frequency domain. ω̂
−4π −3π −2π −π π 2π 3π 4π
It can be shown that the Dirichlet kernel also arises from the
Fourier series representation of a Dirac comb signal with period 2π:
Figure 128: The Dirichlet kernel is 2π-
∞ periodic, which is a general property of
D ′ (ω̂ ) = ∑ δ(ω̂ − 2πk) , (583) discrete-time frequency responses.
k =−∞

which has the following Fourier series representation using M =


2N + 1 terms:
N
1 1 sin(ω̂M/2)
D ′M (ω̂ ) =
2π ∑ eiω̂k =
2π sin(ω̂/2)
. (584)
k =− N

Note that D ′M (ω̂ ) and D M (ω̂ ) are up to a constant the same. The
proof of Equation 584 is essentially the same as the derivation of the
running mean filter frequency response shown above39 . As N → ∞, 39
The Fourier series representation for a
the location of the first zero crossing approaches ω̂ = 0. This means Dirac comb signal was discussed in the
Fourier series chapter.
that the bandwidth of the filter approaches zero and passes through
only signals with frequencies ω̂ = 2πk.
From Equation 583 it is easy to see that the Dirichlet kernel is
2π-periodic. This is a general property that applies to the frequency
response of any discrete-time LTI system, and is due to dicretization
of the signal.
There is a short Python program in Listing 38, which can be used
to plot the magnitude and phase response of the running average
filter. The output of this program is shown in Figure 129.
import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . s p e c i a l as s

def f r e q u e n c y _ r e s p o n s e ( h , n_value , omhat ) :


H = np . z e r o s ( l e n ( omhat ) , dtype=np . complex64 )
f o r idx i n range ( l e n ( h ) ) :
H += h [ idx ] * np . exp ( −1 j * n_value [ idx ] * omhat )
r e t u r n (H)
Figure 129: The output of the program
in Listing 38, which shows the magni-
# Impulse response o f an averaging f i l t e r . tude and phase response of the 21-point
# M = 2N + 1 running average filter. As one can
h = np . r e p e a t ( 1 . 0 / 2 1 . 0 , 2 1 ) see of the plot, the two methods have
equivalent magnitude response.
205

print ( len (h) )


omhat = np . l i n s p a c e ( −np . pi , np . pi , num=1000)
# I n t e g e r s −N t o N.
H = f r e q u e n c y _ r e s p o n s e ( h , np . arange ( − 1 0 , 10+1) , omhat )

# A l t e r n a t i v e way using t h e d i r i c h l e t k e r n e l :
# D_21 (\omega )
D = s . d i r i c ( omhat , 2 1 )

plt . figure ( figsize =(0.8*6 , 0.8*8) )


plt . subplot (211)
plt . p l o t ( omhat , np . abs (D) )
plt . p l o t ( omhat , np . abs (H) )
plt . t i t l e ( " Magnitude response " )
plt . y l a b e l ( r " $|\\mathcal {H} ( \ hat {\omega } ) |$ " )
plt . x l a b e l ( r " $\hat {\omega } $ " )
plt . subplot (212)
plt . t i t l e ( " Phase response " )
plt . p l o t ( omhat , np . angle (D) )
plt . x l a b e l ( r " $\hat {\omega } $ " )
plt . y l a b e l ( r " $\\angle\\mathcal {H} ( \ hat {\omega } ) $ " )
plt . tight_layout ()
plt . s a v e f i g ( " d i r i c h l e t 2 1 . png " )
plt . show ( )

Listing 38: 019_frequency_response/dirichlet.py


206

Exercises: Frequency Response

1. The impulse response of an LTI system is defined as:

42 sin(ωc t)
h(t) = . (585)
πt
The input to the LTI system is a periodic signal with a fundamen-
tal period T = 1:

x (t) = ∑ δ ( t − n ). (586)
n=−∞

When the signal x (t) is fed into an LTI system, the output signal is
given by a convolution of the input signal with the output signal:
Z ∞
y(t) = h(t − τ ) x (τ )dτ. (587)
−∞

a) Determine the Fourier transform x̂ (ω ) of the input signal. Plot


x̂ (ω ) over the range of angular frequencies −7π < ω < 7π.
Hint: It might help if you start by expressing the signal x (t)
using a Fourier series representation first, and then apply
Equation 264.
b) Determine the frequency response H(ω ) of the LTI system. In
other words, Fourier transform the impulse response signal
given in Equation 585. It should be a fairly simple function.
Make a plot of |H(ω )| on the same graph as x̂ (ω ) using ωc =
5π.
c) Use your plot in b) to determine y(t), the output of the LTI
system characterized using h(t) when ωc = 5.5π. Hint: use
property that convolution in time domain is multiplication in
frequency domain.
d) What values of ωc will result in a non-zero constant output
y(t) = c. What is the constant c?

2. A running average filter is defined as:


M −1
1
y[n] =
M ∑ x [ n − k ]. (588)
k =0

This filter only depends on the current and past values of the in-
put, and can thus be implemented in a real-time system. Assume
that M is an odd number.

a) What is the impulse response h M [n] of the system described in


Equation 588?
b) What is the frequency response H M (ω̂ ) of the system described
in Equation 588?
207

c) What is the frequency response Hτ (ω̂ ) of a time-shift system


h τ [ n ] = δ [ n − τ ]?
d) I recommend that you do this task on a computer. Plot the
squared magnitude response |H M (ω̂ )|2 between −π < ω̂ < π.
Assume that M = 11. Compare this with a plot of the squared
magnitude response of the time-symmetric running average
filter | D M (ω̂ )|2 of the same length. It is defined in Equation
580. They should be the same. If you are not sure why, carry on
with the next tasks.
e) Show that the frequency response H M (ω̂ ) be obtained by
multiplying the frequency response of the time symmetric
running average filter given in Equation 580 with the frequency
response of the time shift system with a suitable value of τ: h[n]
2

H M (ω̂ ) = D M (ω̂ )Hτ (ω̂ )? (589)

Hint: Look at the derivation of D M (ω̂ ). n

f) Explain why |H M (ω̂ )|2 = |D M (ω̂ )|2 . Hint: | z |2 = zz∗ . −4 −2 2 4

3. It is possible to numerically estimate the second time derivative


of a discretized signal using an FIR filter with the following filter −2
coefficients:
Figure 130: The impulse response of a
h[n] = Ts−2 δ[n + 1] − 2Ts−2 δ[n] + Ts−2 δ[n − 1] (590) second order derivative estimator filter
with Ts = 1.
where Ts is the sample spacing. This can be motivated by the
following definition of a second time-derivative:

d2 x x (t + Ts ) − 2x (t) + x (t − Ts )
= lim (591)
dt2 Ts →0 Ts2

which is numerically evaluated with a finite difference Ts .


If a signal of the form x [n] = Aeiϕ eiω̂n is fed into the FIR filter
described by an impulse response h[n], the output signal will be of

the form y[n] = A′ eiϕ eiω̂n . Here A, A′ ∈ R≥0 and ω̂, ϕ, ϕ′ ∈ R.

a) Show that the frequency response of this filter is

2
H(ω̂ ) = [cos(ω̂ ) − 1] (592)
Ts2

b) Make a plot of the magnitude response |H(ω̂ )| between nor-


malized angular frequencies −π < ω̂ < π (radians per
sample).
c) What is the phase response ∠H(ω̂ ) of this system?
d) Is this filter a high-pass or a low-pass filter?
208

e) What is A′ when ω̂ = 0?
f) What is A′ and ϕ′ when ω̂ = π/2 and ω̂ = π?
g) Show that for continuous-time signals, the second time-
d2
derivative operation T { x (t)} = dt 2 x ( t ) has a frequency re-
sponse of the form H(ω ) = −ω . 2 ω̂ and ω
π
h) A plot of the continuous-time frequency response found in g)
and the discrete-time frequency response found in a) is shown
−5
in Figure 131. The sample spacing is assumed to be Ts = 1.
With this sample spacing, both ω and ω̂ will have the same
scale (ω = 1 rad/s will correspond to ω̂ = 1 rad/sample). −10 H(ω ) = −ω 2
What does the plot tell you about how well the FIR filter in H(ω̂ ) = 2(cos(ω̂ ) − 1)
Equation 590 approximates the second time derivative op-
−15
eration for continuous-time complex sinusoidal signals as a
function of frequency? Figure 131: The frequency response
of a continuous-time second time-
derivative operation and a numerical
estimator of the second time-derivative
in discrete-time.
209

Suggested solutions: Frequency Response

1. The impulse response for an LTI system is:

42 sin(ωc t)
h(t) = .
πt
Let the input to the LTI system be a periodic signal with period
T = 1 of the form:

x (t) = ∑ δ ( t − n ).
n=−∞

a) Given x (t) we find the Fourier transform as follows, firstly, we


know that the Dirac comb can be expressed as a Fourier series
of the form:
1 ∞ i 2πnt
x (t) = ∑ e T .
T n=− ∞

In this case, we have T = 1, hence:



x (t) = ∑ ei2πnt .
n=−∞

Thus, the Fourier series coefficients are ck = 1 for all k ∈ Z.


Then the Fourier transform is:

x̂ (ω ) = 2π ∑ δ(ω − 2πn).
n=−∞

A plot of the spectrum for x̂ (ω ) is shown in Figure 132 in red.


b) The frequency response function is defined by:
Z ∞
42 sin(ωc t) −iωt
H(ω ) = F {h(t)} = e dt = 42[u(ω + ωc ) − u(ω − ωc )].
−∞ πt

Figure 132 shows the frequency response in green.


Figure 132: Spectrum for the Dirac
c) The output of the LTI system can be found by convolution comb between −7π < ω < 7π.
in time domain, that is y(t) = h(t) ∗ x (t), thus, in frequency
domain we have multiplication by the convolution theorem. By
Figure 132 the frequencies inside (−5.5π, 5.5π ) remain after
multiplication, but the frequencies outside (−5.5π, 5.5π ) gets
mapped to 0.
d) If y(t) = c then ŷ(ω ) = 2πcδ(ω ) so the frequency response
must be such that:

2πcδ(ω ) = x̂ (ω )H(ω ) = 2π ∑ δ(ω − 2πn)42[u(ω + ωc ) − u(ω − ωc )] = 2π42δ(ω ),
n=−∞

the only way for this to work is that |ωc | ≤ π giving c = 42.
210

2. A running average filter is defined as:


M −1
1
y[n] =
M ∑ x [ n − k ].
k =0

We’ll assume that M is an odd number.

a) The impulse response function is obtained by feeding a Dirac


delta function into the LTI system, so if y[n] = T { x [n]}, then
h[n] = T {δ[n]}. Doing this, we find that:
M −1
1
h[n] =
M ∑ δ [ n − k ].
k =0

b) The frequency response is the DTFT of h[n], which gives:


!
M −1
1 −iω̂k 1 1 − e−iω̂M 1 e−iω̂M/2 (eiω̂M/2 − e−iω̂M/2 )
H(ω̂ ) = ∑ M
e =
M (1 − e−iω̂ )
=
M e−iω̂/2 (eiω̂/2 − e−iω̂/2 )
,
k =0
1 sin(ω̂M/2) −iω̂ ( M−1)/2
= e .
M sin(ω̂/2)
| {z }
D M (ω̂ )

The sum, is evaluated using known formulas for geometric


sums.
c) If we have a system with impulse response hτ [n] = δ[n − τ ],
then the frequency response is:
∞ ∞
Hτ (ω̂ ) = ∑ h[k ]e−iω̂k = ∑ δ[k − τ ]e−iω̂k = e−iω̂τ .
k=−∞ k =−∞

d) The following code can be used to make the plot shown in


Figure 133:
import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . s p e c i a l as dd

# P a r t i t i o n t h e i n t e r v a l ( − pi , p i ) i n t o num p o i n t s .
om = np . l i n s p a c e ( −np . pi , np . pi , num=1000)

# Use M = 11 f o r t h e D i r i c h l e t k e r n e l p l o t .
M = 11

# P l o t t h e two f u n c t i o n s i n t h e same c o o r d i n a t e system .


p l t . p l o t (om, np . abs ( dd . d i r i c (om, n=M) ) * * 2 , l a b e l = r " $|D_
{ 1 1 } ( \ hat {\omega } ) | ^ { 2 } $ " )
p l t . p l o t (om, np . abs ( np . exp ( 1 j *om * (M−1) /2) * dd . d i r i c (om, n=M)
) * * 2 , l a b e l = r " $|H_{ 1 1 } ( \ hat {\omega } ) | ^ { 2 } $ " )
p l t . x l a b e l ( r " $\hat {\omega } $ " )
p l t . legend ( )
# C a l l t h i s i f needed .
# plt . plot ( )

Listing 39: Plotting the frequency response code


211

As expected, the functions are identical.


e) The frequency response function was derived in b), and it has
the form:
H(ω̂ ) = D M (ω̂ )Hτ (ω̂ ),
where Hτ (ω̂ ) is a time-shift system with τ = ( M − 1)/2.
Figure 133: Comparison of frequency
f) Claim: |H(ω̂ )|2 = | D M (ω̂ )|2 . responses.

Proof. A simple computation gives:

|H(ω̂ )|2 = H(ω̂ )H∗ (ω̂ ) = ( D M (ω̂ )Hτ (ω̂ ))( D M (ω̂ )Hτ (ω̂ ))∗ ,
= | D M (ω̂ )|2 e|−iω̂τ i ω̂τ 2
{ze } = | D M (ω̂ )| ,
1

as claimed.

In conclusion, both running average filters produce the same


magnitude response, but one of them induces a time-shift
while the other doesn’t.

3. Consider an FIR filter approximation to the second derivative of


the form:

h[n] = Ts−2 δ[n + 1] − 2Ts−2 δ[n] + Ts−2 δ[n − 1].

a) The frequency response can be found by the DTFT (Equation


518) as:

H(ω̂ ) = ∑ h[k]e−iω̂k ,
k =−∞

for this LTI system, we get:

2
H(ω̂ ) = Ts−2 eiω̂ − 2Ts−2 + Ts−2 e−iω̂ = −2Ts−2 + Ts−2 2 cos(ω̂ ) = (cos(ω̂ ) − 1).
Ts2

b) The code shown in Listing 40 can be used to plot the spectral


response.
import m a t p l o t l i b . pyplot as p l t
import numpy as np

# P l o t i n t h e p r i n c i p a l spectrum ( − pi , p i ) .
om = np . l i n s p a c e ( −np . pi , np . pi , num=1000)

# For s i m p l i c i t y t a k e Ts as 1 .
Ts = 1

def f r e q _ r e s p (om: np . ndarray ) −> np . ndarray :


" " " D e f i n i t i o n o f t h e frequency response f u n c t i o n . " " "
r e t u r n 2/( Ts * * 2 ) * ( np . cos (om) −1)

# P l o t t h e frequency response f u n c t i o n over ( − pi , p i ) .


212

p l t . p l o t (om, np . abs ( f r e q _ r e s p (om) ) , l a b e l =r " $\mathcal {H} ( \


hat {\omega } ) $ " )
p l t . legend ( )
# C a l l t h i s i f needed .
# plt . plot ( )

Listing 40: Frequency response for finite difference

Output of the code is shown in Figure 134.


c) In this case, the frequency response function is real, so the
phase response is 0. Meaning, there is no delay applied to the
signal by this filter.
d) By looking at the plot of | H (ω̂ )|, we see that the filter is a high-
pass filter, as the frequencies around 0 are greatly reduced. Figure 134: Output of Listing 40

e) Inspecting the plot, we see that for ω̂ = 0, the amplitude of the


output signal will be A′ = 0. The reason is that in frequency
domain we have:
ŷ(ω̂ ) = H(ω̂ ) x̂ (ω̂ ).

f) The phase is not changed by the filter, so ϕ′ = ϕ. For ω̂ = π/2


the amplitude is doubled, so A′ = 2A, while for ω̂ = π the
amplitude is scaled by 4, giving A′ = 4A. This can be seen
from the plot in Figure 134 or by inserting the values into the
frequency response function.
d 2
g) The system y(t) = T { x (t)} = dt 2 x ( t ) is a continuous-time LTI
system, so:
y(t) = H(ω ) x (t).

In particular, if we take x (t) = Aeiϕ eiωt , then:

d2
y(t) = ( Aeiϕ eiωt ) = Aeiϕ (iω )2 eiωt = − Aeiϕ ω 2 eiωt = −ω 2 ( Aeiϕ eiωt ) = H(ω ) x (t),
dt2
thus H(ω ) = −ω 2 .
h) Comparing the plots, we conclude that the approximation is
only good for small angular frequencies in the range 0 < ω <
π/2.
Programming Assignment 2

This programming assignment deals with deconvolution of transmit


signals for an acoustic sounder or a sonar. A sonar is a device that
measures the amplitude of sound waves that are scattered from
objects at different propagation distances between the acoustic wave
transmitter and receiver. These devices are used, e.g., in cars to assist
with parking, in ships to measure the water depth, and for non-
invasive medical examinations. Sonars are also closely related with
radars, as they share many of the same signal processing concepts.
The main difference is that radars use electromagnetic waves instead
Figure 135: Bats use chirp-like ultra-
of acoustic waves. sound signals to sense their surround-
In order to implement a simple sonar, I have used a loudspeaker ings. Image: David Dennis.
and a microphone connected to a sound card to transmit and receive
acoustic signals. The sampling rate of the audio recording is f s =
44.1 · 103 hertz, which results in a sample-spacing of Ts = 2.2676 ·
10−5 s. The loudspeaker and the microphone are spaced apart by
approximately 30 cm.
If we ignore measurement errors, the acoustic signal can be mod-
eled using a discrete-time convolution equation:
R −1
m[n] = ∑ h [r ] x [ n − r ] = h [ n ] ∗ x [ n ] (593)
r =0

In this equation m[n] is the signal measured with a microphone, x [n]


is the signal transmitted by the loudspeaker, and h[n] is the ampli-
tude of the scattered signal as a function of range. The signal h[n] is
also the impulse response of the space that the sonar is measuring.
The convolution equation can be motivated with a range-time dia-
gram that models the propagation of acoustic signals as a function of
range and time. Such a diagram is shown in Figure 136.
Modern sonars often use long waveforms in order to compress
more energy into the transmitted pulse without increasing the
amount of peak power. Longer waveforms also make it easier to
separate multiple different transmitters from one another, reducing
interference.
One of the main objectives of a sonar measurement is to recover
214

Figure 136: A range-time diagram


depicting the relationship between
r
a transmitted signal and a scattered
Range (samples)

signal.
··· ··· ···
Propagation velocity (r = n)
h[5] h[5] h[5] ···

h[4] h[4] h[4] ···

h[3] h[3] h[3] ···

h[2] h[2] h[2] ···

h[1] h[1] h[1] ···

h[0] h[0] h[0] ···

x[0] x[1] x[2] x[3] x[4] ··· Time (samples) n

m[0] m[1] m[2] m[3] m[4] ···


PM
m[n] = r=0 h[r]x[n − r]

the impulse response h[n] from measurements m[n]. This is trivial


when the transmit pulse is a unit impulse x [n] = Aδ[n]. With longer
transmit signals, we need to deconvolve the transmitted signal. This
is typically achieved by convolving the received signal m[n] with a
deconvolution filter λ[n]:

λ [ n ] ∗ m [ n ] = λ [ n ] ∗ x [ n ] ∗ h [ n ]. (594)

The deconvolution filter needs to have the following property40 : 40


One way to determine λ[n] is to solve
for it in frequency domain, but this is
λ[n] ∗ x [n] ≈ Aδ[n], (595) beyond the scope of this exercise.

where A is a constant. In the case of chirp-like signals, an often used


deconvolution filter is the time reversed chirp signal:

λ[n] = x [−n + n0 ], (596)

where n0 is a suitable time shift.


For this assignment, I have used an amplitude tapered chirp-like
waveform with a linearly increasing frequency of the following form:
(
sin2 πn sin 2πβTs2 n2
 
N when 0≤n<N
x [n] = (597)
0 otherwise

Here N is the integer length of the chirp pulse in samples and


1
β = 2NT s
f max is the chirp-rate, which determines how fast the in-
stantaneous frequency of the signal increases as a function of time,
with f max the maximum frequency used of the chirp (in hertz) at
the end of the transmit pulse. Figure 137 shows this waveform with
N = 1000 and β = 486202.5 s−2 .
You will need to download two data files for this task:

Figure 137: The chirp transmit pulse


signal x [n] used for this exercise. In this
case, the pulse is N = 1000 samples
long, sweeping in frequency between 0
and 22.05 kHz.
215

• http://kaira.uit.no/juha/chirp.bin

• http://kaira.uit.no/juha/sonar_meas.bin

The first one contains the transmitted chirp waveform x [n] and
the second one contains a microphone recording during a sonar
experiment m[n]. Note that if you do not want to use the provided
recordings, feel free to use your own sonar setup. You will need a
microphone and a loudspeaker, and some software that will transmit
the chirp waveform in a loop. You can also borrow my equipment in
order to make your own measurements.
You can read these signals into a computer as follows:
import m a t p l o t l i b . pyplot as p l t
import numpy as np

# Read c h i r p waveform .
c h i r p = np . f r o m f i l e ( " c h i r p . bin " , dtype=np . f l o a t 3 2 )
# Read microphone r e c o r d i n g .
m = np . f r o m f i l e ( " sonar_meas . bin " , dtype=n . f l o a t 3 2 )

plt . plot ( chirp )


plt . x l a b e l ( " Time ( samples ) " )
plt . y l a b e l ( " Transmitted s i g n a l $x [ n ] $ " )
plt . show ( )

# Plot the f i r s t three i n t e r p u l s e periods .


p l t . p l o t (m[ : 3 0 0 0 0 ] )
p l t . x l a b e l ( " Time ( samples ) " )
p l t . y l a b e l ( " Received s i g n a l $m[ n ] $ " )
p l t . show ( )

These acoustic pulses are transmitted every M = 10000 samples. This


means that the sonar is repeating a measurement of h[n] every 10000
samples and the transmitted signal actually looks like this:

N p −1
ϵ[n] = ∑ x [n − Mk ] (598)
k =0

In this task, you should treat each segment of 10000 samples as an


independent sonar measurement, which will allow you to study how
the impulse response evolves over time. You can do this as follows:
import m a t p l o t l i b . pyplot as p l t
import numpy as np

m = np . f r o m f i l e ( " sonar_meas . bin " , dtype=np . f l o a t 3 2 )

i n t e r p u l s e _ p e r i o d = 10000
N_ipps = i n t ( np . f l o o r ( l e n (m) / i n t e r p u l s e _ p e r i o d ) )
f o r i i n range ( N_ipps ) :
echoes = m[ ( i * i n t e r p u l s e _ p e r i o d ) : ( i * i n t e r p u l s e _ p e r i o d +
interpulse_period ) ]
p l t . p l o t ( echoes )
p l t . show ( )
216

For this assignment, you are to perform the following tasks. Write
a short report describing what you did and what results you got. The
report is otherwise free form, as long as it is in PDF format. Include
your code and plots in the report.
You will find a lot of help for this task in the lecture notes that
discusses LTI systems, convolution and frequency response. You may
help each other on Perusall. It is fine to give hints, but please try not
to give away the exact solution.

a) How many meters of distance41 does an acoustic pulse travel 41


I mean total distance, not half the total
distance, which is often used in sonar
during one sample duration Ts = f s−1 ? Assume sound speed in a
and radar to take into account the fact
typical lecture room at UiT (343 m/s). that the wave has to travel to the target
and back.
b) How long is the transmitted pulse read from file chirp.bin in
seconds?
c) How long is the measurement in seconds? You can figure this out
by looking at the length of the signal sonar_meas.bin and using
the known sample-rate of 44.1 · 103 samples per second.
d) How many transmit pulses are contained in the measurement?
One pulse is transmitted every 10000 samples.
e) Why does the deconvolution filter λ[n] need to have the property:
λ[n] ∗ x [n] ≈ Aδ[n], where A is a constant?
f) Evaluate the autocorrelation function of the transmitted signal
c[n] = x [n] ∗ x [−n]. Use the signal that you read from chirp.bin.
You can time-reverse a signal in Python using deconvolution_filter = x[::-1].
Make a plot of c[n] zooming into the portion of the signal that has
significantly non-zero values.
g) How close is c[n] in your opinion to a scaled unit impulse Aδ[n]?
How will deviations from the unit impulse affect deconvolution
quality when using λ[n] = x [−n] as a deconvolution filter?
h) Make a plot of the scattered power as a function of time and
range. Use one pulse for each column of the array that you are
plotting. You should get something similar to the plot in Figure
138. Use decibel scale for power. Use time in seconds on the
x-axis and total propagation distance in meters on the y-axis.
Total propagation distance means the total travelled distance
between the loudspeaker and the microphone. You can use, e.g.,
the pcolormesh command from Python’s Matplotlib to make this
plot.
217

Figure 138: An example range-time


intensity plot showing how much
power in decibel scale is scattered
back from the surroundings of the
sonar system. The distance between
the microphone and the loudspeaker
is about 30 cm, which can be seen
as a constant horizontal line at the
propagation distance. The plot also
shows an acoustic scatterer that is first
moving away and then moving back
towards the transmitter.
Discrete-time Fourier Transform (DTFT)

The equation for frequency response of a discrete-time linear time-


invariant system (Equation 529) is also known as the Discrete-Time
Fourier Transform (DTFT). We can obtain a DTFT for an arbitrary
discrete-time signal x [n] as follows42 : 42
Hat on top of x̂ to signify that it is
a frequency domain representation of
∞ signal x [n], and a hat on top of ω̂ to
x̂ (ω̂ ) = ∑ x [n]e−iω̂n . (599) signify that the frequency has units of
n=−∞ radians per sample.

Here x̂ (ω̂ ) is the DTFT of a discrete-time signal x [n]. We’ll use the
following notation for denoting DTFT pairs:

F
x [n] ←
→ x̂ (ω̂ ) (600)

like we did in the Fourier transform chapter. The left-hand side


denotes the time domain representation, and the right-hand side
denotes the frequency domain representation.
Let’s show that Equation 599 is a special case of the continuous-
time Fourier transform for discretized signals. Remember that the
formula for discretizing a continuous-time signal is:

x [n] = x (nTs ) . (601)

Here Ts is the sample spacing, which is related to sample-rate f s =


1/Ts . The continuous-time representation of the discretized signal is
defined as:
∞ x ( t ), x d ( t )
xd (t) = ∑ x [n]δ(t − nTs ) . (602) Ts
n=−∞
You might recall this from the derivation of the Whittaker-Shannon t
sampling theorem. Figure 139 depicts a continuous-time signal and
the continuous-time representation of the discretized signal xd (t),
which is a sequence of unit impulses multiplied by the value of each
discrete-time signal sample x [n] at the position in time, where it was
sampled. Figure 139: A continuous-time signal
The Fourier transform of xd (t) is x (t) and a continuous-time represen-
tation of a discretized signal xd (t).
Z ∞
Sample spacing Ts is related to sample
x̂d (ω ) = xd (t)e−iωt dt . (603) rate as follows: Ts = 1/ f s .
−∞
220

This can be simplified as follows:


Z ∞
!
x̂d (ω ) =
−∞
∑ δ(t − nTs ) x [n] e−iωt dt (604)
n=−∞
∞ Z ∞
= ∑ δ(t − nTs ) x [n]e−iωt dt (605)
n=−∞ −∞

= ∑ x [n]e−iωnTs . (606)
n=−∞

If we substitute ω̂ = ωTs , we get43 : 43


This also implies that x̂ (ω̂ ) =
x̂d (ω̂/Ts )

x̂ (ω̂ ) = ∑ x [n]e−iω̂n . (607)
n=−∞

This shows that the discrete-time Fourier transform is a special case


of the continuous-time Fourier transform for discretized signals, and
that x̂ (ω̂ ) is the frequency domain representation of the discrete-time
signal x [n].
The fact that the discrete-time Fourier transform is a Fourier
transform means that all the properties of a Fourier transform also
apply to the discrete-time Fourier transform. However, there are | D21 (ω̂ )|
1
some special properties of the discrete-time Fourier transform, which
we will go through.

Periodicity
ω̂
The DTFT x̂ (ω̂ ) is a 2π-periodic function: −2π −π π 2π

x̂ (ω̂ ) = x̂ (ω̂ + 2πk ) (608)


| D21 ( f )|

with k ∈ Z. This is relatively easy to show:


x̂ (ω̂ + 2πk) = ∑ x [n]e−i(ω̂ +2πk)n (609)
n=−∞


f
= x [n]e−iω̂n e|−i2πnk (610)
n=−∞
{z }
=1 − f s − f s /2 f s /2 fs

= x̂ (ω̂ ) . (611)

The periodicity property is the same one that we encountered Figure 140: A discrete-time Fourier
when investigating aliasing of complex sinusoidal discrete-time sig- transform is 2π-periodic in frequency
domain. The top figure shows the
nals, which are essentially frequency components of a discrete-time
magnitude response of the running
signal. Figure 140 shows one example of a discrete-time Fourier trans- average filter | D21 (ω̂ )| with frequency
form, the Dirichlet kernel, that we encountered when investigating in units of radians per sample on the
x-axis. Bottom: The same as the above,
the frequency response of a running average filter. but with x-axis with frequency in units
of cycles per second. The sampling-rate
f = ± f s /2 corresponds to ω̂ = ±π.
221

Inverse transform

The reverse operation is the inverse discrete-time Fourier transform,


which is defined as:

1
Z π
x [n] = x̂ (ω̂ )eiω̂n dω̂ . (612)
2π −π

This allows transforming a discrete-time frequency domain represen-


tation x̂ (ω̂ ) to a discrete-time signal x [n].
It is quite easy to show that this inverse formula recovers x [n] for a
DTFT x̂ (ω̂ ) that is defined as44 : 44
The inverse transform is nearly the
same as the Fourier series analysis
∞ equation with x [n] being the Fourier
x̂ (ω̂ ) = ∑ x [n]e−iω̂n , (613) series coefficients and x̂ (ω̂ ) being a
n=−∞ 2π-periodic function.

we can recover x [n] using the inverse transform formula:

1
Z π
F −1 { x̂ (ω̂ )} = x̂ (ω̂ )eiω̂n dω̂ (614)
2π −π

" #
1
Z π
=
2π −π
∑ x [m]e −i ω̂m
eiω̂n dω̂ (615)
m=−∞

1
Z π
=
2π ∑ x [m]eiω̂ (n−m) dω̂ (616)
m=−∞ −π

1
=
2π ∑ 2πδ[m − n] x [m] (617)
m=−∞

= x [n] (618)

Here F −1 {·} refers to the inverse DTFT.


We now have an inverse and forward discrete-time Fourier trans-
form pair:


1
Z π
F
x [n] =
2π −π
x̂ (ω̂ )eiω̂n dω̂ ←
→ x̂ (ω̂ ) = ∑ x [n]e−iω̂n . (619)
n=−∞

We’ll now list several discrete-time Fourier transform pairs. Note that
h[n] H (ω̂ )
many of these have in fact already been discussed in the chapter on
Time domain Frequency domain
the continuous-time Fourier transform.
Figure 141: The frequency response
H (ω̂ ) of an LTI system is the Fourier
Frequency response transform of the impulse response h[n].

The frequency response of a discrete-time LTI system (shown earlier


in Equation 529)

H (ω̂ ) = ∑ h[k ]e−iω̂k (620)
k =−∞
222

is a discrete-time Fourier transform of the impulse response h[n].


This means that all the properties of a discrete-time Fourier trans-
form will apply also to a frequency response. Using our short hand
notation, we can define this as a DTFT pair:

F
h[n] ←
→ H (ω̂ ) . (621)

The inverse DTFT formula will come in handy when, e.g., defining
LTI systems based on their frequency response characteristics, and
then obtaining the impulse response using the inverse transform.
This leads to, e.g., ideal filters, ideal frequency selective filters, and
arbitrary time shift filters. These are LTI systems that would be
difficult or impossible to derive in time domain.

Time-shifted unit impulse

DTFT for unit impulse is:

F
→ x̂ (ω̂ ) = e−iω̂n0 .
x [ n ] = δ [ n − n0 ] ← (622)

The proof is trivial.

Frequency shift eiω̂0 n x [n]

A shift in frequency domain corresponds to multiplication by a


complex exponential signal in time domain. If
F
x [n] ←
→ x̂ (ω̂ ) , (623)

then
F
eiω̂0 n x [n] ←
→ x̂ (ω̂ − ω̂0 ) . (624)
The proof is as follows. If we multiply the signal x [n] with a complex
exponential signal with frequency ω̂0 , we obtain a new signal y[n]:

y[n] = eiω̂0 n x [n] . (625)

If we DTFT this, we obtain:



ŷ(ω̂ ) = ∑ eiω̂0 k x [k]e−iω̂k (626)
k =−∞

= ∑ x [k]e−i(ω̂ −ω̂0 )k = x̂ (ω̂ − ω̂0 ) . (627)
k =−∞

Multiplying a time domain signal by a complex sinusoid with fre-


quency ω̂0 results in a frequency shift of the signal in frequency
domain.
223

This property is useful for creating a filter with a specific center


frequency. This property can also be used to shift the center fre-
quency of a signal in frequency domain, e.g., in digital up or down
conversion systems that are found in digital radios.

Convolution theorem

An often used theorem of signal processing is the convolution the- x [n] x [n]

orem. It states that convolution in time domain is multiplication in


frequency domain. This also applies to discrete-time Fourier trans- LTI (h1 [n]) LTI (h3 [n])

forms.
F LTI (h2 [n]) y[n]
a[n] = b[n] ∗ c[n] ←
→ â(ω̂ ) = b̂(ω̂ )ĉ(ω̂ ) . (628)
The proof is identical to the proof shown in the chapter on Fourier y[n]
transforms.
Figure 142: A consequence of the as-
sociative property of convolution is
Example: cascaded filters that two LTI systems characterized
with h1 [n] and h2 [n] can be combined
A consequence of the convolution theorem is that we can analyze the as a single LTI system with impulse
combined effect of a cascaded system, simply by multiplying together response h3 [n] = h1 [n] ∗ h2 [n]. The
convolution theorem allows us to inves-
the frequency responses. tigate the combined frequency response
Let’s assume that a signal is first convolved with a filter that has of the system using the following
an impulse response h1 [n] and then with a filter that has an impulse formula: H3 (ω̂ ) = H1 (ω̂ )H2 (ω̂ ).

response h2 [n]:

y[n] = h2 [n] ∗ (h1 [n] ∗ x [n]) (629)


= (h1 [n] ∗ h2 [n]) ∗ x [n] (630)
= h3 [ n ] ∗ x [ n ] . (631)

The combined frequency response H3 (ω̂ ) of the cascaded system is


H1 (ω̂ )H2 (ω̂ ):

F
h3 [ n ] = h1 [ n ] ∗ h2 [ n ] ←
→ H3 (ω̂ ) = H1 (ω̂ )H2 (ω̂ ) (632)

where H1 (ω̂ ) and H2 (ω̂ ) are the frequency responses corresponding


to impulse responses h1 [n] and h2 [n].
224

Exercises: Discrete-time Fourier Transform (DTFT) x [n] x [n]

1. Show that if x [n] ∈ R, then x̂ (ω̂ ) = x̂ ∗ (−ω̂ ).


LTI (h1 [n]) LTI (h4 [n])

2. Show that if x̂ (ω̂ ) ∈ R, then x [n] = x ∗ [−n].


LTI (h2 [n]) y[n]
3. Show that F −1 {42i } = 42iδ[n] using Equation 612. Here F −1 is
the inverse discrete-time Fourier transform. LTI (h3 [n])

4. Consider three systems: T1 { x [n]} = x [n] − x [n − 1], T2 { x [n]} =


x [n] + x [n − 2], and T3 { x [n]} = x [n − 1] + x [n − 2]. y[n]

a) What are the impulse responses for these three LTI systems: Figure 143: A cascade of LTI systems
defined by impulse responses h1 [n],
h1 [n] = T1 {δ[n]}, h2 [n] = T2 {δ[n]}, and , h3 [n] = T3 {δ[n]}? h2 [n], and h3 [n] is equivalent to a single
b) Show that the frequency response of the system y[n] = LTI system defined by an impulse
response h4 [n].
T1 {T2 {T3 { x [n]}}} is e−iω̂ − e−5iω̂ .

5. Inverse DTFT the following functions:

a) x̂ (ω̂ ) = 1 − 2e3iω̂
b) x̂ (ω̂ ) = 2e−3iω̂ cos(ω̂ )
sin(10.5ω̂ )
c) x̂ (ω̂ ) = e−42iω̂ sin(ω̂/2) .
Hint: Look at the derivation for the Dirichlet kernel.
225

Suggested solutions: Discrete-time Fourier Transform (DTFT)

1. Suppose x [n] is a real discrete-time signal, then by definition:



x̂ (ω̂ ) = ∑ x [n]e−iω̂n .
n=−∞

Apply complex conjugation to obtain:


∞ ∞
x̂ ∗ (ω̂ ) = ∑ ( x [n]e−iω̂n )∗ = ∑ x ∗ [n]eiω̂n .
n=−∞ n=−∞

Since the signal x [n] is real: x ∗ [n] = x [n]. Then evaluating at x̂ ∗ (ω̂ )
at −ω̂:

x̂ ∗ (−ω̂ ) = ∑ x [n]e−iω̂n ,
n=−∞

which is equal to x̂ (ω̂ ).

2. Suppose x̂ (ω̂ ) is a real signal, then consider:

1
Z π
x [n] = x̂ (ω̂ )eiω̂n dω̂.
2π −π

Conjugating:

1 1
Z π Z π
∗ i ω̂n ∗
x̂ [n] = ( x̂ (ω̂ )e ) dω̂ = x̂ ∗ (ω̂ )e−iω̂n dω̂,
2π −π 2π −π

and since x̂ (ω̂ ) is real, we obtain x̂ ∗ (ω̂ ) = x̂ (ω̂ ), then evaluating


x̂ ∗ [n] at −n gives:

1
Z π
x̂ ∗ [−n] = x̂ (ω̂ )eiω̂n dω̂,
2π −π

which is equal to x [n].

3. Let x̂ (ω̂ ) = 1, then the inverse discrete-time Fourier transform is:

1 iω̂n π
 
1 1 1
Z π Z π
x [n] = x̂ (ω̂ )eiω̂n dω̂ = eiω̂n dω̂ = e ,
2π −π 2π −π 2π in −π

which gives:

1 1
x [n] = (eiπn − e−iπn ) = sin(πn).
2πin πn
If n ̸= 0, then this is 0. If n = 0 we get, using L’Hôpital’s rule:

sin(πn)
lim = 1.
n →0 πn

Hence, this corresponds to a Dirac-delta δ[n], so by linearity we get


42iδ[n].
226

4. Consider three systems:

T1 { x [n]} = x [n] − x [n − 1],


T2 { x [n]} = x [n] + x [n − 2],
T3 { x [n]} = x [n − 1] + x [n − 2].
a) The impulse responses are given as hi [n] = Ti {δ[n]} for i =
1, 2, 3. We get:

h1 [ n ] = δ [ n ] − δ [ n − 1],
h2 [ n ] = δ [ n ] + δ [ n − 2],
h3 [ n ] = δ [ n − 1] + δ [ n − 2].

b) In time domain we have:

y[n] = T1 {T2 {T3 { x [n]}}},

which can be written as:

y [ n ] = h [ n ] ∗ x [ n ],

since this an LTI system, where:

h [ n ] = h1 [ n ] ∗ h2 [ n ] ∗ h3 [ n ].

In frequency domain, we have:

H(ω̂ ) = H1 (ω̂ )H2 (ω̂ )H3 (ω̂ ).


Each Hi is found by the discrete-time Fourier transform of
h i [ n ]:

H1 (ω̂ ) = 1 − e−iω̂ ,
H2 (ω̂ ) = 1 + e−2iω̂ ,
H3 (ω̂ ) = e−iω̂ + e−2iω̂ .
Then:

H(ω̂ ) = (1 − e−iω̂ )(1 + e−2iω̂ )(e−iω̂ + e−2iω̂ ) = e−iω̂ − e−5iω̂ ,


as we wanted to show.

5. a) If x̂ (ω̂ ) = 1 − 2e3iω̂ , then the IDTFT is:

x [n] = δ[n] − 2δ[n + 3].

b) If x̂ (ω̂ ) = 2e−3iω̂ cos(ω̂ ) then by rewriting the function as


follows:
1
x̂ (ω̂ ) = 2e−3iω̂ (eiω̂ + e−iω̂ ) = e−3iω̂ (eiω̂ + e−iω̂ ) = e−2iω̂ + e−4iω̂ ,
2
which gives that:

x [ n ] = δ [ n − 2] + δ [ n − 4].
227

sin(10.5ω̂ )
c) For the function x̂ (ω̂ ) = e−42iω̂ sin(ω̂/2) , we notice the similarity
to the Dirichlet kernel. Therefore, we start by rewriting the
function and do the Dirichlet calculation backwards.
 −10.5iω̂
− e10.5iω̂

−42i ω̂ e
x̂ (ω̂ ) = e .
e−iω̂/2 − eiω̂/2

We’ve flipped the sign in the numerator and denominator and


canceled the common 2i. Next, multiply by eiω̂/2 .
" #
i ω̂/2 e−10.5i ω̂ − e10.5i ω̂
−42i ω̂ e
x̂ (ω̂ ) = e ,
eiω̂/2 e−iω̂/2 − eiω̂/2
 −10iω̂
− e11iω̂

e
= e−42iω̂ ,
1 − eiω̂

This last expression is a geometric sum, with N = 10, giving:

10 10
x̂ (ω̂ ) = e−42iω̂ ∑ e−iω̂k = ∑ e−iω̂ (k+42) .
k =−10 k =−10

The IDTFT is then:


10
x [n] = ∑ δ[n − k − 42].
k =−10
Ideal and Tapered Filters

The inverse discrete-time Fourier transform can be used for filter


design purposes. It is possible to specify a filter in frequency domain
H(ω̂ ), and then use an inverse Fourier transform to obtain the im-
pulse response h[n] of the filter with these properties. However, this
often leads to filters that are infinitely long and therefore impossible
to realize. In practice, the issue of infinitely long filters can be over-
come by truncating the impulse response into one that has a finite
length.
This chapter will only discuss ideal and tapered windows in the
context of discrete-time signals. We will first go through several
important ideal filters and derive their impulse responses. We will
then discuss various strategies for truncating and tapering these into
finite length filters.

Ideal filters

It is possible to specify a filter in frequency domain. In many cases,


one knows what sort of frequency response is needed. The basic
recipe is to inverse DTFT the desired frequency response to obtain a
time domain impulse response. In this section, we’ll go through some
basic ideal filters.

Example: Ideal low-pass filter


H (ω̂ )
An ideal low-pass filter has the following spectral response:

(
1 |ω̂ | < ω̂0
HLP (ω̂ ) = . (633)
0 ω̂0 ≤ |ω̂ | ≤ π

In order to determine the filter coefficients, one needs to inverse

ω̂
−π − ω0 ω0 π

Figure 144: The frequency response of


an ideal low pass filter, which filters out
signals with normalized frequencies
larger than |ω̂ | > ω̂0
230

DTFT the frequency response:

1 πZ
h[n] = H (ω̂ )eiω̂n dω̂ (634)
2π −π LP
1
Z ω̂0
= eiω̂n dω̂ (635)
2π −ω̂0
ω̂0
1 eiω̂n
= (636)
2π in −ω̂0
1 1 iω̂0 n
= (e − e−iω̂0 n ) (637)
πn 2i
sin(ω̂0 n) h[n]
= . (638)
πn
The impulse response for this ideal filter is infinitely long, but it is
damped by a factor of 1/n. There is also an indeterminate value at
n = 0, which can be determined using L’Hôpital’s rule.
n

f (x) f ′ (x) −10 10


lim = lim ′ (639)
x →c g( x ) x →c g ( x )

sin(ω̂0 n) ω̂ cos(ω̂0 n)
lim = lim 0 (640) Figure 145: The impulse response of an
n →0 πn n →0 π
ideal low-pass filter.
ω̂0
= . (641)
π
Therefore, the ideal low-pass filter is defined as:

sin(ω̂0 n) F
h[n] = → H (ω̂ ) = u(ω̂ + ω̂0 ) − u(ω̂ − ω̂0 ) .
← (642)
πn

The impulse response for an ideal low-pass filter with ω̂0 = 0.4π is
shown in Figure 145.
The function sin(πx )/πx is often referenced to as the sinc-function:

sin(πθ )
sinc(θ ) = . (643)
πθ
Sometimes it is useful to express the ideal low-pass using a sinc
function, as numerical implementations of this function take into
account the limiting behavior at ω̂ = 0 automatically.

Example: Ideal high-pass filter


H (ω̂ )
An ideal high-pass filter with a cutoff at ω̂0 is defined as:
(
0 |ω̂ | < ω̂0
HHP (ω̂ ) = . (644)
1 ω0 ≤ |ω̂ | ≤ π

ω̂
−π − ω0 ω0 π

Figure 146: The frequency response of


an ideal high-pass filter.
231

To obtain the time domain impulse response h[n], we perform an


inverse DTFT, much in the same way as we did for the ideal low pass
filter:
1 π
Z
h[n] = H (ω̂ )eiω̂n dω̂ (645)
2π −π HP
Z −ω 
1 0
Z π
i ω̂n i ω̂n
= e dω̂ + e dω̂ (646)
2π −π ω0
1 h −iω̂0 n i
= e − e−iπn + eiπn − eiω̂0 n (647)
2πni
1
= [sin(πn) − sin(ω̂0 n)] . (648)
πn
Here we need to be careful when n = 0. While sin(πn)/πn = 0 when
n ̸= 0, when n = 0, using L’Hôpital’s rule, we get:

sin(πn)
lim =1. (649)
n →0 πn

Therefore, the impulse response of the ideal high-pass filter is:


H (ω̂ )
sin(ω̂0 n) F
h[n] = δ[n] − ←
→ H (ω̂ ) = 1 − [u(ω̂ + ω0 ) − u(ω̂ − ω0 )]
πn
(650)
which is another way of saying all but the ideally low pass filtered
signal.

Example: Ideal band-pass filter


ω̂
An ideal band-pass filter has a non-zero frequency response only on − π − ω1 − ω0 ω0 ω1 π
a narrow frequency range (passband). For a real-valued passband
filter, one needs to include the positive and negative frequency Figure 147: The frequency response of
an ideal band-pass filter.
passbands. The specification for an ideal band-pass filter that only
lets through real-valued signals with frequencies between ω0 and ω1
is: (
1 ω̂0 < |ω̂ | < ω̂1
HBP (ω̂ ) = . (651)
0 otherwise

The frequency response is shown in Figure 147.


This has the impulse response given by:

1 π
Z
h[n] = H (ω̂ )eiω̂n dω̂ (652)
2π −π BP
Z −ω̂ 
1 0
Z ω̂
1
= eiω̂n dω̂ + eiω̂n dω̂ (653)
2π −ω̂1 ω̂0
1 h −iω̂0 n i
= e − e−iω1 n + eiω1 n − eiω̂0 n (654)
2πni
sin(ω̂1 n) sin(ω̂0 n)
= − . (655)
πn πn
232

Again, using L’Hôpital’s rule, we obtain the behavior at n = 0:

h[0] = (ω̂1 − ω̂0 )/π . (656)

Similarly, it is possible to determine the impulse response for an


ideal band-stop filter, which has zero response between ω0 and ω1 ,
but lets through signals at all other frequencies. We leave this as an
exercise to the reader.

Example: Ideal point frequency filter


A filter, which is selective to only one frequency ω̂0 :

1 iω̂0 n F
h[n] = e ←→ H (ω̂ ) = δ(ω̂ − ω̂0 ) . (657)

Here δ( x ) is the Dirac delta function. The impulse response of such a


filter can be obtained using the IDTFT (for |ω0 | < π:

1 π
Z
h[n] = δ(ω̂ − ω̂0 )eiω̂n dω̂ (658)
2π −π
1 iω̂0 n
= e . (659)

This filter is constant in magnitude, i.e., |h[n]| = (2π )−1 , and


infinitely long. The filter is a complex sinusoid, with frequency
matched to the signal frequency ω̂0 .

Example: Real-valued point frequency filter


The real-valued single frequency filter is actually a filter that lets
through two frequencies. The positive and negative frequency compo-
nent ω̂0 and −ω̂0 .

1 F
h[n] = cos(ω̂0 n) ←
→ H (ω̂ ) = δ(ω̂ − ω̂0 ) + δ(ω̂ + ω̂0 ) . (660)
π

Proof.

1 π 1
Z Z π
h[n] = δ(ω̂ − ω̂0 )eiω̂n dω̂ + δ(ω̂ + ω̂0 )eiω̂n dω̂ (661)
2π −π 2π −π
1 iω̂0 n −i ω̂0 n
= (e +e ) (662)

1
= cos(ω̂0 n) . (663)
π

The ideal point-frequency impulse response will be later revisited


when dealing with discrete Fourier transforms as filter banks.
233

Fractional sample delay


h[n]
An important elementary filter is the time-shift filter, which has an 1
impulse response
h [ n ] = δ [ n − n0 ] . (664)
0.5
This filter delays the signal by n0 samples. We already determined
earlier that time-delay is equivalent to a linear phase slope in fre- n
quency domain: −10 10


H(ω̂ ) = ∑ δ[n − n0 ]e−iω̂n (665) −0.5
n=−∞
−i ω̂n0 Figure 148: Fractional sample delay
=e . (666)
filter impulse response h[n], which
delays the signal by half a sample. This
We can now use this to derive the impulse response of a fractional filter is infinitely long.
sample delay filter, which delays the signal by a real-valued number
of samples n0 ∈ R:

sin(π [n − n0 ]) F
h[n] = → H (ω̂ ) = e−iω̂n0 .
← (667)
π ( n − n0 )

For the proof, we need to evaluate the inverse DTFT of the frequency
domain specification of a time-shift H(ω̂ ) = e−iω̂n0 :

1 π
Z
x [n] = e−iω̂n0 eiω̂n dω̂ (668)
2π −π
1
Z π
= eiω̂ (n−n0 ) dω̂ (669)
2π −π
π
1 eiω̂ (n−n0 )
= (670)
2π i (n − n0 )
ω̂ =−π
1 1 iπ (n−n0 )
= (e − e−iπ (n−n0 ) ) (671)
π (n − n0 ) 2i
sin(π [n − n0 ])
= . (672)
π [ n − n0 ]

Figure 148 shows the impulse response of a filter that delays a signal
by n0 = 0.5, that is by half a sample. It would be difficult to define
this filter in time domain, but relatively straightforward using the
frequency domain specification, which is inverse discrete Fourier
transformed into time domain to obtain an impulse response.
It can be shown that for integer values n0 ∈ Z, the impulse
response simplifies to

sin(π [n − n0 ])
= δ [ n − n0 ] , (673)
π ( n − n0 )

but this would be easy to define already in time domain.


234

Tapered ideal filters

Ideal filters for discrete-time signals, as discussed in the discrete-time


Fourier transform chapter, are impossible to realize. This is because
ideal filters require an infinitely long impulse response h[n].
In practical applications, finite length filters have to be used. One
method for designing filters is to take these infinitely long ideal
filters and truncate these to get a filter with finite length. Approxi-
mating an ideal filter with an FIR filter can be accomplished by using
window functions or tapering windows. A window function is a func-
tion that is 0 outside an interval, so the effect of multiplying with
w R (n)
a window function is that the window function truncates the filter,
making it finite.
If h[n] is an ideal filter centered at n = 0, a windowed version of
this filter is obtained by multiplying the ideal filter with a window
function w(n) of length N:

hw [n] = w(n)h[n − N/2], (674)


n
After this, the non-zero amplitude portion of the impulse response −5 5 10 15
hw [n] is finite length and the filter hw [n] is a finite impulse response w H (n)
filter that can be applied in practice to a signal.
There are two main effects that you need to be aware of when
using Equation 674 to create a truncated finite length filter. Firstly,
there is a time delay of N/2 introduced by the windowing operation.
This results in a time shift when applying this filter. Secondly, due to
time-frequency ambiguity, this type of practical filter will have finite
spectral resolution. It is impossible to achieve a perfectly sharp filter n
cutoff using a practical filters. −5 5 10 15

Figure 149: Top: Rectangular window.


Rectangular window Bottom: Hann window. In both cases,
the length of the window function is
The most basic type of window function is the rectangular window. If N = 10.
the length of the filter is N, the window function evaluates to:
(
1 0 ≤ n ≤ N−1
w R [n] = (675)
0 otherwise.

This window function just selects N central points of an infinitely


long impulse response h[n]. The top panel of Figure 149 shows a
rectangular window function.

Tapered window

The window function can also be tapered in such a way that the am-
plitude of the window is smoothly reduced to zero near the edges of
235

the window. This results in better rejection of out of band signals, at


the expense of slightly broadening sharp frequency domain features.
One example of a good tapered window function is the Hann
window. A Hann window of length N is defined as:
(
sin2 (πn/N ) 0 ≤ n ≤ N − 1
w H [n] = (676)
0 otherwise.

A Hann window is shown on the bottom panel of Figure 149.


A selection of commonly used window function implementa-
tions can be found in the scipy.signal module. This includes an
implementation of the Hann window function45 . 45
The Wikipedia page on Window
functions https://en.wikipedia.org/
wiki/Window_function contains a
Example: Frequency selective filter relatively comprehensive visual catalog
of the frequency responses of different
tapering window functions.
The ideal point-frequency filter for a real-valued signal with fre-
quency ω̂0 was shown in the discrete-time Fourier transform chapter
to be: h R,ω̂0 [n]
h[n] = cos(ω̂0 n) (677)
This is an infinitely long filter, and therefore it is not possible to
implement this filter in practice. The windowed version of this filter
n
of length N in the case of a rectangular window function would be:
10 20
(
cos(ω̂0 (n − N/2)) 0 ≤ n ≤ N − 1
h R,ω̂0 [n] = (678)
0 otherwise.

and using the Hann window: h H,ω̂0 [n]


(
sin2 (πn/N ) cos(ω̂0 (n − N/2)) 0 ≤ n ≤ N−1
h H,ω̂0 [n] = (679)
0 otherwise.
n
The Python code in Listing 41 shows an implementation of a fre-
10 20
quency selective filter using a rectangular and Hann window. The
plot produced is shown in Figure 151.
The key difference between the rectangular windowed and the
Hann windowed filter is the rejection of signals outside the frequen-
cies that we are trying to filter. A rectangular filter has significantly
Figure 150: Top: Frequency selective
worse rejection of out-of-band frequencies compared to the Hann filter impulse response using a rect-
windowed filter. This is why usually tapered window functions are angular window. Bottom: Frequency
selective filter impulse response using a
used.
Hann window. In both cases, the length
import m a t p l o t l i b . pyplot as p l t of the window function is N = 22. The
import numpy as np window functions w(n) are shown in
# Commonly used window f u n c t i o n s a r e found i n red, and the filter impulse response in
# t h e s c i p y . s i g n a l . windows module . blue.
from s c i p y . s i g n a l . windows import hann
236

Figure 151: The impulse response and


magnitude response of a frequency
selective filter, which selects real-valued
signals with frequencies f 0 = 1 kHz.
Top: The impulse response of the
rectangular and Hann windowed
impulse response. Bottom: the power
response of the same filters. The Hann
tapered filter has significantly better
rejection of spectral components with
frequencies outside f 0 = 1e3. However,
the rectangular windowed filter has
a slightly narrower main lobe near
the 1 kHz frequency that the filter is
designed to pass through. Due to time-
frequency uncertainty, the filter has a
finite width.

# C a l c u l a t e t h e DTFT o f s i g n a l x using zero −padded FFT


# a t N evenly spaced p o i n t s between −\ p i and \ p i .
def d f t _ d t f t ( x , N, s a m p l e _ r a t e =1 e3 ) :
# f f t s h i f t s h i f t s t h e f r e q u e n c i e s so t h a t we o b t a i n
# normalized angular frequency between −\ p i t o \pi ,
# i n s t e a d o f 0 t o 2\ p i .
X = np . f f t . f f t s h i f t ( np . f f t . f f t ( x , N) )

# Normalized angular f r e q u e n c i e s .
f r e q s = np . f f t . f f t s h i f t ( np . f f t . f f t f r e q (N, d=1.0/ s a m p l e _ r a t e )
)

return X, freqs

# S i g n a l ( FIR f i l t e r c o e f f i c i e n t s ) .
s a m p l e _ r a t e = 10 e3
t = np . arange ( − 2 0 , 2 0 ) /s a m p l e _ r a t e
# Make a f i l t e r t h a t s e l e c t s 1 kHz s i g n a l s .
f 0 = 1 e3

# R e c t a n g u l a r windowed frequency s e l e c t i v e f i l t e r .
h_R = np . cos ( 2 . 0 * np . p i * f 0 * t )

# Hann windowed frequency s e l e c t i v e f i l t e r .


h_H = hann ( 4 0 ) * np . cos ( 2 . 0 * np . p i * f 0 * t )

# Frequency response o f both f i l t e r s .


H_R, f r e q s = d f t _ d t f t ( h_R , 1 0 0 0 , s a m p l e _ r a t e )
H_H, f r e q s = d f t _ d t f t ( h_H , 1 0 0 0 , s a m p l e _ r a t e )
plt . figure ( f i g s i z e =(8 , 5) )
p l t . subplot (211)
p l t . p l o t ( t , h_R , l a b e l = " R e c t a n g u l a r windowed " )
p l t . p l o t ( t , h_H , l a b e l = "Hann windowed " )
p l t . ylim ( [ − 1 . 5 , 1 . 5 ] )
p l t . x l a b e l ( " Time ( s ) " )
p l t . y l a b e l ( " Impulse response $h [ n ] $ " )
p l t . legend ( )
237

p l t . subplot (212)
p l t . p l o t ( f r e q s /1e3 , 1 0 . 0 * np . l o g 1 0 ( np . abs (H_R) * * 2 . 0 ) , l a b e l = "
R e c t a n g u l a r windowed " )
p l t . p l o t ( f r e q s /1e3 , 1 0 . 0 * np . l o g 1 0 ( np . abs (H_H) * * 2 . 0 ) , l a b e l = "Hann
windowed " )
p l t . x l a b e l ( " Frequency ( kHz ) " )
p l t . y l a b e l ( " Power response ( dB ) " )
p l t . legend ( )
p l t . ylim ( [ − 1 0 0 , 5 0 ] )
plt . tight_layout ()
p l t . s a v e f i g ( " f r e q _ f i l t e r . png " )
p l t . show ( )

Listing 41: 022_window_functions/frequency_selective.py

Example: Low-pass filter

In the discrete-time Fourier transform chapter, we derived the im-


pulse response for an ideal low-pass filter:

sin(ω̂0 n)
h[n] = . (680)
πn
This is an infinitely long filter, so the only way to implement this in
practice is to use a windowed version of the ideal impulse response:

sin(ω̂0 [n − N/2])
hw [n] = w(n) . (681)
π [n − N/2]

The Python code in Listing 42 shows how this is done in practice.


The program evaluates the impulse response of a Hann windowed
ideal low-pass filter and shows the magnitude response in dB scale.
The output of this program is shown in Figure 152. While this filter
does not perfectly remove spectral components with frequencies
outside the passband, it does a pretty decent job. For example, fre-
quencies ω̂ = ±π are reduced in power by a factor larger than 1012 .
import m a t p l o t l i b . pyplot as p l t
import numpy as np
from s c i p y . s i g n a l . windows import hann

om = np . l i n s p a c e ( −np . pi , np . pi , num=1000)

# Low pass f i l t e r c u t o f f frequency .


omc = 0 . 1 * np . p i

# Sample i n d i c e s f o r f i l t e r impulse response .


nn = np . arange ( − 1 0 0 , 1 0 0 )

# I d e a l low−pass f i l t e r impulse response with c u t o f f a t omc .


s i n c = np . s i n ( omc * nn ) /(np . p i * nn )
# Use L ’ H o p i t a l ’ s r u l e t o determine value a t nn = 0 .
s i n c [ nn == 0 ] = omc/np . p i

# Window f u n c t i o n and l e n g t h .
238

Figure 152: The impulse response and


power response of a windowed low-
pass filter. Top: the impulse response of
the ideal filter is shown in blue, and the
Hann windowed filter impulse response
is shown in green. Bottom: the power
response of the filter.

wf = np . z e r o s ( l e n ( nn ) )
wl = 100

# Hann window .
W = hann ( wl )
# Center window f u n c t i o n around 0 .

wf [ i n t ( l e n ( wf ) /2) − i n t ( wl /2) + np . arange ( wl ) ] = W

# Use a zero −padded DFT t o approximate t h e frequency response


# o f t h e windowed low−pass f i l t e r .
WX = np . f f t . f f t s h i f t ( np . f f t . f f t ( wf * s i n c , 4 0 9 6 ) )
# Vector o f f r e q u e n c i e s f o r DFT .
omw = np . l i n s p a c e ( −np . pi , np . pi , num= l e n (WX) )

# I d e a l frequency response ( boxcar ) .


X = np . z e r o s ( l e n (om) )
X [ : ] = 1e −120 # Very s m a l l number t o avoid t a k i n g a l o g o f 0 .
X [ np . abs (om) < omc ] = 1 . 0

# P l o t power s p e c t r a l response i n dB s c a l e f o r
# i d e a l and windowed f i l t e r s .
plt . figure ( f i g s i z e =(10 , 8) )
p l t . subplot (212)
p l t . p l o t (om, 1 0 . 0 * np . l o g 1 0 ( np . abs ( X ) * * 2 . 0 ) , l a b e l = " I d e a l " , c o l o r
= " blue " )
p l t . p l o t (omw, 1 0 . 0 * np . l o g 1 0 ( np . abs (WX) * * 2 . 0 ) , l a b e l = "Windowed" ,
c o l o r = " green " )
p l t . ylim ( [ − 1 3 0 , 3 ] )
p l t . legend ( )
p l t . x l a b e l ( r " $\hat {\omega } $ " )
239

p l t . y l a b e l ( r " $10\l o g _ { 1 0 } |H(\ hat {\omega } ) |^2$ " )


p l t . t i t l e ( " Magnitude response " )

# P l o t t h e impulse response o f i d e a l and windowed f i l t e r .


p l t . subplot (211)
p l t . stem ( nn , s i n c , " b " , markerfmt= " bo " , l a b e l = " I d e a l " )
p l t . p l o t ( nn , np . max ( s i n c ) * wf , l a b e l = "Window" , c o l o r = " green " )
p l t . stem ( nn , wf * s i n c , " g " , markerfmt= " go " , l a b e l = "Windowed" )
p l t . legend ( )
p l t . x l a b e l ( " $n$ " )
p l t . y l a b e l ( " $h [ n ] $ " )
p l t . t i t l e ( " Impulse response " )
plt . tight_layout ()
p l t . s a v e f i g ( " windowed_lpf . png " )
p l t . show ( )

Listing 42: 022_window_functions/windowed_lpf.py


240

Exercises: Ideal and Tapered Filters

1. Define the ideal band-stop filter as: 46 46


That is HBS (ω̂ ) = 1 − HBP (ω̂ ).
(
0 ω̂0 < |ω̂ | < ω̂1
HBS (ω̂ ) = . (682)
1 otherwise

Derive the impulse response for the band-stop filter.


2. Consider the band-stop filter you found in the previous exercise.
The goal of this exercise is to implement this filter in Python and
apply it to filter an audio file.
a) Let w[n] be a tapered window function, like the Hann window
of length N. Show that the windowed filter can be written as:

sin(ω̂0 (n − N/2)) sin(ω̂1 (n − N/2))


hw [n] = δ[n − N/2]w[n] + w[n] − w [ n ].
(n − N/2)π (n − N/2)π

b) Download the crappy.wav file from the link:


https://bit.ly/3CvE89s

The audio file contains a guitar playing, but it is impossible


to hear due to noise. Your task is to filter out this noise using
a band-stop filter. The noise in the signal is contained in a
frequency band between f 0 = 3 kHz and f 1 = 5 kHz, but try
to experiment with these to see what happens. Implement a
band-stop filter using Python to remove the noise and recover
the original audio. You can use the code shown in Listing 43 as
a starting point.

import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . i o . w a v f i l e as sw
import s c i p y . s i g n a l as s s

# Read t h e n o i s y s i g n a l .
t s = sw . read ( " crappy . wav" )
sr , crap = t s

# F i l t e r length .
N = 4000

# F r e q u e n c i e s o f n o i s e ( u n i t s o f Hz) .
f 0 = 3 e3
f 1 = 5 e3

# Choose a window f u n c t i o n .
w = ...
# ^ See t h e s c i p y . s i g n a l documentation f o r a l i s t o f window
functions .

# Implement t h e f i l t e r and apply i t here .


uncrap = . . . # Complete t h i s .
241

# S c a l e t o 0 . 9 , because 1 . 0 i s t h e maximum allowed by t h e . wav


f i l e format .
uncrap = 0 . 9 * uncrap/np . max ( np . abs ( uncrap ) )

# Save t h e f i l t e r e d audio f i l e .
sw . w r i t e ( " t e s t _ u n c r a p p y . wav" , sr , np . a r r a y ( uncrap , dtype=np .
float32 ) )

Listing 43: Band-stop filter starting point


242

Suggested solutions: Ideal and Tapered Filters


H (ω̂ )
1. Let the ideal band-stop filter be given as:
(
0 ω̂0 < |ω̂ | < ω̂1
HBS (ω̂ ) = .
1 otherwise

The impulse response can then be computed using the inverse


DTFT as follows (see Figure 153):

1 π
Z
h[n] = H (ω̂ )eiω̂n dω̂,
2π −π BS
ω̂
Z −ω̂ − π − ω1 − ω0 ω 0 ω 1 π
1 1 1
Z ω̂0 Z π
1
= eiω̂n dω̂ + eiω̂n dω̂ + eiω̂n dω̂,
2π −π 2π −ω̂0 2π ω̂1 Figure 153: The frequency response of
 −ω̂1  ω̂0  π an ideal stop-pass filter.
1 1 iω̂n 1 1 iω̂n 1 1 iω̂n
= e + e + e ,
2π in −π 2π in −ω̂0 2π in ω̂1
1   1   1  iπn 
= e−iω̂1 n − e−iπn + eiω̂0 n − e−iω̂0 n + e − eiω̂1 n ,
2inπ 2inπ 2inπ
1 1 1
= sin(πn) + sin(ω̂0 n) − sin(ω̂1 n),
nπ nπ nπ
sin(ω̂0 n) sin(ω̂1 n)
= δ[n] + − .
nπ nπ
As expected, the ideal stop-pass is just the opposite of an ideal
band-pass filter.

2. Continuing with the filter in the previous exercise.

a) The impulse response of the band-stop filter was shown in the


previous exercise to be:

sin(ω̂0 n) sin(ω̂1 n)
h[n] = δ[n] + − .
nπ nπ
A tapered version of this filter is then found by:

hw [n] = w(n)h[n − N/2],

where N is the length of the filter and the window function.


Putting all of this together, we obtain:

sin(ω̂0 (n − N/2)) sin(ω̂1 (n − N/2))


hw [n] = δ[n − N/2]w[n] + w[n] − w [ n ].
(n − N/2)π (n − N/2)π

b) An implementation of the band-stop filter is shown in Listing


44.
import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . i o . w a v f i l e as sw
from s c i p y . s i g n a l . windows import hann
243

# Read t h e n o i s y s i g n a l .
t s = sw . read ( " crappy . wav" )
sr , crap = t s

# F i l t e r length .
N = 4000

# F r e q u e n c i e s o f n o i s e ( u n i t s o f Hz) .
f 0 = 3 e3
f 1 = 5 e3

# Make t h e band a b i t wider t o h o p e f u l l y remove some


boundary e f f e c t s .
s f 0 = f 0 − 0 . 2 e3
s f 1 = f 1 + 0 . 2 e3

# C a l c u l a t e t h e d i s c r e t e −time f r e q u e n c i e s ( u n i t s o f rad /
sample ) .
om0 = 2 . 0 * np . p i * s f 0 / s r
om1 = 2 . 0 * np . p i * s f 1 / s r

# D e c l a r e t h e f i l t e r as 0 .
h = np . z e r o s (N)

# Use a Hann window .


w = hann (N)

n = np . arange (N)

# C r e a t e a mask t o compute t h e f i l t e r . Otherwise a zero −


division occurs .
mask = ( n ! = N//2)

# Compute t h e impulse response . Add t h e N//2 value


afterwards .
h [ mask ] = ( np . s i n ( om0 * ( n [ mask ] − N//2) ) / ( ( n [ mask ] − N//2)
* np . p i ) − np . s i n ( om1 * ( n [ mask ] − N//2) ) / ( ( n [ mask ] − N
//2) * np . p i ) ) *w[ mask ]
h [N//2] = ( 1 + ( om0 − om1 ) / np . p i ) * w[N//2]

# In frequency domain , we m u l t i p l y t h e f i l t e r with t h e


spectral representation .
# M u l t i p l i c a t i o n i n frequency domain correspond t o
c o n v o l u t i o n i n time domain
# t h e r e f o r e , convolve t h e band−s t o p f i l t e r and t h e s i g n a l .
uncrap = np . convolve ( crap , h , mode= " v a l i d " )
# ^ mode = v a l i d w i l l remove some boundary e f f e c t s .

# S c a l e t o 0 . 9 , because 1 . 0 i s t h e maximum allowed by t h e .


wav f i l e format .
uncrap = 0 . 9 * uncrap/np . max ( np . abs ( uncrap ) )

# Save t h e f i l t e r e d audio f i l e .
sw . w r i t e ( " t e s t _ u n c r a p p y . wav" , sr , np . a r r a y ( uncrap , dtype=np
. float32 ) )

Listing 44: Band-stop filter

In Figure 154 a plot of the impulse response for the band-stop


filter is shown, both in time and frequency domain. In time

Figure 154: Impulse response for the


band-stop filter
244

domain we see the Dirac delta in the middle and also the sinc
function behavior. For the frequency domain plot the filter
is close to what we would expect. Figure 155 shows that the
frequency components corresponding to the band between f 0
and f 1 have been reduced in power. This is what one would
except from a band-stop filter.

Figure 155: Frequency spectrum of the


filtered signal
Time-frequency Uncertainty Principle

A fundamental property of the Fourier transform is that the width


of a signal in time domain and in frequency domain are inversely re-
lated to one another. This relationship is known as the time-frequency
uncertainty principle:
∆ω∆t = γ . (683)
Here γ ∈ R is a real-valued constant, which is dependent on how the
“width” of a signal is defined in the time and frequency domain, ∆t
is the width of the signal in time domain, and ∆ω is the width of the ∆t ∆ω
same signal in frequency domain.
Time-frequency uncertainty is a general relationship that applies F


to Fourier transform pairs. The units don’t have to be time and
frequency. One such uncertainty rule in quantum physics is the
t ω
Heisenberg uncertainty principle which states that the width of the
momentum wave function is inversely proportional to the width of ∆t ∆ω

the position wave function.


F

Example: Gaussian signal


t ω
Figure 156 shows a graphical representation of what Equation 683
means using a Gaussian signal, which has the following Fourier Figure 156: Above: A narrow signal
in time domain is a wide signal in
transform pair: frequency domain. Below: A wide
r signal in time domain is a narrow
−αt2 F π − ω2 signal in frequency domain.
x (t) = e ←→ x̂ (ω ) = e 4α . (684)
α
If we define the width of a Gaussian to be x (∆t) = x (0)e−1 in time
domain and x̂ (∆ω ) = x̂ (0)e−1 in frequency domain, we find that
∆t = α−1/2 and ∆ω = 2α1/2 . In this case, our time-frequency
uncertainty rule would be:

∆ω∆t = 2 . (685)

What this relation means in practice can be seen in Figure 156. A


Gaussian signal that is narrow in time domain is wide in frequency
domain. Conversely, a signal that is wide in time domain is narrow
in frequency domain.
246

Time scaling

Recall the time scaling system from the chapter on Fourier trans-
forms, which adjusts the width of a signal using the constant α:

y(t) = x (αt) . (686)

The Fourier transform of a time scaled signal ŷ(ω ) is scaled inversely


in frequency:

F 1 ω
y(t) = x (αt) ←
→ ŷ(ω ) = x̂ . (687)
|α| α

What does scaling in time mean?


• compressing a signal in time with |α| > 1 will stretch the signal in Figure 157: A video deriving the
diffraction limit from the Fourier
frequency. transform relationship between an
aperture diameter D and the angular
• stretching a signal in time with |α| < 1 will compress the signal in resolution ∆ϕ https://youtu.be/
frequency. gKw46e4Ks4k.

Example: Diffraction limit


HLP (ω̂ )
One example of the time-frequency uncertainty is the diffraction limit

∆φ∆D = λ , (688)

which is encountered, e.g., in optics or in radio antenna theory.


It provides the theoretical limit for angular resolution ∆φ of an
aperture with diameter ∆D that collects electromagnetic waves. ω̂
Here λ is the wavelength of the electromagnetic waves observed by −π −ω̂0 ω̂0 π
∆ω̂ = 2ω̂0
the aperture. In this case, there is a Fourier transform relationship
between ∆φ and ∆D. A derivation of the diffraction limit using a
basic Fourier transform pair was shown in this video: https://youtu. Figure 158: Ideal low-pass filter with
be/gKw46e4Ks4k. cutoff ω̂0 specified in discrete-time
normalized angular frequency (radians
per sample). The width of this filter in
frequency domain is ∆ω̂ = 2ω̂0 .
Example: minimum length of a low-pass filter impulse response

What is the minimum length required for a low-pass filter, in order to


achieve a certain pass bandwidth? hLP [n]
Let’s say that we wanted to low-pass filter a discrete-time signal
with a cutoff frequency ω̂0 . Figure 158 shows the frequency response
of an ideal low-pass filter. In this case, ∆ω̂ = 2ω̂0 is a measure of how
wide the filter is in frequency domain. n
In the discrete-time Fourier transform chapter, we derived the − n0 n0
impulse response for the ideal low-pass filter to be:
∆n = 2n0 = 2π/ω̂0
sin(ω̂0 n)
hLP [n] = . (689)
πn
Figure 159: The impulse response of an
ideal discrete-time low-pass filter h[n].
The width of this filter in time domain
is ∆n = 2π/ω̂0 (samples).
247

This impulse response is shown in Figure 159.


We’ll define the time domain width using the approximate first
zero crossings of h[n]. This is denoted with ±n0 in Figure 159. We
find that n0 = ±π/ω̂0 . Note that because this is a discrete-time
signal, there might not be an actual sample that is zero valued and
the value n0 is the interpolated value where the impulse response
crosses zero. The total effective width of the filter in time domain is
then ∆n = 2π/ω̂0 .
Now that we know the width in time and frequency domain, we
can obtain the product:
∆ω̂∆n = 4π . (690)
This is the time-frequency uncertainty principle applied to filter
design. This tells us that in order to make a low pass filter with
bandwidth ∆ω̂, the impulse response of the filter (the length of the
FIR filter) needs to be at least ∆n ≥ 4π/∆ω̂ samples long.
Note that the value of this constant depends on the definition
of the width of the filter in time and frequency domain, and isn’t
necessarily exactly 4π. The only way to exhaustively characterize the
time and frequency domain properties of a signal is to investigate the
signal in time and frequency domain.
248

Exercises: Time-frequency Uncertainty Principle


h1 ( t ) h2 ( t )
1. The Hann window in continuous-time is defined as:
(
sin2 (πt/T ) when 0≤t≤T
h1 ( t ) = (691)
0 otherwise
t
Let’s define the time-domain width of this filter to be T. The Hann 0 T
window is shown in Figure 160.
Figure 160: Two impulse responses: 1) a
By using sin(θ ) = 2i1 (eiθ − e−iθ ), it is possible to show that the Hann window h1 (t) of length T (blue),
and 2) a rectangular window h2 (t) of
Fourier transform of the Hann window is: length T (red).
      
sin T2 ω 1 sin π − T
2 ω 1 sin π + T
2 ω T
H1 ( ω ) =  + +  e −i 2 ω
2 2π 2 2π
ω T − ω T + ω 0

10 log10 (|H|2 )
(692) −20
−40
A rectangular window h2 (t) of length T is also shown in Figure
−60
160. The Fourier transform of a rectangular window of length T is:
−80
 
−100
2 sin T2 ω T 0 ω2 ω1 9π
H2 ( ω ) = e −i 2 ω (693) ω
ω
Both of these filters can be considered to be low pass filters. It Figure 161: The magnitude responses
is impossible to unambiguously say that one of these has better for the Hann window (blue) and the
rectangular window (red). In both cases,
frequency domain properties. The rectangular window has a more the length of the filter is T = 1. Both are
narrow pass bandwidth in frequency domain, while the Hann shown in decibel scale.
window has significantly better rejection of high frequency signals
outside the pass band. Which one you should use will depend on
your application.

a) Show that Equation 692 is the Fourier transform of the signal in


Equation 691.
b) The largest value (peak) of the Hann window h1 (t) as shown in
Figure 160 is centered at time T2 . You can easily modify H1 (ω )
to obtain the frequency response of a Hann window, with the
peak of the impulse response centered at t = 0 in time. Show
how this can be done.
c) Study the magnitude responses for the two filters in decibel
scale by writing a program to plot 10 log10 (|H1 (ω )|2 ) and
10 log10 (|H2 (ω )|2 ). Use the frequency range −10π < ω < 10π
and assume that T = 1.
d) Define the widths of the two filters in frequency domain by
inspecting the smallest value of ω for which |H(ω )| = 0, i.e.,
the first null of the magnitude response. You can also use
Figure 161 to help you out. In the case of H2 (ω ), the first null
is at ω2 = 2π/T and the null-to-null width of the rectangular
249

filter is thus ∆ω2 = 2ω2 = 4π/T. From Figure 161, it appears


that the null-to-null width of the Hann filter in frequency
domain is twice that of the rectangular window ∆ω1 = 2∆ω2 .
Show that this is the case47 . 47
Note that the width of the passband
of a frequency symmetric low-pass filter
e) How long do the two filters need (in seconds) to be in order to in frequency domain is often given in
achieve a null-to-null pass bandwidth of ∆ω = 2π rad/s? terms of half-power width, not null-to-
null width. In most cases, this needs
f) Recall that if a complex sinusoidal signal to be numerically evaluated, so we
′ haven’t done this here. The half-power
x (t) = eiω t (695) width is defined based on the following
property:
is fed into an LTI system with impulse response h(t), the
1
output signal would be of the form: |H(ω ′ )|2 = |H(0)|2 (694)
2
′ ′ with the half-power width being
y(t) = |H(ω ′ )|ei∠H(ω ) eiω t (696) ∆ω1/2 = 2ω ′ .

Assuming that T = 1 s and ω ′ = 9π rad/s, what would the


amplitudes of the complex sinusoidal signals coming out of the
Hann and rectangular filters (|H1 (9π )| and |H2 (9π )|) be? You
don’t necessarily need to obtain an exact number, you can look
at Figure 161 and estimate it from there.
g) While the rectangular filter has a more narrow filter width in
frequency domain than a Hann window of the same length,
the Hann window is in most cases significantly better when it
comes to filtering out frequency components with frequencies
outside the passband. Explain why by comparing the mag-
nitude responses of the rectangular and Hann windows in
decibel scale, shown in Figure 161.

2. Consider the following function:


1
Ψ( x ) = √ [u( x + T ) − u( x − T )]. (697)
2T
The function |Ψ( x )|2 describes the probability density of finding a
particle at position x.
48
The unitary Fourier transform pair is
a) Show that:
defined as:
1 ∞Z
1
Z∞
Ψ̂(k) = √ Ψ( x )e−ikx dx, fˆ(ω ) = √ f (t)e−iωt dt
2π −∞ 2π −∞
Z ∞
1
1 sin(kT ) f (t) = √ fˆ(ω )eiωt dω
= √ . 2π −∞
Tπ k
This version of the Fourier transform
Here Ψ̂(k) is the unitary Fourier transform48 of Ψ( x ). The results in the following variant of
Plancherel’s theorem:
function |Ψ̂(k )|2 is the probability density function for the Z ∞ Z ∞
fˆ(ω ) ĝ∗ (ω )dω = f (t) g∗ (t)dt.
particle having the wavenumber k. If x has units of meters, −∞ −∞
then k has units of rad/m. In this case, the particle momentum This theorem was derived in the Fourier
and wavenumber are related as follows: p = h̄k with units of transform chapter (see Equation 278).

kg·m/s.
250

b) Define the width of the wave functions Ψ( x ) and Ψ̂(k ). 49 We’ll 49


A solution to the Schrödinger equa-
define the width of the wavefunctions in the following way: tion is called a wave function. If you
aren’t familiar with quantum me-
chanics, just think of it as an ordinary
∆x = min{ x : |Ψ( x )|2 = 0, x > 0}, function.

and
∆k = min{k : |Ψ̂(k )|2 = 0, k > 0},
or, in other words, ∆x and ∆k are the smallest positive values
for which the functions |Ψ( x )|2 and |Ψ̂(k)|2 are 0. Determine
∆x and ∆k for the functions Ψ( x ) and Ψ̂(k) and come up with a
relation between ∆x and ∆k.
c) Sketch the functions Ψ( x ) and Ψ̂(k ) and indicate ∆x and ∆k in
your drawing.
d) Show that: Z ∞
|Ψ̂(k)|2 dk = 1.
−∞
The meaning of this integral is that we are 100% certain that a
particle will have a wavenumber in the range −∞ < k < ∞.
e) Now consider another wavefunction that describes the position
of a particle:
Ψ ( x ) = δ ( x − x0 ), (698)
where x0 is a constant and x is the position of the particle.
In this case, you cannot say that one value of momentum or
wavenumber is more probable than another. Explain why, by
calculating the probability density of the particle wavenum-
ber |Ψ̂(k )|2 corresponding to Equation 698. Here Ψ̂(k) is the
unitary Fourier transform of Ψ( x ).
251

Suggested solutions: Time-frequency Uncertainty Principle

1. The Hann window in continuous-time is defined as:



sin2 (πt/T ), 0 ≤ t ≤ T,
h1 ( t ) =
0, otherwise.

Define the length of the Hann filter to be T in time-domain. The


Fourier transform of h1 (t) can be shown to be:
      
sin T2 ω 1 sin π − T
2 ω 1 sin π + T
2 ω T
H1 ( ω ) =  + +  e −i 2 ω .
2 2π 2 2π
ω T − ω T + ω

Let h2 (t) denote the rectangular window of length T, for which the
Fourier transform is:
 
2 sin T2 ω T
H2 ( ω ) = e −i 2 ω ,
ω
which also has length taken to be T.

a) Using the Fourier transform we have:


Z ∞ Z T 2
1
H1 ( ω ) = sin2 (πt/T )e−iωt dt = eiπt/T − e−iπt/T e−iωt dt,
−∞ (2i )2 0
Z Th
1 i
=− e−i(ω −2π/T )t − 2e−iωt + e−i(ω +2π/T )t dt.
4 0

Last handle each term on its own, the first term is computed as:
Z T
" #T
−i (ω −2π/T )t 1 −i (ω −2π/T )t 1
e dt = − 2π
e = 2π
(1 − e−i(ωT −2π ) ),
0 i (ω − T ) 0
i ( ω − T )
1
= 2π
(ei(ωT/2−π ) − e−i(ωT/2−π ) )e−i(ωT/2−π ) ,
i (ω − T )
  −1
2 T
z}|{
T
= sin ω − π e−iω 2 eiπ ,
ω − 2π
T
2
  
2 T T
= − 2π sin − π − ω e−iω 2 (−1),
T −ω
2
 
2 T T
= − 2π sin π − ω e−iω 2 ,
T −ω
2

the last step uses sin(−θ ) = − sin(θ ). The second term is:

1 −iωt T
Z T  
−iωt 1
e dt = − e = (1 − e−iωT ),
0 iω 0 iω
 
1 iωT/2 −iωT/2 −iωT/2 2 T T
= (e −e )e = sin ω e−iω 2 ,
iω ω 2
252

and finally, the same approach as the first gives:

Z T
" #T
−i (ω +2π/T )t 1 −i (ω +2π/T )t 1
e dt = − 2π
e = 2π
(1 − e−i(ωT +2π ) ),
0 i (ω + T ) 0
i (ω + T )
1
= 2π
(ei(ωT/2+π ) − e−i(ωT/2+π ) )e−i(ωT/2+π ) ,
i (ω + T )
  −1
2 T
z}|{
T
= sin ω + π e−iω 2 e−iπ ,
ω + 2π
T
2
 
2 T T
= − 2π sin π + ω e−iω 2 .
T + ω 2

Putting everything together with the correct signs and factors


we obtain:
      
sin ω T2 1 sin π − ω T2 1 sin π + ω T2 T
H1 ( ω ) =  + +  e −i 2 ω
2 2π 2 2π
T −ω T +ω
ω

as desired.
b) If the peak of the Hann window is at t = T/2, then we can shift
the peak to be at t = 0 as follows:
    
T π T π π π 
h ( t ) = h1 t − = sin2 t− = sin2 t− = cos2 t ,
2 T 2 T 2 T

as sin θ − π2 = − cos(θ ).
c) To plot the spectral responses, we can use Listing 45.
import m a t p l o t l i b . pyplot as p l t
import numpy as np

def H1( omega : np . ndarray ) −> np . ndarray :


" " " Frequency response f o r t h e Hann window with T = 1 .
"""
r e t u r n ( np . s i n ( omega/2)/omega + ( 1 / 2 ) * np . s i n ( np . p i −
( 1 / 2 ) * omega ) / ( 2 * np . p i − omega ) + ( 1 / 2 ) * np . s i n ( np . p i +
( 1 / 2 ) * omega ) / ( 2 * np . p i + omega ) ) * np . exp ( −1 j * 1 / 2 * omega )

def H2( omega : np . ndarray ) −> np . ndarray :


" " " Frequency response f o r t h e r e c t a n g u l a r window with T
= 1. """
r e t u r n 2 * np . s i n ( omega/2)/omega * np . exp ( −1 j * 1 / 2 * omega )

def c o n v e r t _ t o _ d e c i b e l ( x : np . ndarray ) −> np . ndarray :


r e t u r n 1 0 * np . l o g 1 0 ( np . power ( np . abs ( x ) , 2 ) )

# P a r t i t i o n t h e i n t e r v a l ( −10 pi , 10 p i ) i n t o 1000 e q u a l l y
spaced p o i n t s .
x = np . l i n s p a c e ( −10 * np . pi , 1 0 * np . pi , num=1000)
253

# P l o t t h e window f u n c t i o n s t o compare
# r a d i a n s per sample on t h e x− a x i s and dB on t h e y− a x i s .
p l t . p l o t ( x , c o n v e r t _ t o _ d e c i b e l (H1( x ) ) , l a b e l = "Hann window " )
p l t . p l o t ( x , c o n v e r t _ t o _ d e c i b e l (H2( x ) ) , l a b e l = " R e c t a n g u l a r
window " )
p l t . x l a b e l ( r " $\hat {\omega } $ " )
p l t . ylim ( − 1 2 0 , 1 0 ) # L i mi t t h e y− a x i s t o ( − 1 2 0 , 1 0 ) .
p l t . legend ( )
# C a l l t h i s i f needed .
# p l t . show ( )

Listing 45: Filter spectral response

The magnitude response is shown in Figure 162. The Figure


shows that the Hann window manages the rejection of frequen-
cies out of the band-pass better than the rectangular window.
d) By inspecting Figure 161 we conclude that the rectangular
window (in red) has its first zero at ω = ω2 , while the Hann
window (in blue) has the first zero at ω = ω1 . By investigating
the plot it appears that ω1 = 2ω2 , where ω2 = 2π/T as
Figure 162: Comparison of the spectral
given. The width of the rectangular window is then ∆ω2 =
responses of two filters
2ω2 = 4π/T, meaning that the width of the Hann window is
∆ω1 = 2ω1 = 2(4π/T ) = 2∆ω2 = 8π/T.
e) We’ve shown that:

∆ω1 = 4π/T,
∆ω2 = 8π/T,

so the rectangular window needs to be T = 2 seconds long,


while the Hann window needs to be T = 4 seconds long.
f) By Figure 161: the Hann window cuts the power to approxi-
mately −60 dB, which is around 10−6 , while the rectangular
window only cuts around −20 dB which corresponds to 10−2 ,
so not that much reduction. The Hann window does a far
better job in reducing the edges than the rectangular window.
g) As discussed in f) the Hann window is much better at handling
frequencies outside the band-pass. This can be seen from the
plot of the power of the magnitude response. Thus, the Hann
window can be used much more efficiently to reduce spectral
leakage which in many cases can be a huge problem, so the
trade-off when comparing filter width to spectral leakage,
reducing the leakage is preferred over reducing the filter width.

2. Let Ψ( x ) be given as:

1
Ψ( x ) = √ [u( x + T ) − u( x − T )].
2T
254

a) Using the unitary Fourier transform, we can directly compute it


as:
1 ∞Z
Ψ̂(k) = √ Ψ( x )e−ikx dx,
2π −∞
Z ∞
1
= √ √ [u( x + T ) − u( x − T )]e−ikx dx,
2π 2T −∞
Z T
1
= √ e−ikx dx,
4πT −T
1
= √ [eikT − e−ikT ],
2ik πT
1 sin(kT )
= √ ,
Tπ k

where we’ve used that sin θ = 1 iθ


2i ( e − e−iθ ).
b) Have that:
1
|Ψ( x )|2 = [u( x + T ) − u( x − T )],
2T
1 sin2 (kT )
|Ψ̂(k)|2 = .
Tπ k2
For |Ψ( x )|2 , we see that ∆x = T and for |Ψ̂(k)|2 , we get ∆k =
π/T. The time-frequency uncertainty can then be expressed as
Ψ( x ) = √1 [ u ( x + T ) − u ( x − T )]
π 2T
∆x∆k = ( T ) = π.
T
c) The wave functions Ψ( x ) and Ψ̂(k ) are shown in Figure 163 and
Figure 164, respectively.
d) By Parseval’s theorem, we have, in the unitary case: x
Z ∞ Z ∞ −T T
| x (t)|2 dt = | x̂ (ω )|2 dω. Figure 163: The wave function Ψ( x ) in
−∞ −∞ position space, with ∆x = T.
Using this, we get:
Ψ̂(k ) = √1 sin(kT )
Tπ k
Z ∞ Z ∞
1
|Ψ( x )|2 dx = [u( x + T ) − u( x − T )]dx,
−∞ −∞ 2T
Z T
1
= dx = 1,
2T −T k
− πT π
thus, by the unitary form of Parseval’s theorem, we get: T

Z ∞ Z ∞ Figure 164: The wave function Ψ̂(k) in


2 2 wavenumber space, with ∆k = πT .
|Ψ̂(k)| dk = |Ψ( x )| dx = 1.
−∞ −∞

e) If the wavefunction for position is given as δ( x − x0 ), then the


wavefunction for the wavenumber (or momentum) is given by
the unitary Fourier transform as:
Z ∞
1 1
Ψ̂(k ) = √ δ( x − x0 )e−ikx dx = √ e−ikx0 .
2π −∞ 2π
255

Then the modulus of the wavefunction is proportional to the


probability distribution for the wavenumber, so that:

1 1 1
|Ψ̂(k)|2 = √ e−ikx0 √ eikx0 = .
2π 2π 2π

This shows that all wavenumbers are equally likely, so no, we


cannot say that any wavenumber/momentum is more probable
than any other.
Discrete Fourier Transform
Fourier Series
∞ 2πk t
x (t) = ∑ c k ei T

In this chapter, we will introduce the discrete Fourier transform (DFT). k=−∞
1
Z
2πk t
We will also briefly discuss the fast Fourier transform (FFT) algorithm, ck = x ( t ) e −i T dt
T T
which is a very efficient numerical algorithm that can be used to Periodic in time:
evaluate a DFT. x (t + T ) = x (t)

The importance of the DFT and the FFT algorithm cannot be suf- Fourier TransformZ ∞
ficiently stressed. Its use is ubiquitous in modern signal processing. 1
x (t) = x̂ (ω )eiωt dω
2π −∞
Much of the technology that you use in your everyday life is de- Z ∞
x̂ (ω ) = x (t)e−iωt dt
pendent on this algorithm. Whenever you are watching a video or −∞

displaying an image on your computer screen, listening to digital Discrete-time Fourier Transform
1
Z
music, or using a wireless internet connection, you are with a high x [n] = x̂ (ω̂ )eiω̂n dω̂
2π 2π
likelihood using technology that relies on the DFT implemented ∞
using the FFT algorithm. x̂ (ω̂ ) = ∑ x [n]e−iω̂n
n=−∞
The DFT is widely used in practical signal processing applications, Periodic in frequency:
which are impossible to exhaustively list here. For example, the DFT x̂ (ω̂ ) = x̂ (ω̂ + 2π )

can be used: Discrete Fourier Transform


1 N −1 2π

N k∑
• to approximate a Fourier transform x [n] = x̂ [k]ei N kn
=0
N −1 2π kn
• to approximate a discrete-time Fourier transform, x̂ [k ] = ∑ x [ n ] e −i N
n =0

• for spectral analysis of signals, Periodic in time and frequency:


x [n + N ] = x [n]
x̂ [k + N ] = x̂ [k ]
• to efficiently implement a convolution of two signals,
Figure 165: A summary of all the
• in spectral compression algorithms (e.g., the MP3 and JPEG sound frequency domain representations of
and image formats), a signal covered so far in this course.
The discrete Fourier transform will be
covered in this chapter.
• to interpolate and re-sample signals,

• for filtering signals,

• for modulation and demodulation of radio signals in telecommuni-


cations, or

• to solve differential equations (e.g., Poisson’s equation).

This is just a small fraction of the applications of the DFT and the
FFT algorithm.
258

Derivation of the discrete Fourier transform


xd (t) Ts
The DFT is a frequency domain representation for periodic discrete- x [0] x [0]

time signals x [n] = x [n + N ]. Because the time domain signal is x [1] x [1] x [1]
x [2] x [2] x [2]
discrete-time and periodic, it follows that the frequency domain
representation is also a periodic discretized signal x̂ [k] = x̂ [k + N ]. t
Let’s see why. −3Ts − Ts Ts 2Ts 3Ts 4Ts 5Ts
Consider the following continuous-time signal, which has a period NTs
of NTs :
∞ N −1 Figure 166: A continuous-time rep-
xd (t) = ∑ ∑ x [n]δ(t − nTs − pNTs ) . (699) resentation of a periodic discretized
p=−∞ n=0 signal. In this example, the period of
the discrete-time signal is N = 3, which
This is a continuous-time representation of a periodic discrete-time means that x [n] = x [n + 3]. The sample-
signal x [n] with period N, sampled with sample-spacing Ts = 1/ f s . spacing is Ts , which implies that the
period of this signal in continuous-time
Here f s is sample-rate. The index p is used to create infinitely many is NTs .
copies of the signal with time offsets of pNTs . I’ve made an example
plot of this type of signal in Figure 166. In the example N = 3, which
means that xd (t) = xd (t + 3Ts ).
Because this signal is periodic, we can use the Fourier series to
represent this signal as a sum of complex sinusoidal signals:
∞ 2π

xd (t) = ∑ ck ei NTs kt = ∑ ck eiωk t . (700)
k =−∞ k =−∞

We can use the Fourier series analysis formula to determine the


coefficients ck :
Z NT
1 S 2π
ck = xd (t)e−i NTs kt dt (701)
NTs 0

!
Z NT N −1
1 S 2π
=
NTs 0
∑ ∑ x [n]δ(t − nTs − pNTs ) e−i NTs kt dt (702)
p=−∞ n=0
N −1 Z NT
1 S 2π
=
NTs ∑ x [n]
0
δ(t − nTs )e−i NTs kt dt (703)
n =0
N −1
1 2π
=
NTs ∑ x [n]e−i NTs knTs (704)
n =0
N −1
1 2π
=
NTs ∑ x [n]e−i N kn (705)
n =0
1
= x̂ [k] . (706)
NTs

The last term


N −1 2π
x̂ [k ] = ∑ x [n]e−i N kn , (707)
n =0

is the discrete Fourier transform. It is up to a constant 1/NTs , the


Fourier series coefficient that corresponds to the spectral component
259

with angular frequency

2π NTs
x̂d (ω )
ωk = k , (708) 2π ∆ω
x̂ [0] x̂ [0]
NTs
x̂ [1] x̂ [1] x̂ [1]
in a Fourier series representation of xd (t). This equation can be used x̂ [2] x̂ [2] x̂ [2]
to relate the kth spectral component to continuous-time angular ω
frequency. −3∆ω −∆ω ∆ω 3∆ω 5∆ω
By inspecting Equation 707, it is easy to see that x̂ [k] = x̂ [k + N ]. ωs
This means that the Fourier series coefficients repeat every N points.
We can Fourier transform xd (t) to obtain the frequency domain Figure 167: The spectral representation
of a periodic discretized signal x̂d (ω ) =
representation:
x̂d (ω + ωs ), multiplied by NTs /2π. In
Z ∞ this example, N = 3. The frequency
x̂d (ω ) = xd (t)e−iωt dω (709) step between unit impulses is ∆ω = ωNs .
−∞

Z ∞
!
=
−∞
∑ ck eiωk t e−iωt dω (710)
k =−∞
∞ Z ∞
= ∑ ck
−∞
ei(ωk −ω )t dω (711)
k =−∞


=
NTs ∑ x̂ [k ]δ (ωk − ω ) (712)
k =−∞
∞ N −1

=
NTs ∑ ∑ x̂ [k]δ (ωk − ω − pωs ) . (713)
p=−∞ k =0

In the last line, I have emphasized the fact that x̂ [n] is a periodic
signal with period N. Periodicity of x̂ [k ] implies that x̂d (ω ) is also
periodic x̂d (ω ) = x̂d (ω + ωs ) with period ωs = 2π f s .
An example plot of x̂d (ω ) scaled by NTs /2π is shown in Figure
167. In this example N = 3, which means that x̂ [k] = x̂ [k + 3].
By comparing equations 713 and 699, we can see that both are
similar continuous-time representations of a periodic discrete signal.
One is a periodic discrete-time signal x [n] = x [n + N ] and the other
is a periodic discrete-frequency signal x̂ [k] = x̂ [k + N ]. The period of
both signals is N.
260

Discrete Fourier Transform

The discrete Fourier transform (DFT) is defined as:

N −1 2π
x̂ [k] = F D { x [n]} = ∑ x [n]e−i N nk . (714)
n =0

It is used to obtain the complex amplitude of the spectral compo-


nents for normalized angular frequencies50 : 50
With Python, you can use the
numpy.fft.fftfreq command to
2π calculate the continuous-time frequency
ω̂k = k . (715) f k corresponding to each frequency
N component x̂ [k].

We use the notation x̂ [k ] to denote the discrete nature of the spectral


representation.
The inverse operation, i.e., the operation that converts the fre-
quency domain representation x̂ [k] into a time domain representation
x [n] is the inverse discrete Fourier transform (IDFT), which is defined
as:
1 N −1 2π

N k∑
−1
x [n] = F D { x̂ [k]} = x̂ [k]ei N nk . (716)
=0

The term 1/N is a required normalization constant, which is by


convention typically attached to the inverse transform.
−1
We’ll prove the reversibility of the transform x [n] = F D {F D { x [n]}}
by expressing the DFT using orthogonal basis vectors ψk .
ψk

Orthogonal basis vectors


2π ψℓ
Let’s first define a phase constant ϕ = e−i N ∈ C, which implies that
2π Figure 168: The discrete Fourier trans-
ϕkn = e−i N kn , (717) form basis vectors are orthogonal
ψk ⊥ ψℓ for all ℓ ̸= k. This implies that
we can use this to define basis vectors ψk ∈ C N ×1 as: ψkH ψℓ = 0 when ℓ ̸= k.

h iT
ψk = ϕk·0 ϕ k ·1 ϕ k ·2 · · · ϕk·( N −1) . (718)

All N unique basis vectors orthogonal with one another, i.e., the inner
product is:
N −1 2π
ψkH ψℓ = ∑ ei N (k−ℓ)n (719)
n =0
= Nδk,ℓ . (720)

I’ve used the Kronecker delta δk,ℓ here, which is defined as:
(
1 k=ℓ
δk,ℓ = . (721)
0 k ̸= ℓ
261

The proof is divided into two cases. First, when k = ℓ:

N −1 2π
N −1
ψkH ψk = ∑ ei N (k −k )n = ∑ e0 = N . (722)
n =0 n =0

and when k ̸= ℓ

N −1 2π
ψkH ψℓ = ∑ ei N n(k−ℓ) = S . (723)
n =0

We can use a geometric series to solve for S:

S = α 0 + · · · + α N −1
αS = α1 + · · · + α N
S − αS = 1 − α N
1 − αN
S= .
1−α

With α = ei N (k−ℓ) , we obtain:
N −1 2π 1−1
∑ ei N (k−ℓ)n = 2π =0. (724)
n =0 1 − ei N (k−ℓ)

This proves that the discrete Fourier transform basis vectors ψk are
orthogonal.

Reversibility of the transform


Let’s define a vector x̂ that contains the N spectral components x̂ [k]
obtained using a DFT:
h iT
x̂ = x̂ [0] x̂ [1] x̂ [2] · · · x̂ [ N − 1] ∈ C N ×1 . (725)

We also define a vector x, which contains the N elements of the


periodic signal x [n]:
h iT
x = x [0] x [1] x [2] · · · x [ N − 1] ∈ C N ×1 . (726)

Finally, let’s define a matrix F ∈ C N × N by stacking the N basis


vectors ψk :

ψ0T ϕ 0·0 ϕ 0·1 ϕ 0·2 ϕ0·( N −1)


   
···
 ψ T   ϕ 1·0 ϕ 1·1 ϕ 1·2 ··· ϕ1·( N −1) 
 1   
ψ   ϕ 2·0 ϕ 2·1 ϕ 2·2 ϕ2·( N −1)
 T  
···

F=
 2 =
 .
 ..   .. .. ..

.

 .   . . 
T
ψN − 1 ϕ ( N − 1)·0 ϕ( N −1)·1 ϕ( N −1)·2 · · · ϕ( N −1)·( N −1)
262

Using this matrix, we can express the forward and reverse discrete
Fourier transforms as matrix-vector products:

−1 D F
x [n] = F D { x̂ [k]} ←→ x̂ [k] = F D { x [n]} (727)
1 H FD
x= F x̂ ←→ x̂ = Fx . (728)
N
We can now prove reversibility. A combination of a forward and
−1
reverse transform x [n] = F D {F D { x [n]}} is equivalent to:

1 H
x= F Fx . (729)
N

We can see that F H F = N I is an identity matrix multiplied with N:

ψ0H ψ0 ψ0H ψ1 ψ0H ψ2 ψ0H ψN −1


 
···
 ψH ψ ψ1H ψ1 ψ1H ψ2 ··· ψ1H ψN −1 
 1 0 
 H
H ψ2H ψ1 ψ2H ψ2 ··· ψ2H ψN −1  = N I .

F F= ψ ψ
 2 0
.. .. ..

.
 
 . . 
H ψ
ψN H ψ
ψN H ψ
ψN ··· H ψ
ψN
−1 0 −1 1 −1 2 −1 N −1
(730)

This means that a combination of a forward and inverse transform


results in the original discrete-time signal x:
x [n]
1 H
x= F Fx (731)
N
=x . (732) 1

This proves the reversibility of the transform.

Example: Periodic unit impulse


n
Let us consider a periodic discrete-time signal { x [n]}nN=−01 = {1, 0, 0, 0}
−3 −2 −1 1 2 3 4 5 6
of length N = 4. This signal is periodic, so x [n + N ] = x [n]. The signal
x̂ [k ]
is shown in Figure 169.
The DFT of this signal is:
1
3 2π kn
x̂ [k ] = ∑ x [ n ] e −i 4 (733)
n =0
=1. (734)

The spectrum x̂ [k] is plotted in the bottom half of Figure 169. If we


now take the inverse DFT of signal x̂ [k ], we get: k
−3 −2 −1 1 2 3 4 5 6
1 3 2π
x [n] = ∑ ei 4 kn . (735) Figure 169: A periodic unit impulse in
4 k =0
time domain results in a constant val-
ued frequency domain representation.
263

For different values of n, we get:

1 i 2π 0·0 2π 2π 2π 1
x [0] = (e 4 + e i 4 1·0 + e i 4 2·0 + e i 4 3·0 ) = (1 + 1 + 1 + 1) = 1,
4 4
1 2π 2π 2π 2π 1
x [ 1 ] = ( e i 4 0·1 + e i 4 1·1 + e i 4 2·1 + e i 4 3·1 ) = (1 + i − 1 − i ) = 0,
4 4
1 i 2π 0·2 2π 2π 2π 1
x [2] = ( e 4 + e i 4 1·2 + e i 4 2·2 + e i 4 3·2 ) = (1 − 1 + 1 − 1) = 0,
4 4
1 i 2π 0·3 2π 2π 2π 1
x [3] = ( e 4 + e i 4 1·3 + e i 4 2·3 + e i 4 3·3 ) = (1 − i − 1 + i ) = 0.
4 4
Which is the original signal x [n].

Relationship to the discrete-time Fourier transform

The DTFT of a discrete-time signal is:



x̂ (ω̂ ) = ∑ x [n]e−iω̂n . (736)
n=−∞

This is not a DFT, as x̂ (ω̂ ) is a continuous function of ω̂, whereas x̂ [k]


is discrete in frequency.
For a finite-length signal x [n] = 0 when n > N − 1 and x [n] = 0
when n < 0, the discrete-time Fourier transform is:
N −1
x̂ (ω̂ ) = ∑ x [n]e−iω̂n . (737)
n =0

Recall now the definition of the DFT:


N −1 2π
x̂ [k ] = ∑ x [n]e−ik N n (738)
n =0
= x̂ (ω̂k ) . (739)

This means that the DFT is the DTFT evaluated at discrete points:


ω̂k = k . (740)
N

The DFT can be seen as a special case of the DTFT. Note that nothing
about periodicity of signal x [n] was assumed. We just assumed that it
was finite in length.

Zero-padded DFT

This immediately suggests a procedure for evaluating a DTFT x̂ (ω̂ )


on M evenly spaced frequencies {0, 2π 4π 6π 2π
M , M , M , · · · , ( M − 1) M } using
a DFT, where M > N is an integer larger than N, the length of the
finite length discrete-time signal x [n].
264

We can do this with a zero-padded DFT. Zero padding means that


we define a zero-padded signal xzp [n] of length M, which has values
(
x [n] 0 ≤ n < N
xzp [n] = . (741)
0 N≤n<M

We then perform an M-point DFT:

M −1 2π
x̂zp [k ] = x̂ (ω̂k ) = ∑ xzp [n]e−i M kn . (742)
n =0

This means that we can evaluate numerically the DTFT of a finite


length signal at M evenly spaced points along the frequency axis. We
can select M freely, to allow for arbitrary spacing of frequencies at
which the DTFT is evaluated.

Example: Zero-padded DTFT estimate using DFT


The Python code in Listing 46 demonstrates how to estimate the
DTFT of a signal using a DFT. In this example, we are estimating the
DTFT of the running average filter,
L −1
1
y[n] =
L ∑ x [n − k] , (743)
k =0

which is an FIR filter with filter coefficients:


L −1
1
h[n] =
L ∑ δ[n − k] . (744)
k =0

The filter has an analytic DTFT that was found to be:

1 1 − e−iω̂L
H(ω̂ ) = . (745)
L 1 − e−iω̂
The Python program will first calculate the DTFT of the filter h[n]
using the analytic formula. It will then calculate the values of the
DTFT at discrete points by using a zero-padded DFT (this is done
using the numpy.fft.fft function). The output of the program is
shown in Figure 170. The solid line shows the analytic magnitude
response, and the step plot shows the values estimated using a DFT.
By increasing the value of N, it is possible to increase the number of
points that are sampled.
import m a t p l o t l i b . pyplot as p l t
import numpy as np

# C a l c u l a t e t h e DTFT o f s i g n a l x using zero −padded DFT


# a t N evenly spaced p o i n t s between −\ p i and \ p i .
Figure 170: An estimate of the discrete-
time Fourier transform evaluated using
a DFT.
265

def d f t _ d t f t ( x , N) :
# The N parameter determines t h e l e n g t h o f t h e DFT .
# I s N > l e n ( x ) , then t h e s i g n a l x i s zero padded .
X = np . f f t . f f t ( x , N)
# Normalized angular frequency s t e p .
dom = 2 . 0 * np . p i/N
# Normalized angular f r e q u e n c i e s .
om_dft = np . arange (N) * dom
r e t u r n ( X , om_dft )

def he (om, L=20) :


# A n a l y t i c DTFT o f a c a u s a l running average f i l t e r .
r e t u r n ( ( 1 . 0 / L ) * ( 1 − np . exp ( −1 j *om* L ) ) /(1 − np . exp ( −1 j *om) ) )

# C a l c u l a t e using a n a l y t i c formula t h e DTFT o f t h e running


average f i l t e r .
om = np . l i n s p a c e ( 0 , 2 * np . pi , num=1000)

# DTFT o f running average f i l t e r by e v a l u a t i n g using a n a l y t i c


formula
H = he (om, L=20)

# S i g n a l ( FIR f i l t e r c o e f f i c i e n t s ) .
x = np . r e p e a t ( 1 . 0 / 2 0 . 0 , 2 0 )

X , om_dft = d f t _ d t f t ( x , 2 5 6 )

# Zero padded s i g n a l .
x_zp = np . z e r o s ( 2 5 6 )
x_zp [ : 2 0 ] = x
p l t . stem ( x_zp )
p l t . show ( )

plt . figure ( f i g s i z e =(6 , 4) )


p l t . p l o t (om, np . abs (H) , c o l o r = " C0 " , l a b e l = " A n a l y t i c " )
p l t . stem ( om_dft , np . abs ( X ) , l i n e f m t = " C1− " , markerfmt= " C1o " ,
basefmt= " C1 " , l a b e l = "DFT" )
p l t . legend ( )
p l t . t i t l e ( "N=256 " )
p l t . x l a b e l ( r " $\hat {\omega } $ " )
p l t . y l a b e l ( r " $|\mathcal {H} ( \ hat {\omega } ) |$ " )
p l t . s a v e f i g ( " d t f t _ e s t i m a t e . png " )
p l t . show ( )

Listing 46: 020_dtft_estimate/dtft_estimate.py

Fast Fourier Transform

In practical applications, one should rarely naively follow Equation


746 to implement a DFT operation, because it is not a very efficient
way to numerically evaluate this operation for N frequencies. There
exists a much faster algorithm to calculate this operation called the
Fast Fourier Transform (FFT), which is mathematically equivalent
to Equation 746. The algorithm is fast, because it subdivides a long
266

Fourier transform into shorter discrete Fourier transforms in a tree-


like fashion.
The DFT as implemented using the mathematical formula (here

ϕN = e−i N ):
N −1
x̂ [k] = ∑ x [n]ϕnk
N , (746)
n =0

would require O( N 2 ) mathematical operations. The FFT algorithm


manages to perform mathematically the same operation by using
only O( N log2 N ) operations51 .
The FFT algorithm makes use of the fact that you can divide a
Figure 171: The FFT speeds up the
length N DFT into two length N/2 DFTs: calculation by recursively subdividing
an N length vector in half until the
N −1 length of a vector has length 2. Credit:
x̂ [k] = ∑ x [n]ϕnk
N (747) Virens (Wikimedia).
n =0 51
This is big O notation from computer
N/2−1 N/2−1 science, which is used to describe
= ∑ x [2n]ϕnk
N/2 + ϕ N ∑ x [2n + 1]ϕnk
N/2 . (748) how the number of computations
n =0 n =0 grows as a function of input size.
O( N 2 ) means that when the input size
A simple implementation of the first line takes N 2 operations, as grows by a factor of 10, the number of
computations required will grow by a
there are N operations are needed for each of the N frequencies. The factor of 100.
second line is mathematically equivalent to the first line, but it takes
only N 2 /2 operations, as there are two sums that have N/2 unique
frequencies and sum over N/2 terms (2N 2 /4 = N 2 /2 operations).
This subdivision of signals can be repeated until the length of
each DFT is 2. Each time the vectors are split in half, there is a reduc-
tion by a factor of two in the number of operations that are needed.
A length N signal can thus be split in half, approximately log2 N
times. Without going into further details, the end result is a N log2 N
computational complexity.
Because the DFT is such a widely used and important algorithm, Figure 172: The FFTW is included in
much effort has been spent to optimizing it. Every scientific comput- many programming environments.
In many situations, it is the fastest
ing environment includes one or more implementations of the FFT implementation of the FFT algorithm.
algorithm. One of the most popular libraries is FFTW. In Python, the
PyFFTW module provides access to this library. There also exist GPU
optimized libraries, such as CuFFT, which allows NVidia graphics
acceleration cards to be used to calculate FFTs. In Python, one can
also use the numpy.fft and scipy.fftpack modules, although neither
of them is as efficient numerically as PyFFTW.

Convolution

A periodic convolution is defined as:

N −1
a[n] = ∑ b[k]c[(n − k ) mod N ] . (749)
k =0
267

In the case of DFT, multiplication in frequency domain is equivalent


to periodic convolution (denoted with symbol ⊛):

D F
a[n] = b[n] ⊛ c[n] ←→ â[k] = b̂[k ]ĉ[k] . (750)

In order to implement a non-periodic convolution of two finite


signals of length N and M, zero padding to length N + M is needed.
This procedure is widely used to allow FFT to be used to compute a
convolution. The Scipy function scipy.signal.fftconvolve can be
used to convolve two signals using the FFT algorithm. It first calcu-
lates b̂[k] and ĉ[k], then multiplies them together, and finally performs
an inverse Fourier transform to obtain the convolved signals.

Example: Convolution using zero-padded FFT


The Python code in Listing 47 provides an example of how to im-
plement a convolution in frequency domain using a zero-padded
FFT. Because the FFT algorithm is so efficient numerically, it is often
advantageous to implement the convolution operation using FFT.
Figure 174 shows the output of the Python script.
# Use o f FFT t o implement a l i n e a r c o n v o l u t i o n
# zero −padding used t o avoid p e r i o d i c i t y .
import m a t p l o t l i b . pyplot as p l t
import numpy as np Figure 173: Convolving a rectangular
signal with itself over and over again
# Input s i g n a l s t o be convolved . will result in a Gaussian. See example:
a = np . f l i p ( np . arange ( 1 0 ) ) 021_fft_convolution/central_limit.py.
b = np . ones ( 2 0 )

# Zero −padded FFT ( 1 0 + 20 = 3 0 )


A = np . f f t . f f t ( a , 3 0 )
B = np . f f t . f f t ( b , 3 0 )
ab = np . f f t . i f f t (A* B )
# This f u n c t i o n a l s o does t h e same as t h e t h r e e l i n e s
# above ab = s . f f t c o n v o l v e ( a , b )
plt . figure ( f i g s i z e =(4 , 6) )
p l t . subplot (311)
p l t . stem ( a )
p l t . ylim ( [ 0 , 1 2 ] )
p l t . xlim ( [ 0 , 3 0 ] )
p l t . t i t l e ( " $a [ n ] $ " )
p l t . subplot (312)
p l t . stem ( b )
p l t . ylim ( [ 0 , 1 . 2 ] )
p l t . xlim ( [ 0 , 3 0 ] )
p l t . t i t l e ( " $b [ n ] $ " )
p l t . subplot (313)
p l t . t i t l e ( " $a [ n ] * b [ n ] $ " )
p l t . stem ( ab )
p l t . ylim ( [ 0 , 5 0 ] )
plt . tight_layout ()
p l t . s a v e f i g ( " c o n v o l u t i o n . png " )
p l t . show ( )
268

Listing 47: 021_fft_convolution/fft_convolution.py

Example: 2D FFT

There also exists higher dimensional discrete Fourier transforms. For


example, the 2D DFT is defined as:

N −1 N −1 2π 2π
x̂ [k, ℓ] = ∑ ∑ x [n, m]e−i N nk e−i N mℓ . (751)
n =0 m =0

There exists a 2D FFT algorithm that implements this efficiently, and


it is often used in image processing and image compression. There
was an example of the use of a 2D FFT for image compression in the
Fourier series chapter.

Figure 174: An example of a convolu-


tion of signals a[n] and b[n] evaluated
using an FFT.
269

Exercises: Discrete Fourier Transform

1. Let x (t) be some continuous-time signal that is sampled. The


signal is discretized with a sampling rate of f s = 17 kHz, giving a
discrete-time signal x [n] of length N. We apply a discrete Fourier
transform
N −1 2π
x̂ [k] = ∑ x [n]e−i N kn (752)
n =0
to the discrete-time signal x [n], giving the signal x̂ [k]. This allows
us to represent the signal x [n] as a sum of N unique complex
sinusoidal terms (spectral components):
N −1
1 2π
x [n] =
N ∑ x̂ [k]ei N kn . (753)
k =0

Here n and k are integers.

a) What are the N frequencies of the complex sinusoids that make


up signal x [n] in units of radians per sample? What are these in
units of hertz?
b) When using a DFT for spectral analysis, the value of N influ-
ences the spectral resolution, i.e., the spacing if frequencies of
the spectral components that are used to represent the signal.
What value of N gives a frequency resolution of 0.1 Hz when
analyzing the signal x [n] using a DFT?

2. Show that | x̂ [k ]| = | x̂ [−k]| when x [n] ∈ R. Here x̂ [k ] is the DFT of


signal x [n]. Hint: investigate x̂ ∗ [k].

3. The code in Listing 48 creates a discrete-time signal consisting of


three sinusoidal signals:

x [n] = cos(ω̂0 n) + 2 cos(ω̂1 n) + 3 cos(ω̂2 n) (754)


Figure 175: The magnitudes of six
and calculates the magnitude spectrum x̂ [k] of this signal. The spectral components of the signal
defined in Equation 754
spectrum is shown in Figure 175.
import m a t p l o t l i b . pyplot as p l t
import numpy as np

# Sample i n d i c e s f o r 10000 samples .


m = np . arange ( 1 0 0 0 0 )

# Sample period .
Ts = 1e −4

# Create a s i g n a l c o n s i s t i n g of three sinusoids .


x = np . cos ( 2 . 0 * np . p i * 4 0 0 0 . 0 * Ts *m) + 2 * np . cos ( 2 . 0 * np . p i * 1 0 0 0 . 0 *
Ts *m) + 3 * np . cos ( 2 . 0 * np . p i * 2 5 0 0 . 0 * Ts *m)

Listing 48: Create a signal consisting of three sinusoids and analyze


the magnitude spectrum calculated using FFT.
270

a) What are the numerical values of ω̂0 , ω̂1 , and ω̂2 in units of
radians per sample? Show that they correspond to frequencies
f 0 = 4000, f 1 = 1000, and f 2 = 2500 in units of hertz.
b) Show that signal x [n] is periodic (x [n] = x [n + N ]). Determine
the fundamental period of this signal in samples and in units of
seconds.
c) Modify the code so that the figure produced by the code,
shown in Figure 175, displays frequency on the horizontal axis
in units of hertz. Make sure that you plot both the positive
and negative frequency components of the signal, recalling
that a real-valued cosine signal consists of a positive and neg-
ative frequency complex sinusoidal signal. Verify that you get
the correct frequency axis by looking at the relative magni-
tudes of the six non-zero frequency components of the signal.
Hint: you may find Python functions numpy.fft.fftshift and
numpy.fft.fftfreq useful for this exercise.
271

Suggested solutions: Discrete Fourier Transform

1. Let x [n] be a discrete-time signal of length N.

a) The spectral components in the DFT is given as ω̂k = 2πk N for


k = 0, 1, . . . , N − 1, in units of radians per sample. In units of
hertz one has:
ω k k fs
fk = k = = ,
2π NTs N
where f s is the sample rate.
b) The DFT yields evenly spaced points in frequency domain
corresponding to normalized angular frequencies of:
2πk
ω̂k = , for k = 0, 1, . . . , N − 1
N
In other words, the resolution is:
2π (k + 1) 2πk 2π
∆ω̂ = − = .
N N N
In units of hertz, we get:
∆ωk ∆ω̂ 2π 1 1 fs
∆f = = = = = .
2π 2πTs N 2πTs NTs N
Having used that ω̂ = ωk Ts . If we want to have a resolution of
∆ f = 0.1 Hz, then we must choose N, so that:

fs 17 · 103 Hz
N= = = 170 000.
∆f 0.1 Hz

2. Suppose x [n] ∈ R for all n ∈ Z. By definition, the DFT of x [n] is:


N −1 2π
x̂ [k ] = ∑ x [n]e−i N kn .
n =0

If we take the complex conjugate (recall that the complex conju-


gate is distributive over addition and multiplication), we get:
N −1 2π
x̂ ∗ [k] = ∑ x ∗ [n]ei N kn .
n =0

By assumption x [n] is real, so x ∗ [n] = x [n], hence:


N −1 2π
x̂ ∗ [k] = ∑ x [n]ei N kn .
n =0

Consider x̂ ∗ [−k ], which is:


N −1 2π
x̂ ∗ [−k] = ∑ x [n]e−i N kn .
n =0

Therefore, x̂ [k] = x̂ ∗ [−k], so | x̂ [k]| = | x̂ ∗ [−k]|, as desired.


272

3. Let x [n] be a discrete-time signal of the form:

x [n] = cos(ω̂0 n) + 2 cos(ω̂1 n) + 3 cos(ω̂2 n).

a) Have that ω̂ = ωTs , where ω is in units of radians per second


and ω̂ is in units of radians per sample. Furthermore, have that
ω = 2π f , where f is in units of hertz, so that ω̂ = 2π f Ts . In
other words:

ω̂0 2π (4000) Ts
f0 = = = 4000 Hz,
2πTs 2πTs
ω̂1 2π (1000) Ts
f1 = = = 1000 Hz,
2πTs 2πTs
Ts 2π (2500) Ts
f2 = = = 2500 Hz,
2πTs 2πTs

where the values obtained for the normalized frequency is read


from the Python code.
b) Let x [n] be as above, then the signal is periodic if: for all ω̂i , ω̂ j
one has ω̂i /ω̂ j ∈ Q. This is the case here, as:

ω̂0 /ω̂1 = 4,
ω̂1 /ω̂2 = 2/5,
ω̂2 /ω̂0 = 8/5,

for which all are rational numbers, hence x [n] is periodic.


We’ve only checked some choices of indices, as the reciprocal of
a rational number is also a rational number.
To compute the period of the signal, use the fact that the fun-
damental period is related to the greatest common divisor
(GCD) of the angular frequencies. To compute the GCD of
three terms, one can use the fact that: ω̂ = gcd(ω̂0 , ω̂1 , ω̂2 ) =
gcd(ω̂0 , gcd(ω̂1 , ω̂2 )). You can compute the gcd in Python if
you ignore the 2πTs part, using the math module with the func-
tion gcd. Doing this gives ω̂ = 2π500Ts in units of radians per
sample. To obtain the period in units of samples:

2π 2π
N= = = 20 samples.
ω̂ 2π500Ts

The period in units of seconds, is then:

2π 2πTs 2πTs 1
T= = = = = 0.002 s,
ω ω̂ 2π500Ts 500

as ω̂ = ωTs , so the signal is periodic with a period of 20


samples, or 0.0002 seconds.
273

c) Listing 49 shows a way to obtain units of hertz on the x-axis.


The output of Listing 49 is shown in Figure 176. The original
signal was a cosine with frequencies of 4000 Hz, 2500 Hz and
1000 Hz, which is present in the plot, as it should be. The
cosine with frequency of 2500 Hz has the largest amplitude,
followed by the 1000 Hz signal, which is reflected in the plot
too.
import m a t p l o t l i b . pyplot as p l t
import numpy as np

# Sample i n d i c e s f o r 10000 samples .


m = np . arange ( 1 0 0 0 0 )

# Sample period .
Ts = 1e −4

# Create a s i g n a l c o n s i s t i n g of three sinusoids .


x = np . cos ( 2 * np . p i * 4 0 0 0 . 0 * Ts *m) + 2 * np . cos ( 2 * np . p i * 1 0 0 0 . 0 *
Ts *m) + 3 * np . cos ( 2 * np . p i * 2 5 0 0 . 0 * Ts *m)

# C a l l f f t f r e q t o compute t h e f r e q u e n c i e s i n u n i t s o f h e r t z
.
# The f u n c t i o n t a k e s i n t h e f i r s t argument , which i s t h e
l e n g t h o f t h e window and second argument f o r t h e
s t e p s i z e ( Ts ) ,
# use f f t s h i f t t o s h i f t t h e f r e q u e n c i e s t o have zero i n t h e
middle ; t h i s i n c l u d e s both p o s i t i v e and n e g a t i v e
frequencies .
f r e q = np . f f t . f f t s h i f t ( np . f f t . f f t f r e q ( l e n (m) , d=Ts ) )

# P l o t t h e magnitude o f s i n u s o i d s using f f t .
p l t . p l o t ( f r e q , np . abs ( np . f f t . f f t ( x ) ) / l e n (m) )
p l t . x l a b e l ( " Frequency (Hz) " )
# C a l l t h i s i f needed .
# p l t . show ( )

Listing 49: Plot with units of hertz on the x-axis

Figure 176: The magnitudes of six


spectral components, but units of hertz
on the x-axis
Spectral Analysis

The DFT can be used for spectral analysis of signals. One can think
of the DFT as a filter bank of frequency selective filters. This concept
allows us to use the FFT algorithm to efficiently analyze the spectral
contents of a discrete-time signal x [n].

Discrete Fourier transform as a filter bank

The impulse response of an ideal point-frequency filter that filters


only some frequency ω̂0 was derived in the discrete-time Fourier
transform chapter. Dropping the constant (2π )−1 , the impulse re-
sponse was:
h[n] = eiω̂0 n . (755)

If we apply an N sample long rectangular window w R [n] of the


following form:
(
1 when −( N − 1) ≤ n ≤ 0
w R [n] = (756)
0 otherwise ,

we have a windowed filter impulse response52 : 52


Note that this is a truncated point-
frequency filter.
(
eiω̂0 n when −( N − 1) ≤ n ≤ 0
h R [n] = w R [n]h[n] = (757)
0 otherwise .

To apply this filter to a signal, we convolve h R [n] with the signal x [n]
and inspect the output of this filter at sample n = 0. Recall that a
convolution is:

y[n] = ∑ x [ℓ]h R [n − ℓ] (758)
ℓ=−∞

and at n = 0 this would be:



y [0] = ∑ x [ℓ]h R [0 − ℓ] (759)
ℓ=−∞
N −1
= ∑ x [ℓ]e−iω̂0 ℓ (760)
ℓ=0
276

We’ll now say that the center frequency of the filter is ω̂0 = 2πk/N.
The output of the filter y[n] at n = 0 can be written as:

N −1 2π
y [0] = ∑ x [ℓ]e−i N kℓ . (761)
ℓ=0

Now recall that the DFT is defined as:


N −1 2π
x̂ [k] = ∑ x [ℓ]e−i N kℓ (762)
ℓ=0

They are the same! This means that a DFT evaluates the output of
frequency selective filters with frequencies:


ω̂k = k. (763)
N

This is at first counter-intuitive. The output of the DFT should be a


frequency domain quantity, and so it is. However, it also happens to
be the time domain filter output at time 0 for a frequency selective
filter with a rectangular window:
( 2π
ei N kn −( N − 1) ≤ n ≤ 0
h R,k [n] = (764)
0 otherwise.

Frequency response
We can investigate the frequency response Hk (ω̂ ) of each filter h R,k [n]
in the filter bank implemented with a discrete-time Fourier trans-
form:

Hk (ω̂ ) = ∑ h R,k [ℓ]e−iω̂ ℓ (765)
ℓ=−∞
0
∑ ei(ω̂k −ω̂ )ℓ (766)
ℓ=−( N −1)
N −1
= ∑ e−i(ω̂k −ω̂ )ℓ (767)
ℓ=0
1 − e−i(ω̂k −ω̂ ) N
= (768)
1 − e−i(ω̂k −ω̂ )
= Ne−i(ω̂k −ω̂ )( N −1)/2 D N (ω̂k − ω̂ ) (769)

In the second last step, I’ve used the geometric sum closed solution
formula S = (1 − α N +1 )/(1 − α) with α = e−i(ω̂ −ω̂k ) . The function
D N (ω̂ ) is the familiar Dirichlet function we encountered when inves-
tigating the frequency response of the running average filter. In this
case, it is frequency shifted so that the peak is at ω̂ = ω̂k .
277

Figure 177: The magnitude response


|Hk (ω̂ )| for several values of k. Each
DFT output bin x̂ [k ] can be thought of
as the output of a frequency selective
filter with this magnitude response.
For example, k = 0 corresponds to
a low-pass filter that passes mainly
spectral components of the signal x [n]
that have a frequency close to zero.

The magnitude response |Hk (ω̂ )| for N = 32 using several values


of k are shown in Figure 177.
From this plot, it is obvious that complex sinusoidal signals at
nearly all frequencies, except for frequencies ω̂k , will leak into the fil-
ter output with fairly significant amplitudes. This is often unwanted.

Windowed DFT

In order to reduce the spectral sidelobes seen in Figure 177, a tapered


window function can be used. The procedure is relatively simple.
One applies a more selective window function to the signal before
evaluating the DFT. This is equivalent to the tapered frequency
selective filter shown in the chapter on practical filters. The impulse
response of each filter in the filter bank will now be:
( 2π
w( N − 1 + n)ei N kn −( N − 1) ≤ n ≤ 0
h R,k [n] = (770)
0 otherwise.

Here w(n) is a tapered window function of length N.


The filter bank can now be implemented using a DFT as follows:

N −1 2π
x̂w [k] = ∑ x [n]w N (n)e−i N nk . (771)
n =0
278

This results in a wider central peak at each frequency, but less spec-
tral leakage from signals outside frequency ω̂k .
Figure 178 shows the magnitude response for several DFT fre-
quency components below, when a Hann window (Equation 676) of
length N = 32 is applied to the signal:

Figure 178: Windowed DFT filter bank


|Hk (ω̂ )| magnitude responses for
several values of k. Compared to rectan-
gular window function, the magnitude
response of the Hann windowed DFT
filter bank has significantly lowered
sidelobes. There is less out-of-band
signal leaking into the pass band of
each frequency.

In nearly all spectral analysis applications, it is beneficial to apply


a window function to the signal that is being analyzed for spectral
content, to avoid spectral leakage.

Example: Spectral analysis using windowed DFT

The following example illustrates the advantages of using a window


function with low spectral sidelobes when using an FFT for spectral
analysis.
Let’s generate a synthetic signal, which consists of a large and a
small amplitude sinusoidal signal.

y[n] = A1 sin(ω̂1 n) + A2 sin(ω̂2 n), (772)

where A1 ≫ A2 and ω̂1 ̸= ω̂2 . This signal is shown in Figure 179.


When looking at the plot of the signal y[n] in time domain, it is very
difficult, if not impossible, to identify the weak signal. The strong
signal overpowers it.
279

Figure 179: Top: a large amplitude


sinusoidal signal, Middle: a small
amplitude sinusoidal signal. Bottom:
Sum signal y[n]. When added together,
the large amplitude signal dominates
and the small amplitude signal cannot
be visually detected.

When analyzing the signal for spectral content using an unwin-


dowed DFT, we get:
N −1 2π
ŷ[k] = ∑ y[n]e−i N nk . (773)
n =0

The strong sinusoidal signal A1 sin(ω̂1 n) will result in a non-zero


output for all spectral components ŷ[k]. This “spectral” leakage can
easily overpower the weaker spectral component. Power in decibel
scale (10 log10 |ŷ[k]|2 ) is shown with a blue line in Figure 180. It is
easy to identify one strong spectral peak corresponding to the large
amplitude sinusoidal signal with frequency ω̂1 .
In order to detect the sinusoidal signal with the small amplitude,
we can use a windowed DFT:
N −1 2π
ŷw [k] = ∑ y[n]w(n)e−i N nk . (774)
n =0

Power in decibel scale is shown with an orange line in Figure 180. In


this case, spectral leakage is reduced significantly, and it is possible to
identify the weak spectral component.
The Python program used to create the plots for this example is
shown in Listing 50
import m a t p l o t l i b . pyplot as p l t
import numpy as np
from s c i p y . s i g n a l . windows import hann
280

Figure 180: Spectral power with (or-


ange) and without (blue) a tapered
window function. Without a tapering
window, it is impossible to identify
the sinusoidal signal with a very small
amplitude, as spectral leakage from
the large amplitude sinusoidal signal
overpowers the weak signal.

# Using windowed DFT f o r s p e c t r a l a n a l y s i s o f s i g n a l s .


# Demonstrate t h a t a window f u n c t i o n a l l o w s weak
# s i g n a l s t o be found b e t t e r , due t o l e s s s p e c t r a l
# l e a k a g e than i f a r e c t a n g u l a r window i s used .

N = 4096
n = np . arange (N)

f r e q 1 = 0 . 0 1 * np . p i
f r e q 2 = 0 . 1 * np . p i

# Strong low−frequency s i g n a l s i g n a l .
s t r o n g _ s i g n a l = 1 e6 * np . cos ( f r e q 1 * n )

# Weak s i g n a l a t h i g h e r frequency .
weak_signal = np . cos ( f r e q 2 * n )

y = s t r o n g _ s i g n a l + weak_signal

p l t . subplot (311)
plt . plot (n , strong_signal )
p l t . x l a b e l ( " Sample ( $n$ ) " )
p l t . y l a b e l ( " $x_1 [ n ] $ " )
p l t . t i t l e ( r " Strong s i g n a l $\hat {\omega}=%1.2 f $ " % ( f r e q 1 ) )
p l t . subplot (312)
p l t . p l o t ( n , weak_signal )
p l t . x l a b e l ( " Sample ( $n$ ) " )
p l t . y l a b e l ( " $x_2 [ n ] $ " )
p l t . t i t l e ( r "Weak s i g n a l $\hat {\omega}=%1.2 f $ " % ( f r e q 2 ) )
p l t . subplot (313)
# Weak s i g n a l i s i m p o s s i b l e t o see ,
# because i t has a 1 e6 s m a l l e r amplitude .
281

plt . plot (n , y)
plt . x l a b e l ( " Sample ( $n$ ) " )
plt . y l a b e l ( " $y [ n]= x_1 [ n]+ x_2 [ n ] $ " )
plt . t i t l e ( " Total signal " )
plt . tight_layout ()
plt . s a v e f i g ( " windowed_signals . png " )
plt . show ( )

# Analyze spectrum zero padded DFT t o be o f l e n g t h 2 *N.


Y = np . f f t . f f t ( y , 2 *N)
# Windowed zero −padded DFT .
w = hann (N)
# Zero padded DFT t o be o f l e n g t h 2 *N.
WY = np . f f t . f f t (w* y , 2 *N)
# F r e q u e n c i e s i n r a d i a n s per sample .
om = np . f f t . f f t f r e q ( l e n ( Y ) , d=1) * 2 . 0 * np . p i
# Reorder f r e q u e n c i e s so t h a t we go from − p i t o p i i n s t e a d o f 0
t o 2\ p i .
om = np . f f t . f f t s h i f t (om)
Y = np . f f t . f f t s h i f t ( Y )
WY = np . f f t . f f t s h i f t (WY)

p l t . p l o t (om, 1 0 . 0 * np . l o g 1 0 ( np . abs ( Y ) * * 2 . 0 ) , l a b e l = "DFT" )


p l t . p l o t (om, 1 0 . 0 * np . l o g 1 0 ( np . abs (WY) * * 2 . 0 ) , l a b e l = "Windowed DFT
")
p l t . y l a b e l ( " S p e c t r a l power ( dB ) " )
p l t . x l a b e l ( " Frequency ( rad/s ) " )
p l t . t i t l e ( " Power spectrum " )
p l t . legend ( )
plt . tight_layout ()
p l t . s a v e f i g ( " windowed_spec . png " )
p l t . show ( )

Listing 50: 022_window_functions/windowed_dft.py

Example: Dynamic spectrum

Signals are rarely unchanging in their spectral content. In many ap-


plications, it is advantageous to analyze the time-frequency evolution
of signals. This can be done with the help of a windowed DFT and
selecting portions of the signal x [n] of length N. This results in a 2d
signal, containing time and frequency:

M −1 2π
x̂ [t, k] = ∑ x [n + t∆n]w N (n)e−i M kn . (775)
n =0

Here ∆n is the number of samples between spectral estimates. This


type of time-frequency spectrum is sometimes called a dynamic
spectrum or a spectrogram53 53
There is an implementation of a spec-
trogram in scipy.signal.spectrogram.
Note that in this case, N samples in time are used for each spec-
trum, so the spectral estimate is not instantaneous, but an average
over N samples. This is yet again a consequence of time-frequency
ambiguity.
282

Zero padding of the DFT to length M can also be used to produce


a higher resolution DTFT estimate. In this case, M denotes the length
of the DFT, and w N (n) indicates a window function of length N,
which will zero-pad samples of x [n] at values of n beyond sample
N − 1.
An example of a dynamic spectrum is shown in Figure 181. The
signal that is analyzed in time and frequency is a chirp signal:
 
x [n] = sin an2 , (776)

which has a frequency that increases as a function of time.


The figure is produced with the Python program shown in Listing
51 shown below, which implements a dynamic spectrum using the
FFT function in Python.

Figure 181: Time-frequency power


spectrum 10 log10 | X [t, k]|2 for a chirp
signal, which increases in frequency.

import m a t p l o t l i b . pyplot as p l t
import numpy as np
from s c i p y . s i g n a l . windows import hann

def spectrogram ( x , M=1024 , N=128 , d e l t a _ n =100) :


"""
x = signal
M = FFT l e n g t h
N = window f u n c t i o n l e n g t h
delta_n = step step
"""
283

max_t = i n t ( np . f l o o r ( ( l e n ( x ) −N) / d e l t a _ n ) )
X = np . z e r o s ( [ max_t , M] , dtype=np . complex64 )
w = hann (N)
x i n = np . z e r o s (N)

f o r i i n range ( max_t ) :
# Zero padded windowed FFT .
x i n [ 0 :N] = x [ i * d e l t a _ n + np . arange (N) ]
X [ i , : ] = np . f f t . f f t (w* xin , M)

return (X)

# Sample r a t e (Hz) .
fs = 4096.0

# Sample i n d i c e s ( one second o f s i g n a l ) .


n = np . arange ( 4 0 9 6 )
# Generate a c h i r p s i g n a l .
x = np . s i n ( 0 . 1 5 e −14 * n * * 5 . 0 )

# Time s t e p .
d e l t a _ n = 25
M = 2048
# C r e a t e dynamic spectrum .
S = spectrogram ( x , M=M, N=128 , d e l t a _ n = d e l t a _ n )

f r e q s = np . f f t . f f t f r e q ( 2 0 4 8 , d=1.0/ f s )
time = d e l t a _ n * np . arange ( S . shape [ 0 ] ) / f s

plt . figure ( f i g s i z e =(12 , 10) )


plt . subplot (211)
plt . p l o t ( n/ f s , x )
plt . t i t l e ( " S i g n a l $x [ n ] $ " )
plt . x l a b e l ( " Time ( s ) " )
plt . y l a b e l ( " S i g n a l amplitude " )
plt . subplot (212)

p l t . t i t l e ( " Spectrogram " )


p l t . pcolormesh ( time , f r e q s [ : i n t (M/2) ] , np . t r a n s p o s e ( 1 0 . 0 * np .
l o g 1 0 ( np . abs ( S [ : , : i n t (M/2) ] ) * * 2 . 0 ) ) , vmin =0)
p l t . xlim ( [ 0 , np . max ( time ) ] )
p l t . ylim ( [ 0 , f s / 2 . 0 ] )
p l t . x l a b e l ( " Time ( s ) " )
p l t . y l a b e l ( " Frequency (Hz) " )
cb = p l t . c o l o r b a r ( o r i e n t a t i o n = " h o r i z o n t a l " )
cb . s e t _ l a b e l ( " dB " )
plt . tight_layout ()
p l t . s a v e f i g ( " dynspec . png " )
p l t . show ( )

Listing 51: 023_dynamic_spectrum/dynamic_spectrum.py


284

Exercises: Spectral Analysis

1. This task is a programming assignment. Your task will be to


analyze the time-frequency contents of a musical recording. The
aim is to determine what sequence of musical notes are being
played.
Download the audio file from:

https://bit.ly/3EAhn26

You can listen to the audio file to get an idea of the time-frequency
contents of the signal.
Read the audio signal using Python as follows: Figure 182: Ludwig van Beethoven, a
well known musical composer active
around the turn of the 18th and 19th
import scipy.io.wavfile century.
audio = scipy.io.wavfile.read("b.wav")
sample_rate = audio[0]
# read only one channel of the stereo signal
signal = audio[1][:,0]

Complete the following tasks and justify your result:

a) What is the sample-rate? What is the lowest and highest fre-


quency spectral component that can be represented using this
sample-rate?
b) Make a plot of the signal as a function of time. Use time in
seconds on the horizontal axis. How long is the signal?
c) Write a short program to analyze the time-frequency contents
of this signal. There are several Python library functions for im-
plementing a spectrogram. For example: scipy.signal.spectrogram
or matplotlib.pyplot.specgram. For this exercise, I ask that
you do not use these functions, but implement your own func-
tion instead, so that you can gain a good understanding of how
these functions are implemented. You can use the example
program in Listing 51 to help you out. Use a Hann window in
the spectral analysis.
d) Make a plot of the spectrogram produced with your program.
Limit the frequency axis to range 0 to 1500 Hz. Use an appro-
priate time and frequency resolution that allows you to identify
the positions of individual notes being played in time and
frequency.
e) In the time-frequency spectrum of the musical instrument play-
ing an individual note, there are spectral lines with multiple
harmonics f = n f 0 , where n ∈ N is a positive integer, and f 0 is
285

the base frequency. For example, if f 0 = 220 Hz, you will also
see spectral lines at 440, 660, 880, and 1100 Hz. Why are there
multiple spectral lines?
f) Using the spectrogram plot, determine what are the nine
musical notes that are played in the recording? Make sure
that your plot allows you to clearly identify each note in time
and frequency. Use 150 to 400 Hz on the frequency axis as
the axis limits. You will most likely need to adjust the FFT
length, overlap, and zero padding settings to be able to easily
determine the notes. You might also need to adjust the color
scale. I’ll give you a hint. The first note is E.
Assume an equal tempered scale is used, which results in the
following mapping of base frequencies and musical notes:
Note Frequency (Hz)
A 220.0
A# 233.0819
B 246.9417
C 261.6256
C# 277.1826
D 293.6648
D# 311.127
E 329.6276
F 349.2282
F# 369.9944
G 391.9954
286

Suggested solution: Spectral Analysis

1. Read the audio file as follows:


import s c i p y . i o . w a v f i l e
audio = s c i p y . i o . w a v f i l e . read ( " b . wav" )
s a m p l e _ r a t e = audio [ 0 ]
# Read only one channel o f t h e s t e r e o s i g n a l .
s i g n a l = audio [ 1 ] [ : , 0 ]

a) Using the code given, one can simply print the sample rate.
The sample rate is f s = 44100 Hz, so the highest and lowest
frequencies that can be represented are f s /2 = ±22050 Hz.
b) The following code will import the audio file and plot the
signal with seconds on the x-axis.
import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . i o . w a v f i l e

audio = s c i p y . i o . w a v f i l e . read ( " b . wav" )


s a m p l e _ r a t e = audio [ 0 ]

# Read only one channel o f t h e s t e r e o s i g n a l .


s i g n a l = audio [ 1 ] [ : , 0 ]

# The sample r a t e i s :
p r i n t ( s a m p l e _ r a t e ) # output i s 44100 Hz .

# P a r t i t i o n t h e i n t e r v a l such t h a t t h e
# u n i t s become seconds .
t = np . arange ( l e n ( s i g n a l ) ) /( s a m p l e _ r a t e )

plt . plot ( t , signal )


p l t . x l a b e l ( " Time ( s ) " )
# C a l l t h i s i f needed .

Listing 52: Code to plot audio signal

The output of Listing 52 is shown in Figure 183.


The length in seconds can be computed as time in seconds =
(number of samples)(sample spacing) which in this case is
t = 215678/44100 ≈ 4.89, so around a 5 seconds long signal.
From Figure 183 we see that the signal is around 5 seconds
long.
Figure 183: Audio signal
c) The following code shown in Listing 53 will compute and print
the spectrogram for the audio signal.
import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . i o . w a v f i l e
from s c i p y . s i g n a l . windows import hann
287

def c o n v e r t _ t o _ d e c i b e l ( x ) :
" " " Function t o c o n v e r t t o dB . " " "
r e t u r n 1 0 * np . l o g 1 0 ( np . abs ( x ) * * 2 )

audio = s c i p y . i o . w a v f i l e . read ( " b . wav" )


s a m p l e _ r a t e = audio [ 0 ]

# Read only one channel o f t h e s t e r e o s i g n a l .


s i g n a l = audio [ 1 ] [ : , 0 ]

def spectrogram ( s i g n a l , d e l t a _ n , N, M) :
" " " Have a s i g n a l x o f l e n g t h L .
We d i v i d e t h e s i g n a l i n t o sub a r r a y s o f l e n g t h N, t h e
s t e p s i z e i s then d e l t a _ n
t h e maximum time u n i t s a r e then L/ d e l t a _ n

M − l e n g t h o f FFT .
N − l e n g t h o f window .
d e l t a _ n − s t e p s i z e i n time .
"""

# Window f u n c t i o n ( Hann window ) .


w = hann (N)

# Length o f s i g n a l .
L = len ( signal )

# Compute t h e maximum number o f time s t e p s .


t_max = ( L − N) // d e l t a _ n

# A l l o c a t e space f o r t h e spectogram and s u b _ a r r a y s .


H = np . z e r o s ( [ t_max , M] , dtype=np . complex64 )
sub_array = np . z e r o s (N)

# Step through t h e s i g n a l .
f o r i i n range ( t_max ) :
# Get a sub_array and then f f t i t with t h e window
and s t o r e i t i n H.
sub_array [ :N] = s i g n a l [ i * d e l t a _ n + np . arange (N) ]
H[ i , : ] = np . f f t . f f t ( sub_array *w, M)

return H

M = 10480
N = 2000
d e l t a _ n = 40

# Compute t h e spectrogram .
s p e c t = spectrogram ( s i g n a l , d e l t a _ n , N, M)

# P a r t i t i o n t h e axes c o r r e c t l y with
# u n i t s o f Hertz and seconds .
f r e q s = np . f f t . f f t f r e q (M, d=1.0/ s a m p l e _ r a t e )
time = d e l t a _ n * np . arange ( s p e c t . shape [ 0 ] ) /s a m p l e _ r a t e

# C r e a t e t h e spectrogram p l o t , l i m i t i n g frequency t o ( 0 ,
1 5 0 0 ) Hz .
p l t . pcolormesh ( time , f r e q s [ :M/ / 2 ] , np . t r a n s p o s e (
288

c o n v e r t _ t o _ d e c i b e l ( s p e c t [ : , :M/ / 2 ] ) ) , vmin =40)


p l t . x l a b e l ( " Time ( s ) " )
p l t . y l a b e l ( " Frequency (Hz) " )
p l t . ylim ( 0 , 1 5 0 0 )
plt . colorbar ( )
# C a l l t h i s i f needed .

Listing 53: Spectrogram code

d) The output of Listing 53 is shown in Figure 184.

Figure 184: Für Elise spectrogram

e) Instruments don’t produce pure frequency tones, so there will


be harmonics, which can be seen in Figure 184. Different instru-
ments emphasize different harmonics, giving each instrument
its respective sound among other factors.
f) Comparing the spectrogram with the frequency table, one
can read that the musical phrase is E D# E D# E B D C A. You
can compare this with sheet music for the piece, which is the
correct phrase.
Arbitrary Frequency Response Filters

The discrete Fourier transform and the FFT algorithm allow us to


efficiently implement filters with an arbitrary frequency response.
This is because the frequency domain representation of the filter
impulse response h[n] can be specified as in frequency domain ĥ[k ].
Convolution (i.e., filtering) is multiplication in frequency domain:

D F
y[n] = h[n] ⊛ x [n] ←→ ŷ[k ] = ĥ[k] x̂ [k]. (777)

This means that we don’t even have to determine the filter impulse
response in time domain h[n]. One can FFT the signal x [n], specify
and apply the filter ĥ[k] directly in frequency domain through multi-
plication, and finally inverse FFT the signal to obtain a filtered signal
y [ n ].

Example: Filtering out spectrally narrow frequency components

In this example, we will create a filter that removes strong spectral


components from a signal using a band-stop filter.
Consider the following signal:

x1 [n] = A1 cos(ω1 n + ϕ1 ) + A2 cos(ω2 n + ϕ2 ) (778)


+ A3 cos(ω3 n + ϕ3 ), (779)

and another signal

x2 [ n ] = a1 δ [ n − n1 ] + a2 δ [ n − n2 ] + a3 δ [ n − n3 ], (780)

overlaid on top of it:

x [ n ] = x1 [ n ] + x2 [ n ]. (781)

We’ll assume that signal x1 [n] is the problematic signal that we


want to filter out, so that we are able to detect the signal x2 [n]. We’ll
assume that | Ai | ≫ | a j | for all i, j. This means that the signal x1 [n]
greatly overpowers the signal x2 [n] in amplitude. Figure 185 shows
x1 [n], x2 [n], and x [n] = x1 [n] + x2 [n] for this example. It is virtually
impossible to visually detect the weak signal x2 [n] from the plot.
290

Figure 185: A plot of the strong narrow-


band signal x1 [n], the weak wideband
signal x2 [n], and the summed signal
x1 [n] + x2 [n]. It is difficult to observe
the weak signal x2 [n] just by visually
inspecting x [n].

In order to detect signal x2 [n], we need to filter out signal x1 [n].


We can do this by creating a filter, which will remove the strong
spectral components, allowing the weak broad band signal to be
recovered. Forming such a filter in frequency domain is relatively
easy. We specify that:
(
α
| x̂ [k]|
for strong spectral components k
ĥ[k ] = (782)
1 otherwise

The strong spectral components can be identified by inspecting the


spectrum of the signal x [n], that is | x̂ [k]|. When this filter is applied
in frequency domain, we obtain a frequency domain representation
of the filtered signal:
ŷ[k] = ĥ[k] x̂ [k]. (783)
The strong spectral components will be attenuated to a constant mag-
nitude α, while the rest of the spectral components are unaffected.
Figure 186 shows ŷ[k] and x̂ [k]. Note that the spectrally narrow sig-
nals have approximately 1012 more power than the weak signals.
In order to obtain the filtered signal, we simply inverse DFT:
−1
y[n] = F D {ŷ[k]} (784)

The filtered signal is shown in Figure 187. The plot shows that when
strong spectrally narrow signal components are filtered out, the weak
signal becomes visible. There are some artifacts caused by filtering,
291

Figure 186: The magnitude spectrum


of signal | x̂ [k ]| and the magnitude
spectrum of the filtered signal |ŷ[k ]|. In
this case, a logarithmic scale is used.
A Hann window was applied to the
signal when calculating the spectrum to
reduce spectral leakage.

because some spectral components of the weak signal have also been
reduced in amplitude.
In most cases, it is advantageous to use a tapered window on the
signal x [n] when estimating the discrete Fourier transform, in order
to reduce spectral leakage:

N −1 2π
x̂ [k] = ∑ w[n] x [n]e− N nk (785)
n =0

Here w[n] would be a tapered window with better suppression of


spectral sidelobes than a rectangular window. The Hann window is
one good example.
The Python program used to create the plots in this example is
shown in Listing 54.
#
# Using DFT t o c r e a t e a f i l t e r t h a t
# a t t e n u a t e s p e c t r a l components t o allow
# weak s i g n a l s buried under t h e s t r o n g but
# s p e c t r a l l y narrow s i g n a l s t o be found .
#
import m a t p l o t l i b . pyplot as p l t
import numpy as np
from s c i p y . s i g n a l . windows import hann

N = 4096*2*2
n = np . arange (N)
freqs = [0.0012 , 0.0021 , 0.0032 , 0.1 , 0.9]
A = [ 1 e5 , 0 . 5 e5 , 1 e3 , 1 e4 , 0 . 5 e4 ]
292

Figure 187: A comparison of the filtered


estimate y[n] of the weak signal x2 [n]
and the original signal.

s t r o n g _ s i g n a l = np . z e r o s (N)

f o r i , f i n enumerate ( f r e q s ) :
s t r o n g _ s i g n a l += A[ i ] * np . cos ( np . p i * f * n + np . random . randn ( 1 ) )

# Weak s i g n a l a t t h e middle o f t h e s i g n a l .
weak_signal = np . z e r o s (N)
weak_signal [ i n t (N/2) ] = 1 0 . 0
weak_signal [ i n t (N/2) +1000] = −5.0
weak_signal [ i n t (N/2) −1000] = 1 . 0

x = s t r o n g _ s i g n a l +weak_signal

plt . subplot (311)


plt . plot (n , strong_signal )
plt . x l a b e l ( " Sample ( $n$ ) " )
plt . y l a b e l ( " $x_1 [ n ] $ " )
plt . t i t l e ( " Strong s i g n a l " )
plt . subplot (312)
plt . p l o t ( n , weak_signal )
plt . x l a b e l ( " Sample ( $n$ ) " )
plt . y l a b e l ( " $x_2 [ n ] $ " )
plt . t i t l e ( "Weak s i g n a l " )
plt . subplot (313)
plt . plot (n , x )
plt . x l a b e l ( " Sample ( $n$ ) " )
plt . y l a b e l ( " $x [ n]= x_1 [ n]+ x_2 [ n ] $ " )
plt . t i t l e ( " Total signal " )
plt . tight_layout ()
plt . s a v e f i g ( " f i l t e r _ s i g n a l s . png " )
plt . show ( )

# Windowed zero −padded DFT .


293

w = hann (N)
WX = np . f f t . r f f t (w* x , 2 *N)

# C r e a t e a f i l t e r than f i l t e r s out s t r o n g s p e c t r a l components


the s i g n a l .
WH = np . z e r o s ( l e n (WX) )
WH[ : ] = 1 . 0 # Pass a l l f r e q u e n c i e s .
WH[ 0 : 4 0 0 ] = 1 . 0 / np . abs (WX[ 0 : 4 0 0 ] ) # A t t e n u a t e s t r o n g
frequencies .
WH[ 1 4 0 0 : 1 8 0 0 ] = 1 . 0 / np . abs (WX[ 1 4 0 0 : 1 8 0 0 ] ) # A t t e n u a t e s t r o n g
frequencies .
WH[ 1 4 0 0 0 : 1 5 0 0 0 ] = 1 . 0 / np . abs (WX[ 1 4 0 0 0 : 1 5 0 0 0 ] ) # A t t e n u a t e
strong frequencies .

plt . semilogy ( np . abs (WX) , l a b e l =r " $|\hat { x } [ k ]| $ " )


plt . semilogy ( np . abs (WX*WH) , l a b e l =r " $|\hat { y } [ k ]| $ " )
plt . legend ( )
plt . t i t l e ( " O r i g i n a l and f i l t e r e d spectrum " )
plt . tight_layout ()
plt . s a v e f i g ( " f i l t e r _ s p e c . png " )
plt . show ( )

# Apply f i l t e r i n frequency domain .


wy = np . f f t . i r f f t (WH*WX) [ 0 :N]

# P l o t f i l t e r e d s i g n a l and compare with o r i g i n a l weak


# s i g n a l buried under s t r o n g e r s i g n a l s
p l t . subplot (211)
p l t . p l o t ( n , wy)
plt . t i t l e ( " Filtered signal " )
p l t . x l a b e l ( " $n$ " )
p l t . y l a b e l ( " $y [ n ] $ " )

plt . subplot (212)


plt . p l o t ( n , weak_signal )
plt . t i t l e ( " O r i g i n a l weak s i g n a l " )
plt . x l a b e l ( " $n$ " )
plt . y l a b e l ( " $x_2 [ n ] $ " )
plt . tight_layout ()
plt . s a v e f i g ( " f i l t e r _ f i l t e r e d . png " )
plt . show ( )

Listing 54: 024_fft_filter/fft_filter.py

Example: Whitening filter

It is also possible to approach the previous example in a more au-


tomatic fashion. We can also simply form a filter that whitens the
spectrum, i.e., ensures that all the spectral components are unity
magnitude after at the filter output. This is a useful filter, especially if
the signal also contains random noise.
In this example, we’ll use the same signals as we used in the
previous example. Instead of reducing the amplitudes of spectral
components within a certain range, we’ll simply set the amplitudes of
294

all spectral components to unity:

1
ĥ[k] = (786)
| x̂ [k]|
This type of filter is called a whitening filter54 54
In statistical signal processing, | x̂ [k ]|
When this filter is applied in frequency domain: would be replaced with an estimate
of
p the mean squared spectral power
E| x̂ [k ]|2 . Here E is the statistical
ŷ[k] = ĥ[k] x̂ [k], (787) expectation operator.

the resulting signal spectrum |ŷ[k ]| = 1 will have a unity magnitude


at all frequencies. Figure 188 shows the spectrum of the original
signal x̂ [k] (this is the same as in the previous example), as well as
|ŷ[k]| = 1.

Figure 188: Windowed magnitude


spectrum of the original signal | x̂ [k]|
and the magnitude spectrum of the
whitened signal |ŷ[k]|.

When we now inverse Fourier transform ŷ[k ], we obtain the fil-


tered signal:
−1
y[n] = F D {ŷ[k]}. (788)
The filtered signal is shown in Figure 189. Note that while |ŷ[k]| = 1,
there is still information in the phase ∠ŷ[k], which allows us to detect
the weak signal x2 [n].
The Python code used to produce this example is shown in Listing
55.
#
# Using DFT t o c r e a t e a f i l t e r t h a t
# a t t e n u a t e s p e c t r a l components t o allow
# weak s i g n a l s b u r r i e d under t h e s t r o n g but
# s p e c t r a l l y narrow s i g n a l s t o be found .
295

Figure 189: The output of the whitening


filter y[n]. It is possible to identify the
weak signal x2 [n].

#
import numpy as n
import m a t p l o t l i b . pyplot as p l t
import s c i p y . s i g n a l as s

N= 4 0 9 6 * 2 * 2
nn=n . arange (N)
freqs =[0.0012 ,0.0021 ,0.0032 ,0.1 ,0.9]
A=[1 e5 , 0 . 5 e5 , 1 e3 , 1 e4 , 0 . 5 e4 ]
s t r o n g _ s i g n a l =n . z e r o s (N)
f o r i , f i n enumerate ( f r e q s ) :
s t r o n g _ s i g n a l +=A[ i ] * n . cos ( n . p i * f r e q s [ i ] * nn+n . random . randn ( 1 )
)

# weak s i g n a l a t t h e middle o f t h e s i g n a l
weak_signal=n . z e r o s (N)
weak_signal [ i n t (N/2) ] = 1 0 . 0
weak_signal [ i n t (N/2) +1000]= −5.0
weak_signal [ i n t (N/2) − 1 0 0 0 ] = 2 . 0

# add n o i s e
n o i s e =n . random . randn (N) * 0 . 2
x= s t r o n g _ s i g n a l +weak_signal+ n o i s e

plt . subplot (311)


plt . p l o t ( nn , s t r o n g _ s i g n a l )
plt . x l a b e l ( " Sample ( $n$ ) " )
plt . y l a b e l ( " $x_1 [ n ] $ " )
plt . t i t l e ( " Strong s i g n a l " )
plt . subplot (312)
plt . p l o t ( nn , weak_signal )
plt . x l a b e l ( " Sample ( $n$ ) " )
plt . y l a b e l ( " $x_2 [ n ] $ " )
plt . t i t l e ( "Weak s i g n a l " )
296

plt . subplot (313)


plt . p l o t ( nn , x )
plt . x l a b e l ( " Sample ( $n$ ) " )
plt . y l a b e l ( " $x [ n]= x_1 [ n]+ x_2 [ n]+\\ x i [ n ] $ " )
plt . t i t l e ( " Total signal " )
plt . tight_layout ()
plt . s a v e f i g ( " w h i t e n _ s i g n a l s . png " )
plt . show ( )

# windowed zero −padded DFT


w=s . hann (N)
WX=n . f f t . r f f t (w* x , 2 * N)

# c r e a t e a f i l t e r than s e t s f i l t e r s a l l s p e c t r a l components t o
u n i t y magnitude
H=1.0/n . abs (WX)

plt . semilogy ( n . abs (WX) , l a b e l = " $|\hat { x } [ k ]| $ " )


plt . semilogy ( n . abs (H*WX) , l a b e l = " $|\hat { y } [ k ]| $ " )
plt . legend ( )
plt . x l a b e l ( " $k$ " )
plt . t i t l e ( " Spectra " )
plt . tight_layout ()
plt . s a v e f i g ( " whiten_spec . png " )
plt . show ( )

# apply f i l t e r i n frequency domain and


# IFFT t h e s i g n a l i n t o time domain .
wy=n . f f t . i r f f t (H*WX) [ 0 :N]

# p l o t f i l t e r e d s i g n a l and compare with o r i g i n a l weak s i g n a l


buried under
# stronger signals
p l t . subplot (211)
p l t . p l o t ( nn , wy)
plt . t i t l e ( " Filtered signal " )
p l t . x l a b e l ( " $n$ " )
p l t . y l a b e l ( " $y [ n ] $ " )

plt . subplot (212)


plt . p l o t ( nn , weak_signal )
plt . t i t l e ( " O r i g i n a l weak s i g n a l " )
plt . x l a b e l ( " $n$ " )
plt . y l a b e l ( " $x_2 [ n ] $ " )
plt . tight_layout ()
plt . s a v e f i g ( " w h i t e n _ f i l t e r e d . png " )
plt . show ( )

Listing 55: 024_fft_filter/whitening_filter.py


297

Exercises: Arbitrary Frequency Response Filters

1. The Python code in Listing 56 creates a signal consisting of nar-


rowband sinusoids with large amplitudes (x1 [n]), and three time
shifted unit impulses with weak amplitudes (x2 [n]):

x [ n ] = x1 [ n ] + x2 [ n ] (789)

import numpy as np

N = 16384
n = np . arange (N)
freqs = [0.0003 , 0.012 , 0.055 , 0.102 , 0.85]
A = [ 1 e5 , 0 . 5 e5 , 1 e3 , 1 e4 , 0 . 5 e4 ]
x1 = np . z e r o s (N)

f o r i , f i n enumerate ( f r e q s ) :
x1 += A[ i ] * np . cos ( np . p i * f * n + np . random . randn ( 1 ) )

# Weak s i g n a l a t t h e middle o f t h e s i g n a l .
x2 = np . z e r o s (N)
x2 [ i n t (N/2) ] = 1 0 . 0
x2 [ i n t (N/2) +1000] = −5.0
x2 [ i n t (N/2) −1000] = 1 . 0

x = x1 + x2

Listing 56: fftex.py

a) Estimate the spectrum of the signal x [n] using a Hann window


w[n] of length N = 16384 to reduce spectral leakage.
N −1 2π
x̂w [k] = ∑ w[n] x [n]e−i N nk (790)
n =0

Use FFT to evaluate the DFT.


Make a plot of the magnitude spectrum with power in dB
scale 10 log10 | x̂w [ω̂k ]|2 . Only plot the positive frequencies
between 0 and π with units of radians per sample. Identify
the frequency ranges that are occupied by strong narrowband
spectral components.
b) Filter out the large magnitude frequency components in fre-
quency domain and inverse DFT to obtain a time domain
representation of the signal.
n o
−1
y[n] = F D ĥ[k ] x̂w [k] (791)

Use FFT to evaluate the inverse DFT. Your plot should look like
Figure 187.
c) Explain why the filtered signal y[n] still looks like the original
weak signal x2 [n] consisting of unit impulses, even though it is
missing some frequency components.
298

Suggested solutions: Arbitrary Frequency Response Filters

1. Consider two signals of the form:

5
x1 [ n ] = ∑ Ai cos(2π fi i) + wn ,
i =1
x2 [n] = a1 δ[n − 8192] + a2 δ[n − 9192] + a3 δ[n − 7192],

where | Ai | > | a j | for every pair i, j, here wn is white noise. 55 In 55


White noise is considered as a ran-
Listing 57 is code to generate these signals and plot them. dom variable with a normal distribu-
tion with µ = 0 and variance σ2 = 1
import m a t p l o t l i b . pyplot as p l t here
import numpy as np

N = 16384
n = np . arange (N)
freqs = [0.0003 , 0.012 , 0.055 , 0.102 , 0.85] # x1 [ n ]
frequencies
A = [ 1 e5 , 5 e5 , 1 e3 , 1 e4 , 0 . 5 e4 ] # x1 [ n ]
amplitudes
x1 = np . z e r o s (N)

# Create the f i r s t s i g n a l .
f o r i , f i n enumerate ( f r e q s ) :
x1 += A[ i ] * np . cos ( np . p i * f * n + np . random . randn ( 1 ) )

# C r e a t e t h e second s i g n a l ( t h e weak s i g n a l ) .
x2 = np . z e r o s (N)
x2 [N//2] = 1 0 . 0
x2 [N//2 + 1 0 0 0 ] = −5.0
x2 [N//2 − 1 0 0 0 ] = 1 . 0

x = x1 + x2

# Plot the s i g n a l s .
plt . plot (n , x )
p l t . x l a b e l ( " Samples " )
p l t . y l a b e l ( " $x [ n ] $ " )
# C a l l t h i s i f needed :
# p l t . show ( )

Listing 57: Example signal code

a) To estimate the spectrum, we use the Hann window to avoid


spectral leakage. The code for computing the spectrum using
the Hann window is shown in Listing 58.
import m a t p l o t l i b . pyplot as p l t
import numpy as np Figure 190: Noisy signal we want to
from s c i p y . s i g n a l . windows import hann filter

def c o n v e r t _ t o _ d e c i b e l ( x : np . ndarray ) −> np . ndarray :


" " " Function t o c o n v e r t t o dB . " " "
r e t u r n 1 0 * np . l o g 1 0 ( np . abs ( x ) * * 2 )
299

N = 16384
nn = np . arange (N)
freqs = [0.0003 , 0.012 , 0.055 , 0.102 , 0.85] # x1 [ n ]
frequencies
A = [ 1 e5 , 5 e5 , 1 e3 , 1 e4 , 0 . 5 e4 ] # x1 [ n ]
amplitudes
x1 = np . z e r o s (N)

# Create the f i r s t s i g n a l .
f o r i , f i n enumerate ( f r e q s ) :
x1 += A[ i ] * np . cos ( np . p i * f r e q s [ i ] * nn + np . random . randn
(1) )

# C r e a t e t h e second s i g n a l ( t h e weak s i g n a l ) .
x2 = np . z e r o s (N)
x2 [N//2] = 1 0 . 0
x2 [N//2 + 1 0 0 0 ] = −5.0
x2 [N//2 − 1 0 0 0 ] = 1 . 0

x = x1 + x2

# Hann window t o reduce s p e c t r a l l e a k a g e .


w = hann (N)

xw = np . f f t . r f f t (w* x , 2 *N) # Use FFT t o compute t h e


spectrum .
om_freqs = np . l i n s p a c e ( 0 , np . pi , num= l e n (xw) ) # P a r t i t i o n
the i n t e r v a l ( 0 , pi ) .

p l t . p l o t ( om_freqs , c o n v e r t _ t o _ d e c i b e l (xw) )
p l t . x l a b e l ( r " $\hat {\omega } $ ( rad / sample ) " )
p l t . y l a b e l ( r " $|\hat { x } _ {w} [ k ] | ^ { 2 } $ ( dB ) " )
p l t . t i t l e ( " Spectrum o f $x [ n ] $ " )
# C a l l t h i s i f needed :
# p l t . show ( )

Listing 58: Spectrum for noisy signal in Figure 190

b) To filter out the noise, we use a filter that will remove strong
spectral components and keep the weak components constant
at 1.0. That is, our filter will be:

 1 , for strong spectral components,
ĥ[k] = | x̂w [k]| Figure 191: Spectrum in dB for the
1, otherwise. signal shown in Figure 190

Where x̂w [k ] is the Hann windowed tapered signal. Looking at


the spectral power, we can determine which components need
to be lowered. After doing this we apply the inverse DFT to
obtain our filtered signal,
−1
x [k] = F D { x̂w [k]ĥ[k]}.

The implementation of this is shown in Listing 59.


import m a t p l o t l i b . pyplot as p l t
import numpy as np
from s c i p y . s i g n a l . windows import hann
300

def c o n v e r t _ t o _ d e c i b e l ( x : np . ndarray ) −> np . ndarray :


" " " Function t o c o n v e r t t o dB . " " "
r e t u r n 1 0 * np . l o g 1 0 ( np . abs ( x ) * * 2 )

N = 16384
n = np . arange (N)
freqs = [0.0003 , 0.012 , 0.055 , 0.102 , 0.85] # x1 [ n ]
frequencies
A = [ 1 e5 , 5 e5 , 1 e3 , 1 e4 , 0 . 5 e4 ] # x1 [ n ]
amplitudes
x1 = np . z e r o s (N)

# Create the f i r s t s i g n a l .
f o r i , f i n enumerate ( f r e q s ) :
x1 += A[ i ] * np . cos ( np . p i * f * n + np . random . randn ( 1 ) )

# C r e a t e t h e second s i g n a l ( t h e weak s i g n a l ) .
x2 = np . z e r o s (N)
x2 [N//2] = 1 0 . 0
x2 [N//2 + 1 0 0 0 ] = −5.0
x2 [N//2 − 1 0 0 0 ] = 1 . 0

x = x1 + x2

# Hann window t o reduce s p e c t r a l l e a k a g e .


w = hann (N)

xw = np . f f t . r f f t (w* x , 2 *N) # Use FFT t o


compute t h e spectrum .
om_freqs = np . l i n s p a c e ( 0 , np . pi , num= l e n (xw) ) # Partition
the i n t e r v a l ( 0 , pi ) .

# Define a f i l t e r t o reduce s t r o n g s p e c t r a l components .


h = np . ones ( l e n (xw) ) # I n i t i a l i z e each e n t r y t o 1 .

# Lower t h e s t r o n g s p e c t r a l components .
# Look a t t h e pr ev io u s e x e r c i s e output p l o t t o determine
the i n t e r v a l s
# a l t e r n a t i v e l y , p l o t t h e spectrum with samples on x− a x i s
i n s t e a d o f \hat {\omega } .
h [ : 1 0 5 0 ] = 1 . 0 / np . abs (xw [ : 1 0 5 0 ] )
h [ 1 5 0 0 : 1 8 6 0 ] = 1 . 0 / np . abs (xw [ 1 5 0 0 : 1 8 6 0 ] )
h [ 1 3 5 0 0 : 1 4 2 0 0 ] = 1 . 0 / np . abs (xw[ 1 3 5 0 0 : 1 4 2 0 0 ] )

# P l o t t h e s p e c t r a l power t o compare t h e two s i g n a l s .


p l t . p l o t ( om_freqs , c o n v e r t _ t o _ d e c i b e l (xw) , l a b e l = " O r i g i n a l
spectrum " )
p l t . p l o t ( om_freqs , c o n v e r t _ t o _ d e c i b e l (xw * h ) , l a b e l = "
Windowed spectrum " )
p l t . x l a b e l ( r " $\hat {\omega } $ ( rad / sample ) " )
p l t . y l a b e l ( " Power o f spectrum ( dB ) " )
p l t . legend ( )
# C a l l t h i s i f needed :
# p l t . show ( )

# F i n a l l y , i n v e r s e DFT t o o b t a i n t h e f i l t e r e d s i g n a l .
f i l t e r _ s i g n a l = np . f f t . i r f f t ( h * xw)
plt . plot ( f i l t e r _ s i g n a l )
301

p l t . x l a b e l ( " Samples " )


plt . t i t l e ( " Filtered signal " )
# C a l l t h i s i f needed :
# p l t . show ( )

Listing 59: Filtering of the signal

Running this code will generate the plots shown in Figure 192
and 193.

Figure 193: Filtered signal, the weak


signal is now visible

Figure 192: The comparison of the


spectral power

c) The filter in this case reduces the strong frequencies while


at the same time keeping lower frequencies fixed. The effect
is that the original signal which had a lot of noise coming
from x1 [n] will have that noise significantly reduced. The
remaining information in the signal is the other part, being
x2 [n], which consists of unit impulses for which the frequency
components were kept at 1. Therefore, the filtered signal looks
like x2 [n] even though a lot of frequency components have been
removed.
Programming Assignment 3

In 2017, the Nobel Prize in physics was awarded to Rainer Weiss,


Barry Barish and Kip Thorne for the discovery of gravitational waves.
Only two years earlier, on September 14, th 2015, 09:50:45 UTC, the Figure 194: Artist’s depiction of gravi-
tational waves created by a merger of
two Laser Interferometer Gravitational-Wave Observatory (LIGO) two neutron stars. Credits: US National
instruments detected a gravitational wave for the first time in history. Science Foundation.
The existence of gravitational waves had been predicted by Einstein’s
general theory of relativity, but never before detected in situ.
The first gravitational wave event detected by LIGO is thought
to be created from a collision of two black holes, which sends out
a localized chirp like pulse in the space-time. LIGO utilizes two
detectors, which are spaced 3000 km apart. These detectors measure
strain (∆L/L) as a function of time. Here ∆L is the variation in length
and L is the total length in which the variation is measured. In
other words, strain is the normalized variation in length L of the
interferometer line due to gravitational-wave. Figure 198 provides a
high level overview of the measurement.
LIGO uses two geographically separated stations to measure gravi-
tational waves: Hanford (H1 ), and Livingston (L1 ). The gravitational
wave propagates at the speed of light c ≈ 3 · 108 m/s. If the same
signal is detected at two different places with a time difference less
than or equal to the speed of light propagation time between the
sensors, then this provides more confidence that the event is in fact
real, and not caused by for example local seismic activity. A third
sensor would allow determining the direction of arrival based on
time of arrival.
The LIGO data is severely corrupted with instrumental noise.
This noise is much larger in amplitude than the gravitational wave Figure 195: The Hanford and Liv-
signal. However, this noise is very narrowband in nature, and it can ingston interferometers. Credits: LIGO.
be filtered out using relatively basic signal processing techniques
without affecting the relatively broad band gravitational signal
very much. Such filtering is routinely used by LIGO to improve the
sensitivity of the instrument.
Verification of scientific results is an important part of science. UiT
does not have the resources for building a giant interferometer, and
304

this course really isn’t about solving Einstein’s field equations, so we


won’t be able to reproduce all the results. However, we can verify
the signal processing part. In this programming assignment, your
task is to independently develop signal processing software to verify
that you can also find the gravitational wave signature in the LIGO
measurements.

Instructions

We expect you to complete the listed signal processing tasks. The


submission form is a written report, which answers the questions
given in each part of this assignment. The report should include the
code with comments that indicate what each part of the program
does.
You will only need to know about signal processing concepts
taught in FYS-2006. You do not need to know anything about grav-
itational waves or the LIGO instrument in order to complete the
assignment. The lecture notes for the course contain several helpful
signal processing examples. Take a look at lecture notes for Week 43
on spectral analysis, and arbitrary frequency response filters.

Instruction for reading the data

To begin, download the data files and a simple program that demon-
strates reading these files from this location: https://bit.ly/
3bIcRlB. These data files contain real measurements from LIGO
starting at 2015-09-14T09:50:30 UTC. After downloading the files, the
next step is to read the strain signal from the data files. In Python,
this can be done using the h5py module56 . 56
Make sure you have the h5py module
installed on your computer!
import h5py
h = h5py.File("file.hdf5","r")
data = h["strain/Strain"][()]

You will need to read two data vectors. One for the Livingston
station (L1 ) and one for the Hanford station (H1 ). We will use the
symbol x H [n] of the Hanford signal and x L [n] for the Livingston
signal.

1. Data

The sample rate of both of the signals is f s = 4096 Hz. The samples
in both signals are synchronized in time, i.e., sample n in signal x H [n]
and x L [n] occur at the same time.
305

a) Write code to read the Hanford and Livingston signals from the
data file.

b) How many samples are in each of the signals: x H [n] and x L [n]?

c) How many seconds of signal does each of the two data vectors
x H [n] and x L [n] represent?

d) The gravitational wave signal is a one dimensional real-valued sig-


nal that is spectrally confined to the frequency range [−300, 300]
Hz. Is the signal sampled at a sufficiently high sample rate to
retain all the gravitational wave information?

e) What is the sample spacing in units of seconds?

2. Plotting the data

In order to see what the signals look like, you will need to plot the
data.

a) Plot the signals x H [n] and x L [n], with time in seconds on the
horizontal axis and strain on the vertical axis. Assume that time
at the beginning of the signal array starts at 0 seconds. Label the
axes of your plot. Use separate plots for x H [n] and x L [n] signals.
Hint: you can use the plt.plot(t,signal) command found in
Matplotlib. Use an array t to denote the seconds of each sample
of the array signal.

b) What are the minimum, maximum, and mean values of the x H [n]
and x L [n] signals?

3. Selecting a tapered window function

You will need to apply a discrete Fourier transform to analyze the


spectral content of the signal. You will need to select a suitable
tapered window function in order to obtain a good rejection of out of
band signals. In this task, we’ll use a synthetic narrowband signal to
compare the performance of a discrete Fourier transform (DFT) based
spectral analysis using a tapered window to spectral analysis done
without a tapered window.
In order to calculate the magnitude spectrum of a signal x [n] of
length N, you need to evaluate a DFT on the signal:

N −1 2π
x̂ [k] = ∑ x [n]e−i N kn . (792)
n =0
306

and the windowed signal:


N −1 2π
x̂w [k] = ∑ w[n] x [n]e−i N kn . (793)
n =0

Hint: Use the fft function to evaluate the DFT. This function is
available in Python as numpy.fft.fft.

a) Chose a tapered window function and implement it. Hint: you


can use window functions available in the scipy.signal module.

b) In order to get an idea of how your window function behaves,


apply it to a sinusoidal signal

x [n] = cos(2π f n/ f s ) (794)

with frequency f = 31.5 Hz, which is sampled at f s = 4096


Hz. Sample signal at n ∈ [0, 1, 2, · · · , 4095]. Make a plot of the
signal x [n] and the windowed signal w[n] x [n]. Use a window
of the same length as your signal N = 4096. Hint: You can use
numpy.arange(N) to create a sequence of integers between 0 and
N − 1.

c) The FFT algorithm will evaluate x̂ [k] at integer values of k be-


tween 0 and N − 1. What frequencies f k in hertz do frequencies
ω̂k = 2πk/N in radians per sample correspond to on the principal
spectrum (− f s /2 < f k < f s /2)?

d) Which values of k correspond to a frequency f k that is nearest to


31.5 and −31.5 hertz?

e) Estimate the power spectrum of the windowed signal w[n] x [n]


(with tapering) and the signal x [n] (without tapering). Plot
the power spectrum in decibel scale (power) for both. Use fre-
quency in Hz on the horizontal axis and magnitude squared
10 log10 (| x̂ [k]|2 ) (decibels) on the vertical axis. Plot both positive
and negative frequencies. Hint: You can use numpy.fft.fftfreq
and numpy.fft.fftshift to determine what frequency (in hertz)
each FFT bin k corresponds to, and to order the frequencies in
ascending order.

f) Mark the locations of 31.5 and -31.5 Hz on the plot of the power
spectrum.

g) The frequency corresponding to ω̂ = π radians per second or


f = 2048 Hz is non-zero for both x̂ [k] and x̂w [k ]. However, your
test signal has a frequency of 31.5 Hz. Why is there a non-zero
frequency component anywhere else than at 31.5 Hz when you
analyze the signal?
307

h) How many decibels is the frequency response better with the


tapered window function (10 log10 (| x̂w [k]|2 )) than without
(10 log10 (| x̂ [k]|2 )) at frequency ω̂ = π (radians per second)?

4. Estimating the spectrum of the LIGO signal

a) Calculate the power spectrum of the LIGO signals | x̂ L [k]|2 and


| x̂ H [k]|2 . Here x̂ L [k] is the windowed DFT of the Livingston signal
x L [n] and x̂ H [k] is the windowed DFT of the Hanford signal x H [n].
The windowed DFT is obtained using

N −1 2π
x̂ [k] = ∑ w[n] x [n]e−i N kn (795)
n =0

Perform the DFT over the whole dataset, i.e., N is the number of
samples in the whole signal vector. Use the window function that
you have chosen in the previous exercise, but make sure that use
a window of length N, where N is the LIGO data vector length.
Hint: Use FFT.

b) Plot the results with frequency in Hz in the horizontal axis and


power using decibel scale on the vertical axis. Make one plot
for the Hanford and one plot for the Livingston. Plot only the
positive frequencies.

c) On what frequencies are there strong spectral components in


the Hanford and Livingston power spectra? Use Hz as the unit
of frequency. Identify up to 12 frequency bands that contain
narrowband interference. Label these regions on the plot of the
power spectrum.

5. Whitening filter

In order to remove instrumental noise, you will next implement a


whitening filter and apply it to the LIGO signal. A whitening filter
is a filter that modifies the amplitudes of each spectral component
in such a way that the magnitude spectrum of the filter output is
constant-valued.
A whitening filter can be implemented as an FIR filter, which in
frequency domain can be implemented as a multiplication:

ŷ[k] = ĥ[k] x̂ [k]. (796)

Here ŷ[k] is the DFT of the output of the filter, ĥ[k] is the DFT of the
whitening filter, and x̂ [k] is the windowed DFT of the input signal
x [ n ].
308

The purpose of a whitening filter is to filter the signal in such a


way that the magnitude of the output signal is unity: |ŷ[k]| = 1. This
can be obtained using a filter of the form:

1
ĥ[k] = . (797)
| x̂ [k]|

a) Show that ĥ[k] as defined in equation 797 will filter signal x [n] in
such a way that |ŷ[k]| = 1.

b) Implement a whitening filter in frequency domain ĥ[k] for the


LIGO data. Implement a separate filter for the Hanford and
Livingston signals. Use all the signal as input to the windowed
DFT when calculating x̂ [k]. Do not filter the signal in smaller
blocks!

c) Use an inverse discrete Fourier transform to transform the


whitened signal ŷ[k] into time-domain. Do this for both Liv-
ingston and Hanford signals separately (obtaining y L [n] and
y H [n]).

d) Plot the whitened signals y H [n] and y L [n] for both Hanford and
Livingston. Use x-axis for time in seconds (t ∈ [0, tmax ]) and y-axis
for whitened strain y[n]. The gravitational wave signal is in the
middle of the signal, between 16.2 and 16.5 seconds. If you’ve
done everything correctly, you should see the gravitational wave
signal. It looks like a chirp (see plot in Figure 196). However,
because we are not done yet, the signal will look noisy.

6. Low-pass filtering

The gravitational wave signal is at frequencies below 300 Hz. Design


a simple running mean averaging low-pass filter
L −1
1
y[n] =
L ∑ x [n − k] (798)
k =0

that will attenuate spectral components with frequencies above 300


Hz.

a) Find an integer value of L such that the filter will reduce the
power of frequency components at f = 300 Hz by approximately
-6 dB compared to the filter output for a f = 0 Hz signal.

b) Plot the power spectral response of the filter in dB scale (10 log10 |H (ω̂ ) |2 ),
where H (ω̂ ) is the discrete-time Fourier transform of the FIR
filter coefficients of the averaging filter. Use frequency on the
horizontal axis in Hz. Label the -6 dB point in frequency and in
309

power spectral response, verifying that the -6 dB point is close to


300 Hz.

c) What is the time delay τ to the signal introduced by the filter, in


seconds?

d) Apply the running mean average low-pass filter on the whitened


Hanford and Livingston signals (y H [n] and y L [n]).

e) Undo the effects of the filter time-delay by shifting the signal in


time. You can do this by adjusting the time variable t′ = t − τ,
instead of filtering the signal.

f) Plot the low pass filtered whitened Hanford and Livingston


signals. You should see the gravitational wave signal more clearly
now. Use the horizontal axis for time and the vertical axis for
the signal amplitude. Plot only the time interval between 16.1
and 16.6 seconds where the gravitational wave signal is located.
Compare your plot with Figure 196. Your plot should be similar,
but not necessarily exactly the same, as your filter is not exactly
the same as the one used for Figure 196.

Figure 196: The figure is from: B. P.


Abbott et al. (LIGO Scientific Collabo-
ration and Virgo Collaboration) Phys.
Rev. Lett. 116, 061102 – Published 11
February 2016

7. Time delay

Gravitational waves are expected to propagate at the speed of light.


The Hanford and Livingston detectors are separated by about 3000
km. A gravitational wave will propagate this distance in about 10 ms.
The gravitational wave angle of arrival is not known, but the
relative time delay between the signal detected at Hanford and
310

Livingston is expected to be −10 < τ < 10 ms, if it is moving at the


speed of light in vacuum.

a) Determine the time separation between the two signals by plotting


the magnitudes of the filtered gravitational wave signals (|y L [n]|
and |y H [n + n0 ]|) with different delays n0 on the same plot. Try
different values of n0 until the signals are approximately aligned
in time. The magnitude of the signal is used to avoid phase-time
ambiguities. Hint: Use magnitude, not amplitude. Magnitude can
be obtained using numpy.abs.

b) What value do you obtain for the sample delay n0 ?

c) What time delay τ in seconds does the sample delay n0 corre-


spond to?

d) Is the time delay τ in agreement with gravitational-wave propaga-


tion speed (i.e., that −10 < τ < 10 ms)?

8. Dynamic spectrum

Study the time-frequency behavior of the gravitational wave signal.

a) Calculate the dynamic spectrum (spectrogram) of the low-pass fil-


tered whitened signal | x̂ [t, k]|, where t is time and k is frequency.
Figure 197: A dynamic spectrum plot of
b) Plot the dynamic power spectrum | x̂ [t, k]|2 in dB scale. Perform the gravitational wave signal.
this in such a way that you can see the time-frequency response
behavior of the gravitational wave signal clearly. Plot your result
at between t = 15.5 s and t = 17 s. Hint: You will need to use
zero-padding, a tapered window, and overlapping windows. An
example of the time-frequency domain behavior of the gravita-
tional wave signal is shown in Figure 197.

9. Extra task

For earning an extra point, improve any part of the signal processing
in a way that you see fit. Document your improvements. You can,
e.g., try to create a filter that removes only the strong frequency
components from the signal, or you can use a better low-pass filter.
311

Figure 198: The gravitational wave


measurement using a Michelson-
Morley interferometer. Credits: Johan
Jarnestad, The Royal Swedish Academy
of Sciences.
Z-transform

The z-transform is a general frequency domain representation of


discrete-time signals. A special case of the z-transform is the discrete-
time Fourier transform.
The main benefit of the z-transform is that it allows us to use
algebraic manipulations of complex-valued polynomials to study
the frequency domain behavior of discrete-time signals. This can
simplify calculations and provide further mathematical intuition. The
z-transform also provides a framework that can be used to derive
numerically efficient low-order filters57 . The z-transform also has 57
filters that require only a few compu-
applications, e.g., in statistics and statistical signal processing. tations to apply on signals

System function H(z)


x [n] = zn H(z)zn
LTI h[n]
The system function H(z) provides an alternative frequency domain
representation of the frequency response of LTI systems. We’ll start Figure 199: The motivation for the
z-transform is studying how an LTI
by defining what this is. system modifies a signal zn with z ∈ C.
Recall that a discrete-time LTI system can be represented as a
convolution of an input signal x [n] with the impulse response h[n] of
the LTI system:

y[n] = T { x [n]} (799)


= h[n] ∗ x [n] (800)

= ∑ h[k] x [n − k] (801)
k =−∞

The system function arises from investigating what is the output of


this LTI system when using an input signal of the form x [n] = zn ,
where z ∈ C is an arbitrary complex number. What happens to this
314

signal when it is fed into an LTI system? Let’s find out:

y[n] = T { x [n]} (802)



= ∑ h[k] x [n − k] (803)
k =−∞

= ∑ h [ k ] zn−k (804)
k =−∞

!
= ∑ h[k]z −k
zn (805)
k =−∞
| {z }
H(z)

= H(z)zn . (806)

The output signal is the original input signal zn multiplied with H(z).
This term is called the system function of the LTI system:


H(z) = ∑ h [ k ] z−k (807)
k =−∞

Sometimes the term transfer function is used instead of the system


function 58 . They both refer to H(z). 58
The term transfer function is used
The system function also happens to be a transformation called e.g., by the SciPy z-transform functions.

the z-transform of the signal h[n]. Recall that this is similar to the
2
frequency response of an LTI system, which we investigated earlier. 0.8n eiω̂n
There we investigated the response of a signal of the form x [n] =
n
Aeiω̂n for a discrete-time LTI system.

What is the meaning of zn ? −2

5 1.2n eiω̂n
What does a signal of the form x [n] = zn look like? The variable
z ∈ C is a complex number. We can express a complex number in n
polar form z = Aeiω̂ , where A = |z| and ω̂ = ∠z. This means that:

−5
zn = An eiω̂n (808)

This signal is exponentially decaying when A < 1, exponentially 1n eiω̂n


1
growing when A > 1, or constant in amplitude when A = 1. The
n
meaning of ω̂ is the familiar normalized angular frequency in units
of radians per sample.
Figure 200 shows several examples of a signal of the form zn = −1
A eiω̂n u[n]. The unit step function u[n] is used to restrict the values
n

of zn to be non-zero only for positive values of n. Figure 200: Several examples of the
signal zn = An eiω̂n with different values
The signal zn is more general than just a complex sinusoidal signal
of A.
eiω̂n . It allows the amplitude of the signal to exponentially grow,
decay, or stay constant.
315

Z-transform

Forward transform
The forward z-transform X (z) of an arbitrary discrete-time signal
x [n] is defined as:


X (z) = Z { x [n]} = ∑ x [ k ] z−k . (809)
k =−∞

The z-transform is not necessarily defined everywhere, as the sum


may not converge for some values of z. The set of points SROC on
the complex plane where the sum X (z) converges to a finite value is
known as the region of convergence:
Im{z}
SROC = {z ∈ C : | X (z)| < ∞} (810) 1 z = eiω̂

Re{z}
In terms of filter design, the region of convergence indicates the set of −2 −1 1 2
signals for which a filter provides a finite output.
−1

Reverse transform Figure 201: The function z = eiω̂ is


a parametric curve for a circle on the
The inverse z-transform is defined as a contour integral: complex plane.

1
I
x [n] = Z −1 { X (z)} = X (z)zn−1 dz . (811)
2πi C

Here C is a closed loop that encircles the origin within the region of
convergence.
Solving the inverse z-transform directly using this formula re-
quires the use of contour integration59 . However, one typically does 59
This topic is beyond the scope of this
not need to use this formula, because with practical signal processing course. You will not need to know how
to solve Equation 811 in the exam.
applications, it is nearly always possible to algebraically manipulate
the z-transform polynomial X (z) in such a way that it is possible
to use a table of elementary z-transform pairs to obtain an inverse
z-transform symbolically.

Relationship to the discrete-time Fourier transform x [n]

It is possible to determine the DTFT of a signal using a z-transform of Z DTFT


the signal X (z). This is done by substituting z = eiω̂ . In other words,
evaluating the z-transform along the unit circle. X (z) x̂ (ω̂ )
z = eiω̂
Recall the definition of the z-transform:
∞ Figure 202: The z-transform X (z)
X (z) = ∑ x [ k ] z−k . (812) evaluated on the unit circle z = eiω̂
corresponds to the discrete-time Fourier
k =−∞
transform.
316

If we replace z = eiω̂ , we obtain the discrete-time Fourier transform:


x̂ (ω̂ ) = ∑ x [k]e−iω̂k . (813)
k =−∞

In a sense, the z-transform is a more general frequency domain


representation of a discrete-time signal than a discrete-time Fourier
transform, as a subset of the z-transform is the discrete-time Fourier
transform.
One application is that the z-transform of the impulse response
h[n] of an LTI system can be used to obtain the frequency response of
an LTI system:
H(ω̂ ) = H(z)|z=eiω̂ (814)

In other words, if one knows the system function, it is possible to


evaluate the frequency response.

Linearity

The z-transform is a linear transformation:

Z
α1 x1 [ n ] + α2 x2 [ n ] ←
→ α 1 X1 ( z ) + α 2 X2 ( z ) . (815)

To show this, let’s investigate the following linear combination of


signals x [n] = α1 x1 [n] + α2 x2 [n]. The z-transform for x [n] is:

X (z) = ∑(α1 x1 [n] + α2 x2 [n])z−n (816)


n
= α1 ∑ x1 [ n ] z − n + α2 ∑ x2 [ n ] z − n (817)
n n
= α 1 X1 ( z ) + α 2 X2 ( z ) . (818) 1.5
δ [ n − n0 ]

The result is a linear combination of the z-transforms of the two


1
individual signals that form the signal x [n].

0.5
Time-shifted unit impulse δ[n − n0 ]

One of the elementary z-transform pairs is a time-shifted unit im- n


pulse: n0

Z
→ X ( z ) = z − n0 .
x [ n ] = δ [ n − n0 ] ← (819) Figure 203: A time-shifted unit impulse
is a basic building block of arbitrary
signals. If you know the z-transform
A time-shifted unit impulse is a basic building block of discrete-time of this signal, you can determine the
signals, which have a finite number of non-zero elements. z-transform of an arbitrary finite-length
discrete-time signal, as it will be a
This can be easily obtained using Equation 809 for the forward
linear combination of time-shifted unit
impulses.
317

z-transform:

X (z) = Z {δ[n − n0 ]} (820)



= ∑ δ [ k − n0 ] z − k (821)
k =−∞
− n0
=z (822)

The special case of this is the unit impulse δ[n], which results in
Z {δ[n]} = 1.
It is also possible to evaluate the reverse transform of:

X ( z ) = z − n0 , (823)
Im{z}
by using Equation 811. We select the unit circle |z| = 1 as the contour 1 z = eiω̂
of integration C. It is easy to see that the unit circle is in the region of Re{z}
convergence, as the system function is of constant magnitude along −2 −1 1 2
the unit circle |e−iω̂n0 | = 1.
−1
Integration along the unit circle is achieved with variable substi-
tution z = eiω̂ . By sweeping the parameter ω̂ between 0 and 2π, the
Figure 204: The function z = eiω̂ is
variable z draws a unit circle on the complex plane. This is the closed
a parametric curve for a circle on the
curve C. complex plane.
With the chosen variable substitution, we find that dz/dω̂ = ieiω̂
and we can substitute the infinitesimal dz with ieiω̂ dω̂.
The limits of integration, which result in a closed counterclockwise
loop around the contour of integration, are: [0, 2π ]. We now have
everything that we need to inverse z-transform X (z) = z−n0 :

x [ n ] = Z −1 { z − n0 } (824)
1
I
= z−n0 zn−1 dz (825)
2πi C
1
I
= zn−n0 −1 dz (826)
2πi C
Let’s now insert dz = ieiω̂ dω̂, z = eiω̂ and use the limits 0, 2π for ω̂:
1 2π Z
x [n] = eiω̂ (n−n0 −1) ieiω̂ dω̂ (827)
2πi 0
Z 2π
1
= eiω̂ (n−n0 ) dω̂. (828)
2π 0
Let’s investigate two cases. In the first case n ̸= n0 . It is easy to see
that this integral is zero when n ̸= n0 . This is because n − n0 is an
integer. The second case is when n = n0 , we then get:
Z 2π
1
x [ n0 ] = dω̂ (829)
2π 0
= 1. (830)

Combining these two results, we get:

x [ n ] = δ [ n − n0 ] (831)
318

Arbitrary signals of finite length

By using Equation 819 and linearity, you can forward and reverse
z-transform an arbitrary signal x [n] with a finite number of non-zero
values:
N N
Z
x [n] = → X ( z ) = ∑ bk z − n k
∑ bk δ [ n − n k ] ← (832)
k =0 k =0

Here nk denotes the time-shift of the kth non-zero element x [nk ] = bk . 4


h[n]
A large fraction of practical signals are of this form, so this is a very
useful formula.
2

Example: System function of an FIR filter


n
A discrete-time LTI system y[n] = T { x [n]} is defined using the 3 5 7
following difference equation:
−2
y[n] = x [n − 3] − 2x [n − 5] + 3x [n − 7]. (833)

This is a finite impulse response filter (shown in Figure 205), with the Figure 205: A finite impulse response
following impulse response: h [ n ].

h[n] = δ[n − 3] − 2δ[n − 5] + 3δ[n − 7]. (834)

Using Equation 832 we get the following system function (z-transform


of the LTI system impulse response):

H(z) = z−3 − 2z−5 + 3z−7 . (835)

We can also multiply this with z7 /z7 and obtain a polynomial frac-
tion with more familiar positive valued exponents of the following
form:
z4 − 2z2 + 3
H(z) = (836)
z7
This means that the system function in this case is a polynomial
fraction, with a fourth order polynomial on the numerator and a
seventh order polynomial on the denominator.

Example: Algebraic inverse z-transform


Given a system function H(z), it is possible to go back into time
domain and to determine the impulse response h[n] of an FIR filter
using simple algebraic manipulations, instead of the more involved
contour integration discussed earlier. From the impulse response,
it is then possible to reconstruct a difference equation describing
the output of the discrete-time LTI system as a function of the input
signal.
319

For example, let’s say that we have a system function of the follow-
ing form:
3z3 − 2z + 1
H(z) = (837)
z2
We can find the inverse z-transform of H(z) purely by algebraic
manipulation. First, let’s write Equation 837 as:

H(z) = 3z1 − 2z−1 + z−2 . (838)

We can now use Equation 832, to obtain the impulse response, which
is the inverse z-transform of H(z):

h[n] = 3δ[n + 1] − 2δ[n − 1] + δ[n − 2]. (839) 4


h[n]

This is shown in Figure 206. We can see that the difference equation
for this LTI system can be written as 2

y[n] = 3x [n + 1] − 2x [n − 1] + x [n − 2]. (840)


n
3 5 7

Time-shift theorem
−2
The time-shift theorem allows us to obtain the z-transform of a time
delayed signal using polynomial multiplication:
Figure 206: A finite impulse response
h [ n ].
Z
→ Y ( z ) = z − n0 X ( z )
y [ n ] = x [ n − n0 ] ← (841)

Here X (z) is the z-transform of signal x [n], and Y (z) is the z-transform
of signal x [n − n0 ]. We’ll use this property later to prove the convolu-
tion theorem for the z-transform.
It is easy to see that the time-shift property follows from the
definition of the z-transform. Let us assume that the z-transform of
the signal x [n] is X (z):

X (z) = ∑ x [ k ] z−k . (842)
k =−∞

If we now delay x [k] by n0 samples, and look at the z-transform of


the delayed signal y[n] = x [n − n0 ], we get:

Y (z) = ∑ y [ k ] z−k , (843)
k =−∞

= ∑ x [ k − n0 ] z − k , (844)
k =−∞

= z − n0 ∑ x [ℓ]z−ℓ , (845)
ℓ=−∞
= z − n0 X ( z ) (846)

We used variable substitution: k − n0 = ℓ.


320

Convolution theorem

The convolution theorem is of fundamental importance for theoretical


and practical applications in signal processing. This theorem states
x [n] y[n] = h[n] ∗ x [n]
that convolution in time domain is multiplication in frequency domain. LTI
X (z) Y (z) = H(z) X (z)
We’ve already encountered this with Fourier transforms. Now we’ll
introduce the z-transform equivalent. Figure 207: One consequence of the
If the signal a[n] is a convolution of signals b[n] and c[n] and the convolution theorem is that the z-
transform of the output of an LTI
z-transforms of these signals are A(z), B(z), and C (z), we have the system Y (z) is the z-transform of the
following z-transform rule: input signal X (z) multiplied with the
system function H(z).
Z
a[n] = b[n] ∗ c[n] ←
→ A ( z ) = B ( z ) C ( z ). (847) h[n]
1
One application of this is that we can use the z-transform to investi-
gate the z-domain representation of the output of an LTI system:
n
Z
y[n] = h[n] ∗ x [n] ←
→ Y (z) = H(z) X (z) (848)

Here H(z) is the system function, which is the z-transform of the


−1
impulse response h[n].
4
x [n]
Proof. The proof of the z-transform convolution theorem is as follows.
We start with the definition of a convolution operation: 3

a[n] = b[n] ∗ c[n] = ∑ b [ k ] c [ n − k ]. (849) 2


k

z-transforming a[n], one obtains: 1

A(z) = ∑(b[n] ∗ c[n])z −n


(850)
n 4
! y[n]

=∑ ∑ b[k]c[n − k] z −n
. (851)
n
2
k
!
= ∑ b[k] ∑ c [ n − k ] z−n
n
(852)
k n
| {z }
z−k C (z) −2
= ∑ b[k]z −k
C (z) (853)
k
! Figure 208: A convolution of signals
h[n] (top) and x [n] (middle) is y[n] =
= ∑ b[k]z −k
C (z) (854) x [n] ∗ h[n] (bottom).
k
| {z }
B(z)

= B(z)C (z) (855)

We rely on the time-shift theorem to obtain the z-transform of the


delayed signal b[n − k].
321

Example: convolution in z-domain


Let h[n] = δ[n] − δ[n − 1] and x [n] = 2δ[n] + δ[n − 1] + 3δ[n − 2].
These signals are shown in Figure 208.
In this case, H(z) = 1 − z−1 and X (z) = 2 + z−1 + 3z−2 . What is
y[n] = h[n] ∗ x [n]? We can of course directly solve this in time domain,
but we can often more efficiently solve this in frequency domain.
We can obtain the result of the convolution using polynomial
calculations and z-transforms. The z-transform of y[n] is:
Y (z) = H(z) X (z) (856)
−1 −1 −2
= (1 − z )(2 + z + 3z ) (857)
−1 −2 −3
= 2−z + 2z − 3z (858)
= y [ 0 ] z 0 + y [ 1 ] z −1 + y [ 2 ] z −2 + y [ 3 ] z −3 . (859)
Since Y (z) = ∑n y[n]z−n , it follows that:
y[n] = 2δ[n] − δ[n − 1] + 2δ[n − 2] − 3δ[n − 3]. (860)
The signal y[n] is shown on the bottom panel of Figure 208.

Example: Two filter cascade


When two LTI systems are cascaded as shown in Figure 209, the
system is mathematically described by
w[n] = ∑ h1 [ℓ]x[n − ℓ] = h1 [n] ∗ x[n] (861) x [n]
h1 [ n ]
w[n]
h2 [ n ]
y[n]

and
Figure 209: A system that consists of
y[n] = ∑ h2 [ k ] w [ n − k ] (862) two LTI systems connected in series
(cascade).
k
= h2 [ n ] ∗ w [ n ] (863)
= h2 [n] ∗ (h1 [n] ∗ x [n]). (864) x [n] y[n]
h1 [ n ] ∗ h2 [ n ]
Because convolution is associative, a ∗ (b ∗ c) = ( a ∗ b) ∗ c, the two
convolutions can be combined as follows:
y[n] = h2 [n] ∗ (h1 [n] ∗ x [n]) (865)
= (h1 [n] ∗ h2 [n]) ∗ x [n]. (866)
| {z }
h[n]

Which is equivalent to a single convolution by a combined LTI sys-


tem with an impulse response that is a convolution of two impulse
responses h[n] = h1 [n] ∗ h2 [n]. In z-domain, the equivalent system
function that combines the two systems is:
Z
h [ n ] = h1 [ n ] ∗ h2 [ n ] ←
→ H(z) = H1 (z)H2 (z), (867)
and the z-transform of the output y[n] is:
Y (z) = H1 (z)H2 (z) X (z) = H(z) X (z). (868)
322

FIR filters as cascaded first order systems

It is possible to view an arbitrary finite impulse response filter as a


cascade of first order filters.
Let’s assume that all the non-zero elements of the impulse re-
sponse of a FIR filter lie in the range n ∈ {0, . . . , N }. If this is not
the case, we can always shift the impulse response in time so that
the first non-zero element of the impulse response is at sample index
n = 0 (time-shift theorem).
The impulse response is then:

N
h[n] = ∑ bk δ [ n − k ] . (869)
k =0

And the z-transform is the following polynomial fraction:

H(z) = b0 + b1 z−1 + b2 z−2 + · · · b N z− N (870)


= z− N (b0 z N + b1 z N −1 + b2 z N −2 + · · · b N ) (871)

The fundamental theorem of algebra states that a polynomial of degree


N has N roots when counting with multiplicity. This means that we
can represent the polynomial on the right-hand side of H(z) in the
following form:

N
H(z) = z− N ∏ (z − αk ) (872)
k =1
N
= ∏ (1 − α k z −1 ) (873)
k =1
N
= ∏ H k ( z ). (874)
k =1

In the second line, we have distributed z− N into the product. Here αk


is a root of the Nth order polynomial H(αk ) = 0.
We can see that the FIR filter is a product of elementary polynomi-
als:
H k ( z ) = 1 − α k z −1 . (875)

The convolution theorem says that convolution in time domain is


equivalent to multiplication in frequency domain. This means that
H(z) is the result of a convolution of N first order system functions
H k ( z ) = 1 − α k z −1 .
We can use the inverse z-transform on these systems to obtain the
impulse response of each first order filter:

hk [n] = Z −1 {Hk (z)} = δ[n] − αk δ[n − 1] (876)


323

The full impulse response h[n] is the result of these first order filters
convolved together:

h [ n ] = h1 [ n ] ∗ h2 [ n ] ∗ h3 [ n ] ∗ · · · ∗ h N [ n ]. (877)

This means that an FIR filter of arbitrary length can be decomposed


into elementary first order FIR filters. It is often easy to analytically
study how the individual first order filters behave (e.g., which signals
each filter attenuates).

Zeros of the system function of an FIR filter

The system function of an FIR filter H(z) is a polynomial. The zeros


of the system function H(z) are values z = αk , where the system
function evaluates to zero: H(αk ) = 0.
The location of the zeros determine the frequency domain be-
havior of an FIR system. This allows us to use analysis of complex-
valued polynomials to investigate the frequency domain behavior of
LTI systems.
A zero of the system function means that the LTI system will com-
pletely eliminate a signal of the form x [n] = αnk , i.e., the amplitude of
the output of the LTI system will be |T {αnk }| = 0.

Example: First order FIR filter


Consider the first order LTI system:

y [ n ] = x [ n ] − x [ n − 1] (878)

This has an impulse response:

h [ n ] = δ [ n ] − δ [ n − 1]. (879)

And the system function is a first order polynomial:

H(z) = 1 − z−1 . (880)

This is a first order polynomial with one zero at z−1 = 1. In other


words, α1 = 1.
This means that if we feed in a constant (DC) signal x [n] = α1n = 1,
the system will completely filter out this signal. This makes sense,
4
as this system is a simple high-pass filter, which we have already h[n]
studied before in the discrete-time Fourier transform chapter.
2

Example: Third order FIR filter


n
Let’s look at a more complicated system. Consider the following 1 2 3
system:
y[n] = x [n] − 2x [n − 1] + 2x [n − 2] − x [n − 3]. (881) −2

Figure 210: A finite impulse response


h[n] of a third order system.
324

This is an FIR filter with the following impulse response:

h[n] = δ[n] − 2δ[n − 1] + 2δ[n − 2] − δ[n − 3]. (882)

This has a z-transform of the form

H(z) = 1 − 2z−1 + 2z−2 − z−3 . (883)

This is a third order polynomial. Based on the fundamental theorem


of algebra, this polynomial has three zeros. These zeros are at α1 = 1, 60
It may be helpful to convert the
polynomial into a form with positive
α2 = ei 3 , and α3 = e−i 3 . You can find this using one of the many
π π

exponents: H(z) = z−3 (z3 − 2z2 + 2z1 −


solution strategies for finding the roots of a polynomial60 Figure 211 1) and to solve for z3 − 2z2 + 2z1 − 1 = 0
shows the locations of these zeros, marked with blue circles, on the using a symbolic calculator such as
Wolfram Alpha. You don’t need to
complex plane. make it a priority to learn how to solve
With the help of the roots of the polynomial, we can now write the higher order polynomials using pencil
and paper. We have computers for that.
polynomial in the following form:

H(z) = (1 − z−1 )(1 − ei 3 z−1 )(1 − e−i 3 z−1 ).


π π
(884)

This can also be viewed as a cascade of three different first order FIR
filters:

H(z) = H1 (z)H2 (z)H3 (z) (885)


= (1 − z−1 )(1 − e z−1 )(1 − e
i π3 −i π3
z −1 ) (886)
Im(z)
Using the z-transform, we can find the impulse responses of the
π
individual filters: h1 [n] = δ[n] − δ[n − 1], h2 [n] = δ[n] − ei 3 δ[n − 1],
h3 [n] = δ[n] − e−i 3 δ[n − 1]. The cascaded filter equivalent to h[n] is
π z = eiω̂

shown below.
Re(z)

x [n] y[n]
h[n]

x [n] y[n]
h1 [ n ] h2 [ n ] h3 [ n ]

Figure 211: Locations of the three zeros


π π
The first filter h1 [n] will completely remove a signal of the form (αk = {ei 3 n , e−i 3 n , 1}) of H(z) in the
complex plane are marked with blue
x [n] = α1n = 1. The second will completely remove signals of the circles. In this case, they all coincide
π
form x [n] = α2n = ei 3 n , and the third filter will remove signals of the with the unit circle.
form x [n] = α3n = e−i 3 n . This filter completely removes three spectral
π

components of the input signal with normalized angular frequencies


ω̂1 = 0, ω̂2 = π3 , and ω̂3 = − π3 .

Plotting H (ω̂ ) on the complex plane


In order to investigate what the system function H(z) looks like, I’ve
written some Python code to plot the magnitude and phase of the
325

system function H(z) on the complex plane for various values of z.


This code is shown in Listing 60. The plot is shown in Figure 212.
From this plot, one can see that |H(z)| obtains very low values
near the locations of the zeros of H(z). This means that signals with
normalized frequencies near ω̂1 = 0, ω̂2 = π3 , and ω̂3 = − π3 are also
reduced in amplitude as a result of the filtering operation.
The magnitude of H(z) is larger than unity on the left-hand side
of the plot. This means that complex sinusoidal signals with suffi-
ciently high normalized angular frequencies are amplified by the
system.
import m a t p l o t l i b . pyplot as p l t
import numpy as np

# Define system f u n c t i o n .
def h ( z ) :
r e t u r n ( ( 1 − z * * ( − 1 ) ) * (1 − np . exp ( 1 j * np . p i / 3 . 0 ) * z * * ( − 1 ) ) * (1 − np .
exp ( −1 j * ( np . p i /3) ) * z * * ( − 1 ) ) )

xx = np . l i n s p a c e ( − 3 , 3 , num=1000)
yy = np . l i n s p a c e ( − 3 , 3 , num=1000)
x , y = np . meshgrid ( xx , yy )

f i g , axs = p l t . s u b p l o t s ( 2 , 1 , f i g s i z e = ( 3 , 6 ) )

# P l o t magnitude o f system f u n c t i o n .
c = axs [ 0 ] . pcolormesh ( xx , yy , 1 0 . 0 * np . l o g 1 0 ( np . abs ( h ( x+1 j * y ) ) Figure 212: The magnitude and phase
* * 2 . 0 ) , vmin= −20 , vmax=20 , cmap= " j e t " ) angle of the system function plotted on
f i g . c o l o r b a r ( c , ax=axs [ 0 ] ) the complex plane. The unit circle is
om = np . l i n s p a c e ( 0 , 2 . 0 * np . pi , num=1000) shown in white.
axs [ 0 ] . p l o t ( np . cos (om) , np . s i n (om) , c o l o r = " white " )
axs [ 0 ] . s e t _ a s p e c t ( ’ equal ’ )
axs [ 0 ] . s e t _ y l a b e l ( " Imaginary p a r t o f z " )
axs [ 0 ] . s e t _ x l a b e l ( " Real p a r t o f z " )
axs [ 0 ] . s e t _ x l i m ( [ − 1 . 5 , 1 . 5 ] )
axs [ 0 ] . s e t _ y l i m ( [ − 1 . 5 , 1 . 5 ] )
axs [ 0 ] . s e t _ t i t l e ( " $|\\mathcal {H} ( z ) |^2$ ( dB ) " )

# P l o t phase angle o f system f u n c t i o n .


c = axs [ 1 ] . pcolormesh ( xx , yy , np . angle ( h ( x+1 j * y ) ) , cmap= " j e t " )
f i g . c o l o r b a r ( c , ax=axs [ 1 ] )
axs [ 1 ] . s e t _ a s p e c t ( ’ equal ’ )
axs [ 1 ] . s e t _ y l a b e l ( " Imaginary p a r t o f z " )
axs [ 1 ] . s e t _ x l a b e l ( " Real p a r t o f z " )
axs [ 1 ] . s e t _ x l i m ( [ − 1 . 5 , 1 . 5 ] )
axs [ 1 ] . s e t _ y l i m ( [ − 1 . 5 , 1 . 5 ] )

axs [ 1 ] . s e t _ t i t l e ( " $\\angle\\mathcal {H} ( z ) $ " )

om = np . l i n s p a c e ( 0 , 2 . 0 * np . pi , num=1000)
axs [ 1 ] . p l o t ( np . cos (om) , np . s i n (om) , c o l o r = " white " )
plt . tight_layout ()
p l t . s a v e f i g ( " z_mag_angle . png " )
p l t . show ( )

Listing 60: 025_system_function/third_order.py


326

Frequency response H (ω̂ )


In order to obtain the frequency response, evaluate H(z) on the unit
circle z = eiω̂ for ω̂ ∈ [−π, π ]:
6
H (ω̂ ) = (1 − e−iω̂ )(1 − ei( 3 −ω̂ ) )(1 − e−i( 3 +ω̂ ) )
π π
(887) |H(ω̂ )|

The magnitude response |H(ω̂ )| is shown in Figure 213. This is a 4

high-pass filter. Compare |H(ω̂ )| with the values of H(z) on the unit
2
circle, shown in on the top panel of Figure 212.
The zeros of the system function, that lie on the unit circle z = eiω̂ , ω̂
correspond to the frequencies at which the gain of the system is zero. −π − π3 π
3
π
Thus, complex sinusoids at these frequencies are blocked.
Figure 213: The magnitude response
|H(ω̂ )| for the third order FIR filter.
Blocking filters

The factorization of a filter can be used to design a filter that blocks


certain frequencies. A filter of the form:
N
H(z) = z−n0 ∏ (1 − α k z −1 ) (888)
k =1

blocks all signals of form αnk . By selecting values of αk = eiω̂k , we can


make a filter that blocks complex sinusoidal signals x [n] = eiω̂k n with
frequencies ω̂k , as we saw in the previous example. The term z−n0 is
an arbitrary time shift for this filter, which doesn’t affect the locations
of the zeros of the system function.

Example: blocking filter for a real-valued sinusoidal signal


Let’s design a filter that blocks a 50 Hz power line signal. We’ll
assume that this signal is sinusoidal:

1 iω̂0 n
x [n] = cos(ω̂0 n) = (e + e−iω̂0 n ) (889)
2
If our sampling rate is f s = 103 Hz, a 50 Hz frequency would corre-
spond to a normalized angular frequency of ω̂0 = 2π50/ f s = 0.1π
radians per sample.
Because our signal has two spectral components, we need to
filter out normalized angular frequencies ω̂ = ±0.1π. This would
correspond to zeros of the system function at α1 = ei0.1π and α2 =
e−i0.1π . We now have a filter description:

H(z) = (1 − e−i0.1π z−1 )(1 − ei0.1π z−1 ) (890)


= 1 − (e−i0.1π + ei0.1π )z−1 + (e−i0.1π ei0.1π )z−2 (891)
−1 −2
= 1 − 2 cos(0.1π )z +z (892)
327

This filter has an impulse response:

h[n] = δ[n] − 2 cos(0.1π )δ[n − 1] + δ[n − 2] (893)

and system function description:

y[n] = x [n] − 2 cos(0.1π ) x [n − 1] + x [n − 2]. (894)

This is a simple filter that will completely remove a pure 50 Hz


sinusoidal signal. You can expand on this to, e.g., build a filter that
blocks harmonics of 50 Hz.

Running average filter

In the frequency response chapter, we analyzed the frequency re-


sponse of the running average filter. Now we will analyze the z-
transform of the same filter.
This running average filter with M = 2N + 1 is defined using the
following system definition:
N
1
y[n] =
M ∑ x [ n − k ]. (895)
k =− N

This system has the following impulse response:


N
1
h[n] =
M ∑ δ [ n − k ]. (896)
k =− N

The z-transform of this filter is:



H(z) = ∑ h [ k ] z−k , (897)
k =−∞
N
1
=
M ∑ z−k . (898)
k =− N

This is a geometric series, which we can solve by our usual method.


We obtain:
1 z N − z−( N +1)
H(z) = . (899)
M 1 − z −1
Multiplying the right-hand side with z N +1 /z N +1 gives:

1 z2N +1 − 1 Figure 214: Above: The running average


H(z) = (900)
M z N ( z − 1) system function magnitude |H(z)|
plotted on the complex plane. The
1 zM − 1 M − 1 zeros of the system function
= (901)
M z N ( z − 1) 2π
are evenly distributed αk = ei M k with
k ∈ 1, 2, · · · , M − 1. These are the
The system function is zero when z M − 1 = 0, or z M = 1. Since 1 = frequencies that are perfectly removed
ei2πk for k ∈ Z, it follows that the roots of the Mth order polynomial by the filter. The unit circle eiω̂ is
2π denoted with a white line. Below: The
are αk = ei M k . This has M unique values when k ∈ [0, 1, 2, · · · , M − 1]. phase angle of the system function
corresponding to a running average
filter.
328

We can now express the system function in factorized form:

M −1
1 1 2π
H(z) =
M ( z − 1) z N ∏ ( z − ei M k ) (902)
k =0
M −1
1 1 2π
=
M( z − 1) z
  N
(
 z −
 1

 ) ∏ ( z − ei M k ) (903)
k =1
M −1
1 1 2π
=
M zN ∏ ( z − ei M k ) (904)
k =1

Notice that one of the zeros (for k = 0) of the denominator polyno-


mial coincides with a zero of the numerator. This means that this root
of the system function polynomial cancels out.
Finally, we multiply with z−( M−1) /z−( M−1) to obtain:

1 N M −1 2π
H(z) = z ∏ (1 − e i M k z −1 ). (905)
M k =1

We can see that this the same form as Equation 888. This means that
the running average filter is actually a set of blocking filters which

blocks complex sinusoidal signals x [n] = ei M kn with normalized
angular frequencies ω̂k = 2π M k with k = [1, 2, · · · , M − 1]. The term z
N

means that there is a time-shift (negative delay) by N samples.


The magnitude of the running average system function |H(z)| is
shown in Figure 214 for N = 5 (M = 2N + 1 = 11). This system

function has 10 nulls at complex-valued positions αk = ei 11 k with
k ∈ 1, 2, · · · , 10. They are all along the unit circle (shown with a
white circle). Only the region near z = 1 doesn’t have a null. This
corresponds to the pass-band region of this filter. Figure 214 also
shows the phase angle of the same system function.
If we evaluate the value of the system function on the unit circle
z = eiω̂ , we obtain the magnitude and phase response of the system.
The magnitude and phase response shown in Figure 215 looks ex-
actly like the one we obtained earlier using a discrete-time Fourier Figure 215: The magnitude and phase
response of the running average filter.
transform for the same running average filter.
Within the main lobe of the low-pass
filter pass band, the filter introduces no
phase shift. The phase then alternates
between 0 and π within the sidelobes.
329

Exercises: Z-transform

1. Find the z-transforms for the following signals:

a) h[n] = δ[n]
b) h[n] = 2δ[n + 4]
c) h[n] = −41δ[n − 42]
d) h[n] = δ[n + 1] − 2δ[n − 1] + 4δ[n − 4]

2. The system function H(z) of a discrete-time LTI system is defined


as:
H(z) = z1 + 1 + 3z−1 − 0.5z−2 + 4z−10 (906)

a) What is the difference equation of the form:

y[n] = ∑ αk x [n − nk ] (907)
k

that describes the relationship between the input signal x [n]


and the output signal y[n] for the discrete-time LTI system, x [n] x [n]
uniquely determined by Equation 906?
b) Sketch a plot of the impulse response h[n] = T {δ[n]} of this H1 ( z ) H(z)

LTI system.
H1 ( z ) y[n]
c) Modify H(z) in such a way that a delay by 2 samples (y2 [n] =
y[n − 2]) will be applied to the signal y[n] defined in a).
H1 ( z )

3. The system function H(z) of a discrete-time LTI system is defined


y[n]
as:
H(z) = (z − eiπ/4 )(z − e−iπ )z−2 (908)
Figure 216: A cascade of three elemen-
tary LTI systems defined by system
You can view this system as a cascade of three elementary sys-
functions H1 (z), H2 (z) and H3 (z)
tems: is equivalent to a single LTI system
H(z) = H1 (z)H2 (z)H3 (z) (909) H(z) = H1 (z)H2 (z)H3 (z).

Two of these systems will null out a single frequency and one of
these systems will apply time-shift to the signal.

a) Define H1 (z), H2 (z), and H3 (z) and describe what each of


these elementary LTI systems does to an input signal. It will be
one of two options: 1) null out the frequency component with
frequency ω̂0 , or 2) time-shift the signal by n0 samples.
b) Sketch the locations of the poles and zeroes of the system
function on the complex plane. Include the unit circle on the
plot.
c) Sketch a plot of the magnitude response |H(ω̂ )| of this system.
Mark the nulls on the plot.
330

d) List the frequencies of the frequency components of the input


signal that will be completely removed by this system (nulled
out). Use radians per sample as the units for frequency.
e) Write the system function on the form:

H(z) = ∑ β k z−k (910)


k

What are the non-zero values of β k ?


f) What is the impulse response h[n] of the LTI system?
g) If a real-valued signal will be fed into the system, will the
output always be real-valued? Justify your answer.

4. You have a real-valued discrete-time signal that is sampled with


f s = 44.1 · 103 samples per second. Come up with a simple
LTI system that will remove a 1500 and 5000 hertz frequency
component of the input signal. This means that the magnitude
response of the LTI needs to have a null at frequencies -1500, 1500,
-5000 and 5000 hertz. Use a suitably selected system function
(z-transform of the impulse response) to derive this filter 61 . 61
Remember that you can relate fre-
quency in units of radians per sample
a) What is the system function that you came up with? to radians per second using ω̂ = ωTs ,
where Ts = 1/ f s . Also remember that
b) Draw a pole-zero diagram of your system function. you can relate frequency in units of ra-
dians per second to frequency in cycles
c) Write a computer program to verify that the magnitude re-
per second (hertz) using ω = 2π f
sponse has nulls on the correct frequencies. Hint: you can use
the code in Listing 61 to help you out. An example output of
this program is shown in Figure 217.
d) What is the impulse response of h[n] of your LTI system?

import m a t p l o t l i b . pyplot as p l t
import numpy as np

# Frequency , i n u n i t s o f r a d i a n s per sample .


omhat = np . l i n s p a c e ( −np . pi , np . pi , num=100000)

def h ( z ) :
# System f u n c t i o n . Figure 217: Magnitude response of a
om0 = 2 . 0 * np . p i * 5 0 0 0 . 0 / ( 4 4 . 1 e3 ) filter that notches out two frequencies.
om1 = 2 . 0 * np . p i * 1 5 0 0 . 0 / ( 4 4 . 1 e3 )
r e t u r n ( z * * ( − 4 ) * ( z * * 4 − 2 * z * * 3 * ( np . cos ( om1 ) +np . cos ( om0 ) )
+ 2 * z * * 2 * ( 1 + 2 * np . cos ( om1 ) * np . cos ( om0 ) )
− 2 * z * ( np . cos ( om0 ) +np . cos ( om1 ) ) + 1 ) )

# Magnitude response .
magresp = np . abs ( h ( np . exp ( 1 j * omhat ) ) )
# P l o t t h e magnitude response i n dB s c a l e .
p l t . p l o t ( omhat , 1 0 . 0 * np . l o g 1 0 ( magresp * * 2 . 0 ) )
p l t . x l a b e l ( r " Frequency $\hat {\omega } $ ( rad/sample ) " )
p l t . y l a b e l ( r " Magnitude response $10\l o g _ { 1 0 } ( | \ mathcal {H} ( \ hat {\
omega } ) |^2) $ dB " )
331

p l t . show ( )

Listing 61: 030_z_fir_magresp/plot_mag_resp.py


332

Suggested solutions: Z-transform

1. Given a discrete time signal x [n], then the z-transform is defined


as:

X (z) = ∑ x [ k ] z−k .
k =−∞

a) Using the definition, we have:



X (z) = Z {δ[n]} = ∑ δ[k ]z−k = δ[0]z−0 = 1,
k =−∞

since the impulse function is only non-zero for n = 0, for which


the value is 1.
b) The z-transform is linear and for a time-shift, we then have:

X (z) = Z {2δ[n + 4]} = 2Z {δ[n + 4]},



=2 ∑ δ [ k + 4] z − k ,
k =−∞
4
= 2z Z {δ[n]},
= 2z4 ,

since by the previous exercise Z {δ[n]} = 1.


c) The z-transform of h[n] = −41δ[n − 42] is done in the same way
as the previous problem. Skipping the details, we obtain:

X (z) = Z {−41δ[n − 42]} = −41z−42 ,

again by linearity and the time-shift theorem.


d) For h[n] = δ[n + 1] − 2δ[n − 1] + 4δ[n − 4] we obtain again by
linearity and the time-shift property:

X (z) = Z {h[n]} = z − 2z−1 + 4z−4 .

2. Let the system function for an LTI system be H(z) = z1 + 1 +


3z−1 − 0.5z−2 + 4z−10 .

a) If the system function is defined as above, then the impulse


response is found by taking the inverse z-transform of H(z).
We get:

h[n] = δ[n + 1] + δ[n] + 3δ[n − 1] − 0.5δ[n − 2] + 4δ[n − 10],

from this we conclude that the difference equation for the


system is:

y[n] = x [n + 1] + x [n] + 3x [n − 1] − 0.5x [n − 2] + 4x [n − 10],


333

since every LTI system can be expressed as a convolution of the


form:

y[n] = h[n] ∗ x [n] = ∑ h [ k ] x [ n − k ],
k =−∞

with h[n] = T {δ[n]}.


b) In Listing 62 is a simple program to plot the impulse response
for this system.
import m a t p l o t l i b . pyplot as p l t
import numpy as np

# Sample v a l u e s we want t o p l o t on .
nn = np . a r r a y ( [ − 2 , −1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 1 0 ] )

# Values f o r t h e impulse response f u n c t i o n .


h = np . a r r a y ( [ 0 , 1 , 1 , 3 , − 0 . 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 4 ] )

# P l o t as a stem p l o t t o emphasize t h e
# d i s c r e t e nature of the function .
p l t . stem ( nn , h )
p l t . x l a b e l ( " samples [ n ] " )
p l t . y l a b e l ( " $h [ n ] $ " )
p l t . t i t l e ( " Impulse response " )
# C a l l t h i s i f needed :
# p l t . show ( )

Listing 62: Code to plot the impulse response function

The impulse response plot is shown in Figure 218.


c) If we define a new signal y2 [n] = y[n − 2], then by the time-shift
theorem we have that the new system function will be:

Y2 (z) = z−2 Y (z) = z−2 H(z) X (z)

which gives: Figure 218: Output of Listing 62

H2 (z) = z−2 H(z) = z−1 + z−2 + 3z−3 − 0.5z−4 + 4z−12 ,

this new system function will describe a system that is delayed


by 2 samples compared to H(z).

3. Let the system function of a discrete-time LTI system be defined


as:
H(z) = (z − eiπ/4 )(z − e−iπ )z−2 .

a) One can view the system function as a cascade of three systems


of the form:

H1 (z) = z − eiπ/4 ,
H2 (z) = z − e−iπ ,
H3 ( z ) = z −2 ,
334

where the effect is to null-out a frequency for H1 and H2 , the


frequencies being π/4 and π, respectively. The last elementary
system H3 will delay by 2 samples. To determine the overall
effect of this system, factor it:

H(z) = z(1 − eiπ z−1 )z(1 − e−iπ z−1 )z−2 = (1 − eiπ/4 z−1 )(1 − e−iπ z−1 ).

Then we can view the system function as a cascade of two


elementary systems of the form: Im(z)

H1 (z) = 1 − eiπ/4 z−1 ,


H2 (z) = 1 − e−iπ z−1 .

This filter blocks sinusoidal signals where α1 = eiπ/4 and Re(z)


α2 = e−iπ , which correspond to normalized angular frequencies
of ω̂ = π/4 and ω̂ = π.
b) The poles and zeros of the system function are shown in Figure
219.
c) The following code snippet will plot the magnitude response
corresponding to H(ω̂ ) = H(z)|z=eiω̂ :
Figure 219: The zeros of the system
import m a t p l o t l i b . pyplot as p l t function H(z), have αk = {eiπ/4 , e−iπ }.
import numpy as np Zeros are marked with blue circles and
poles are marked with red crosses.
# P a r t i t i o n t h e i n t e r v a l ( − pi , p i ) .
om = np . l i n s p a c e ( −np . pi , np . pi , num=100)

def s y s t e m _ f u n c t i o n ( z ) :
r e t u r n ( 1 − np . exp ( 1 j * np . p i /4) * z * * ( − 1 ) ) * ( 1 − np . exp ( −1 j
* np . p i ) * z * * ( − 1 ) )

def f r e q u e n c y _ r e s p o n s e (om) :
r e t u r n s y s t e m _ f u n c t i o n ( np . exp ( 1 j *om) )

# Marking t h e z e r o s on t h e graph .
x_markers = [ np . p i /4 , −np . p i ]
y_markers = [ np . abs ( f r e q u e n c y _ r e s p o n s e ( np . p i /4) ) , np . abs (
f r e q u e n c y _ r e s p o n s e ( −np . p i ) ) ]

# P l o t t h e system f u n c t i o n with t h e z e r o s marked .


p l t . p l o t (om, np . abs ( f r e q u e n c y _ r e s p o n s e (om) ) )
p l t . p l o t ( x_markers , y_markers , " rx " , l a b e l = " z e r o s " )
p l t . x l a b e l ( r " $\hat {\omega } $ ( rad / sample ) " )
p l t . y l a b e l ( r " $|\mathcal {H} ( \ hat {\omega } ) |$ " )
p l t . t i t l e ( " Magnitude response p l o t " )
p l t . legend ( )
# C a l l t h i s i f needed .
# p l t . show ( )

Listing 63: Script to plot the magnitude response

Output from Listing 63 is shown in Figure 220.

Figure 220: Plot of |H(ω̂ )|, which has


zeros at ω̂ = π/4 and ω̂ = −π.
335

d) The zeros of the magnitude response will get completely


removed by the system. The original system has a system
function H(z) which has two zeros, these being α1 = eiπ/4 and
α2 = e−iπ . These correspond to ω̂ = π/4 and ω̂ = −π in units
of radians per sample. These discrete-time angular frequencies
will be completely removed by the system.
e) From the original definition of the system, we have:

H(z) = (z − eiπ/4 )(z − e−iπ )z−2 ,


= 1 − eiπ/4 z−1 − e−iπ z−1 + e−i3π/4 z−2 ,
= 1 − (eiπ/4 + e−iπ )z−1 + (e−i3π/4 )z−2
The non-zero values for β k are β 1 = α1 + α2 and β 2 = α1 α2 .
f) To find the impulse response, we find the inverse z-transform
of the system function. The impulse response h[n] is then:

h[n] = δ[n] − (eiπ/4 + e−iπ )δ[n − 1] + e−i3π/4 δ[n − 2].

g) For any discrete-time LTI system, we have:

y [ n ] = h [ n ] ∗ x [ n ].

If the impulse response is real-valued, then the output signal


will also be real-valued. If h[n] is real-valued, it must satisfy:
h[n]∗ = h[n]. We can easily see that the impulse response
doesn’t satisfy this property, therefore a real-valued signal is
not necessarily real-valued after applying this system. This
can also be seen from the pole-zero diagram. The positive and
negative frequencies are not treated the same, as the frequency
ω̂ = π/4 gets filtered out, while ω̂ = −π/4 passed through.

4. Let f s = 44.1 · 103 Hz.

a) To filter out the frequencies, we require that the system func-


tion has four zeros. These being at frequencies corresponding
to ±5000 and ±1500 Hz. Thus, the system function will be a
polynomial of degree four with four distinct roots. That is, the
system function looks like:

H(z) = (z − α1 )(z − α2 )(z − α3 )(z − α4 )


The frequencies in hertz corresponds to discrete-time angular
frequencies of ω̂k = ωTs = 2π f k / f s. This gives:

ω̂0 = 2π5000/44100 = 100π/441,


ω̂1 = −2π5000/44100 = −100π/441,
ω̂2 = 2π1500/44100 = 10π/147,
ω̂3 = −2π1500/44100 = −10π/147.
336

Finally, the system function is therefore of the form:


Im(z)
H(z) = (z − e10iπ/441 )(z − e−10iπ/441 )(z − e10iπ/147 )(z − e−10iπ/147 ),
10π 10π 10π 10π
= (1 − e 441 i z−1 )(1 − e− 441 i z−1 )(1 − e 147 i z−1 )(1 − e− 147 i z−1 )z4 ,
   
10π −1 −2 10π
= (1 − 2 cos z + z )(1 − 2 cos z −1 + z −2 ) z 4 .
441 147
Re(z)

b) A pole-zero diagram for the system function H(z) is shown in


Figure 221.
c) Listing 64 provides code to plot the magnitude response for
this system.
import m a t p l o t l i b . pyplot as p l t
import numpy as np
Figure 221: The zeros of the
system function H(z), have αk =
# Function t o c o n v e r t t o dB .
{e10iπ/441 , e−10iπ/441 , e20iπ/441 , e−20iπ/441 }.
Zeros are marked with blue circles and
def c o n v e r t _ t o _ d e c i b e l ( x ) :
poles are marked with red crosses.
r e t u r n 1 0 * np . l o g 1 0 ( np . abs ( x ) * * 2 )

# P a r t i t i o n t h e i n t e r v a l ( − pi , p i ) .
om = np . l i n s p a c e ( −np . pi , np . pi , num=10000)

def s y s t e m _ f u n c t i o n ( z ) :
r e t u r n ( 1 − np . exp ( 1 j * 1 0 0 * np . p i /441) * z * * ( − 1 ) ) * ( 1 − np .
exp ( −1 j * 1 0 0 * np . p i /441) * z * * ( − 1 ) ) * ( 1 − np . exp ( 2 0 * 1 j * np . p i
/441) * z * * ( − 1 ) ) * ( 1 − np . exp ( −20 * 1 j * np . p i /441) * z * * ( − 1 ) ) * z
**4

def f r e q u e n c y _ r e s p o n s e (om) :
r e t u r n s y s t e m _ f u n c t i o n ( np . exp ( 1 j *om) )

# P l o t t h e system f u n c t i o n with t h e z e r o s marked .


p l t . p l o t (om, c o n v e r t _ t o _ d e c i b e l ( f r e q u e n c y _ r e s p o n s e (om) ) ,
l a b e l = r " $\mathcal {H} ( \ hat {\omega } ) $ " )
p l t . x l a b e l ( r " $\hat {\omega } $ ( rad / sample ) " )
p l t . y l a b e l ( r " $|\mathcal {H} ( \ hat {\omega } ) |$ " )
p l t . t i t l e ( " Magnitude response p l o t " )
p l t . legend ( )
# C a l l t h i s i f needed :
# p l t . show ( )

Listing 64: Code to plot the magnitude response

Output of Listing 64 is shown in Figure 222.


d) Having found the system function, one can easily determine
the impulse response function by using the inverse z-transform.
Have that:
   
10π −1 −2 10π
H(z) = (1 − 2 cos z + z )(1 − 2 cos z−1 + z−2 )z4 . Figure 222: Plot of the magnitude
441 147
response in dB
337

Expanding all of this out, one obtains:

H(z) = z4 − 2(cos(θ1 ) + cos(θ2 ))z3 + 2(2 cos θ1 cos θ2 + 1)z2 − 2(cos θ1 + cos θ2 )z + 1,

where θ1 = 10π
441 and θ2 =
10π
147 . The impulse response is then the
inverse z-transform:

h[n] = δ[n] − 2(cos θ1 + cos θ2 )δ[n + 1] + 2(2 cos θ1 cos θ2 + 1)δ[n + 2] − 2(cos θ1 + cos θ2 )δ[n + 3] + δ[n + 4].
Infinite Impulse Response Filters

In this chapter, we will introduce infinite impulse response (IIR)


filters. Just like finite impulse response filters (FIR), they are also
linear time-invariant systems. The main difference is that infinite
impulse response filters include feedback from the samples of the
output signal y[n] of the system, and as the name already implies,
this feedback makes the impulse response of the system infinitely
long.
The advantage of IIR filters compared to FIR filters is that it is
often possible to design a lower order filter, which meets design
requirements for a frequency response. The lower the order of the
filter, the fewer computations per output sample are needed. This is
important in some digital signal processing applications, especially
ones where the sampling rate of the signal is very large and there are
strict requirements for real-time processing speeds.
A drawback of IIR filters is that the feedback terms can result in
an unstable filter output. These types of filters are also often not as
accurate numerically as FIR filters.

Infinite impulse response filter

Consider the following system:

N M
y[n] = ∑ aℓ y[n − ℓ] + ∑ bk x[n − k] (911)
ℓ=1 k =0
| {z } | {z }
feedback terms feed forward terms

The filter output depends on the current and past values of the input
signal, as well as past values of the system output y[n]. This is a causal
system62 , because the system output does not depend on future 62
a system is causal if it only depends
on present and past values.
values of the input or output signal.
The system defined in Equation 911 is called an infinite impulse
response filter. A total of N + M + 1 filter coefficients aℓ and bk are
needed to specify this filter.
340

System function

The system function, i.e., the z-transform of the impulse response for
an IIR filter, is:

∑kM=0 bk z−k
H(z) = N
(912)
1 − ∑ℓ= −ℓ
1 aℓ z

Proof. Apply the z-transform to the definition of the IIR, and use
linearity and time-shift properties:

N M
y[n] = ∑ aℓ y[n − ℓ] + ∑ bk x[n − k] Z {·} (913)
ℓ=1 k =0 Figure 223: A block diagram represen-
N M tation of an infinite impulse response
Y (z) = ∑ aℓ z−ℓ Y (z) + ∑ bk z−k X (z) (914) filter system function.
ℓ=1 k =0
! !
N M
1− ∑ aℓ z −ℓ
Y (z) = ∑ −k
bk z X (z) (915)
ℓ=1 k =0

∑kM=0 bk z−k
Y (z) = N
X (z) (916)
1 − ∑ℓ= −ℓ
1 aℓ z
Y (z) = H(z) X (z). (917)

The system function of an IIR filter can be represented as a block


diagram, as shown in Figure 223. In this diagram, a delay by one
sample is indicated with z−1 , as this corresponds to the z-transform
of a time delay by one sample. The triangle in this diagram indicates
multiplying the signal with a constant coefficient.

Impulse response

An IIR filter is a linear time-invariant system. This means that the δ[n] h[n]
LTI
impulse response h[n] completely characterizes this system.
The impulse response of an LTI system is obtained by feeding a Figure 224: The impulse response h[n]
of an LTI system is obtained by feeding
unit impulse δ[n] into the system. Using the system definition for an
a unit impulse into the system.
IIR filter, the impulse response is defined as:

N M
h[n] = ∑ aℓ h[n − ℓ] + ∑ bk δ [ n − k ] . (918)
ℓ=1 k =0

The impulse response h[n] must satisfy this difference equation for all
values of n.
341

Elementary IIR system

An important type of IIR system is one of the following form:

y[n] = a1 y[n − 1] + b0 x [n] (919)

In most cases, a more complicated IIR filter can be expressed as a


superposition of such elementary filters. Note that Equation 919
is the same as the general system described in Equation 911 using
N = 1 and M = 0. This system is described using two coefficients a1
and b0 .
The system defined in Equation 919 has the following impulse
response and system function: 1.5
b0 a1n u[n]

Z b0
h[n] = b0 a1n u[n] ←
→ H(z) = . (920) 1
1 − a 1 z −1
0.5 ...
The impulse response h[n] is shown in Figure 225.
The derivation is as follows. We start with the impulse response in n
the form obtainable from Equation 919:

h[n] = a1 h[n − 1] + b0 δ[n]. (921) Figure 225: The impulse response of an


elementary first order IIR system. In
this example, b0 = 1 and a1 = 0.5.
In order to solve this impulse response, we need to know the initial
value of the impulse response at h[−∞]. We need to specify this
boundary condition somehow. We’ll assume that the impulse response
is zero valued initially at h[−∞] = 0.
It is now possible to calculate the values of the impulse response.
The unit impulse arrives at n = 0, at which time we get the first
non-zero value of the impulse response h[n]. It is then possible to
evaluate the output recursively using the previous output and find
that a pattern emerges:

h[−∞] = 0 (922)
..
.=0 (923)
h[−1] = 0 (924)
h[0] = a1 h[−1] + b0 = b0 (925)
h [1] = a1 h [0] = b0 a1 (926)
h [2] = a1 h [1] = b0 a21 (927)
h [3] = a1 h [2] = b0 a31 (928)
h [4] = a1 h [3] = b0 a41 (929)
.. .. .
.=. = .. (930)
h [ n ] = a1 h [ n − 1] = b0 a1n (931)
342

The impulse response is therefore:

h[n] = b0 a1n u[n] , (932)

where u[n] is the unit step function63 63

While Equation 912 already allows us to determine the system



1 n≥0
u[n] = (933)
function for this elementary system, it is also possible to directly z- 0 n < 0.

transform the impulse response and obtain the same result. Applying
the z-transform on the impulse response gives us:
∞ ∞
H(z) = ∑ h [ n ] z−n = ∑ b0 a1n u[n]z−n (934)
n=−∞ n=−∞

= b0 ∑ ( a 1 z −1 ) n . (935)
n =0

Using the formula for geometric series, assuming that | a1 z−1 | < 1, we
obtain64 : 64
Geometric series closed form solution
b0 formula:
H(z) = . (937)
1 − a 1 z −1 L
1 − α L +1
∑ αk = 1−α
, (936)
This elementary z-transform pair will be used later in analysis of k =0

more complicated impulse responses, when applying partial fraction


expansion to decompose an Nth order IIR system into a superposi- 2
h[n]
tion of 1st order systems like the one we just derived. One can think
of this as an elementary building block for IIR system functions. 1

n
Example: IIR system function
Consider an impulse response of the following form: −1

h[n] = (−0.9)n u[n] − 0.5(−0.9)n−1 u[n − 1] (938) −2

Figure 226: The impulse response


What is the system function H(z)? What is the system description shown in Equation 938.
for this system? What is the frequency response of this system? Let’s
find out.
We can use linearity and the two z-transform pairs that we have
learned about. First, a time delay is multiplication by z−n0 :
Z
→ Y ( z ) = z − n0 X ( z ),
y [ n ] = x [ n − n0 ] ← (939)

and
Z b0
x [n] = b0 a1n u[n] ←
→ X (z) = . (940)
1 − a 1 z −1
Let’s apply this in practice:

1 0.5
H(z) = −
− z −1 , (941)
1 + 0.9z 1 1 + 0.9z−1
1 − 0.5z−1
= . (942)
1 + 0.9z−1
343

By comparing the system function in Equation 942 with Equations


911 and 912, we can see that the system is described using the follow-
ing difference equation:

y[n] = −0.9y[n − 1] + x [n] − 0.5x [n − 1] (943)

The impulse response is a polynomial fraction. We can multiply


this by z/z to obtain:

z − 0.5 P(z)
H(z) = = . (944)
z + 0.9 Q(z)

The numerator polynomial P(z) has a zero at z = 0.5, and the


denominator polynomial Q(z) has a zero at z = −0.9. This means
that H(0.5) = 0 and H(−0.9) → ∞. The zeros of P(z) and Q(z) have
the opposite effect on the signal.
Zeros of the denominator polynomial Q(z) are called poles of the
system function. They indicate signals T {zn } → ∞ for which the
output of the system grows exponentially.
Figure 227 shows the magnitude of the system function |H(z)| on
the complex plane. The pole at z = −0.9 is indicated with a white
cross, and the zero at z = 0.5 is indicated with a white circle. We can
see the implication of the zeros and the poles of the system function. Figure 227: The magnitude of the
system function given in Equation 944.
The system function has a large magnitude near the poles and low The white cross indicates the pole at
values in the region near the zeros. z = −0.9 and the white circle indicates
A plot like the one shown in Figure 227 is known as a pole-zero the zero at z = 0.5. The black circle
indicates the unit circle.
diagram. Typically, such a diagram only depicts the locations of the
poles and zeros with crosses and circles. These diagrams are useful
for analyzing the properties of discrete-time LTI systems.
We can obtain the frequency response of the system by evaluating
H(z) on the unit circle:

eiω̂ − 0.5
H(ω̂ ) = (945)
eiω̂ + 0.9
This is shown in Figure 228. This filter is a high-pass filter.

Stability for an arbitrary IIR system

Because of the feedback terms in IIR systems, these systems are not
necessarily stable. Instability means that the output of the system
for some inputs can become indeterminate. This is one of the main
drawbacks of IIR systems.
Bounded Input Bounded Output (BIBO) stability is used to deter-
mine the stability of an IIR system. BIBO stability implies that for a
bounded input:
| x [n]| ≤ Mx < ∞. (946)

Figure 228: The magnitude and fre-


quency response of the system given in
Equation 944. This filter is a high-pass
filter.
344

The output is also bounded:

|y[n]| ≤ My < ∞. (947)

The output of a filter is a convolution sum of the input signal x [n]


convolved with the impulse response h[n] of the system. In the case
of an LTI system, the magnitude of the output is:


|y[n]| = ∑ h[k] x [n − k] (948)
k =−∞

Using the triangle inequality (c = a + b ⇒ |c| ≤ | a| + |b|), we can


obtain the following bounds:

∞ ∞ ∞
∑ h[k] x [n − k] ≤ ∑ |h[k]| | x [n − k]| ≤ Mx ∑ |h[k]|. (949)
k =−∞ k =−∞ k =−∞
| {z }
≤ Mx

Thus, a sufficient condition for bounded output |y[n]| ≤ My < ∞


given a bounded input | x [n]| ≤ Mx < ∞ is that the impulse response
h[n] is bounded to some finite value Mh :


∑ |h[k]| ≤ Mh < ∞. (950)
k =−∞

Example: Stability of a first order IIR system


A first order system has the following system function:

b
H(z) = (951)
1 − az−1
When is this system stable?
The impulse response of this system is:

h[n] = ban u[n]. (952)

Using the BIBO stability criterion, the output of this system is stable
if:

∑ |h[k]| ≤ Mh < ∞. (953)
k =−∞

Using Equation 952, we obtain:


N N
∑ |h[k]| = ∑ |bak u[k]| (954)
k =− N k =− N
N
= |b| ∑ | a|k (955)
k =0
345

If | a| ≥ 1, then
∞ N
∑ |h[k]| = lim |b|
N →∞
∑ | a|k → ∞ (956)
k=−∞ k =0

The system is BIBO unstable if | a| ≥ 1.


We can use the geometric series solution formula for the case
| a| < 1:
N
1 − | a | N +1
|b| ∑ | a|k = |b| 1 − | a|
. (957)
k =0

A limit of the infinite sum is thus a limit of the finite sum:



1 − | a | N +1
∑ |h[k]| = lim |b|
N →∞ 1 − | a|
. (958)
k =−∞

When | a| < 1, we get:



|b|
∑ |h[k]| =
1 − | a|
< ∞. (959)
k =−∞

The system is BIBO stable when | a| < 1.

Poles and zeros

An arbitrary order IIR system has a system function, which is a


polynomial fraction:

∑kM=0 bk z−k P(z)


H(z) = N
= . (960)
1 − ∑ℓ=1 aℓ z−ℓ Q(z) Figure 229: A band-stop filter.

The fundamental theorem of algebra says that because the numerator


polynomial P(z) is an Mth order polynomial, it has M values of
z = αk for which P(αk ) = 0.
Similarly, the denominator polynomial Q(z) is an Nth order
polynomial, which has N roots Q( β ℓ ) = 0.
We will assume that all the zeros of the numerator and denomina-
tor are distinct: αk ̸= αℓ , αk ̸= β ℓ , and β k ̸= β ℓ for all k ̸= ℓ. We’ll also
assume that M > 0, that there is at least one zero of the numerator
polynomial.
One can then express the system function of an IIR filter in the
following form:

∏kM=1 (z − αk ) P(z)
H(z) = N
= (961)
∏ℓ=1 (z − β ℓ ) Q(z)

The terms β ℓ are the poles of the system function. They indicate
regions of the complex plane where the system function magnitude
Figure 230: A low-pass filter.
approaches infinity H( β ℓ ) → ∞.
346

The terms αk are the zeros of the system function. They indicate
regions of the complex plane where the system function magnitude
becomes zero valued H(αk ) = 0.

Example: Pole-zero diagrams

By knowing the poles and zeros of the system function, it is possible


to analytically say quite a lot about the behavior of the filter. Figures
229, 230, 231 and 232 show some examples of pole-zero diagrams and
the magnitude responses associated with these filters. It is possible to
tell from the positions of the poles and zeros what type of magnitude
response the filter has.

Partial fraction decomposition


Figure 231: A band-pass filter that
Using partial fraction decomposition, we can refactor the system func- passes through positive frequencies.
The resulting signal will be complex-
tion of an IIR filter in such a way that we can easily calculate an
valued, even if a real-valued signal is
inverse z-transform of the system function using the elementary first fed into the system.
order IIR filter z-transform pair that we covered earlier. I won’t pro-
vide the general formulation here. Instead, I’ll demonstrate this with
an example, which can be generalized for higher order IIR systems.

Example: Partial fraction decomposition N = 2 and M = 2

Consider the case where the system function of an IIR filter is defined
as:
b0 + b1 z−1 + b2 z−2
H(z) = . (962)
(1 − β 1 z−1 )(1 − β 2 z−1 )

What is the impulse response of this system? Is this filter BIBO


stable?
Note that the denominator of Equation 962 is already expressed
in a form where β 1 and β 2 correspond to the poles of the system
function.
The order of the numerator and denominator is N = M = 2. In Figure 232: A band-pass filter
this case, the partial fraction decomposition results in the following
form:
A B
H(z) = + + C, (963)
1 − β 1 z −1 1 − β 2 z −1

where A, B, and C are coefficients that need to be determined.


347

We can determine what these coefficients are:


A(1 − β 2 z−1 ) + B(1 − β 1 z−1 ) + C (1 − β 1 z−1 )(1 − β 2 z−1 )
H(z) =
(1 − β 1 z−1 )(1 − β 2 z−1 )
(964)
( A + B + C ) + (− Aβ 2 − Bβ 1 − C ( β 1 + β 2 ))z−1 + (Cβ 1 β 2 )z−2
= .
(1 − β 1 z−1 )(1 − β 2 z−1 )
(965)

By comparing Equation 962 with Equation 965, we get a set of linear


equations, which can be used to solve for coefficients A, B, and C:


 A+B+C = b0
− Aβ 2 − Bβ 1 − C ( β 1 + β 2 ) = b1 (966)

 Cβ 1 β 2 = b2

The inverse z-transform can then be done using known z-transform


pairs:
Z
→ az−n0
aδ[n − n0 ] ← (967)
and
Z b
ban u[n] ←
→ . (968)
1 − az−1
The time-domain impulse response h[n] is therefore:

h[n] = Aβn1 u[n] + Bβn2 u[n] + Cδ[n]. (969)

In order to address the question of filter stability, we can now


apply our previous BIBO stability result for first order IIR filter
impulse response. The filter is stable as long as | β 1 | < 1 and | β 2 | <
1. In other words, the filter is stable if all the poles of the system
function are inside the unit circle. This will hold for any system
function that can be split using partial fraction decomposition.

IIR filtering with Scipy

It is possible to implement an IIR filter using Scipy. There is a func-


tion scipy.signal.lfilter, which implements an IIR filtering opera-
tion.
The function lfilter expects the filter coefficients to be specified
in the following format:

b0 + b1 z−1 + b2 z−2 + · · · + b N z− N
H(z) = (970)
1 + a 1 z −1 + a 2 z −2 + · · · + a M z − M
The example filter that is defined in Equation 942 is as follows:

1 − 0.5z−1
H(z) = (971)
1 + 0.9z−1
348

In Python, we would form two vectors: b=[1,-0.5] and a=[1,0.9]


and pass these to lfilter(b,a,signal) to IIR filter a vector signal.
The Python code in Listing 65 demonstrates implementing the IIR
filter that has the system function defined in Equation 971. It filters
a Gaussian random noise signal, which has a flat power spectrum.
How the filter modifies this spectrum allows us to inspect what
the filter does to the signal. The code also has a simple routine that
demonstrates how the same IIR filter could be naively implemented
without resorting to a library function.
import m a t p l o t l i b . pyplot as p l t
import numpy as np
import s c i p y . s i g n a l as s Figure 233: The power spectrum
estimate of the filtered white noise
signal using the simple high-pass filter
def simple_hpf ( s i g n a l ) : specified in Equation 971. Compare this
# Implement I I R f i l t e r : with the analytical magnitude response
# y [ n ] = − 0 . 9 * y [ n −1] + x [ n ] − 0 . 5 * x [ n −1] shown in Figure 228
out = np . z e r o s ( l e n ( s i g n a l ) )

f o r i i n range ( l e n ( s i g n a l ) ) :
if i > 1:
out [ i ] = s i g n a l [ i ] − 0 . 9 * out [ i − 1 ] − 0 . 5 * s i g n a l [ i −1]

r e t u r n out

# Implement t h i s system f u n c t i o n
# 1 − 0 . 5 z ^{ −1}
# H( z ) = −−−−−−−−−−−−−−
# 1 + 0 . 9 z ^{ −1}
b = [ 1 . 0 , −0.5]
a = [1 , 0.9]

# C r e a t e a white n o i s e s i g n a l , which has a f l a t power


# spectrum
s i g n a l = np . random . randn ( 1 0 0 0 0 0 )

# F i l t e r the noise s i g n a l .
i i r _ f i l t e r e d = s . l f i l t e r (b , a , signal )
# The same , but using a slower d i r e c t r o u t i n e .
i i r _ f i l t e r e d _ s l o w = simple_hpf ( s i g n a l )

sample_rate = 1000.0
# P l o t t h e power spectrum e s t i m a t e .
p l t . psd ( i i r _ f i l t e r e d , NFFT=1024 , Fs=s a m p l e _ r a t e )
p l t . s a v e f i g ( " ex_psd_hpf . png " )
p l t . show ( )

Listing 65: 026_iir/simple_hpf.py

IIR filter design with Scipy

Scipy includes an IIR filter design routines scipy.signal.iirfilter


and scipy.signal.iirdesign. These functions can be used to design
349

bandpass, lowpass, highpass, and bandstop filters that meet design


specifications.
Scipy functions scipy.signal.lfilter and scipy.signal.sosfilt
can be used to filter signals using an IIR filter.
The Python code in Listing 66 shows an example of designing an
IIR low-pass filter. The filter cutoff is set to be 100 Hz and the filter
band-stop is set to be 200 Hz. The minimum attenuation for signals
with frequencies higher than 200 Hz is set to be 100 dB.
I then generate a Gaussian random signal and filter this signal
using the low-pass filter that was designed. A Gaussian random
noise has a flat power spectrum, which allows us to investigate how
the filter shapes this spectrum.
Finally, I use the matplotlib.pyplot.psd function to estimate the
power spectrum of the filtered signal.

import m a t p l o t l i b . pyplot as p l t
import numpy as np Figure 234: The pole-zero diagram and
# You ’ l l f i n d t h i s on : magnitude response of the designed
# github . com/ j v i e r i n e / s i g n a l _ p r o c e s s i n g /026 _ i i r / p l o t _ h . py IIR low-pass filter. The bandwidth
import p l o t _ h is specified to be 100 Hz, with the
import s c i p y . s i g n a l as s stop-band starting at 200 Hz. Spectral
components of the signal in the stop-
# Design an I I R band−pass f i l t e r with : band are attenuated by at least 100
# Sample r a t e = 1000 Hz . dB.
# Pass −band = 100 t o 200 Hz .
# Maximum pass band r i p p l e 1 dB .
# Minimum a t t e n u a t i o n o f s i g n a l s out o f band = 100 dB .
sample_rate = 1000.0
# Return z e r o s and p o l e s o f t h e f i l t e r .
zeros , poles , k = s . i i r d e s i g n ( 1 0 0 , 2 0 0 , gpass = 3 . 0 , gstop =100 ,
f t y p e = ’ e l l i p ’ , output= " zpk " , f s =
sample_rate )
# Same f i l t e r , but r e t u r n f i l t e r c o e f f i c i e n t s .
b , a = s . i i r d e s i g n ( 1 0 0 , 2 0 0 , gpass = 3 . 0 , gstop =100 ,
f t y p e = ’ e l l i p ’ , output= " ba " , f s =s a m p l e _ r a t e )

# C r e a t e a white n o i s e s i g n a l .
s i g n a l = np . random . randn ( 1 0 0 0 0 0 )
Figure 235: A power spectrum estimate
# F i l t e r s i g n a l with t h e I I R f i l t e r s p e c i f i e d with of the filtered signal. The spectrum
# c o e f f i c i e n t s b and a . looks nearly identical to the analytic
filtered_signal = s . l f i l t e r (b , a , signal ) magnitude response of the filter shown
plt . plot ( f i lt e re d _s i g na l ) in Figure 234.
p l t . show ( )

# P l o t t h e power spectrum e s t i m a t e .
p l t . psd ( f i l t e r e d _ s i g n a l , NFFT=1024 , Fs=s a m p l e _ r a t e )
p l t . s a v e f i g ( " e x _ p s d _ l p f . png " )
p l t . show ( )

# P l o t t h e system f u n c t i o n and t h e magnitude response o f t h e I I R


filter .
p l o t _ h . plot_hmag ( " e x _ d e s i g n _ l p f . png " ,
z e r o s =zeros , p o l e s =poles , vmin= −150 ,
f s =s a m p l e _ r a t e )
350

Listing 66: 026_iir/iir_design.py

The inverse z-transform [Optional]

This section covers some details on the inverse z-transform. Although


the definition looks complicated, it is pretty straight forward to
compute. To do this, we require some tools from complex analysis.
Recall that the inverse z-transform is defined as:
1
I
x [n] = Z −1 { X (z)} = X (z)zn−1 dz. (972)
2πi C

The contour integral on the right can easily be computed by the


residue theorem, which states:
I
f (z)dz = 2πi ∑ Res( f , z0 ). (973)
C

The Res( f , z0 ) means the residue of f at z0 , where z0 is a pole for f .


The proper definition of the residue of a complex function f doesn’t
make sense if you haven’t seen the concept of a Laurent series, so
we’ll ignore a proper definition and just state how this quantity is
computed. The basic idea is to find this quantity called the residue of
f at a pole z0 with multiplicity m. The formula is given here as:

1 d m −1
Res( f , z0 ) = lim [(z − z0 )m f (z)]. (974)
z → z0 (m − 1)! dzm−1

Example: Pole without multiplicity


b
As an example, consider X (z) = 1−az −1 . The inverse z-transform is
then:
1 bzn−1
I
x [n] = dz = ∑ Res( f , z0 ),
2πi C 1 − az−1
where C is a loop going counterclockwise around all the poles of
X (z). In this case X (z) has one pole, being z = a. We get:

1 bzn−1
Res( f , a) = lim ( z − a )1 = lim bzn = ban .
z→ a 0! 1 − az−1 z→ a

We assume that the sequence x [n] is only valid for n ≥ 0, so the


resulting discrete-time signal is then:

x [n] = ban u[n], (975)

as was shown previously.


351

Example: Pole with multiplicity


Consider a more complicated example. Take X (z) as:

1
X (z) = .
(1 − az−1 )m

This can be rewritten as:


zm
X (z) = , (976)
(z − a)m

so we have a pole of order m. The inverse z-transform is then:

1 zm
I
x [n] = zn−1 dz, (977)
2πi C (z − a)m

where C is a loop going counterclockwise around the pole z0 = a.


Here we use the residue theorem (Equation 973) and Equation 974:

d m −1 zm
 
1 m n −1
Res( f , a) = lim ( z − a ) z , (978)
z→ a (m − 1)! dzm−1 (z − a)m
1 d m −1 m + n −1
= lim z , (979)
z→ a ( m − 1) ! dzm−1

1 ( m + n − 1) !
= lim zm+n−1−(m−1) , (980)
z→ a ( m − 1) ! ( m + n − 1 − ( m − 1)) !

1 ( m + n − 1) ! n
= lim z , (981)
z → a ( m − 1) ! n!
m+n−1 n
 
= a , (982)
m−1

where we’ve used the fact that:

dk n n!
k
z = zn−k , (983)
dz (n − k)!

and that (nk) = n!


(n−k)!k!
. Therefore, the signal is:

m+n−1 n
 
x [n] = a u [ n ], (984)
m−1

assuming x [n] = 0 for all n < 0.


352

Exercises: Infinite Impulse Response Filters

1. Consider the following linear time-invariant system y[n] =


T { x [n]}, which is defined as:

y [ n ] = y [ n − 1] + y [ n − 2] + x [ n − 1] (985)

a) Assuming that h[n] = 0 when n < 0, show that the first six
values of the impulse response h[n] = T {δ[n]} of the system
defined in Equation 985 are:

h [0] = 0 h [1] = 1 h [2] = 1 h [3] = 2 h [4] = 3 h [5] = 5

b) Would this filter be classified in signal processing jargon as a


finite impulse response filter (FIR), or an infinite impulse response
(IIR) filter?
c) z-transform Equation 985. Use Y (z) = Z {y[n]} and X (z) =
Z { x [n]}.
d) Show that the system function H(z) that corresponds to the
system defined in Equation 985 can be written as:

z −1
H(z) =
(1 − φ1 z−1 )(1 − φ2 z−1 )
Remember that the output of a linear time invariant system in
frequency domain is: Y (z) = H(z) X (z).
e) Determine the values of φ1 and φ2 .
f) The impulse response h[n] of the system defined in Equation
985 corresponds to the Fibonacci sequence. Provide a closed form
formula for the Fibonacci sequence without using recursion.
Hint: h[n] = Z −1 {H(z)}. You can check your result by com-
paring the first few values of the sequence h[0] = 0, h[1] = 1,
h[2] = 1, · · · .
g) Is the system described in Equation 985 bounded-input
bounded-output (BIBO) stable? In other words, does the sys-
tem provide a bounded output for every bounded input?
Justify your answer.

2. A time-shifted unit impulse signal is given by: x [n] = δ[n − n0 ]. The


z-transform of this signal is X (z) = z−n0 . What is the discrete-time
Fourier transform of x [n]?

3. Determine the inverse z-transforms of the following:


1+2z−2
a) A(z) = 1−0.25z−1
3 2
b) B(z) = 1−0.3z−1
− 1+0.7z −1
353

1+2z−2
c) C (z) = 1−0.4z−1 −0.32z−2

4. The output of a filter is given by the first-order difference equa-


tion:
y[n] = 0.8y[n − 1] + 5x [n] (986)

a) What type of filter is this (IIR or FIR)?


b) Find the system function H(z) of the filter described by equa-
tion 986.
c) Provide a pole-zero plot of H(z). Is this system stable or not?
Explain why.
d) Find the corresponding impulse response h[n].
e) Is this filter a high-pass filter, band-pass filter, or a low-pass
filter? Briefly explain why.
f) Let the input signal fed into the system by given by:

x [n] = u[n] (987)

Find the output y[n].

a) Im b) Im c) Im Figure 236: Three different pole-zero


diagrams. The unit circle z = eiω̂ is
marked in each diagram with a solid
black line. The zeros of the system
Re Re Re function H(z) are marked with circles
(o) and the locations of the poles are
marked with crosses (x). The real and
imaginary components of poles and
zeros only consist of the
√ following
numbers: 0, ±1, or ±(2 2)−1 .

5. A system function H(z) is the z-transform of the impulse re-


sponse of a discrete-time LTI system. Figure 236 shows pole-zero
diagrams that characterize three system functions: 236a), 236b)
and 236c). Answer the following questions related to these three
pole-zero diagrams. Justify your answers.

a) Sketch an approximate magnitude response for the three


LTI systems shown in Figure 236. Use normalized angular
frequency ω̂ in units of radians per sample on the x-axis and
the magnitude response |H(ω̂ )| of the y-axis.
b) Which one of the pole-zero diagrams corresponds to:

(z + 1)(z − α)(z − α∗ )
H(z) = . (988)
z3
√ √
Here α = i (2 2)−1 − (2 2)−1 .
354

c) A low pass filter is a filter that reduces the absolute ampli-


tude of high frequency spectral components relative to low
frequency spectral components. Which one of the diagrams
represents a stable low-pass filter?
d) Which one of the diagrams represents a stable high-pass filter?
e) The filter described in Figure 236c) completely filters out one
spectral component of the input signal. What is the frequency
of this spectral component in units of radians per sample.
f) Which of the filters have a finite length impulse response?
355

Suggested solutions: Infinite Impulse Response Filters

1. Let y[n] = T { x [n]} be a discrete LTI system, defined as:

y [ n ] = y [ n − 1] + y [ n − 2] + x [ n − 1].

a) The impulse response is simply h[n] = T {δ[n]}, giving that


h[n] must satisfy the following difference equation:

h [ n ] = h [ n − 1] + h [ n − 2] + δ [ n − 1].

Assume h[n] = 0 for n < 0, then by iterating, we have:

h[0] = h[−1] + h[−2] + δ[0 − 1] = 0,


h[1] = h[0] + h[−1] + δ[0] = 1,
h[2] = h[1] + h[0] + δ[1] = 1,
h[3] = h[2] + h[1] + δ[2] = 2,
h[4] = h[3] + h[2] + δ[3] = 3,
h[5] = h[4] + h[2] + δ[4] = 5,

as we wanted to show.
b) This is an infinite impulse response filter as it can be written as:
2 1
y[n] = ∑ a l y [ n − l ] + ∑ bk x [ n − k ] ,
l =1 k =0

with a1 = a2 = 1, b0 = 0 and b1 = 1.
c) Calculating the z-transform, setting Y (z) = Z {y[n]}, one gets:

Y (z) = Z {y[n − 1]} + Z {y[n − 2]} + Z { x [n − 1]},


Y (z) = z−1 Z {y[n]} + z−2 Z {y[n]} + z−1 Z { x [n]},
Y ( z ) = z −1 Y ( z ) + z −2 Y ( z ) + z −1 X ( z ),

where the final equation can be solved to obtain:

z −1
Y (z) = X ( z ).
1 − z −1 − z −2
d) The system function is, by definition, the function H(z), such
that:
Y (z) = H(z) X (z).
Clearly, we must have:

z −1 z z
H(z) = = 2 =
1 − z −1 − z −2 z −z−1 (z − φ1 )(z − φ2 )
This can be factored as:
z −1
H(z) = ,
(1 − φ1 z−1 )(1 − φ2 z−1 )
356

where φ1 and φ2 are the roots of the quadratic equation:

z2 − z − 1 = 0.

e) The values of φ1 and φ2 are the roots of the equation z2 −


z − 1. You can easily find the roots by solving the quadratic
equation by the standard formula. On the other, the equation
is the second degree polynomial that defined the golden ratio,
therefore, we know the roots are:

1+ 5
φ1 = ,
2√
1− 5
φ2 = .
2

f) Apply partial fraction decomposition to the system function:

z −1 A B
H(z) = = + ,
(1 − φ1 z−1 )(1 − φ 2 z −1 ) 1 − φ 1 z −1 1 − φ 2 z −1

from which we get:

z −1 = A (1 − φ 2 z −1 ) + B (1 − φ 1 z −1 ).

Collecting terms gives the following relations:

A + B = 0,
− Aφ2 − Bφ1 = 1.

Solving this linear system, we conclude that:

1
A= ,
φ1 − φ2
1
B=− .
φ1 − φ2

The system function can then be written as:

1 1 1 1
H(z) = −
− .
φ1 − φ2 1 − φ1 z 1 φ 1 − φ 2 1 − φ 2 z −1

Apply the inverse z-transform:


   
1 1 1 1
h[n] = Z −1 {H(z)} = Z −1 − Z −1
,
φ1 − φ2 1 − φ 1 z −1 φ1 − φ2 1 − φ 2 z −1
1 1
= φn u[n] − φ n u [ n ].
φ1 − φ2 1 φ1 − φ2 2

You can verify that this is indeed a function that generates the
Fibonacci sequence (do it!).
357

g) By looking at the system function H, we can see that there are


one zero at z = 0 and two poles having values of φ1 and φ2 ,
as defined above. Here φ1 is outside the unit circle, hence the
system is BIBO unstable.

2. If a time-shifted unit impulse: x [n] = δ[n − n0 ] satisfy X (z) =


z−n0 . Then the discrete-time Fourier transform is the z-transform
evaluated on the unit circle. Thus:

F { x [n]} = Z { x [n]}|z=eiω̂ = (eiω̂ )−n0 = e−iω̂n0 .

You can compare this with previous results and see that it is
correct.

3. Recall the following inverse z-transforms:

Z −1 { z − k } = δ [ n − k ] ,
 
b
Z −1 = ban u[n].
1 − az−1
a) For the complex function:

1 + 2z−2
A(z) = ,
1 − 0.25z−1
we can apply polynomial division to obtain:

33
A(z) = −8z−1 − 32 + .
1 − 0.25z−1
Now A(z) consists of known inverse z-transforms. The inverse
z-transform of A(z) is then:

a[n] = Z −1 { A(z)} = −8δ[n − 1] − 32δ[n] + 33(0.25)n u[n].

b) For the complex function:


3 2
B(z) = −
− ,
1 − 0.3z 1 1 + 0.7z−1
we can apply the known inverse z-transforms immediately:

b[n] = Z −1 { B(z)} = 3(0.3)n u[n] − 2(−0.7)n u[n].

c) For the complex function:

1 + 2z−2
C (z) = ,
1 − 0.4z−1 − 0.32z−2
we apply a polynomial division:

7.25 − 2.5z−1
C (z) = −6.25 + .
1 − 0.4z−1 − 0.32z−2
358

Factor the denominator:


7.25 − 2.5z−1
C (z) = −6.25 + .
0.32(2.5 + z−1 )(1.25 − z−1 )

Apply a partial fraction decomposition on the last term:

7.25 − 2.5z−1 A B
= + ,
0.32(2.5 + z−1 )(1.25 − z−1 ) 2.5 + z−1 1.25 − z−1
7.25 − 2.5z−1 = 0.32A(1.25 − z−1 ) + 0.32B(2.5 + z−1 ).

Get the following linear relations:

(0.32)(1.25) A + (0.32)(2.5) B = 7.25,


−0.32A + 0.32B = −2.5,

so A = 11.25 and B = 3.4375. Then C (z) can be written as:


11.25 3.4375
C (z) = −6.25 + + ,
2.5 + z−1 1.25 − z−1
4.5 2.75
= −6.25 + −
+ .
1 + 0.4z 1 1 − 0.8z−1
Now, we can apply our known inverse z-transform pairs to
obtain:

c[n] = −6.25δ[n] + 4.5(−0.4)n u[n] + 2.75(0.8)n u[n].

4. Let y[n] be given by the difference equation:

y[n] = 0.8y[n − 1] + 5x [n].

a) The equation contains y[n − 1] depending on one past value,


hence the filter is an IIR.
b) To find the system function, apply the z-transform:

Y (z) = 0.8z−1 Y (z) + 5X (z),

where Y (z) = Z {y[n]} and X (z) = Z { x [n]}. Rewriting it on


the form Y (z) = H(z) X (z) one gets:
5
Y (z) = X ( z ),
1 − 0.8z−1
Im(z)
so the system function is:
5 5z
H(z) = − 1
= .
1 − 0.8z z − 0.8

c) A pole-zero diagram for this system is shown in Figure 237. Re(z)

The poles for this system function all lie inside the unit circle,
so the system is stable.

Figure 237: The system function has


one zero and one pole in this case.
There is a zero at z = 0 and a pole at
z = 0.8. The zero is displayed as a blue
circle and the pole as a red cross.
359

d) The impulse response function can be found by the inverse


z-transform of the system function. In this case, we get:
 
5
h[n] = Z −1 {H(z)} = Z −1 = 5(0.8)n u[n].
1 − 0.8z−1

e) Any discrete LTI system can be written on the form:

y[n] = |H(ω̂ )|∠H(ω̂ ) x [n],

so the frequencies are multiplied with |H(ω̂ )|. In this case,


the filter is a low-pass filter. This can be seen by drawing the
magnitude response function, |H(ω̂ )|. Low frequencies are
attenuated while high frequencies are reduced, signifying a
low-pass filter. On the other hand, this can be seen from the
pole-zero diagram, as there is a pole close to z = 1, which
corresponds to the frequency ω̂ = 0. The pole will increase
the magnitude of the frequencies close to the frequency ω̂ = 0,
meaning they will get increased, which is the defining property
of a low-pass filter.
f) The filter is LTI, so the system can be written as a convolution:

y [ n ] = h [ n ] ∗ x [ n ].

Taking x [n] = u[n] gives:


∞ ∞
y[n] = h[n] ∗ u[n] = ∑ u[k]h[n − k] = ∑ h [ n − k ].
k =−∞ k =0

Set m = n − k, then the series can be rewritten as:


n
y[n] = ∑ h [ m ],
m=−∞
n
= ∑ 5(0.8)m u[m],
m=−∞

now u[m] kills the negative terms as u[m] = 0 for m < 0 and
u[m] = 1 for m ≥ 1, thus the series simplifies to a regular sum,
which can be found by the formula for a geometric sum:
n
y[n] = 5 ∑ (0.8)m ,
m =0
1 − 0.8n+1
=5 = 25(1 − 0.8n+1 ).
1 − 0.8

5. Consider the diagrams in Figure 236.

a) The first diagram has a pole at 0 and three zeros. There is a


zero at z = 1, which corresponds to ω̂ = 0, thus this frequency
360

is filtered out. The other zeros and poles are barely noticeable,
but the function will dip close to the zeros. For diagram b)
there is a pole at z = 1, which is on the unit circle. Therefore,
the function will tend towards infinity here. The other poles
don’t do much in comparison, but some small peaks would
occur close to these values. The last diagram filters out ω̂ =
±π as there is a zero for z = −1. A plot of the magnitude
responses is shown in Figure 238.

Figure 238: Magnitude response for the


three pole-zero diagrams. Note that the
limits of b) has been set to 0-10.

b) The system function:

(z + 1)(z − α)(z − α∗ )
H(z) = ,
z3
has zeros corresponding to z = −1, α, α∗ , thus this is the system

function corresponding to diagram c) as α = i (2 2)−1 −

(2 2)−1 , which has a negative real part.
c) Diagram b) and c) both reduce high frequency components
relative to low frequency components, meaning both of these
are low-pass filters. However, only diagram c) is stable. This
can be seen from Figure 238 as the magnitude response be-
comes unbounded at ω̂ = 0, but we can also see this from the
pole-zero diagram, as diagram b) has a pole on the unit circle.
Recall that a system function corresponds to a stable BIBO LTI
system if all the poles lie inside the unit circle. If any pole is on
the unit circle or outside the unit circle, the system is unstable.
d) The only diagram left is a), as b) and c) are both low-pass
filters. Diagram a) is indeed a high-pass filter as ω̂ = 0 gets
361

sent to 0 and the high frequencies pass through. The filter is


also stable as all the poles lie inside the unit circle.
e) The filter corresponding to diagram c) filters out ω̂ = π
completely. The filter has a zero at z = −1, which correspond
to eiω̂ = −1, meaning ω̂ = π.
f) Diagram a) and diagram c) represents FIR filters, as these only
have a pole at 0. Recall that the system function for a FIR filter
can be found by feeding a δ[n] into the LTI system. Observe:

y[n] = ∑ bk x [ n − k ] ,
k

which yield a system function of the form:

H(z) = ∑ bk z−k .
Bibliography

David W Kammler. A first course in Fourier analysis. Cambridge


University Press, 2007.

Andrey Kolmogoroff. Une série de Fourier-Lebesgue divergente


presque partout. Fundamenta mathematicae, 1(4):324–328, 1923.

Andreas Maier, Christopher Syben, Tobias Lasser, and Christian


Riess. A gentle introduction to deep learning in medical image
processing. Zeitschrift für Medizinische Physik, 29(2):86–101, 2019.

Claude Elwood Shannon. A mathematical theory of communication.


The Bell system technical journal, 27(3):379–423, 1948.

Edmund Taylor Whittaker. On the functions which are represented


by the expansions of the interpolation-theory. Proceedings of the Royal
Society of Edinburgh, 35:181–194, 1915.
Index

cos, 55 dependent variable, 33


sin, 55 difference equation, 318
z-transform, 314 diffraction limit, 246
Dirac delta function, 49
aliases, 137 Dirichlet kernel, 203
amplitude, 55 discrete Fourier transform,
analysis, 75, 78 257, 260
discrete Fourier transform
angular frequency, 55
basis vectors, 261
discrete-time, 12
band-pass filter, 200
discrete-time angular
bandpass, 349 frequency, 136
bandstop, 349 Discrete-Time Fourier
basis vectors, 260 Transform, 219
blocking filter, 326 dynamic spectrum, 281
boundary condition, 341
Bounded Input Bounded eigenfunction, 56, 195
Output, 343 electromagnetic waves, 62
Euclid’s algorithm, 77
cascade of first order filters, Euler’s formula, 10, 23
322 exponentially decaying, 314
chained LTI system, 170 exponentially growing, 314
commensurable, 77
complex number, 23 Fast Fourier Transform, 265
complex sinusoidal signal, 55 fast Fourier transform, 257
compression, 9 Fibonacci sequence, 352
compression algorithm, 9 filter bank, 276
continuous-time, 12 filters, 289
continuous-time Fourier folded alias, 139
transform, 87 forward z-transform, 315
contour integral, 315 forward Fourier transform,
convolution, 38, 165 111
convolution theorem, 320 Fourier series, 9, 75
366

Fourier transform, 11 Maxwell’s equations, 62


frequency, 56 modulus, 23
frequency domain, 111 MP3, 10
frequency response, 195, 196
fundamental angular NumPy, 18
frequency, 76 Nyquist oversampling, 143
fundamental frequency, 75
fundamental period, 57, 75, 76 operator., 37
fundamental theorem of oversampled, 143
algebra, 322
partial fraction decomposition,
geometric series, 203, 261, 327 346
periodic discrete-time signals,
Heaviside-function, 50 258
Heisenberg uncertainty periodic convolution, 266
principle, 245 periodic function, 57
high-pass filter, 200 periodic sinc, 203
highpass, 349 phase, 55
phase response, 198
IIR, 339 phasor, 55
impulse response, 168 phasor summation, 58
independent variable, 33 polar representation, 23
infinite impulse response, 339 pole-zero diagrams, 346
inverse z-transform, 315 pole-zero diagram, 343
inverse discrete Fourier poles, 345
transform, 260 principal alias, 141
inverse Euler, 24 principal spectrum, 141
inverse Fourier transform, 111 Python, 15, 18

JPEG, 10 quantized, 36

Kronecker delta, 260 radians per sample, 136


region of convergence, 315
Laplacian operator, 63 reversibility, 262
Linear system, 38 Richard Feynman, 10
linear time-invariant, 38 running average, 327
Linear time-invariant systems,
165 sample-rate, 135
linearity, 38 sample-spacing, 135
lowpass, 349 sampling-rate, 135
LTI, 38 sifting, 50
signal, 33
magnitude, 23 signal processing, 9
magnitude response, 197 signals and systems, 9
367

simple algebraic time-shifted unit impulse, 316


manipulations, 318 transfer function, 314
spectral analysis, 275 two dimensional complex
spectrogram, 281 sinusoidal signal, 59
stability, 343 two-dimensional complex
synthesis, 75, 78 sinusoidal signal, 62
system, 33, 36
system function, 313, 314 unit impulse, 49
unit step, 49
tapering windows, 234
time domain, 111 wave equation, 63
time shifted signal, 121 wavenumber, 57
time-frequency ambiguity, 204 whitening filter, 294
time-frequency uncertainty window functions, 234
principle, 245
time-invariance, 38 z-transform, 313
Time-invariant, 39 zero-padded DFT, 264
time-shift theorem, 319 zeros, 346

You might also like