LAB INDEX
SL NO Experiment Name Page
1 Introduction to MATLAB Software.
2 Analog to Digital Signal Conversion Process
Using MATLAB.
3 Testing Standard Different Discrete-Time
Signals Using MATLAB.
4 Study of Periodic and Aperiodic Discrete-Time
Signals using MATLAB.
5 Implementation of LP IIR Filter.
6 Implementation of LP FIR Filter.
7 Discrete time Fourier transform (DTFT)
computation.
8 Implementation of Decimation Process and
Implementation of Interpolation Process.
Experiment No: 01
Experiment Name: Introduction to MATLAB Software.
Objectives:
➢ To learn the basic interface and functionalities of MATLAB.
➢ To practice writing and executing basic mathematical equations in MATLAB.
➢ To explore the use of MATLAB for solving mathematical problems.
Different types of equations with output:
Code
Linear Function:
clc;
clear all;
close all;
x = -10:0.1:10; % Range of x
y = 2*x + 5;
stem(x, y);
title('y = 2x + 5');
xlabel('x');
ylabel('y');
Logarithmic Function:
x = 0.1:0.1:10; % Range of x (avoiding log(0))
y = log(x);
stem(x, y);
title('y = log(x)');
xlabel('x');
ylabel('y');
Circular Motion (Parametric Equation):
clc;
clear all;
close all;
t = 0:0.05:2*pi; % Time range
x = cos(t);
y = sin(t);
stem(x, y);
axis equal;
% Keep axis ratio equal
title('Circular Motion');
xlabel('x');
ylabel('y');
grid on;
Gaussian Function:
x = -5:0.1:5;
y = exp(-x.^2);
plot(x, y);
% Range of x
% Plot the Gaussian curve
title('Gaussian Function: y = e^{-x^2}');
xlabel('x');
ylabel('y');
Step Function (Heaviside Function):
x = -5:0.1:5;
% Range of x
y = double(x >= 0);
plot(x, y);
% Plot the step function
title('Step Function (Heaviside)');
xlabel('x');
ylabel('y');
Experiment No: 02
Experiment Name: Analog to Digital Signal Conversion Process Using MATLAB.
Objective:
1. To understand the fundamental concepts of Analog-to-Digital Conversion (ADC).
2. To simulate the ADC process in MATLAB, including sampling, quantization, and binary encoding.
3. To observe the effects of different sampling rates and quantization levels on the accuracy of the digital signal.
4. To analyze and visualize how analog signals are converted into discrete digital signals for digital processing and
applications.
Code:
clc;
clear all;
close all;
% Analog Signal Generation
t = 0:0.001:1; f = 5; analog_signal = sin(2*pi*f*t);
% Sampling
fs = 20; ts = 0:1/fs:1; sampled_signal = sin(2*pi*f*ts);
% Quantization
n_bits = 3; levels = 2^n_bits; quantized_signal = round(sampled_signal *
(levels/2)) / (levels/2);
% Coding
binary_codes = dec2bin(floor((quantized_signal + 1) * (levels/2)), n_bits);
% Plotting
subplot(3,1,1); plot(t, analog_signal); title('Analog Signal');
subplot(3,1,2); stem(ts, sampled_signal, 'g'); title('Sampled Signal');
subplot(3,1,3); stem(ts, quantized_signal, 'r'); title('Quantized Signal');
% Display Results
disp('Quantized Values and Corresponding Binary Codes:');
disp(table(quantized_signal', cellstr(binary_codes), 'VariableNames',
{'QuantizedValue', 'BinaryCode'}));
Experiment No: 03
Experiment Name: Testing Standard Different Discrete-Time Signals Using MATLAB.
Objective:
1. To generate and analyze standard discrete-time signals in MATLAB.
2. To observe the characteristics of different signals in the discrete domain.
3. To understand the significance of each signal in DSP applications.
Code:
clc;
clear all;
close all;
n = -10:10; % Time index
delta = (n == 0);
subplot(3,2,1);
stem(n, delta, 'r', 'filled');
title('Unit Impulse/Sample Signal');
xlabel('n');
ylabel('Amplitude');
grid on;
u = (n >= 0);
subplot(3,2,2);
stem(n, u, 'b', 'filled');
title('Unit Step Signal');
xlabel('n');
ylabel('Amplitude');
grid on;
r = (n .* (n >= 0));
subplot(3,2,3);
stem(n, r, 'g', 'filled');
title('Ramp Signal');
xlabel('n');
ylabel('Amplitude');
grid on;
alpha = 0.8;
x_exp = alpha.^n .* (n >= 0);
subplot(3,2,4);
stem(n, x_exp, 'm', 'filled');
title('Exponential Signal');
xlabel('n');
ylabel('Amplitude');
grid on;
Experiment No: 04
Experiment Name: Study of Periodic and Aperiodic Discrete-Time Signals using MATLAB
Objective:
1. To generate periodic and aperiodic discrete-time signals in MATLAB.
2. To plot and compare their characteristics.
3. To understand the repeating nature of periodic signals.
4. To observe the decay behavior of aperiodic signals.
Code:
% ----- Define index range -----
n = -20:20;
% ----- Periodic signal example -----
% Discrete-time sinusoid: cos(0.2*pi*n)
x_periodic = cos(0.2*pi*n);
% ----- Aperiodic signal example -----
% Exponential decay: (0.9).^n (n>=0)
x_aperiodic = (n >= 0) .* (0.9).^n;
% ----- Plot Periodic Signal -----
subplot(2,1,1);
stem(n, x_periodic, 'filled');
title('Periodic Signal: cos(0.2 \pi n)');
xlabel('n'); ylabel('x[n]');
grid on;
% ----- Plot Aperiodic Signal -----
subplot(2,1,2);
stem(n, x_aperiodic, 'filled', 'r');
title('Aperiodic Signal: (0.9)^n u[n]');
xlabel('n'); ylabel('x[n]');
grid on;
Experiment No: 05
Experiment Name: Implementation of LP IIR Filter.
Objectives:
● To implement LP IIR filter for a given sequence.
Program:
Code Input & Figure
clc; Enter the IIR filter design specifications
clear Enter the passband ripple:40
all; Enter the stopband ripple:80
close Enter the passband freq:2200
all; Enter the stopband freq:4400
disp('Enter the IIR Enter the sampling freq:9900
filter design Frequency res
specifications');
rp=input('Enter the
passband ripple:');
rs=input('Enter the
stopband ripple:');
wp=input('Enter the
passband freq:');
ws=input('Enter the ponse of IIR LPF is:
stopband freq:');
fs=input('Enter the
sampling freq:');
w1=2*wp/fs;w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,r
s,'s');
disp('Frequency response
of IIR LPF is:');
[b,a]=butter(n,wn,'low','
s'); w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(1);plot(om/pi,m);
title('magnitude response
of IIR filter is:');
xlabel('(a) Normalized
freq. --
>');
ylabel('Gain in dB-->');
figure(2);plot(om/pi,an);
title('phase response of
IIR filter is:');
xlabel('(b) Normalized
freq. --
>');
ylabel('Phase in radians--
>');
Experiment No: 06
Experiment Name: Implementation of LP FIR Filter.
Objectives:
⮚ Design and implement a low-pass FIR filter with specified parameters.
⮚ Compare theoretical and simulated results to asses Implementation accuracy.
Program:
Code:
clc;
clear all;
close all;
fp = 250; % Passband frequency
fs = 2500; % Sampling frequency
n = 15; % Filter order (kept the same)
fn = 2 * fp / fs;
% Normalized cutoff frequency
% Window functions
windows = {'boxcar', 'bartlett', 'hanning', 'hamming', 'blackman'};
colors = {'#7E2F8E', '#77AC30', '#A2142F', '#0072BD', '#D95319'}; % Hexadecimal
colors
%magnitude response
figure(1);
hold on;
title('Magnitude Response of LPF');
ylabel('Gain in dB');
xlabel('Normalized Frequency');
grid on;
% Initialize Figure 2 for phase response
figure(2);
hold on;
title('Phase Response of LPF');
ylabel('Angle');
xlabel('Normalized Frequency');
grid on;
for i = 1:length(windows)
window = feval(windows{i}, n + 1); % Get the window function
b = fir1(n, fn, window); % FIR filter design
[H, W] = freqz(b, 1, 128); % Frequency response
% Plot magnitude response
figure(1);
plot(W / pi, abs(H), 'Color', colors{i}, 'DisplayName', windows{i});
% Plot phase response
figure(2);
plot(W / pi, angle(H), 'Color', colors{i}, 'DisplayName', windows{i});
end
figure(1);
legend show;
figure(2);
legend show;
Experiment No: 07
Experiment Name: Discrete time Fourier transform (DTFT) computation
Objective:
⮚ To compute the Discrete-Time Fourier Transform (DTFT) of discrete signals.
⮚ To understand the frequency domain representation of signals.
⮚ To analyze the amplitude and phase spectrum of signals using MATLAB.
⮚ To validate the theoretical properties of DTFT through practical implementation.
Program & Output:
1. Write a MATLAB program to find Fourier coefficients of the discrete time signal x(n)={1,2,-1}, and sketch the
magnitude and phase spectrum.
Code:
clear all;
close all;
% Signal x(n) and its length
x = [1, 2, -1];
N = length(x); % Period of the signal
i = sqrt(-1); % Imaginary unit
% Initialize Fourier coefficients array
Ck = [];
% Compute Fourier coefficients
for k = 0 : N-1
C = (1 / N) * sum(x .* exp(-i * 2 * pi * k * (0:N-1) / N));
Ck = [Ck, C];
end
% Fourier coefficients, magnitude, and phase
Ck % Fourier coefficients
Mag_of_Ck = abs(Ck) % Magnitude of Fourier coefficients
Pha_of_Ck = angle(Ck) % Phase of Fourier coefficients
% Plot magnitude and phase spectra
figure;
subplot(2, 1, 1);
stem(0:N-1, Mag_of_Ck, 'filled');
xlabel('k'), ylabel('Magnitude of Ck');
title('Magnitude Spectrum');
subplot(2, 1, 2);
stem(0:N-1, Pha_of_Ck, 'filled');
xlabel('k'), ylabel('Phase of Ck (rad)');
title('Phase Spectrum');
1. Write a MATLAB program to sketch the magnitude and phase spectrum of discrete time systems represented by the
following transfer functions.
Code Output
clear all;
close all;
% Initialize arrays for storing magnitude,
phase, and frequency values
MagH1 = []; MagH2 = []; MagH3 = [];
PhaH1 = []; PhaH2 = []; PhaH3 = [];
w1 = [];
% Frequency range from -2π to 2π with step size
of 0.01
for w = -2*pi : 0.01 : 2*pi
H1 = (1/3) * (1 - exp(-3*i*w)) / (1 - exp(-
i*w));
H2 = 2 * exp(-i*w/2) * cos(w/2);
H3 = 2 * exp(-i*w/2) * sin(w/2);
H1_M = abs(H1); H2_M = abs(H2); H3_M =
abs(H3);
H1_P = angle(H1); H2_P = angle(H2); H3_P =
angle(H3);
MagH1 = [MagH1, H1_M];
MagH2 = [MagH2, H2_M];
MagH3 = [MagH3, H3_M];
PhaH1 = [PhaH1, H1_P];
PhaH2 = [PhaH2, H2_P];
PhaH3 = [PhaH3, H3_P];
w1 = [w1, w];
end
subplot(3, 2, 1), plot(w1, MagH1);
xlabel('w in rad.'), ylabel('Mag. of H1');
title('Magnitude Spectrum of H1');
subplot(3, 2, 2), plot(w1, PhaH1);
xlabel('w in rad.'), ylabel('Phase of H1');
title('Phase Spectrum of H1');
subplot(3, 2, 3), plot(w1, MagH2);
xlabel('w in rad.'), ylabel('Mag. of H2');
title('Magnitude Spectrum of H2');
subplot(3, 2, 4), plot(w1, PhaH2);
xlabel('w in rad.'), ylabel('Phase of H2');
title('Phase Spectrum of H2');
subplot(3, 2, 5), plot(w1, MagH3);
xlabel('w in rad.'), ylabel('Mag. of H3');
title('Magnitude Spectrum of H3');
subplot(3, 2, 6), plot(w1, PhaH3);
xlabel('w in rad.'), ylabel('Phase of H3');
title('Phase Spectrum of H3');
2. Write a MATLAB program to sketch the frequency response of the first order discrete time system governed by the
transfer function,
Code Output
clear all;
close all;
j = sqrt(-1);
w = [];
Mag_H1 = []; % Magnitude for a = 0.5
Pha_H1 = []; % Phase for a = 0.5
Mag_H2 = []; % Magnitude for a = -0.5
Pha_H2 = []; % Phase for a = -0.5
for w1 = -pi : 0.01 : pi
H1 = 1 / (1 - 0.5 * exp(-j * w1)); % H(e^jw)
for a = 0.5
H2 = 1 / (1 + 0.5 * exp(-j * w1)); % H(e^jw)
for a = -0.5
Mag_H1 = [Mag_H1, abs(H1)];
Mag_H2 = [Mag_H2, abs(H2)];
Pha_H1 = [Pha_H1, angle(H1)];
Pha_H2 = [Pha_H2, angle(H2)];
% Store frequency values
w = [w, w1];
end
figure;
subplot(2, 2, 1);
plot(w, Mag_H1, 'LineWidth', 1.5);
xlabel('w (rad)'), ylabel('Magnitude |H1(jw)|');
title('Magnitude Spectrum for a = 0.5');
grid on;
subplot(2, 2, 2);
plot(w, Mag_H2, 'LineWidth', 1.5);
xlabel('w (rad)'), ylabel('Magnitude |H2(jw)|');
title('Magnitude Spectrum for a = -0.5');
grid on;
subplot(2, 2, 3);
plot(w, Pha_H1, 'LineWidth', 1.5);
xlabel('w (rad)'), ylabel('Phase of H1(jw)
(rad)');
title('Phase Spectrum for a = 0.5');
grid on;
subplot(2, 2, 4);
plot(w, Pha_H2, 'LineWidth', 1.5);
xlabel('w (rad)'), ylabel('Phase of H2(jw)
(rad)');
title('Phase Spectrum for a = -0.5');
grid on;
Experiment No: 08
Experiment Name: Implementation of Decimation Process and Interpolation Process.
Objectives:
⮚ To verify the decimation of given sequence.
⮚ To verify the Interpolation of given sequence.
Program:( Decimation Process)
Code Input & Output
clc; Enter the down sampling factor: 10
clear all; Enter the length of the input signal:
close all; 100
Enter the frequency of the first
D = input('Enter the downsampling factor:
sinusoid: 0.03
'); Enter the frequency of the second
L = input('Enter the length of the input sinusoid: 0.05
signal: ');
f1 = input('Enter the frequency of the first
sinusoid: ');
f2 = input('Enter the frequency of the
second sinusoid: ');
n = 0:L-1;
x = sin(2 * pi * f1 * n) + sin(2 * pi * f2 *
n);
y = decimate(x, D, 'fir');
% Plot the input sequence
figure(1);
h1 = stem(n, x(1:L), 'r');
set(h1, 'LineWidth', 1); % Adjust LineWidth
title('Input Sequence');
xlabel('Time (n)');
ylabel('Amplitude');
% Plot the decimated sequence
figure(2);
m = 0:(L/D)-1;
h2 = stem(m, y(1:L/D), 'o');
set(h2, 'LineWidth', 2); % Adjust LineWidth
title('Decimated Sequence');
xlabel('Time (n)');
ylabel('Amplitude');
Program:( Interpolation Process.)
Code Input
clc;
clear enter the up sampling factor
all; 5
close enter the length of the input signal 10
all; enter the frequency of first sinusoidal 0.4
L=input('enter the upsampling enter the frequency of second sinusoidal 0.6
factor'); N=input('enter the length
of the input signal'); % Length
should be greater than 8
f1=input('enter the frequency of
first sinusodal');
f2=input('enter the frequency of
second sinusodal');
n=0:N-1;
x=sin(2*pi*f1*n)
+sin(2*pi*f2*n);
y=interp(x,L);
figure(1)
stem(n,x(1:N),'Color','m',LineWidth
=2) title('input sequence');
xlabel('time(n)');
ylabel('amplitude'); figure(2);
m=0:N*L-1;
stem(m,y(1:N*L),'Color','m',LineWidth=
0.8) title('output sequence ');
xlabel('time(n)');
ylabel('amplitude');