0

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:(2pi+p); fourier = 0; for n=1:2:1000 m = n; fourier_an = A(2sin(np)cos(npi)-2sin(np))cos(nt)/(npi); fourier_bn = A(2cos(mp)-2cos(mpi)cos(mp)+cos(mp))sin(mt)/(mpi); fourier = fourier_an + fourier_bn + fourier;

end

plot(t,fourier);

Where did I mistake ?

Piko
  • 27
  • 1
    The $\cos (np)$ term in $b_n$ looks suspicious. Removing it gives us something looks like a square wave. Is it what went wrong? $b_n$ after correction: fourier_bn = A*(2*cos(m*p)-2*cos(m*pi)*cos(m*p))*sin(m*t)/(m*pi); – 光復香港 時代革命 Free Hong Kong Apr 17 '21 at 15:10
  • @光復香港時代革命FreeHongKong Thank you so much. Yes your answer is right, after I replace it, I got the right one. How could you see it ? Why did you think mistake was it ? – Piko Apr 18 '21 at 07:42
  • 1
    Your definition of $a_n$ doesn't match your implementation of fourier_an ($\sin(n\pi)$ vs $\sin(np)$). – Steven Clark Apr 18 '21 at 15:17

2 Answers2

1

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)$

Steven Clark
  • 7,363
  • Thank you so much for your answer. That is also right but what is the difference between mine and yours ?Did you use trigonometry identity ? Because mine is also right after replace what @光復香港 時代革命 Free Hong Kong said. – Piko Apr 18 '21 at 07:44
  • Also why there are noise at the sides while N = too big ? – Piko Apr 18 '21 at 07:45
  • 1
    @Pico The amplitude $A$ in my formulas is peak-to-peak (so you need to multiply my coefficients by 2 when compared to yours) and I didn't include the $\cos(n t)$ and $\sin(n t)$ functions in my coefficients. My coefficients are more general in that they support periods other than $T=2\pi$, but for the period $T=2\pi$ they're equivalent to your coefficients (when accounting for the differences in the previous sentence). The noise that you describe is known as the Gibbs phenomenon (see https://en.wikipedia.org/wiki/Gibbs_phenomenon). – Steven Clark Apr 18 '21 at 15:40
  • 1
    @Pico For the period $T=2 \pi$ our coefficients both simplify to $a(n)=\frac{2 A \left((-1)^n-1\right) \sin(n p)}{n \pi}$ and $b(n)=-\frac{2 A \left((-1)^n-1\right) \cos(n p)}{n \pi}$ (assuming mine is multiplied by 2 and omitting the $\cos(n t)$ and $sin(n t)$ from yours). – Steven Clark Apr 18 '21 at 16:19
1

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.