Signal Processing
Signal Processing
Signal Processing
SIGNAL PROCESSING
UNIVERSITY OF TROMSØ
Copyright © 2024 Juha Vierinen, Jørn Olav Jensen
http://kaira.uit.no/fys2006
August 2024
Contents
Syllabus 7
Introduction 9
Python 15
Complex Algebra 23
Elementary Signals 49
Sinusoidal Signals 55
Fourier Series 75
Z-transform 313
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.
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
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:
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
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
Exercises: Python
1. Obtain the source code for the examples by cloning the GitHub
repository for this course.
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 )
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
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?
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 r i n t ( np . exp ( 1 j * np . p i ) +1)
# 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 )
# 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 )
# 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 ( )
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
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:
e z1 e z2 = e z1 + z2 . (7)
z∗ = x − iy (8)
= A[cos(θ ) − i sin(θ )] (9)
= A[cos(−θ ) + i sin(−θ )] (10)
−iθ
= Ae . (11)
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
z = Aeiθ (17)
# 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 )
# 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 )
Listing 9: 008_complex_ops/ops_example.py
27
2. Use Euler’s formula to write 1/i into polar form Aeiϕ with A, ϕ ∈
R. What is the phase angle ϕ?
zn = 1. (19)
A cos(ωt + ϕ)
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.
1 i
= 2 = −i.
i i
3π
Using Euler’s formula eiθ = cos θ + i sin θ we take θ = 2 , this gives
a polar representation of −i as:
3π i
−i = e 2 .
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 .
import m a t p l o t l i b . pyplot as p l t
import numpy as np
# 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 )
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:
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
1 3iθ 1
cos(3θ ) = (e + e−3iθ ) = ((eiθ )3 + (e−iθ )3 ).
2 2
When expanded the first term is:
eαi e βi = (cos α + i sin α)(cos β + i sin β) = (cos α cos β − sin α sin β) + i (sin α cos β + cos α sin β).
10. Consider the complex exponential of the form eiθ , then by this
new definition we have:
∞
(iθ )k
eiθ = ∑ k!
.
k =0
θ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:
Signals
• density,
34
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
x : ZN → C (24)
Systems
signal in x (t) signal out y(t) Figure 24: A simple signal processing
System T {·} system block diagram.
37
signal processing.
x1 ( t ) T {·} × c2
Linear system
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:
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)
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.
x (t)
Example: Overdriven amplifier −2 2
1 4
5 k∑
y[n] = x [ n − k ]. (43)
=0
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
# 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 ( )
# 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 .
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
Here we get that the outputs are equal, hence the system is
time-invariant.
1 4
5 k∑
y[n] = x [ n − k ].
=0
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.
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
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
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:
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 − τ ) > β,
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
# 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
# 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 )
alpha = 2 . 0 # A m p l i f i e r gain .
beta = 0.08 # Cut o f f .
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
return y
# 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 ( )
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
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ω =?
−∞
evaluates to:
α(eiωL − e−iωL )
What is α?
assuming L > 0.
53
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.
ω = 2π f (58)
2π 1
T= = . (62)
ω f
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:
π
The output of the time-shift system for a complex sinusoidal signal 2
This means that all the time delays that could have caused a phase
shift θ are:
τ = −ω −1 (θ + 2πk). (78)
dθ
= −τ. (79)
dω
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.
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 :
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 τ:
dθ
=τ (85)
dω
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.
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 ( )
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
∂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.
∂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
∂2 ⃗E
= −ω 2 ⃗E( x, t) (95)
∂t2
∇2 ⃗E = −k2 ⃗E( x, t). (96)
∂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:
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
Hint: You can often write a Python program to verify your result if
you are not sure if it is correct!
2. Prove that:
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
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,
# 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 ( )
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.
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 .
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
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
# 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 )
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 ( )
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
# 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 ( )
# 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 )
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:
ω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 = ℓk ω . (120)
2π
T= . (121)
ω
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.
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).
N 2π kt
x N (t) = ∑ c k ei T . (126)
k =− N
Figure 49: Jean-Baptiste Joseph Fourier
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
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).
N 2π kt
x N (t) = ∑ c k ei T . (147)
k =− N
81
1
Z
2π kt
ck = x ( t ) e −i T dt . (148)
T T
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)
∞
2π
= 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.
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
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
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.
Z T
1
ck = x (t)e−i2πkt/T dt , (164)
T 0
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
1 T
Z
2π
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(θ ).
π π
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
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
# 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
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
2πk n
(n)
ck := i n ck . (182)
T
∞ 2π kt
x (t) = ∑ c k ei T , (185)
k =−∞
87
c −1 c1
c −2 c2
c −3 c3
c −4 c4
··· ∆ω ···
ω
−4∆ω−3∆ω−2∆ω −∆ω ∆ω 2∆ω 3∆ω 4∆ω
Z ∞
1
x (t) = x̂ (ω )eiωt dω . (198)
2π −∞
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
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 ( )
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
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
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
ω π 1
f = = = .
2π 2π 2
2π 2π
T= = = 2 seconds.
ω π
Simplifying gives:
(60π, 6π ),
(54π, 6π ),
(48π, 6π ),
(42π, 6π ),
(36π, 6π ),
(30π, 6π ),
(24π, 6π ),
(18π, 6π ),
(12π, 6π ),
(6π, 6π ),
2π 2π 1
T= = = ,
ω 6π 3
1
hence T = 3 in units of seconds.
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
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 )
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 ( )
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.
# Compress audio .
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
# 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 " )
# 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 .
# 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 )
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 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
# 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 )
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.
# 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 ( ) )
r e t u r n ( ( comp_image , i m a g e _ f f t ) )
103
# 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$ " )
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 ( )
N 2π kt
IN (t) = ∑ c k ei T . (207)
k =− N
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
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
# Measurement ti me s .
m_error = d [ : , 2 ]
# Fundamental period .
T = 13.124349
# 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 ( t , s i g . imag )
p l t . x l a b e l ( " Time ( s ) " )
p l t . show ( )
• Convolution theorem
Z ∞
x̂ (ω ) = x (t)e−iωt dt (213)
−∞
Z ∞
1
x (t) = x̂ (ω )eiωt dω . (214)
2π −∞
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
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)
ω ω
f (ω ) f ′ (ω )
lim = lim ′ . (234)
ω →c g(ω ) ω →c g (ω )
2 sin(ωT/2)
lim = lim T cos(ωT/2) = T . (235)
ω →0 ω ω →0
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
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)
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:
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)
F 1
x (t) = e− βt u(t) ←
→ x̂ (ω ) = . (253)
β + iω
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)
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)
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 =−∞
∞ 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.
F 1 ω
y(t) = x ( at) ←
→ ŷ(ω ) = x̂ . (273)
| a| a
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)
−∞
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π −∞
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π −∞
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
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)
−∞ −∞
F 1
y ( t ) = x1 ( t ) x2 ( t ) ←
→ ŷ(ω ) = x̂ (ω ) ∗ x̂2 (ω ) . (298)
2π 1
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.
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π −∞
Note that there are more than one way to solve the Fourier trans-
forms!
a) x̂ (ω ) = 2 + cos(ω )
1 1
b) x̂ (ω ) = 1+iω − 2+iω
c) x̂ (ω ) = iδ(ω − 100π ) − iδ(ω + 100π ).
1. a) If
x ( t ) = δ ( t + 2) + δ ( t ) + δ ( t − 2),
then by linearity we have that:
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:
c) Take:
x ( t ) = e − t u ( t ) − e − t u ( t − 4),
then by linearity, the Fourier transform is:
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:
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 (ω ),
ω
−ωmax ωmax
F 1
y(t) = x (t)c(t) ←
→ ŷ(ω ) = x̂ (ω ) ∗ ĉ(ω ) . (319)
2π
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).
ω
−ωc − ωmax −ωc + ωmax −ωmax ωmax ωc − ωmax ωc + ωmax
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.
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:
H(ω )
ω
−ωmax ωmax
RC-circuit [Optional]
vin C vout
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
−α α
• Reconstruction
We’ll need to know about complex sinusoidal signals and the fre-
quency domain representation of signals (the Fourier transform).
1
fs = . (348)
Ts
136
ω̂0 = ω0 Ts . (353)
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
where k ∈ Z. What this means is that we can select any one of the
discrete-time angular frequencies:
ωk = ω0 + 2πk f s (363)
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)
A iϕ iωt A −iϕ
2e
A iϕ
2e
A cos(ωt + ϕ) = (e e + e−iϕ e−iωt ) . (366)
2
A −iϕ i0.1t
(e e + eiϕ e−i0.1t ) = A cos(0.1t − ϕ) (368)
2
= A cos(−0.1t + ϕ) . (369)
fk = f0 + fs k (378)
= . . . , −19, −9, 1, 11, 21, 31, . . . (379)
Folded Alias
Alias
Alias
Alias
ω̂
−2π −π π 2π
141
Principal spectrum
The principal spectrum (also called normalized angular frequency) is
the range of discrete-time angular frequencies between:
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.
with frequencies:
f 0 = 0.1 f s , (386)
f 1 = 0.2 f s , (387)
f 2 = 0.3 f s . (388)
f 3 = 0.7 f s , (395)
f 4 = 0.8 f s , (396)
f 5 = 0.9 f s . (397)
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 .
Alias
Alias
Alias
True
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
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π
f = −0.8 2s , ω̂ = −0.8π
f = 1.2 2s , ω̂ = 1.2π
sinusoidal signals:
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)
2D aliasing
Consider a two-dimensional signal I ( x, y) ∈ R, which is discretized
as follows:
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 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
This filter is infinitely long, which implies that all samples need to
be used in order for the reconstructed signal to be mathematically
perfect.
Sampling
∞ 2π
s(t) = ∑ ck ei Ts kt . (431)
k =−∞
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
2π
= δ(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 =−∞
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
| x̂s (ω )|
··· ···
ω
− ωs −ωs /2 ωs /2 ωs
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
ŝ(ω + kωs )
x̂s (ω )
··· ···
ω
− ωs −ωs /2 ωs /2 ωs
Complex-valued signal
x̂ (ω ) ∈ C
ω
− ωs −ωs /2 ωs /2 ωs
| 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
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
= 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
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
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?
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)?
| 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?
| 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
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 ( )
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
| x̂s (ω )|
ω
ω0 ω1
R∞
5. As before, let x (t) = (2π )−1 −∞ x̂ (ω )eiωt dω and:
> 0 when ω0 < | ω | < ω1 ,
| x̂ (ω )| =
0, otherwise,
| x̂s (ω )|
ω
− ω1 − ω0 ω0 ω1
2π30 ≤ |ω | ≤ 2π40,
Then:
∞
1
H(ω ) x̂s (ω ) = Ts
Ts ∑ x̂ (ω − kωs ) = x̂ (ω ).
k =−∞
1 7
y[n] = ∑ x [n − k] .
15 k=−
(464)
7
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 =−∞
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 =−∞
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 )
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
Convolution
Z ∞
a(t) ∗ b(t) = a(τ )b(t − τ )dτ . (489)
−∞
∞
a[n] ∗ b[n] = ∑ a[k]b[n − k] . (490)
k =−∞
Properties of a convolution
= ∑ ∑ a[ℓ]b[m]c[(n − m) − ℓ] . (494)
ℓ m
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!
signal.
··· ··· ···
Propagation velocity (r = n)
h[5] h[5] h[5] ···
λ[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
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
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
# 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 )
f o r i i n range ( l e n ( h ) ) :
H += h [ i ] * np . exp ( 1 j * omhat * i )
# 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 )
p l t . stem ( time_vec , h )
else :
p l t . p l o t ( time_vec , h )
# 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 " )
# 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 ) ) )
L −1
1
y[n] = T { x [n]} =
L ∑ x [n − k] (506)
k =0
L −1
1
y[n] =
L ∑ x [ n − k ]. (507)
k =0
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
y = np . z e r o s _ l i k e ( x )
# TODO: implement me !
return y
i f l e n ( sys . argv ) ! = 2 :
p r i n t ( f " Usage : L " )
e x i t ( −1)
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)
y [ n ] = h [ n ] ∗ x [ n ], (508)
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
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].
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 ),
L k∑ ∑e
y[n] = e = e = e .
=0
L k =0
L 1 − e−iω̂0
e−iω̂0 ( L−1)/2
ω̂ = 0,
ω̂ = π,
ω̂ = 0.5π,
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)
y2 [n] = T {T { x [n]}} = T {h[n] ∗ x [n]} = h[n] ∗ (h[n] ∗ x [n]) = (h[n] ∗ h[n]) ∗ x [n].
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
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 .
3. a) The output signal y[n] will have the same length as x [n], thus,
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.
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 )
return y
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 ) )
import m a t p l o t l i b . pyplot as p l t
import numpy as np
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 l e n ( sys . argv ) ! = 2 :
p r i n t ( " Usage : <L> " )
exit (1)
i f L i s None :
p r i n t ( " L should be an i n t e g e r ! " )
exit (1)
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
# 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
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 . " " "
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 ) )
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 l e n ( sys . argv ) ! = 3 :
p r i n t ( " Usage : <filename > <L> " )
exit (1)
# 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 )
# 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 )
The effect on the audio can be heard between the original and
for large L. The higher frequencies have been reduced, so they
187
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.
# 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
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 . " " "
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 ) )
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 ) .
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
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
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 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
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
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 ) .
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 )
t = np . arange ( l e n g t h )
t = Ts n.
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
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
# 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
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
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 ) )
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 ) .
return h
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
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 " )
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
F
y(t) = h(t) ∗ x (t) ←
→ ŷ(ω ) = H(ω ) x̂ (ω ) . (520)
)|
(ω̂
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
Im{H (ω̂ )}
−1
∠H (ω̂ ) = tan . (536)
Re{H (ω̂ )}
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
y [ n ] = x [ n ] − x [ n − 1] . (545) −1
import m a t p l o t l i b . pyplot as p l t
import numpy as np
1.5
h[n]
Example: Delay filter 1
0.5
A delay system is defined as: n
n0
|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.
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
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:
D M (0) = 1 . (582)
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
# 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 )
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)
−∞
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.
d2 x x (t + Ts ) − 2x (t) + x (t − Ts )
= lim (591)
dt2 Ts →0 Ts2
2
H(ω̂ ) = [cos(ω̂ ) − 1] (592)
Ts2
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
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=−∞
the only way for this to work is that |ωc | ≤ π giving c = 42.
210
# 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
|H(ω̂ )|2 = H(ω̂ )H∗ (ω̂ ) = ( D M (ω̂ )Hτ (ω̂ ))( D M (ω̂ )Hτ (ω̂ ))∗ ,
= | D M (ω̂ )|2 e|−iω̂τ i ω̂τ 2
{ze } = | D M (ω̂ )| ,
1
as claimed.
2
H(ω̂ ) = Ts−2 eiω̂ − 2Ts−2 + Ts−2 e−iω̂ = −2Ts−2 + Ts−2 2 cos(ω̂ ) = (cos(ω̂ ) − 1).
Ts2
# 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
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
signal.
··· ··· ···
Propagation velocity (r = n)
h[5] h[5] h[5] ···
λ [ n ] ∗ m [ n ] = λ [ n ] ∗ x [ n ] ∗ h [ n ]. (594)
• 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 )
N p −1
ϵ[n] = ∑ x [n − Mk ] (598)
k =0
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.
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)
∞
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=−∞
Periodicity
ω̂
The DTFT x̂ (ω̂ ) is a 2π-periodic function: −2π −π π 2π
∞
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
1
Z π
x [n] = x̂ (ω̂ )eiω̂n dω̂ . (612)
2π −π
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)
∞
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].
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.
F
→ x̂ (ω̂ ) = e−iω̂n0 .
x [ n ] = δ [ n − n0 ] ← (622)
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]:
Convolution theorem
An often used theorem of signal processing is the convolution the- x [n] x [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]:
F
h3 [ n ] = h1 [ n ] ∗ h2 [ n ] ←
→ H3 (ω̂ ) = H1 (ω̂ )H2 (ω̂ ) (632)
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ω̂ .
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
Since the signal x [n] is real: x ∗ [n] = x [n]. Then evaluating at x̂ ∗ (ω̂ )
at −ω̂:
∞
x̂ ∗ (−ω̂ ) = ∑ x [n]e−iω̂n ,
n=−∞
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π −π
1
Z π
x̂ ∗ [−n] = x̂ (ω̂ )eiω̂n dω̂,
2π −π
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
h1 [ n ] = δ [ n ] − δ [ n − 1],
h2 [ n ] = δ [ n ] + δ [ n − 2],
h3 [ n ] = δ [ n − 1] + δ [ n − 2].
y [ n ] = h [ n ] ∗ x [ n ],
h [ n ] = h1 [ n ] ∗ h2 [ n ] ∗ h3 [ n ].
H1 (ω̂ ) = 1 − e−iω̂ ,
H2 (ω̂ ) = 1 + e−2iω̂ ,
H3 (ω̂ ) = e−iω̂ + e−2iω̂ .
Then:
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
10 10
x̂ (ω̂ ) = e−42iω̂ ∑ e−iω̂k = ∑ e−iω̂ (k+42) .
k =−10 k =−10
Ideal filters
(
1 |ω̂ | < ω̂0
HLP (ω̂ ) = . (633)
0 ω̂0 ≤ |ω̂ | ≤ π
ω̂
−π − ω0 ω0 π
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
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.
ω̂
−π − ω0 ω0 π
sin(πn)
lim =1. (649)
n →0 πn
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
1 iω̂0 n F
h[n] = e ←→ H (ω̂ ) = δ(ω̂ − ω̂0 ) . (657)
2π
1 π
Z
h[n] = δ(ω̂ − ω̂0 )eiω̂n dω̂ (658)
2π −π
1 iω̂0 n
= e . (659)
2π
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)
2π
1
= cos(ω̂0 n) . (663)
π
∞
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 )
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
# 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 )
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 ( )
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]
om = np . l i n s p a c e ( −np . pi , np . pi , num=1000)
# Window f u n c t i o n and l e n g t h .
238
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 .
# 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
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 .
# 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 ) )
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.
sin(ω̂0 n) sin(ω̂1 n)
h[n] = δ[n] + − .
nπ nπ
A tapered version of this filter is then found by:
# 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
# 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)
n = np . arange (N)
# 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 ) )
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.
∆ω∆t = 2 . (685)
Time scaling
Recall the time scaling system from the chapter on Fourier trans-
forms, which adjusts the width of a signal using the constant α:
F 1 ω
y(t) = x (αt) ←
→ ŷ(ω ) = x̂ . (687)
|α| α
∆φ∆D = λ , (688)
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.
kg·m/s.
250
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
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.
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
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
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
# 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 ( )
∆ω1 = 4π/T,
∆ω2 = 8π/T,
1
Ψ( x ) = √ [u( x + T ) − u( x − T )].
2T
254
1 1 1
|Ψ̂(k)|2 = √ e−ikx0 √ eikx0 = .
2π 2π 2π
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π )
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
This is just a small fraction of the applications of the DFT and the
FFT algorithm.
258
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 =−∞
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 =−∞
∞
2π
=
NTs ∑ x̂ [k ]δ (ωk − ω ) (712)
k =−∞
∞ N −1
2π
=
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
N −1 2π
x̂ [k] = F D { x [n]} = ∑ x [n]e−i N nk . (714)
n =0
N k∑
−1
x [n] = F D { x̂ [k]} = x̂ [k]ei N nk . (716)
=0
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
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
S = α 0 + · · · + α N −1
αS = α1 + · · · + α N
S − αS = 1 − α N
1 − αN
S= .
1−α
2π
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.
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
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].
This means that the DFT is the DTFT evaluated at discrete points:
2π
ω̂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
M −1 2π
x̂zp [k ] = x̂ (ω̂k ) = ∑ xzp [n]e−i M kn . (742)
n =0
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
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 )
# 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 ( )
Convolution
N −1
a[n] = ∑ b[k]c[(n − k ) mod N ] . (749)
k =0
267
D F
a[n] = b[n] ⊛ c[n] ←→ â[k] = b̂[k ]ĉ[k] . (750)
Example: 2D FFT
N −1 N −1 2π 2π
x̂ [k, ℓ] = ∑ ∑ x [n, m]e−i N nk e−i N mℓ . (751)
n =0 m =0
# Sample period .
Ts = 1e −4
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
fs 17 · 103 Hz
N= = = 170 000.
∆f 0.1 Hz
ω̂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
ω̂0 /ω̂1 = 4,
ω̂1 /ω̂2 = 2/5,
ω̂2 /ω̂0 = 8/5,
2π 2π
N= = = 20 samples.
ω̂ 2π500Ts
2π 2πTs 2πTs 1
T= = = = = 0.002 s,
ω ω̂ 2π500Ts 500
# Sample period .
Ts = 1e −4
# 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 ( )
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].
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)
ℓ=−∞
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
They are the same! This means that a DFT evaluates the output of
frequency selective filters with frequencies:
2π
ω̂k = k. (763)
N
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
Windowed DFT
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:
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 ( )
M −1 2π
x̂ [t, k] = ∑ x [n + t∆n]w N (n)e−i M kn . (775)
n =0
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
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
# 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
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]
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
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
# 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 )
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 )
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 .
"""
# Length o f s i g n a l .
L = len ( signal )
# 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
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 ].
x2 [ n ] = a1 δ [ n − n1 ] + a2 δ [ n − n2 ] + a3 δ [ n − n3 ], (780)
x [ n ] = x1 [ n ] + x2 [ n ]. (781)
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
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
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
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
w = hann (N)
WX = np . f f t . r f f t (w* x , 2 *N)
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.
#
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
# 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)
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
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
5
x1 [ n ] = ∑ Ai cos(2π fi i) + wn ,
i =1
x2 [n] = a1 δ[n − 8192] + a2 δ[n − 9192] + a3 δ[n − 7192],
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 ( )
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
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 ( )
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
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
# 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 ] )
# 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
Running this code will generate the plots shown in Figure 192
and 193.
Instructions
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?
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?
N −1 2π
x̂ [k] = ∑ x [n]e−i N kn . (792)
n =0
306
Hint: Use the fft function to evaluate the DFT. This function is
available in Python as numpy.fft.fft.
f) Mark the locations of 31.5 and -31.5 Hz on the plot of the power
spectrum.
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.
5. Whitening filter
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
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.
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
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
7. Time delay
8. Dynamic spectrum
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
= 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 =−∞
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.
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)
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 =−∞
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
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.
∞
x̂ (ω̂ ) = ∑ x [k]e−iω̂k . (813)
k =−∞
Linearity
Z
α1 x1 [ n ] + α2 x2 [ n ] ←
→ α 1 X1 ( z ) + α 2 X2 ( z ) . (815)
0.5
Time-shifted unit impulse δ[n − 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:
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)
x [ n ] = δ [ n − n0 ] (831)
318
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
This is a finite impulse response filter (shown in Figure 205), with the Figure 205: A finite impulse response
following impulse response: h [ n ].
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.
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:
We can now use Equation 832, to obtain the impulse response, which
is the inverse z-transform of H(z):
This is shown in Figure 206. We can see that the difference equation
for this LTI system can be written as 2
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 =−∞
Convolution theorem
=∑ ∑ 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)
N
h[n] = ∑ bk δ [ n − k ] . (869)
k =0
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
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)
y [ n ] = x [ n ] − x [ n − 1] (878)
h [ n ] = δ [ n ] − δ [ n − 1]. (879)
This can also be viewed as a cascade of three different first order FIR
filters:
shown below.
Re(z)
x [n] y[n]
h[n]
x [n] y[n]
h1 [ n ] h2 [ n ] h3 [ n ]
# 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 ) " )
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 ( )
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
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:
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
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
2π
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
Exercises: Z-transform
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]
y[n] = ∑ αk x [n − nk ] (907)
k
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 )
Two of these systems will null out a single frequency and one of
these systems will apply time-shift to the signal.
import m a t p l o t l i b . pyplot as p l t
import numpy as np
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 ( )
# 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 ] )
# 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 ( )
H1 (z) = z − eiπ/4 ,
H2 (z) = z − e−iπ ,
H3 ( z ) = z −2 ,
334
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 ).
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 ) ) ]
y [ n ] = h [ n ] ∗ x [ n ].
# 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) )
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
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)
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
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[−∞] = 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
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
n
Example: IIR system function
Consider an impulse response of the following form: −1
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
z − 0.5 P(z)
H(z) = = . (944)
z + 0.9 Q(z)
eiω̂ − 0.5
H(ω̂ ) = (945)
eiω̂ + 0.9
This is shown in Figure 228. This filter is a high-pass filter.
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)
∞
|y[n]| = ∑ h[k] x [n − k] (948)
k =−∞
∞ ∞ ∞
∑ h[k] x [n − k] ≤ ∑ |h[k]| | x [n − k]| ≤ Mx ∑ |h[k]|. (949)
k =−∞ k =−∞ k =−∞
| {z }
≤ Mx
∞
∑ |h[k]| ≤ Mh < ∞. (950)
k =−∞
b
H(z) = (951)
1 − az−1
When is this system stable?
The impulse response of this system is:
Using the BIBO stability criterion, the output of this system is stable
if:
∞
∑ |h[k]| ≤ Mh < ∞. (953)
k =−∞
If | a| ≥ 1, then
∞ N
∑ |h[k]| = lim |b|
N →∞
∑ | a|k → ∞ (956)
k=−∞ k =0
∏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.
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 )
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
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]
# 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 ( )
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 ( )
1 d m −1
Res( f , z0 ) = lim [(z − z0 )m f (z)]. (974)
z → z0 (m − 1)! dzm−1
1 bzn−1
Res( f , a) = lim ( z − a )1 = lim bzn = ban .
z→ a 0! 1 − az−1 z→ a
1
X (z) = .
(1 − az−1 )m
1 zm
I
x [n] = zn−1 dz, (977)
2πi C (z − a)m
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
dk n n!
k
z = zn−k , (983)
dz (n − k)!
m+n−1 n
x [n] = a u [ n ], (984)
m−1
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:
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.
1+2z−2
c) C (z) = 1−0.4z−1 −0.32z−2
(z + 1)(z − α)(z − α∗ )
H(z) = . (988)
z3
√ √
Here α = i (2 2)−1 − (2 2)−1 .
354
y [ n ] = y [ n − 1] + y [ n − 2] + x [ n − 1].
h [ n ] = h [ n − 1] + h [ n − 2] + δ [ n − 1].
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:
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
z2 − z − 1 = 0.
z −1 A B
H(z) = = + ,
(1 − φ1 z−1 )(1 − φ 2 z −1 ) 1 − φ 1 z −1 1 − φ 2 z −1
z −1 = A (1 − φ 2 z −1 ) + B (1 − φ 1 z −1 ).
A + B = 0,
− Aφ2 − Bφ1 = 1.
1
A= ,
φ1 − φ2
1
B=− .
φ1 − φ2
1 1 1 1
H(z) = −
− .
φ1 − φ2 1 − φ1 z 1 φ 1 − φ 2 1 − φ 2 z −1
You can verify that this is indeed a function that generates the
Fibonacci sequence (do it!).
357
You can compare this with previous results and see that it is
correct.
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:
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
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 ).
The poles for this system function all lie inside the unit circle,
so the system is stable.
y [ n ] = h [ n ] ∗ x [ n ].
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
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.
(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
y[n] = ∑ bk x [ n − k ] ,
k
H(z) = ∑ bk z−k .
Bibliography
JPEG, 10 quantized, 36