Lab Report: Jatiya Kabi Kazi Nazrul Islam University

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

JATIYA KABI KAZI NAZRUL ISLAM UNIVERSITY

TRISHAL, MYMENSINGH

Lab Report
Course Name: Digital Signal Processing Lab
Course Code: CSE-410

Submitted To

Dr. Md. Sujan Ali

Professor

Dept. of CSE, JKKNIU


Submitted By
Abu Bakar Sidddique

Roll: 17102018
Reg.: 5695
Session: 2016-17

Submission Date: 03.10.2021


INDEX

Experiment Experiment Name Page No.


No.

01 Representation of Basic signals 01-02


i) Sine wave
ii) Cosine wave
iii) Unit impulse
iv) Unit step wave
v) Square wave
vi) Exponential waveform

02 To find DFT / IDFT of given DT signal 03-05


03 Linear convolution of two sequences 06-07
04 Auto correlation 08-09
05 To find the FFT of a given sequence 10-12
06 Implementation of interpolation process 13-14
07 Computation of n-point DFT of a given sequences 15-16
08 Verification of sampling theorem 17-18
09 To Design a Butterworth digital IIR filter 19-22
10 Design of FIR filter: low pass filter, highpass filter, 23-28
bandstop filter, bandpass filter
Experiment No.01

Experiment Name: Representation of Basic signals

i) Sine wave
ii) Cosine wave
iii) Unit impulse
iv) Unit step wave
v) Square wave
vi) Exponential waveform

Objective: To learn to represent basic signals using Matlab.

Software: Matlab.

Theory: MATLAB is an interactive, matrix-based system for scientific and engineering


numeric computation and visualization. Its strength lies in the fact that complex numerical
problems can be solved easily and in a fraction of the time required by a programming
language such as Fortran or C.

Program Code:
clc
clear
t=0:0.01:1;
a=2;
b=a*sin(2*pi*2*t);
subplot(3,3,1);
stem(t,b);
xlabel('Time');
ylabel('Amplitude');
title('Sine wave');

t=0:0.01:1;
a=2;
b=a*cos(2*pi*2*t);
subplot(3,3,2);
stem(t,b);
xlabel('Time');
ylabel('Amplitude');
title('Cosine wave');

n=-5:5;
a=[zeros(1,5),ones(1,1),zeros(1,5)];
subplot(3,3,3);
stem(n,a);
xlabel('Time');
ylabel('Amplitude');
title('Unit impulse');

n=-5:5;
a=[zeros(1,5),ones(1,6)];
subplot(3,3,4);
stem(n,a);
xlabel('Time');
ylabel('Amplitude');
title('Unit step');

t=0:0.01:1;
a=2;
b=a*square(2*pi*2*t);
subplot(3,3,5);
stem(t,b);
xlabel('Time');
ylabel('Amplitude');
title('Square wave');

t=0:0.01:1;
a=2;
b=a*exp(2*pi*2*t);
subplot(3,3,6);
stem(t,b);
xlabel('Time');
ylabel('Amplitude');
title('Exponential Wave');

Output:

Discussion: We have successfully represented basic signals using matlab.


Experiment No.02

Experiment Name: To find DFT / IDFT of given DT signal

Objective: To learn to find DFT/IDFT of given DT signal.

Software: Matlab.

Theory: Basic equation to find the DFT of a sequence is given below.

Basic equation to find the IDFT of a sequence is given below.

Algorithm:

Step 1: Get the input sequence.

Step 2: Find the DFT of the input sequence using direct equation of DFT.

Step 3: Find the IDFT using the direct equation.

Step 4: Plot DFT and IDFT of the given sequence using matlab command stem.

Step 5: Display the above outputs.

Program Code:
clc;
close all;
clear all;
xn=input('Enter the sequence x(n):');
ln=length(xn);
xk=zeros(1,ln);
ixk=zeros(1,ln);

for k=0:ln-1
for n=0:ln-1
xk(k+1)=xk(k+1)+(xn(n+1)*exp((i)*2*pi*k*n/ln));
end
end
magnitude=abs(xk);
t=0:ln-1;
subplot(2,2,1);
stem(t,xn);
ylabel ('Amplitude');
xlabel ('Time Index');
title('Input Sequence');

t=0:ln-1;
subplot(2,2,2);
stem(t,magnitude);
ylabel ('Amplitude');
xlabel ('K');
title('Magnitude Response');
phase=angle(xk);

t=0:ln-1;
subplot(2,2,3);
stem(t,phase);
ylabel ('Phase');
xlabel ('K');
title ('Phase Response');

for n=0:ln-1
for k=0:ln-1
ixk(n+1)=ixk(n+1)+(xk(k+1)*exp(i*2*pi*k*n/ln));
end
end
ixk=ixk./ln;

t=0:ln-1;
subplot(2,2,4);
stem(t,ixk);
ylabel ('Amplitude');
xlabel ('Time Index');
title ('IDFT sequence');

Input
Output:

Discussion: We have successfully found DFT and IDFT of a given sequence.


Experiment No.03

Experiment Name: Linear convolution of two sequences.

Objective: To find linear convolution of two sequences using Matlab.

Software: Matlab.

Theory: 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(n)=x(n)∗h(n)

Where,

y (n) = output of LTI

x (n) = input of LTI

h (n) = impulse response of LTI

Discrete Convolution

y(n)=x(n)∗h(n)

By using convolution we can find zero state response of the system.

Algorithm:

Step 1: Give input sequence x[n].

Step 2: Give impulse response sequence h(n)

Step 3: Find the convolution y[n] using the matlab command conv.

Step 4: Plot x[n],h[n],y[n].

Program Code:
clc
clear
x1=input('Enter the first sequence:');
subplot(3,1,1);
stem(x1);
ylabel('amplitude');
title('Plot the first sequence');
x2=input('Enter second sequence:');
subplot(3,1,2);
stem(x2);
ylabel('amplitude');
title('Plot of second sequence');
f=conv(x1,x2);
disp('Output of linear convolution is');
disp(f);
xlabel('time index n');
ylabel('amplitude f');
subplot(3,1,3);
stem(f);
title('Linear convolution of sequence');

Input:

Output:

Discussion: We have successfully found the linear convolution of two given sequences.
Experiment No.04

Experiment Name: Find autocorrelation of a sequence.

Objective: To compute autocorrelation of an input sequence in matlab.

Software: Matlab.

Theory:

Auto Correlation Function Auto correlation function is a measure of similarity between a


signal & its time delayed version. It is represented with R(k). The auto correlation function of
x(n) is given by

Algorithm:

Step 1: Give input sequence x[n].

Step 2: Give impulse response sequence h (n)

Step 3: Find auto correlation using the matlab command xcorr.

Step 4: Plot x[n], h[n], z[n].

Step 5: Display the results

Program Code:
clc;
close all;
clear all;
% two input sequences
x=input('Enter input sequence:')
subplot(1,2,1);
stem(x);
xlabel('n');
ylabel('x(n)');
title('Input Sequence');
% auto correlation of input sequence
z=xcorr(x,x);
disp('The values of z are = ');
disp(z);
subplot(1,2,2);
stem(z);
xlabel('n');
ylabel('z(n)');
title('Auto correlation of input sequence');
Input:

Output:

Discussion: We have successfully computed autocorrelation of an input sequence using


matlab.
Experiment No.05

Experiment Name: To find the FFT of a given sequence.

Objective: To learn to find the FFT of a given sequence using matlab.

Software: Matlab.

Theory: DFT of a sequence

Where N= Length of sequence.

K= Frequency Coefficient.

n = Samples in time domain.

FFT: -Fast Fourier transform.

There are two methods.

1. Decimation in time (DIT ) FFT.

2. Decimation in Frequency (DIF) FFT.

Need for FFT

The no of multiplications in DFT = N2.

The no of Additions in DFT = N (N-1).

For FFT,

The no of multiplication = N/2 log2N.

The no of additions = N log2 N.

Algorithm:

Step 1: Give input sequence x[n].

Step 2: Find the length of the input sequence using length command.

Step 3: Find FFT and IFFT using matlab commands fft and ifft.

Step 4: Plot magnitude and phase response Step


Step 5: Display the results

Program Code:
clc;
clear all;
close all;
x=input('Enter the sequence : ')
N=length(x)
xK=fft(x,N)
xn=ifft(xK)
n=0:N-1;
subplot (2,2,1);
stem(n,x);
xlabel('n---->');
ylabel('amplitude');
title('Input sequence');
subplot (2,2,2);
stem(n,abs(xK));
xlabel('n---->');
ylabel('magnitude');
title('Magnitude response');
subplot (2,2,3);
stem(n,angle(xK));
xlabel('n---->');
ylabel('phase');
title('Phase responce');
subplot (2,2,4);
stem(n,xn);
xlabel('n---->');
ylabel('amplitude');
title('IFFT');

Input:
Output:

Discussion: We have successfully calculated the FFT for a given sequence using Matlab.
Experiment no.06

Experiment Name: Implementation of interpolation process.

Objective: To verify the decimation of given sequence.

Software: Matlab.

Theory: “Upsampling” is the process of inserting zero-valued samples between original


samples to increase the sampling rate. (This is called “zero-stuffing”.) “Interpolation”, is the
process of upsampling followed by filtering. The filtering removes the undesired spectral
images. The primary reason to interpolate is simply to increase the sampling rate at the output
of one system so that another system operating at a higher sampling rate can input the signal.

Algorithm:

Step 1: Define up sampling factor and input frequencies f1 and f2

Step 2: Represent input sequence with frequencies f1 and f2

Step 3: Perform the interpolation on input signal using matlab command interp.

Step 4: Plot the input and output signal/sequence.

Program Code:
clc
clear

L=input('Enter the upsampling factor:');


N=input('Enter the length of the input signal:');
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);
subplot(2,1,1)
stem(n,x(1:N))
title('Input sequence');
xlabel('time(n)');
ylabel('amplitude');
subplot(2,1,2)
m=0:N*L-1;
stem(m,y(1:N*L))
title('Output sequence ');
xlabel('time(n)');
ylabel('amplitude');

Output:

Discussion: We have successfully implemented the interpolation process.


Experiment No.07

Experiment Name: Computation of n-point DFT of a given sequences.

Objective: To learn to compute the n-point DFT of a sequence using matlab.

Software: Matlab.

Theory: An N-point DFT is expressed as the multiplication, where is the original input
signal, is the N-by-N square DFT matrix, and is the DFT of the signal.

Program Code:
clc
clear
N=input('Enter the value of N(Value of N in N-point DFT): ');
x=input('Enter the sequence for which DFT is to be calculated: ');
n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-1j*2*pi/N);
nk=n'*k;
WNnk=WN.^nk;
Xk=x*WNnk;
MagX=abs(Xk)
PhaseX=angle(Xk)*180/pi
figure(1);
subplot(2,1,1);
plot(k,MagX);
subplot(2,1,2);
plot(k,PhaseX);

Output:

Discussion: We have successfully computed n-point DFT for a given sequence using matlab.
Experiment No.08

Experiment Name: Verification of sampling theorem.

Objective: To learn to verify sampling theorem in matlab.

Software: Matlab.

Theory: The sampling theorem essentially says that a signal has to be sampled at least with
twice the frequency of the original signal. Since signals and their respective speed can be
easier expressed by frequencies, most explanations of artifacts are based on their
representation in the frequency domain.

Program Code:
clc
clear
T=0.04;
t=0:0.0005:0.02
f=1/T;
n1=0:40;
size(n1);
xa_t=sin(2*pi*2*t/T);
subplot(2,2,1);
plot(200*t,xa_t);
title('Verification of Sampling Theorem');
title('Continuous signal');
xlabel('t');
ylabel('x(t)');
ts1=0.002;
ts2=0.01;
ts3=0.1;
n=0:20;
x_ts1=2*sin(2*pi*n*ts1/T);
subplot(2,2,2);
stem(n,x_ts1);
title('Greater than Nq');
xlabel('n');
ylabel('x(n)');

n=0:4;
x_ts2=2*sin(2*sym('pi')*n*ts2/T);
subplot(2,2,3);
stem(n,x_ts2);
title('Equal to Nq');
xlabel('n');
ylabel('x(n)');
n=0:10;
x_ts3=2*sin(2*pi*n*ts3/T);
subplot(2,2,4);
stem(n,x_ts3);
title('Less than Nq');
xlabel('n');
ylabel('x(n)');
Output:

Discussion: We have successfully verified the sampling theorem using matlab.


Experiment No.09

Experiment Name: To Design a Butterworth digital IIR filter.

Objective: To learn to design Butterworth digital IIR filter using matlab.

Software: Matlab.

Theory: The Butterworth filter is a type of signal processing filter designed to have a
frequency response as flat as possible in the passband. It was first described in 1930 by the
British engineer and physicist Stephen Butterworth in his paper entitled "On the Theory of
Filter Amplifiers".

Algorithm:

Step 1: Get the passband and stopband ripples.

Step 2: Get the passband and stopband frequencies.

Step 3: Get the sampling frequency.

Step 4: Calculate the filter order using

Step 5: Find the filter coefficient.

Step 6: Draw the magnitude and phase response.

Program Code:
clc
clear
format long
rp=input('Enter the pass band ripple:');
rs=input('Enter the stop band ripple:');
wp=input('Enter the passband frequency:');
ws=input('Enter the stopband frequency:');
fs=input('Enter the sampling frequency:');
w1=2*wp/fs;
w2=2*ws/fs;

%low pass filter


[n,wn]=buttord(w1,w2,rp,rs);
[b,a]=butter(n,wn);
[b,a]=butter(n,wn);
W=0:0.01:pi;
[h,om]=freqz(b,a,W);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('gain in db...>');
xlabel('(a)normalized frequency...>');
subplot(2,1,2);
plot(om/pi,an);
xlabel('(b)normalized frequency...>');
ylabel('phase in radians...>');

%high pass filter


[n,wn]=buttord(w1,w2,rp,rs);
[b,a]=butter(n,wn,'high');
W=0:0.01:pi;

[h,om]=freqz(b,a,W);
m=20*log10(abs(h));
an=angle(h);
figure(2);
subplot(2,1,1);
plot(om/pi,m);
ylabel('gain in db...>');
xlabel('(a)normalized frequency...>');
subplot(2,1,2);
plot(om/pi,an);
xlabel('(b)normalized frequency...>');
ylabel('phase in radians...>');

%band pass filter


[n]=buttord(w1,w2,rp,rs);
wn=[w1,w2];
[b,a]=butter(n,wn,'bandpass');
W=0:0.01:pi;

[h,om]=freqz(b,a,W);
m=20*log10(abs(h));
an=angle(h);
figure(3);
subplot(2,1,1);
plot(om/pi,m);
ylabel('gain in db...>');
xlabel('(a)normalized frequency...>');
figure(3);
subplot(2,1,2);
plot(om/pi,an);

Input:
Outputs
Discussion: We have successfully designed a Butterworth digital IIR filter,
Experiment No.10

Experiment Name: Design of FIR filter: low pass filter, highpass filter, bandstop filter,
bandpass filter.

Objective: To design FIR filters using Matlab.

Software: Matlab.

Theory:

An FIR filter is designed by finding the coefficients and filter order that meet certain
specifications, which can be in the time domain (e.g. a matched filter) and/or the frequency
domain (most common). Matched filters perform a cross-correlation between the input signal
and a known pulse shape.

Program Code:

Low pass filter


clc;
clear all;
wc=input('Enter the value of cut off frequency: ');
N=input('Enter the value of filter: ');
alpha=(N-1)/2;
eps=0.001;
%Rectangular Window
n=0:1:N-1;
hd=sin(wc*(n-alpha+eps))./(pi*(n-alpha+eps));
hn=hd;
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h));
hold on
%Hamming Window
n=0:1:N-1;
wh=0.54-0.46*cos((2*pi*n)/(N-1));
hn=hd.*wh;
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'ms');
hold off;
hold on
%Hanning Window
n=0:1:N-1;
wh=0.5-0.5*cos((2*pi*n)/(N-1));
hn=hd.*wh;
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'blue');
hold off;
hold on
%Blackman Window
n=0:1:N-1; wh=0.42-0.5*cos((2*pi*n)/(N-1))+0.08*cos((4*pi*n)/(N-1));
hn=hd.*wh;
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'green');
hold off;

Highpass Filter
clc;
clear all;
wc=input('Enter the value of cut off frequency: ');
N=input('Enter the value of filter: ');
alpha=(N-1)/2;
eps=0.001;
%Rectangular Window
n=0:1:N-1;
hd=(sin(pi*(n-alpha+eps))-sin((n-alpha+eps)*wc))./(pi*(n-alpha+eps));
hn=hd;
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h));
hold on
%Hamming Window
n=0:1:N-1;
wh=0.54-0.46*cos((2*pi*n)/(N-1));
hn=hd.*wh;
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'ms');
hold off;
hold on
%Hanning Window
n=0:1:N-1;
wh=0.5-0.5*cos((2*pi*n)/(N-1));
hn=hd.*wh;
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'blue');
hold off;
hold on
%Blackman Window
n=0:1:N-1; wh=0.42-0.5*cos((2*pi*n)/(N-1))-0.08*cos((4*pi*n)/(N-1));
hn=hd.*wh;
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'green');
hold off;

Bandstop Filter
clc;
Wc1=input('Enter the value of Wc1=');
Wc2=input('Enter the value of Wc2=');
N=input('Enter the value of N=');
alpha=(N-1)/2;
eps=0.001;
%Rectangular Window
n=0:1:N-1; hd=(sin(Wc1*(n-alpha+eps))-sin(Wc2*(n-alpha+eps)*pi))./((n-
alpha+eps)*pi);
hn=hd;
W=0:0.01:pi;
h=freqz(hn,1,W);
plot(W/pi,abs(h));
hold on;
%Hamming Window
n=0:1:N-1;
Wn=0.54-0.46*cos((2*pi*n)/(N-1));
hn=hd.*Wn;
W=0:0.01:pi;
h=freqz(hn,1,W);
plot(W/pi,abs(h),'green');
hold on;
%Hanning Window
n=0:1:N-1;
Wn=0.5-0.5*cos((2*pi*n)/(N-1));
hn=hd.*Wn;
W=0:0.01:pi;
h=freqz(hn,1,W);
plot(W/pi,abs(h),'red');
hold off;
%Blackman Window
n=0:1:N-1;
wh=0.42-0.5*cos((2*pi*n)/(N-1))-0.08*cos((4*pi*n)/(N-1));
hn=hd.*wh;
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(W/pi,abs(h),'green');
hold off;

Band-Pass Filter
clc;
Wc1=input('Enter the value of Wc1=');
Wc2=input('Enter the value of Wc2=');
N=input('Enter the value of N=');
alpha=(N-1)/2;
eps=0.001;
%Rectangular Window
n=0:1:N-1;
hd=(sin(Wc1*(n-alpha+eps))-sin(Wc2*(n-alpha+eps)*pi))./((n-alpha+eps)*pi);
hn=hd;
W=0:0.01:pi;
h=freqz(hn,1,W);
plot(W/pi,abs(h));
hold on;
%Hamming Window
n=0:1:N-1;
Wn=0.54-0.46*cos((2*pi*n)/(N-1));
hn=hd.*Wn;
W=0:0.01:pi;
h=freqz(hn,1,W);
plot(W/pi,abs(h),'green');
hold on;
%Hanning Window
n=0:1:N-1;
Wn=0.5-0.5*cos((2*pi*n)/(N-1));
hn=hd.*Wn;
W=0:0.01:pi;
h=freqz(hn,1,W);
plot(W/pi,abs(h),'red');
hold off;
%Blackman Window
n=0:1:N-1;
wh=042-0.5*cos((2*pi*n)/(N-1))-0.08*cos((4*pi*n)/(N-1));
hn=hd.*wh;
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(W/pi,abs(h),'green');
hold off;

Inputs

Low pass filter

Highpass Filter

Bandstop Filter

Bandpass Filter
Outputs:

Lowpass Filter

Highpass Filter
Bandstop Filter

Band-Pass Filter

Discussion: We have successfully designed FIR filter using Matlab.

You might also like