Frontmatter

If you are publishing this notebook on the web, you can set the parameters below to provide HTML metadata. This is useful for search engines and social media.

using Plots, Waveforms
2.6 s

Exercise

Calculate the first four Fourier coefficients (magnitude and phase) for this wave. Then construct a frequency plot of the magnitude components.

md"""
## Exercise

Calculate the first four Fourier coefficients (magnitude and phase) for this wave. Then construct a frequency plot of the magnitude components.

"""
149 μs
begin
t = 0:0.001:2
plot(t, wave.(t), label=false)
end
698 ms

The wave in the first period is defined as follows:

$$ x(t) = \begin{cases} -t, \quad t<0.5 \textrm{sec} \\ 0, \quad 0.5 \leq t \leq 1 \textrm{sec} \end{cases}$$

md"""
The wave in the first period is defined as follows:

```math
x(t) = \begin{cases} -t, \quad t<0.5 \textrm{sec} \\ 0, \quad 0.5 \leq t \leq 1 \textrm{sec} \end{cases}
```
"""
152 μs

Side note

md"""### Side note"""
109 μs

The function to generate the wave is given below:

md"""
The function to generate the wave is given below:
"""
116 μs
wave (generic function with 1 method)
wave(t) = mod(t, 1) < 0.5 ? -mod(t, 1) : 0
286 μs

And you can plot it by calling

 plot(t,wave.(t))
md"""
And you can plot it by calling

```julia
plot(t,wave.(t))

```
"""
114 μs
Click to see the Matlab version

t = linspace(0, 2, 100);
y = arrayfun(@wave, t);
plot(t, y)

function [y] = wave(t)
	c = mod(t, 1);
	if c < 1/2
    	y = -c;
	else
    	y = 0;
	end
end

Foldable("<b>Click to see the Matlab version</b>", md"""
```octave
t = linspace(0, 2, 100);
y = arrayfun(@wave, t);
plot(t, y)

function [y] = wave(t)
c = mod(t, 1);
if c < 1/2
y = -c;
else
y = 0;
end
end
```
""")
1.7 ms

Solution

Sine coefficient
md"""
### Solution


##### Sine coefficient
"""
139 μs

Find $a$'s and $b$'s and $a_0$. Then calculate the magnitude and phase.

$$b_k = \dfrac{2}{T} \int_0^T x(t) \sin(k \omega_0 t)dt = \dfrac{2}{1} \int_0^{0.5} -t \sin(2 \pi kt) dt $$

where we used $\omega_0 = 2\pi f$

md"""
Find $a$'s and $b$'s and $a_0$. Then calculate the magnitude and phase.
```math
b_k = \dfrac{2}{T} \int_0^T x(t) \sin(k \omega_0 t)dt = \dfrac{2}{1} \int_0^{0.5} -t \sin(2 \pi kt) dt
```
where we used $\omega_0 = 2\pi f$
"""
156 μs

Use integration by parts: $\int u \cdot dv = uv - \int v \cdot du$. Here $u = -t$ and $dv = \sin(2 \pi kt)$. We have

$$\begin{align*} v &= \dfrac{-\cos(2\pi kt)}{2\pi k} \\ \int_0^{0.5} u \cdot dv &= uv - \int_0^{0.5} v \cdot du \end{align*}$$

md"""
Use integration by parts: $\int u \cdot dv = uv - \int v \cdot du$.
Here $u = -t$ and $dv = \sin(2 \pi kt)$. We have
```math
\begin{align*}
v &= \dfrac{-\cos(2\pi kt)}{2\pi k} \\
\int_0^{0.5} u \cdot dv &= uv - \int_0^{0.5} v \cdot du
\end{align*}
```
"""
126 μs
Show answer

$$b_k = 2 \left[ \left. -t \dfrac{-\cos(2\pi kt)}{2\pi k} \right| _{0} ^{0.5} - \int_0^{0.5} \dfrac{\cos(2\pi kt)}{2\pi k} \right]$$

which simplifies to

$$b_k = \dfrac{1}{2\pi k}\cos(\pi k)$$

Foldable("Show answer", md"""
```math
b_k = 2 \left[ \left. -t \dfrac{-\cos(2\pi kt)}{2\pi k} \right| _{0} ^{0.5} - \int_0^{0.5} \dfrac{\cos(2\pi kt)}{2\pi k} \right]
```

which simplifies to
```math
b_k = \dfrac{1}{2\pi k}\cos(\pi k)
```
""")
804 μs
b (generic function with 1 method)
b(k) = cos(π*k)/(2*π*k)
249 μs
Click to show MATLAB

% This is part of the activity to turn in!

Foldable("Click to show MATLAB", md"""
```octave
% This is part of the activity to turn in!
```
""")
99.0 μs
Cosine coefficient

Similarly for $a$,

$$ a_k = \dfrac{2}{T} \int_0^T x(t) \cos(k\omega_0 t)dt = \dfrac{2}{1} \int_0^{0.5} -t \cos(2\pi kt) dt$$

md""" ##### Cosine coefficient

Similarly for $a$,

```math
a_k = \dfrac{2}{T} \int_0^T x(t) \cos(k\omega_0 t)dt = \dfrac{2}{1} \int_0^{0.5} -t \cos(2\pi kt) dt
```
"""
137 μs

Use integration by parts $\int u \cdot dv = uv - \int v \cdot du$. Here $u = -t$ and $dv = \cos(2 \pi kt)$. We have

$$\begin{align*} v &= \dfrac{\sin(2\pi kt)}{2\pi k} \\ \int_0^{0.5} u \cdot dv &= uv - \int_0^{0.5} v \cdot du \end{align*}$$

md"""
Use integration by parts $\int u \cdot dv = uv - \int v \cdot du$.
Here $u = -t$ and $dv = \cos(2 \pi kt)$. We have
```math
\begin{align*}
v &= \dfrac{\sin(2\pi kt)}{2\pi k} \\
\int_0^{0.5} u \cdot dv &= uv - \int_0^{0.5} v \cdot du
\end{align*}
```

"""
122 μs
Show answer

$$a_k = 2 \left[ \left. -t \dfrac{\sin(2\pi kt)}{2\pi k} \right| _{0} ^{0.5} - \int_0^{0.5} \dfrac{-\sin(2\pi kt)}{2\pi k} \right]$$

which simplifies to

$$a_k = \dfrac{-1}{2\pi^2 k^2}\left[\cos(\pi k)-1 \right]$$

Foldable("Show answer", md"""
```math
a_k = 2 \left[ \left. -t \dfrac{\sin(2\pi kt)}{2\pi k} \right| _{0} ^{0.5} - \int_0^{0.5} \dfrac{-\sin(2\pi kt)}{2\pi k} \right]
```

which simplifies to
```math
a_k = \dfrac{-1}{2\pi^2 k^2}\left[\cos(\pi k)-1 \right]
```
""")
131 μs
a (generic function with 1 method)
a(k) = -1/(2*(k*π)^2)*(cos(π*k)-1)
321 μs
Click to show MATLAB

% This is part of the activity to turn in!

Foldable("Click to show MATLAB", md"""
```octave
% This is part of the activity to turn in!
```
""")
88.5 μs
Constant coefficient

Now, find $a_0$

$$ C_0 = a_0 = \dfrac{2}{T} \int_0^T x(t) dt$$

md""" ##### Constant coefficient

Now, find $a_0$
```math

C_0 = a_0 = \dfrac{2}{T} \int_0^T x(t) dt
```
"""
129 μs
Loading...