DFT (Digital Fourier Transform) - Cos(x)

 

 

 

 

 

 

Followings are the code that I wrote in Octave to creates all the plots shown in this page. You may copy these code and play with these codes. Change variables and try yourself until you get your own intuitive understanding.

 

< Code 1 >

 

f = 8;

ph = 0;

x = linspace(0,2*pi,80);

sig = cos(f .* x);

 

n = 0:length(x)-1;

N = length(n);

 

i = 80;

 

ft = []

for k = 0:i

  e_k = exp(-j*2*pi*k*n ./ N);

  

  sig_re = real(sig);

  sig_im = imag(sig);

  e_k_re = real(e_k);

  e_k_im = imag(e_k);

  sig_c = sig_re + j .* sig_im;

  e_k_c = e_k_re + j .* e_k_im;

  

  ft_k = sum(sig_c .* e_k_c)/N;

  ft = [ft ft_k];

end

 

hFig = figure(1,'Position',[300 300 800 500]);

 

sig_y = [-1.5 1.5];

ek_y = [-1.5 1.5];

ft_y = [0 1.0];

ft_phase_y = [-pi pi];

 

subplot(3,5,1);

axis([0 1 0 1]);

text(-0.5,0.5,'x(n)','FontSize',16,'fontweight','bold');

set(gca,'Visible','off')

 

subplot(3,5,[2 3]);

hold on;

plot(n,sig_re,'ko-','MarkerFaceColor',[0 1 0],'MarkerSize',4);

plot(n,zeros(1,length(n)),'k--');

ylim(sig_y);

title('real[x(n)]');

box on;

hold off;

 

subplot(3,5,[4 5]);

hold on;

plot(n,sig_im,'ko-','MarkerFaceColor',[0 1 0],'MarkerSize',4);

plot(n,zeros(1,length(n)),'k--');

ylim(sig_y);

title('imaginary[x(n)]');

box on;

hold off;

 

 

subplot(3,5,6);

axis([0 1 0 1]);

text(-0.5,0.7,'e^{-j (2\pi k n)/N}','FontSize',16,'fontweight','bold');

tStr = sprintf('@ k = %d',i);

text(-0.5,0.3,tStr,'FontSize',16,'fontweight','bold','color','red');

set(gca,'Visible','off')

 

subplot(3,5,[7 8]);

hold on;

plot(n,e_k_re,'ko-','MarkerFaceColor',[0 0 1],'MarkerSize',4);

plot(n,zeros(1,length(n)),'k--');

ylim(ek_y);

title('real[e^{-j (2\pi k n)/N}]');

box on;

hold off;

 

subplot(3,5,[9 10]);

hold on;

plot(n,e_k_im,'ko-','MarkerFaceColor',[0 0 1],'MarkerSize',4);

plot(n,zeros(1,length(n)),'k--');

ylim(ek_y);

title('imaginary[e^{-j (2\pi k n)/N}]');

box on;

hold off;

 

 

subplot(3,5,11);

axis([0 1 0 1]);

text(-1.0,0.7,'\Sigma_{n=0}^{N} x(n) e^{-j (2\pi k n)/N}','FontSize',16,'fontweight','bold');

tStr = sprintf('@ k = %d',i);

text(-0.5,0.3,tStr,'FontSize',16,'fontweight','bold','color','red');

set(gca,'Visible','off')

 

subplot(3,5,[12 13]);

hold on;

plot(abs(ft),'ko-','MarkerFaceColor',[1 0 0],'MarkerSize',4);

plot(i+1,abs(ft(end)),'ro','MarkerFaceColor',[1 0 0],'MarkerSize',8);

plot(n,zeros(1,length(n)),'k--');

xlim([0 length(n)]);

ylim(ft_y);

title('Magnitude[Fourier]');

box on;

hold off;

 

subplot(3,5,[14 15]);

hold on;

plot(arg(ft),'ko-','MarkerFaceColor',[1 0 0],'MarkerSize',4);

plot(i+1,arg(ft(end)),'ro','MarkerFaceColor',[1 0 0],'MarkerSize',8);

plot(0,zeros(1,length(n)),'k--');

xlim([0 length(n)]);

ylim(ft_phase_y);

title('phase[Fourier]');

box on;

hold off;