DSP Lab Manual
DSP Lab Manual
DSP Lab Manual
Belekere, Channapatna
V SEMESTER
BEC502
BATCH :____________________________
PREPARED BY
LATHAMANI H.N
ASSISTANT PROFESSOR
DEPARTMENT OF ECE
SITAR
LIST OF EXPERIMENTS
MATLAB
TOOL BOX
Signal Processing Image Processing
Statistics Control Systems
Neural Network Communications
It also allows you to put a list of your processing requests together in a file and save
that combined list with a name so that you can run all of those commands in the same
order at some later time. Furthermore, it allows you to run such lists of commands such
that you pass in data and/or get data back out (i.e. the list of commands is like a function in
most programming languages). Once you save a function, it becomes part of your toolbox
(i.e. it now looks to you as if it were part of the basic toolbox that you started with).
For those with computer programming backgrounds: Note that MATLAB runs as
aninterpretive language (like the old BASIC). That is, it does not need to be compiled. It
simply reads through each line of the function, executes it, and then goes on to the next
line. (In practice, a form of compilation occurs when you first run a function, so that it can
run faster the next time you run it.
MATLAB Windows:
MATLAB works with through three basic windows
Graphics window: the output of all graphics commands typed in the command window
are flushed to the graphics or figure window, a separate gray window with white
background color the user can create as many windows as the system memory will allow
Edit window: This is where you write edit, create and save your own programs in
filescalled M files.
INPUT-OUTPUT:
MATLAB supports interactive computation taking the input from the screen and
flushing, the output to the screen. In addition it can read input files and write output files
Data Type: the fundamental data –type in MATLAB is the array. It encompasses several
distinct data objects- integers, real numbers, matrices, character strings, structures and
cells.There is no need to declare variables as real or complex, MATLAB automatically sets
the variable to be real.
Dimensioning: Dimensioning is automatic in MATLAB. No dimension statements are
required for vectors or arrays .we can find the dimensions of an existing matrix or a vector
with the size and length commands.
1. T = 0: 1:10
This instruction indicates a vector T which as initial value 0 and final value 10
with an increment of 1
Therefore T = [0 1 2 3 4 5 6 7 8 9 10]
2. F= 20: 1: 100
Therefore F = [20 21 22 23 24 ……… 100]
3. T= 0:1/pi: 1
Therefore T= [0, 0.3183, 0.6366,
The above instruction creates a vector of one row and three columns whose values are
zero Output= [0 0 0]
5. ones (5,2)
The above instruction creates a vector of five rows and two
columns Output = 11
11
11
11
11
6. zeros (2, 4)
Output = 0000
0000
7. a = [ 1 2 3] , b = [4 5 6]
THEN a.*b = [4 10 18]
8. if C= [2 2 2] and b = [4 5 6]
b.*C results in [8 10 12]
9. plot (t, x)
If x = [6 7 8 9] and t = [1 2 3 4]; This instruction will display a figure window which
indicates the plot of x versus t
11. Subplot: This function divides the figure window into rows and columns Subplot (2 2 1)
divides the figure window into 2 rows and 2 columns 1 represent number of the figure
5. Filter:Syntax: y = filter(b,a,X)
Description:y = filter(b,a,X) filters the data in vector X with the filter described by
numerator coefficient vector b and denominator coefficient vector a. If a(1) is not equal to
1, filter normalizes the filter coefficients by a(1). If a(1) equals 0, filter returns an error.
9. Disp:Syntax: disp(X)
Description:disp(X) displays an array, without printing the array name. If X contains a
text string, the string is displayed.Another way to display an array on the screen is to
type its name, but this prints a leading "X=," which is not always desirable.Note that
disp does not display empty arrays.
Exponential sequence
n = -10:10;
a = 0.8; % Adjust the value of 'a' to change the decay rate
x = a.^n;
stem(n, x);
xlabel('Time index (n)');
ylabel('Amplitude');
title('Exponential Sequence');
grid on;
Sinusoidal sequence
n = 0:99;
A = 2; % Amplitude
w = pi/4; % Angular frequency
phi = pi/3; % Phase shift
x = A*cos(w*n + phi);
stem(n, x);
xlabel('Time index (n)');
ylabel('Amplitude');
title('Sinusoidal Sequence');
grid on;
n = -10:10;
x = zeros(size(n));
x(n == 0) = 1;
stem(n, x);
xlabel('Time index (n)');
ylabel('Amplitude');
title('Unit Sample Sequence');
grid on;
Random sequence
N = 20; % Specify the length of the random sequence
x = randn(1, N); % Generate a random sequence of N samples
stem(0:N-1, x); % Plot the random sequence
xlabel('Time index (n)');
ylabel('Amplitude');
title('Random Sequence');
grid on;
RESULT:
2. Program to perform the following operations on signals.
a) Signal addition, b) Signal multiplication, c)Scaling, d) Shifting, e)Folding
Signal addition
n = 0:10; % Define the time index range
x1 = 2*sin(0.5*pi*n); % Define the two input signals
x2 = 3*cos(0.2*pi*n);
y = x1 + x2; % Add the two signals
subplot(3,1,1); % Plot the input and output signals
stem(n, x1);
xlabel('Time index (n)');
ylabel('Amplitude');
title('Signal x1');
grid on;
subplot(3,1,2);
stem(n, x2);
xlabel('Time index (n)');
ylabel('Amplitude');
title('Signal x2');
grid on;
subplot(3,1,3);
stem(n, y);
xlabel('Time index (n)');
ylabel('Amplitude');
title('Sum of x1 and x2');
grid on;
Signal multiplication
subplot(3,1,2);
stem(n, x2);
xlabel('Time index (n)');
ylabel('Amplitude');
title('Signal x2');
grid on;
subplot(3,1,3);
stem(n, y);
xlabel('Time index (n)');
ylabel('Amplitude');
title('Product of x1 and x2');
grid on;
Signal Scaling
n = 0:10; % Define the time index range
x = sin(0.5*pi*n); % Define the input signal
alpha = 2; % Scaling factor
y = alpha * x; % Scale the signal
subplot(2,1,1); % Plot the input and output signals
stem(n, x);
xlabel('Time index (n)');
ylabel('Amplitude');
title('Original Signal');
grid on;
subplot(2,1,2);
stem(n, y);
xlabel('Time index (n)');
ylabel('Amplitude');
title('Scaled Signal');
grid on;
Time Shifting
subplot(2,1,2);
stem(n, y);
xlabel('Time index (n)');
ylabel('Amplitude');
title('Shifted Signal');
grid on;
Signal Folding
subplot(2,1,2);
stem(n, y);
xlabel('Time index (n)');
ylabel('Amplitude');
title('Folded Signal');
grid on;
RESULT:
3. Program to perform convolution of two given sequences (without using built-in
function) and display the signals.
x1 = [1 2 3]; % Define the two input sequences
x2 = [4 5 6];
N = length(x1) + length(x2) - 1; % Determine the length of the output sequence
y = zeros(1, N); % Initialize the output sequence
for n = 0:N-1 % Perform convolution
for k = 0:n
y(n+1) = y(n+1) + x1(k+1)*x2(n-k+1);
end
end
subplot(3,1,2);
stem(0:length(x2)-1, x2);
xlabel('Time index (n)');
ylabel('Amplitude');
title('Signal x2');
grid on;
subplot(3,1,3);
stem(0:N-1, y);
xlabel('Time index (n)');
ylabel('Amplitude');
title('Convolution of x1 and x2');
grid on;
RESULT:
4. Consider a causal system y(n) = 0.9y(n-1)+x(n).
Pole-Zero Plot: The pole of the system is at z = 0.9. There are no zeros.
To plot the magnitude and phase response, we substitute z = e^(jω) into H(z):
H(e^jω) = 1 / (1 - 0.9e^(-jω))
|H(e^jω)| = |1 / (1 - 0.9e^(-jω))|
The impulse response h(n) is the inverse Z-transform of H(z). For a system with a single pole at
z = a, the impulse response is given by:
MATLAB implementation
H = 1 ./ (1 - 0.9*exp(-1j*omega));
% Plot the magnitude response
figure;
plot(omega, abs(H));
xlabel('Frequency (rad/sample)');
ylabel('Magnitude |H(e^jω)|');
title('Magnitude Response');
figure;
plot(omega, angle(H));
xlabel('Frequency (rad/sample)');
ylabel('Phase ∠H(e^jω)');
title('Phase Response');
RESULT:
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) is a mathematical transform that converts a sequence of
N complex numbers into another sequence of N complex numbers. The DFT is defined as:
where:
MATLAB Implementation:
function X = my_dft(x)
N = length(x);
X = zeros(1, N);
for k = 0:N-1
for n = 0:N-1
end
end
end
% Example usage:
x = [1 2 3 4];
X = my_dft(x);
magnitude = abs(X);
phase = angle(X);
subplot(2,1,1);
stem(0:length(X)-1, magnitude);
xlabel('Frequency Bin');
ylabel('Magnitude');
title('Magnitude Spectrum');
grid on;
subplot(2,1,2);
stem(0:length(X)-1, phase);
xlabel('Frequency Bin');
ylabel('Phase (radians)');
title('Phase Spectrum');
grid on;
RESULT:
6. Using the DFT and IDFT, compute the following for any two given sequences
a)Circular convolution
b) Linear convolution
Circular Convolution
Method:
1. Zero-pad the sequences: Ensure both sequences have the same length, N. If they are
not, zero-pad the shorter sequence.
2. Compute the DFT of both sequences: Use the DFT formula or a built-in FFT function.
3. Multiply the DFT coefficients: Multiply the corresponding DFT coefficients of the two
sequences.
4. Compute the IDFT of the product: Use the IDFT formula or a built-in IFFT function
to obtain the circular convolution result.
Linear Convolution
Method:
1. Zero-pad the sequences: Pad the sequences with zeros to a length equal to the sum of
their lengths minus one.
2. Compute the DFT of both zero-padded sequences.
3. Multiply the DFT coefficients: Multiply the corresponding DFT coefficients of the two
sequences.
4. Compute the IDFT of the product: Use the IDFT to obtain the linear convolution
result.
MATLAB CODE
RESULT:
7. Verification of Linearity property, circular time shift property & circular frequency
shift property of DFT.
% Define a sequence
x = [1 2 3 4];
% DFT of x
X = fft(x);
% Linearity Verification
y = 2*x + 3*[5 6 7 8];
Y = fft(y);
linearity_check = abs(Y - (2*X + 3*fft([5 6 7 8]))) < 1e-10;
% Circular Time Shift Verification
x_shifted = circshift(x, 1);
X_shifted = fft(x_shifted);
time_shift_check = abs(X_shifted - exp(-1i*2*pi*(0:3)/4)*X) < 1e-10;
% Display results
disp('Linearity Verification:');
disp(linearity_check);
disp('Circular Time Shift Verification:');
disp(time_shift_check);
disp('Circular Frequency Shift Verification:');
disp(freq_shift_check);
RESULT:
8. Develop decimation in time radix-2 FFT algorithm without using built-in functions.
function X = dit_fft(x)
N = length(x);
if N == 1
X = x;
return;
end
% Bit-reversal permutation
x = bitrevorder(x);
RESULT:
9. Design and implementation of digital low pass FIR filter using a window to meet the given
specifications.
function y = lowpass_fir(x, fp, fs, Fs, As, Rp)
% Calculate normalized frequencies
wp = 2*pi*fp/Fs;
ws = 2*pi*fs/Fs;
RESULT: