0% found this document useful (0 votes)
10 views

MathLab Computational Tools Problem Set 3 Solution PDF

Uploaded by

Sukanya Paul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

MathLab Computational Tools Problem Set 3 Solution PDF

Uploaded by

Sukanya Paul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

MathLab: Problem Set 3

Solutions
Mathematics Club IITM
Navin, KK, Achintya
June 2024

• The solutions to the problems in Problem Set 3 can be found in this solution sheet.

• Feel free to reach out to us for doubts! Contact information of the problem-set creators:
– Achintya Raghavan - +91 96068 52240
– Karthik Kashyap - +91 80739 78167
– Navin Kumar - +91 90287 70420

Section 1 Section 2 Section 3 Appendix

§1 The Basics of Manipulation


This section contains simple conceptual-level questions which test your understanding of the topic
and prepare you for the upcoming sections. Show your working in each question.

1. ex and a constant were walking down the street one day. While ex continued walking, oh dear
had the constant run away. What operator in the path would cause the constant much dismay?
Find the solutions y(x) to the following differential equations given the boundary conditions:
d2 y dy
a) 2 +5 − 3y = 0
dx2 dx
[ y(2) = e−2 + e12 and y(16) = e−30 + e5 ]

Final Expected Answer: y(6)

Solution:

Using the learnt method, we substitute esx . This gives us:

d2 esx desx
2 2
+5 − 3esx = 0
dx dx

=⇒ (2s2 + 5s − 3)esx = 0

=⇒ (2s − 1)(s + 3) = 0

1
=⇒ s = , −3
2

1
Mathematics Club IITM MathLab: Problem Set 3
Navin, KK, Achintya — June 2024 Solutions

=⇒ y = Ae 2 x + Be−3x
1

Substituting initial conditions and solving the system of 2 equations, we get:

A = e−3 , B = e18

=⇒ y = e 2 x−3 + e−3x+18
1

∴ y(6) = 2

d2 y dy √ ix
b) 2 + 5 − 3y = 15 2e
dx2 dx
[ Assume only the forced part of the solution as discussed in the presentation i.e. system
is at rest in the beginning ]

Final Expected Answer: The magnitude of the solution (Since the solution will be
complex)

Solution:


Since we already have an exponential driving force f (x) = 15 2eix , we assume the
solution is of the form Aeix .

d2 Aeix dAeix √
=⇒ 2 2
+ 5 − 3Aeix = 15 2eix
dx dx

=⇒ Aeix (2(i)2 + 5i − 3) = 15 2eix


=⇒ A(−5 + 5i) = 15 2

15 2
=⇒ A =
−5 + 5i

3 2
=⇒ A =
i−1

3 2
∴ |A| = √ = 3
2


d2 y dy √
c)
kix
X
2
− 2 − 5y = 25 13k 2 e 2
dx dx
k=−∞

[ Assume only the forced part of the solution as discussed in the presentation i.e. system
is at rest in the beginning ]

Final Expected Answer: The magnitude of the coefficient of the k = 2 term in the
solution series.

Solution:

Applying the usual method of solving a LCCDE with the driving force f (x) being a

2
Mathematics Club IITM MathLab: Problem Set 3
Navin, KK, Achintya — June 2024 Solutions


Fourier series, we assume the solution is of the form .
kix
X
Ak e 2

k=−∞
∞ ∞ ∞ ∞
d2 X d X √
=⇒
kix kix kix kix
X X
2
A k e 2 − 2 A k e 2 − 5 A k e 2 = 25 13k 2 e 2
dx dx
k=−∞ k=−∞ k=−∞ k=−∞

∞ ∞
kix kix
!
d2 e 2 de 2 √
=⇒
kix kix
X X
Ak 2
−2 − 5e 2 = 25 13k 2 e 2
dx dx
k=−∞ k=−∞

∞ ∞
2 !


ki ki
=⇒
kix kix
X X
Ak +2 +5 e 2 = 25 13k 2 e 2
2 2
k=−∞ k=−∞

∞ ∞
k2 √
 
ki
=⇒
kix kix
X X
−Ak +5+2 e 2 = 25 13k 2 e 2
4 2
k=−∞ k=−∞

Using the fact that exponentials of different frequencies are linearly independent,
we equate coefficients to get:

k2 √
 
ki
− Ak +5+2 = 25 13k 2
4 2


−25 13 k 2
=⇒ Ak = k2 ki
4 +5+2 2


−100 13
∴ A2 =
6 + 2i


50 13
=⇒ |A2 | = √
10

2. Why did the function f go see a Fourier therapist? Because it wanted to break down its complex
personality into a series of sines and cosines!
Consider the function:
f (x) = x, −π ≤ x < π

Here f has a period of 2π, that is f (x + 2π) = f (x). Find the Fourier series corresponding to
f (x).

A tiny plot of the periodic function f (x)

Final Expected Answer: If the coefficients of the series are an , bn , then find the value of
a2 + b2 .

3
Mathematics Club IITM MathLab: Problem Set 3
Navin, KK, Achintya — June 2024 Solutions

Solution:

We make an observation that the function f (x) = x is an odd function. Thus, by applying
the properties of Fourier series, we find that the Fourier Series corresponding to this
function is a sine series and conclude that:

an = 0 ∀ n
Z π
1
To find bn , we can use: bn = f (x) sin(nx) dx
π −π
Z π
1
=⇒ bn = x sin(nx) dx
π −π

x cos(nx)
π !
 Z π
1 1
=⇒ bn = − + cos(nx) dx
π n −π n −π

−π cos(nπ) + (−π cos(−nπ)) 1 sin(nx)


   π !
1
=⇒ bn = +
π n n n −π

Since sin(nπ) is always 0 we get

2
bn = − cos(nπ)
n

∴ a2 = 0 & b2 = −1

Hence, we get the final answer a2 + b2 as −1.

4
Mathematics Club IITM MathLab: Problem Set 3
Navin, KK, Achintya — June 2024 Solutions

§2 The Hunt for Harmonics


§2.1 A janky spring
You are expected to use the Fourier series to solve this problem. Doing so requires that you have
knowledge of both the exponential and trigonometric series. However, in order to help you out, we
have provided the relevant conversion formulae to convert a given Fourier trigonometric series to
an exponential series and vice versa in the appendix. We highly encourage you work them out for
yourselves.

Achintya remembers learning about an interesting function called the ramp function r(t) in his
signal processing class. He remembers his teacher defining it as:

t t≥0
r(t) =
0 otherwise

On his class quiz, he comes across this differential equation when he attempts to model an old,
rusty and stiff spring that is being slowly but steadily pulled by an insanely strong ant. However
since he missed half of his math classes, he has no idea how to solve it. Can you help him?

d3 x d2 x dx
5 + 3 +6 + 2x = rp (t)
dt3 dt2 dt

Assume rp (t) = r(t) when −π < t ≤ π and is periodic with time period 2π. Also solve for only the
particular solution (this is what we discussed in the session).

The insanely strong ant

Final Expected Answer: The solution to the differential equation given above using Fourier
series methods.

Solution:

We begin by decomposing the given periodic ramp function into a trigonometric Fourier series.

2π 2π
ω= = =1
t 2π

Thus,

Zπ Zπ
1 1 π
a0 = rp (t)dt = tdt =
π π 2
−π 0

5
Mathematics Club IITM MathLab: Problem Set 3
Navin, KK, Achintya — June 2024 Solutions

Zπ Zπ k
!
1 1 1 (−1) − 1
ak = rp (t) cos(kt)dt = t cos(kt)dt =
π π π k2
−π 0

Zπ Zπ k+1
1 1 (−1)
bk = rp (t) sin(kt)dt = t sin(kt)dt =
π π k
−π 0

∞ ∞
a0 X
ak cos(kt) + bk sin(kt)
X
∴ rp (t) = +
2
k=1 k=1

∞ ∞
!
k k+1
π 1 (−1) − 1 (−1)
=⇒ rp (t) = cos(kt) + sin(kt)
X X
+
4 π k2 k
k=1 k=1

Rearranging the formulae given in the Appendix, we get:


Ak = ak − ibk & A−k = ak + ibk ∀k>0

Substituting our values:


!
k k
1 iπ (−1) (−1) − 1
Ak = + ∀ k 6= 0
2π k k2


X
∴ rp (t) = A0 + Ak eikt
k=−∞
k6=0


!
k k
π 1 iπ (−1) (−1) − 1
=⇒ rp (t) = +
X
+ eikt
4 2π k k2
k=−∞
k6=0

The differential equation now reduces to the standard LCCDE with an driving force in the
form of a Fourier Series.


d3 x d2 x dx X
+ 3 + 6 + 2x = Ak eikt
dt3 dt2 dt
k=−∞


As usual, we assume the solution is some series Bk eikt . Substituting in the above
X

k=−∞
differential equation and solving, we get:

∞ 
X  ∞
X
3 2
5 (ik) + 3 (ik) + 6ik + 2 Bk eikt = Ak eikt
k=−∞ k=−∞

Using our knowledge of Linear Independence, we get:


 
3 2
5 (ik) + 3 (ik) + 6ik + 2 Bk = Ak

6
Mathematics Club IITM MathLab: Problem Set 3
Navin, KK, Achintya — June 2024 Solutions

Ak
=⇒ Bk =
−5ik 3 − 3k 2 + 6ik + 2

!
k k

1 jπ (−1) (−1) − 1
k 6= 0


Ak

 +
Ak = 2π k k2
X
∴ x(t) = eikt
−5ik 3 − 3k 2 + 6ik + 2
π

k=−∞ 
k=0
4

Bonus Question 1: A recovering Ant [For those with 7/10 attendance]:

Consider the function f (t) = |t| when −π < t ≤ π with the same period as the above question
(T = 2π). Can you relate it somehow to the ramp function? Now use that knowledge to solve the
above question but with the driving force replaced by f (t) (with the same period) instead of the
ramp function.

NOTE: For those who have already met the attendance criteria, there is absolutely NO need to
submit this bonus question. But I still ask you to try this out. Including it in your solution will
make my day.

Solution:

The function f (t) can be represented as rp (t) + rp (−t).

Thus,
f (t) = rp (t) + rp (−t)

∞ ∞
! !
k k k k
π X 1 iπ (−1) (−1) − 1 π X 1 iπ (−1) (−1) − 1
= + + e ikt
+ + + e−ikt
4 2π k k2 4 2π k k2
k=−∞ k=−∞
k6=0 k6=0

Replacing k by −k for the second term, we get:


! ! !
k k (−k) (−k)
π X 1 iπ (−1) (−1) − 1 1 iπ (−1) (−1) −1
= + + eikt + + eikt
2 2π k k2 2π −k (−k)2
k=−∞
k6=0


! ! !
k k k k
π X 1 iπ (−1) (−1) − 1 1 iπ (−1) (−1) − 1
= + + eikt + + eikt
2 2π k k2 2π −k k2
k=−∞
k6=0


! !
k k k k
π X 1 iπ (−1) (−1) − 1 iπ (−1) (−1) − 1 ikt
= + + + + e
2 2π k k2 −k k2
k=−∞
k6=0


!
k k
π X 1 (−1) − 1 (−1) − 1
= + + eikt
2 2π k2 k2
k=−∞
k6=0

7
Mathematics Club IITM MathLab: Problem Set 3
Navin, KK, Achintya — June 2024 Solutions


!
k
π X 1 (−1) − 1
= + eikt
2 π k2
k=−∞
k6=0

!
k

1 (−1) − 1
k 6= 0



∴ Ak = π k2
π


k=0
2

The differential equation now reduces to the standard LCCDE with an driving force in the
form of a Fourier Series.


d3 x d2 x dx X
+ 3 + 6 + 2x = Ak eikt
dt3 dt2 dt
k=−∞


As usual, we assume the solution is some series Bk eikt . Substituting in the above
X

k=−∞
differential equation and solving, we get:

∞ 
X  ∞
X
3 2
5 (ik) + 3 (ik) + 6ik + 2 Bk eikt = Ak eikt
k=−∞ k=−∞

Using our knowledge of Linear Independence, we get:


 
3 2
5 (ik) + 3 (ik) + 6ik + 2 Bk = Ak

Ak
=⇒ Bk =
−5ik 3 − 3k 2 + 6ik + 2

!
k

1 (−1) − 1
k 6= 0



Ak 
k2
Ak = π
X
∴ x(t) = eikt
−5ik 3 − 3k 2 + 6ik + 2
π

k=−∞ 
k=0
2

8
Mathematics Club IITM MathLab: Problem Set 3
Navin, KK, Achintya — June 2024 Solutions

§2.2 Hmm, integrals


The integral 1
is something that you would have encountered a lot in your high school
R
1+x2 dx
calculus and some of you might even scream tan in verse ex plus sea (yes, live with it) the moment
that you see this fairly innocent integral.

But if I were to task you to find 1


you would be walking into integration-by-parts
R
(1+x2 )2 dx
and partial-fractions war-zone territory. Let us not ponder over that mess now!

A spooky figure made using 1


1+x2 and 1
(1+x2 )2

If I were to simplify things a bit, and ask you to find the value of the definite integral
Z ∞
1
dx
−∞ (1 + x2 )2

what would your approach be? There is a really elegant way to compute this integral using the
concepts that were taught in the third session. Try to recall them and compute the value of this
definite integral.

Hint: Think of a function whose Fourier transform is 1


1+x2 or in other terms finding the
inverse Fourier transform of 1
1+x2 might be helpful to you

Final Expected Answer: The value of the definite integral computed using
R∞ 1
−∞ (1+x2 )2
dx
the concepts taught in the third session.

Solution:

The Fourier Transform of the function e−|t| is 1+ω 2 .


2
Using Parseval’s Theorem along with this
observation gives us the equation:
Z ∞ 2 Z ∞  2

−|t| 1 2
e dt = dω
−∞ 2π −∞ 1 + ω2

Z ∞ Z ∞ Z ∞
4
=⇒ dω = 2π × e−2|t| dt = 4π × e−2t dt
−∞ (1 + ω 2 )2 −∞ 0

Z ∞ Z ∞
1 1 π
=⇒ dω = π × e−2t dt = π × =
−∞ (1 + ω 2 )2 0 2 2

Z ∞
1 π
Thus, we have computed the value of the required integral; 2 2
dx =
−∞ (1 + x ) 2

9
Mathematics Club IITM MathLab: Problem Set 3
Navin, KK, Achintya — June 2024 Solutions

§3 A Tale of Musical Mismanagement


Welcome to the clumsy life of Navin!

This morning Navin was assigned the work of recording an orchestral performance that was being
performed by the coordinators of the Mathematics Club. He was asked to record each instrument
in a different channel, so that the audio can be equalized easily later on by our Music Directors -
KK and Achintya. Unfortunately, Navin being the clumsy person that he is accidentally ended up
recording all of the instruments as a single audio file.

The Music directors want to tone down (decrease the volume of) the snare drums by 25%. The
snare drums used by the Mathematics Club produce sounds in the frequency range of 150Hz
to 250Hz. Assume that no other musical instruments produce sounds in this audio range.
Luckily, he has you to help him! Navin hoping that you listened to his presentation attentively is
asking you to write a program in MATLAB or GNU Octave which fulfills the directors’ request and
saves his job!

Final Expected Submission: A script written in MATLAB or GNU Octave that performs the
following:

• Reads an audio file named "i_hope_i_don't_lose_my_job.wav" using the function audioread


as an array along with the sample-rate of the audio

• Uses Fourier Transform methods to perform the request of reducing the volume of the drums
by 25% in the audio data

• Writes the newly generated audio data using the function audiowrite to an audio file named
"thankyou_for_saving_my_job.wav"

Note:

1. The functions audioread, audiowrite have been documented here: 1, 2, 3.


Feel free to reach out to us in case you don’t understand how these functions work!

2. If you are submitting your code via a Google Drive / GitHub link make sure to enable access
for all, so that we will be able to access your submission.

10
Mathematics Club IITM MathLab: Problem Set 3
Navin, KK, Achintya — June 2024 Solutions

Solution:

The GNU Octave / MATLAB code is given below. This code also accounts for multiple channels
being present in the audio file.

clc;
clear;

% Parameters for our filter, you can play around with them
f1 = 150;
f2 = 250;
atten = 0.75;

% Read the audio file


[inputAudio, fs] = audioread('i_hope_i_dont_lose_my_job.wav');

% Get the number of channels in it


[numSamples, numChannels] = size(inputAudio);

% Run through each channel in the audio file and perform Fourier
,→ Transform
for i = 1:numChannels
audioFT(:, i) = fft(inputAudio(:, i));
end

% Calculate the list of frequencies generated by the Fourier Transform


frequencies = (0:numSamples-1)*(fs/numSamples);

% Find indices corresponding to the 150Hz to 250Hz range


idx = (frequencies >= f1 & frequencies <= f2) | (frequencies >= (fs - f2)
,→ & frequencies <= (fs - f1));

% Reduce the corresponding frequencies by 25%


audioFT(idx, :, :) = audioFT(idx, :, :) * atten;

% Perform inverse Fourier Transform


for i = 1:numChannels
modifiedAudio(:, i) = ifft(audioFT(:, i));
end

% Ensure the audio is real to account for computational errors


modifiedAudio = real(modifiedAudio);

% Write the modified audio to a new file


audiowrite('thankyou_for_saving_my_job.wav', modifiedAudio, fs);

11
Mathematics Club IITM MathLab: Problem Set 3
Navin, KK, Achintya — June 2024 Solutions

If you have not accounted for multiple channels, your code might look like this:

clc;
clear;

% Parameters for our filter, you can play around with them
f1 = 150;
f2 = 250;
atten = 0.75;

% Read the audio file


[inputAudio, fs] = audioread('i_hope_i_dont_lose_my_job.wav');
numSamples = length(inputAudio);

% Perform Fourier Transform on the read audio file


audioFT = fft(inputAudio);

% Calculate the list of frequencies generated by the Fourier Transform


frequencies = (0:numSamples-1)*(fs/numSamples);

% Find indices corresponding to the 150Hz to 250Hz range


idx = (frequencies >= f1 & frequencies <= f2) | (frequencies >= (fs - f2)
,→ & frequencies <= (fs - f1));

% Reduce the corresponding frequencies by 25%


audioFT(idx, :) = audioFT(idx, :) * atten;

% Perform inverse Fourier Transform


modifiedAudio = ifft(audioFT);

% Ensure the audio is real to account for computational errors


modifiedAudio = real(modifiedAudio);

% Write the modified audio to a new file


audiowrite('thankyou_for_saving_my_job.wav', modifiedAudio, fs);

An alternative approach would be to use for loops instead of using the smart indexing features
provided in GNU Octave / MATLAB to filter the frequencies from 150Hz to 250Hz. You can also
use fftshift to reorder the frequencies if you wish to.

Your code doesn’t need to be exactly the same as the solution given here. As long
as your code has the same functionality as what is demanded by this problem you
will get a good grade.

12
Mathematics Club IITM MathLab: Problem Set 3
Navin, KK, Achintya — June 2024 Solutions

Bonus Question 2: Thinking in the Time Domain [For those with 7/10 attendance]:

NOTE: For those who have already met the attendance criteria, you DO NOT need to
submit this! You will be eligible for the certificate even if you don’t attempt this question. This is
just a bonus question. I will be very happy if you attempt this question, and that is a good thing!

Can you think of a way to perform the same task of changing the contribution of some
frequencies in a given function of time f (t) without analysing the function in the frequency-domain
using a Fourier Transform, instead working entirely in the time-domain. You can use the same
example as the above question or use any example that you like.

Explain your algorithm.

Solution:

Let X(ω) be the Fourier Transform of your time-domain signal x(t) and the filter H (attenuation
of certain frequencies) that you wish apply be represented in the frequency-domain by H(ω).
Let h(t) be the inverse Fourier Transform of H(ω), that is the h(t) is the time-domain
representation of the filter.
You might have noticed that when you are applying the filter H to x(t), what you are doing is:
computing X(ω), multiplying it with H(ω) and then computing the inverse Fourier Transform
of X(ω)H(ω) to obtain xnew (t). Let’s try to perform this for a general x(t).
Z +∞
1
xnew (t) = eiωt X(ω)H(ω)dω
2π −∞

Writing X(ω) in terms of x(t) using the expression for Fourier Transform, we get
Z +∞ Z +∞  Z +∞ Z +∞
iωt −iωτ
2πxnew (t) = e e x(τ )dτ H(ω)dω = eiω(t−τ ) x(τ )H(ω)dτ dω
−∞ −∞ −∞ −∞

Now we change the order of the integration using Fubini’s theorem from dτ dω to dωdτ . Note
that we are assuming here that the order of the integration can be reversed. This can be done
as long as the inner integral exists and the functions inside are integrable, which is indeed true
since we have assumed beforehand that X(ω) and every other quantity involved exist.
Z +∞ Z +∞ Z +∞ Z +∞ 
iω(t−τ ) iωt −iωτ
2πxnew (t) = e x(τ )H(ω)dωdτ = x(τ ) e H(ω)e dω dτ
−∞ −∞ −∞ −∞

Z +∞  Z +∞ 
1
=⇒ xnew (t) = x(τ ) eiωt H(ω)e−iωτ dω dτ
−∞ 2π −∞

The expression H(ω)e is simply the Fourier Transform of h(t − τ ) and the expression
−iωτ

)dω is nothing but the inverse Fourier Transform of H(ω)e−iωτ . Using


1
R +∞ iωt −iωτ
2π −∞ e (H(ω)e
these observations, Z +∞
xnew (t) = x(τ )h(t − τ )dτ = (x ∗ h)(t)
−∞

In the expression obtained above, we have shown the can result of the filtering process be
obtained directly by convolving the original signal x(t) with the time-domain representation of
the required filter h(t). The process of working in the frequency domain has been completely
eliminated.

13
Mathematics Club IITM MathLab: Problem Set 3
Navin, KK, Achintya — June 2024 Solutions

Bonus Question 3: A more sophisticated Filter [For those with 7/10 attendance]:

NOTE: For those who have already met the attendance criteria, you DO NOT need to
submit this! You will be eligible for the certificate even if you don’t attempt this question. This is
just a bonus question. I will be very happy if you attempt this question, and that is a good thing!

Repeat Problem 3 (A Tale of Musical Mismanagement) but instead of toning down the
amplitude of all frequencies in 150Hz to 250Hz by 25%, you have to tone down the frequencies:

• 150Hz to 175Hz by 20%,

• frequencies 175Hz to 225Hz by 25%,

• and frequencies 225Hz to 250Hz by 15%.

Follow the same instructions as given in Problem 3.

Solution:

The modifications to GNU Octave / MATLAB code in Problem 3 in order to make the filtering
more sophisticated is given here. The variations in the code mentioned in the solution to
Problem 3 apply here as well. You can work with mono or stereo audio files, use fftshift
or not, and so on. The code snippet here shows only the modifications to the filtering
process.

...

% Perform Fourier Transform on the read audio file


audioFT = fft(inputAudio);

% Parameters for our filter, you can play around with them
filterFreqs = [150, 175, 225, 250]
filterAttens = [0.80, 0.75, 0.85]

% Apply the filter to the given frequency ranges


for i = 1:length(filterAttens)
f1 = filterFreqs(i);
f2 = filterFreqs(i+1);

% Find indices corresponding to each frequency range


idx = (frequencies >= f1 & frequencies <= f2) | (frequencies >= (fs -
,→ f2) & frequencies <= (fs - f1));

% Multiply the corresponding frequencies by their attenuation factor


audioFT(idx, :) = audioFT(idx, :) * filterAttens(i);
end

...

14
Mathematics Club IITM MathLab: Problem Set 3
Navin, KK, Achintya — June 2024 Solutions

Appendix

Let the exponential series be of the form Ak eikω0 t and the trigonometric series be of the form
X

k=−∞
∞ ∞
a0 X
bk sin(ω0 t). Then, it can be shown that:
X
+ ak cos(ω0 t) +
2
k=1 k=1

• ak = Ak + A−k

• bk = i(Ak − A−k )

Note that k ∈ Z, k ≥ 0 or put simply k ranges from 0 (inclusive) to ∞ in integers.

15

You might also like