matlab programming

SUBMITTED TO:MR. Y.P.S. MARAVIAISHWARY

SUBMITTED BY:gaurav sharma ROLL NO.-:ec041

CONTENT
S.NO . 1. 2. 3. 4. 5. 6. 7. 8. PROGRAMME
Write a program for y=exp^((1+3j)n) function Write a program for the function y=1.5^n and for different values of n. Write a program for step function Write a program for ramp function Write a program to plot periodic function. 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). 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

9. 10. 11. 12. 13. 14. 15.

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 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 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. 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

19.

20.

21.

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 Output:2 0 1 8 1 6 1 4 1 2 1 0 8 6 4 2 0 0

0.0610 + 0.0417i

0.1249 +0.1573i

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 8

Output:3 0

2 5

2 0

1 5

1 0

5

0 8

6

4

2

0

2

4

6

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

2

2 .5

3

3 .5

4

Q-4) Write a program for ramp function. Solution: Editor window : n=5; t=0:1:n-1; y=t; stem(t,y); Output:-

4 3 .5 3 2 .5 2 1 .5 1 0 .5 0 0

0 .5

1

1 .5

2

2 .5

3

3 .5

4

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);

stem(n,y2) output:
1 0 .5 0 - .5 0 1 0

5

1 0

1 5

2 0

2 5

3 0

3 5

4 0

1 0 .5 0 - .5 0 1 0

5

1 0

1 5

2 0

2 5

3 0

3 5

4 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 a=1 C =1 2 3 1 3 6 5 3 1

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:s p s nl imle ig a 4

2

0

2 1

1 .5

2

2 .5

3 s a ds n l c le ig a

3 .5

4

4 .5

5

1 0

5

0

5 1

1 .5

2

2 .5

3

3 .5

4

4 .5

5

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

g e s nl iv n ig a 4 2 0 2 4 3

- .5 2

2

- .5 1

1

- .5 0 f ld ds n l o e ig a

0

0 .5

1

1 .5

2

4 2 0 2 4 2

- .5 1

1

- .5 0

0

0 .5

1

1 .5

2

2 .5

3

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
1 8 1 6 1 4 1 2 1 0 8 6 4 2 0 1

17

15

13

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 0

Q.17) Theory of digital filters:

Digital filter can be classified as : 1. finite duration impulse response (FIR)

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 5.9465

Columns 8 through 13 5.0970 3.1856 1.4158 0.4248 0.0772 0.0064

a =Columns 1 through 7 1.0000 2.7575 4.9313 5.8618 5.2618 3.5782 1.8938

Columns 8 through 13 0.7714 0.2398 0.0550 0.0088 0.0009 0.0000

OUTPUT:
20 0 M g itu e (d a n d B)

0

-0 20

-0 40

0

0 .1

0 .2

0 .3 0 .4 0 .5 Nr a e Fe u n y ( omliz d r q e c

0 .6 0 .7 rd a p) a /s mle ×π

0 .8

0 .9

1

0 Ph se (d g e a e re s)

-0 50

- 00 10

- 50 10

0

0 .1

0 .2

0 .3 0 .4 0 .5 Nr a e Fe u n y ( omliz d r q e c

0 .6 0 .7 da p) ×πra /s mle

0 .8

0 .9

1

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:

<<fdatool

Miu R oe B a td e n ( ) g e s sd n p 0 -0 1 -0 2 Mg it d ( B a nu e d ) -0 3 -0 4

-0 5 -0 6

0

0 . 5

1

1 . 5 F uc(H rqn k ) eey z

2

2 . 5

P/ eP oZol t l r o e 1 0 . 8 0 . 6 0 . 4 I a i ayP r mg r at n 0 . 2 0 -. 0 2 -. 0 4 -. 0 6 -. 0 8 1 -. 1 5 1 -. 0 5 0 RP ea ar l t 0 . 5 1 1 . 5

PsR oe hee n a s s p 0 1 2 Pa e( a i n ) hs r d s a 3 4 5 6 7 8 0

0 . 5

1

1 . 5 F uc(H rqn k ) eey z

2

2 . 5

Miu (BnP eeoe a tdd a h R ns g e ) da s s n s p 0 -0 1 -0 2 Mg it d ( B a nu e d ) -0 3 -0 4 -.6 00 09 -.2 16 21 -.9 21 34 -.5 36 57 -.2 41 79 Pa e( a i n ) hs r d s a

-0 5 -0 6

-.8 57 82 -.5 72 04

0

0 . 5

1

1 . 5 F uc(H rqn k ) eey z

2

2 . 5

Gpe ruDy o l a 1 0 9 8 Go pd l y(ns mle ) ru e i a ps a 7 6 5 4 3 2 0 0 . 5 1 1 . 5 F uc(H rqn k ) eey z 2 2 . 5

Pe ly hD ae s a

4 3 . 5 Pa e e y( a i n / z h s Dl r d sH) a a 3 2 . 5 2 1 . 5 1 0 . 5 0 0 0 . 5 1 1 . 5 Fuc(H rqn k) eey z 2 2 . 5

Ipe sn m Roe u e s l p s 0 . 4

0 . 3

0 . 2 A pt d ml u e i

0 . 1

0

-. 0 1

-. 0 2 0 1 2 3 4 5 T (sos i e en) m cd m 6 7 8 9

SRoe tp sn ee s p 1 . 2

1

0 . 8 A pt d ml u e i

0 . 6

0 . 4

0 . 2

0 0

1

2

3

4 5 T (sos i e en) m cd m

6

7

8

9

Rf ooS m ooiee c u f swe n N rp d P t r u -0 1 9 P ef euny d/ z o r r qec ( B ) w/ H -0 2 0 -0 2 1 -0 2 2 -0 2 3 -0 2 4 -0 2 5 -0 2 6 0 0 . 5 1 1 . 5 FnH ru (z ec ) qy ek 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 0.1295 0.1295 0.0797 0.0528

a = 1.0000 -1.8107

2.4947 -1.8801

0.9537 -0.2336

Output:
0 M g itu e( B an d d )

-0 5

-0 1 0

-5 1 0

0

0 . 1

0 . 2

0 . 3 0 . 4 N a eFq n ( o lzdr u c r i m eey

0 . 5

0 . 6 0 . 7 / l e × rdap) πasm

0 . 8

0 . 9

1

1 0 0 P a e( e r e ) h s d ge s 0 -0 1 0 -0 2 0 -0 3 0

0

0 . 1

0 . 2

0 . 3 0 . 4 N a eFq n ( o lzdr u c r i m eey

0 . 5

0 . 6 0 . 7 rdap) asm / l e × π

0 . 8

0 . 9

1

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) n=7 f0 = 0 0.2000 0.4000 1.0000 a0 = 1 1 0 0 w =86.4863 1.0

command window: