%% dftdemo3 %% show how DFT captures frequency % define signal, sample rate, sample domain steps = 32; N = 64; t = (0:1:(N-1))/N; I = sqrt(-1); 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(1,k),zeros(1,N-k)]*(N/k); %sig = rand(1,N); %sig = 2*rand(1,N)-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 pause; end