You are on page 1of 51

Practical Lab File

Digital Signal Processing


Code: ECE311

Submitted by
NIKHIL RANJAN
A2305120017
6ECE1

Submitted to
Ms. Anupama Bhan
Assistant Professor

Department of Electronics and Communication Engineering


Amity School of Engineering and Technology
Amity University, Noida, Uttar Pradesh
Table of Contents
Date of Ma
S. Category of Exp Name of Date of M. Signature of
Code Allotment of x
No. Assignment No. Experiment Eval. Obt Faculty
Expt. M.

LR To plot different types of


Mandatory (10) signals in Continuous and
1. 1. Dscrete form 03/01/2023 10/01/2023 1
Experiment

To study the effect of


Mandatory sampling at different
2. 2. sampling frequencies 10/01/2023 17/01/2023 1
Experiment

Mandatory To perform linear and


3. 3. circular convolution. 17/01/2023 24/01/2023 1
Experiment
To perform Discrete
Mandatory Fourier Transform (DFT)
4. 4. 24/01/2023 31/01/2023 1
Experiment and its inverse.

To study various
Mandatory representations of digital
5. 5. 31/01/2023 7/02/2023 1
Experiment systems and determine
system stability

To perform FIR Filter


design using rectangular
Mandatory window method: Lowpass,
6. 6. 7/02/2023 14/02/2023 1
Experiment Highpass, Bandpass and
Bandstop filters

To design FIR Low-pass


Mandatory filter using Rectangular,
7. 7. Hamming and Hann 14/02/2023 21/02/2023 1
Experiment windows

To perform Digital IIR


Mandatory Butterworth Filter Design
8. 8. 21/02/2023 28/02/2023 1
Experiment of different orders.

To perform Digital IIR


Mandatory Butterworth Filter Design of
9. 9. 28/02/2023 7/03/2023 1
Experiment different orders.

10 Mandatory 10. To analyze the effects 7/03/2023 14/03/2023 1


Experiment of up-sampler and
down-sampler in
multi-rate systems
EXPERIMENT 1
AIM
To plot different types of signals in Continuous and Discrete form

SOFTWARE/HARDWARE REQUIRED:
Octave Online

THEORY:
In electrical engineering, the fundamental quantity of representing some information is called a
signal. It does not matter what the information is i-e: Analog or digital information. In
mathematics, a signal is a function that conveys some information. In fact any quantity
measurable through time over space or any higher dimension can be taken as a signal. A signal
could be of any dimension and could be of any form.
CODE
clc; clear all;
% time axis
n = -25:25;
% unit impulse
delta = n==0;

figure;
subplot(1,2,1);
plot(n,delta,'linewidth',1.5);
title ("continuous delta");
xlabel ("time");
ylabel ("Amplitude");

subplot(1,2,2);
stem(n,delta,'markersize',2);
title ("discrete delta");
xlabel ("n");
ylabel ("Amplitude");

%unit step
step = n>=0;

figure;
subplot(1,2,1);
plot(n,step);
title ("continuous step");
xlabel ("time");
ylabel ("Amplitude");

subplot(1,2,2);
stem(n,step);
title ("discrete step");
xlabel ("n");
ylabel ("Amplitude");

%ramp
ramp = n.*step;

figure;
subplot(1,2,1);
plot(n,ramp);
title ("continuous ramp");
xlabel ("time");
ylabel ("Amplitude");
subplot(1,2,2);
stem(n,ramp);
title ("discrete ramp");
xlabel ("n");
ylabel ("Amplitude");
% sinusoidal

a = 0.4;
t = 0:0.1:6;
sig=4*sin(2*pi*a*t);

figure;
plot(sig);
title("Sinusoidal Signal");
xlabel('Time');
ylabel('Amplitude');

figure;
stem(sig);
title("Sinusoidal Signal");
xlabel('Time');
ylabel('Amplitude');
% Exponential

X = -10:10;
res = exp(X);

figure;
plot(X,res);
title("Continuos Exponential");
xlabel('Time');
ylabel('Amplitude');

figure;
stem(X,res);
title("Discrete Exponential");
xlabel('Time');
ylabel('Amplitude');
OBSERVATIONS

Fig 1. Continuous Delta and Discrete Delta Signals

Fig 2. Continuous Step and Discrete Steps Signals


Fig 3. Continuous Ramp and Discrete Ramp Signals

Fig 4. Continuous Sinusoidal Signal


Fig 5. Discrete Sinusoidal Signal

Fig 6. Continuous Exponential Signal


Fig 7. Discrete Exponential Signal

CONCLUSION
Hence we plotted different types of signals in Continuous and Discrete form using MATLAB.

Criteria Total Marks Marks Obtained Comments


Concept (A) 2
Implementation (B) 2
Performance (C) 2
Total 6
EXPERIMENT 2
AIM
To study the effect of sampling at different sampling frequencies

SOFTWARE/HARDWARE REQUIRED:
Octave Online

THEORY:
Sampling: sampling is the reduction of a continuous-time signal to a discrete-time signal. A
common example is the conversion of a sound wave (a continuous signal) to a sequence of
samples (a discrete-time signal).

Fig1. Sampled signal of a continuous-time input signal

Sampling Rate:Sampling frequency is the reciprocal of the sampling period. This sampling
frequency can be simply called as Sampling rate.The sampling rate denotes the number of
samples taken per second, or for a finite set of values.To discretize the signals, the gap between
the samples should be fixed. That gap can be termed as a sampling period Ts
Sampling Frequency, FS = 1/TS
Where,TS = Sampling Time
FS = Sampling Frequency or Sampling Rate

Fig 2. Sampling rate illustration

Nyquist Rate:If the sampling frequency (Fs) equals twice the input signal frequency (Fm), then
such a condition is called the Nyquist Criteria for sampling. When sampling frequency equals
twice the input signal frequency is known as “Nyquist rate”.
Fs=2Fm

Where,FS = Sampling rate


Fm = Highest frequency

This rate of sampling is called as Nyquist rate.

Fig 3. Nyquist rate illustration


Shannon’s Sampling Theorem:
Sampling theorem states that “continues form of a time-variant signal can be represented in the
discrete form of a signal with help of samples and the sampled (discrete) signal can be recovered
to original form when the sampling signal frequency Fs having the greater frequency value than
or equal to the input signal frequency Fm.

Fs ≥ 2Fm
If the sampling frequency (Fs) is less than twice the input signal frequency, such criteria called
an Aliasing effect.
Fs<2Fm

Fig 4. Sampling-output-waveforms
Sampling at different Sampling Frequencies
Critical Sampling:A sampling is termed as a critical or perfect sampling when the sampling
frequency (FS) is equal to the twice of the input/message signal (Fm). The relation of
critical/perfect sampling is given as follows:
Fs= 2Fm

Fig 5. Critical/Perfect sampling

Under Sampling: Under sampling is a technique where one samples a bandpass-filtered signal at
a sample rate below its Nyquist rate (twice the upper cut-off frequency), but is still able to
reconstruct the signal.
In simple terms A sampling is termed to be an under sampling when the sampling frequency (FS)
is less than the twice of the input/message signal (Fm). The relation of an under sampling is
given as follows:

Fs< 2Fm

Fig 6. Under sampling


Over Sampling:A sampling is termed to be an over sampling when the sampling frequency (FS)
is greater than the twice of the input/message signal (Fm). The relation of an over sampling is
given as follows:
Fs> 2Fm

Fig 7. Over sampling

CODE
clc;
clear all;
close all;
A = 1; f0=9;
x = @(t) A.*cos(2*pi*f0.*t);
%% Critical Sampling
fs1 = 2*f0; %Sampling rate
t1 = 0:1/fs1:1; %time axis
sig1 = x(t1);
figure;
ezplot(x,[0 1]);
hold on;
stem(t1,sig1);
hold off;
ylabel('cos(2 \pi 5t)')
title('Critical Sampling, fs=10Hz')
%% Under Sampling
fs2 =f0; %Sampling rate
t2 = 0:1/fs2:1; %time axis
sig2 = x(t2);
figure;
ezplot(x,[0 1]);
hold on;
stem(t2,sig2);
hold off;
ylabel('cos(2 \pi 5t)')
title('Under Sampling, fs=5Hz')
%% Over Sampling
fs3 = 10*f0; %Sampling rate
t3 = 0:1/fs3:1; %time axis
sig3 = x(t3);
figure;
ezplot(x,[0 1]);
hold on;
stem(t3,sig3);
hold off;
ylabel('cos(2 \pi 5t)')
title('Over Sampling, fs=50Hz')
%% Over Sampling
fs4 = 74; %Sampling rate
t4 = 0:1/fs4:1; %time axis
sig4 = x(t4);
figure;
ezplot(x,[0 1]);
hold on;
stem(t4,sig4);
hold off;
ylabel('cos(2 \pi 5t)')
title('Over Sampling, fs=74Hz')

OBSERVATIONS
Fig 8. Criticial Sampling

Fig 9. Under Sampling

Fig 10. Over sampling at 50Hz

Fig 11. Over Sampling at 74 Hz

CONCLUSION
Hence we studied the effect of sampling at different sampling frequencies on MATLAB.

Criteria Total Marks Marks Obtained Comments


Concept (A) 2
Implementation (B) 2
Performance (C) 2
Total 6

EXPERIMENT 3
AIM
To perform linear and circular convolution.

SOFTWARE/HARDWARE REQUIRED:
Octave Online

THEORY:
Convolution:
Convolution is a mathematical operation used to express the relation between input and output of
an LTI system. It relates input, output and impulse response of an LTI system as
Y(t) = X(t) ∗ H(t)
Where Y(t) = Output of LTI system
X(t) = Input of LTI system
H(t) = Impulse Response of LTI system

Fig1. Convolution of signal illustration

Discrete Convolution: Given the impulse response of a discrete-time LTI system ℎ[𝑛], its
response to any general input 𝑥[𝑛] is given as

𝑦[𝑛] = 𝑥[𝑛]∗ ℎ[𝑛]


Fig 2. Discrete System and its convolution equations

Linear Convolution:
Linear convolution is a mathematical operation done to calculate the output of any Linear-Time
Invariant (LTI) system given its input and impulse response. It is represent as:

g[n] = Input signal


h[n] = Impulse response of the LTI system

 Used to find response to a filter to any general input.


 Convolution length 𝑁𝑦 = 𝑁𝑔 + 𝑁ℎ − 1
 𝑔 [𝑛] and ℎ [𝑛] are not periodic.

Circular Convolution:
Circular convolution is essentially the same process as linear convolution. Just like linear
convolution, it involves the operation of folding a sequence, shifting it, multiplying it with
another sequence, and summing the resulting.
However, in circular convolution, the signals are all periodic. Thus, the shifting can be thought
of as actually being a rotation. Since the values keep repeating because of the periodicity. Hence,
it is known as circular convolution.

g[n] = Input signal


h[n] = Impulse response of the LTI system

 Used to calculate linear convolution more efficiently (using DFT)


 Convolution Length 𝑁𝑦 = 𝑁𝑔 = 𝑁ℎ = 𝑁
 𝑔 [𝑛] and ℎ [𝑛] are periodic with period N
CODE
clc;
clear all;
close all;
pkg load signal;
g=[1,2,3,4]; Ng=4;
h= [5,6]; Nh=2;
%linear convolution
y1=conv(g,h)

%%circular convolution with modulo 4


N=4;
y2= cconv(g,h,N)

%%
Ny=Ng+Nh-1;
y2a= cconv(g,h,Ny)

figure;
subplot(3,2,1);
stem(0: Ng-1,g); grid on;
xlabel('Discrete Time,n');
ylabel('g[n]');
xlim([-0.5 4.5])

subplot(3,2,2);
stem(0: Nh-1,h); grid on;
xlabel('Discrete Time,n');
ylabel('h[n]');
xlim([-0.5 4.5])

subplot(3,2,3);
stem(0:(Ng+Nh-1)-1,y1); grid on;
xlabel('Discrete Time,n');
ylabel('linear conv, y1[n]');
xlim([-0.5 4.5])

subplot(3,2,4);
stem(0:N-1,y2); grid on;
xlabel('Discrete Time,n');
ylabel('Circular conv, y2[n]')
xlim([-0.5 4.5])
subplot(3,2,5);
stem(0:Ny-1,y2a); grid on;
xlabel('Discrete Time,n');
ylabel('Circular conv, y2a[n]')
xlim([-0.5 4.5])

OUTPUT
y1 =

5 16 27 38 24

y2 =

29 16 27 38

y2a =

5 16 27 38 24
Fig 3. Linear and Circular Convolution in OCTAVE Online

CONCLUSION
Hence we performed linear and circular convolution on MATLAB.

Criteria Total Marks Marks Obtained Comments


Concept (A) 2
Implementation (B) 2
Performance (C) 2
Total 6
EXPERIMENT 4
AIM
To perform Discrete Fourier Transform (DFT) and its inverse.

SOFTWARE/HARDWARE REQUIRED:
Octave Online

THEORY:
Fourier Transform- The Fourier Transform is a mathematical technique that transforms a
function of time, x(t), to a function of frequency, X(jω).
Discrete Fourier Transform - In mathematics, the discrete Fourier transform (DFT) converts
a finite sequence of equally spaced samples of a function into a same-length sequence of
equally spaced samples of the discrete-time Fourier transform (DTFT), which is a complex-
valued function of frequency. The interval at which the DTFT is sampled is the reciprocal of
the duration of the input sequence.
The discrete Fourier transform transforms a sequence of N complex numbers {XN} = x0,
x1,…, xN-1 into another sequence of complex numbers, {X[k]} = X0, X1,..., XN-1 which
is defined by:

Fig 1. Discrete Fourier Transform expression

Inverse Discrete Fourier Transform – An inverse DFT is a Fourier series, using the DTFT
samples as coefficients of complex sinusoids at the corresponding DTFT frequencies. It has the
same sample-values as the original input sequence. The inverse DFT (IDFT) transforms NN
discrete-frequency samples to the same number of discrete- times.

Fig 2. Inverse Discrete Fourier Transform expression


Matrix-form of DFT:
An N-point DFT is expressed as the multiplication ,
An N-point Inverse DFT is expressed as the multiplication

WhereXN is the original input signal, WN is the N-by- N square DFT


matrix, and is the DFT of the signal.
The transformation matrix can be defined as , or equivalently:
Some of the Commands used in the Code:
 Fourier Transform- It computes the discrete Fourier transform (DFT) of X using a
fast Fourier transform (FFT)algorithm.
 Syntax- y=fft(x)
 Inverse fast Fourier transform- It returns Modulo-n circular convolution.
 Syntax- X = ifft(Y)

CODE
clc;
clear all;
%short sequence
x= [1;1;1;1]
%4 point DFT and iDFT
N=4;
x_fft=fft(x,N)
x_fft= ifft(x_fft,N)

%8 point DFT and iDFT


N=8;
x_8fft=fft(x,N)
x_8fft= ifft(x_8fft,N)

%Generate sinusoid
T=2;
f0=5; %Fundamental Freq= 5 cycles per sec
fs=50; %sampling rate-50 samples per second
t = 0:1/fs:T-1/fs;
y=cos(2*pi*f0.*t);
Ny=128; %take value greater than 100 to recover the sig
y_fft= fft(y,Ny);
%freq axis
f = 0:fs/Ny:fs-fs/Ny;

figure;
subplot(2,1,1);
stem(t,y,'markersize',1);
xlabel('Time,s');
ylabel('cosine');
subplot(2,1,2);
stem(f,abs(y_fft),'markersize',1);
xlabel('Freq,Hz');
ylabel('|y[k]|');

OUTPUT
x =

1
1
1
1

x_fft =

4
0
0
0

x_fft =

1
1
1
1

x_8fft =

4.0000 + 0i
1.0000 - 2.4142i
0 + 0i
1.0000 - 0.4142i
0 + 0i
1.0000 + 0.4142i
0 - 0i
1.0000 + 2.4142i

x_8fft =

1.0000
1.0000
1.0000
1.0000
0
0
0.0000
0

Fig 3. Discrete Fourier Transform (DFT) and its inverse on OCTAVE Online

CONCLUSION
Hence we performed Discrete Fourier Transform (DFT) and its inverse on MATLAB.

Criteria Total Marks Marks Obtained Comments


Concept (A) 2
Implementation (B) 2
Performance (C) 2
Total 6
EXPERIMENT 5
AIM
To study various representations of digital systems and determine system stability

SOFTWARE/HARDWARE REQUIRED:
Octave Online

THEORY:
Transfer Function:
A transfer function (or system function) is a frequency domain representation of a dynamical
system. They are used for linear time-invariant systems. Nonlinear or time-varying systems need
different analysis techniques.

where X (z) and Y(z)


are the z-transforms of the system input x[n] and output y[n], respectively.
Frequency Response:
Frequency response is the quantitative measure of the output spectrum of a system or device in
response to a stimulusand is used to characterize the dynamics of the system. It is a measure of
magnitude and phase of the output as a function of frequency, in comparison to the input.

Impulse Response:
In signal processing, the impulse response, or impulse response function (IRF), of a dynamic
system is its output when presented with a brief input signal, called an impulse. More generally,
an impulse response is the reaction of any dynamic system in response to some external change.

Poles and Zeroes:


 The poles of the characteristic equation can be found by solving the denominator for
X(z)=0.
 The zeros of the characteristic equation can be found by solving the numerator for
Y(z)=0.
System Stability:
System is Stable if

 condition for existence of Fourier transform


Thus, 𝐻 (𝑒𝑗𝜔) exists-
 𝐻 (𝑧) for |𝑧| = 1 exists (note: 𝑧 = 𝑟𝑒𝑗𝜔)
ROC of (𝑧) includes the unit circle i.e., Poles are inside the unit circle (for causal system).

Some Commands used in the code:


freqz:It gives Frequency response of digital filter.
 Syntax- freqz(b,a);
Impz: It gives Impulse response of digital filters
 Syntax- impz(b,a);
tf2zp: Convert transfer function filter parameters to zero-pole-gain form
 Syntax-[z,p] = tf2zp(b,a)
zp2tf: Convert zero-pole-gain filter parameters to transfer function form.
 Syntax-[b,a] = zp2tf(z,p,k)

CODE
clc;
clear all;
close all;
%system 1
%denomcoeff
b= [0.21 0.41 0.21]

%num Coeff
a= [1 -0.37 0.19]

%freq response
figure;
freqz(b,a)

%Impulse response
figure;
impz(b,a)

%pole-zero plot
[zer1,pol1]= tf2zp(b,a)
%zeroes=2,pole=2
figure;
zplane(zer1,pol1)
%system 2
zer2= zer1;
pol2= pol1 + 1;

%transfer func
[b2,a2]= zp2tf(zer2,pol2,1)
figure;
zplane(zer2,pol2);

%Frequency Response
figure;
freqz(b2,a2)

%Impulse response system 2


figure;
impz(b2,a2)

OUTPUT
b =

0.2100 0.4100 0.2100

a =

1.0000 -0.3700 0.1900


zer1 =

-0.9762 + 0.2169i
-0.9762 - 0.2169i

pol1 =

0.1850 + 0.3947i
0.1850 - 0.3947i
b2 =

1.0000 1.9524 1.0000

a2 =

1.0000 -2.3700 1.5600


Fig 1. Frequency Response of System 1

Fig 2. Impulse Response of System 1


Fig 3. Pole – Zero Plot for System 1

Fig 4. Frequency Response of System 2


Fig 5. Impulse Response of System 2

Fig 6. Pole – Zero Plot for System 2

CONCLUSION
Hence we studied various representations of digital systems and determine system stability on
MATLAB

Criteria Total Marks Marks Obtained Comments


Concept (A) 2
Implementation (B) 2
Performance (C) 2
Total 6
EXPERIMENT 6
AIM
To perform FIR Filter design using rectangular window method: Lowpass, Highpass, Bandpass
and Bandstop filters

SOFTWARE/HARDWARE REQUIRED:
Octave Online

THEORY:
FIR Filter-A finite impulse response (FIR) filter is a filter whose impulse response (or response
to any finite length input) is of finite duration because it settles to zero in finite-time. It is
a filter structure that can be used to implement almost any sort of frequency response digitally.
Mathematically FIR can be represented in these equations:

Fig 1. FIR Filter equation


a) Low pass filter- Low Pass filter allow low-frequency signals without any attenuation
(decrease in power) but it rejects any high-frequency signals.

b) High pass filter- The type of filter that allows the high-frequency signals to pass without
any attenuation in its amplitude & blocks (rejects) any low-frequency signal is called
high pass filter.
c) Band pass filter:The band pass filter allows signals falling within a certain frequency
band set up between two points to pass through while blocking both the lower and higher
frequencies either side of this frequency band.

d) Band stop filter:The band stop filter blocks signals falling within a certain frequency
band set up between two points while allowing both the lower and higher frequencies
either side of this frequency band.

FIR filter using rectangular window method- FIR filters can be obtained using
truncation(estimation for the number without doing any rounding) of IIR response with suitable
window functions.

Fig 2.
Some of the Commands used in the Code:

 rectwin: Itreturns a rectangular window of length L.


Syntax- w = rectwin(L)

 fir: It is used for Window-based FIR filter design and designs a lowpass, highpass,
bandpass, bandstop, or multiband filter, depending on the value of ftype and the number
of elements of Wn.
Syntax- b = fir1(n,Wn,ftype)
CODE
clc;
clear all;
close all;

N=40;%order
Fs= 8000; % sampling freq
win= rectwin(N+1);

%%Low-Pass filter
fc=1000; % Cutofffreq(Hz)
bLPF=fir1(N,2*fc/Fs,'low',win);
%impulse response
figure;impz(bLPF,1);
%Frequency response
figure;freqz(bLPF,1);

%band pass filter


fc1= 1000;
fc2= 2000;
bBPF=fir1(N,[2*fc1/Fs 2*fc2/Fs],'bandpass',win);
%impulse response
figure;impz(bBPF,1);
%Frequency response
figure;freqz(bBPF,1);

%% band-stop filter
fc1=1000;
fc2=2000;
bBSF=fir1(N,[2*fc1/Fs 2*fc2/Fs],'stop',win);
%impulse response
figure;impz(bBSF,1);
%Frequency response
figure;freqz(bBSF,1);

%% High-pass filter
fc3=1000; % Cutofffreq(Hz)
bHPF=fir1(N,2*fc3/Fs,'high',win);
%impulse response
figure;impz(bHPF,1);
%Frequency response
figure;freqz(bHPF,1);

OUTPUT

Fig 3. Low Pass Filter Impulse Response

Fig 4. Low pass filter Frequency Response


Fig 5. Band Pass Filter Impulse Response

Fig 6. Band Pass Filter Frequency Response


Fig 7. Band Stop Filter Impulse Response

Fig 8. Band Stop Filter Frequency Response


Fig 9. High Pass Filter Impulse Response

Fig 10. High Pass Filter Impulse Response

CONCLUSION
Hence we studied FIR Filter design using rectangular window method: Lowpass, Highpass,
Bandpass and Bandstop filters on MATLAB

Criteria Total Marks Marks Obtained Comments


Concept (A) 2
Implementation (B) 2
Performance (C) 2
Total 6
EXPERIMENT 7
AIM
To design FIR Low-pass filter using Rectangular, Hamming and Hann windows

SOFTWARE/HARDWARE REQUIRED:
Octave Online

THEORY:
FIR Filters – In signal processing, a finite impulse response (FIR) filter is a filter whose impulse
response (or response to any finite length input) is of finite duration, because it settles to zero in
finite time. This is in contrast to infinite impulse response (IIR) filters, which may have internal
feedback and may continue to respond indefinitely (usually decaying).

LPF Filter Designing:


Low Pass Filter:
Low pass filter allow low frequency signals without any attenuation (decrease in power) but it
rejects any high frequency signals.

Order of filter N
Filter length M=N+1

Window Functions:

Rectangular Window
 Abrupt truncation
 Hamming and Hann window have tapering towards edges
 Hence truncation is less abrupt
 narrower transition band
 less attenuation in stop band

Hamming, Hann Windows:


• wider transition band
• more attenuation in stop band.
• Attenuation in stop-band Rectangular< Hamming

Some Commands used in the Code:


 w = rectwin(L) returns a rectangular window of length L.
 w = hamming(L) returns an L-point symmetric Hamming window.
 w = hann(L) returns an L-point symmetric Hann window.
 [h,w]=freqz(b,a,n) returns the n-point frequency response vector h and the corresponding
angular frequency vector w for the digital filter with transfer function coefficients stored
in b and a.
 plot(w/pi,20*log10(abs(h))) used for plotting only the magnitude(dB) vs freq(Hz) plot.

CODE
clc; clear all;
N = 40; % order
Fc = 1000; % Cutofffreq (HZ)
Fs = 8000; % Sampling freq (HZ)

%% Compare windows
win1 = rectwin(N+1);
win2 = hamming(N+1);
win3 = hann(N+1);

figure;
hold on;
stem(0:N,win1,'markersize',2);
stem(0:N,win2,'markersize',2);
stem(0:N,win3,'markersize',2);
hold off;
legend('Rectangular','Hamming','Hann');

xlabel('Discrete Time,n');
ylabel('window function');

%% Low-pass filter
b1 = fir1(N,2*Fc/Fs,'low',win1);
b2 = fir1(N,2*Fc/Fs,'low',win2);
b3 = fir1(N,2*Fc/Fs,'low',win3);

[h1,w1] = freqz(b1,1,1024,Fs);
[h2,w2] = freqz(b2,1,1024,Fs);
[h3,w3] = freqz(b3,1,1024,Fs);

figure;
hold on;
plot(w1, 20*log10(abs(h1)));
plot(w2, 20*log10(abs(h2)));
plot(w3, 20*log10(abs(h3)));
hold off;
legend('Rectangular','Hamming','Hann');
xlabel('Frequency, Hz');
ylabel('|H(w)|,LPF dB');

OUTPUT

Fig 1. Comparison of Different Window Functions


Fig 2. Comparison of Frequency Response (Magnitude(dB)) of different windows
(Rectangular, Hamming and Hann)

CONCLUSION
Hence we designed FIR Low-pass filter using Rectangular, Hamming and Hann windows.

Criteria Total Marks Marks Obtained Comments


Concept (A) 2
Implementation (B) 2
Performance (C) 2
Total 6
EXPERIMENT 8
AIM
To perform Digital IIR Butterworth Filter Design of different orders.

SOFTWARE/HARDWARE REQUIRED:
Octave Online

THEORY:
IIR Filter:
The infinite impulse response (IIR) filter is a recursive filter in that the output from the filter is
computed by using the current and previous inputs and previous outputs. Because the filter uses
previous values of the output, there is feedback of the output in the filter structure.

Butterworth Filter:
The Butterworth filter is a type of signal processing filter designed to have a frequency
response as flat as possible in the passband. It is also referred to as a maximally flat magnitude
filter.
The frequency response of the Butterworth filter is flat in the passband (i.e. a bandpass filter) and
roll-offs towards zero in the stopband. The rate of roll-off response depends on the order of the
filter. The number of reactive elements used in the filter circuit will decide the order of the filter.

Fig 1. Frequency Response of Butterworth Filter

Syntax:
 [b,a] = butter(n,Wn) returns the transfer function coefficients of an nth-order lowpass digital
Butterworth filter with normalized cutoff frequency Wn.
 [n,Wn] = buttord(Wp,Ws,Rp,Rs) returns the lowest order, n, of the digital Butterworth filter
with no more than Rp dB of passband ripple and at least Rs dB of attenuation in the
stopband. Wp and Ws are respectively the passband and stopband edge frequencies of the
filter, normalized from 0 to 1, where 1 corresponds to π rad/sample.
 [h,w] = freqz(b,a,n) returns the n-point frequency response vector h and the corresponding
angular frequency vector w for the digital filter with transfer function coefficients stored
in b and a.
 plot(w/pi,20*log10(abs(h))) used for plotting only the magnitude(dB) vs freq(Hz) plot.

CODE
clc;
clear all;
close all;
Fs = 8000; %sampling freq
fp= 1500; %passband edge freq
fs= 2000; %stopband edge freq
Ap = 3; %passband ripple

As = 40; %stopband attenuation

%Filter order
[N,wc] = buttord(2*fp/Fs,2*fs/Fs,Ap,As);

%%Butterworth Filter designing


[b1,a1] = butter(N-5,wc,'low');
[b2,a2] = butter(N,wc,'low');
[b3,a3] = butter(N+5,wc,'low');

%%plots
[h1,w1]= freqz(b1,a1,1024,Fs);
[h2,w2]= freqz(b2,a2,1024,Fs);
[h3,w3]= freqz(b3,a3,1024,Fs);

figure;
hold on;
plot(w1,20*log10(abs(h1)) );
plot(w2,20*log10(abs(h2)) );%magnitude of H(jw) on y axis
plot(w3,20*log10(abs(h3)) );
hold off;
grid on;
legend('N-5','N','N+5');
xlabel('Frequency,Hz');
ylabel('|H(\omega)|,dB');

ylim([-70 1]);xlim([0 2800]);

OUTPUT
Fig 2. Comparison of different Frequency Responses with Different Orders

CONCLUSION
Successfully studied and designed an IIR Butterworth Filter with minimum order that would
allow specified passband ripple and the specified at least attenuation in stopband, order less than
the minimum order, and more than the minimum order.
We observed that the passband is maximally flat, there are no ripples in pass band and stop band.
The magnitude response is monotonically decreasing. On increasing the order, the transition
band becomes sharper, so we move closer to the ideal filter response.

Criteria Total Marks Marks Obtained Comments


Concept (A) 2
Implementation (B) 2
Performance (C) 2
Total 6
EXPERIMENT 9
AIM
To perform Digital IIR Butterworth Filter Design of different orders.

SOFTWARE/HARDWARE REQUIRED:
Octave Online

THEORY:
Chebyshev Function:
It is a mathematical function used to produce passband, or stopband, ripples constrained within
fixed bounds. There are families of Chebyshev functions based on the amount of ripple such as 1
dB, 2 dB, and 3 dB of ripple.
Chebyshev filters can be designed to have a frequency response with ripples in the passband and
flat passbands (Chebyshev Type I), or flat passbands and ripples in the stopband (Chebyshev
Type II). Chebyshev filters cannot have ripples in both the passband and the stopband.

Type-I Chebyshev Filter:


This type of filter is the basic type of Chebyshev filter. Exhibits equi-ripple behaviour in
Passband.

Fig 1. Type I Chebyshev Filter

Type II Chebyshev Filter:


The type II Chebyshev filter is also known as an inverse filter, this type of filter is less common.
Because it does not roll off and needs various components. It has no ripple in the passband, but it
has equi-ripple in the stopband.
Fig 2. Type II Chebyshev Filter.

Some Commands used in the code:


 [n,Wp] = cheb1ord(Wp,Ws,Rp,Rs) returns the lowest order n of the Chebyshev Type I
filter that loses no more than Rp dB in the passband and has at least Rs dB of attenuation
in the stopband. The scalar (or vector) of corresponding cutoff frequencies Wp is also
returned.

 [b,a] = cheby1(n,Rp,Wp,ftype) designs a lowpass, high-pass, bandpass, or band-stop


Chebyshev Type I filter, depending on the value of ftype and the number of elements of
Wp. The resulting bandpass and band-stop designs are of order 2n.

CODE
clc; clear all;
Fs = 8000; % Sampling Frequency
fp = 1500;
fs = 2000;
Ap = 3;
As = 40;
%% Filter Order
[N1,wc1] = buttord(2*fp/Fs,2*fs/Fs,Ap,As);
[N2,wc2] = cheb1ord(2*fp/Fs,2*fs/Fs,Ap,As);
[N3,wc3] = cheb2ord(2*fp/Fs,2*fs/Fs,Ap,As);
disp('Order of Butterworth Filter:')
disp(N1)
disp('Order of Chebyshev Type I Filter:')
disp(N2)
disp('Order of Chebyshev Type II Filter:')
disp(N3)
%% Filter Design
[b1,a1] = butter(N1,wc1,'low'); %Butterworth
[b2,a2] = cheby1(N2,Ap,wc2,'low'); %Chebyshev Type I LPF
[b3,a3] = cheby2(N3,As,wc3,'low'); %Chebyshev Type II LPF
%% Plots
[h1,w1] = freqz(b1,a1,1024,Fs);
[h2,w2] = freqz(b2,a2,1024,Fs);
[h3,w3] = freqz(b3,a3,1024,Fs);
figure;hold on;
plot(w1,20*log10(abs(h1)),'r');
plot(w2,20*log10(abs(h2)),'k');
plot(w3,20*log10(abs(h3)),'m');
hold off;
xlabel('Frequency, Hz');
ylabel('|H(\omega)|, dB');
title('IIR LPF');
legend('Butterworth','ChebyshevTypeI','Chebyshev TypeII');
axis tight; grid on;
%%
figure;hold on;
plot(w1,20*log10(abs(h1)),'r');
plot(w2,20*log10(abs(h2)),'k');
plot(w3,20*log10(abs(h3)),'m');
hold off;
xlabel('Frequency, Hz');
ylabel('|H(\omega)|, dB');
title('IIR LPF: zoomed in');
legend('Butterworth','ChebyshevTypeI','Chebyshev TypeII');
axis tight;ylim([-60 2]); grid on;

OUTPUT
Order of Butterworth Filter:
12
Order of Chebyshev Type I Filter:
6
Order of Chebyshev Type II Filter:
6

Fig 3. IIR Low Pass Filter Waveform


Fig 4. IIR Low Pass Filter Zoomed in Waveform

CONCLUSION
Successfully studied and designed Chebyshev Filter of Type I and Type II. For the given values
of Attenuation of pass band, stop band and certain order N, Chebyshev Filter gives Narrower
Transition Band. AlsoChebyshev Filter of Type I has ripples in pass band, while the Chebyshev
Filter of Type II has ripples in the stop band.

Criteria Total Marks Marks Obtained Comments


Concept (A) 2
Implementation (B) 2
Performance (C) 2
Total 6
EXPERIMENT 10
AIM
To analyze the effects of up-sampler and down-sampler in multi-rate systems

SOFTWARE/HARDWARE REQUIRED:
Octave Online

THEORY:
Up Sampler
Up sampling can be synonymous with expansion, or it can describe an entire process of
expansion and filtering (interpolation). When up sampling is performed on a sequence of
samples of a signal or other continuous function, it produces an approximation of the sequence
that would have been obtained by sampling the signal at a higher rate.

Changing the Sampling Rate


 Upsample => Increase Sample Rate
 Upsample by L => Insert (L-1) zeros in between adjacent samples
 Upsample by 3 => Insert 2 zeros in between adjacent samples

Fig 1. Up Sampled waveform

Down Sampler
In digital signal processing, down sampling, compression, and decimation are terms associated
with the process of resampling in a multi-rate digital signal processing system. Both down
sampling and decimation can be synonymous with compression, or they can describe an entire
process of bandwidth reduction (filtering) and sample-rate reduction.

Changing the Sampling Rate


 Downsample => Decrease Sample Rate
 Downsample by M => Keep one sample, discard (M-1) samples
 Downsample by 3 => Keep one sample, discard 2 samples
Fig 2. Down Sampled waveform

Some Commands used in the Code:


y = upsample(x,n) increases the sample rate of x by inserting n – 1 zeros between samples. If x
is a matrix, the function treats each column as a separate sequence.
y = downsample(x,n) decreases the sample rate of x by keeping the first sample and then every
nth sample after the first. If x is a matrix, the function treats each column as a separate sequence.
y = resample(x,p,q) resamples the input sequence, x, at p/q times the original sample rate.
resample applies an FIR Antialiasing Lowpass Filter to x and compensates for the delay
introduced by the filter. The function operates along the first array dimension with size greater
than 1.

CODE
clc; clear all;
%Generate Sinusoid
T = 0.3; %Total Time Duration
fo=5; %Frequency of Sinsuoid
fs=600; %Sampling Frequency
t1 = 0:1/fs:T-1/fs; %Time Vector
y1= sin(2*pi*fo.*t1);

%Upsample: Increase Sample Rate by Integer Factor, 4


y1u= interp(y1,4);
t1u=0:1/(4*fs):T-1/(4*fs);

%Downsample: Decrease Sample Rate by Integer Factor, 4


y1d= decimate(y1,4);
t1d=0:1/(fs/4):T-1/(fs/4);

%Resample: Change Sample Rate by 2/3


y1r = resample (y1,2,3);
t1r=0:1/(2*fs/3):T-1/(2*fs/3);

%Plot
figure;
subplot(411); stem(t1,y1,'markersize',1); axis tight;
ylabel('Original');
subplot(412); stem(t1u,y1u,'markersize', 1); axis tight;
ylabel('Interpolation by 4');
subplot(413); stem(t1d,y1d,'markersize', 1); axis tight;
ylabel('Decimate by 4');
subplot(414); stem(t1r,y1r,'markersize', 1); axis tight;
ylabel('Resample by 2/3');

OUTPUT

Fig 3. Original Signal, UpSampled Signal, DownSampled Signal and Re Sampled Signal

CONCLUSION
Upsampling increases the number of signals whereas downsampling decreases the number of
signals and hence analyzed the effects of up-sampler and down-sampler in multi-rate systems.

Criteria Total Marks Marks Obtained Comments


Concept (A) 2
Implementation (B) 2
Performance (C) 2
Total 6

You might also like