0% found this document useful (0 votes)
48 views8 pages

PHYS 381 W23 Assignment 4

This document outlines an assignment for a physics course on Fourier analysis using Python. The assignment introduces Fourier analysis of periodic and non-periodic signals using Fourier series and transforms. Students are asked to write code to analyze example periodic signals and square and pulse train waves by calculating their Fourier coefficients and reconstructing the original signals using varying numbers of coefficients. They are also asked to discuss and attach their Jupyter notebooks.

Uploaded by

Nathan Ngo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views8 pages

PHYS 381 W23 Assignment 4

This document outlines an assignment for a physics course on Fourier analysis using Python. The assignment introduces Fourier analysis of periodic and non-periodic signals using Fourier series and transforms. Students are asked to write code to analyze example periodic signals and square and pulse train waves by calculating their Fourier coefficients and reconstructing the original signals using varying numbers of coefficients. They are also asked to discuss and attach their Jupyter notebooks.

Uploaded by

Nathan Ngo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PHYS 381 - Winter term 2023

Assignment #4: Fourier analysis using Python (total 10


points), due by 3:00 pm Monday, 27 March 2023

This assignment introduces the following:


• Fourier analysis of both periodic and non-periodic signals (Fourier series, Fourier transform,
Discrete Fourier transform);
• The use of Simpson’s rule for numerical integration.
Fourier analysis is an extremely important tool in the investigation of signals; it essentially
decomposes a signal into constituent harmonic vibrations. Sometimes the signals are periodic, and
the period can be measured directly. But Fourier analysis is also useful for investigating signals
which are not periodic. In the first part of this assignment, you will deal with periodic signals, while
in the second part you will see that the numerical Fourier method applies with some modification
also to the case of non-periodic signals (known as the Discrete Fourier Transform, or DFT.)

1 Fourier series
Any periodic function f (t), with period T = 2π/ω, can be represented as a Fourier series

X
f (t) = a0 + [an cos(nωt) + bn sin(nωt)] (1)
n=1

The sine and cosine functions are harmonic functions, and the series (1) contains a possibly infinite
set of harmonic functions with discrete frequencies ωn = nω, n = 1, 2, . . . . The frequency ω1 = ω is
known as the fundamental frequency, and ωn , n > 1, are the harmonics. The coefficients an and bn
measure the ‘amount’ of cos(nωt) and sin(nωt) present in the function f (t). The result of Fourier-
analysing a signal is a set of values for these coefficients for all n. In practice, the coefficients will
be obtained for all n up to some finite maximum N . The Fourier coefficients are evaluated using
the orthogonality properties of sines and cosines:
2 T
Z
sin(nωt) sin(kωt)dt = δnk (2)
T 0
2 T
Z
cos(nωt) sin(kωt)dt = 0 (3)
T 0
2 T
Z
cos(nωt) cos(kωt)dt = δnk (4)
T 0

1 of 8
PHYS 381 - Winter term 2023

where δnk is the Kronecker delta. Applying these orthogonality rules onto equation (1), we obtain
the coefficients
1 T
Z
a0 = f (t)dt (5)
T 0
2 T
Z
ak = f (t) cos(kωt)dt k = 1, 2, . . . (6)
T 0
2 T
Z
bk = f (t) sin(kωt)dt k = 1, 2, . . . (7)
T 0

1.1 Simpson’s Rule


A Fourier analysis program must perform the integrations as shown in the equations above for any
function f (t) of interest. Simpson’s rule will be used in this assignment to perform our numerical
Rb
integrations. Simpson’s rule approximates the integral a dx f (x) by splitting the interval from
x = a to x = b into n steps of equal length h = (b − a)/n where n is an even number. The
integration is approximated as
 
Z b n/2−1 n/2
h X X
f (x) dx ≈ f (x0 ) + 2 f (x2j ) + 4 f (x2j−1 ) + f (xn ) (8)
a 3 j=1 j=1

where xj = a + jh for j = 0, 1, . . . , n − 1, n with h = (b − a)/n, in particular, x0 = a and xn = b.

1.2 Problems
R1
(a) Write a Python code for the computation of the integral I = 0 exp(x) dx using Simpson’s rule.
Compare your numerical result with the analytical value for I. Test your integration for n = 50
and n = 100 steps for the Simpson’s rule. This exercise item does not render points but it is
an important one to start the exercise and to test the integration routine that will be used in
the next exercises.

(b) Write a Python code to compute and plot the Fourier coefficients ak and bk for the functions
below. You should plot ak × k and bk × k in the same plot. Calculate a maximum of 10
coefficients, i.e., kmax = 10. Note that these functions are chosen so that you can check the
performance of your program, as the functions are already in the form of equation (1). Set
ω = 1 in the numerical computations. (1 point)

• f (t) = sin(ωt)
• f (t) = cos(ωt) + 3 cos(2ωt) − 4 cos(3ωt)
• f (t) = sin(ωt) + 3 sin(3ωt) + 5 sin(5ωt)
• f (t) = sin(ωt) + 2 cos(3ωt) + 3 sin(5ωt)

2 of 8
PHYS 381 - Winter term 2023

(c) Analyse the square wave with period T = 2π/ω and ω = 1. We define θ = ωt where the square
wave function can be written as: (0.5 points)
(
1 0≤θ≤π
f (θ) =
−1 π < θ ≤ 2π

• Plot the function f (θ) × θ, compute its Fourier coefficients and compare your output with
the analytic result:

ak = 0 k = 1, 3, 5, . . . (9)
 4 k = 1, 3, 5, . . .

bk = πk (10)
0 k = 2, 4, 6, . . .

To generate the square wave signal, use the function square from scipy.signal library.
Its documentation can be found in https://docs.scipy.org/doc/scipy/reference/
generated/scipy.signal.square.html.
• Plot the reconstructed function, i.e. use equation (1) and the coefficients you just calcu-
lated to reconstruct the original signal. Check how your signal is reconstructed using 10,
20, and 30 Fourier coefficients.

(d) Analyse the pulse train with period T = 2π/ω as: (0.5 points)
(
1 0 ≤ ωt ≤ ωτ
f (ωt) =
−1 ωτ < ωt ≤ 2π

in the same way as in item (c) (including the reconstruction of the signal). The analytic results,
setting ωτ = 2π/α where α is a predefined parameter of your choice that will introduce an
asymmetry to your signal (over a period), are:
2
a0 = −1 (11)
α  
2 2kπ
ak = sin k = 1, 2, 3, . . . (12)
kπ α
  
2 2kπ
bk = 1 − cos k = 1, 2, 3, . . . (13)
kπ α

(e) IMPORTANT: In order to be considered for the full marks assigned in items (b), (c), and
(d) above, discuss the results obtained in each of these items in your report.

(f) IMPORTANT: Attach ALL .ipynb files you generated for items (b), (c), and (d) above, in
addition to your report. (0.5 points)

3 of 8
PHYS 381 - Winter term 2023

2 Fourier transform for non-periodic functions


A non-periodic function f (t) may be expanded in terms of cosine and sine functions but, in this
case, the expansion is a Fourier integral over a continuous range of frequencies, instead of a sum
over a discrete set of frequencies. The Fourier integral may be viewed as the limit of a Fourier series
(1) in the limit T → ∞. The summation over n in equation (1) is replaced by an integration over
ω, Z ∞
f (t) = dω [a(ω) cos(ωt) + b(ω) sin(ωt)] (14)
0

and the equations (5), (6), and (7) are replaced by

1 ∞
Z
a(ω) = dt f (t) cos(ωt) (15)
π −∞
1 ∞
Z
b(ω) = dt f (t) sin(ωt) (16)
π −∞

It is frequently more convenient to use complex notation to express functions that carry cos(. . .)
and sin(. . .) elements because eiωt = cos(ωt) + i sin(ωt). We can then define
Z ∞
1
f (t) = √ dω F (ω) eiωt (17)
2π −∞
Z ∞
1
F (ω) = √ dt f (t) e−iωt (18)
2π −∞

where the function F (ω) is called the Fourier transform of f (t). If the signal function has the
dimensions of energy, for instance, then its Fourier transform has the dimensions of power, and its
magnitude |F (ω)| is a measure of the total power in the signal at frequency ω. |F (ω)| is given by
r
p
2 2
πp 2
|F (ω)| = Re{F (ω)} + Im{F (ω)} = a (ω) + b2 (ω) (19)
2

2.1 The Discrete Fourier Transform - DFT


In practice, the numerical solution of (18) will involve replacing the integration with a discrete
summation. The exact integrals are approximated by a Discrete Fourier transform (DFT) as defined
below. DFTs are useful for analysing physical amplitude-time or intensity-time data. In such cases,
it is not known whether or not the signal is periodic and, even if it is, its period is unknown.
Suppose we have a time-dependent physical signal represented by a function f (t) and we sample it
N times at intervals h from t = 0 to t = (N − 1)h. We can define a discrete timeline as tm = mh
with m = 0, 1, 2, . . . , N − 1. The function is approximated by the discrete set of values at these
instants, and time τ = N h becomes the period of the approximated function. We need τ to be the
longest time over which we are interested in the behaviour of f (t), and we assume

f (t) = f (t + τ ) i.e. f (tm ) = f (tm+N ) or in shorthand form, fm = fm+N (20)

4 of 8
PHYS 381 - Winter term 2023

In this case, the lowest frequency in the DFT will be ν1 = 1/τ = 1/(N h). This is the fundamental
frequency in the case where the function f (t) is periodic and τ = T . The frequency spectrum is
given by
n 1
νn = = n = nν1 for n = 1, 2, . . . , N. (21)
τ Nh
The DFT evaluates equations (17) and (18) as
N −1 N −1
1 X i 2πνn tm 1 X
fm = Fn e = Fn ei 2πmn/N (22)
N n=0 N n=0
N
X −1 N
X −1
−i 2πνn tm
Fn = fm e = fm e−i 2πmn/N (23)
m=0 m=0

Note that there exists an orthogonality relation for sums that leads to an identity when inserting fm
from (22) into (23) and reverse. Fn are generally complex numbers. Not all the Fourier components

Fn are independent of each other and one can show that FN/2−n = FN/2+n where Fn∗ is the complex
conjugate of Fn . The highest frequency component is thus FN/2−1 , corresponding to [see equation
(21)] a frequency of νmax = (N/2 − 1)/(N h) = 1/(2h) − 1/(N h) ≈ 1/(2h) for large N . This is
called the Nyquist frequency expressed as νNyquist . If the signal has a component with frequency
ν > νNyquist , there are less than two sample points per period. In this case, there will be one or
more frequencies less than νNyquist for which the amplitude equals the true amplitude at the sample
points, and these lower (incorrect) frequencies will appear in the calculated spectrum. This is known
as aliasing.
The power spectrum of the DFT [see equation (23)] is given by a plot of all the values Pn2 =
Re{Fn }2 +Im{Fn }2 as a function of n, where Re{Fn } is the real part of Fn and Im{Fn } its imaginary
part.

2.2 Exercises - DFT


Modify your Python script for the Fourier series exercise to calculate the DFT in the example below.
Use the following relations for Re{Fn } and Im{Fn }:
N −1  
X 2πmn
Re{Fn } = fm cos (24)
m=0
N
N −1  
X 2πmn
Im{Fn } = fm sin (25)
m=0
N

The original signal is then reconstructed via equation (22), resulting in


N −1     
1 X 2πmn 2πmn
fm = Re{Fn } cos + Im{Fn } sin (26)
N n=0 N N

Remember that the effective fundamental frequency, ν1 , to be used in each case is determined by
the total sampling time τ = T . It is recommended to make your program evaluate and print out

5 of 8
PHYS 381 - Winter term 2023

the sampling rate νs = 1/h as well as ν1 so that you can see how many Fourier components you
would expect to be able to reconstruct the signal accurately.

(a) Consider the function f (t) = sin(0.45πt): (1 point)

• Set N = 128 and h = 0.1 and plot the function for a total time τ = N h, together with the
points where it is sampled.
• Write a code that returns the Fourier components Re{Fn } and Im{Fn } using equations
(24) and (25) and plot them as a function of n. Plot in a separate figure the power
spectrum of the signal.
• You should find one dominant Fourier component νn = n/(N h). How does its value
compare with what you expect for the frequency from the given function f (t) above?
• From the Fourier components Re{Fn } and Im{Fn }, reconstruct the initial function f (t)
and plot it as a function of time tm = mh. Include f (t) calculated directly from the
sin(0.45πt) function in this figure to see how well the reconstruction works.
• Repeat the four steps above for the function f (t) = cos(6πt). Start with N = 32 and
h = 0.6. In the following, start decreasing the time spacing as h = 0.5, h = 0.4, and
h = 0.1 and keeping N = 32 fixed. What is happening with the reconstruction of your
signal? Is it improving the reconstruction?

(b) Radio and TV Transmission: Radio, television, and some other forms of communication trans-
mit information via electromagnetic waves. The various sources in these applications can be
transmitting simultaneously and in the same geographic region. But how is it that we can
“tune in” to a specific radio station, or television program for example, rather than hearing the
jumble of all the various transmissions put together? Luckily different transmissions operate at
different frequencies. Thus, even though all the signals are “jumbled” together in the time do-
main, they are distinct in the frequency domain. With some basic frequency domain processing
based on Fourier analysis, one can separate the signals and “tune in” to the frequency we wish.
You will now process a real sound data stored in the file pitch.txt. Modify your code to read
the content of the file which is in one-column format representing vibrations in the air over time
in seconds. Obtain the Fourier components and plot them as a function of n or as a function of
frequency (in Hz). Plot in a separate figure the power spectrum of the signal. Using the Fourier
components, reconstruct the original signal. Which frequencies can you identify in your power
spectrum? Can you ‘denoise’ the signal? To do that, reconstruct the signal using equation (26)
but impose a threshold of ϵ = 50 in which Fn = 0 only if |Fn | < ϵ. (1 point)

(c) IMPORTANT: In order to be considered for the full marks assigned in items (a), and (b)
above, discuss the results obtained in each of these items in your report.

(d) IMPORTANT: Attach ALL .ipynb files you generated for items (a), and (b) above, in
addition to your report. (0.5 points)

6 of 8
PHYS 381 - Winter term 2023

3 Report writing guide (5 points)


After generating your codes and results for the exercises above, you will summarize them in the
form of a written report. Please, follow the instructions provided in the report template located in
the sub-module of the assignment to write your report. All margins, font style/size, line spacing,
and page limits set on the template need to be respected. We will deduct a significant amount
of points from reports that do not align with the template provided or we will not be
able to assess the report, meaning we will have to deduct -5/10 points from the total
marks.

There is a docx file (generated in MS Word editor) available in the sub-module of the assignment
that can, in principle, be used and exported to other text editors. However, some of its pre-defined
macros may not be read correctly by other editors. Moreover, inserting text on the editable docx
file may shift or duplicate some of the original headers. Make sure your written content obeys the
format set in the read-only PHYS 381 Assignment 4 Template.pdf file. In case you have problems
with the docx file provided, a suggestion is to start a blank document in the text editor of your
choice and set the layout as below:

• Page size: 8.5” × 11” (letter);

• Margins: 2.54 cm (top, bottom, left, right);

• Font type and size: Times New Roman, 12 pt, non-italic, non-bold;

• Line spacing: between 1.0 and 1.15 pt;

• Figures must be numbered and contain captions. Axes must carry names and their quantities
must be expressed with the proper units whenever required. Do not insert too large figures
just to cover space. Choose figure sizes typically seen in textbooks and scientific manuscripts.

• Follow the exact page configuration and order as described in the report template, e.g., title
page on page 1, abstract on page 2, introduction on page 3, etc. The title page needs to
contain the exact same information as in the report template.

• NOTE FOR THIS ASSIGNMENT ONLY: Points will not be deducted if the section
‘Results and Analysis’ go over the page limit set in the report template. But this is valid
ONLY for the ‘Results and Analysis’ section; all other sections need to follow the template
requirements.

7 of 8
PHYS 381 - Winter term 2023

3.1 Submission
The due date for the submission of your report is depicted on the first page of the assignment. Once
you conclude the writing of your report in accordance with the template provided, generate a pdf
file and name it as report_assignment_4.pdf (since this is report #4). You will upload the pdf
file of your report plus your Notebook .ipynb files requested in the assignment to the Gradescope
platform. It is important that you log in to Gradescope.ca and not Gradescope.com.
You can upload multiple files to the Gradescope platform and you can resubmit your work until
the due date. We will test-run all your submitted codes to test for errors. We will also check if the
results/figures presented in your report match the output of your codes.

For Groups: If you worked in a group, remember to include your group members in Gradescope
after you submit all the files on behalf of the group. You will see an option “Add Group Member”
after you upload all files to Gradescope. Students can work in groups of up to 4 members.

Autograder option: Ignore the autograder results that may appear after you upload files to
Gradescope. There is no autograder configured for this assignment since everything will be evaluated
manually by the PHYS 381 Team.

Please, remember to add comments in all your submitted Notebook codes! Pure code
lines without explanatory comments or written Markdown cells will have reduced
marks.

Standard .py Python codes are not accepted in this submission. Work only with
Notebook environments. If any member of the group is experiencing problems with
their coding environment, please, contact the instructor immediately.

ONLY pdf (for the written report) AND .ipynb (for codes) FILES ARE ACCEPTED.

*****

8 of 8

You might also like