close all clear all clc Tp = 5; % period d = .5; % duty cycle om0 = 2*pi/Tp; % omega0 T = 0.001; % sampling spacing t = 0:T:Tp/2; s = square_wave(t,Tp,d); figure plot(t,s) grid axis([0 Tp/2 -.2 1.2]) xlabel('t') ylabel('s(t)') hold on for N = [5,10,20,50,100,200] tfs = zeros(size(t)); % truncated Fourier series for k = -N:N % cicle on coefficients ak = d*sinc(k*d); % Fourier coefficient tfs = tfs + ak*exp(1i*k*om0*t); end tfs = real(tfs); % prevent numerical errors plot(t,tfs) end legend('N=\infty','5','10','20','50','100','200') title('truncated Fourier series') % printing figure in png format set(gcf,'PaperUnits','inches','PaperPosition',[0 0 6 4.5]) print -dpng exercise1.png -r100 function s = square_wave(t,Tp,d) t1 = mod(t/Tp,1); s = rect(t1/d) + rect((t1-1)/d); end function s = rect(t) s = (abs(t)<.5)+.5*(abs(t)==.5); end