1
$\begingroup$

I want to approximate below signal using fourier series on Matlab.

enter image description here

My code is below

close all
clear all
clc
a_0 = 0
t=-10:0.001:10
x_t = 2/5
T = 10;
w0 = 2*pi/T
numOfTerms = 100
plot(t, x_t);

for k=1:numOfTerms-1
   a_k = (1/(w0*i*k*T))*(  exp(-1*i*k*w0)-exp(-3*i*k*w0)+ exp(-7*i*k*w0)- exp(-9*i*k*w0))
   x_t = x_t + a_k * exp(i*k*w0*t)
   plot(t, x_t);
   title(['Number Of Terms = ',num2str(k+1)]);
   pause(0.1);
end

enter image description here

You see that min point of this signal above zero and max point below 1. How can I solve this problem? Why it gives such result?

$\endgroup$
3
$\begingroup$

The formula for the Fourier series is

$$x(t)=\sum_{k=-\infty}^{\infty}c_ke^{jk\omega_0 t}\tag{1}$$

You only use the non-negative indices $k$, but you must also use the negative ones. Since your signal $x(t)$ is real-valued, you can in fact compute everything using only the positive indices, but then you must take two times the real part:

$$x(t)=c_0+2\Re\left\{\sum_{k=1}^{\infty}c_ke^{jk\omega_0 t}\right\}\tag{2}$$

Note that you must add the DC term $c_0$ separately.

This is the corresponding Matlab code:

t=-10:0.001:10;
x_t = 0;
T = 10;
w0 = 2*pi/T;
numOfTerms = 100;


for k=1:numOfTerms-1
   a_k = (1/(w0*i*k*T))*(  exp(-1*i*k*w0)-exp(-3*i*k*w0)+ exp(-7*i*k*w0)- exp(-9*i*k*w0));
   x_t = x_t + a_k * exp(i*k*w0*t);
end

x_t = 2*real(x_t) + 2/5;

plot(t,x_t);

enter image description here

$\endgroup$
2
$\begingroup$

I have simulated on my computer, and I found that when I added the negative frequency terms, the result was correct.

$\endgroup$
  • $\begingroup$ Please make one answer out of the two answers that you gave (and delete the other one). Furthermore, note that the coefficients $a_k$ are correct. $\endgroup$ – Matt L. May 3 '15 at 9:58
  • $\begingroup$ OK! And your answer is perfect. $\endgroup$ – 楊嘯天 May 3 '15 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.