matlab programming
SUBMITTED TO:MR. Y.P.S. MARAVIAISHWARY
SUBMITTED BY:gaurav sharma ROLL NO.-:ec041
CONTENT S.NO . 1.
PROGRAMME Write a program for y=exp^((1+3j)n) function
3.
Write a program for the function y=1.5^n and for different values of n. Write a program for step function
4.
Write a program for ramp function
5.
Write a program to plot periodic function.
6.
Write a program to perform convolution of two sequences Write a program to find scaled signal from given signal( to increase the magnitude of following program by a factor of 2.) Write a program to a causal LTI system by the diff equation y(n)=y(n-1)+y(n-2)+x(n-1) where x(n)=input signal, y(n)=output signal, Then find transfer function, plot the ROC, and poles and Zeros for H(Z)=Y(Z)/X(Z).
2.
7. 8.
9. 10. 11. 12. 13. 14. 15.
W.A.P. to find inverse z transform of following 2*z/(z-2)^2. W.A.P. to perform folding property on given sequence. W.A.P.to perform circular convolution of given sequence. W.A.P to perform roots plotting W.A.P to perform crosscorelation W.A.P to perform FFT of given sequences W.A.P to perform DFT of given sequences
DATE OF SIGNATURE SUBMISSION
16. 17. 18.
W.A.P to perform inverse DFT of given sequences Theory of digital filters. W.A.P.to find the order of low pass butterworth IIR digital filter having following specification: fp= 1500hz fs=1800hz Fs=5000hz Rp=2dB Rs=40dB
19.
W.A.P. Obtain the freq. response of low pass iir butterworth filter with following specifications: fp= 1500Hz fs=1800Hz Fs=5000Hz Rp=2dB Rs=40dB
20.
design a type 2 cabycshev lowpass filter with following specifications: fp=1200Hz fs=1700 Hz Fs=6000 Hz Rp=0.5dB Rs=50dB show magnitude and pole-zero respose.
21.
design elliptic IIR lowpass filter with following specifications:show magnitude and phase response . fp=800Hz fs=1000Hz Fs=4000Hz Rp=0.5db Rs=40db design a fir lowpass filter using hamming windows for following specification. Pass band edge=2rad/esc Stop band edge=4rad/sec Max. pass band ripplpe=0.1db Min. stopband attenuation =4.0db Sampling frequency =2rad/sec
22.
Show magnitude and phase response.
23.
FUNCTIONS USED IN FILE 1. clc:
clc clears all input and output from the command window display, giving you a “clean screen”.After using clc , you cannot use a scroll bar to see the history of functions , but you still can use the up arrow to recall statements from the command history.
2. disp( ): disp(text) displays text centered on the icon where text is any MATLAB expression that evaluates to a string. Disp(text,’textmode’,’on’)allows you to use TeX formatting command in the text.The TeX formatting commands in turn allows you to include symbols and greek letters in icon text.
3. length( ): The statement length(X) is equivalent to max(size(X)) for non-empty arrays and zero for empty arrays.n=length(X) returns the size of the longest dimension of X.If X is a vector , this is the same as its length.
4. stem( ): stem(X,Y) plots X v/s the columns of Y.X and Y must be vectors or matrices of the same size. Additionally , X can be a row or a column vector and Y a matrix with length(X) rows.
5. subplot( ): subplot divides the current figure into
a rectangular planes that are numbered rowwise . Each plane contains an axes object. Subsequent plots are output to the current plane. h= subplot(m,n,p) or subplot(mnp) breaks the figure window into an m –by-n matrix os small axes , selects the path axes object for the current plt, and returns the axes handle. The axes are counted along the top row of the figure window , then the second row, etc.
6. conv( ): w=conv(u,v) convolves vectors u and v, Algebraically , convolution is the same operation as multiplying the polynomials whose coefficients are the elements of u and v.
7. Zplane( ): This function displays the poles and zeroes of discrete time systems . Zplane(z,p) plots the zeroes specified in column vector z and the poles specified in column vector p in the current figure window. The symbol ‘O’ represents a zero and the symbol’x’ represents a pole. The plot includes the unit circle for reference. If z and
p are arrays , Zplane plots the poles and zeroes in the columns of z and p in different colours.
8. Title( ): Each axes graphics object can have one title . The title is located at the top and in the center of the axes . Title(‘string’) outputs the string at the top and in the center of the current axes.
9. Fft( ): Y=fft(X) returns the discrete fourier transform (DFT) of vector X , computed with the fast fourier transform algorithm.If X is a matrix , then FFT returns the fourier transform of each column of the matrix. IF X is the multidimensional array , Fft operates on the non singleton dimension . Y=fft(X,[],dim) and Y=fft(X,n,dim) applies the FFT operation across the dimension dim.
10. Ifft( ): Y= ifft(X) returs they inverse discrete fourier transform( DFT) of vector X, computed with a fast fourier transform(FFT) algorithm. If X is a matrix, ifft returns the inverse DFT of each column of the matrix.
Q-1) Write a program for y=exp^((1+3j)n) function. Editor window: n=0:1:5; y=exp((1+(j*.3))*n) stem(n,y); Command window:y =1.0e+002 * Columns 1 through 5 0.0100 0.0260 + 0.0080i 0.1978 + 0.5089i Column 6 0.1050 + 1.4804i
0.0610 + 0.0417i
0.1249 +0.1573i
Output:2 0 1 8 1 6 1 4 1 2 1 0 8 6 4 2 0 0
0 .5
1
1 .5
2
2 .5
3
3 .5
4
4 .5
5
Q-2) Write a program for the function y=1.5^n and for different values of n. Editor window: n=[-8:8] x=1.5.^n; stem(n,x); Command window: n= -8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
Output:30
25
20
15
10
5
0 -8
-6
-4
-2
0
2
4
6
8
8
Q-3) Write a program for step function. Solution: Editor window: n=5; t=0:1:n-1; y=ones(1,n); stem(t,y); Output:-
1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 0
0.5
1
1.5
Q-4) Write a program for ramp function. Solution: Editor window : n=5; t=0:1:n-1; y=t; stem(t,y); Output:-
2
2.5
3
3.5
4
4 3.5 3 2.5 2 1.5 1 0.5 0 0
0.5
1
1.5
2
2.5
Q-5) Write a program to plot periodic function. Solution: Editor window : clc; disp('to plot periodic func'); n=0:1:40; y1=sin(2*3.14*50*n); subplot(2,1,1); stem(n,y1) y2=cos(2*3.14*50*n); subplot(2,1,2);
3
3.5
4
stem(n,y2) output: 1 0.5 0 -0.5 -1 0
5
10
15
20
25
30
35
40
5
10
15
20
25
30
35
40
1 0.5 0 -0.5 -1 0
Q-6) Write a program for convolution. Editor window : b=[1 1 1] a=[1 2 3] C=conv(b,a) stem(C); Command window:b =1
1
a=1
2
3
C =1
3
6
1
5
3
Output:6
5
4
3
2
1
0 1
1.5
2
2.5
3
3.5
4
4.5
5
Q-7) Write a program to find scaled signal from given signal( to increase the magnitude of following program by a factor of 2.) Solution: Editor window: x=[2 -1 4 3 1]; z=2.*x; subplot(2,1,1); stem(x,'fill'); title('simple signal') subplot(2,1,2); stem(z,'fill') title('scaled signal')
Output:simplesignal 4
2
0
-2 1
1.5
2
2.5
3
3.5
4
4.5
5
3.5
4
4.5
5
scaledsignal 10
5
0
-5 1
1.5
2
2.5
3
Q.8) Write a program to a causal LTI system by the diff equation y(n)=y(n1)+y(n-2)+x(n-1) where x(n)=input signal, y(n)=output signal, Then find transfer function, plot the ROC, and poles and Zeros for H(Z)=Y(Z)/X(Z). Solution: Editor window: b=input('enter the coefficient of the numerator'); a=input('enter the coefficient of dnominator'); zplane(b,a); command window: enter the coefficient of the numerator[1] enter the coefficient of dnominator[1 -1 -1] Y(Z)=Z-1 Y(Z)+ Z-2Y(Z) + Z-1 X(Z) Y(Z)[1-Z-1-Z-2]=Z-1X(Z) As we know that : H(Z)=Y(Z)/X(Z) Hence :
H(Z)= Z-1 /(1-Z-1-Z-2 ) The funtion is causal,
Q-9) find inverse z transform of following 2*z/(z-2)^2 Editor window: g = 2*z/(z-2)^2 d=iztrans(g) command window: d =2^n*n
Q-10) write a programme to perform folding property on given sequence. Editor window: x=input ('enter the given sequence in sqaure brakets'); n1=input ('enter the lower limit of the sequence'); n2=input('enter the upper limit of the sequence'); n=[n1:n2]; m=-fliplr(n); z=fliplr(x) disp('the instants of the sequences are:'); disp(n); disp('the given sequence as:'); disp(x); disp('the instants of the folded sequence are:'); disp(m); disp('the folded sequence as:'); disp(z); subplot(2,1,1);
stem(n,x,'fill') title('given signal'); subplot(2,1,2); stem(m,z,'fill') title('folded signal'); command window: enter the given sequence in sqaure brakets[-3 -2 -1 1 2 3] enter the lower limit of the sequence-3 enter the upper limit of the sequence2 z =3
2
1
-1
-2
-3
the instants of the sequences are: -3 -2 -1 0 1 2 the given sequence as: -3 -2 -1 1 2
3
the instants of the folded sequence are: -2 -1 0 1 2 3 the folded sequence as: 3 2 1 -1 -2 -3
givensignal 4 2 0 -2 -4 -3
-2.5
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
1
1.5
2
2.5
3
foldedsignal 4 2 0 -2 -4 -2
-1.5
-1
-0.5
0
0.5
Q.11)write a programme to perform circular convolution of given sequence . Editor window: x=input('enter the first sequence'); h=input('enter the second sequence');
y=cconv(x,h,4) stem(y) command window: enter the first sequence[1 1 2 2] enter the second sequence[1 2 3 4] y= 15
17
15
13
18 16 14 12 10 8 6 4 2 0 1
1.5
2
2.5
3
3.5
4
Q.12)write a programme to perform roots plotting of given sequence . Editor window: P=[1 -6 -72 -27] r=roots(P); stem(r); title('roots plotting')
Output : -
Q. 13)wap to perform crosscorrelation of sequences. Editor window: a=[1 4 6 3]; y=[3 7 1 5]; z=xcorr(a,y) stem(z);
OUTPUT: -
Q. 14)wap to perform FFT of sequence. Editor window: x=[0 1 2 3 4 5 6 7]; a=fft(x,8) stem(a); Command window:
a = Columns 1 through 4 28.0000
-4.0000 + 9.6569i
-4.0000 + 4.0000i
-4.0000 + 1.6569i
Columns 5 through 8 -4.0000
-4.0000 - 1.6569i
-4.0000 - 4.0000i
-4.0000 - 9.6569i
OUTPUT:
Q. 15)wap to perform DFT of sequence. Editor window: x=input('enter the given sequence in square brackets:'); disp(x); A=x*dftmtx(4) Stem(A);
Command window: enter the given sequence in square brackets:[1 2 3 4] 1 2 3 4 A = Columns 1 through 3 10.0000 -2.0000 + 2.0000i -2.0000 Column 4 -2.0000 - 2.0000i OUTPUT:
Q. 16) WAP to perform inverse DFT of given sequence. Editor window: X=input('enter the given seq in sqaure brackets'); disp(X); N=length(X); Ai=conj(dftmtx(N))/N;
y=X*Ai
command window: enter the given seq in sqaure brackets[1 0 1 0] 1 0 1 0 y= 0.5000
0
0.5000
Q.17) Theory of digital filters:
Digital filter can be classified as : 1. finite duration impulse response (FIR)
0
2. infinite duration impulse response(IIR). IIR filter further can classified as : 1. Butterworth 2. Chebyshev type 1 3. Chebyshev type 2 4. Elliptical filter COMMENT LINE: • To calculate the order of the filter: [N,Wn]= butterworth(Wp,Ws,Rp,Rs)edge [N,Wp]=cheb1ord(Wp,Ws,Rp,Rs) [N,Ws]=cheb2ord(Wp,Ws,Rp,Rs) [N,Wp]=ellipord(Wp,Ws,Rp,Rs)
where Wp and Ws are edge frequencies for pass band and stop band resp. normalized from 0-1 , Rp is pass band ripple , N is the order of the filter, Rs is stop band attenuation in dB, Wn is length 2 -vector fp and fs are passband and stopband frequencies Wp=(2*fp/Fs) Ws=(2*fs/Fs) Q.18) Find the order of low pass butterworth IIR digital filter having following specification: fp= 1500hz fs=1800hz Fs=5000hz Rp=2dB Rs=40dB
Editor window: fp=1500; fs=1800 ; Fs=5000; Rp=2; Rs=40; Wp=(2*fp/Fs); Ws=(2*fs/Fs); [N,Wn]= buttord(Wp,Ws,Rp,Rs) fprintf('order of the filter:%f\n',N); command window: N =12 Wn = 0.6152 order of the filter:12.000000
NOTE: In case of lowpass and high pass filter the frequency scaling factor Wn is having only one value whereas for passband and stopband filter Wn is length 2-vector and N is the half the order of the filter to be designed . [b,a]= filter coefficent of length N+1 , vector b gives numerator and vector a gives denominator values in desending power of z.
[b,a]=butter(N,Wn) [b,a]=cheb1(N,Rp,Wp) [b,a]=cheb2(N,Rs,Ws) [b,a]=ellip(N,Rp,Rs,Wp)
Q.19)Obtain the freq. response of low pass iir butterworth filter with following specifications: fp= 1500Hz fs=1800Hz Fs=5000Hz Rp=2dB Rs=40dB
Editor window:
fp=1500; fs=1800 ; Fs=5000; Rp=2; Rs=40; Wp=(2*fp/Fs); Ws=(2*fs/Fs); [N,Wn]= buttord(Wp,Ws,Rp,Rs) fprintf('order of the filter:%f\n',N); [b,a]=butter(N,Wn) freqz(b);
command window: N =12 Wn =0.6152 order of the filter:12.000000 b =Columns 1 through 7 0.0064
0.0772
0.4248
1.4158
3.1856
5.0970
0.4248
0.0772
0.0064
5.8618
5.2618
3.5782
0.0088
0.0009
0.0000
5.9465
Columns 8 through 13 5.0970
3.1856
1.4158
a =Columns 1 through 7 1.0000
2.7575
4.9313
Columns 8 through 13 0.7714
0.2398
0.0550
1.8938
OUTPUT:
Magnitude (dB)
200
0
-200
-400
0
0.1
0.2
0.3 0.4 0.5 NormalizedFrequency (
0.6 0.7 r a d /s a m ple) ×π
0.8
0.9
1
0.1
0.2
0.3 0.4 0.5 NormalizedFrequency (
0.6 0.7 ×πrad/sample)
0.8
0.9
1
Phase (degrees)
0
-500
-1000
-1500
0
Q.20) design a type 2 cabycshev lowpass filter with following specifications: fp=1200Hz fs=1700 Hz Fs=6000 Hz Rp=0.5dB Rs=50dB show magnitude and pole-zero respose. COMMAND WINDOW:
<
M a g n itu d eR e s p o n s e(d B ) 0 -1 0
M agnitude(dB )
-2 0 -3 0 -4 0
-5 0 -6 0
0
0 .5
1
1 .5 F re q u e n c y(k H z )
2
2 .5
P o le /Z e roP lo t 1 0 .8 0 .6
Im aginaryP art
0 .4 0 .2 0 -0 .2 -0 .4 -0 .6 -0 .8 -1 -1 .5
-1
-0 .5
0 R e a lP a rt
0 .5
1
1 .5
P h a s eR e s p o n s e 0 -1
P hase(radians)
-2 -3 -4 -5 -6 -7 -8 0
0 .5
1
1 .5 F re q u e n c y(k H z )
2
2 .5
M a g n itu d e(d B )a n dP h a s eR e s p o n s e s -0 .0 6 0 9
-1 0
-1 .2 2 6 1
-2 0
-2 .3 9 1 4
-3 0
-3 .5 5 6 7
-4 0
-4 .7 2 1 9
-5 0
-5 .8 8 7 2
-6 0
-7 .0 5 2 4
0
0 .5
1
1 .5 F re q u e n c y(k H z )
2
2 .5
P hase(radians)
M agnitude(dB )
0
G ro u pD e la y 1 0 9
G roupdelay(insam ples)
8 7 6 5 4 3 2 0
0 .5
1
1 .5 F re q u e n c y(k H z )
2
2 .5
P h a s e D e la y
4
P haseD elay(radians/H z)
3 .5 3 2 .5 2 1 .5 1 0 .5 0 0
0 .5
1
1 .5 F re q u e n c y(k H z )
2
2 .5
Im p u ls e R e s p o n s e 0 .4
0 .3
A m plitude
0 .2
0 .1
0
-0 .1
-0 .2 0
1
2
3
4 5 T im e (m s e c o n d s )
6
7
8
9
S te p R e s p o n s e 1 .2
1
A m plitude
0 .8
0 .6
0 .4
0 .2
0 0
1
2
3
4 5 T im e (m s e c o n d s )
6
7
8
9
R o u n d o ffN o is e P o w e r S p e c t r u m
P o w e r/fre q u e n c y(d B /H z )
1 9 0 2 0 0 2 1 0 2 2 0 2 3 0 2 4 0 2 5 0 2 6 0 0
0 . 5
1
1 . 5 F r e q u e n c y ( k H z )
2
2 . 5
Q.21) Design elliptic IIR lowpass filter with following specifications show magnitude and phase response . fp=800Hz fs=1000Hz Fs=4000Hz Rp=0.5db Rs=40db Solution: Editor window: fp=800; fs=1000; Fs=4000; Rp=0.5; Rs=40; Wp=2*fp/Fs; Ws=2*fs/Fs; [N,Wn]= ellipord(Wp,Ws,Rp,Rs) fprintf('order of the filter:%f\n',N); [b,a]=ellip(N,Rp,Rs,Wp) freqz(b); Command window:
N =5 Wn= 0.4000 order of the filter:5.000000 b = 0.0528
0.0797
a = 1.0000 -1.8107
0.1295
0.1295
2.4947 -1.8801
0.0797
0.0528
0.9537 -0.2336
Output:
Magnitude(dB)
0
-5 0
-1 0 0
-1 5 0
0
0 .1
0 .2
0 .3 0 .4 N o rm a liz e dF re q u e n c y(
0 .5
0 .6 0 .7 d /s a m p le ) ×πra
0 .8
0 .9
1
0 .1
0 .2
0 .3 0 .4 N o rm a liz e dF re q u e n c y(
0 .5
0 .6 0 .7 r a d / s a m p l e ) ×π
0 .8
0 .9
1
Phase(degrees)
1 0 0 0 -1 0 0 -2 0 0 -3 0 0
0
Q.22) Design a fir lowpass filter using hamming windows for following specification. Pass band edge=2rad/esc Stop band edge=4rad/sec Max. pass band ripplpe=0.1db Min. stopband attenuation =4.0db Sampling frequency =2rad/sec Show magnitude and phase response. Solution: Editor window:
fs=20; f=[2 ,4]; a=[1,0]; Rp=0.1; Rs=40; %calculate allowable deviation or ripple dp=1-10^(-Rp/20); ds=1-10^(-Rs/20); dev=[dp ds]; %calculate order [n,f0,a0,w]=firpmord(f,a ,dev,fs) freqz(f0)
command window: n=7 f0 = 0 0.2000 0.4000 1.0000 a0 = 1 1 0 0 w =86.4863 1.0