Sie sind auf Seite 1von 10

DFT and FFT Implementation

THUSI 200731549 10/27/2013

AIM
The main objective of this part is to implement the discrete Fourier transform (DFT) and the fast Fourier transform (FFT) on matlab.

BACKGROUND
The Discrete Fourier Transform (DFT) is the equivalent of the continuous Fourier transform for signals known only at N instants separated by sample times T(i.e.a finite sequence of data). It is difined by X(k) = where k= 0, 1, ...,N-1. The fast Fourier Transform (FFT) is an efficient algorithm for calculating the Discrete Fourier Transform. It computes the transformation at a rapid/fast rate and hence it is the most preferred in lot of applications. 2.1 The Discrete Fourier Transform which is represented by X(k) = be implemented on Matlab as follows.
function [ dftX ] = DFTdir( input_signal , N ) %DFTdir computes the Fourier transform of an input input_signal for N samples %initilize the output vector size N dftX = zeros(1, N); %the outer loop for k=0,1,,N-1 for K = 0:N-1 % the inner loop for the sum for n = 0:N-1 dftX(K+1) = dftX(K+1) + input_signal(n+1)*exp(-1i*2 *pi*K*n/N); end end end

can

(a) The implementation can be compared with the Matlab function fft using inputs such

as [11], [1 1 0 1], [1 0 1 -1], [1 1 0 1 -1 -1 -1 -1]. The comparison is as follows.

Thusi S.I: 200731549

Page 1

%define test vectors input1 = [1 1]; input2 = [1 1 0 1]; input3 = [1 0 1 -1]; input4 = [1 1 0 1 -1 -1 -1 -1]; %calculate foutput1 = foutput2 = foutput3 = foutput4 = %calculate doutput1 = doutput2 = doutput3 = doutput4 = fft for each input fft(input1,length(input1)); fft(input2,length(input2)); fft(input3,length(input3)); fft(input4,length(input4)); the DFTdir for each input DFTdir(input1,length(input1)); DFTdir(input2,length(input2)); DFTdir(input3,length(input3)); DFTdir(input4,length(input4));

%plot both outputs for comparison subplot(4, 2, 1), plot(abs(foutput1)), ('amplitude'),xlabel('frequency'); subplot(4, 2, 2), plot(abs(doutput1)), ('amplitude'),xlabel('frequency'); subplot(4, 2, 3), plot(abs(foutput2)), ('amplitude'),xlabel('frequency'); subplot(4, 2, 4), plot(abs(doutput2)), ('amplitude'),xlabel('frequency'); subplot(4, 2, 5), plot(abs(foutput3)), ('amplitude'),xlabel('frequency'); subplot(4, 2, 6), plot(abs(doutput3)), ('amplitude'),xlabel('frequency'); subplot(4, 2, 7), plot(abs(foutput4)), ('amplitude'),xlabel('frequency'); subplot(4, 2, 8), plot(abs(doutput4)), ('amplitude'),xlabel('frequency');

title('fft : input1'), ylabel title('DFTdir: input1'), ylabel title('fft : input2'), ylabel title('DFTdir: input2'), ylabel title('fft : input3'), ylabel title('DFTdir: input3'), ylabel title('fft : input4'), ylabel title('DFTdir: input4'), ylabel

(b) 1024 samples are generated so as to plot the absolute values of the transform

calculated by DFTdir and fft. The results are depicted below, including the code for generating the samples).

Thusi S.I: 200731549

Page 2

N = 1024; %time spaning 5 full cycles t = linspace(0, 5*2*pi, N); %sine wave over the 5 cycles x = sin(t); %two fourier transforms y = fft(x, N); z = DFTdir(x, N); %plot of both outputs subplot(2,1,1), plot(abs(y)), title('fft'), ylabel('amplitude'),xlabel ('frequency'); subplot(2,1,2), plot(abs(z)), title('DFTdir'), ylabel ('amplitude'),xlabel('frequency');

fft 600

amplitude

400

200

200

400

600 frequency DFTdir

800

1000

1200

600

amplitude

400

200

200

400

600 frequency

800

1000

1200

The results are the same as expected but the difference is that the fft computes the transformation more faster that the DFTdir.
(c) The time taken by fft and DFTdir to compute the transformation can be estimated on

Matlab using tic and toc. The code for such implementation is shown below.
N = 1024; %time spaning 5 full cycles t = linspace(0, 5*2*pi, N); %sine wave over the 5 cycles x = sin(t); %timed fft timefft = zeros(1,24);

Thusi S.I: 200731549

Page 3

for counter = 1:24; tic fft(x, N); timefft(counter) = toc; end averagetimefft = mean(timefft) %timed DFTdir timeDFTdir = zeros(1,24); for counter = 1:24; tic DFTdir(x, N); timeDFTdir(counter) = toc; end averagetimeDFTdir = mean(timeDFTdir)

The estimated times are as follows: fft = 1.99*10^(-5) s and DFTdir = 0.5503 s. It is clear that the fft responds much faster that the DFTdir.
(d) Using the same code as above for estimating the time, we increase N to be large and

observe the results. The results for different N are as follows. N = 4096: fft = 7.3*10^(-5) s DFTdir = 8.9 s N = 8192: fft = 1.6137 *10^(-4) s DFTdir = 35.4412 s Clearly the fft is still faster than the DFTdir. The time to respond is becoming slower as the number of samples increases. 2.2 The pseudo code can be implemented in matlab using using the function FFTij. It involves the reordering and butterflies. The implementation code is shown below.
function [ x ] = fftij( data, N ) %fftij computes the fft of an input input_signal for N samples %reorder %initilize output vector x = zeros(1, N); m = floor(log2(N)); for k=0 : N-1 naddr = 0; iaddr = k; for i = 0 : m-1 rmndr = mod(iaddr,2);

Thusi S.I: 200731549

Page 4

naddr = naddr+rmndr*2^(m-1-i); iaddr = floor(iaddr/2); end x(naddr+1) = data(k+1); end %butterflys %(evaluate for m stages) for s=1 : m %(evaluating for the stage s) bsep = 2^s ; %(p = N=2s; change in exponent) p = N/bsep ; %(bwidth = 2^s-1) separation between butterfly inputs) bwidth = bsep/2 ; %(work out weighting factors for a particular stage) for j = 0 : (bwidth-1) %(calculate power of WN) r = p*j ; %(calculate exponent of e^(-j)) theta = 2*pi*r/N ; %(calculate WrN) wn = complex(cos(theta),-sin(theta)) ; %(for all the j-th butteries in the stage) for topval = j:bsep:N-1 botval = topval+bwidth; temp = x(botval+1)*wn; x(botval+1) = x(topval+1)-temp; x(topval+1) = x(topval+1)+temp; end end end end

(a) The FFTij was tested using the code below and the results were obtained as follows.

%define test vectors input1 = [1 1]; input2 = [1 1 0 1]; input3 = [1 0 1 -1]; input4 = [1 1 0 1 -1 -1 -1 -1]; %calculate foutput1 = foutput2 = foutput3 = foutput4 = %calculate doutput1 = doutput2 = doutput3 = doutput4 = fft for each input fft(input1,length(input1)); fft(input2,length(input2)); fft(input3,length(input3)); fft(input4,length(input4)); the fftij for each input fftij(input1,length(input1)); fftij(input2,length(input2)); fftij(input3,length(input3)); fftij(input4,length(input4));

%plot both outputs for comparison subplot(4, 2, 1), plot(abs(foutput1)), title('fft : input1'), ylabel('amplitude'),xlabel('frequency');

Thusi S.I: 200731549

Page 5

subplot(4, 2, 2), plot(abs(doutput1)), title('fftij: input1'), ylabel('amplitude'),xlabel('frequency'); subplot(4, 2, 3), plot(abs(foutput2)), title('fft : input2'), ylabel('amplitude'),xlabel('frequency'); subplot(4, 2, 4), plot(abs(doutput2)), title('fftij: input2'), ylabel('amplitude'),xlabel('frequency'); subplot(4, 2, 5), plot(abs(foutput3)), title('fft : input3'), ylabel('amplitude'),xlabel('frequency'); subplot(4, 2, 6), plot(abs(doutput3)), title('fftij: input3'), ylabel('amplitude'),xlabel('frequency'); subplot(4, 2, 7), plot(abs(foutput4)), title('fft : input4'), ylabel('amplitude'),xlabel('frequency'); subplot(4, 2, 8), plot(abs(doutput4)), title('fftij: input4'), ylabel('amplitude'),xlabel('frequency');

(b) In this part a vector of 1024 random numbers is generated using the function randn. This will help plot the difference in absolute values of DFT and fft as calculated by FFTij. The results are depicted below including the code used to generate the vector.
%create vector containing 1024 random numbers input_vector = randn(1,1024); foutput = fft(input_vector,length(input_vector)); %calculate the fftij for each input doutput = fftij(input_vector,length(input_vector)); %plot both outputs for comparison

Thusi S.I: 200731549

Page 6

subplot(2, 1, 1), plot(abs(foutput1)), title('fft : randn(1,1024)'), ylabel('amplitude'),xlabel('frequency'); subplot(2, 1, 2), plot(abs(doutput1)), title('fftij: randn(1,1024)'), ylabel('amplitude'),xlabel('frequency');

fft : randn(1,1024) 2 1.5

amplitude

1 0.5 0

1.1

1.2

1.3

1.4

1.5 1.6 frequency fftij: randn(1,1024)

1.7

1.8

1.9

2 1.5

amplitude

1 0.5 0

1.1

1.2

1.3

1.4

1.5 1.6 frequency

1.7

1.8

1.9

The result depicts similar results as expected. The results for fft and DFTdir will always be the same but the difference will be in amount of time taken for computation. (c) The time required for the execution of FFTij and fft for few values of N can be determined and compared as follows. The code and the results are as follows.
N = 8192; t = linspace(0, 5*2*pi, N); x = sin(t); iterations = 64; timefft = zeros(1,iterations); for counter = 1:iterations; tic fft(x, N); timefft(counter) = toc; end averagetimefft = mean(timefft) timefftij = zeros(1,iterations); for counter = 1:iterations; tic fftij(x, N); timefftij(counter) = toc; end

Thusi S.I: 200731549

Page 7

averagetimefftij = mean(timefftij)

N = 1024: fft = FFTij = 0.033 s N = 4096: fft = FFTij = 0.16 s N = 8192: fft = FFTij = 0.337 s s s s

It is clear from the results that the fft responds much faster than the FFTij and FFTij tend to respond more faster than DFTdir. 2.3 (a) The DFTdir, fft and the FFTij were used to calculate the coefficients of x(nT) = {3; 6; 4; 7; 2; 5; 8; 1} and the results compare as follows.
FFTij 36.0000 -2.5355 - 0.9497i -7.0000 - 3.0000i 4.5355 - 8.9497i -2.0000 4.5355 + 8.9497i -7.0000 + 3.0000i -2.5355 + 0.9497i DFTdir 36.0000 -2.5355 - 0.9497i -7.0000 - 3.0000i 4.5355 - 8.9497i -2.0000 4.5355 + 8.9497i -7.0000 + 3.0000i -2.5355 + 0.9497i fft 36.0000 -2.5355 - 0.9497i -7.0000 - 3.0000i 4.5355 - 8.9497i -2.0000 4.5355 + 8.9497i -7.0000 + 3.0000i -2.5355 + 0.9497i

The results of coefficients for all cases are the same. (b) In this part we are required to obtain the time sequence given the coefficients. This can be

achieved using the inverse of the DFTdir. The result is computed as follows.
36 -2.5355 -0.9497j -7 -3j 4.5355 -8.9497j 2 4.5355 + 8.9497j -7 +3j

Thusi S.I: 200731549

Page 8

-2.5355 +0.9497j

These coefficients results in the following time sequence.

The following code was used for the computation.


function [ dftx ] = IDFT( input_signal , N ) %DFTdir computes the inverse Fourier transform %initilize the output vector size N dftx = zeros(1, N); %the outer loop for k=0,1,,N-1 for n = 0:N-1 % the inner loop for the sum for K = 0:N-1 dftx(n+1) = dftx(n+1) + input_signal(K+1)*exp(1i*2*pi*K*n/N); end end end

3. Conclusion. The main objective of this practical was achieved in the sense that the DFT was implemented and it was compared to the matlab function fft. The results were as expected because the fft was expected to respond faster than the DFT. Thus the implementation of DFT and the results in matlab agrees with the theory.
4. References

[1] A. R. ndjiongue, Hailing Zhu, SIG3B21 practical guide, 2013, University of Johannesburg. [2] http://www.robots.ox.ac.uk/~sjrob/Teaching/SP/l7.pdf, Lecture 7- The Discrete Fourier Transform, accessed on 23-10-2013.

Thusi S.I: 200731549

Page 9

Das könnte Ihnen auch gefallen