0% found this document useful (0 votes)
43 views43 pages

DSP Lab Experiments BEC502

Uploaded by

jimmjamm678
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views43 pages

DSP Lab Experiments BEC502

Uploaded by

jimmjamm678
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Experiment No.

1
Elementary Discrete-Time Signals

Aim: To write a program to generate the following discrete-time signals:


a) Unit sample sequence, b) Unit step sequence, c) Exponential sequence,
d) Sinusoidal sequence, e) Random sequence

Software used: MATLAB

Theory: Write relevant theory with expressions and sketches for each signal.

Procedure:

• Open MATLAB.
• Click on New Script.
• In the editor, type the program.
• Save the file with .m extension.
• Run the file by pressing Run or typing the filename in the command window.

Programs:

a) Unit sample sequence


% Generate a vector from -10 to 10
n=-10:10;
% Generate the unit sample sequence
u = [zeros(1,10) 1 zeros(1,10)];
% Plot the unit sample sequence
stem(n,u);
axis([-10 10 0 1.2]);
title('Unit Sample Sequence');
xlabel('n');
ylabel('Amplitude');

b) Unit step sequence


% Generate a vector from -10 to 10
n=-10:10;
% Generate the unit step sequence
u=[zeros(1,10) ones(1,11)];
% Plot the unit step sequence
stem(n,u);
axis([-10 10 0 1.2]);
title('Unit Step Sequence');
xlabel('n');
ylabel('Amplitude');

c) Exponential sequence
i. Real exponential sequence
% Generation of a real exponential sequence
n=0:40;
K = 0.2;
% Exponential sequence for a>1
a1 = 1.2;
x1 = K*a1.^n;
subplot(2,2,1);
stem(n,x1);
title('Exponential Sequence (a>1)');
xlabel('n'); ylabel('Amplitude');

% Exponential sequence for 0<a<1


a2 = 0.8;
x2 = K*a2.^n;
subplot(2,2,2);
stem(n,x2);
title('Exponential Sequence (0<a<1)');
xlabel('n'); ylabel('Amplitude');

% Exponential sequence for a<-1


a3 = -1.2;
x3 = K*a3.^n;
subplot(2,2,3);
stem(n,x3);
title('Exponential Sequence (a<-1)');
xlabel('n'); ylabel('Amplitude');

% Exponential sequence for -1<a<0


a4 = -0.8;
x4 = K*a4.^n;
subplot(2,2,4);
stem(n,x4);
title('Exponential Sequence (-1<a<0)');
xlabel('n'); ylabel('Amplitude');

ii. Complex exponential sequence


% Generation of a complex exponential sequence
n=0:40;
K=2;
c=-(1/12)+(pi/6)*i;
x=K*exp(c*n);

% Plot the real part


subplot(2,1,1);
stem(n,real(x));
title('Real part');
xlabel('n'); ylabel('Amplitude');

% Plot the imaginary part


subplot(2,1,2);
stem(n,imag(x));
title('Imaginary part');
xlabel('n'); ylabel('Amplitude');

d) Sinusoidal sequence
n=-10:10;
A=1.2;
f=0.1;
x=A*cos(2*pi*f*n);
stem(n,x);
title('Sinusoidal Sequence');
xlabel('n');
ylabel('Amplitude');

e) Random sequence
n=-10:10;
x=rand(size(n))*20-10;
stem(n,x);
title('Random Sequence');
xlabel('n');
ylabel('Amplitude');

Output:

a) Unit sample sequence

b) Unit step sequence


c) Exponential sequence
i. Real exponential sequence

ii. Complex exponential sequence


d) Sinusoidal sequence

e) Random sequence

Result:

The programs to generate elementary discrete-signal signals were written and the
signals were plotted successfully.
Experiment No. 2
Simple Operations on Discrete-Time Signals

Aim: To write a program to perform the following operations on signals:


a) Signal addition, b) Signal multiplication, c) Scaling, d) Shifting, e) Folding

Software used: MATLAB

Theory: Write relevant theory with expressions and examples for each operation.

Procedure:

• Open MATLAB.
• Click on New Script.
• In the editor, type the program.
• Save the file with .m extension.
• Run the file by pressing Run or typing the filename in the command window.

Programs:

a) Signal addition
n=-5:5;
x1=[1 -3 2 -4 2 1 -3 5 1 -3 -4];
x2=[2 3 4 3 1 0 3 -2 1 2 3];
% Perform signal addition
y=x1+x2;
% Plot Signal x1
subplot(3,1,1);
stem(n,x1);
title('Signal x1');
xlabel('n');ylabel('Amplitude');
% Plot Signal x2
subplot(3,1,2);
stem(n,x2);
title('Signal x2');
xlabel('n');ylabel('Amplitude');
% Plot the output signal
subplot(3,1,3);
stem(n,y);
title('Signal y=x1+x2');
xlabel('n');ylabel('Amplitude');

b) Signal multiplication
n=-5:5;
x1=[1 -3 2 -4 2 1 -3 5 1 -3 -4];
x2=[2 3 4 3 1 0 3 -2 1 2 3];
% Perform signal multiplication
y=x1.*x2;
% Plot Signal x1
subplot(3,1,1);
stem(n,x1);
title('Signal x1');
xlabel('n');ylabel('Amplitude');
% Plot Signal x2
subplot(3,1,2);
stem(n,x2);
title('Signal x2');
xlabel('n');ylabel('Amplitude');
% Plot the output signal
subplot(3,1,3);
stem(n,y);
title('Signal y=x1.*x2');
xlabel('n');ylabel('Amplitude');

c) Scaling
n=-10:10;
f=0.1;
A=3; %scaling factor
x=1.5*sin(2*pi*f*n);
y=A*x;
% Plot the input signal x
subplot(2,1,1);
stem(n,x);
title('Signal x');
xlabel('n');ylabel('Amplitude');
% Plot the output signal y
subplot(2,1,2);
stem(n,y);
title('Signal y=A*x');
xlabel('n');ylabel('Amplitude');

d) Shifting
n=-5:5;
% Input signal
x=[1 -3 2 -4 2 1 -3 5 1 -3 -4];
k=3; % Shifting units of time
%Generate time delay by k units
x_delayed=[zeros(1,k) x(1:end-k)];
%Generate time advance by k units
x_advanced=[x(k+1:end) zeros(1,k)];
% Plot the input signal x
subplot(3,1,1);
stem(n,x);
title('Signal x');
xlabel('n');ylabel('Amplitude');
% Plot the delayed signal
subplot(3,1,2);
stem(n,x_delayed);
title('Signal Delayed by k=3');
xlabel('n');ylabel('Amplitude');
% Plot the advanced signal
subplot(3,1,3);
stem(n,x_advanced);
title('Signal Advanced by k=3');
xlabel('n');ylabel('Amplitude');
e) Folding
n=-5:5;
% Input signal
x=[1 -3 2 -4 2 1 -3 5 1 -3 -4];
% Perform signal folding
y=fliplr(x);
% Plot the input signal x
subplot(2,1,1);
stem(n,x);
title('Signal x(n)');
xlabel('n');ylabel('Amplitude');
% Plot the folded signal y
subplot(2,1,2);
stem(n,y);
title('Signal y=x(-n)');
xlabel('n');ylabel('Amplitude');

Output:

a) Signal addition
b) Signal multiplication

c) Scaling
d) Shifting

e) Folding

Result:

The programs to perform simple operations on discrete-signal signals were written


and the outputs were verified successfully.
Experiment No. 3
Convolution of Discrete-Time Signals

Aim: To write a program to perform convolution of two given sequences (without using
built-in function) and display the signals.

Software used: MATLAB

Theory: Write relevant theory with the expression and an example for convolution.

Consider 𝑥(𝑛) = {1, 2, 3, 1} and ℎ(𝑛) = {1, 2 , 1, −1} and perform convolution.

Procedure:

• Open MATLAB.
• Click on New Script.
• In the editor, type the program.
• Save the file with .m extension.
• Run the file by pressing Run or typing the filename in the command window.

Program:
clc
% Take input for the signal x(n)
x_start = input('Enter the starting index of x(n): ');
x = input('Enter the input signal x(n): ');
len_x = length(x); % Length of the input signal x(n)

% Take input for the impulse response h(n)


h_start = input('Enter the starting index of h(n): ');
h = input('Enter the impulse response h(n): ');
len_h = length(h); % Length of the impulse response h(n)

% Length of the convolution output y(n)


len_y = len_x + len_h - 1;

% Initialize the output y(n) to zero


y = zeros(1, len_y);

% Perform convolution using nested loops


for i = 1:len_x
for j = 1:len_h
y(i+j-1) = y(i+j-1) + x(i) * h(j);
end
end

% Calculate the end indices


x_end = x_start + len_x - 1;
h_end = h_start + len_h - 1;

y_start = x_start + h_start; % Start index of y(n)


y_end = x_end + h_end; % End index of y(n)

% Defining the index ranges for plotting


n_x = x_start:x_end;
n_h = h_start:h_end;
n_y = y_start:y_end;
% Display the convolution result
disp('The convolution result y(n): ')
disp(y)

% Plot the input signal x(n)


subplot(3, 1, 1)
stem(n_x, x)
title('Input signal x(n)')
xlabel('n'); ylabel('x(n)'); grid on

% Plot the impulse response h(n)


subplot(3, 1, 2)
stem(n_h, h)
title('Impulse response h(n)')
xlabel('n'); ylabel('h(n)'); grid on

% Plot the convolution result y(n)


subplot(3, 1, 3)
stem(n_y, y)
title('Convolution result y(n)')
xlabel('n'); ylabel('y(n)'); grid on

Output:
Enter the starting index of x(n): 0
Enter the input signal x(n): [1 2 3 1]
Enter the starting index of h(n): -1
Enter the impulse response h(n): [1 2 1 -1]
The convolution result y(n):
1 4 8 8 3 -2 -1
Result:

The program to perform convolution of two discrete-signal signals was written and
the output was verified successfully.
Experiment No. 4
System Function and Impulse Response of a Causal System

Aim: To write a program to determine the system function and impulse response of a
given causal system.

Problem Statement:

Consider a causal system 𝑦(𝑛) = 0.9𝑦(𝑛 − 1) + 𝑥(𝑛).


a) Determine 𝐻(𝑧) and sketch its pole-zero plot.
b) Plot |𝐻(𝑒 𝑗𝜔 )| and ∠ 𝐻(𝑒 𝑗𝜔 )
c) Determine the impulse response ℎ(𝑛).

Software used: MATLAB

Theory: Write relevant theory.

Consider a causal system 𝑦(𝑛) = 0.9𝑦(𝑛 − 1) + 𝑥(𝑛). Find system function and poles
and zeros and determine impulse response theoretically.

Procedure:

• Open MATLAB.
• Click on New Script.
• In the editor, type the program.
• Save the file with .m extension.
• Run the file by pressing Run or typing the filename in the command window.

Program:
% Coefficients of the system
x= [1 0]; % Coefficients of x(n), corresponding to numerator
y= [1 -0.9]; % Coefficients of y(n), corresponding to denominator
% H(z) = Y(z)/X(z)
H=tf(x, y, -1) % Transfer function (discrete-time)
% Pole-Zero Plot
figure;
zplane(x, y);
title('Pole-Zero Plot of the System');
grid on;

% Frequency response
w=linspace(-pi, pi, 1000); % Define frequency range
H_freq=freqz(x,y,w); % Compute frequency response
% Plot Magnitude response |H(e^jω)|
figure;
subplot(2,1,1);
plot(w,abs(H_freq));
title('Magnitude Response |H(e^{j\omega})|');
xlabel('Frequency (rad/sample)'); ylabel('Magnitude');
grid on;
% Plot Phase response ∠H(e^jω)
subplot(2,1,2);
plot(w,angle(H_freq));
title('Phase Response ∠H(e^{j\omega})');
xlabel('Frequency (rad/sample)'); ylabel('Phase (radians)');
grid on;

% Impulse response
n=-10:10; % Sample indices
h=impz(x,y,n) % Compute impulse response
% Plot impulse response
figure;
stem(n,h);
title('Impulse Response h(n)');
xlabel('n (samples)'); ylabel('h(n)');
grid on;

Output:
H =

z
-------
z - 0.9

Sample time: unspecified


Discrete-time transfer function.

h =

0
0
0
0
0
0
0
0
0
0
1.0000
0.9000
0.8100
0.7290
0.6561
0.5905
0.5314
0.4783
0.4305
0.3874
0.3487
a) Pole-Zero Plot

b) Frequency Response
c) Impulse Response

Result:

The program to determine the system function and impulse response of a given
causal system was written and the outputs were verified successfully.
Experiment No. 5
Computation of N-Point DFT

Aim: To write a program to compute N-point DFT of a given sequence (without using
built-in function) and plot the magnitude and phase spectrum.

Software used: MATLAB

Theory: Write relevant theory with the expression and an example for N-point DFT.

Procedure:

• Open MATLAB.
• Click on New Script.
• In the editor, type the program.
• Save the file with .m extension.
• Run the file by pressing Run or typing the filename in the command window.

Program:
% Input the sequence
x = input('Enter the sequence x[n]: ');
% Length of the sequence
N = length(x);
% Initialize DFT matrix
X = zeros(1, N);
% Compute N-point DFT
for k = 0:N-1
for n = 0:N-1
X(k+1) = X(k+1) + x(n+1) * exp(-1i * 2 * pi * k * n / N);
end
end
% Frequency index
k = 0:N-1;

disp('The DFT of the given sequence is:');


disp(X);

% Plot the magnitude spectrum


subplot(2,1,1);
stem(k, abs(X));
title('Magnitude Spectrum');
xlabel('Frequency Index (k)');
ylabel('|X[k]|');
grid on;

% Plot the phase spectrum


subplot(2,1,2);
stem(k, angle(X));
title('Phase Spectrum');
xlabel('Frequency Index (k)');
ylabel('Phase (radians)');
grid on;
Output:
Enter the sequence x[n]: [1 2 3 4]
The DFT of the given sequence is:
10.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 - 0.0000i -2.0000 - 2.0000i

Result:

The program to compute N-point DFT of a given sequence was written and the
magnitude and phase spectrum were plotted and the output was verified.
Experiment No. 6 (a)
Circular Convolution using DFT and IDFT

Aim: To write a program to compute circular convolution of two given sequences using
DFT and IDFT.

Software used: MATLAB

Theory: Write relevant theory for circular convolution using DFT and IDFT.

Procedure:

• Open MATLAB.
• Click on New Script.
• In the editor, type the program.
• Save the file with .m extension.
• Run the file by pressing Run or typing the filename in the command window.

Program:
clc
% Input two sequences
x1 = input('Enter the first sequence x1[n]: ');
x2 = input('Enter the second sequence x2[n]: ');

% Length of the sequences


N1 = length(x1);
N2 = length(x2);
N = max(N1, N2); % Circular convolution length is max(N1, N2)

% Zero padding if necessary to make lengths equal


x1_pad = [x1 zeros(1, N-N1)];
x2_pad = [x2 zeros(1, N-N2)];

% Compute DFT of both sequences


X1 = fft(x1_pad);
X2 = fft(x2_pad);

% Perform element-wise multiplication in frequency domain


Y = X1 .* X2;

% Compute IDFT to get circular convolution result


y = ifft(Y);

% Display circular convolution result


disp('The Circular Convolution of the given sequences is: ');
disp(y);

% Plot Signal x1
subplot(3,1,1);
stem(0:N-1, x1_pad);
title('Signal x1[n]');
xlabel('n'); ylabel('x1[n]'); grid on;

% Plot Signal x2
subplot(3,1,2);
stem(0:N-1, x2_pad);
title('Signal x2[n]');
xlabel('n'); ylabel('x2[n]'); grid on;

% Plot Circular Convolution Result


subplot(3,1,3);
stem(0:N-1, y);
title('Circular Convolution using DFT and IDFT');
xlabel('n'); ylabel('y[n]'); grid on;

Output:
Enter the first sequence x1[n]: [2 1 2 1]
Enter the second sequence x2[n]: [1 2 3 4]
The Circular Convolution of the given sequences is:
14 16 14 16

Result:

The program to compute circular convolution of two given sequences using DFT
and IDFT was written and the output was verified.
Experiment No. 6 (b)
Linear Convolution using DFT and IDFT

Aim: To write a program to compute linear convolution of two given sequences using
DFT and IDFT.

Software used: MATLAB

Theory: Write relevant theory for linear convolution using DFT and IDFT.

Procedure:

• Open MATLAB.
• Click on New Script.
• In the editor, type the program.
• Save the file with .m extension.
• Run the file by pressing Run or typing the filename in the command window.

Program:
clc
% Input two sequences
x = input('Enter the sequence x(n): ');
h = input('Enter the sequence h(n): ');

% Length of the sequences


N1 = length(x);
N2 = length(h);

% Linear convolution length is N1 + N2 - 1


N = N1 + N2 - 1;

% Zero padding to the length N


x_pad = [x zeros(1, N - N1)];
h_pad = [h zeros(1, N - N2)];

% Compute DFT of both sequences


X = fft(x_pad);
H = fft(h_pad);

% Perform element-wise multiplication in frequency domain


Y = X .* H;

% Compute IDFT to get linear convolution result


y= ifft(Y);

% Display linear convolution result


disp('The Linear Convolution of the given sequences is: ');
disp(y);

% Plot Signal x1
subplot(3,1,1);
stem(0:N-1, x_pad);
title('Signal x[n]');
xlabel('n'); ylabel('x[n]'); grid on;

% Plot Signal x2
subplot(3,1,2);
stem(0:N-1, h_pad);
title('Signal h[n]');
xlabel('n'); ylabel('h[n]'); grid on;

% Plot Linear Convolution Result


subplot(3,1,3);
stem(0:N-1, y);
title('Linear Convolution using DFT and IDFT');
xlabel('n'); ylabel('y[n]'); grid on;

Output:
Enter the sequence x(n): [1 2 3 1]
Enter the sequence h(n): [1 2 1 -1]
The Linear Convolution of the given sequences is:
1.0000 4.0000 8.0000 8.0000 3.0000 -2.0000 -1.0000
Enter the first sequence x1[n]: [2 1 2 1]

Result:

The program to compute linear convolution of two given sequences using DFT
and IDFT was written and the output was verified.
Experiment No. 7
Verification of DFT Properties

Aim: To write programs to verify linearity property, circular time shift property & circular
frequency shift property of DFT.

Software used: MATLAB

Theory: Write relevant theory for linearity property, circular time shift property & circular
frequency shift property of DFT.

Procedure:

• Open MATLAB.
• Click on New Script.
• In the editor, type the program.
• Save the file with .m extension.
• Run the file by pressing Run or typing the filename in the command window.

Programs:

a) Linearity Property
clc;

% Input two sequences


x1 = input('Enter the first sequence x1[n]: ');
x2 = input('Enter the second sequence x2[n]: ');

% Input the constants a1 and a2


a1 = input('Enter the constant a1: ');
a2 = input('Enter the constant a2: ');

% Length of the sequences


N1 = length(x1);
N2 = length(x2);
N = max(N1, N2);

% Zero padding if necessary to make lengths equal


x1_pad = [x1 zeros(1, N-N1)];
x2_pad = [x2 zeros(1, N-N2)];

% Compute DFT of individual sequences


X1 = fft(x1_pad);
X2 = fft(x2_pad);

% Compute left-hand side of the property


lhs = fft(a1 * x1_pad + a2 * x2_pad);

% Compute right-hand side of the property


rhs = a1 * X1 + a2 * X2;

% Display LHS and RHS values


disp('Left-hand side (DFT of a1*x1[n] + a2*x2[n]):');
disp(lhs);
disp('Right-hand side (a1*DFT(x1[n]) + a2*DFT(x2[n])):');
disp(rhs);

% Plot results for comparison


sgtitle('Verification of Linearity Property of DFT');

subplot(2,1,1);
stem(0:N-1, abs(lhs));
title('Magnitude of LHS (DFT of a1*x1[n] + a2*x2[n])');
xlabel('Frequency Index'); ylabel('Magnitude'); grid on;

subplot(2,1,2);
stem(0:N-1, abs(rhs));
title('Magnitude of RHS (a1*DFT(x1[n]) + a2*DFT(x2[n]))');
xlabel('Frequency Index'); ylabel('Magnitude'); grid on;

b) Circular Time Shift Property


clc

% Input the sequence


x = input('Enter the sequence x(n): ');
% Input the time-shifting factor
m = input('Enter the time-shifting factor m: ');

% Length of the sequence


N = length(x);
% Compute the DFT of the original sequence
X = fft(x);

% Circularly shift the sequence by m


x_shifted = circshift(x, m);

% Compute the LHS of the property


lhs = fft(x_shifted);

k = 0:N-1;
% Compute the RHS of the property
rhs = X .* (exp(-1i * 2 * pi * k * m / N));

% Display LHS and RHS values


disp('Left-hand side (DFT of shifted x(n)):');
disp(lhs);
disp('Right-hand side (Original DFT with phase shift):');
disp(rhs);

% Plot results for comparison


sgtitle('Verification of Time-Shifting Property of DFT');

subplot(2, 1, 1);
stem(k, abs(lhs));
title('Magnitude of LHS: DFT of shifted x(n)');
xlabel('Frequency Index'); ylabel('Magnitude');
grid on;

subplot(2, 1, 2);
stem(k, abs(rhs));
title('Magnitude of RHS: Original DFT with phase shift');
xlabel('Frequency Index'); ylabel('Magnitude');
grid on;
c) Circular Frequency Shift Property
clc

% Input the sequence


x = input('Enter the sequence x(n): ');
% Input the frequency-shifting factor
m = input('Enter the frequency shifting factor m: ');

% Length of the sequence


N = length(x);
% Compute the DFT of the original sequence
X = fft(x);

% Time index for the input sequence


n = 0:N-1;
% Compute LHS of the frequency-shifting property
lhs = fft(x .* (exp(1i * 2 * pi * m * n / N)));

% Compute RHS of the frequency-shifting property


rhs = circshift(X, m);

% Display LHS and RHS values


disp('Left-hand side (DFT of frequency-shifted x(n)):');
disp(lhs);
disp('Right-hand side (Circularly shifted DFT of x(n)):');
disp(rhs);

% Plot results for comparison


sgtitle('Verification of Frequency-Shifting Property of DFT');

subplot(2, 1, 1);
stem(0:N-1, abs(lhs));
title('Magnitude of LHS: DFT of frequency-shifted x(n)');
xlabel('Frequency Index'); ylabel('Magnitude');
grid on;

subplot(2, 1, 2);
stem(0:N-1, abs(rhs));
title('Magnitude of RHS: Circularly shifted DFT of x(n)');
xlabel('Frequency Index'); ylabel('Magnitude');
grid on;

Output:

a) Linearity Property
Enter the first sequence x1[n]: [1 2 3 4]
Enter the second sequence x2[n]: [4 5 2]
Enter the constant a1: 2
Enter the constant a2: 3
Left-hand side (DFT of a1*x1[n] + a2*x2[n]):
53.0000 + 0.0000i 2.0000 -11.0000i -1.0000 + 0.0000i 2.0000 +11.0000i

Right-hand side (a1*DFT(x1[n]) + a2*DFT(x2[n])):


53.0000 + 0.0000i 2.0000 -11.0000i -1.0000 + 0.0000i 2.0000 +11.0000i
b) Circular Time Shift Property
Enter the sequence x(n): [1 3 5 2]
Enter the time-shifting factor m: 3
Left-hand side (DFT of shifted x(n)):
11.0000 + 0.0000i 1.0000 - 4.0000i -1.0000 + 0.0000i 1.0000 + 4.0000i

Right-hand side (Original DFT with phase shift):


11.0000 + 0.0000i 1.0000 - 4.0000i -1.0000 - 0.0000i 1.0000 + 4.0000i
c) Circular Frequency Shift Property
Enter the sequence x(n): [1 4 3 2]
Enter the frequency shifting factor m: 2
Left-hand side (DFT of frequency-shifted x(n)):
-2.0000 + 0.0000i -2.0000 + 2.0000i 10.0000 - 0.0000i -2.0000 - 2.0000i

Right-hand side (Circularly shifted DFT of x(n)):


-2.0000 + 0.0000i -2.0000 + 2.0000i 10.0000 + 0.0000i -2.0000 - 2.0000i

Result:

The programs to verify linearity property, circular time shift property & circular frequency
shift property of DFT were written and the output was verified.
Experiment No. 8
Decimation-in-Time Radix-2 FFT

Aim: To write a program to develop decimation-in-time radix-2 FFT algorithm without using
built-in functions

Software used: MATLAB

Theory: Write relevant theory for decimation-in-time radix-2 FFT algorithm including butterfly
diagram.

Procedure:

• Open MATLAB.
• Click on New Script.
• In the editor, type the program.
• Save the file with .m extension.
• Run the file by pressing Run or typing the filename in the command window.

Program:
clc;
x = input('Enter the input sequence x(n): '); % Input sequence
N = input('Enter the value of N: '); % FFT length (must be a power of 2)

% Compute FFT using radix-2 FFT function


X = radix2_fft(x, N);

disp('FFT of the input: '); % Display the computed FFT


disp(X);

% Plot the magnitude and phase of the FFT


subplot(2, 1, 1);
stem(0:N-1, abs(X)); % Magnitude plot
title('Magnitude of FFT');
xlabel('Frequency Index k');
ylabel('|X(k)|');
grid on;

subplot(2, 1, 2);
stem(0:N-1, angle(X)); % Phase plot
title('Phase of FFT');
xlabel('Frequency Index k');
ylabel('\angle X(k) (radians)');
grid on;

% Function to compute radix-2 FFT


function x = radix2_fft(x, N)

% Ensure N is a power of 2
if log2(N) ~= floor(log2(N))
error('N must be a power of 2');
end

% Zero-pad input sequence if its length is less than N


x = [x zeros(1, N - length(x))];
% Compute bit-reversed indices
num_bits = log2(N); % Number of bits required for bit-reversal
reversed_indices = bit_reverse(0:N-1, num_bits);
x = x(reversed_indices + 1); % Reorder input sequence

% Main FFT computation loop


len = 2; % Start with 2-point FFT
while len <= N
half_len = len / 2; % Compute half the length of the current stage
twiddle_factor = exp(-1j * 2 * pi * (0:half_len-1) / len);

% Perform butterfly computations


for k = 1:len:N
even = x(k:k+half_len-1); % Even-indexed terms
odd = x(k+half_len:k+len-1); % Odd-indexed terms

% Compute FFT for the current stage


x(k:k+len-1) = [even + twiddle_factor .* odd, ... % Top branch
even - twiddle_factor .* odd]; % Bottom branch
end

len = len * 2; % Double the FFT length for the next stage
end
end

% Function to compute bit-reversed indices


function reversed_indices = bit_reverse(indices, num_bits)
reversed_indices = zeros(size(indices)); % Initialize output array
for k = 1:length(indices)
bin_str = dec2bin(indices(k), num_bits); % Convert index to binary string
reversed_bin_str = bin_str(end:-1:1); % Reverse the binary string
reversed_indices(k) = bin2dec(reversed_bin_str); % Convert to decimal
end
end

Output:
Enter the input sequence x(n): [1 2 3 4]
Enter the value of N: 8
FFT of the input:
Columns 1 through 7

10.0000 + 0.0000i -0.4142 - 7.2426i -2.0000 + 2.0000i 2.4142 - 1.2426i -2.0000 +


0.0000i 2.4142 + 1.2426i -2.0000 - 2.0000i

Column 8

-0.4142 + 7.2426i
Result:

The program to develop decimation-in-time radix-2 FFT algorithm was written and the output
was verified.
Experiment No. 9
Digital Low Pass FIR Filter

Aim: To write a program for the design and implementation of a digital low pass FIR filter using a
window to meet the given specifications

Software used: MATLAB

Theory: Write relevant theory for digital low pass FIR filter and different windows.

Procedure:

• Open MATLAB.
• Click on New Script.
• In the editor, type the program.
• Save the file with .m extension.
• Run the file by pressing Run or typing the filename in the command window.

Program:
clc;

fs = input('Enter the sampling frequency (Hz): ');


fc = input('Enter the cutoff frequency (Hz): ');
N = input('Enter the filter order: ');
window_type = input('Enter the window type (rectangular, hamming, hanning,
bartlett, blackman): ', 's');

% Calculate normalized cutoff frequency


wc = fc / (fs / 2);

% Select window based on user input


switch lower(window_type)
case 'rectangular'
window = rectwin(N + 1);
case 'hamming'
window = hamming(N + 1);
case 'hanning'
window = hann(N + 1);
case 'bartlett'
window = bartlett(N + 1);
case 'blackman'
window = blackman(N + 1);
otherwise
error('Invalid window type. Choose from: rectangular, hamming, hanning,
bartlett, blackman.');
end

% Design FIR filter


h = fir1(N, wc,'low', window);

% Plot the frequency response


freqz(h, 1, 1024, fs);
Output:
Enter the sampling frequency (Hz): 1000
Enter the cutoff frequency (Hz): 200
Enter the filter order: 20
Enter the window type (rectangular, hamming, hanning, bartlett, blackman): rectangular

Enter the sampling frequency (Hz): 5000


Enter the cutoff frequency (Hz): 500
Enter the filter order: 30
Enter the window type (rectangular, hamming, hanning, bartlett, blackman): hamming
Result:

The program for the design and implementation of a digital low pass FIR filter using a window to
meet the given specifications is written and the output is verified.
Experiment No. 10
Digital High Pass FIR Filter

Aim: To write a program for the design and implementation of a digital high pass FIR filter using
a window to meet the given specifications

Software used: MATLAB

Theory: Write relevant theory for digital high pass FIR filter and different windows.

Procedure:

• Open MATLAB.
• Click on New Script.
• In the editor, type the program.
• Save the file with .m extension.
• Run the file by pressing Run or typing the filename in the command window.

Program:
clc;

fs = input('Enter the sampling frequency (Hz): ');


fc = input('Enter the cutoff frequency (Hz): ');
N = input('Enter the filter order: ');
window_type = input('Enter the window type (rectangular, hamming, hanning,
bartlett, blackman): ', 's');

% Calculate normalized cutoff frequency


wc = fc / (fs / 2);

% Select window based on user input


switch lower(window_type)
case 'rectangular'
window = rectwin(N + 1);
case 'hamming'
window = hamming(N + 1);
case 'hanning'
window = hann(N + 1);
case 'bartlett'
window = bartlett(N + 1);
case 'blackman'
window = blackman(N + 1);
otherwise
error('Invalid window type. Choose from: rectangular, hamming, hanning,
bartlett, blackman.');
end

% Design FIR filter


h = fir1(N, wc,'high', window);

% Plot the frequency response


freqz(h, 1, 1024, fs);
Output:
Enter the sampling frequency (Hz): 1000
Enter the cutoff frequency (Hz): 200
Enter the filter order: 20
Enter the window type (rectangular, hamming, hanning, bartlett, blackman): rectangular

Enter the sampling frequency (Hz): 5000


Enter the cutoff frequency (Hz): 1000
Enter the filter order: 30
Enter the window type (rectangular, hamming, hanning, bartlett, blackman): hamming
Result:

The program for the design and implementation of a digital high pass FIR filter using a window to
meet the given specifications is written and the output is verified.
Experiment No. 11
Digital IIR Butterworth Low Pass Filter

Aim: To write a program for the design and implementation of a digital IIR Butterworth low pass
filter to meet the given specifications

Software used: MATLAB

Theory: Write relevant theory for digital IIR Butterworth low pass filter.

Procedure:

• Open MATLAB.
• Click on New Script.
• In the editor, type the program.
• Save the file with .m extension.
• Run the file by pressing Run or typing the filename in the command window.

Program:
clc;

Fs = input('Enter the sampling frequency (Hz): ');


fp = input('Enter the passband frequency (Hz): ');
fs = input('Enter the stopband frequency (Hz): ');
rp = input('Enter the passband ripple (dB): ');
rs = input('Enter the stopband ripple (dB): ');

% Normalized frequencies
wp = fp / (Fs / 2); % Passband edge frequency (normalized)
ws = fs / (Fs / 2); % Stopband edge frequency (normalized)

% Design Butterworth filter using "butter" function


[n, wn] = buttord(wp, ws, rp, rs); % Determine the filter order and cutoff
frequency
[b, a] = butter(n, wn, 'low'); % Design the low-pass Butterworth filter

% Plot the frequency response


freqz(b, a, 1024, Fs);

% Signal filtering example


t = 0:1/Fs:1; % Time vector
input_signal = cos(2 * pi * 50 * t) + cos(2 * pi * 300 * t); % Input: 50 Hz + 300
Hz components
output_signal = filter(b, a, input_signal); % Filter the signal

% Plot input and output signals


figure;
subplot(2, 1, 1);
plot(t, input_signal);
grid on;
title('Input Signal');
xlabel('Time (s)'); ylabel('Amplitude');

subplot(2, 1, 2);
plot(t, output_signal);
grid on;
title('Filtered Signal (Output)');
xlabel('Time (s)'); ylabel('Amplitude');

Output:
Enter the sampling frequency (Hz): 1000
Enter the passband frequency (Hz): 40
Enter the stopband frequency (Hz): 150
Enter the passband ripple (dB): 3
Enter the stopband ripple (dB): 60
Result:

The program for the design and implementation of a digital IIR Butterworth low pass filter to
meet the given specifications is written and the output is verified.
Experiment No. 12
Digital IIR Butterworth High Pass Filter

Aim: To write a program for the design and implementation of a digital IIR Butterworth high pass
filter to meet the given specifications

Software used: MATLAB

Theory: Write relevant theory for digital IIR Butterworth high pass filter.

Procedure:

• Open MATLAB.
• Click on New Script.
• In the editor, type the program.
• Save the file with .m extension.
• Run the file by pressing Run or typing the filename in the command window.

Program:
clc;

Fs = input('Enter the sampling frequency (Hz): ');


fp = input('Enter the passband frequency (Hz): ');
fs = input('Enter the stopband frequency (Hz): ');
rp = input('Enter the passband ripple (dB): ');
rs = input('Enter the stopband ripple (dB): ');

% Normalized frequencies
wp = fp / (Fs / 2); % Passband edge frequency (normalized)
ws = fs / (Fs / 2); % Stopband edge frequency (normalized)

% Design Butterworth filter using "butter" function


[n, wn] = buttord(wp, ws, rp, rs); % Determine the filter order and cutoff
frequency
[b, a] = butter(n, wn, 'high'); % Design the high-pass Butterworth filter

% Plot the frequency response


freqz(b, a, 1024, Fs);

% Signal filtering example


t = 0:1/Fs:1; % Time vector
input_signal = cos(2 * pi * 50 * t) + cos(2 * pi * 300 * t); % Input: 50 Hz + 300
Hz components
output_signal = filter(b, a, input_signal); % Filter the signal

% Plot input and output signals


figure;
subplot(2, 1, 1);
plot(t, input_signal);
grid on;
title('Input Signal');
xlabel('Time (s)'); ylabel('Amplitude');

subplot(2, 1, 2);
plot(t, output_signal);
grid on;
title('Filtered Signal (Output)');
xlabel('Time (s)'); ylabel('Amplitude');

Output:
Enter the sampling frequency (Hz): 1000
Enter the passband frequency (Hz): 40
Enter the stopband frequency (Hz): 150
Enter the passband ripple (dB): 3
Enter the stopband ripple (dB): 60
Result:

The program for the design and implementation of a digital IIR Butterworth high pass filter to
meet the given specifications is written and the output is verified.

You might also like