Shifted square wave Fourier series

779 Views Asked by At

I am trying to find Fourier series of shifted square wave. I found $a_n$ and $b_n $ coefficients but when I draw it by matlab, unfortunately I don't get the right graph.

here the coefficients:

$$ a_n = \frac{A}{n \cdot \pi}\left( 2 \cdot \sin(n \cdot p) \cdot \cos(n \cdot \pi) - 2 \cdot \sin(n \cdot \pi) \right) $$

$$ b_n = \frac{A}{\pi \cdot n}\left( 2 \cdot \cos(n \cdot p)-2 \cdot \cos(n \cdot \pi) \cdot \cos(n \cdot p) + \cos(n \cdot p) \right) $$

p: shifting angle.

Here the graph

Here the codes that I used to draw graph

A = 12;

p =pi/6;
t = 0:0.001:(2*pi+p);
fourier = 0;
for n=1:2:1000
    m = n;
    fourier_an = A*(2*sin(n*p)*cos(n*pi)-2*sin(n*p))*cos(n*t)/(n*pi);
    fourier_bn = A*(2*cos(m*p)-2*cos(m*pi)*cos(m*p)+cos(m*p))*sin(m*t)/(m*pi);
    fourier = fourier_an + fourier_bn + fourier;
  
end

plot(t,fourier);

Where did I mistake ?

2

There are 2 best solutions below

2
On BEST ANSWER

As confirmed by @Piko, the problem is fixed by correcting $b_n$.

fourier_bn = A*(2*cos(m*p)-2*cos(m*pi)*cos(m*p))*sin(m*t)/(m*pi);


Explanation

I spotted this because in the original Fourier coeffs, there is an asymmetry between the $a_n$ and $b_n$. As you can see, the length of the formula for $b_n$ is longer than that of $a_n$.

fourier_an = A*(2*sin(n*p)*cos(n*pi)-2*sin(n*p))*cos(n*t)/(n*pi);
fourier_bn = A*(2*cos(m*p)-2*cos(m*pi)*cos(m*p)+cos(m*p))*sin(m*t)/(m*pi);

This looks suspicious. Upon inspection, this asymmetry is caused by an extra $\cos np$ term in $b_n$. After removing it, we plot the graph in Octave to see what we get, and indeed we get something looks like a square wave.

4
On

The Fourier series for a shifted square wave with amplitude $A$, period $T$, and phase $p$ is as follows:

$$f(t)=\sum\limits_{n=1}^N (a(n)\ \cos(n t)+b(n)\ \sin(n t))\tag{1}$$

where

$$a(n)=\frac{2 A \sin ^2\left(\frac{\pi n}{2}\right) \sin \left(\frac{\pi n (2 p+T)}{T}\right)}{\pi n}\tag{2}$$

and

$$b(n)=-\frac{2 A \sin ^2\left(\frac{\pi n}{2}\right) \cos \left(\frac{\pi n (2 p+T)}{T}\right)}{\pi n}\tag{3}$$


The following plot illustrates formula (1) for $f(t)$ in orange overlaid on the reference function in blue where $A=12$, $T=2 \pi$, $p=\frac{\pi}{6}$, and formula (1) is evaluated using an upper evaluation limit of $N=16$.


Illustration of Formula (1) for f(t)

Figure 1: Illustration of Formula (1) for $f(t)$