Tammy Logger
Tammy Logger

Reputation: 61

Fourier Series MATLAB plotting

I am trying to plot a decomposed triangle wave with various number of harmonics and I am stuck

I was able to calculate the coefficient values and they are in vector form but I can't transform this into my output signal and plot it. I have copied what I have done so far.

I am just getting quite confused and would appreciate some direction.

syms t k w_0;

w_0=(2*pi);
k=[1:4]

a_k=(2/1)*(int(2*t*cos(k*w_0*t),t,0,.5)+(int((2-2*t)*cos(k*w_0*t),t,.5,1)))
b_k=(2/1)*(int(2*t*sin(k*w_0*t),t,0,.5)+(int((2-2*t)*sin(k*w_0*t),t,.5,1)))
a_0=(int(2*t,t,0,.5)+int(2-2*t,t,.5,1))


t=[0:.1:10];

%x=a_0+(symsum((a_k.*cos(w_0*k*t)+b_k.*sin(w_0*k*t)),1,2))

for i=[1:4]
    x=a_0+a_k(i)*cos(w_0*i.*t)+b_k(i)*sin(w_0*i.*t)
end

plot(t,x)

Upvotes: 0

Views: 288

Answers (1)

eigenchris
eigenchris

Reputation: 5821

You seem to be doing everything right, but you are not summing your wave components together. You are just assigning them one after another to x, overwriting the last.

Try

x = 0;
for i=[1:4]
    x = x + a_0+a_k(i)*cos(w_0*i.*t)+b_k(i)*sin(w_0*i.*t)
end

Upvotes: 1

Related Questions