electronics & communication dept. · 7 write a matlab program to compute linear convolution...
TRANSCRIPT
Electronics & Communication Dept.
1
Government Engineering College, Bharuch
Electronics and Communication Department
Semester 7
Lab Manual
For
Digital Signal Processing (171003)
Academic Term: 15-6-2015 to 15-10-2015
Electronics & Communication Dept.
2
Index
Sr. No.
Title Page No.
Date Sign
1 Generate the following sequence with MATLAB
a) UNIT Sample sequence
b) Unit step sequence
c) Ramp sequence
2 Generate the following sequence with MATLAB.
a) Complex exponential sequence.
b) Sine, Cosine, Square wave at 5 Hz.
3 Write a MATLAB program to generate
a) to plot Impulse response computation using
stem function.
b) Signal smoothing by moving- average Filter.
4 Write a MATLAB program to generate
a) Computation of cross-correlation of a
sequence
b) Computation of Autocorrelation of a
sequence.
c) Linear convolution of a two sequence
5 Write a MATLAB program to generate
a) Partial fraction expansion to Rational Z-
transform
b) Determination of the rotational Z-trnsform
from its poles and zeros and vice versa. c) Determination of the factored form of a
Rational Z-transform. d) Power series expansion of a Rational Z-
transform.
6 Write a MATLAB program to generate
a) DFT (Discrete Fourier Transform)
computation.
b) IDFT (Inverse Discrete Fourier Transform)
computation.
7 Write a MATLAB program to Compute Linear
convolution with DFT
8 Write a MATLAB program to generate
a) Generate the filter coefficients.
b) 4-th order analog Butterworth low pass filter.
9 Write a MATLAB program to generate
a) Butterworth low pass filter.
b) Elliptic low pass filter.
c) Type 1 chebyshev Low pass filter.
Electronics & Communication Dept.
3
10 Write a MATLAB program to generate
a) Estimation of FIR Filter Order using
remezord.
b) IIR Butterworth Band pass Filter.
c) Type 1 Chebyshev IIR High pass Filter
Design
d) Elliptic IIR Low pass Filter Design.
11 Write a MATLAB program to generate
a) Generation of rectangular, hamming,
hanning, blackman and Kaiser Window.
b) Low pass filter Design Using the Kaiser
Window.
c) Coefficient Quantization Effects on the
Frequency Response of a Cascade Form IIR
Filter
Electronics & Communication Dept.
4
EXP.1 Generate the following sequence with MATLAB
a) UNIT Sample sequence
b) Unit step sequence
c) Ramp sequence
Program:
a) UNIT Sample sequence
clc;
clear all;
close all;
n=-50:1:50;
x=[zeros(1,50),1,zeros(1,50)];
stem(n,x);
-50 -40 -30 -20 -10 0 10 20 30 40 500
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
samples---->
am
plitu
de--
----
->
Unit impulse sequence
Electronics & Communication Dept.
5
b) Unit step sequence
clc;
clear all;
close all;
n=-50:1:50;
x=[zeros(1,50),ones(1,51)];
stem(n,x);
-50 -40 -30 -20 -10 0 10 20 30 40 500
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
samples---->
am
plit
ude--
----
->
Unit step sequence
Electronics & Communication Dept.
6
c) Unit Ramp Sequence
clc;
clear all;
close all;
n=-50:1:50;
x=zeros(1,101)
t=1:1:102;
for t=1:1:101
if t>=51
x(1,t)=(t-51);
else
x(1,t)=0;
end
end
stem(n,x)
Output:
-50 -40 -30 -20 -10 0 10 20 30 40 500
5
10
15
20
25
30
35
40
45
50
samples---->
am
plit
ude--
----
->
Ramp sequence
Electronics & Communication Dept.
7
EXP.2 Generate the following sequence with MATLAB.
a) Complex exponential sequence.
clc;
clear all;
a=input('Type in real exponent : ');
b=input('Type in complex exponent : ');
c=a+b*i;
k=input('Type in the gain constant : ');
N=input('Type in length of sequence : ');
n=1:N;
x=k*exp(c*n);
subplot(2,1,1)
stem(n-1,real(x));
xlabel('time index n');ylabel('amplitude');
title('real part');
subplot(2,1,2)
stem(n-1,imag(x));
xlabel('time index n');ylabel('amplitude');
title('imaginary part');
Electronics & Communication Dept.
8
INPUT:
Type in real exponent : -1/12
Type in complex exponent : pi/6
Type in the gain constant : 4
Type in length of sequence : 40
OUTPUT:
Electronics & Communication Dept.
9
b) Sine, Cosine, Sinc wave at 5 Hz. clc;
clear all;
f=input('Enter the value of signal freq: ');
fs=input('Enter the value of sampling freq: ');
a=input('Enter the amplitude of signal:');
t=0:1/fs:1;
x=a*sin(2*pi*f*t);
subplot(3,1,1);plot(x);
grid on;
xlabel('Time index n samples'); ylabel('Amplitude');
y=a*cos(2*pi*f*t);
subplot(3,1,2);plot(y);
grid on;
xlabel('Time index n samples'); ylabel('Amplitude');
z=a*sinc(2*pi*f*t);
subplot(3,1,3);plot(z);
grid on;
xlabel('Time index n samples'); ylabel('Amplitude');
Electronics & Communication Dept.
10
INPUT:
Enter the value of signal freq: 5
Enter the value of sampling freq: 100
Enter the amplitude of signal:10
OUTPUT:
Electronics & Communication Dept.
11
EXP.3 Write a MATLAB program to generate
a) Programme to plot Impulse response computation using stem function.
clc;
clear all;
n=input('Enter the no of samples: ');
p=[0.8 0.44 -0.36 -0.02];
d=[1 0.7 -0.45 -0.6];
imp=[1 zeros(1,n-1)];
stp=[ones(1,n)];
k=0:1:n-1;
ir=filter(p,d,imp);
subplot(2,1,1)
stem (k,ir);
title('impulse response');
grid on;
axis([0 50 -1 1.5]);
str=filter(p,d,stp);
subplot(2,1,2)
stem (k,str);
title('step response');
grid on;
axis([0 50 -0.5 1.5]);
Electronics & Communication Dept.
12
INPUT:-
Enter the no of samples: 41
OUTPUT:-
Electronics & Communication Dept.
13
b) Signal smoothing by moving- average Filter.
clc
clear all;
w=input('Enter window size:');
fs=input('Enter sampling freq:');
f=input('Enter freq:');
t=0:1/fs:1;
x=sin(2*pi*f*t);
e=0.5*randn(1,fs+1);
xe=x+e;
for n=1:fs-w
xblk=xe(1,n:n+w-1);
y(n)=(1/w)*sum(xblk);
end
subplot 211; plot(xe);title('Noisy signal');grid on;
subplot 212; plot(y);title('filtered signal');grid on;
Electronics & Communication Dept.
14
INPUT:-
Enter window size: 200
Enter sampling freq: 10000
Enter freq: 10
OUTPUT:-
Electronics & Communication Dept.
15
EXP. 4 Write a MATLAB program to compute.
a) Computation of cross-correlation of a sequence
clc;
x=input('Enter the first frequency: ');
h=input('Enter the second frequency:');
y=xcorr(x,h);
disp(y);
figure
stem(y);
INPUT:
Enter the first frequency: [5 4 2 6]
Enter the second frequency:[ 0 1 2 3]
15 22 19 26 14 6 0
OUTPUT:
Electronics & Communication Dept.
16
b) Computation of Autocorrelation of a sequence.
clc;
clear all;
x=input('Enter the first frequency: ');
y=xcorr(x);
disp(y);
figure
stem(y);
grid on;
INPUT:
Enter the first frequency: [2 4 7 3]
6.0000 26.0000 57.0000 78.0000 57.0000 26.0000 6.0000
OUTPUT:
Electronics & Communication Dept.
17
c) Linear convolution of a two sequence clc;
clear all;
a=input('Type in the first sequence: ');
b=input('Type in the second sequence: ');
c=conv(a,b);
M=length(c)-1;
n=0:1:M;
disp('output seq=');disp(c);
stem(n,c);
xlabel('time index n');
ylabel('amplitude');
Electronics & Communication Dept.
18
INPUT:
Type in the first sequence [1 2 5 4]
type in the second sequence[1 5 4 9]
Output seq=
1 7 19 46 58 61 36
OUTPUT:
Electronics & Communication Dept.
19
EXP.5 Write a MATLAB program for.
a) Partial fraction expansion to Rational Z-transform
clc;
clear all;
num=input('type in numeractor coeff');
den=input('type in denominator coeff');
[r,p,k]=residuez(num,den);
disp('residues');disp(r);
disp('Poles');disp(p);
disp('Constant');disp(k);
INPUT:
Type in numerator coeff [3 4 3]
Type in denominator coeff [1 3 4]
OUTPUT:
Residues
1.1250 + 0.6142i
1.1250 - 0.6142i
Poles
-1.5000 + 1.3229i
-1.5000 - 1.3229i
Constant
0.7500
Electronics & Communication Dept.
20
b) Determination of the rotational Z-trnsform from its poles and zeros and vice versa.
clc
clear all;
num = input('type in the numerator coeff');
den = input('type in the denominator coeff');
[z,p,k]=tf2zp(num,den);
m = abs(p);
disp('zeros ar at');disp(z);
disp('poles ar at');disp(p);
disp('gain constant');disp(k);
disp('radius of poles');disp(m);
sos = zp2sos(z,p,k);
disp('second order sections ');disp(real(sos));
zplane(num,den);
Electronics & Communication Dept.
21
INPUT:
type in the numerator coeff[1 2 3]
type in the denominator coeff[3 4 2]
OUTPUT:
zeros ar at
-1.0000 + 1.4142i
-1.0000 - 1.4142i
poles ar at
-0.6667 + 0.4714i
-0.6667 - 0.4714i
gain constant
0.3333
radius of poles
0.8165
0.8165
second order sections
0.3333 0.6667 1.0000 1.0000 1.3333 0.6667
Electronics & Communication Dept.
22
c) Determination of the factored form of a Rational Z-transform.
clc;
clear all;
z= input('type in the zeroes:');
p= input('type in the poles:');
k= input('type in the constant:');
[num,den]=zp2tf(z,p,k)
disp(num);
disp(den);
INPUT:]
type in zeros: 3
type in the poles:3
type in the constant:4
OUTPUT:
num =
4 -12
den =
1 -3
4 -12
1 -3
Electronics & Communication Dept.
23
d) Power series expansion of a Rational Z-transform.
clc;
clear all;
L=input('type in the length of output vector');
num=input('enter num poly: ');
den=input('enter den poly: ');
[y,t]=impz(num,den,L);
disp('coeff of the power series expansion');
disp(y);
INPUT:
type in the length of output vector[1 2 3]
enter num poly: [2 3 4]
enter den poly: [1 3 6]
OUTPUT:
coeff of the power series expansion
-3
1
15
Electronics & Communication Dept.
24
EXP.6 Write a MATLAB program to compute.
a) DFT (Discrete Fourier Transform) computation. clc;
clear all;
N=input('Enter the value of N:');
fs=input('Enter the value of sampling freq:');
f=input('Enter the value of signal freq:');
t=0:1/fs:1;
x=sin(2*pi*f*t);
y=fft(x,N);
ymag=abs(y(1,1:N/2));
yangle=angle(y(1,1:N/2));
subplot 411; plot(x);title('input signal');
subplot 412; stem(ymag);title('Magnitude Spectrum');
subplot 413; stem(yangle);title('Phase Spectrum');
subplot 414; plot(ymag);title(' Magnitude Spectrum');
Electronics & Communication Dept.
25
INPUT:
Enter the value of N:100
Enter the value of sampling freq:100
Enter the value of signal freq:10
OUTPUT:
Electronics & Communication Dept.
26
b) IDFT (Inverse Discrete Fourier Transform) computation.
clc;
clear all;
N=input('Enter the value of N:');
fs=input('Enter the value of sampling freq:');
f=input('Enter the value of signal freq:');
t=0:1/fs:1;
x=sin(2*pi*f*t);
y=fft(x,N);
z=ifft(y,N);
z=real(z);
ymag=abs(y(1,1:N/2));
yangle=angle(y(1,1:N/2));
subplot 411; plot(x);title('input signal');
subplot 412; stem(ymag);title('Frequency Spectrum');
subplot 413; stem(yangle);title('Phase Spectrum');
subplot 414;plot(z);title('inverse DFT');
Electronics & Communication Dept.
27
INPUT:
Enter the value of N: 200
Enter the value of sampling freq: 200
Enter the value of signal freq: 20
OUTPUT:
Electronics & Communication Dept.
28
EXP.7 Write a MATLAB program for.
a) Computation of Linear convolution with DFT
clc;
clear all;
g=input('Type in the first sequence:');
h=input('Type in the second sequence:');
ga=[g zeros(1,length(h)-1)];
ha=[h zeros(1,length(g)-1)];
G=fft(ga);
H=fft(ha);
Y=G.*H;
y=ifft(Y);
subplot 411; stem(g); title('First sequence');
subplot 412; stem(h); title('second sequence');
subplot 413; stem(y); title('New sequence');
subplot 414; plot(y); title('New sequence');
Electronics & Communication Dept.
29
INPUT:
Type in the first sequence:[1 2 5]
Type in the second sequence:[ 6 7 8]
OUTPUT:
Electronics & Communication Dept.
30
EXP.8 Write a MATLAB program for.
a) Generate the filter coefficients.
clc;
clear all;
h1=ones (1,5)/5;
h2=ones(1,14)/14;
w=0:pi/255:pi;
H1=freqz(h1,1,w);
H2=freqz(h2,1,w);
m1=abs(H1);m2=abs(H2);
plot(w/pi,m1,'r-',w/pi,m2,'b--');
grid on;
ylabel('Magnitute ');xlabel('\omega/pi');
legend('r','M=5','b--','M=18');
ph1=angle(H1)*180/pi;
ph2=angle(H2)*180/pi;
plot(w/pi,ph1,w/pi,ph2);
grid on;
ylabel('phase and angle ');xlabel('\omega/pi');
legend('r','M=5','b--','M=18');
Electronics & Communication Dept.
31
OUTPUT:
Magnitude Plot
Phase Plot
Electronics & Communication Dept.
32
b) 4-th order analog Butterworth low pass filter.
clc;
clear all;
[z,p,k] = buttap(4);
disp('Poles are at');disp(p);
[pz, pp] = zp2tf(z, p, k);
disp('Numerator polynomial'); disp(pz)
disp('Denominator polynomial'); disp(real(pp))
omega = [0: 0.01: 5];
h = freqs(pz,pp,omega);
plot (omega,20*log10(abs(h)));grid
xlabel('Normalized frequency'); ylabel('Gain, dB');
Electronics & Communication Dept.
33
OUTPUT:
Poles are at
-0.38268343236509 + 0.92387953251129i
-0.38268343236509 - 0.92387953251129i
-0.92387953251129 + 0.38268343236509i
-0.92387953251129 - 0.38268343236509i
Numerator polynomial
0 0 0 0 1
Denominator polynomial
Columns 1 through 2
1.00000000000000 2.61312592975275
Columns 3 through 4
3.41421356237309 2.61312592975275
Column 5
1.00000000000000
Electronics & Communication Dept.
34
EXP.9 Write a MATLAB program for.
a) Butterworth low pass filter.
clc;
clear all;
N=input('Type in filter order=');
wn=input('3-dB cutoff frequency=');
%Determine the transfer function
[num,den]=butter(N,wn,'s');
%compute and plot the frequency response
omega=[0:1200*pi];
h=freqs(num,den,omega);
plot(omega/(2*pi),20*log10(abs(h)));grid on;
xlabel('Frequency,hz');ylabel('Gain,dB');
Electronics & Communication Dept.
35
INPUT:
Type in filter order=10
3-dB cutoff frequency=400
OUTPUT:
Electronics & Communication Dept.
36
b) Elliptic low pass filter.
clc;
clear all;
N = input('Order = ');
Fp = input('Passband edge frequency in Hz = ');
Rp = input('Passband ripple in dB = ');
Rs = input('Minimum stopband attenuation in dB = ');
% Determine the coefficients of the transfer function
[num,den] = ellip(N,Rp,Rs,2*pi*Fp,'s');
% Compute and plot the frequency response
omega = [0: 200: 12000*pi];
h = freqs(num,den,omega);
plot (omega/(2*pi),20*log10(abs(h)));
xlabel('Frequency, Hz'); ylabel('Gain, dB');
Electronics & Communication Dept.
37
INPUT:
Order = 5
Passband edge frequency in Hz = 2000
Passband ripple in dB = 1
Minimum stopband attenuation in dB = 40
OUTPUT:
Electronics & Communication Dept.
38
c) Type 1 chebyshev Low pass filter.
clc;
clear all;
N = input('Order = ');
Fp = input('Passband edge frequency in Hz = ');
Rp = input('Passband ripple in dB = ');
% Determine the coefficients of the transfer function
[num,den] = cheby1(N,Rp,Fp,'s');
% Compute and plot the frequency response
omega = [0: 200: 12000*pi];
h = freqs(num,den,omega);
plot (omega/(2*pi),20*log10(abs(h)));grid on;
xlabel('Frequency, Hz'); ylabel('Gain, dB');
Electronics & Communication Dept.
39
INPUT:
Order = 3
Passband edge frequency in Hz = 2000
Passband ripple in dB = 1
OUTPUT:
Electronics & Communication Dept.
40
EXP.10 Write a MATLAB program for.
a) Estimation of FIR Filter Order using remezord.
clc;
clear all;
fedge = input('Type in the bandedges = ');
mval = input('Desired magnitude values in each band = ');
dev = input('Allowable deviation in each band = ');
FT = input('Type in the sampling frequency = ');
[N,fpts,mag,wt]=remezord(fedge,mval,dev,FT);
fprintf('Filter order is %d \n',N);
end;
INPUT:
Type in the bandedges = [1500 2000]
Desired magnitude values in each band = [1 0]
Allowable deviation in each band = [0.01 0.1]
Type in the sampling frequency = 8500
OUTPUT:
Filter order is 23
Electronics & Communication Dept.
41
b) IIR Butterworth Band pass Filter.
clc;
clear all;
w0=input('Enter the value of center frequency:');
bw=input('Enter the value of bandwidth:');
alpha=(1-sin(bw))/cos(bw);
beta=cos(w0);
k=(1-alpha)/2;
num=[k 0 -k];
den=[1-beta*(1+alpha) alpha];
[h,w]=freqz(num,den);
plot(w/pi,abs(h));grid on;
Electronics & Communication Dept.
42
INPUT:
Enter the value of center frequency:3
Enter the value of bandwidth:0.5
OUTPUT:
Electronics & Communication Dept.
43
c) Type 1 Chebyshev IIR High pass Filter Design
clc;
clear all;
N=input('Enter the order of the filter:');
Rp=input('Enter the passband ripple: ');
Wn=input('enter the pass band edge:');
[b,a]=cheby1(N,Rp,Wn,'high');
disp('numerator polynomial');
disp(b);
disp('denominator polynomial');
disp(a);
pause;
w=0:0.01/pi:pi;
h=freqz(b,a,w);
gain=20*log10(abs(h));
plot(w/pi,gain); axis([0 1 -50 5]); grid on;
xlabel('Normalized frequency');
ylabel('Gain,dB');
Electronics & Communication Dept.
44
INPUT:
Enter the order of the filter:8
Enter the passband ripple: 3
enter the pass band edge:0.4
OUTPUT:
numerator polynomial
0.0084 -0.0335 0.0502 -0.0335 0.0084
denominator polynomial
1.0000 2.3741 2.7057 1.5917 0.4103
Electronics & Communication Dept.
45
d) Elliptic IIR Low pass Filter Design.
clc;
clear all;
N = input('Order = ');
Fp = input('Passband edge frequency in Hz = ');
Rp = input('Passband ripple in dB = ');
Rs = input('Minimum stopband attenuation in dB = ');
[num,den] = ellip(N,Rp,Rs,2*pi*Fp,'s');
omega = [0: 20: 120*pi];
h = freqs(num,den,omega);
plot (omega/(2*pi),20*log10(abs(h))); grid on;
xlabel('Frequency, Hz'); ylabel('Gain, dB');
Electronics & Communication Dept.
46
INPUT:
Order = 8
Passband edge frequency in Hz = 30
Passband ripple in dB = 1
Minimum stopband attenuation in dB = 40
OUTPUT:
Electronics & Communication Dept.
47
EXP.11 Write a MATLAB program for.
a) Generation of rectangular,hamming,hanning,blackman and kaiser window.
clc;
clear all;
N = input('Order = ');
x1=hann(N);
x2=hamming(N);
x3=bartlett(N);
x4=blackman(N);
x5=triang(N);
x6=kaiser(N);
plot(x1);hold on; grid on;
plot(x2,'*');
plot(x3,'.');
plot(x4,'x');
plot(x5,'+');
plot(x6,'^');
Electronics & Communication Dept.
48
INPUT:
Order = 60
OUTPUT:
Electronics & Communication Dept.
49
b) Low pass filter Design Using the Kaiser Window.
clc;
clear all;
fpts = input('Type in the bandedges = ');
mag = input('Type in the desired magnitude values = ');
dev = input('Type in the ripples in each band = ');
type= input('Type in the sampling frequency = ');
[N,Wn,beta,ftype] = kaiserord(fpts,mag,dev,type)
kw = kaiser(N+1,beta);
b = fir1(N,Wn, kw);
[h,omega] = freqz(b,1,512);
plot(omega/pi,20*log10(abs(h)));grid;
xlabel('\omega/\pi'); ylabel('Gain, dB');
Electronics & Communication Dept.
50
INPUT:
Type in the bandedges = [1500 2000]
Type in the desired magnitude values = [1 0]
Type in the ripples in each band = [0.01 0.1]
Type in the sampling frequency = 8000
OUTPUT:
N =
36
Wn =
0.4375
beta =
3.3953
ftype =
low
Electronics & Communication Dept.
51
c) Coefficient Quantization Effects on the Frequency Response of a Cascade Form IIR Filter
clc;
clear all;
[z,p,k] = ellip(5,0.4,50,0.4);
[b,a] = zp2tf(z,p,k);
bq=roundn(b,-2);
aq=roundn(a,-2);
[h,w] = freqz(b,a,512); g = 20*log10(abs(h));
[hq,w] = freqz(bq,aq,512); gq = 20*log10(abs(hq));
plot(g);hold on; plot(gq,'r');
grid on;
OUTPUT: