UI21CS66_DSP_Lab
UI21CS66_DSP_Lab
Submitted by
Course Faculty
August 2023
Index
CODE:
clc;
close all;%Close all opened Graphs
t=-1:0.001:1;
l=(length(t));
for i=1:l
if(t(i)<0)
x(i)=0;
else
x(i)=1;
end
end
n=-1:0.05:1;
N=length(n);
for i=1:N
if n(i)<0
y(i)=0;
else
y(i)=1;
end
end
figure;
stem(n,y,'b','LineWidth',0.2);
grid on;
axis([-1,1,-0.1,1.1]);
xlabel('Time(n)');
ylabel('Amplitude');
title('Unit Step Sequence');
% Continuous Time Ramp Signal
t=-1:0.05:1;
N=length(n);
for i=1:N
if t(i)<0
z(i)=0;
else
z(i)=t(i);
end
end
figure;
plot(t,z,'b','LineWidth',0.2);
grid on;
axis([-1,1,-0.1,1.1]);
xlabel('Time(t)');
ylabel('Amplitude');
title('Ramp Signal');
OUTPUT:
TASK 2: Perform addition, subtraction and multiplication on
Sinusoidal signals and plot the results.
CODE:
clc;
clear All;
close all;
t = -2:0.001:2;
f1=1;
f2=3;
pi=3.14;
x1=sin(2*pi*f1*t);
x2=sin(2*pi*f2*t);
OUTPUT:
1) Sine wave with frequency = 1Hz
CODE:
% Time Shifting
figure;
plot(t,sin(2*pi*f1*(t)));
axis([-2,2,-2.1,2.1]);
grid on;
xlabel("Time");
ylabel("Amplitude");
title("Time Shifting");
% Time Shifting
figure;
plot(t,sin(2*pi*f1*(t+100)));
axis([-2,2,-2.1,2.1]);
grid on;
xlabel("Time");
ylabel("Amplitude");
title("Time Shifting");
OUTPUT:
1)ORIGINAL SINE WAVE
2)TIME SHIFTED SINE WAVE
TASK 4: Perform 4, 8 and 16 levels of digitization on sinusoidal
signals and plot them.
CODE:
t = -2:0.001:2;
f=1;
pi=3.14;
x1=sin(2*pi*f*t);
y2=floor(x1/0.5);
figure;
plot(t,y2/2);
axis([-2,2,-2.1,2.1]);
grid on;
xlabel("Time");
ylabel("Amplitude");
legend('1 Hz','3 Hz')
title("Sine Wave(4 Quantization Levels)");
y3=floor(x1/0.25);
figure;
plot(t,y3/4);
axis([-2,2,-2.1,2.1]);
grid on;
xlabel("Time");
ylabel("Amplitude");
legend('1 Hz','3 Hz')
title("Sine Wave(8 Quantization Levels)");
y4=floor(x1/0.125);
figure;
plot(t,y4/8);
axis([-2,2,-2.1,2.1]);
grid on;
xlabel("Time");
ylabel("Amplitude");
legend('1 Hz','3 Hz')
title("Sine Wave(16 Quantization Levels)");
OUTPUT:
1) SINE WAVE (4 QUANTIZATION LEVELS)
CONCLUSION:
Through this experiment, we came to know about the plotting of various analog
signals and the digitization of the same with variable step size.
LAB-2
Ztrans(4,[1,2,3,4])
Ztrans(5,[4,3,1,2,9])
Ztrans(6,[3,5,7,9,10,12])
Ztrans(2,[2,9])
Ztrans(5,[1,2,5,3,4])
function res=Ztrans(s,xn)
n_min=0;
n_max=s-1;
syms k;
syms x;
syms z;
f(x,k)=x*(z^(-k));
m=1;
for n=n_min:n_max
xz(m)=f(xn(m),n);
m=m+1;
end
res=sum(xz);
end
clc;
clear all;
close all;
n = 0:10;
for i = 0:11
if i==0
ui(i+1)=1;
else
ui(i+1)=0;
end
end
syms z;
Z_transform = ztrans(ui,z);
disp(Z_transform)
f=[1 2 3 4];
Z_transform = ztrans(f,z);
disp(Z_transform)
syms n;
f=sin(n);
Z_transform = ztrans(f,z);
disp(Z_transform)
disp(ztrans(cos(n),z))
disp(ztrans([4,3,1,2],z))
OUTPUT:
a) Without using inbuilt function:
b) Using inbuilt function:
TASK 1: To implement Z-transform in MATLAB with and without
inbuilt function.
CODE :
z = tf('z');
H = z;
pzmap(H);
figure;
H = (z - 1) / (z + 1);
pzmap(H);
figure;
H = ((z - 1j)*(z + 1j)) / ((z - exp(1j*pi/4))*(z - exp(-1j*pi/4)));
pzmap(H);
figure;
H = ((z - 0.5 - 0.5j)*(z - 0.5 + 0.5j)) / ((z - 0.7 - 0.2j)*(z - 0.7 + 0.2j));
pzmap(H);
figure;
H = 1 / ((z - 0.9*exp(1j*pi/10))*(z - 0.9*exp(-1j*pi/10)));
pzmap(H);
OUTPUT:
1. H=z
2. H=(z-1)/(z+1)
syms z n;
a=0.2;
w = 1 / (1 - a*z^(-1));
disp(iztrans(w, z, n));
b = 0.5;
x = 1 / (1 - b*z^(-1));
disp(iztrans(x, z, n));
y=z^(-1) / (1 - 0.5*z^(-1));
disp(iztrans(y, z, n));
xy=z/(z-1);
disp(iztrans(xy, z, n));
OUTPUT:
CONCLUSION:
% Function-1
N = 4; % Number of samples
x = [1,2,2,1];
X=DFT(4,[1,2,2,1]);
% Display original sequence and DFT result
disp('Input Original Sequence:');
disp(x);
disp('DFT Result:');
disp(X);
x_reconstructed=IDFT(N,X);
% Display reconstructed sequence using IDFT
disp('Reconstructed Sequence using IDFT:');
disp(x_reconstructed);
% Function-2
N = 5; % Number of samples
n = 0:N-1; % Time indices
f = 0.3;
x = exp(1j*2*pi*f*n);
X=DFT(N,x);
% Display original sequence and DFT result
disp('Input Original Sequence:');
disp(x);
disp('DFT Result:');
disp(X);
x_reconstructed=IDFT(N,X);
% Display reconstructed sequence using IDFT
disp('Reconstructed Sequence using IDFT:');
disp(x_reconstructed);
% Function-3
N = 5; % Number of samples
x = [9,4,1,2,9];
X=DFT(N,x);
% Display original sequence and DFT result
disp('Input Original Sequence:');
disp(x);
disp('DFT Result:');
disp(X);
x_reconstructed=IDFT(N,X);
% Display reconstructed sequence using IDFT
disp('Reconstructed Sequence using IDFT:');
disp(x_reconstructed);
% Function-4
N = 16; % Number of samples
n = 0:N-1; % Time indices
f = 0.25; % Frequency of the signal
x = cos(2*pi*f*n); % Input signal
X=DFT(N,x);
% Display original sequence and DFT result
disp('Input Original Sequence:');
disp(x);
disp('DFT Result:');
disp(X);
x_reconstructed=IDFT(N,X);
% Display reconstructed sequence using IDFT
disp('Reconstructed Sequence using IDFT:');
disp(x_reconstructed);
% Function-5
N = 6; % Number of samples
x = [19,12,3,4,9,2];
X=DFT(N,x);
% Display original sequence and DFT result
disp('Input Original Sequence:');
disp(x);
disp('DFT Result:');
disp(X);
x_reconstructed=IDFT(N,X);
% Display reconstructed sequence using IDFT
disp('Reconstructed Sequence using IDFT:');
disp(x_reconstructed);
function res=DFT(s,x)
X = zeros(1, s); % Initialize DFT result
for k = 1:s
for n = 1:s
X(k) = X(k) + x(n) * exp(-1j*2*pi*(k-1)*(n-1)/s);
end
end
res=X;
end
function res2=IDFT(s,X)
x_reconstructed = zeros(1, s); % Initialize reconstructed signal
for n = 1:s
for k = 1:s
x_reconstructed(n) = x_reconstructed(n) + (1/s) * X(k) *
exp(1j*2*pi*(k-1)*(n-1)/s);
end
end
res2=x_reconstructed;
end
OUTPUT:
CONCLUSION:
Task-1
clc;
clear;
close all;
%
% %DIsplay circle
% n=0:0.01:1;
% a=sin(2*pi*n);
% b=cos(2*pi*n);
% plot(a,b);
%
% m = input('Enter number of points');
% thetao = 2*pi/m;
% theta = thetao;
% for i=1:m
% text(sin(theta),cos(theta),'o');
% theta = theta + thetao;
% end
N = length(x);
M = length(h);
Task-2
clc;
clear;
close all;
N = length(x);
M = length(h);
Task-3
clc;
clear;
close all;
N = length(x);
M = length(h);
Task-4
clc;
clear all;
close all;
% Example usage with different input functions and varying lengths
N = max(length(x_sequence_1), length(x_sequence_2));
%DIsplay circle
n=0:0.01:1;
a=sin(2*pi*n);
b=cos(2*pi*n);
m = N;
thetao = 2*pi/m;
x1 = x_sequence_1;
x3 = flip(x_sequence_2);
for i = 1:N
subplot(N,3,(3*i - 2));
plot(a,b);
theta = 0;
for j=1:m
val = int2str(x1(j));
text(cos(theta),sin(theta), val);
theta = theta + thetao;
end
x3 = circshift(x3, 1);
subplot(N,3,(3*i - 1));
plot(a,b);
theta = 0;
for j=1:m
val = int2str(x3(j));
text(cos(theta),sin(theta), val);
theta = theta + thetao;
end
x4 = x1.*x3;
subplot(N,3,3*i);
plot(a,b);
theta = 0;
for j=1:m
val = int2str(x4(j));
text(cos(theta),sin(theta), val);
theta = theta + thetao;
end
x5(i) = sum(x1.*x3);
end
Task-1
Task-2
Task-3
Task-2
Task-4
CONCLUSION:
function X = fft_dit(x)
N = length(x);
if N <= 1
X = x;
else
% Split the input into even and odd-indexed elements
x_even = x(1:2:N);
x_odd = x(2:2:N);
OUTPUT:
DIT-FFT Code
2-point DIT-FFT
4-point DIT-FFT
CONCLUSION:
function X = dif_fft(x)
N = length(x);
OUTPUT:
DIF-FFT Code
4-point DIT-FFT
8-point DIT-FFT
CONCLUSION:
c = max(abs(H)) / sqrt(2);
indices = find(abs(H) >= c);
cutoff_freq=F(indices);
c = max(abs(H)) / sqrt(2);
indices = find(abs(H) >= c);
cutoff_freq=F(indices);
c = max(abs(H)) / sqrt(2);
indices = find(abs(H) >= c);
cutoff_freq=F(indices);
c = max(abs(H)) / sqrt(2);
indices = find(abs(H) <= c);
cutoff_freq=F(indices);
% Calculate output
y(n) = b * x_n' - a(2:end) * y_n(2:end)';
end
end
y = zeros(size(x));
for j = 1:length(R)
% Calculate contribution of each term
y_j = R(j) * P(j)^(-1) * (1 - exp(P(j) * x));
y = y + y_j;
end
end
% Calculate output
y(n) = b(1) * (sum(b .* x_n) - sum(a(2:end) .* y_n(2:end)));
end
end
OUTPUT:
CONCLUSION:
OUTPUT:
Odd Length ,Even Symmetry
CONCLUSION:
This lab aimed to implement linear phase characteristics of Finite
Impulse Response (FIR) discrete digital filters using MATLAB. Through
rigorous design and analysis, the objective was to achieve linear phase
response, a crucial property in various signal processing applications.
The study involved coding and simulation in MATLAB, showcasing the
effectiveness of the implemented FIR filters in preserving the phase
relationship of input signals across different frequencies
LAB-11
AIM : To implement various window methods to obtain FIR
filter response from IIR signal using MATLAB.
CODE :
Rectangular Window
% Define parameters
N = 45; % Window length
cutoff = 0.2; % Cutoff frequency
Fs = 1000; % Sampling frequency
b = fir1(N-1, cutoff,"low",rectangular_window);
subplot(2, 1, 2);
plot(w, 20*log10(abs(H)));
title('Low-Pass Filter Frequency Response');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
% Display plots
% sgtitle('Low-Pass FIR Filter Plots with Rectangular Window');
Triangular Window
% Define parameters
N = 45; % Window length
cutoff = 0.2; % Cutoff frequency
Fs = 1000; % Sampling frequency
subplot(2, 1, 2);
plot(w, 20*log10(abs(H)));
title('Low-Pass Filter Frequency Response');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
% Display plots
% sgtitle('Low-Pass FIR Filter Plots with Triangular Window');
Blackman Window
% Define parameters
N = 45; % Filter order
cutoff = 0.2; % Cutoff frequency
Fs = 1000; % Sampling frequency
figure;
% Generate the Hamming window
blackman_window = blackman(N);
subplot(2, 1, 2);
plot(w, 20*log10(abs(H)));
title('Low-Pass Filter Frequency Response');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
% Display plots
% sgtitle('Low-Pass FIR Filter Plots with Blackman Window');
Hamming Window
% Define parameters
N = 45; % Filter order
cutoff = 0.2; % Cutoff frequency
Fs = 1000; % Sampling frequency
figure;
% Generate the Hamming window
hamming_window = hamming(N);
subplot(2, 1, 2);
plot(w, 20*log10(abs(H)));
title('Low-Pass Filter Frequency Response');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
% Display plots
% sgtitle('Low-Pass FIR Filter Plots with Hamming Window');
Hanning Window
% Define parameters
N = 45; % Filter order
cutoff = 0.2; % Cutoff frequency
Fs = 1000; % Sampling frequency
figure;
% Generate the Hamming window
hanning_window = hann(N);
subplot(2, 1, 2);
plot(w, 20*log10(abs(H)));
title('Low-Pass Filter Frequency Response');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
% Display plots
% sgtitle('Low-Pass FIR Filter Plots with Hanning Window');
Kaiser Window
% Define parameters
N = 45; % Filter order
cutoff = 0.2; % Cutoff frequency
Fs = 1000; % Sampling frequency
figure;
% Generate the Rectangular window
beta=3;
kaiser_win = kaiser(N,beta);
subplot(2, 1, 2);
plot(w, 20*log10(abs(H)));
title('Low-Pass Filter Frequency Response');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
% Display plots
% sgtitle('Low-Pass FIR Filter Plots with Kaiser Window');
OUTPUT:
Rectangular Window
Triangular Window
Blackman Window
Hamming Window
Hanning Window
Kaiser Window
CONCLUSION:
In this lab, various window methods were implemented to obtain Finite
Impulse Response (FIR) filter responses from Infinite Impulse
Response (IIR) signals using MATLAB. By applying different window
functions, such as Hamming, Blackman, and Kaiser, the goal was to
analyze their impact on the filter's frequency response. This study
provided insights into selecting appropriate windowing techniques for
transforming IIR signals into FIR filters with desired characteristics,
enhancing the understanding of signal processing methodologies.
LAB-12
AIM : To implement IIR filter realisation using Bilinear
transformation and Impulse Invariance method.
CODE :
% IIR Filter Design using Impulse Invariant and Bilinear Transform Methods
% Filter specifications
fs = 1000; % Sampling frequency
fc = 100; % Cutoff frequency
order = 4; % Filter order
% Bilinear transformation
[num_bilin, den_bilin] = bilinear(b, a, fs);
subplot(2,2,3);
plot(w_bilin, 20*log10(abs(h_bilin)));
title('Bilinear Transform Method - Magnitude Response');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
grid on;
subplot(2,2,4);
plot(w_bilin, angle(h_bilin));
title('Bilinear Transform Method - Phase Response');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
grid on;
OUTPUT:
CONCLUSION: