DSP Lab Manual New MLR15 PDF

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

MLR INSTITUTE OF TECHNOLOGY

Dundigal, hyderabad

LAB MANUAL
Digital Signal Processing

Date of Issue Compiled by Authorized by

Document No: 01-July-2009 N. Vasudheva reddy


MLRIT/EC/LAB
MANUAL/DSP Date of Revision HOD(ECE)
Verified by
09-DEC-2017 Dr. Arul Ananth

Department of Electronics and Communication Engineering

Department of Electronics and Communication Engineering


1
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Department
Of
Electronics & Communication Engineering

Digital Signal Processing


Lab Manual

(III B.TECH II SEM - ECE)

MLR15

Department of Electronics and Communication Engineering

2
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

LIST OF EXPERIMENTNS

PART – A

1. Generation of Various Signals and sequences


2. Operations on signals and Sequences such as addition, Multiplication, scaling, Shifting,
folding, computation of energy and average power.
3. Convolution between Signals and sequences
4. Auto Correlation and Cross Correlation between Signals and sequences.
5. Verification of Linearity and Time Invariance properties of a given Continuous/Discrete
System
6. Generation of Sinusoidal waveform / signal based on recursive difference equations.
7. To find DFT/IDFT of given DT signal.
8. To find frequency response of a given system given in (Transfer Function/Differential
equation form).
9. Implementation of FFT of given sequence.

PART – B

1. Determination of Power Spectrum of a given signal(s).


2. Implementation of LPF, HPF, BPF, BSF FIR filter for a given sequence.
3. Implementation of LPF IIR filters for a given sequence.
4. Generation of Sinusoidal signal through filtering.
5. Implementation of Decimation and Interpolation Process.
6. Implementation of sampling rate I/D converters.
7. Noise removal: Add noise above 3 KHz and then remove, interference suppression using
400 Hz tone.
8. Impulse response of first order and second order systems.
Checking a random process for stationary in wide sense

Department of Electronics and Communication Engineering

3
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Experiment No: 1
Aim​:-
: Generate various signals and sequences (Aperiodic), such as Unit Impulse, Unit Step,
Square, Saw Tooth, Triangular, Sinusoidal, Ramp, Sinc.

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS
Program​:-
i. Generate Sinusoidal signal

clear all;
close all;
clc;
N = input('Enter the number of cycles ...:: ');
t = 0:0.05:N;
x = sin(2*pi*t);

subplot(1,2,1);
plot(t,x);
xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Sinusoidal');

subplot(1,2,2);
stem(t,x);
xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Sinusoidal');

OUTPUT
Enter the number cycles ...:: 2

Department of Electronics and Communication Engineering

4
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

of
​ii. Generate Unit Impulse function

clc;
clear ​all​;
close ​all​;
disp(​'UNIT IMPULSE SIGNAL'​);
N=input(​'Enter Number of Samples: '​);
n=-N:1:N;
x=[zeros(1,N) 1 zeros(1,N)];
stem(n,x);
xlabel(​'Time'​);
ylabel(​'Amplitude'​);
title(​'Impulse Response'​);

OUTPUT
UNIT IMPULSE SIGNAL
Enter Number of Samples: 5

Department of Electronics and Communication Engineering

5
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

iii. Generate Unit Step function

clc;
clear all;
close all;
disp('UNIT STEP SIGNAL');
N=input(' Enter Number of Samples : ');
n=-N:1:N x=[zeros(1,N) 1 ones(1,N)]
stem(n,x);
xlabel('Time');
ylabel('Amplitude');
title('Unit Step Response');

OUTPUT
UNIT STEP SIGNAL
Enter Number of Samples: 6

Department of Electronics and Communication Engineering

6
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

iv. Generate Ramp function

clear all;
close all;
clc;

t = 0:25;
y = t;

subplot(1,2,1);
plot(t,y);
xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Ramp function');

Department of Electronics and Communication Engineering

7
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

subplot(1,2,2);
stem(t,y);
xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Ramp function');

OUTPUT

v. Generate Sawtooth Waveform

clear all;
close all;
clc;

N = input('Enter the number of cycles ...:: ');

t1 = 0:25;
t = [];

for i = 1:N,
t = [t,t1];
end;

Department of Electronics and Communication Engineering

8
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

subplot(2,1,1);
plot(t);
xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Sawtooth waveform');

subplot(2,1,2);
stem(t);
xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Sawtooth waveform');

OUTPUT

Enter the number of cycles ...:: 3

vi. Generate Triangular Waveform

clear ​all​;
close ​all​;
clc;

N = input(​'Enter the number of cycles .....:: '​);


M = input(​'Enter the period ....:: '​);
t1 = 0:0.1:M/2;
t2 = M/2:-0.1:0;
t3=0:-0.1:-M/2;
t4=-M/2:0.1:0;

Department of Electronics and Communication Engineering

9
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

t = [];

for​ i = 1:N,
t = [t,t1,t2,t3,t4];
end​;

subplot(1,2,1);
plot(t);
xlabel(​'---------> Time'​);
ylabel(​'---------> Amplitude'​);
title(​'Triangular waveform'​);

subplot(1,2,2);
stem(t);
xlabel(​'---------> Time'​);
ylabel(​'---------> Amplitude'​);
title(​'Triangular waveform'​);

OUTPUT

Enter the number of cycles .....:: 2


Enter the period ....:: 4
>>

vii. Generate Square waveform

close ​all​;
clear ​all​;
clc;

A=input(​'Enter the amplitude of the square wave A = '​);


t=0:0.1:10;
y=A*square(t);
subplot(2,1,1)

Department of Electronics and Communication Engineering

10
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

plot(t,y);
xlabel(​'time'​)
ylabel(​'amplitude'​)
subplot(2,1,2)
stem(t,y)
xlabel(​'time'​)
ylabel(​'amplitude'​)

OUTPUT
Enter the amplitude of the square wave A = 4

viii. Generate Sinc function

clear all;
close all;
clc;

t = -3:0.1:3;
x = sin(pi*t)./(pi*t);

Department of Electronics and Communication Engineering

11
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

subplot(1,2,1); plot(x);
xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Sinc function');
axis([0,100,-0.5,1.0]);

subplot(1,2,2); stem(x);
xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Sinc function');
axis([0,100,-0.5,1.0]);

OUTPUT

Result​:-

Experiment No:2
Operations on signals and Sequences such as addition, Multiplication, scaling, Shifting,
folding, computation of energy and average power

Aim​:-

Department of Electronics and Communication Engineering

12
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

To perform operations on Signals and Sequences such as Addition, Multiplication, Scaling,


Shifting, Folding, Computation of Energy and Average Power.

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

Program​:-

i. Addition and Multiplication of two Sequences

clear all;
close all;
clc;

x = input(' Enter sequence 1 :: ');


y = input(' Enter sequence 2 :: ');
M = length(x);
N = length(y);

subplot(2,2,1);
stem(x);
xlabel('---->Time ');
ylabel('---->Amplitude ');
title('Input sequence 1');
subplot(2,2,2);
stem(y);
xlabel('---->Time ');
ylabel('---->Amplitude ');
title('Input sequence 2');

if M > N
z = x + [y,zeros(1,M-N)];
else
z = [x,zeros(1,N-M)] + y;
end;

subplot(2,2,3);
stem(z);
xlabel('---->Time ');
ylabel('---->Amplitude ');
title('Addition of sequences 1 and 2');

if M > N

Department of Electronics and Communication Engineering

13
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

a = x.*[y,zeros(1,M-N)];
else
a = [x,zeros(1,N-M)].*y;
end;

subplot(2,2,4);
stem(a);
xlabel('---->Time ');
ylabel('---->Amplitude ');
title('Multiplication of sequences 1 and 2');

OUTPUT

Enter sequence 1 :: [1 2 3 4 5 1 2 3 4 5 1 2 3 4 5]
Enter sequence 2 :: [5 6 7 8 9 9 8 7 6 5]

ii. Scaling Operation on a given Sequence

clear all;
close all;

Department of Electronics and Communication Engineering

14
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

clc;

s = input('Enter the sequence ......:: ');


a = input('Enter the scaling Factor ...:: ');
m = length(s);
k = ceil(m/a);
s_scal = [];
for i = 1:m,
if mod(i-1,a) == 0
s_scal = [s_scal,s(i)];
end;
end;
subplot(2,1,1)
stem([0:m-1],s);
xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Input Sequence');

subplot(2,1,2);
stem([0:k-1],s_scal);
xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Scaled Sequence');

OUTPUT

Enter the sequence ......:: [1 2 3 4 5 6 7 8 9 10 11 12 13 14]


Enter the scaling Factor ...:: 4

Department of Electronics and Communication Engineering

15
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

iii. Shifting Operation on a given Sequence

clear all;
close all;
clc;

s = input('Enter the sequence ......:: ');


a = input('Enter the length to be shifted ...:: ');
m = length(s);

if a == 0
subplot(2,1,1)
stem([0:m-1],s); xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Input Sequence');

subplot(2,1,2);
stem([0:m-1],s); xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Shifted Sequence');
elseif a > 0
subplot(2,1,1)
stem([0:m-1],s); xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Input Sequence');

subplot(2,1,2);
stem([0:m+a-1],[zeros(1,a),s]); xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Shifted Sequence');
else
subplot(2,1,1)
stem([0:m-1],s); xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Input Sequence');

subplot(2,1,2);
stem([a:m-1],[s,zeros(1,abs(a))]); xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Shifted Sequence');
end;

OUTPUT

Department of Electronics and Communication Engineering

16
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Enter the sequence ......:: [1 2 3 4 5 6 7 8 9 10 11 12 13 14]


Enter the length to be shifted ...:: 5

Enter the sequence ......:: [1 2 3 4 5 6 7 8 9 10 11 12 13 14]


Enter the length to be shifted ...:: -5

Department of Electronics and Communication Engineering

17
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

iv. Folding Operation on a given Sequence

clear all;
close all;
clc;

s = input('Enter the sequence ......:: ');


m = length(s);

subplot(2,1,1)
stem([-m:m],[zeros(1,m),s,0]);
xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Input Sequence');

subplot(2,1,2);
stem([-m:m],[0,fliplr(s),zeros(1,m)]);
xlabel('---------> Time');
ylabel('---------> Amplitude');
title('folded Sequence');

OUTPUT

Enter the sequence ......:: [1 2 3 4 5 6 7]

Department of Electronics and Communication Engineering

18
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

v. Energy and Average Power on a given Sequence

clear all;
close all;
clc;

tes = input('Enter the sequence ...:: ');

M = length(tes);
sum = 0;
for i = 1:M,
sum = sum + tes(i)*tes(i);
end;
disp('Energy of the given sequence is ..... :: ');
Energy = sum
disp('Average Power of the given sequence is ..... :: ');
Average_power = sum/M
OUTPUT

Enter the sequence ...:: [1 2 3 4 5 4 3 2 1]


Energy of the given sequence is ..... ::

Energy =

85

Average Power of the given sequence is ..... ::

Average_power =

9.4444

vi. Addition and Multiplication of two Signals

clear all;
close all;
clc;

t = 0:0.01:2;
x = sin(2*pi*t);
y = square(2*pi*t);

Department of Electronics and Communication Engineering

19
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

M = length(x);
N = length(y);

subplot(4,1,1);
stem(x);
xlabel('---->Time ');
ylabel('---->Amplitude ');
title('Input signal 1');

subplot(4,1,2);
stem(y);
xlabel('---->Time ');
ylabel('---->Amplitude ');
title('Input signal 2');

if M > N
z = x + [y,zeros(1,M-N)];
else
z = [x,zeros(1,N-M)] + y;
end;

z = x + y;
subplot(4,1,3);
stem(z);
xlabel('---->Time ');
ylabel('---->Amplitude ');
title('Addition of signals 1 and 2');

if M > N
a = x.*[y,zeros(1,M-N)];
else
a = [x,zeros(1,N-M)].*y;
end;

a = x.*y;
subplot(4,1,4);
stem(a);
xlabel('---->Time ');
ylabel('---->Amplitude ');
title('Multiplication of signals 1 and 2');

OUTPUT

Department of Electronics and Communication Engineering

20
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

vii. Scaling Operation on a given Signal

clear all;
close all;
clc;

t = 0:0.01:2;
s = sin(2*pi*t);
a = input('Enter the scaling Factor ...:: ');
m = length(s);
k = ceil(m/a);
s_scal = [];
for i = 1:m,
if mod(i,a) == 0
s_scal = [s_scal,s(i-1)];
end;
end;
subplot(2,1,1)
stem([0:m-1],s);
xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Input Sequence');

subplot(2,1,2);
stem([0:k-1],s_scal);

Department of Electronics and Communication Engineering

21
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Scaled Sequence');

OUTPUT

Enter the scaling Factor ...:: 3

viii. Shifting Operation on a given Signal

clear all;
close all;
clc;

t = 0:0.01:2;
s = sin(2*pi*t);
a = input('Enter the length to be shifted ...:: ');
m = length(s);

if a == 0
subplot(2,1,1)
stem([0:m-1],s); xlabel('---------> Time');

Department of Electronics and Communication Engineering

22
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

ylabel('---------> Amplitude');
title('Input Sequence');

subplot(2,1,2);
stem([0:m-1],s); xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Shifted Sequence');
elseif a > 0
subplot(2,1,1)
stem([0:m-1],s); xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Input Sequence');

subplot(2,1,2);
stem([0:m+a-1],[zeros(1,a),s]); xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Shifted Sequence');
else
subplot(2,1,1)
stem([0:m-1],s); xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Input Sequence');

subplot(2,1,2);
stem([a:m-1],[s,zeros(1,abs(a))]); xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Shifted Sequence');
end;

OUTPUT

Enter the length to be shifted ...:: 75

Department of Electronics and Communication Engineering

23
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

ix. Folding Operation on a given Signal

clear all;
close all;
clc;

t = 0:0.01:2;
s = sin(2*pi*t);
m = length(s);

subplot(2,1,1)
stem([-m:m],[zeros(1,m),s,0]);
xlabel('---------> Time');
ylabel('---------> Amplitude');
title('Input Sequence');

subplot(2,1,2);
stem([-m:m],[0,fliplr(s),zeros(1,m)]);
xlabel('---------> Time');
ylabel('---------> Amplitude');
title('folded Sequence');

OUTPUT

Department of Electronics and Communication Engineering

24
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

x. Energy and Average Power on a given Signal

clear all;
close all;
clc;
t = 0:0.01:4;
tes = cos(2*pi*t);

M = length(tes);
sum = 0;
for i = 1:M,
sum = sum + tes(i)*tes(i);
end;
disp('Energy of the given sequence is ..... :: ');
Energy = sum
disp('Average Power of the given sequence is ..... :: ');
Average_power = sum/M

OUTPUT

Energy of the given sequence is ..... ::

Energy =

201

Department of Electronics and Communication Engineering

25
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Average Power of the given sequence is ..... ::

Average_power =

0.5012

Result​:-

Experiment No: 3
Convolution between Signals and sequences
Aim​:-
: Verifying the Convolution between Signals and Sequences

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

Program​:-

i. Linear Convolution of two Sequences

clear all;
close all;
clc;

x=input('enter the seq1 :: ');


h=input('enter the seq2 :: ');

Department of Electronics and Communication Engineering

26
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

subplot(3,1,1);
stem(x);

subplot(3,1,2);
stem(h);
o=zeros(1,length(x)+length(h)-1);

for m=1:length(x),
for n=1:length(h),
y(m,n)=x(m)*h(n);
end;
end;
for n=1:length(x)+length(h)-1,
for i=1:length(x),
for j=1:length(h),
if(i+j==n+1)
o(n)=o(n) + y(i,j);
end;
end;
end;
end;

subplot(3,1,3);
stem(o);

Department of Electronics and Communication Engineering

27
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

OUTPUT
enter the seq1 :: [1 2 3 4 3 2 1]
enter the seq2 :: [4 3 2 1 2 3 4]

ii. Linear Convolution of two Sequences using conv() function

clear all;
close all;
clc;

x=input('enter the seq1 :: ');


h=input('enter the seq2 :: ');

subplot(3,1,1);
stem(x);

subplot(3,1,2);
stem(h);

Department of Electronics and Communication Engineering

28
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

o = conv(x,h);

subplot(3,1,3);
stem(o);
OUTPUT

enter the seq1 :: [1 2 3 4 3 2 1]


enter the seq2 :: [4 3 2 1 2 3 4]
>>

iii. Linear Convolution of two Signals

clear all;
close all;
clc;

t = 1:10;
x = sin(t);
h = square(t);

subplot(3,1,1);

Department of Electronics and Communication Engineering

29
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

stem(x);

subplot(3,1,2);
stem(h);

o=zeros(1,length(x)+length(h)-1);

for m=1:length(x),
for n=1:length(h),
y(m,n)=x(m)*h(n);
end;
end;
for n=1:length(x)+length(h)-1,
for i=1:length(x),
for j=1:length(h),
if(i+j==n+1)
o(n)=o(n) + y(i,j);
end;
end;
end;
end;

subplot(3,1,3);
stem(o);

OUTPUT

>> x

x=

Columns 1 through 7

0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570

Columns 8 through 10

0.9894 0.4121 -0.5440

>> h

h=

1 1 1 -1 -1 -1 1 1 1 -1

Department of Electronics and Communication Engineering

30
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

>>

iv. Linear Convolution of two Signals using conv() function

clear all;
close all;
clc;

t = 1:10;
x = sin(t);
h = square(t);

subplot(3,1,1);
stem(x);

subplot(3,1,2);
stem(h);

o = conv(x,h);

subplot(3,1,3);

Department of Electronics and Communication Engineering

31
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

stem(o);

OUTPUT

>> x

x=

Columns 1 through 7

0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570

Columns 8 through 10

0.9894 0.4121 -0.5440

>> h

h=

1 1 1 -1 -1 -1 1 1 1 -1

Department of Electronics and Communication Engineering

32
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Result​:-

Department of Electronics and Communication Engineering

33
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Experiment No:4
Auto Correlation and Cross Correlation between Signals and sequences
Aim​:-
: Auto Correlation and Cross Correlation between Signals and Sequences

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

Program​:-

i. Auto Correlation of a Sequence

clear all;
close all;
clc;

a = input('Enter the sequence ....:: ');

res = xcorr(a);

subplot(2,1,1);
stem(a);
xlabel('----> Samples');
ylabel('----> Amplitude');
title('Input Sequence');

subplot(2,1,2);
stem(res);
xlabel('----> Samples');
ylabel('----> Amplitude');
title('Output Sequence');

Department of Electronics and Communication Engineering

34
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

OUTPUT

Enter the sequence ....:: [1 2 3 4]

ii. Auto Correlation of a Signal

clear all;
close all;
clc;
t = 0:0.01:2;
a = cos(2 * pi * t);

res = xcorr(a);

subplot(2,1,1);

Department of Electronics and Communication Engineering

35
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

plot(a);
xlabel('----> Samples');
ylabel('----> Amplitude');
title('Input Sequence');

subplot(2,1,2);
plot(res);
xlabel('----> Samples');
ylabel('----> Amplitude');
title('Output Sequence');

OUTPUT

iii. Cross Correlation of two Sequences

clear all;

Department of Electronics and Communication Engineering

36
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

close all;
clc;

a = input('Enter the first sequence ....:: ');


b = input('Enter the second sequence ....:: ');
res = xcorr(a,b);

subplot(2,2,1);
stem(a);
xlabel('----> Samples');
ylabel('----> Amplitude');
title('Input Sequence(1)');

subplot(2,2,2);
stem(b);
xlabel('----> Samples');
ylabel('----> Amplitude');
title('Input Sequence(2)');
subplot(2,2,[3,4]);
stem(res);
xlabel('----> Samples');
ylabel('----> Amplitude');
title('Output Sequence');

OUTPUT

Enter the first sequence ....:: [1 2 3 4]


Enter the second sequence ....:: [4 3 2 1]

Department of Electronics and Communication Engineering

37
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

iv. Cross Correlation of a two Signals

clear all;
close all;
clc;
t = 0:0.01:2;
a = cos(2 * pi * t);
b = sin(2 * pi * t);

res = xcorr(a,b);

subplot(2,2,1);
plot(a);
xlabel('----> Samples');
ylabel('----> Amplitude');
title('Input signal(1)');

subplot(2,2,2);
plot(b);
xlabel('----> Samples');
ylabel('----> Amplitude');

Department of Electronics and Communication Engineering

38
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

title('Input Signal(2)');

subplot(2,2,[3,4]);
plot(res);
xlabel('----> Samples');
ylabel('----> Amplitude');
title('Output Signal');

OUTPUT

Department of Electronics and Communication Engineering

39
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Result​:-

Department of Electronics and Communication Engineering

40
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Experiment No: 5
Verification of Linearity and Time Invariance properties of a given Continuous/Discrete
System

Aim​:-
: Verification of Linearity and Time Invariance properties of a given Continuous
/ Discrete system.

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS
Program​:-

i. Linearity of a system

clear all;
close all;
clc;

n=0:0.01:2;
a= -3; b= 5;

x1=cos(2*pi*n);
x2=cos(3*pi*n);
x=a*x1+b*x2;

ic=[0 0];
num=[2.2403 2.4908 2.2403];
den=[1 -0.4 0.75];

y1=filter(num,den,x1,ic);
y2=filter(num,den,x2,ic);
y=filter(num,den,x,ic);

yt=a*y1+b*y2;
d=y-yt;

subplot(3,1,1), stem(n,y);
xlabel('---------> Time');
ylabel('-> Amplitude');
title('y = a*x1 + b*x2');

subplot(3,1,2), stem(n,yt);

Department of Electronics and Communication Engineering

41
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

xlabel('---------> Time');
ylabel('-> Amplitude');
title('yt = a*y1 + b*y2');

subplot(3,1,3), stem(n,d);
xlabel('---------> Time');
ylabel('-> Amplitude');
title('difference of y and yt');

OUTPUT

ii. Time Invariance of a system

clear ​all​;
close ​all​;
clc;

n=0:0.05:4;
D=10;
x=cos(2*pi*n);
xd=[zeros(1,D) x];

Department of Electronics and Communication Engineering

42
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

num=[2.2403 2.4908 2.2403];


den=[1 -0.4 0.75];
ic=[0 0];

y=filter(num,den,x,ic);
yd=filter(num,den,xd,ic);
d=y-yd(1+D:1+D);

subplot(3,1,1), stem(y);
xlabel(​'---------> Time'​);
ylabel(​'-> Amplitude'​); title(​'y '​);

subplot(3,1,2), stem(yd);
xlabel(​'---------> Time'​);
ylabel(​'-> Amplitude'​); title(​'yd'​);

subplot(3,1,3), stem(d);
xlabel(​'---------> Time'​);
ylabel(​'-> Amplitude'​);
title(​'difference of y and yd'​);

OUTPUT

Department of Electronics and Communication Engineering

43
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Result​:-

Experiment No: 6
Sinusoidal signal generation through recursive function

AIM:
To Write the Matlab Program for sinusoidal signal generation through recursive
function

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

PROGRAM:

A=3;
f0=50;
fs=2000;
t=0.1;
length=fs*t;
y=zeros(1,length);
impulse=zeros(1,length);
impulse(1)=1;
a1=-2*cos(2*pi*f0/fs);
a2=1;
b0=2*sin(2*pi*f0/fs);
y(1)=0;
y(2)=b0;
for i=3:length;
y(i)=b0*impulse(i)-a1*y(i-1)-a2*y(i-2);
end
plot(y);
xlabel('samples n');
ylabel('y(n)');
title('sinusoidal waveform');
grid

Department of Electronics and Communication Engineering

44
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

OUTPUT:-

Result​:-

Department of Electronics and Communication Engineering

45
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Experiment No: 7

Discrete & Inverse Discrete Fourier Transform

AIM:
To Write a Matlab program for determining DFT and IDFT of the given sequence.

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

PROGRAM:

%MATLAB program to determine DFT of the sequence and plot magnitude and
%phase response
clc;
clear all;
close all;
xn=input('enter the input sequence:');
N=input('enter the length of sequence:');
% To compute N-point DFT of the sequence xn
L=length(xn);
%Checking for the length of the DFT
if (N<L)
error('N must be >=L');
end;
x1=[xn zeros(1,N-L)];%Appending zeros
%Computation of TWIDDLE FACTORS
for k=0:1:N-1;
for n=0:1:N-1
p=exp(-1i*2*pi*n*k/N);
x2(k+1,n+1)=p;
end

Department of Electronics and Communication Engineering

46
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

end
xk=x1*x2;
magxk=abs(xk);
k=0:N-1;
subplot(2,2,[1 2]);
stem(k,xn);
grid;
title('INPUT SEQUENCE');
xlabel('time index--> k');
ylabel('amplitude-->');
subplot(2,2,3);
stem(k,magxk);
grid;
title('MAGNITUDE RESPONSE');
xlabel('time index, k');
ylabel('Magnitude in dB');
angxk=angle(xk);
subplot(2,2,4);
stem(k,angxk);
grid;
title('PHASE RESPONSE');
xlabel('time index, k');
ylabel('Phase angle in radians');

INPUTS:

enter the input sequence:[1 2 3 4]


enter the length of sequence:4

OUTPUT WAVEFORMS:

Department of Electronics and Communication Engineering

47
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Result​:-

Write a MATLAB program for determining IDFT of the given sequence.

%MATLAB program to determine IDFT of the sequence and plot magnitude and​ ​phase
response
clc;
clear all;
close all;
xk=input('enter the input sequence:');
N=length(xk);
%Computation of TWIDDLE FACTORS
for n=0:1:N-1;
for k=0:1:N-1;
p=exp(1i*2*pi*n*k/N);
x(k+1,n+1)=p;
end
end

Department of Electronics and Communication Engineering

48
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

xn=(xk*x)/N;
magxn=abs(xn);
n=0:N-1;
subplot(2,1,1);
stem(n,magxn);
grid;
title('MAGNITUDE RESPONSE');
xlabel('time index, k');
ylabel('Magnitude in dB');
angxn=angle(xn);
subplot(2,1,2);
stem(n,angxn);
grid;
title('PHASE RESPONSE');
xlabel('time index, k');
ylabel('Phase angle in radians');
disp(xn);

INPUTS:

enter the input sequence:[1 2 1 1]

1.2500 + 0.0000i 0.0000 + 0.2500i -0.2500 + 0.0000i 0.0000 - 0.2500i

OUTPUT WAVEFORMS:

Department of Electronics and Communication Engineering

49
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Result​:-

Experiment No: 8
Frequency Response

Department of Electronics and Communication Engineering

50
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

AIM:
To Write a Matlab program to plot the frequency response (magnitude and phase

response) of a given difference equation as a below .


.
Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

PROGRAM:

%MATLAB program to plot the frequency response (magnitude and phase response)
%of a given difference equation.
clc;
clear all;
b=input('Enter the numerator coefficients:');
a=input('Enter the denominator coefficients:');
[h,ph]=freqz(b,a);
subplot(2,1,1);
plot(ph/pi,abs(h));
grid;
xlabel('Normalised Frequency');
ylabel('Magnitude in dB');
title('Magnitude Response');
subplot(2,1,2);
plot(ph/pi,angle(h));
grid;
xlabel('Normalised Frequency');
ylabel('phase in radians');
title('Phase Response');

INPUTS:

Enter the numerator coefficients: [1]


Enter the denominator coefficients: [1 -1/6 -1/6]

Department of Electronics and Communication Engineering

51
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

OUTPUT WAVEFORMS:

Result​:-

Department of Electronics and Communication Engineering

52
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Experiment No: 9
Fast Fourier Transform
AIM:
To Write a Matlab program for determining FFT of the given Sequence.

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

PROGRAM:

clc;
clear all;
close all;
x=input('enter input sequence:');
N= input('size of fft(e.g . 2,4,8,16,32):');
n=length(x);
p=log2(N);

Y=x;
Y=[Y, zeros(1, N - n)];
N2=N/2;
YY = -pi*sqrt(-1)/N2;
WW = exp(YY);
JJ = 0 : N2-1;
W=WW.^JJ;
for L = 1 : p-1
u=Y(:,1:N2);
v=Y(:,N2+1:N);
t=u+v;
S=W.*(u-v);
Y=[t ; S];
U=W(:,1:2:N2);
W=[U ;U];
N=N2;
N2=N2/2;
end;
u=Y(:,1);
v=Y(:,2);
Y=[u+v;u-v];
Y

Department of Electronics and Communication Engineering

53
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

subplot(2,2,[1,2])
stem(x);grid
title('input sequence x')
xlabel('n');
ylabel('Amplitude');
subplot(2,2,3)
stem(abs(Y));grid
title('Magnitude of FFT')
xlabel('N');
ylabel('Amplitude');
subplot (2,2,4)
stem(angle(Y));grid
title('phase of FFT')
xlabel('N');
ylabel('phase');

output:

Enter the input sequence[1 2 3 4]

INPUT:
enter input sequence:[1 2 3 4]

size of fft(e.g . 2,4,8,16,32): 4

Y=

10.0000 + 0.0000i
-2.0000 + 2.0000i
-2.0000 + 0.0000i
-2.0000 - 2.0000i

OUTPUT WAVEFORMS:

Department of Electronics and Communication Engineering

54
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

(b) To Write a Matlab program for determining FFT of the given Sequence using predefined
function.

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

​PROGRAM:

clc;
clear ​all​;
close ​all​;
N=4;
m=4;
a=input(​'Enter the input sequence'​);
n=0:1:N-1;
subplot(2,2,1);
stem(n,a);
xlabel(​'Time Index n'​);
ylabel(​'Amplitude'​);
title(​' input Sequence'​);
x=fft(a,m);
k=0:1:N-1;
subplot(2,2,2);
stem(k,abs(x));
ylabel(​'magnitude'​);

Department of Electronics and Communication Engineering

55
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

xlabel(​'Frequency Index K'​);


title(​'Magnitude of the DFT sample'​);
subplot(2,2,3);
stem(k,angle(x));
xlabel(​'Frequency Index K'​);
ylabel(​'Phase'​);
title(​'Phase of DFT sample'​);

output:

Enter the input sequence[1 2 3 4]

Department of Electronics and Communication Engineering

56
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Result​:-

Department of Electronics and Communication Engineering

57
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

PART-B

Department of Electronics and Communication Engineering

58
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Experiment No: 1
Power Spectrum

AIM:
To obtain power spectrum of given signal using MATLAB.

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

PROGRAM:

​%Power spectral density


clc;
clear ​all​;
close ​all​;
t = 0:0.001:0.6;
x =sin(2*pi*50*t)+sin(2*pi*120*t);
y = x + 2*randn(size(t));
subplot(2,1,1);
plot(1000*t(1:50),y(1:50));
grid;
title(​'Signal Corrupted with Zero-Mean Random Noise'​)
xlabel(​'time (milliseconds)'​);
ylabel(​'amplitude'​)
Y = fft(y,512);
%The power spectral density, a measurement of the energy at various frequencies, is:
Pyy = Y.* conj(Y) / 512; f = 1000*(0:256)/512;
subplot(2,1,2);
plot(f,Pyy(1:257));
grid;
title(​'Frequency content of y'​);
xlabel(​'frequency (Hz)'​);
ylabel(​'amplitude'​)

Department of Electronics and Communication Engineering

59
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

OUTPUT WAVFORMS:

Result​:-

Department of Electronics and Communication Engineering

60
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Experiment No: 2
Fir Low pass Filter
AIM:
To Write a Matlab program of FIR Low pass filter using rectangular, Hanning
Hamming, Blackman and Kaiser Window.
.
Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

PROGRAM:
%MATLAB program of FIR Low pass filter using Hanning
%Hamming, Blackman and Kaiser window
clf;
wc=.5*pi;
N=25;
w=0:0.1:pi;
b=fir1(N,wc/pi,blackman(N+1));
h=freqz(b,1,w);
subplot(2,2,1)
plot(w/pi,abs(h))
grid;xlabel(​'normalised frequency'​);
ylabel(​'magnitude in dB'​)
title(​'FIR LPF USING BLACKMAN WINDOW'​)
b=fir1(N,wc/pi,hamming(N+1));
h=freqz(b,1,w);
subplot(2,2,2)
plot(w/pi,abs(h));
grid;
xlabel(​'normalised frequency'​);
ylabel(​'magnitude in dB'​)
title(​'FIR LPF USING HAMMING WINDOW'​)
b=fir1(N,wc/pi,hanning(N+1));
h=freqz(b,1,w);
subplot(2,2,3)
plot(w/pi,abs(h));
grid;

Department of Electronics and Communication Engineering

61
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

xlabel(​'normalised frequency'​);
ylabel(​'magnitude in dB'​)
title(​'FIR LPF USING HANNING WINDOW'​)
b=fir1(N,wc/pi,kaiser(N+1,3.5));
h=freqz(b,1,w);
subplot(2,2,4)
plot(w/pi,abs(h));
grid;
xlabel(​'normalised frequency'​);
ylabel(​'magnitude in dB'​)
title(​'FIR LPF USING KAISER WINDOW'​)

OUTPUT WAVEFORMS:

Result​:-

Department of Electronics and Communication Engineering

62
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Department of Electronics and Communication Engineering

63
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Fir High pass Filter

AIM:
To Write a Matlab program of FIR High pass filter using rectangular, triangular and
Kaiser Window.
.
Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

PROGRAM:

%FIR Filter design window techniques


clc;
clear ​all​;
close ​all​;
rp=input(​'enter passband ripple'​);
rs=input(​'enter the stopband ripple'​);
fp=input(​'enter passband freq'​);
fs=input(​'enter stopband freq'​);
f=input(​'enter sampling freq '​);
beta=input(​'enter beta value'​);
wp=2*fp/f; ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1; ​if​(rem(n,2)~=0) n1=n; n=n-1;
end
c=input(​'enter your choice of window function 1. rectangular 2. triangular 3.kaiser: \n '​);
if​(c==1) y=rectwin(n1);
disp(​'Rectangular window filter response'​);
end
if​ (c==2) y=triang(n1);
disp(​'Triangular window filter response'​);
end
if​(c==3) y=kaiser(n1,beta);
disp(​'kaiser window filter response'​);
end
%HPF
b=fir1(n,wp,​'high'​,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
plot(o/pi,m);
title(​'HPF'​);

Department of Electronics and Communication Engineering

64
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

ylabel(​'Gain in dB-->'​);
xlabel(​'(b) Normalized frequency-->'​);

INPUT:

enter passband ripple:0.02


enter the stopband ripple:0.01
enter passband freq:1000
enter stopband freq:1500
enter sampling freq: 10000
enter beta value:5

OUTPUT WAVEFORM:

enter your choice of window function 1. rectangular 2. triangular 3.kaiser:


2
Triangular window filter response

Department of Electronics and Communication Engineering

65
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

enter your choice of window function 1. rectangular 2. triangular 3.kaiser:


1
Rectangular window filter response

enter your choice of window function 1. rectangular 2. triangular 3.kaiser:


3
kaiser window filter response

Result​:-

Department of Electronics and Communication Engineering

66
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Experiment No: 3
IIR LOWPASS FILTER

AIM:
To Design a Butterworth low pass filter for the given specifications using Matlab.

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

PROGRAM:

clc;
clear ​all​;
close ​all​;
alphap=input(​'enter pass attenuation in db='​);​%passband attenuation in db
alphas=input(​'enter stopband attenuation in db='​);​% stopband attenuation in db
fp=input(​'enter passband frequency in hz='​); ​% passband frequency in hz
fs=input(​'enter stopband frequency in hz='​); ​% stopband frequency in hz
F=input(​'enter sampling frequency in hz='​); ​% sampling frequency in hz
omp=2*fp/F; ​%frequency in radians
oms=2*fs/F;
%to find cutoff frequency and order of the filter
[n,wn]=buttord(omp,oms,alphap,alphas);
%system function of the filter
[b,a]=butter(n,wn);
w=0:0.01:pi;
[h,om]=freqz(b,a,w,​'whole'​);
m=20*log10(abs(h));
an=angle(h);
subplot(1,2,1);
plot(om/pi,m);
grid;
xlabel(​'normalised frequency'​);
ylabel(​'gain in db'​);
title(​'magnitude response'​);
subplot(1,2,2);
plot(om/pi,an);

Department of Electronics and Communication Engineering

67
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

grid;
xlabel(​'normalised frequency'​);
ylabel(​'phase in radians'​);
title(​'phase response'​);
disp(b);
disp(a);

INPUTS:
enter pass attenuation in db=4
enter stopband attenuation in db=30
enter passband frequency in hz=400
enter stopband frequency in hz=800
enter sampling frequency in hz=2000

0.1600 0.4800 0.4800 0.1600

1.0000 -0.0494 0.3340 -0.0045

OUTPUT WAVEFORM:

Result​:-

Department of Electronics and Communication Engineering

68
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

(b) To Design a Butterworth Band stop filter for the given specifications using Matlab.

%To design a Butterworth Band stop filter for the given specifications
clc;
clear ​all​;
close ​all​;
alphap=input(​'enter pass attenuation in db='​);​%passband attenuation in db
alphas=input(​'enter stopband attenuation in db='​);​% stopband attenuation in db
wp=[.1*pi .5*pi]; ​% passband frequency in rad
ws=[.2*pi .4*pi]; ​% stopband frequency in rad
​%to find cutoff frequency and order of the filter
[n,wn]=buttord(wp/pi,ws/pi,alphap,alphas);
%system function of the filter
[b,a]=butter(n,wn,​'stop'​);
w=0.1:0.01:pi;
[h,ph]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(1,2,1);
plot(ph/pi,m);
grid;
xlabel(​'normalised frequency'​);
ylabel(​'gain in db'​);
title(​'magnitude response'​);
subplot(1,2,2);
plot(ph/pi,an);
grid;
xlabel(​'normalised frequency'​);
ylabel(​'phase in rad'​);
title(​'phase response'​);
disp(b);
disp(a);

INPUTS:

Department of Electronics and Communication Engineering

69
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

enter pass attenuation in db=2


enter stopband attenuation in db=20

0.2348 -1.1611 3.0921 -5.2573 6.2629 -5.2573 3.0921 -1.1611 0.2348

1.0000 -3.2803 5.4917 -6.1419 5.0690 -3.0524 1.3002 -0.3622 0.0558

OUTPUT WAVEFORMS:

Result​:-

Department of Electronics and Communication Engineering

70
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

(c) To Design a Chebyshev-I low pass filter for the given specifications using Matlab.

% To design a chebyshev-1 Lowpass filter for the given specifications


clc;
clear all;
close all;
aphap=input(​'passband attenuation in db='​); ​%passband attenuation in db
alphas=input(​'stopband attenuation in db='​);​% stopband attenuation in db
wp=.2*pi;​% passband frequency in rad
ws=.3*pi;​% stopband frequency in rad
%order and cutoff frequency of the filter
[n,wn]=cheb1ord(wp/pi,ws/pi,alphap,alphas);
%system function of the filter
[b,a]=cheby1(n,alphap,wn);
w=0:0.01:pi;
[h,ph]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(1,2,1);
plot(ph/pi,m);
grid;
xlabel(​'normalised frequency'​);
ylabel(​'gain in db'​);
title(​'magnitude response'​);
subplot(1,2,2);
plot(ph/pi,an);
grid;
xlabel(​'normalised frequency'​);
ylabel(​'phase in rad'​);
title(​'phase response'​);
% [b,a]=cheby1(n,alphap,wn)
disp(b);
disp(a);

Department of Electronics and Communication Engineering

71
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

INPUTS:

passband attenuation in db=2


stopband attenuation in db=20

0.0013 0.0053 0.0079 0.0053 0.0013

1.0000 -3.1910 4.1494 -2.5703 0.6385

OUTPUT WAVE FORMS:

Result​:-

Department of Electronics and Communication Engineering

72
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

IIR HIGHPASS FILTER

AIM:
To Design a Butterworth high pass filter for the given specifications using Matlab

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

PROGRAM:
a) To Design a Butterworth High pass filter for the given specifications using
Matlab.

%To design a Butterworth Highpass filter for the given specifications


clc;
clear all;
close all;
alphap=input(​'enter pass attenuation in db='​);​%passband attenuation in db
alphas=input(​'enter stopband attenuation in db='​);​% stopband attenuation in db
fp=input(​'enter passband frequency in hz='​); ​% passband frequency in hz
fs=input(​'enter stopband frequency in hz='​); ​% stopband frequency in hz
F=input(​'enter sampling frequency in hz='​); ​% sampling frequency in hz
omp=2*fp/F; ​%frequency in radians
oms=2*fs/F;
%to find cutoff frequency and order of the filter
[n,wn]=buttord(omp,oms,alphap,alphas);
%system function of the filter

Department of Electronics and Communication Engineering

73
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

[b,a]=butter(n,wn,​'high'​);
w=0:0.01:pi;
[h,om]=freqz(b,a,w,​'whole'​);
m=20*log10(abs(h));
an=angle(h);
subplot(1,2,1);
plot(om/pi,m);
grid;
xlabel(​'normalised frequency'​);
ylabel(​'gain in db'​);
title(​'magnitude response'​);
subplot(1,2,2);
plot(om/pi,an);
grid;
xlabel(​'normalised frequency'​);
ylabel(​'phase in radians'​);
title(​'phase response'​);
disp(b);
disp(a);

INPUTS:

enter pass attenuation in db=4


enter stopband attenuation in db=30
enter passband frequency in hz=400
enter stopband frequency in hz=800
enter sampling frequency in hz=2000
0.1735 -0.5205 0.5205 -0.1735

1.0000 -0.0494 0.3340 -0.0045

OUTPUT WAVEFORMS:

Department of Electronics and Communication Engineering

74
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Result​:-

b) To Design a Butterworth Band pass filter for the given specifications using
Matlab.

%To design a Butterworth Band pass filter for the given specifications
clc;
clear all;
close all;
alphap=input(​'enter pass attenuation in db='​);​%passband attenuation in db
alphas=input(​'enter stopband attenuation in db='​);​% stopband attenuation in db
wp=[.2*pi .4*pi]; ​% passband frequency in rad
ws=[.1*pi .5*pi]; ​% stopband frequency in rad
%to find cutoff frequency and order of the filter
[n,wn]=buttord(wp/pi,ws/pi,alphap,alphas);
%system function of the filter
[b,a]=butter(n,wn);
w=0.1:0.01:pi;

Department of Electronics and Communication Engineering

75
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

[h,ph]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(1,2,1);
plot(ph/pi,m);
grid;
xlabel(​'normalised frequency'​);
ylabel(​'gain in db'​);
title(​'magnitude response'​);
subplot(1,2,2);
plot(ph/pi,an);
grid;
xlabel(​'normalised frequency'​);
ylabel(​'phase in rad'​);
title(​'phase response'​);
disp(b);
disp(a);

INPUTS:

enter pass attenuation in db=2


enter stopband attenuation in db=20

0.0060 0 -0.0240 0 0.0359 0 -0.0240 0 0.0060

1.0000 -3.8710 7.9699 -10.6417 10.0781 -6.8167 3.2579 -1.0044 0.1670

OUTPUT WAVE FORMS:

Department of Electronics and Communication Engineering

76
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Result​:-

(c) To Design a Chebyshev-I High pass filter for the given specifications using Matlab.

% To design a chebyshev-1 Hihpass filter for the given specifications

Department of Electronics and Communication Engineering

77
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

clc;
clear ​all​;
close ​all​;
alphap=input(​'passband attenuation in db='​); ​%passband attenuation in db
alphas=input(​'stopband attenuation in db='​);​% stopband attenuation in db
wp=.3*pi;​% passband frequency in rad
ws=.2*pi;​% stopband frequency in rad
%order and cutoff frequency of the filter
[n,wn]=cheb1ord(wp/pi,ws/pi,alphap,alphas);
%system function of the filter
[b,a]=cheby1(n,alphap,wn,​'high'​);
w=0:0.01:pi;
[h,ph]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(1,2,1);
plot(ph/pi,m);
grid;
xlabel(​'normalised frequency'​);
ylabel(​'gain in db'​);
title(​'magnitude response'​);
subplot(1,2,2);
plot(ph/pi,an);
grid;
xlabel(​'normalised frequency'​);
ylabel(​'phase in rad'​);
title(​'phase response'​);
disp(b);
disp(a);

INPUTS:

passband attenuation in db=1


stopband attenuation in db=15

0.2005 -0.8022 1.2033 -0.8022 0.2005

1.0000 -1.0920 1.0639 -0.2680 0.1764

OUTPUT WAVE FORMS:

Department of Electronics and Communication Engineering

78
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Result​:-

Department of Electronics and Communication Engineering

79
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Experiment No:4

GENERATION OF SINUSOIDAL SIGNAL THROUGH FILTERING

Aim:- ​To generate the sinusoidal signal using filtering for the following difference equation
y(n)+0.5y(n-1)-0.001y(n-2)= x(n)

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

PROGRAM:

clc;
clear ​all​;
close ​all​;
n=0:0.2:20;
x=sin(2*pi*0.4*n);
b=input(​'enter the numenator coefficients:'​);
a=input(​'enter the denominator coefficients:'​);
y=filter(b,a,x);
subplot(2,2,1)
plot(n,x);
xlabel(​'time'​)
ylabel(​'amplitude'​)
title(​'input signal'​)
subplot(2,2,2)
stem(n,x);
xlabel(​'time'​)
ylabel(​'amplitude'​)
title(​'given signal in discrete form'​)
subplot(2,2,3)
plot(n,y);
xlabel(​'time'​)
ylabel(​'amplitude'​)
title(​'generated sinusoidal signal through filtering'​)
subplot(2,2,4);
stem(n,y);
xlabel(​'time'​)
ylabel(​'amplitude'​)
title(​'generated sinusoidal signal in discrete form'​);

Department of Electronics and Communication Engineering

80
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

output:

enter the numenator coefficients:1


enter the denominator coefficients:[1 0.5 -0.001]

OUTPUT WAVEFORM:

Result​:-

Department of Electronics and Communication Engineering

81
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Experiment No: 5

IMPLEMENTATION OF DECIMATION AND INTERPOLATION PROCESSESS

DECIMATION PROCESS
AIM:
To Write a Matlab program for Down-sampling of a sinusoidal input sequence.

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

Matlab Functions Used:

The function​ decimate​ can be employed to reduce the sampling rate of an input signal vector
X by an integer factor M generating the output signal vector Y and is available with four
options:
(i)Y=decimate(X,M) : This function employs an eight-order Type 1 Chebyshev IIR Lowpass
filter by default and filters the input sequence in both directions to ensure zero-phase filtering.
(ii)Y=decimate(X,M,n) : This function utilizes an order-n Type 1 Chebyshev IIR Lowpass
filter where n should be less than 13 to avoid numerical stability.
(iii)Y=decimate(X,M,’fir’) : This function employs a 30- tap FIR lowpass filter which filters
the input only in the forward direction. The FIR filter is designed with stop edge at π/M using
the function fir1 of MATLAB.
(iv)Y=decimate(X,M,N,’fir’) : This function design and employs a length-N FIR lowpass
filter.

PROGRAM:
%MATLAB program of Down-Sampling by an integer factor
clc;
clear ​all​;
close ​all​;
N=input(​'Enter the length of output sequence='​);
M=input(​'Enter the Down-Sampling factor='​);
fo=input(​'Enter the input signal frequency='​);

Department of Electronics and Communication Engineering

82
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

%Generate the input sinusoidal sequence


n=0:N-1;
m=0:N*M-1;
X=sin(2*pi*fo*m);
%Generate the Down-Sampled sequence
Y=X(1:M:length(X));
%Plot input and Down-Sampled sequences
subplot(2,1,1);
stem(n,X(1:N));
title(​'input sequence'​);
xlabel(​'Time Index,n'​);
ylabel(​'Amplitude'​);
subplot(2,1,2);
stem(n,Y);
title(​' Down-Sampled sequence'​);
xlabel(​'Time Index,n'​);
ylabel(​'Amplitude'​);

INPUTS:

Enter the length of output sequence=50


Enter the Down-Sampling factor=3
Enter the input signal frequency=0.042

OUTPUT WAVEFORM:

Department of Electronics and Communication Engineering

83
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Result​:-

(b) .Write a Matlab program of decimation of a sum of sinusoidal sequences of


normalized frequencies f​1​ and f​2​ by an arbitrary down-sampling factor M.
%MATLAB program of decimation process
clc;
close ​all​;
t = 0:.00025:1;
x = sin(2*pi*30*t) + sin(2*pi*60*t);
y = decimate(x,4);
%%Plotthe original and decimated signals.

subplot(211)
stem(0:120,x(1:121),​'filled'​,​'markersize'​,3)
grid ​on
xlabel(​'Sample number'​)
ylabel (​'Original'​)
subplot(212)
stem(0:30,y(1:31),​'filled'​,​'markersize'​,3)
grid ​on
xlabel(​'Sample number'​)
ylabel (​'Decimated'​)

OUTPUT WAVEFORM:

Department of Electronics and Communication Engineering

84
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Result​:-

Department of Electronics and Communication Engineering

85
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

INTERPOLATION PROCESS:

AIM:
To Write a Matlab program for Up-sampling of a sinusoidal input sequence.

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

Matlab Functions Used:

The signal processing toolbox of MATLAB includes three specific functions for sampling
rate alteration.These are decimate ,interp and resample.
The function ​interp ​ can be employed to increase the sampling rate of an input signal vector
X by an integer factor L generating the output signal vector Y. It is available with three
options.
(i). ​Y = interp(X,L): ​In this function the default values of and N are respectively, 0.5
and 4.
(ii). ​Y = interp(X,L,N,alpha): ​In this function the bandedge of the input signal and
length N of the interpolation filter can be specified.
(iii).​ [Y,h] = interp(X,L,N,alpha): ​In this function the bandedge of the input signal and
length N of the interpolation filter can be specified. Also, In this function the output data
contains in addition to the interpolated output vector Y, the filter coefficients ‘h’.

PROGRAM:
%MATLAB program of Up-Sampling by an integer factor
clc;
close ​all​;
N=input(​'Enter the length of input sequence='​);
L=input(​'Enter the Up-Sampling factor='​);
fo=input(​'Enter the input signal frequency='​);
%Generate the input sinusoidal sequence
n=0:N-1;
X=sin(2*pi*fo*n);
%Generate the Up-Sampled sequence
Y=zeros(1,L*length(X));
Y([1:L:length(Y)])=X;
%Plot input and Up-Sampled sequences
subplot(2,1,1);
stem(n,X);
title(​'input sequence'​);
xlabel(​'Time Index,n'​);
ylabel(​'Amplitude'​);

Department of Electronics and Communication Engineering

86
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

subplot(2,1,2);
stem(n,Y(1:length(X)));
title(​' Up-Sampled sequence'​);
xlabel(​'Time Index,n'​);
ylabel(​'Amplitude'​);

INPUTS:

Enter the length of input sequence=50


Enter the Up-Sampling factor=10
Enter the input signal frequency100

OUTPUT WAVEFORMS:

Result​:-

Department of Electronics and Communication Engineering

87
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

(b). Write a Matlab program of interpolation of a sum of sinusoidal sequences of


normalized frequencies f​1​ and f​2​ by an arbitrary interpolating factor L​.
%MATLAB program of interpolation process
clf;
N=input(​'Enter the length of input sequence='​);
L=input(​'Enter the Up-Sampling factor='​);
f1=input(​'Enter the input signal frequency of first sinusiod'​);
f2=input(​'Enter the input signal frequency of second sinusiod'​);
%Generate the input sinusoidal sequence
n=0:N-1;
X=sin(2*pi*f1*n)+sin(2*pi*f2*n);
%Generate the interpolated output sequence
Y = interp(X,L);
%Plot input and interpolated output sequences
subplot(2,1,1);
stem(n,X(1:N));
title(​'input sequence'​);
xlabel(​'Time Index,n'​);
ylabel(​'Amplitude'​);
subplot(2,1,2);
m=0:N*L-1;
stem(m,Y(1:N*L));
title(​' interpolated output sequence'​);
xlabel(​'Time Index,n'​);
ylabel(​'Amplitude'​);

INPUT:

Enter the length of input sequence=50


Enter the Down-Sampling factor=2
Enter the input signal frequency of first sinusiod=0.043
Enter the input signal frequency of second sinusiod=0.031

Department of Electronics and Communication Engineering

88
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

OUTPUTWAVEFORMS:

Result​:-

Department of Electronics and Communication Engineering

89
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

(c) ​Write a Matlab program of interpolation of a sinusoidal sequence of normalized by


an arbitrary interpolating factor L​.

clc;
close all;
L = input('Up-sampling factor = ');
N = input('enter number of samples :');
n = 0:N-1;
x = sin(2*pi*0.043*n);
y = interp(x,L);
subplot(2,1,1);
stem(n,x(1:N));
title('Input Sequence');
xlabel('Time index n');
ylabel('Amplitude');
subplot(2,1,2);
m = 0:(N*L)-1;
stem(m,y(1:N*L));
title('Output Sequence');
xlabel('Time index n');
ylabel('Amplitude');

output:-

Up-sampling factor = 3
enter number of samples :50

OUTPUTWAVEFORMS:

Department of Electronics and Communication Engineering

90
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Result​:-

Experiment No:6
INTERPOLATION/DECIMATION SAMPLING RATE CONVERTERS

AIM:

(a) To Write a MATLAB program of the sampling rate increase by a ratio of two positive
integers of a signal composed of two sinusoids with frequencies f​1 and
​ f​2.

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

Matlab Functions Used:

The function resample in MATLAB can be utilized to increase the sampling rate of an input
vector X by a ratio of two positive integers, L/M, generating an output vector Y. There are
five functions available with this resample function.
(i). Y = resample(X, L, M)
(ii). Y = resample(X, L,M,R)
(iii). Y = resample(X, L,M,R,beta)
(iv). Y = resample(X,L,M,h)
(v).[ Y,h] = resample(X,L,M)

PROGRAM:
%MATLAB program of sampling rate alteration by a ratio of two integers

Department of Electronics and Communication Engineering

91
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

clc;
close all;
N=input(​'Enter the length of input sequence='​);
L=input(​'Enter the Up-Sampling factor='​);
M=input(​'Enter the Down-Sampling factor='​);
f1=input(​'Enter the input signal frequency of first sinusoid='​);
f2=input(​'Enter the input signal frequency of second sinusoid='​);
%Generate the input sinusoidal sequence
n=0:N-1;
X=sin(2*pi*f1*n)+sin(2*pi*f2*n);
%Generate the Resampled output sequence
Y = resample(X,L,M);
%Plot input and Resampled output sequences
subplot(2,1,1);
stem(n,X(1:N));
title(​'input sequence'​);
xlabel(​'Time Index,n'​);
ylabel(​'Amplitude'​);
subplot(2,1,2);
m=0:N*L-1;
stem(m,Y(1:N*L/M));
title(​' resampled output sequence'​);
xlabel(​'Time Index,n'​);ylabel(​'Amplitude'​);

INPUTS:
Enter the length of input sequence=50
Enter the Up-Sampling factor=10
Enter the Down-Sampling factor=5
Enter the input signal frequency of first sinusoid=0.043
Enter the input signal frequency of second sinusoid=0.031

OUTPUT WAVEFORMS:

Department of Electronics and Communication Engineering

92
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Result​:-

AIM:

(b) To Write a MATLAB program to study sampling rate conversion by a rational factor
of two positive integers of a sinusoidal signal

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

PROGRAM:

Department of Electronics and Communication Engineering

93
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

clc;
close ​all​;
L = input(​'Enter Up-sampling factor :'​);
M = input(​'Enter Down-sampling factor :'​);
N = input(​'Enter number of samples :'​);
n = 0:N-1;
x = sin(2*pi*0.041*n);
y = resample(x,L,M);
subplot(2,1,1);
stem(n,x(1:N));
axis([0 29 -2.2 2.2]);
title(​'Input Sequence'​);
xlabel(​'Time index n'​);
ylabel(​'Amplitude'​);
subplot(2,1,2);
m = 0:(N*L/M)-1;
stem(m,y(1:N*L/M));
axis([0 (N*L/M)-1 -2.2 2.2]);
title(​'Output Sequence'​);
xlabel(​'Time index n'​);
ylabel(​'Amplitude'​);

output:
Enter Up-sampling factor :4
Enter Down-sampling factor :2
Enter number of samples :40

OUTPUT WAVEFORMS:

Department of Electronics and Communication Engineering

94
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Result​:-

Department of Electronics and Communication Engineering

95
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Experiment No:7
Noise Removal

Aim:- ​To write a MATLAB program to


1) Add noise above 3 kHz and then remove (using adaptive filters)
2) Interference suppression using 400 Hz tone
Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

Procedure:-
1) Open MATLAB
2) Open new M-file
3) Type the program
4) Save in current directory
5) Compile and Run the program
6) For the output see command window\ Figure window

Program:
clc
close ​all
clear ​all
x= cos(2*pi*12*[0:0.001:1.23]);
[b a] = butter(2,[0.6 0.7],​'bandpass'​);
filtered_noise = filter(b,a,randn(1, length(x)*2));
x = (x + 0.5*filtered_noise(500:500+length(x)-1))/length(x)*2;
figure
plot(x)
title(​'Noisy signal'​)
xlabel(​'Samples'​);
ylabel(​'Amplitude'​)

Department of Electronics and Communication Engineering

96
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

X_mags = abs(fft(x));
figure
plot(X_mags)
xlabel(​'DFT Bins'​)
ylabel(​'Magnitude'​)
num_bins = length(X_mags);
[b a] = butter(25, 0.3, ​'low'​);
H = freqz(b,a, floor(num_bins/2));
hold ​on
figure
plot([0:1/(num_bins/2 -1):1], abs(H),​'m'​);
x_filtered = filter(b,a,x);
%plot the filtered signal
figure
plot(x_filtered,​'m'​)
title(​'Filtered Signal - Using Second Order Butterworth'​)
xlabel(​'Samples'​);
ylabel(​'Amplitude'​)

Department of Electronics and Communication Engineering

97
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Output:

Result​:-

Department of Electronics and Communication Engineering

98
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Experiment No:8
Impulse response of first order and second order systems
AIM:
To find the impulse response of first order system by using MATLAB.

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

PROGRAM:
clc;
close all;
n=0:10;
b=[1 0 0];
a=[1 -0.6];
y=impz(b,a,length(n));
subplot(2,2,1);
stem(n,y);
xlabel('t');
ylabel('a')
title('impulse response of first order system');
w=abs(y);
subplot(2,2,2);
stem(n,w);
xlabel('t');
ylabel('a');
title('magnitude response');
x=angle(y);
subplot(2,2,3);
stem(n,x);
xlabel('t');
ylabel('a');
title ('phase response');

Department of Electronics and Communication Engineering

99
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

OUTPUT WAVEFORMS:

​Result​:-

Department of Electronics and Communication Engineering

100
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

(a) . To find the impulse response of second order system by using Matlab.

AIM:
To find the impulse response of second order system by using MATLAB.

Equipment Required​:-

1. MATLAB 8.6(2015b)
2. PC loaded with windows7 OS

PROGRAM:

clc;
close all;
n=0:10;
b=[1 0 0];
a=[1 -0.6 0.08];
y=impz(b,a,length(n));
subplot(2,2,1);
stem(n,y);
xlabel('t');
ylabel('a');
title('impulse response of second order system');
w=abs(y);
subplot(2,2,2);
stem(n,w);
xlabel('t');
ylabel('a');
title('magnitude response');
x=angle(y);
subplot(2,2,3);
stem(n,x);

Department of Electronics and Communication Engineering

101
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

xlabel('t');
ylabel('a');
title ('phase response');

OUTPUT WAVEFORMS:

Result​:-

Department of Electronics and Communication Engineering

102
MLR INSTITUTE OF TECHNOLOGY
Dundigal, hyderabad.

Department of Electronics and Communication Engineering

103

You might also like