%% dftdemo3
% show how DFT captures frequency

% S. Allen Broughton

%% define signal, sample rate, sample domain, signals are column vectors
steps = 32;
N = 64;
t = (0:1:(N-1))/N;
t= t'
sig = 2*cos(2*2*pi*t)+0.5*sin(7*2*pi*t);
%sig = 2*cos(5*2*pi*t)+0.5*sin(10*2*pi*t);
%k = 45; sig = [ones(k,1);zeros(N-k,1)]*(N/k);
%sig = rand(N,1);
%sig = 2*rand(N,1)-1;
%sig = 2*cos(5*2*pi*t)+ I*3*sin(7.1*2*pi*t);
SD = 0:(N-1);

%% get dft and frequency domain
fsig = fft(sig); 
FD = 0:(N-1);

%% display results 
figure(1)
subplot(2,2,1)
plot(SD,real(sig),'r-',SD,imag(sig),'g-')
title('sampled signal');
axis tight

for k = 0:steps 
subplot(2,2,2)
plot(FD,real(fsig),'r-',FD,imag(fsig),'g-', [k],real(fsig(k+1)),'k*',[k],imag(fsig(k+1)),'k*')
axis tight
title('DFT'); 
E = exp(2*pi*i*k*t);   
subplot(2,2,3)
plot(SD, real(E),'r-',FD,imag(E),'g-')
axis([0 (N-1) -1.1 1.1]);
title('pure wave form')
axis tight
subplot(2,2,4)
plot(SD, real(sig.*conj(E)),'r-',SD, imag(sig.*conj(E)),'g-', SD,zeros(size(SD)),'k-' );
title('product of signal and waveform');
axis tight
k = k
coeff = E'*sig
disp('press a key')
pause
end