Submitted By: Name: Jaisrihari.G
Submitted By: Name: Jaisrihari.G
Name : Jaisrihari.G
Roll.No : CB.EN.U4ELC22015
Ex. No.: 1 Date: 29/07/2024
EXPERIMENT-1A:
Sample Code:
%Experiment-1a Date:29/07/2024
%Program to compute the DFT of the sequence
% JAISRIHARI.G, CB.EN.U4ELC22015
%x[n]={2 2 1 1}
clc
clear all
close all
x = [2, 2, 1, 1];
N = length(x);
X = zeros(1, N);
for k = 0:N-1
for n = 0:N-1
X(k+1) = X(k+1) + x(n+1) * exp((-2j * pi * k * n) / N);
end
end
disp(X);
OUTPUT:
EXPERIMENT 1B:
SAMPLE CODE:
%Experiment-1b Date:29/07/2024
%Program to compute the IDFT of the sequence
% JAISRIHARI.G, CB.EN.U4ELC22015
%x[n]={2 2 1 1}
X = [6 1-j 0 1+j];
N = length(X);
x = zeros(1, N);
for n = 0:N-1
for k = 0:N-1
x(n+1) = x(n+1) + X(k+1) * exp(1j*2*pi*k*n/N);
end
end
x = x / N;
disp(x);
OUTPUT:
EXPERIMENT 1C:
SAMPLE CODE:
%Experiment-1c Date:29/07/2024
%Program to compute the DFT of the sequence in matrix form
% JAISRIHARI.G, CB.EN.U4ELC22015
clc
clear all
close all
OUTPUT:
EXPERIMENT 1D:
TO COMPUTE THE DFT OF THE GIVEN SEQUENCE USING MATRIX METHOD PLOT
THE GRAPH OF MAGNITUDE AND PHASE USING SUBPLOT
SAMPLE CODE:
%Experiment-1d Date:29/07/2024
% JAISRIHARI.G, CB.EN.U4ELC22015
% Define the input sequence
x = [2, 2, 1, 1];
EXPERIMENT 1E:
SAMPLE CODE:
%Experiment-1d Date:29/07/2024
% JAISRIHARI.G, CB.EN.U4ELC22015
% Define the input sequence
clc
clear all
close all
N=8;
x=[2 2 1 1]
l=length(x);
X=zeros(1,l);
if l<N
x=[x,zeros(1,N-l)];
disp(x)
k=[0 1 2 3];
n=[0 1 2 3];
for k=0:l-1
for n=0:l-1
X(k+1)=X(k+1)+x(n+1)*exp((1i.*2.*pi.*k.*n)/l);
end
end
disp(real(X))
end
if l==N
disp('Lengths are Equal')
k=[0 1 2 3];
n=[0 1 2 3];
for k=0:l-1
for n=0:l-1
X(k+1)=X(k+1)+x(n+1)*exp((1i.*2.*pi.*k.*n)/l);
end
end
disp(real(X))
end
if l>N
disp('Enter the len of x less than or equal to N')
end
OUTPUT:
Submitted by:
Total 10
Verified by
Faculty Signature with date.
Ex. No.: 2 Date: 5/8/2024
AIM:
i. circularly shift the given sequence and visualize the original and shifted versions using stem
plots in MATLAB.
ii. circularly shift the given and visualize both the original and shifted versions using MATLAB's
circshift function.
iii. Circularly reverse the given sequence and visualize the original and shifted versions using
stem plots in MATLAB.
iv. Circular convolution using circular shift and circular reverse and visualize the original and
shifted versions using stem plots in MATLAB.
v. Linear convolution using circular convolution and visualize the original and shifted versions
using stem plots in MATLAB.
TASK 1
circularly shift the given sequence and visualize the original and shifted versions using stem plots
in MATLAB.
SOURCE CODE:
% JAISRIHARI.G, CB.EN.U4ELC22015
clc
clear all
close all
OUTPUT:
TASK 2
Circularly shift the given and visualize both the original and shifted versions using MATLAB's
circshift function
SOURCE CODE:
% JAISRIHARI.G, CB.EN.U4ELC22015
x = [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8];
n0 = 2;
x1 = circshift(x, n0);
subplot(2, 1, 1);
stem(0:numel(x)-1, x, 'b', 'LineWidth', 1.5);
xlabel('Index');
ylabel('Value');
title('Plot of x');
grid on;
subplot(2, 1, 2);
stem(0:numel(x)-1, x1, 'r', 'LineWidth', 1.5);
xlabel('Index');
ylabel('Value');
title('Plot of x1 (circularly shifted by 2)');
grid on;
OUTPUT:
TASK 3
Circularly reverse the given sequence and visualize the original and shifted versions using stem
plots in MATLAB.
SOURCE CODE:
% JAISRIHARI.G, CB.EN.U4ELC22015
% Circularly Reverse the given sequence
clc
clear all
close all
x_rev(1) = x(1);
for i = 2:N
x_rev(i) = x(N - i + 2);
end
subplot(2, 1, 1);
stem(0:N-1, x(1:N), 'b', 'LineWidth', 1.5);
xlabel('Index');
ylabel('Value');
title('Plot of original x');
grid on;
subplot(2, 1, 2);
stem(0:N-1, x_rev, 'r', 'LineWidth', 1.5);
xlabel('Index');
ylabel('Value');
title('Plot of circularly reversed x with first term fixed');
grid on;
OUTPUT:
TASK 4
Circular convolution using circular shift and circular reverse and visualize the original and shifted
versions using stem plots in MATLAB.
SOURCE CODE:
% JAISRIHARI.G, CB.EN.U4ELC22015
clc
clear all
close all
x = [1, 2, 3, 4];
h = [3, 2, 5, 1];
N = length(x);
y = zeros(1, N);
h_rev = flip(h);
for n = 1:N
h_shifted = circshift(h_rev, [0, n-1]);
y(n) = sum(x .* h_shifted);
end
figure;
subplot(3, 1, 1);
stem(0:N-1, x, 'filled');
title('Sequence x');
xlabel('n');
ylabel('x[n]');
subplot(3, 1, 2);
stem(0:N-1, h, 'filled');
title('Sequence h');
xlabel('n');
ylabel('h[n]');
subplot(3, 1, 3);
stem(0:N-1, y, 'filled');
title('Circular Convolution Result');
xlabel('n');
ylabel('y[n]');
OUTPUT:
TASK 5
Linear convolution using circular convolution and visualize the original and shifted versions using
stem plots in MATLAB.
SOURCE CODE:
% JAISRIHARI.G, CB.EN.U4ELC22015
clc
clear all
close all
x = [1, 2, 3, 4];
h = [1, 1, 1, 1];
N1 = 4;
N2 = 4;
N = N1 + N2 - 1;
% Zero-pad the sequences
x_padded = [x, zeros(1, N2 - 1)];
h_padded = [h, zeros(1, N1 - 1)];
figure;
subplot(4, 1, 1);
stem(0:N1-1, x, 'filled');
title('Sequence x');
xlabel('n');
ylabel('x[n]');
subplot(4, 1, 2);
stem(0:N2-1, h, 'filled');
title('Sequence h');
xlabel('n');
ylabel('h[n]');
subplot(4, 1, 3);
stem(0:N-1, y_circular, 'filled');
title('Circular Convolution Result');
xlabel('n');
ylabel('y_{circular}[n]');
subplot(4, 1, 4);
stem(0:N1 + N2 - 2, y_linear, 'filled');
title('Linear Convolution Result from Circular Convolution');
xlabel('n');
ylabel('y_{linear}[n]');
Submitted by:
Conduction 5
Total 10
Verified by
Faculty Signature with date.
Ex. No.: 3 Date: 19/8/2024
AIM:
a) Compute FFT of a given sequence
b) Compute IFFT of a given sequence
c) Compute time taken for FFT computation of a given sequence
d) Compute the FFT of the given sequence of random length (ex:5)
e) Power Spectral Density Analysis of a Sine Wave
f) Power Spectral Density of Combined Sine Waves (5Hz,2Hz,3Hz)
g) Power Spectral Density of Combined Sine Waves (5Hz,2Hz,3Hz) with noise
h) Amplitude Spectrum of the Signal: Two-Sided and One-Sided
TASK 1:
Compute FFT of a given sequence
SOURCE CODE:
% Experiment-3a Date:19/08/2024
% JAISRIHARI.G, CB.EN.U4ELC22015
%Computing FFT of a given sequence
x = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8];
X = fft(x, 8);
disp(X)
OUTPUT:
TASK 2
Compute IFFT of a given sequence
SOURCE CODE:
% Experiment-3b Date:19/08/2024
% JAISRIHARI.G, CB.EN.U4ELC22015
%Computing IFFT of a given sequence
OUTPUT:
TASK 3
Compute time taken for FFT computation of a given sequence
SOURCE CODE:
% Experiment-3c Date:19/08/2024
% JAISRIHARI.G, CB.EN.U4ELC22015
% Compute time taken for FFT computation of a given sequence
N = length(x);
stages = log2(N);
TASK 4
Compute the FFT of the given sequence of random length (ex:5)
SOURCE CODE:
% Experiment-3d Date:19/08/2024
% JAISRIHARI.G, CB.EN.U4ELC22015
% Compute the FFT of the given sequence of random length (ex:5)
x = rand(1,5);
N = 8;
x_padded = [x, zeros(1, N - length(x))];
X = fft(x_padded, N);
disp(X);
OUTPUT:
TASK 5
Power Spectral Density Analysis of a Sine Wave
SOURCE CODE:
% JAISRIHARI.G, CB.EN.U4ELC22015
% Power Spectral Density Analysis of a Sine Wave
fs = 100;
t = 0:1/fs:1-1/fs;
f = 5;
y = sin(2*pi*f*t);
subplot(2,1,1)
plot(t, y);
title('5 Hz Sine Wave');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
N = length(t);
Y = fft(y);
Py = abs(Y).^2/N;
freq = fs*(0:(N/2))/N;
subplot(2,1,2)
plot(freq, Py(1:length(freq)));
title('Power Spectral Density (5 Hz Sine Wave)');
xlabel('Frequency (Hz)');
ylabel('Power');
grid on;
OUTPUT:
TASK 6
Power Spectral Density of Combined Sine Waves (5Hz,2Hz,3Hz)
SOURCE CODE:
% JAISRIHARI.G, CB.EN.U4ELC22015
%Power Spectral Density of Combined Sine Waves (5Hz,2Hz,3Hz)
fs = 100;
t = 0:1/fs:1-1/fs;
f1 = 5; f2 = 2; f3 = 3;
y1 = sin(2*pi*f1*t);
y2 = sin(2*pi*f2*t);
y3 = sin(2*pi*f3*t);
y = y1 + y2 + y3;
subplot(2,1,1)
plot(t, y);
title('Combined Sine Waves (5 Hz, 2 Hz, 3 Hz)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
N = 128;
Y = fft(y, N);
Py = abs(Y(1:N/2+1)).^2/N;
freq = fs*(0:(N/2))/N;
subplot(2,1,2)
plot(freq, Py);
title('Power Spectral Density (Combined 5 Hz, 2 Hz, 3 Hz)');
xlabel('Frequency (Hz)');
ylabel('Power');
grid on;
OUTPUT:
TASK 7
Power Spectral Density of Combined Sine Waves (5Hz,2Hz,3Hz) with noise
SOURCE CODE:
% JAISRIHARI.G, CB.EN.U4ELC22015
%Power Spectral Density of Combined Sine Waves (5Hz,2Hz,3Hz) with noise
fs = 100;
t = 0:1/fs:1-1/fs;
f1 = 5; f2 = 2; f3 = 3;
noise = randn(size(t));
y = y_clean + noise;
subplot(2,1,1)
plot(t, y);
title('Combined Sine Waves with Noise (5 Hz, 2 Hz, 3 Hz)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
N = 128;
Y = fft(y, N);
Py = abs(Y(1:N/2+1)).^2/N;
freq = fs*(0:(N/2))/N;
subplot(2,1,2)
plot(freq, Py);
title('Power Spectral Density with Noise (5 Hz, 2 Hz, 3 Hz)');
xlabel('Frequency (Hz)');
ylabel('Power');
grid on;
OUTPUT:
TASK 8
Amplitude Spectrum of the Signal: Two-Sided and One-Sided
SOURCE CODE:
% JAISRIHARI.G, CB.EN.U4ELC22015
%Amplitude Spectrum of the Signal:Two-Sided and One-Sided
clear all;
fs = 20000;
N = 2000;
x = 3*cos(4000* pi*[0:1:N-1]/fs)-cos(12000* pi*[0:1:N-1]/fs)- sin(2000* pi*[0:1:N-
1]/fs);
x=[x, zeros(1,48)];
N = length(x);
xf = abs(fft(x))/N;
f = [0:1:N-1]*(fs/N);
subplot(2,1,1)
plot(f, xf,LineWidth=2);
title('Amplitude Spectrum');
xlabel('Frequency(Hz)');
ylabel('Amplitude Spectrum(DFT)');
grid on;
xf(2:N)=2*xf(2:N)
f = [0:1:N/2]*fs/N;
subplot(2,1,2)
plot(f, xf(1:N/2+1),LineWidth=2);
title('Amplitude Spectrum');
xlabel('Frequency(Hz)');
ylabel('One Sided Amplitude Spectrum(DFT)');
grid on;
OUTPUT:
Inference:
- The use of `fft(x, N)` and `ifft(x, N)` efficiently transformed sequences between the time and
frequency domains, showcasing the flexibility of FFT algorithms in processing both real and
complex data. The application of zero-padding (`[x, zeros(1, N - length(x))]`) underscored the
significance of matching sequence lengths for precise frequency resolution.
- By using `abs(fft(y)).^2/N`, the Power Spectral Density (PSD) effectively displayed the energy
distribution across frequencies, even in the presence of noise (`randn(size(t))`). The analysis of both
two-sided and one-sided amplitude spectra (`xf(1:N/2+1)`) emphasized the influence of signal
symmetry on the frequency domain representation.
Submitted by:
Conduction 5
Total 10
Verified by
Faculty Signature with date.
Ex. No.: 4 Date: 21/09/2024
TASK 1:
SOURCE CODE:
%Experiment-4a Date:23/09/2024
% JAISRIHARI.G, CB.EN.U4ELC22015
% 32bit(Single Precision) floating point(IEEE754 format) to decimal
float = [0 1 0 0 0 0 0 1 1 1 0 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1]
num = 0;
exp = 0;
man = 0;
r = 0
if float(1) == 0
s = 0;
else
s = 1;
end
for i = 9:-1:2
X(i) = (float(i)*(2^r));
r = r + 1;
exp = exp + X(i);
end
exp = exp - 127
disp('The exponent is ');
disp(exp)
b = -1
for i = 10:32
X(i) = (float(i)*(2^b));
b = b-1;
man = man + X(i);
end
disp('The mantissa is ');
disp(man)
dec = ((-1)^s)*(1+man)*(2^(exp))
disp('The decimal is ');
disp(dec)
OUTPUT:
TASK 2:
SOURCE CODE:
%Experiment-4b Date:23/09/2024
% JAISRIHARI.G, CB.EN.U4ELC22015
% Decimal to floating point
clc
clear all
close all
decimal = -70.7;
t = decimal;
if t > 0
s = 0;
else
s = 1;
decimal = abs(decimal);
end
h = floor(decimal);
d = decimal - h;
f = [];
while(decimal > 0)
r = 0;
a = [];
r = mod(decimal,2);
decimal = floor(decimal/2);
if r == 1
f = [f,ones(1,1)];
else
f = [f,zeros(1,1)];
end
end
f;
f1 = [];
for i=0:length(f)-1
f1(i+1) = f(length(f)-i);
end
f1;
m = [];
e = length(f1) - 1 + 127;
for i=2:length(f1)
m(i-1) = f1(i);
end
n = [];
j = 23 - length(m);
i = 0;
while(d ~= 0)
d = d*2;
i = i+1;
if i > j
break;
end
if floor(d) == 1
d = d - 1;
n = [n,ones(1,1)];
else
n = [n,zeros(1,1)];
end
end
k = [];
while(e > 0)
r = 0;
a = [];
r = mod(e,2);
e = floor(e/2);
if r == 1
k = [k,ones(1,1)];
else
k = [k,zeros(1,1)];
end
end
k1 = [];
for i=0:length(k)-1
k1(i+1) = k(length(k)-i);
end
k1;
m;
n;
g = [s,k1,m,n]
OUTPUT:
Inference:
• Task 1 (IEEE 754 to Decimal): The MATLAB code takes a 32-bit binary string as input and
splits it into sign_bit, exponent_bits, and mantissa_bits. The exponent is calculated using
bin2dec(exponent_bits) - 127, while the mantissa is constructed iteratively using bitwise
• Task 2 (Decimal to IEEE 754): For the reverse conversion, the input decimal number is split
into its sign_bit, exponent, and mantissa. The exponent is computed using log2(abs_value) and
adjusted by adding 127 to fit the IEEE 754 bias. The mantissa_bits are determined through
repeated multiplication and binary extraction, ensuring a precise 23-bit mantissa.
Submitted by:
Conduction 5
Total 10
Verified by
Faculty Signature with date
Ex. No.: 5 Date: 07/10/2024
AIM:
To design an Infinite Impulse Response (IIR) digital filter using the Impulse Invariant
Transformation (IIT) method
SOURCE CODE:
% Design of IIR LPF filter using
% Impulse Invariant Transform
clc;
clear all;
close all;
Ap = 0.707;
As = 0.2;
omega_p = 0.5*pi;
omega_s = 0.75*pi;
T = 1;
p_att = -20*log10(Ap);
s_att = -20*log10(As);
[N, cf] = buttord(omega_p, omega_s, p_att, s_att, 's');
fprintf('Order of analog Butterworth filter (H_s): %d\n', N);
[B, A] = butter(N, cf, 's');
H_s = tf(B, A);
H_s
[num, den] = impinvar(B, A, 1/T);
H_z = tf(num, den, T);
H_z
order_Hz = length(den) - 1;
fprintf('Order of discrete-time filter (H_z): %d\n', order_Hz);
[H, f] = freqz(num, den, 1024);
figure;
plot(f/pi, abs(H));
xlabel('Normalized Frequency (\times \pi rad/sample)');
ylabel('Magnitude');
title('Frequency vs Magnitude Response');
grid on;
OUTPUT:
Inference:
The code is intended to build an IIR digital filter via Impulse Invariant Transformation (IIT). First, it
defines the filter's properties, such as passband and stopband attenuation values and frequencies. The
passband and stopband attenuations are then determined in decibels. Using these settings, the 'buttord'
function calculates the order and cutoff frequency of an analog Butterworth filter. After building the
filter with the 'butter' function, the transfer function of the analog filter is calculated. Finally, the Impulse
Invariant Transformation is used to turn the analog filter into a discrete-time filter, and the frequency
response is shown for study.
Submitted by:
Conduction 5
Total 10
Verified by
Faculty Signature with date
Ex. No.: 6 Date: 07/10/2024
AIM:
To design an Infinite Impulse Response (IIR) digital filter using the Bilinear Transformation method, which
converts an analog filter into a digital filter while avoiding aliasing and maintaining stability.
SOURCE CODE:
% Design of IIR LPF filter using
% Bilinear Transformation
clc;
clear all;
close all;
Ap = 0.707;
As = 0.2;
omega_p = 0.5*pi;
omega_s = 0.75*pi;
T = 1;
p_att = -20*log10(Ap);
s_att = -20*log10(As);
omega_p_warp = (2/T) * tan(omega_p/2);
omega_s_warp = (2/T) * tan(omega_s/2);
[N, Wn] = buttord(omega_p_warp, omega_s_warp, p_att, s_att, 's');
[B, A] = butter(N, Wn, 's');
H_s = tf(B, A)
order_Hs = length(A) - 1;
disp(['Order of H_s (Analog Filter): ', num2str(order_Hs)]);
[num, den] = bilinear(B, A, 1/T);
H_z = tf(num, den, T)
order_Hz = length(den) - 1;
disp(['Order of H_z (Digital Filter): ', num2str(order_Hz)]);
[H, f] = freqz(num, den, 1024);
figure;
plot(f/pi, abs(H));
xlabel('Normalized Frequency (\times \pi rad/sample)');
ylabel('Magnitude');
title('Frequency vs Magnitude Response (Bilinear Transformation)');
grid on;
OUTPUT:
Inference:
The code creates an IIR digital filter using the Bilinear Transformation method. First, the passband and
stopband ripple values and frequencies are established. The attenuations are then calculated in dB, and
frequency pre-warping is performed to account for the nonlinearity of the bilinear transformation. The
code uses the 'buttord' function to compute the minimal filter order and cutoff frequency for the analog
Butterworth filter. The filter is then created in the analog domain and converted to its digital equivalent
using the 'bilinear' function. Finally, the code shows the frequency response of the digital filter for
analysis.
Submitted by:
Conduction 5
Total 10
Verified by
Faculty Signature with date
Ex. No.: 7 Date: 14/10/2024
AIM:
To plot different windows that are used to design FIR filters
(a)Rectangular Window
(b)Hanning Window
(c)Hamming Window
(d)Triangular Window (Bartlett Window)
(e)Blackman Window
SOURCE CODE:
% (a)Rectangular Window
N = 51;
y_rect = rectwin(N);
stem(y_rect);
xlabel('n');
ylabel('W(n)');
title('Rectangular Window');
grid on;
% (b)Hanning Window
N = 51;
y_hann = hanning(N);
stem(y_hann);
xlabel('n');
ylabel('W(n)');
title('Hanning Window');
grid on;
% (c)Hamming Window
N = 51;
y_hamm = hamming(N);
stem(y_hamm);
xlabel('n');
ylabel('W(n)');
title('Hamming Window');
grid on;
OUTPUT:
Inference:
The code creates and plots different window functions used in FIR filter design with ( N = 51 ).
i. Rectangular Window is generated using ‘rectwin’ and plotted, showing constant amplitude.
ii. Hanning Window is generated with ‘hanning’, producing a smooth, tapered curve.
iii. Hamming Window is created using ‘hamming’, which tapers slightly less than Hanning.
iv. Triangular (Bartlett) Window uses ‘bartlett’, showing a linear rise and fall.
v. Blackman Window is generated with ‘blackman’, displaying a heavily tapered shape.
Submitted by:
Conduction 5
Total 10
Verified by
Faculty Signature with date
Ex. No.: 8 Date: 14/10/2024
Design low pass, high pass, band pass and band stop FIR
digital filters using different windows
AIM:
To design low pass, high pass ,band pass and band stop FIR digital filters using :
a) Rectangular window
b) Hanning window
c) Hamming window
a) Rectangular window :
SOURCE CODE:
% Experiment-8a Date:14/10/2024
% JAISRIHARI.G, CB.EN.U4ELC22015
% To design an FIR low pass,high pass,band pass and band stop filters using
% rectangular window
% input specifications
% pass band ripple = 0.05
% stop band ripple = 0.04
% pass edge frequency = 1500Hz
% stop edge frequency = 2000Hz
% sampling frequency = 9000Hz
clc;
clear all;
close all;
rp=input("enter the passband ripple: ");
rs=input("enter the stopband ripple: ");
fp=input("enter the passband frequency: ");
fs=input("enter the stopband frequency: ");
f=input("enter the sampling frequency: ");
wp= 2*fp/f
ws =2*fs/f
num = -20*log10(sqrt(rp*rs))-13;
den=14.6*(fs-fp)/f;
n=ceil(num/den)
n1 = n+1
if(rem(n,2)~=0)
n1=n;
n=n-1;
end
y=rectwin(n1);
stem(y)
b=fir1(n,wp,y)
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,1);
plot(o/pi,m,'b','LineWidth',1.5)
xlabel('Normalized frequency');
ylabel('Gain in db')
grid;
title('LPF')
b=fir1(n,wp,'high',y)
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,2);
plot(o/pi,m,'b','LineWidth',1.5)
xlabel('Normalized frequency');
ylabel('Gain in db')
grid;
title('HPF')
wn=[wp,ws];
b=fir1(n,wn,y)
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,3);
plot(o/pi,m,'b','LineWidth',1.5)
xlabel('Normalized frequency');
ylabel('Gain in db')
grid;
title('Bandpass filter')
b=fir1(n,wn,'stop',y)
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,4);
plot(o/pi,m,'b','LineWidth',1.5)
xlabel('Normalized frequency');
ylabel('Gain in db')
grid;
title('BandStop filter')
grid;
title('BandStop filter')
OUTPUT:
b) Hanning window :
SOURCE CODE:
% Experiment-8b Date:14/10/2024
% JAISRIHARI.G, CB.EN.U4ELC22015
% To design an FIR low pass, high pass, band pass and band stop filters using
% hanning window
% input specifications
% pass band ripple = 0.03
% stop band ripple = 0.01
% pass edge frequency = 1400Hz
% stop edge frequency = 2000Hz
% sampling frequency = 8000Hz
clc;
clear all;
close all;
rp = input('enter the passband ripple :');
rs = input('enter the stopband ripple :');
fp = input('enter the passband frequency :');
fs = input('enter t1he stopband frequency :');
f = input('enter the sampling frequency :');
% hanning window
y = hann(n1); % window of length n1
stem(y)
%bandpass filter
wn=[wp,ws];
b=fir1(n,wn,y) % fir design
[h,o]=freqz(b,1,256); % frequency response of the filter
m=20*log10(abs(h));
subplot(2,2,3);
plot(o/pi,m,'b','LineWidth',1.5)
xlabel('Normalized frequency');
ylabel('Gain in db')
grid;
title('Bandpass filter')
%bandstop filter
b=fir1(n,wn,'stop',y) % fir design
[h,o]=freqz(b,1,256); % frequency response of the filter
m=20*log10(abs(h));
subplot(2,2,4);
plot(o/pi,m,'b','LineWidth',1.5)
xlabel('Normalized frequency');
ylabel('Gain in db')
grid;
title('BandStop filter')
OUTPUT:
c) Hamming window :
SOURCE CODE:
% Experiment-8c Date:14/10/2024
% JAISRIHARI.G, CB.EN.U4ELC22015
% To design an FIR low pass,high pass,band pass and band stop filters using
% hamming window
% input specifications
% pass band ripple = 0.02
% stop band ripple = 0.01
% pass edge frequency = 1200Hz
% stop edge frequency = 1700Hz
% sampling frequency = 9000Hz
clc;
clear all;
close all;
rp=input('Enter the pass band ripple: ');
rs=input('Enter the stop band ripple: ');
fp=input('Enter the pass band frequency: ');
fs=input('Enter the stop band frequency: ');
f=input('Enter the sampling frequency: ');
wp=2*fp/f
ws=2*fs/f
num=-20*log10(sqrt(rp*rs))-13;
den=14.6*(fs-fp)/f;
n=ceil(num/den)
n1=n+1
if(rem(n,2)~=0)
n1=n;
n=n-1;
end
y=hamming(n1);
stem(y)
b=fir1(n,wp,y)
[h,o]=freqz(b,1,256);
m=20*log10(abs(h))
subplot(2,2,1);
plot(o/pi,m,'b','linewidth',1.5)
xlabel('normalized freq');
ylabel('gain in db')
grid;
title('LPF')
b=fir1(n,wp,'high',y)
[h,o]=freqz(b,1,256);
m=20*log10(abs(h))
subplot(2,2,2);
plot(o/pi,m,'b','linewidth',1.5)
xlabel('normalized freq');
ylabel('gain in db')
grid;
title('HPF')
wn=[wp,ws];
b=fir1(n,wn,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h))
subplot(2,2,3);
plot(o/pi,m,'b','linewidth',1.5)
xlabel('normalized freq');
ylabel('gain in db')
grid;
title('Bandpass filter')
b=fir1(n,wn,'stop',y)
[h,o]=freqz(b,1,256);
m=20*log10(abs(h))
subplot(2,2,4);
plot(o/pi,m,'b','linewidth',1.5)
xlabel('normalized freq');
ylabel('gain in db')
grid;
title('BandStop filter')
OUTPUT:
Inference:
The experiment successfully illustrated the creation of several FIR filters using three
different windowing methods: rectangular, Hanning, and Hamming. Each window yielded various
frequency responses. The rectangular window produced sharp transitions but caused significant
waves. In contrast, the Hanning window flattened the transitions, lowering ripple and improving
performance. The Hamming window proved to be the most effective, reducing sidelobes while
boosting stopband attenuation.
These distinctions emphasize the necessity of selecting the appropriate window for each
application. Overall, the experiment demonstrated the importance of windowing in FIR filter
design and how it affects filter behaviour across frequencies.
Submitted by:
Name: Jaisrihari.G Reg. No.: CB.EN.U4ELC22015
Conduction 5
Total 10
Verified by
Faculty Signature with date