Plotting Fourier Series Coefficients

6 views (last 30 days)
Hi, I am trying to find Fourier coefficients of two signals: x(t) = u(t) - u(t-1) [0<t<2] and y(t) = u(t) - u(t-0.5) [0<t<1]. I have found the Fourier series coefficients through this code:
syms t k L n
evalin(symengine,'assume(k,Type::Integer)');
a = @(f,t,k,L) int(f*cos(k*pi*t/L)/L,t,-L,L);
b = @(f,t,k,L) int(f*sin(k*pi*t/L)/L,t,-L,L);
fs = @(f,t,n,L) a(f,t,0,L)/2 + ...
symsum(a(f,t,k,L)*cos(k*pi*t/L) + b(f,t,k,L)*sin(k*pi*t/L),k,1,n);
f = heaviside(t) - heaviside(t-1);
f1 = heaviside(t) - heaviside(t-0.5);
>> [B,how]=simple(b(f,t,k,1)); B
B =
(2*sin((pi*k)/2)^2)/(pi*k)
>> [B,how]=simple(b(f1,t,k,1)); B
B =
(2*sin((pi*k)/4)^2)/(pi*k)
However, I am unsure how to plot the magnitude line spectra of these two in the same figure from 0 to 40*pi rad/sec. I have tried ez plot but keep getting errors. I am new to this and not sure what to do. Thanks!

Accepted Answer

Walter Roberson
Walter Roberson on 4 Jun 2012
bfun = matlabFunction(B, k);
kspan = linspace(0,40,100);
plot(kspan, bfun(kspan));
  2 Comments
goal24
goal24 on 4 Jun 2012
When I run this function right after my previous code, i receive an error:
Error using deal (line 38)
The number of outputs should match the number of inputs.
Error in
sym/matlabFunction>makeFhandle/@(k)deal((sin(pi.*k.*(1.0./4.0)).^2.*2.0)./(pi.*k),k)
Any suggestions how to fix this error?
goal24
goal24 on 4 Jun 2012
Oh nvm I got it. Thank You!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!