close all clear all clc Tp = 3; % period om0 = 2*pi/Tp; % omega0 T = 0.001; % sampling spacing N = 100; % number of Fourier coefficients t = 0:T:Tp; s = signal(t); figure plot(t,s) grid xlabel('t') ylabel('s(t)') hold on % numerical coefficients for M = [1e3,1e4] tfs = zeros(size(t)); % truncated Fourier series nT = (0:M-1)*Tp/M; snT = signal(nT); for k = -N:N % cicle on coefficients bk = sum(snT.*exp(-1i*k*om0*nT))/M; tfs = tfs + bk*exp(1i*k*om0*t); end tfs = real(tfs); % prevent numerical errors plot(t,tfs) end legend('signal','M=1000','M=10000') title('approx truncated Fourier series') % printing figure in png format set(gcf,'PaperUnits','inches','PaperPosition',[0 0 6 4.5]) print -dpng homework2.png -r100 function s = signal(t) t1 = mod(t,3); s = t1.*(t1<1) + (t1>=1).*(t1<2); end