DSP PGM

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 18

List of Program

1. generation of waveforms
2 Compute N DFT of a sequence
3 Linear Convolution
4 Circular convolution
5 Impulse response of a system
6 difference equation
7 Correlation
8 sampling theorem
9 Butterworth filter
PRG1
%program to generate different types of waveform
% program for generation of unit sample
clc;clear all;
close all;
t = -3:1:3;
y = [zeros(1,3),ones(1,1),zeros(1,3)];% y=[0,0,0,1,0,0,0]
subplot(2,2,1);
stem(t,y);
ylabel('Amplitude------>');
xlabel('(a)n ------>');
title('Unit Impulse Signal')
% program for genration of unit step of sequence
t = -4:1:9;
y1 = [zeros(1,4),ones(1,10)];
subplot(2,2,2);
stem(t,y1);
ylabel('Amplitude------>');
xlabel('(b)n ------>');
title('Unit step');
% program for generation of ramp signal
%n1 = input('Enter the value for end of the seqeuence '); %n1 = <any value>7 %
x = 0:9;
subplot(2,2,3);stem(x,x);
ylabel('Amplitude------>');
xlabel('(c)n ------>');
title('Ramp sequence');
% program for generation of exponential signal
%n2 = input('Enter the length of exponential seqeuence '); %n2 = <any value>7
%
t = 0:3;
%a = input('Enter the Amplitude'); %a=1%
a=1;
y2 = exp(a*t);
subplot(2,2,4);stem(t,y2);
ylabel('Amplitude------>');
xlabel('(d)n ------>');
title('Exponential sequence');
disp('Unit impulse signal');
disp('Unit step signal');
disp('Unit Ramp signal');
disp('Exponential signal');
Results
Convolution

%Linear convolution: without using MATLAB function

clear all;
x=input('Enter the sequence x(n) ');
h=input('Enter the sequence h(n) ');
N1=length(x);
N2=length(h);
h=[h zeros(1,N1)]; %zero pad each input
x=[x zeros(1,N2)];
disp('Sequence x(n)');
disp(x);
disp('Sequence h(n)');
disp(h);
for i = 1:N1+N2-1;

y(i)= 0;
for j=1:i;
y(i)=y(i)+x(j)*h(i-j+1)
end
end

stem(y);
disp(y);
ylabel('Y[n]');
xlabel('----->n');
title('Convolution of Two Signals without conv function');

Result
Enter the sequence x(n) [1 2 3 4]
Enter the sequence h(n) [4 3 2 1]
Sequence x(n)
1 2 3 4 0 0 0 0
Sequence h(n)
4 3 2 1 0 0 0 0
y=
4 11 20 30 20 11 4
%linear convolution: using conv MATLAB function
% right sided sequence

clear all;
x1=input('enter first sequence');

subplot(2,3,1);
stem(x1);
title('x1(n)');
x2=input('enter second sequence');
subplot(2,3,2);
stem(x2);
title('x2(n)');
y= conv(x1,x2); % conv is function for convolution
subplot(2,3,3);
stem(y);
title('linear convolution');
disp(y);

%Circular convolution :without using MATLAB function


clear all;
x=input('Enter the sequence x(n) ');
h=input('Enter the sequence h(h) ');
N1=length(x);
N2=length(h);
N3=N1-N2;
N=length(x)
%N3 is used for zero padding
if(N3>0)
h=[h zeros(1,abs(N3))];
else
x=[x zeros(1,abs(N3))]; %take absolute value if N3 is negative
end
disp('Sequence x(n) ');
disp(x);
disp('Sequence h(n) ');
disp(h);
n =1:N
disp(n);
y=0;
for m=1:N
disp(m);
y=y+x(m)*h(mod(n-m,N)+1) %circular convolution
end
Result
Enter the sequence x(n) [1 2 3 4]
Enter the sequence h(h) [2 3 4 5]
y = 36 38 36 30

%Linear convolution using DFT IDFT method

x1=input('Enter the sequence x1(n) ');


x2=input('Enter the sequence x2(n) ');
N1=length(x1);
N2=length(x2);
N3=N1+N2-1;
if (N3>N2)
x2=[x2 zeros(1,N3-N2)];
end
if (N3>N1)
x1=[x1 zeros(1,N3-N1)];
end
X1=fft(x1,N3)
X2=fft(x2,N3)
Y=X1.*X2;
y=ifft(Y,N3)
subplot(3,1,1);
n1=0:1:length(x1)-1;
stem(n1,x1);
xlabel('time');
ylabel('Amplitude');
title('First Sequence');
subplot(3,1,2);
n2=0:1:length(x2)-1;
stem(n2,x2);
xlabel('time');
ylabel('Amplitude');
title('Second Sequence');
subplot(3,1,3);
n=0:1:length(y)-1;
stem(n,y);
xlabel('Time');
ylabel('Amplitude');
title('Linear Convolution using DFT and IDFT');
Enter the sequence x1(n) [1 2 3 4]
Enter the sequence x2(n) [9 8 1 6]

X1 = 10.0000 -2.0245 - 6.2240i 0.3460 + 2.4791i 0.1784 - 2.4220i 0.1784 + 2.4220i 0.3460 -
2.4791i -2.0245 + 6.2240i

X2 = 24.0000 8.3596 - 9.8329i 10.0598 - 2.6746i 1.0806 - 8.5388i 1.0806 + 8.5388i 10.0598 +
2.6746i 8.3596 + 9.8329i

y = 9.0000 26.0000 44.0000 68.0000 47.0000 22.0000 24.0000


%Circular Convolution using DFT IDFT
x1 = input('Enter the first sequence');
x2 = input('Enter the second sequence');
N1= length(x1);
N2= length(x2);
N=max(N1,N2);
N3=N1-N2;
if(N3>0)
x2=[x2 zeros(1,N3)];
else
x1=[x1 zeros(1,N3)];
end
X1=fft(x1,N)
X2=fft(x2,N)
y=X1.*X2
y=ifft(y,N)
subplot(3,1,1);
n1=0:1:length(X1)-1;
stem(n1,X1);
xlabel('Time');
ylabel('Amplitude');
title('First sequence');
subplot(3,1,2);
n2=0:1:length(X2)-1;
stem(n2,X1);
xlabel('Time');
ylabel('Amplitude');
title('Second sequence');
subplot(3,1,3);
n=0:1:length(y)-1;
stem(n,y);
xlabel('Time');
ylabel('Amplitude');
title('Circular Convolution using DFT and IDFT');

Result

Enter the first sequence[1 2 3 4]


Enter the second sequence[5 6 7 8 9]
X1 =
10.0000 -4.0451 - 1.3143i 1.5451 - 2.1266i 1.5451 + 2.1266i -4.0451 + 1.3143i
X2 =
35.0000 -2.5000 + 3.4410i -2.5000 + 0.8123i -2.5000 - 0.8123i -2.5000 - 3.4410i
y=
1.0e+002 *
3.5000 0.1464 - 0.1063i -0.0214 + 0.0657i -0.0214 - 0.0657i 0.1464 + 0.1063i
y=
75 75 70 60 70
Verification of sampling theorem :nyquist rate sampling

t=0:0.01:1;
f=5;
x=cos(2*pi*f*t);
subplot(3,1,1);
plot(t,x);
title('original signal');
fs=2*f;
n=0:1/fs:1;
xn=cos(2*pi*f*n);
subplot(3,1,2);
stem(n,xn);
title('sampled signal');
n1=0:1/fs:1;
xn1=interp1(n,xn,n1);
subplot(3,1,3);
plot(n1,xn1,t,x);
title('reconstructed signal');
%%Butterworth Prototype
Ap=input('enter passband attenuation in db =');
As=input('enter stopband attenuation in db =');
fp=input('enter passband frequency in hz =');
fs= input('enter stopband frequency in hz =');
Fs=input('enter sampling frequency in hz=');
W1=2* fp / Fs ;
W2=2* fs / Fs ;
[N,Wn]=buttord(W1,W2,Ap,As)
[b,a]=butter(N,Wn)
freqz(b,a);
title('Butterworth frequency response')

%INPUT:
%enter passband attenuation =1
%enter stopband attenuation =15
%enter passband frequency =1500
%enter stopband frequency =2000
%enter sampling frequency=8000

enter passband attenuation in db =1

enter stopband attenuation in db =15

enter passband frequency in hz =1500

enter stopband frequency in hz =2000

enter sampling frequency in hz=8000

N= 6
Wn = 0.4104

b = 0.0117 0.0699 0.1748 0.2331 0.1748 0.0699 0.0117

a = 1.0000 -1.0635 1.2005 -0.5836 0.2318 -0.0437 0.0043


DFT without using MATLAB function
x=input('Enter the sequence x= ');
N=input('Enter the length of the DFT N= ');
len=length(x);
if N>len
x=[x zeros(1,N-len)];
end
i=sqrt(-1);
w=exp(-i*2*pi/N);
n=0:(N-1);
k=0:(N-1);
nk=n'*k;
W=w.^nk;
X=x*W;
disp(X);
subplot(211);
stem(k,abs(X),'filled');
title('Magnitude Spectrum');
xlabel('Discrete frequency');
ylabel('Amplitude');
grid on;
subplot(212);
PhaseX=angle(X)*180/pi
plot(k,angle(X));
title('Phase Spectrum');
xlabel('Discrete frequency');
ylabel('Phase Angle');
grid on;
result: Enter the sequence x= [1 2 3 4]
Enter the length of the DFT N= 4
Columns 1 through 3
10.0000 -2.0000 + 2.0000i -2.0000 - 0.0000i
Column 4
-2.0000 - 2.0000i
PhaseX =
0 135.0000 -180.0000 -135.0000
%Calculation of N-point DFT and IDFT using MATLAB function

x1=input('enter the sequence X1=');


N=input('enter the value of N=');
xk=fft(x1,N);
subplot(2,2,1);
n=0:1:length(xk)-1;
stem(n,abs(xk));
disp(abs(xk));
xlabel('time index');
ylabel('amplitude');
title('magnitude plot');
subplot(2,2,2);
stem(n,angle(xk));
disp(angle(xk));
xlabel('time index');
ylabel('amplitude');
title('angle plot');
xk1=ifft(xk,N);
subplot(2,2,3);
stem(n,(xk1));
xlabel('time index');
ylabel('amplitude');
title('inverse fourier transform');
Result

enter the sequence X1=[1 2 3 4]


enter the value of N=4
10.0000 2.8284 2.0000 2.8284
0 2.3562 3.1416 -2.3562
%Auto and cross correlation of two sequences and verification of their
properties
x=input('enter the sequences')
subplot(2,2,1);
n=0:1:length(x)-1;
stem(n,x);
xlabel('n');
ylabel('amplitude');
title('input sequence');
Rxx=xcorr(x,x);
subplot(2,2,2);
nRxx=-length(x)+1:length(x)-1;
stem(nRxx,Rxx);
xlabel('auto correlation axis');
ylabel('auto correlation magnitude');
title('auto correlation output');

%verification of properties

energy = sum(x.^2)
center_index = ceil(length(Rxx)/2)
Rxx_0 = Rxx(center_index)
if Rxx_0==energy
disp('Rxx(0) gives energy - proved');
else
disp('Rxx(0) gives energy - not proved');
end
Rxx_right=Rxx(center_index:1:length(Rxx))
Rxx_left=Rxx(center_index:-1:1)
if Rxx_right == Rxx_left
disp('Rxx is even');
else
disp('Rxx is odd');
end

result

enter the sequences [4 5 6 7]


x= 4 5 6 7
energy = 126
center_index = 4
Rxx_0 = 126
Rxx(0) gives energy - proved
Rxx_right = 126.0000 92.0000 59.0000 28.0000
Rxx_left = 126.0000 92.0000 59.0000 28.0000
Rxx is even
plot

%cross correlation
X=[1 2 4 6 5]
Y=[1 3 2 1 4]
n = 0:1:length(X)-1;
m = 0:1:length(Y)-1;
subplot(2,2,1);
stem(n,X);
xlabel('n');
ylabel('amplitude');
title('input1 sequence');
subplot(2,2,2);
stem(m,Y);
xlabel('n');
ylabel('amplitude');
title('input2 sequence');
RXY = xcorr(X,Y)
nRXY = -length(X) +1:length(X)-1
subplot(2,2,3);
stem(nRXY,RXY);
xlabel('crosscorr axis');
ylabel('crosscorr magnitude');
title('crosscorr output');
%solving a given difference equation without initial condition

b=input('enter the co efficients of x(n), x(n-1)... ');


a=input('enter the co efficients of y(n),y(n-1).....');
n=input('enter the number of samples of imp response');

[h,t]=impz(b,a,n);
disp(h);
stem(t,h);
grid on;
xlabel('samples n');
ylabel('amplitude');
title('unit sample response');

%enter the co efficients of x(n), x(n-1)... [1 0.5 0.85]


%enter the co efficients of y(n),y(n-1).....[1 -1 -1]
%enter the number of sampples of imp response 4

Result
enter the co efficients of x(n), x(n-1)... [2 -1]
enter the co efficients of y(n),y(n-1).....[1 3 2]
enter the number of samples of imp response4
2
-7
17
-37

You might also like