%fftspa.m, m=file to demonstrate sampling, aliasing, and leakage
% calls m-files cosine.m, sumofsin.m, fftamps.m, plotspa.m

clear
%callstart

sig = input('Enter your name for signature on plots:  ','s');

nyes=1;

while nyes==1


disp([' '])
disp(['Please enter 1 (cosine)'])
disp(['             2 (combination of sinusoids)'])
disp(['             3 (exit)'])
nwave=input(' ');

if nwave==3
  nyes=0;
  disp(['You have exited FFTSPA'])
  break
end

if nwave==1|nwave==2

 disp([' '])
 %N=input('Enter the number of points in the sample: ');
 %dt=input('Enter dt, the time between sampled data: ');
 N=128; dt=1;
 disp(['Number of points in the sample, N:  Auto-set to ' int2str(N)]);
 disp(['Time between sampled data, dt:  Auto-set to ' int2str(dt)]);
 disp('  ')
 fn=1/(2*dt);
 disp(['The Nyquist frequency,fn = 1/(2*dt) = ' num2str(fn) ' Hz.'])
 disp(['To avoid ALIASING do not choose a frequency above this.'])
 Nf = (N/2) + 1;   % the index of the Nyquist frequency
 disp('  ')
 T=N*dt;
 disp(['The total sampling time or period, T= N*dt = ' num2str(T) ' sec'])
 t=0:dt:(N-1)*dt;
 tplot=0:dt:(2*N-1)*dt;
 f1=1/T;
 disp(['The fundamental frequency, f1=1/T. To avoid LEAKAGE,'])
 disp(['choose a frequency that is an integral multiple of f1 = ' num2str(f1) ' Hz.'])
 disp([' '])
 freq=fn * (0:(Nf-1)) / (Nf-1);


if nwave==2

  clf
  wavetype='Combination of Sinusoids';
  [y,yplot,off,Amp,phase] = sumofsin(N,f1,t);
  [Fftamp,Ay,Gy] = fftamps(y,dt,f1); 
  plotspa(wavetype,tplot,yplot,Amp,off,phase,nwave,Fftamp,f1,fn,freq,T,sig)

end


if nwave==1 

  clf
  wavetype='Sinusoid';
  f=input('Enter wave frequency (eg, 5*f1): ');
  w1=2*pi*f;
  Amp=input('What is the amplitude? ');
  off=input('What is the offset or mean value? ');
  phase=input('What is the phase shift (in degrees) ? ');
  disp('  ')
  [y,yplot] =cosine(Amp,off,phase,w1,t);
  [Fftamp,Ay,Gy] = fftamps(y,dt,f1);
  plotspa(wavetype,tplot,yplot,Amp,off,phase,nwave,Fftamp,f1,fn,freq,T,sig)

end

%text(.30,.4,['N= ',num2str(N),' dt= ',num2str(dt),' T= ',num2str(T)],'sc')
%text(.30,.37,['df= ',num2str(f1),' Nyquist freq.= ',num2str(fn)],'sc')
%text(.30,.34,['Variance= ', num2str(variance)],'sc')


else 
  disp(['Please try again and enter an integer 1,2,3,4,or 5 '])
end  % if else

end  % while

