%reconstruct.m  
%m-file to generate various waveforms, does an fft to find the Fourier
%amplitudes, then does an inverse fft to reconstruct the waveform, using
%using just those frequency components specified by the user.
%calls program triang.m. (cos.m,square.m, and sawtooth.m are matlab intrinsic
%functions)

f1=.1;
T=10;
N=128;
m=7;
dt=.078125;
Nf=(N/2) + 1;
w1=2*pi*f1;
t=0:dt:(N-1)*dt;
while 1
disp(' ');
disp('Please select a wave type from the menu.');
nwave=menu('Choose a waveform','cosine','square','sawtooth','triangle','exit');
if nwave==1 
  yin=cos(w1*t);
elseif nwave==2
  yin=square(w1*t);
elseif nwave==3
  yin=sawtooth(w1*t);
elseif nwave==4
  yin=triang(w1*t);
elseif nwave==5
  se=('You have exited RECONSTRUCT');
  disp(se)
  break
end
clf
subplot(211),plot(t,yin)
xlabel('time (sec)')
title('Original Signal')
Y=fft(yin);
v=[0 10 -1.5 1.5];
axis(v)
nofreq=input('How many components do you want (no more than 64)? ')
nofreq=nofreq+1;
Ysave=Y;
Y(nofreq+1:N)=[];
nf=N-nofreq;
A=zeros(1,nf);
Y=[2*Y A];
yout=ifft(Y);
subplot(212),plot(t,real(yout))
xlabel('time (sec)')
nfreq=nofreq-1;
title(['Reconstructed wave, No. of components= ',num2str(nfreq)])
%text(.2,.02,['     Number of components= ',num2str(nfreq)],'sc')
axis(v)

end  % while 1

return;
