Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views49 pages

DSP Lab Manual

The document is a lab manual for the Digital Signal Processing course at The Oxford College of Engineering, detailing the vision, mission, program outcomes, course outcomes, and specific experiments using MATLAB. It includes various signal processing experiments such as generating discrete time signals, performing operations on signals, and convolution of sequences. The manual emphasizes the integration of technical education with practical applications in electronics and communication engineering.
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)
15 views49 pages

DSP Lab Manual

The document is a lab manual for the Digital Signal Processing course at The Oxford College of Engineering, detailing the vision, mission, program outcomes, course outcomes, and specific experiments using MATLAB. It includes various signal processing experiments such as generating discrete time signals, performing operations on signals, and convolution of sequences. The manual emphasizes the integration of technical education with practical applications in electronics and communication engineering.
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/ 49

The Oxford College of Engineering, Bommanahalli, Bangalore

THE OXFORD COLLEGE OF ENGINEERING


Hosur Road, Bommanahalli, Bengaluru-560 068
Website:www.theoxford.edu Email : [email protected]

(Approved by AICTE, New Delhi, Accredited by NBA, New Delhi & Affiliated to VTU, Belgaum)

DEPARTMENT OF
ELECTRONICS & COMMUNICATION ENGINEERING

LAB MANUAL

Subject Name: Digital Signal Processing


Subject Code: BEC502
Semester: 5th
Subject Type: IPCC Lab

Prepared By: Ms. Remya Bharathy K.


Ms. Iffat Fatima
The Oxford College of Engineering, Bommanahalli, Bangalore
The Oxford College of Engineering, Bommanahalli, Bangalore
Vision

To emerge as a pioneer in the field of Electronics and Communication

Engineering, through excellence in technical education and research.

Mission

The Department of Electronics and Communication Engineering shall

 Provide a transformative educational experience focusing on disciplinary

knowledge, problem solving techniques and innovative projects.

 Excel in research and promote Industry-Academia interaction.

 Inculcate entrepreneurial traits in the student community, by fostering

managerial and leadership qualities.


The Oxford College of Engineering, Bommanahalli, Bangalore
PROGRAM OUTCOMES (POs):
Engineering knowledge: Apply the knowledge of mathematics, science, engineering
1 fundamentals, and an engineering specialization to the solution of complex engineering
problems
Problem analysis: Identify, formulate, review research literature, and analyze complex
2 engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences
Design/development of solutions: Design solutions for complex engineering problems
and design system components or processes that meet the specified needs with
3
appropriate consideration for the public health and safety, and the cultural, societal, and
environmental considerations
Conduct investigations of complex problems: Use research-based knowledge and
4 research methods including design of experiments, analysis and interpretation of data,
and synthesis of the information to provide valid conclusions
Modern tool usage: Create, select, and apply appropriate techniques, resources, and
5 modern engineering and IT tools including prediction and modeling to complex
engineering activities with an understanding of the limitations
The engineer and society: Apply reasoning informed by the contextual knowledge to
6 assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to the professional engineering practice
Environment and sustainability: Understand the impact of the professional engineering
7 solutions in societal and environmental contexts, and demonstrate the knowledge of,
and need for sustainable development
Ethics: Apply ethical principles and commit to professional ethics and responsibilities
8
and norms of the engineering practice
Individual and team work: Function effectively as an individual, and as a member or
9
leader in diverse teams, and in multidisciplinary settings
Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend
10
and write effective reports and design documentation, make effective presentations, and
give and receive clear instructions
Project management and finance: Demonstrate knowledge and understanding of the
11 engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and in multidisciplinary environments
Life-long learning: Recognize the need for, and have the preparation and ability to
12 engage in independent and life-long learning in the broadest context of technological
change
The Oxford College of Engineering, Bommanahalli, Bangalore

COURSE OUTCOMES (CO)

CO.1 Analyse the different types of signals and systems used in digital signal processing.

CO.2 Compute the response of an LTI system using time and frequency domain
techniques.

CO.3 Develop algorithms for the efficient computations of DFT and IDFT.

CO.4 Design of digital FIR filters for the given specifications using different window
methods.

CO.5 Design of digital IIR digital filters using bilinear transformation method.

PROGRAM SPECIFIC OUTCOMES (PSO)


An ability to understand the basic core courses of Electronics &
Communication and to relate them to various areas of application like
PSO 1
Communication Systems, control system, Signal processing, VLSI and
Embedded systems.
Should have capability to apply modern tools to analyze and solve complex
PSO 2
design with optimal solutions for various real-world applications
Excellent adaptability to changing work environment with good leadership
PSO 3
qualities and zeal for social and environmental well-being.

CO-PO MAPPING
PO P0- P0- P0- P0- P0- P0- P0- P0- P0- P0- P0- P0-
CO 1 2 3 4 5 6 7 8 9 10 11 12
CO1 2 1 - 1 - - - - - - - 1
CO2 3 2 2 1 - - - - - - - 1
CO3 2 1 - 1 - - - - - - - 1
CO4 3 2 2 1 - - - - - - - 1
CO5 2 1 - 1 - - - - - - - 1

CO-PSO MAPPING
CO PSO1 PSO2 PSO3
CO1 1 - -
CO2 1 - -
CO3 1 - -
CO4 1 - -
CO5 1 - -
The Oxford College of Engineering, Bommanahalli, Bangalore
Syllabus
The Oxford College of Engineering, Bommanahalli, Bangalore

MATLAB
MATLAB is a high-performance language for technical computing. It integrates computation,
visualization, and programming in an easy-to-use environment where problems and solutions are
expressed in familiar mathematical notation. Typical uses include: Math and computation .

General Procedure for using MATLAB


1. Go to MATLAB.
2. Select new script.

3. Write the MATLAB code in editor window.


4. Save and run the code
5. Check the output.
The Oxford College of Engineering, Bommanahalli, Bangalore

Experiment No:1

1. 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

a.Unit Sample Sequence: δ[n]=1 for n=0, and δ[n]=0 for all other values of n

n = -10:10; % Range for n


unit_sample = (n == 0); % 1 at n=0, 0 elsewhere
stem(n, unit_sample);
title('Unit Sample Sequence');
xlabel('n');
ylabel('Amplitude');
grid on;

b.Unit Step Sequence: u[n]=1 for n≥0, and u[n]=0 for n<0.

n = -10:10; % Range for n


unit_step = (n >= 0); % 1 for n >= 0, 0 for n < 0
figure;
stem(n, unit_step);
title('Unit Step Sequence');
xlabel('n');
ylabel('Amplitude');
grid on;

c.Exponential Sequence : x[n]=A⋅eαn


n = 0:10; % Range for n

8
The Oxford College of Engineering, Bommanahalli, Bangalore

A = 1; % Amplitude
alpha = 0.3; % Exponential rate
exp_seq = A * exp(alpha * n);
figure;
stem(n, exp_seq);
title('Exponential Sequence');
xlabel('n');
ylabel('Amplitude');
grid on;

d.Sinusoidal Sequence: x[n]=A⋅sin(ωn+ϕ)

n = 0:50; % Range for n


A = 1; % Amplitude
omega = pi/8; % Frequency in radians
phi = 0; % Phase
sin_seq = A * sin(omega * n + phi);
figure;
stem(n, sin_seq);
title('Sinusoidal Sequence');
xlabel('n');
ylabel('Amplitude');
grid on;

e.Random Sequence
n = 0:20; % Range for n
random_seq = rand(1, length(n)); % Generate random numbers between 0 and 1
figure;
stem(n, random_seq);
title('Random Sequence');
xlabel('n');
ylabel('Amplitude');
grid on;

9
The Oxford College of Engineering, Bommanahalli, Bangalore

Result:

10
The Oxford College of Engineering, Bommanahalli, Bangalore

11
The Oxford College of Engineering, Bommanahalli, Bangalore

Experiment No:2
2. Program to perform the following operations on signals.
a) Signal addition,
b) Signal multiplication,
c)Scaling,
d) Shifting,
e)Folding

a.Signal Addition
n = 0:10; % Time index

% Define two signals


x1 = sin(0.2 * pi * n); % Example sinusoidal signal 1
x2 = cos(0.2 * pi * n); % Example sinusoidal signal 2

% Signal Addition
x_add = x1 + x2;

% Plot
figure;
subplot(3,1,1);
stem(n, x1);
title('Signal 1: sin(0.2 \pi n)');
xlabel('n');
ylabel('Amplitude');

subplot(3,1,2);
stem(n, x2);
title('Signal 2: cos(0.2 \pi n)');
xlabel('n');
ylabel('Amplitude');

subplot(3,1,3);

12
The Oxford College of Engineering, Bommanahalli, Bangalore

stem(n, x_add);
title('Signal Addition: x_1[n] + x_2[n]');
xlabel('n');
ylabel('Amplitude');
grid on;

b.Signal Multiplication
% Signal Multiplication
x_mul = x1 .* x2; % Element-wise multiplication

% Plot
figure;
subplot(3,1,1);
stem(n, x1);
title('Signal 1: sin(0.2 \pi n)');
xlabel('n');
ylabel('Amplitude');
grid on;

subplot(3,1,2);
stem(n, x2);
title('Signal 2: cos(0.2 \pi n)');
xlabel('n');
ylabel('Amplitude');
grid on;

subplot(3,1,3);
stem(n, x_mul);
title('Signal Multiplication: x_1[n] \cdot x_2[n]');
xlabel('n');
ylabel('Amplitude');
grid on;

c.Scaling : scale x1[n] by a factor of 2.

13
The Oxford College of Engineering, Bommanahalli, Bangalore

% Signal Scaling
scale_factor = 2;
x_scaled = scale_factor * x1;

% Plot
figure;
subplot(2,1,1);
stem(n, x1);
title('Original Signal: sin(0.2 \pi n)');
xlabel('n');
ylabel('Amplitude');
grid on;

subplot(2,1,2);
stem(n, x_scaled);
title('Scaled Signal: 2 \cdot sin(0.2 \pi n)');
xlabel('n');
ylabel('Amplitude');
grid on;

d.Shifting: shift x1[n] by 3 units


% Signal Shifting
shift_units = 3;
x_shifted = [zeros(1, shift_units), x1(1:end-shift_units)];

% Plot
figure;
subplot(2,1,1);
stem(n, x1);
title('Original Signal: sin(0.2 \pi n)');
xlabel('n');
ylabel('Amplitude');
grid on;

14
The Oxford College of Engineering, Bommanahalli, Bangalore

subplot(2,1,2);
stem(n, x_shifted);
title('Shifted Signal: sin(0.2 \pi (n-3))');
xlabel('n');
ylabel('Amplitude');
grid on;

e.Folding (Time Reversal): flipping the signal around n=0.


% Signal Folding
x_folded = fliplr(x1);

% Plot
figure;
subplot(2,1,1);
stem(n, x1);
title('Original Signal: sin(0.2 \pi n)');
xlabel('n');
ylabel('Amplitude');
grid on;

subplot(2,1,2);
stem(n, x_folded);
title('Folded Signal: sin(0.2 \pi (-n))');
xlabel('n');
ylabel('Amplitude');
grid on;

15
The Oxford College of Engineering, Bommanahalli, Bangalore

Results:

16
The Oxford College of Engineering, Bommanahalli, Bangalore

17
The Oxford College of Engineering, Bommanahalli, Bangalore

Experiment No:3
3. Program to perform convolution of two given sequences (without using built-in function)
and display the signals

% Define two sequences


x1 = [1, 2, 8, 4, 5, 7]; % First sequence
x2 = [1, -1, 2, 0]; % Second sequence

% Lengths of the two sequences


N1 = length(x1);
N2 = length(x2);

% The length of the output sequence (N1 + N2 - 1)


N_out = N1 + N2 - 1;

% Initialize the output sequence (convolution result) to zeros


y = zeros(1, N_out);

% Perform convolution manually


for i = 1:N_out

18
The Oxford College of Engineering, Bommanahalli, Bangalore

for j = 1:N1
if (i - j + 1 > 0 && i - j + 1 <= N2)
y(i) = y(i) + x1(j) * x2(i - j + 1);
end
end
end

% Time index for plotting


n1 = 0:N1-1; % Time indices for x1
n2 = 0:N2-1; % Time indices for x2
n_out = 0:N_out-1; % Time indices for the convolution result

% Plot the first sequence


figure;
subplot(3,1,1);
stem(n1, x1, 'filled');
title('First Sequence (x1[n])');
xlabel('n');
ylabel('Amplitude');
grid on;

% Plot the second sequence


subplot(3,1,2);
stem(n2, x2, 'filled');
title('Second Sequence (x2[n])');
xlabel('n');
ylabel('Amplitude');
grid on;

% Plot the convolution result


subplot(3,1,3);
stem(n_out, y, 'filled');
title('Convolution Result (y[n] = x1[n] * x2[n])');
xlabel('n');
ylabel('Amplitude');
19
The Oxford College of Engineering, Bommanahalli, Bangalore

grid on;
Results:

Experiment No:4
Consider a causal system y(n) = 0.9y(n-1) +x(n).
a) Determine H(z) and sketch its pole zero plot.
b) Plot |H(ejω)| and ∠ H(ejω)
c) Determine the impulse response h(n).

We are given a causal discrete-time system with the difference equation:

This system represents a recursive relationship where the current output depends on the previous
output and the current input.
Determine H(z)H(z)H(z) and sketch its pole-zero plot

20
The Oxford College of Engineering, Bommanahalli, Bangalore

% Define the zero and pole


zero = [0]; % Zero at the origin
pole = [0.9]; % Pole at z = 0.9

% Plot the pole-zero plot


figure;
zplane(zero, pole);
title('Pole-Zero Plot');
xlabel('Real Part');
ylabel('Imaginary Part');

21
The Oxford College of Engineering, Bommanahalli, Bangalore

grid on;
Plot |H(ejω)| and ∠ H(ejω)

% Frequency range
omega = linspace(-pi, pi, 1000);

% Calculate H(e^jω) for each omega


H_ejw = exp(1j*omega) ./ (exp(1j*omega) - 0.9);

% Magnitude response |H(e^jω)|


magnitude_response = abs(H_ejw);

% Phase response ∠ H(e^jω)


phase_response = angle(H_ejw);

% Plot magnitude response


figure;
subplot(2,1,1);
plot(omega, magnitude_response);
title('Magnitude Response |H(e^{j\omega})|');
xlabel('\omega');
ylabel('|H(e^{j\omega})|');
grid on;

% Plot phase response


subplot(2,1,2);
plot(omega, phase_response);

22
The Oxford College of Engineering, Bommanahalli, Bangalore

title('Phase Response ∠ H(e^{j\omega})');


xlabel('\omega');
ylabel('∠ H(e^{j\omega})');
grid on;

Determine the impulse response h(n).

n = 0:20; % Time indices


h_n = 0.9.^n; % Impulse response

% Plot impulse response


figure;
stem(n, h_n, 'filled');
title('Impulse Response h(n) = 0.9^n u(n)');
xlabel('n');
ylabel('h(n)');
grid on;
Results:

23
The Oxford College of Engineering, Bommanahalli, Bangalore

24
The Oxford College of Engineering, Bommanahalli, Bangalore

Experiment No:5
Computation of N point DFT of a given sequence (without using built-in function) and to
plot the magnitude and phase spectrum.

The Discrete Fourier Transform (DFT) of a sequence can be computed manually without using the
built-in fft function. Here's how to compute the N-point DFT of a given sequence and plot its
magnitude and phase spectra.

% Given sequence
x = [1, 2, 3, 4, 8, 3, 1]; % Example input sequence
N = length(x); % Length of the sequence (can modify this for N-point DFT)

% Initialize the DFT result


X = zeros(1, N);

% Compute the DFT manually


for k = 0:N-1
for n = 0:N-1
X(k+1) = X(k+1) + x(n+1) * exp(-1j * 2 * pi * k * n / N);
end
end

% Magnitude and Phase


magnitude_spectrum = abs(X); % Magnitude of the DFT
phase_spectrum = angle(X); % Phase of the DFT

% Frequency axis for plotting


k = 0:N-1;

25
The Oxford College of Engineering, Bommanahalli, Bangalore

% Plot the Magnitude Spectrum


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

% Plot the Phase Spectrum


subplot(2,1,2);
stem(k, phase_spectrum, 'filled');
title('Phase Spectrum ∠X[k]');
xlabel('Frequency Index (k)');
ylabel('∠X[k] (radians)');
grid on;

26
The Oxford College of Engineering, Bommanahalli, Bangalore

Results:

Experiment No:6
Using the DFT and IDFT, compute the following for any two given sequences
a) Circular convolution
b) Linear convolution

Circular Convolution:
 The sequences are zero-padded to the length of the longest sequence.
 The DFT of both sequences is computed using the fft() function.
 The element-wise product of the DFTs is taken to perform circular convolution.
 Finally, the IDFT of the result is computed using ifft() to get the circular convolution
output.
Linear Convolution:
 The sequences are zero-padded to the length N1+N2−1 to avoid overlap and to ensure linear
convolution.
 The DFT of both zero-padded sequences is computed.
 The element-wise product of the DFTs is taken, followed by the IDFT of the result to
obtain the linear convolution.
% Define two sequences
x = [1, 2, 3]; % First sequence
h = [4, 5, 6]; % Second sequence

% Lengths of the two sequences


27
The Oxford College of Engineering, Bommanahalli, Bangalore

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

%% Circular Convolution
% Choose N as the maximum length between the two sequences
N_circular = N1+N2-1;

% Zero-pad both sequences to length N_circular


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

% Perform DFT on both sequences


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

% Multiply DFTs (element-wise multiplication)


Y_circular = X .* H;

% Compute IDFT to get the circular convolution result


y_circular = ifft(Y_circular);

% Plot circular convolution result


figure;
stem(0:N_circular-1, real(y_circular), 'filled');
title('Circular Convolution Result');
xlabel('n');
ylabel('y_{circular}[n]');
grid on;

%% Linear Convolution
% Choose N for linear convolution as N1 + N2 - 1 (full length)
N_linear = N1 + N2 - 1;

% Zero-pad both sequences to length N_linear


x_pad = [x, zeros(1, N_linear - N1)];
28
The Oxford College of Engineering, Bommanahalli, Bangalore

h_pad = [h, zeros(1, N_linear - N2)];

% Perform DFT on both sequences (length N_linear)


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

% Multiply DFTs (element-wise multiplication)


Y_linear = X .* H;

% Compute IDFT to get the linear convolution result


y_linear = ifft(Y_linear);

% Plot linear convolution result


figure;
stem(0:N_linear-1, real(y_linear), 'filled');
title('Linear Convolution Result');
xlabel('n');
ylabel('y_{linear}[n]');
grid on;
Results:

29
The Oxford College of Engineering, Bommanahalli, Bangalore

Experiment No:7.
Verification of Linearity property, circular time shift property & circular frequency shift
property of DFT.

30
The Oxford College of Engineering, Bommanahalli, Bangalore

% Define two sequences


x = [1, 2, 3, 4]; % First sequence
y = [4, 3, 2, 1]; % Second sequence

% Length of sequences
N = length(x);

%% Linearity Property
a = 2; % Constant multiplier for x
b = 3; % Constant multiplier for y

% DFT of x and y
X = fft(x);
Y = fft(y);

% Left side: DFT of (a*x + b*y)


left_side = fft(a * x + b * y);

% Right side: a*DFT(x) + b*DFT(y)


right_side = a * X + b * Y;

% Verification of linearity
disp('Linearity Property Verification:');
disp('DFT of (a*x + b*y):');
disp(left_side);
disp('a*DFT(x) + b*DFT(y):');
disp(right_side);

%% Circular Time Shift Property


m = 1; % Shift amount

% Left side: DFT of circularly time-shifted x


x_shifted = circshift(x, -m); % Circularly shift x by m positions
left_side_time_shift = fft(x_shifted);

31
The Oxford College of Engineering, Bommanahalli, Bangalore

% Right side: X[k] * exp(-j*2*pi*k*m/N)


k = 0:N-1;
twiddle_factor = exp(-1j * 2 * pi * k * m / N);
right_side_time_shift = X .* twiddle_factor;

% Verification of circular time shift


disp('Circular Time Shift Property Verification:');
disp('DFT of circularly time-shifted x:');
disp(left_side_time_shift);
disp('X[k] * exp(-j*2*pi*k*m/N):');
disp(right_side_time_shift);

%% Circular Frequency Shift Property


m = 1; % Frequency shift amount

% Left side: DFT of frequency-shifted x


n = 0:N-1;
x_freq_shifted = x .* exp(1j * 2 * pi * m * n / N); % Frequency shift in time domain
left_side_freq_shift = fft(x_freq_shifted);

% Right side: Circularly shifted X[k] in the frequency domain


right_side_freq_shift = circshift(X, -m);
% Verification of circular frequency shift
disp('Circular Frequency Shift Property Verification:');
disp('DFT of frequency-shifted x:');
disp(left_side_freq_shift);
disp('Circularly shifted X[k]:');
disp(right_side_freq_shift);
Results:
1] Linearity Property :-
Linearity Property Verification:
DFT of (a*x + b*y):
50.0000 + 0.0000i 2.0000 - 2.0000i 2.0000 + 0.0000i 2.0000 + 2.0000i
a*DFT(x) + b*DFT(y):
50.0000 + 0.0000i 2.0000 - 2.0000i 2.0000 + 0.0000i 2.0000 + 2.0000i

2] Circular Time Shift Property :-


Circular Time Shift Property Verification:
DFT of circularly time-shifted x:
10.0000 + 0.0000i -2.0000 - 2.0000i 2.0000 + 0.0000i -2.0000 + 2.0000i
32
The Oxford College of Engineering, Bommanahalli, Bangalore

X[k] * exp(-j*2*pi*k*m/N):
10.0000 + 0.0000i 2.0000 + 2.0000i 2.0000 + 0.0000i 2.0000 - 2.0000i

3] Circular Frequency Shift Property :-


Circular Frequency Shift Property Verification:
DFT of frequency-shifted x:
-2.0000 - 2.0000i 10.0000 - 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i
Circularly shifted X[k]:
-2.0000 + 2.0000i -2.0000 + 0.0000i -2.0000 - 2.0000i 10.0000 + 0.0000i

Experiment No 8
Develop decimation in time radix-2 FFT algorithm without using built-in functions.
The Decimation in Time (DIT) Radix-2 Fast Fourier Transform (FFT) is an efficient algorithm for
computing the Discrete Fourier Transform (DFT) by recursively breaking down the DFT of a
sequence into smaller DFTs. This algorithm is designed for sequences whose length NNN is a
power of 2. Here is a step-by-step explanation and MATLAB implementation of the DIT Radix-2
FFT without using built-in functions.
Steps of the DIT Radix-2 FFT Algorithm
1. Bit-Reversal Reordering: First, rearrange the input sequence into bit-reversed order to
prepare for the butterfly calculations.
2. Butterfly Calculations: Use butterfly operations to combine the results of smaller DFTs
into larger ones.
3. Recursive Decimation in Time: Process the even and odd parts of the signal separately at
each stage and combine them using the butterfly structure.
close all
clc
% dit_radix2_fft.m
x = [1, 2, 3, 4, 5, 6, 7, 8]; % Input signal (length should be a power of 2)
X = dit_radix2_fft(x); % Compute DFT using custom FFT
disp('Input Sequence');
disp(x)
disp('DFT of the input sequence:');
disp(X);

function X = dit_radix2_fft(x)
% DIT Radix-2 FFT Implementation without built-in functions
% Input: x - input sequence (length must be a power of 2)
% Output: X - DFT of the input sequence

% Check if length of x is a power of 2


N = length(x);
33
The Oxford College of Engineering, Bommanahalli, Bangalore

if mod(log2(N), 1) ~= 0
error('Length of input sequence must be a power of 2');
end

% Bit-reversal ordering
X = bit_reverse_reorder(x);

% FFT computation using the DIT Radix-2 algorithm


% Number of stages (log2(N))
stages = log2(N);

for s = 1:stages
% Number of butterflies in this stage
m = 2^s;
% Twiddle factor (complex exponential for FFT)
wm = exp(-2i * pi / m);

for k = 1:m:N % Process each group of size 'm'


w = 1; % Twiddle factor starting value
for j = 0:(m/2)-1
% Butterfly calculations
t = w * X(k + j + m/2);
u = X(k + j);

% Update values for FFT


X(k + j) = u + t;
X(k + j + m/2) = u - t;

% Update twiddle factor


w = w * wm;
end
end
end
end

34
The Oxford College of Engineering, Bommanahalli, Bangalore

function x_reordered = bit_reverse_reorder(x)


% Bit-reversal reordering of input sequence
N = length(x);
x_reordered = zeros(1, N);

% Compute bit-reversal indices


for k = 0:N-1
reversed_index = bit_reverse(k, log2(N));
x_reordered(reversed_index + 1) = x(k + 1);
end
end

function r = bit_reverse(k, num_bits)


% Bit-reverse the integer k for num_bits bits
r = 0;
for i = 1:num_bits
r = r * 2 + mod(k, 2);
k = floor(k / 2);
end
end
Results:
Input Sequence
1 2 3 4 5 6 7 8
DFT of the input sequence:
36.0000 + 0.0000i -4.0000 + 9.6569i -4.0000 + 4.0000i -4.0000 + 1.6569i -4.0000 + 0.0000i -4.0000 - 1.6569i -
4.0000 - 4.0000i -4.0000 - 9.6569i

Experiment No:9.
Design and implementation of digital low pass FIR filter using a window to meet the given
specifications
Step-by-Step Design
1. Filter Specifications: Define key parameters:
 Sampling Frequency (fs): Determines the Nyquist frequency (fN=fs/2).
 Cutoff Frequency (fc): The desired maximum frequency to pass.
 Filter Order (N): Determines the sharpness of the transition band.
 Window Type: Choose a window function (e.g., Hamming, Hanning, Kaiser).
2. Window Method:
35
The Oxford College of Engineering, Bommanahalli, Bangalore

 Use the ideal impulse response of the desired filter.


 Multiply it with the selected window function to suppress ripples.
3. Normalized Frequency: Cutoff frequency is normalized with respect to the Nyquist
frequency.
4. Filter Implementation: Use MATLAB's built-in functions to compute the filter
coefficients, and then plot the impulse and frequency responses.

MATLAB Implementation
Example Specifications
 Sampling Frequency (fs=2000 Hz)
 Cutoff Frequency (fc=300 Hz)
 Filter Order (N=50)
 Window: Hamming

% Specifications
fs = 2000; % Sampling frequency in Hz
fc = 300; % Cutoff frequency in Hz
N = 50; % Filter order (should be even for linear phase)
Wn = fc / (fs / 2); % Normalized cutoff frequency

% Design FIR filter using Hamming window


b = fir1(N, Wn, 'low', hamming(N + 1)); % Filter coefficients

% Plot the impulse response


figure;
stem(0:N, b, 'filled');
title('Impulse Response of FIR Low-Pass Filter');
xlabel('n');
ylabel('Amplitude');
grid on;

% Frequency response
[H, f] = freqz(b, 1, 1024, fs); % 1024-point FFT for frequency response

% Plot magnitude response

36
The Oxford College of Engineering, Bommanahalli, Bangalore

figure;
plot(f, abs(H));
title('Magnitude Response of FIR Low-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

% Plot phase response


figure;
plot(f, angle(H));
title('Phase Response of FIR Low-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
grid on;
Explanation
1. Normalized Cutoff Frequency: Wn=fc /fN , where fN=fs/2.
2. Filter Design: The fir1 function generates filter coefficients. The 'low' argument specifies a
low-pass filter, and the Hamming window is applied.
3. Impulse Response: Visualizes the filter in the time domain.
4. Frequency Response: The magnitude and phase responses show how the filter attenuates
signals at different frequencies.
Choosing the Window
 Hamming: Good balance of mainlobe width and sidelobe attenuation.
 Hanning: Similar to Hamming but with slightly more attenuation.
 Kaiser: Offers flexibility with a parameter (β) to trade-off mainlobe width and sidelobe
level.
Replace hamming(N + 1) in the code with:
 hann(N + 1) for Hanning.
 kaiser(N + 1, beta) for Kaiser, where beta is the parameter (e.g., β=5).

37
The Oxford College of Engineering, Bommanahalli, Bangalore

Results:

38
The Oxford College of Engineering, Bommanahalli, Bangalore

Experiment No:10.
Design and implementation of digital high pass FIR filter using a window to meet the given
specifications.
Steps for High-Pass FIR Filter Design
1. Filter Specifications:
 Sampling frequency (fs)
 Cutoff frequency (fc) above which frequencies should pass
 Filter order (N), affecting transition width and ripple
 Window type (e.g., Hamming, Hanning, Kaiser)
2. Normalized Frequency:
 Normalize the cutoff frequency to the Nyquist frequency: Wn=fc/(fs/2).
3. Ideal Impulse Response:
 High-pass filter's impulse response is derived from the sinc function by subtracting
the low-pass response from an impulse (δ[n]−sinc(t)).
4. Window Application:
 Multiply the ideal impulse response by a window function to reduce ripples.
5. Implementation and Plotting:
 Compute and plot the impulse response, magnitude response, and phase response.

MATLAB Code
Example Specifications
 Sampling Frequency (fs=2000Hz)
 Cutoff Frequency (fc=500Hz)
 Filter Order (N=50)
 Window: Hamming

% Filter Specifications
fs = 2000; % Sampling frequency in Hz
fc = 500; % Cutoff frequency in Hz
N = 50; % Filter order (even for linear phase FIR)
Wn = fc / (fs / 2); % Normalized cutoff frequency

% Design High-Pass FIR Filter using Hamming Window


b = fir1(N, Wn, 'high', hamming(N + 1)); % Filter coefficients

39
The Oxford College of Engineering, Bommanahalli, Bangalore

% Plot Impulse Response


figure;
stem(0:N, b, 'filled');
title('Impulse Response of High-Pass FIR Filter');
xlabel('n');
ylabel('Amplitude');
grid on;

% Frequency Response
[H, f] = freqz(b, 1, 1024, fs); % Frequency response using 1024-point FFT

% Plot Magnitude Response


figure;
plot(f, abs(H));
title('Magnitude Response of High-Pass FIR Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

% Plot Phase Response


figure;
plot(f, angle(H));
title('Phase Response of High-Pass FIR Filter');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
grid on;
Code Explanation
1. fir1 Function:
 'high': Specifies a high-pass filter.
 hamming(N + 1): Applies the Hamming window to the filter's impulse response.
2. Impulse Response Plot:
 Shows the time-domain representation of the FIR filter.
3. Frequency Response:
 Magnitude response displays attenuation in the stopband and amplification in the
passband.

40
The Oxford College of Engineering, Bommanahalli, Bangalore

 Phase response confirms the linear phase behaviour of FIR filters.

Alternative Window Functions


Replace hamming(N + 1) with:
 hann(N + 1) for Hanning.
 blackman(N + 1) for Blackman.
 kaiser(N + 1, beta) for Kaiser, with β controlling the trade-off between ripple and transition
width.

Adjusting the Filter Order


 Higher Order (N): Sharper transition band but more computational effort.
 Lower Order (N): Wider transition band but simpler implementation.
Results:

41
The Oxford College of Engineering, Bommanahalli, Bangalore

Experiment No:11.
Design and implementation of digital IIR Butterworth low pass filter to meet the given
specifications.

An IIR Butterworth filter has a maximally flat frequency response in the passband and is
commonly used for applications where a smooth and monotonic filter response is required.

Specifications to Define
1. Filter Type: Low-pass.
2. Filter Order (N): Determines the roll-off steepness.
3. Cutoff Frequency (fc): The frequency below which signals pass.
4. Sampling Frequency (fs): For digital implementation.
5. Passband and Stopband Characteristics:
 Passband ripple (ϵ).
 Stopband attenuation (As).
Steps to Design Butterworth Filter
1. Convert Specifications to Digital Domain:
 Normalize the cutoff frequency: Wn=fc/(fs/2).
2. Determine Filter Order:
 Based on passband ripple and stopband attenuation.
3. Compute Filter Coefficients:
 Use the butter function in MATLAB.
4. Analyze the Filter:
 Plot frequency response (magnitude and phase).
42
The Oxford College of Engineering, Bommanahalli, Bangalore

 Plot impulse and step responses.

MATLAB Implementation
Example Specifications
 Sampling Frequency (fs=1000Hz)
 Cutoff Frequency (fc=200Hz)
 Filter Order (N=4)

% Specifications
fs = 1000; % Sampling frequency (Hz)
fc = 200; % Cutoff frequency (Hz)
N = 4; % Filter order
Wn = fc / (fs / 2); % Normalized cutoff frequency

% Design Butterworth Low-Pass Filter


[b, a] = butter(N, Wn, 'low'); % Compute filter coefficients

% Frequency Response
[H, f] = freqz(b, a, 1024, fs); % Frequency response

% Plot Magnitude Response


figure;
plot(f, abs(H));
title('Magnitude Response of Butterworth Low-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

% Plot Phase Response


figure;
plot(f, angle(H));
title('Phase Response of Butterworth Low-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
grid on;
43
The Oxford College of Engineering, Bommanahalli, Bangalore

% Impulse Response
impulse = [1, zeros(1, 49)]; % Delta function (impulse)
h = filter(b, a, impulse); % Impulse response
figure;
stem(0:49, h, 'filled');
title('Impulse Response of Butterworth Low-Pass Filter');
xlabel('n');
ylabel('Amplitude');
grid on;

% Step Response
step = ones(1, 50); % Unit step function
s = filter(b, a, step); % Step response
figure;
stem(0:49, s, 'filled');
title('Step Response of Butterworth Low-Pass Filter');
xlabel('n');
ylabel('Amplitude');
grid on;
Results:

44
The Oxford College of Engineering, Bommanahalli, Bangalore

45
The Oxford College of Engineering, Bommanahalli, Bangalore

Experiment No:12.
Design and implementation of digital IIR Butterworth high pass filter to meet the given
specifications

Steps for Designing Butterworth High-Pass Filter


1. Define Filter Specifications:
 Sampling Frequency (fs): The sampling rate of the system.
 Cutoff Frequency (fc): Frequency above which signals are passed.
 Filter Order (N): Determines the sharpness of the transition band.
 Normalized Frequency (Wn): Wn=fc/(fs/2).
2. Compute Filter Coefficients:
 Use MATLAB's butter function for the Butterworth filter.
 Specify the filter type as 'high'.
3. Analyze Filter:
 Frequency response (magnitude and phase).
 Impulse and step responses.

MATLAB Implementation
Example Specifications:
 Sampling Frequency: fs=1000Hz
 Cutoff Frequency: fc=200 Hz
 Filter Order: N=4
% Specifications
fs = 1000; % Sampling frequency (Hz)
fc = 200; % Cutoff frequency (Hz)
N = 4; % Filter order
Wn = fc / (fs / 2); % Normalized cutoff frequency

% Design Butterworth High-Pass Filter


[b, a] = butter(N, Wn, 'high'); % Filter coefficients

% Frequency Response
[H, f] = freqz(b, a, 1024, fs); % Frequency response

% Plot Magnitude Response

46
The Oxford College of Engineering, Bommanahalli, Bangalore

figure;
plot(f, abs(H));
title('Magnitude Response of Butterworth High-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

% Plot Phase Response


figure;
plot(f, angle(H));
title('Phase Response of Butterworth High-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
grid on;

% Impulse Response
impulse = [1, zeros(1, 49)]; % Delta function (impulse)
h = filter(b, a, impulse); % Impulse response
figure;
stem(0:49, h, 'filled');
title('Impulse Response of Butterworth High-Pass Filter');
xlabel('n');
ylabel('Amplitude');
grid on;

% Step Response
step = ones(1, 50); % Unit step function
s = filter(b, a, step); % Step response
figure;
stem(0:49, s, 'filled');
title('Step Response of Butterworth High-Pass Filter');
xlabel('n');
ylabel('Amplitude');
grid on;

47
The Oxford College of Engineering, Bommanahalli, Bangalore

Key Points in the Code


1. butter(N, Wn, 'high'):
 Computes the coefficients for a high-pass Butterworth filter.
 N: Filter order.
 Wn: Normalized cutoff frequency.
2. Frequency Response:
 freqz calculates the frequency response, showing magnitude and phase plots.
3. Impulse Response:
 The response of the filter to a delta input signal.
4. Step Response:
 The response of the filter to a unit step input.

Customizations
1. Stopband Attenuation:
 Increase N to achieve higher stopband attenuation but at the cost of computational
complexity.
2. Different Filter Types:
 Replace 'high' with 'low', 'bandpass', or 'stop' for other filter types.
3. Windowed Design:
 For a finite impulse response (FIR) alternative, consider designing using window
methods (like Hamming or Kaiser).
Results:

48
The Oxford College of Engineering, Bommanahalli, Bangalore

49

You might also like