Lab Report: Jatiya Kabi Kazi Nazrul Islam University
Lab Report: Jatiya Kabi Kazi Nazrul Islam University
Lab Report: Jatiya Kabi Kazi Nazrul Islam University
TRISHAL, MYMENSINGH
Lab Report
Course Name: Digital Signal Processing Lab
Course Code: CSE-410
Submitted To
Professor
Roll: 17102018
Reg.: 5695
Session: 2016-17
i) Sine wave
ii) Cosine wave
iii) Unit impulse
iv) Unit step wave
v) Square wave
vi) Exponential waveform
Software: Matlab.
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:
Software: Matlab.
Algorithm:
Step 2: Find the DFT of the input sequence using direct equation of DFT.
Step 4: Plot DFT and IDFT of the given sequence using matlab command stem.
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:
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,
Discrete Convolution
y(n)=x(n)∗h(n)
Algorithm:
Step 3: Find the convolution y[n] using the matlab command conv.
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
Software: Matlab.
Theory:
Algorithm:
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:
Software: Matlab.
K= Frequency Coefficient.
For FFT,
Algorithm:
Step 2: Find the length of the input sequence using length command.
Step 3: Find FFT and IFFT using matlab commands fft and ifft.
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
Software: Matlab.
Algorithm:
Step 3: Perform the interpolation on input signal using matlab command interp.
Program Code:
clc
clear
Output:
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
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:
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:
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;
[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...>');
[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.
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:
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
Highpass Filter
Bandstop Filter
Bandpass Filter
Outputs:
Lowpass Filter
Highpass Filter
Bandstop Filter
Band-Pass Filter