Experiment 4
Aim:
To compute the Discrete Fourier Transform (DFT) of a given signal using two efficient algorithms:
1. Decimation in Time (DIT) Fast Fourier Transform (FFT)
2. Decimation in Frequency (DIF) Fast Fourier Transform (FFT)
Software Used:Matlab (R2025a)
Theory :
IT (Decimation in Time) FFT
D
:Breaks the input sequence into even and odd indexed parts, computes smaller FFTs
recursively, and combines them using twiddle factors.
➔ Input is bit-reversed first.
➔ Output is in natural order.
DIF (Decimation in Frequency) FFT:
: Splits the output spectrum into smaller parts, applies butterfly operations before
recursion, and reorders the final output using bit-reversal.
➔
Input is in natural order.
➔ Output is bit-reversed during computation, reordered at the end.
Code:
clc; clear; close
all
;
%% ----- User Input -----
x = input(
'Enter the input sequence (in square brackets):
');
x = x(:).';
% force row vector
N = length(x);
% Check radix-2
L = log2(N);
if
abs(L - round(L)) > eps
error(
'Length N=%d is not a power of 2. Use radix-2
length.'
, N);
end
%% ----- Compute -----
[X_DIT, stages_DIT] = DIT_FFT_iter(x);
% iterative,
stages included
[X_DIF, stages_DIF] = DIF_FFT_iter(x);
% iterative,
stages included
X_fft = fft(x);
% MATLAB built-in
%% ----- Command Window Output -----
disp(
'DIT-FFT result:'
); disp(X_DIT.');
disp(
'DIF-FFT result:'
); disp(X_DIF.');
disp(
'Built-in FFT result:'
); disp(X_fft.');
fprintf(
'Max abs error DIT vs fft: %.3e\n'
, max(abs(X_DIT - X_fft)));
fprintf(
'Max abs error DIF vs fft: %.3e\n'
, max(abs(X_DIF
- X_fft)));
%% ----- Figure 1: Final Comparison -----
figure(
'Name'
,
'Final Comparison'
);
% 1) Input x(n)
subplot(2,2,1);
stem(0:N-1, x,
'filled'
); grid
on
;
title(
'Input Sequence x(n)'
); xlabel(
'n'
); ylabel(
'x(n)'
);
% 2) |X_DIT(k)|
subplot(2,2,2);
stem(0:N-1, abs(X_DIT),
'filled'
); grid
on
;
title(
'DFT using DIT-FFT'
); xlabel(
'k'
); ylabel(
'|X_{DIT}(k)|'
);
% 3) |X_DIF(k)|
subplot(2,2,3);
stem(0:N-1, abs(X_DIF),
'filled'
); grid
on
;
title(
'DFT using DIF-FFT'
); xlabel(
'k'
); ylabel(
'|X_{DIF}(k)|'
);
% 4) |fft(x)|
subplot(2,2,4);
stem(0:N-1, abs(X_fft),
'filled'
); grid
on
;
title(
'DFT using fft()'
); xlabel(
'k'
); ylabel(
'|X_{fft}(k)|'
);
%% ----- Figure 2: DIT FFT Intermediate Stages -----
figure(
'Name'
,
'DIT FFT Stages'
);
numStages = length(stages_DIT);
for
s = 1:numStages
subplot(ceil(numStages/2),2,s);
stem(0:N-1, abs(stages_DIT{s}),
'filled'
); grid
on
;
title([
'DIT Stage '
, num2str(s)]);
xlabel(
'k'
); ylabel(
'|Value|'
);
end
%% ----- Figure 3: DIF FFT Intermediate Stages -----
figure(
'Name'
,
'DIF FFT Stages'
);
numStages = length(stages_DIF);
for
s = 1:numStages
subplot(ceil(numStages/2),2,s);
stem(0:N-1, abs(stages_DIF{s}),
'filled'
); grid
on
;
title([
'DIF Stage '
, num2str(s)]);
xlabel(
'k'
); ylabel(
'|Value|'
);
end
%% ================= DIT (iterative, radix-2, natural order) =================
function
[X, stages] = DIT_FFT_iter(x)
N = length(x);
smax = log2(N);
stages = {};
% Bit-reverse inputs
X = bitrevorder(x);
stages{end+1} = X;
% initial (bit reversed input)
m = 2;
for
s = 1:smax
mh = m/2;
Wm = exp(-1j*2*pi*(0:mh-1)/m);
for
k = 1:m:N
idx = k:(k+mh-1);
idy = (k+mh):(k+m-1);
t = Wm .* X(idy);
u = X(idx);
X(idx) = u + t;
X(idy) = u - t;
end
stages{end+1} = X;
% store after each stage
m = m*2;
end
end
%% ================= DIF (iterative, radix-2, then reorder) ==================
function
[X, stages] = DIF_FFT_iter(x)
N = length(x);
smax = log2(N);
stages = {};
X = x;
stages{end+1} = X;
% initial
m = N;
for
s = smax:-1:1
mh = m/2;
Wm = exp(-1j*2*pi*(0:mh-1)/m);
for
k = 1:m:N
idx = k:(k+mh-1);
idy = (k+mh):(k+m-1);
u = X(idx);
v = X(idy);
X(idx) = u + v;
X(idy) = (u - v) .* Wm;
end
stages{end+1} = X;
% store after each stage
m = mh;
end
% DIF → bit-reversed order
X = bitrevorder(X);
stages{end+1} = X;
% final reordered
end
Output:
Conclusion:
I n this experiment, the Discrete Fourier Transform (DFT) was computed using iterative Decimation
in Time (DIT) and Decimation in Frequency (DIF) FFT algorithms.
Both methods efficiently reduced computational complexity by using butterfly operations and
bit-reversal ordering.
The final FFT results matched closely with MATLAB’s built-infft()function, confirming their
correctness.