%series.m        
%m-file to generate various waveforms 
% and their Fourier series amplitudes
%calls cosine.m, squarewv.m,sawtoothwv.m, triangwv.m, triang.m to generate
%the waveforms, and calls fftamps.m to do the fft and find the Fourier
%amplitudes of the incoming time signals.

usrname=input('Please enter your name so it can be added to each plot: ','s');
filename=input('Please enter desired name for output file: ','s');

f1=.1;    %the fundamental frequency
T=10;     %the Period is 10 sec
N=128;    %the number of data points
m=7;      %128=2^7
dt=.078125;    %f=1/(N*dt) = 1/T so dt=T/N 
fn = 1/(2*dt);
Nf=(N/2) + 1;  %the index of the Nyquist frequency
freq=fn * (0:(Nf-1)) / (Nf-1);
w1=2*pi*f1;    %the radian frequency
t=0:dt:(N-1)*dt;
wavenames=struct('type',{{'cosine','square','sawtooth','triangle'}});

while 1

disp('Select a wave type from the menu.');
nwave=menu('Choose a waveform','cosine','square','sawtooth','triangle','exit');
disp(' ');
if nwave<5
  wavetype=wavenames.type(nwave);
  disp(['Wave type selected: ']);
  disp(wavetype);
  disp(' ');
  Amp=input('What is the amplitude? ');
  off=input('What is the offset or mean value? ');
end

if nwave==1 
  wavetype='sinusoid';
  phase=input('What is the phase shift (in degrees) ? ');
  y =cosine(Amp,off,phase,w1,t);
elseif nwave==2
  wavetype='square';
  phase = 0;
  y =squarewv(Amp,off,phase,w1,t);
elseif nwave==3
  wavetype='sawtooth';
  phase = 0;
  y =sawtoothwv(Amp,off,phase,w1,t);
elseif nwave==4
  wavetype='triangle';
  phase = 0;
  y =triangwv(Amp,off,phase,w1,t);
end % if

if nwave<5
  [Ampl,Ay,Gy] = fftamps(y,dt,f1);
  variance = sum(Gy(2:Nf))*f1;
  clf;
  plotfft(wavetype,t,y,Amp,off,phase,nwave,Ampl,f1,fn,freq,T,variance,usrname)
  disp(['   '])
  printfft(wavetype,f1,freq,Ampl,Ay,Gy,Amp,phase,off,Nf,filename)  
else
  disp(['You have exited SERIES.'])
  break
end


end
