% Recontruct step-by-step, by user prompt, the waveform of some Periodic % functions from their Fourier Series coefficients % %Enter the value of the string variable "waveform" below as follows: %sq--for a square wave %6p--for a 6-pulse square wave %rs--for a rectified sinewave %sw--for a sawtooth %tr--for a triangular wave % %Hit left mouse button to see the stages of reconstruction; Rght mouse to %stop close all; clc; clear all waveform='sq'; %Define time t=-1.5:0.005:1.5;%Plot time points tm=mod(t+1/2,1)-1/2;%normalized time points to the intervale [-1/2 1/2] switch lower(waveform) case 'sq' f=-1*(((-0.5<=tm)&(tm<=-0.25))|((0.25<=tm)&(tm<=0.5)))... +1*((-0.25<=tm)&(tm<=0.25)); r=[0:1:7]; n=2*r+1;%Harmonic number A=(4/pi)*(1./(((-1).^(3*r+2)).*n));%Fourier cosine term amplitudes B(r+1)=0;%Fourier sine term amplitudes case '6p' f=-1*( ((-0.5<=tm)&(tm<=-1/3))|((1/3<=tm)&(tm<=0.5)) )+1*((-1/6<=tm)&(tm<=1/6)); r=0:1:5; n=[6*r+1 6*r(2:end)-1];n=sort(n);%Harmonic order A=((n-6*round(n/6))).*(2*sqrt(3)/pi).*(1./n);%Fourier cosine term amplitude B(2*r+1)=0;%Fourier sine term amplitude case 'rs' f=abs(sin(2*pi*t)); r=[0:1:7]; n=2*r;%Harmonic number A=-(4/pi)*1./((n-1).*(n+1));%%Fourier cosine term amplitudes A(1)=A(1)/2;%Average value B(r+1)=0;%Fourier sine term amplitudes case 'sw' f=2*tm; r=0:1:7; n=r+1;%Harmonic number A(r+1)=0;%Fourier term cosine amplitude B=((-1).^r).*(2/pi)./n;%Fourier sine term amplitude case 'tr' f=((-0.5<=tm)&(tm<=0)).*(tm+0.25)-1*((0