dsp for begineers

14
“DIGITAL SIGNAL PROCESSING MATLAB PROGRAMMING FOR BEGINNERS “ AUTHOR : Mr. Priya Ranjan Muduli D D D e e e p p p t t t . . . O O O f f f E E E l l l e e e c c c t t t r r r o o o n n n i i i c c c s s s a a a n n n d d d C C C o o o m m m m m m u u u n n n i i i c c c a a a t t t i i i o o o n n n E E E n n n g g g i i i n n n e e e e e e r r r i i i n n n g g g N N A A T T I I O O N N A A L L I I N N S S T T I I T T U U T T E E O O F F T T E E C C H H N N O O L L O O G G Y Y , , R R o o u u r r k k e e l l a a

Upload: dhananjaya-mandal

Post on 12-Jan-2016

218 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: DSP for Begineers

“DIGITAL SIGNAL PROCESSING

MATLAB PROGRAMMING FOR

BEGINNERS “

AUTHOR : Mr. Priya Ranjan Muduli

DDDeeepppttt... OOOfff EEEllleeeccctttrrrooonnniiicccsss aaannnddd CCCooommmmmmuuunnniiicccaaatttiiiooonnn EEEnnngggiiinnneeeeeerrriiinnnggg

NNNAAATTTIIIOOONNNAAALLL IIINNNSSSTTTIIITTTUUUTTTEEE OOOFFF TTTEEECCCHHHNNNOOOLLLOOOGGGYYY ,,, RRRooouuurrrkkkeeelllaaa

Page 2: DSP for Begineers

PROGRAM:1

clc; clear all ; close all ; a=[1 8 2 3 5]; b=zeros(5,5); for i=1:5 for j=1:6-i b(i,j)=a(1,j+i-1); end end b b =

1 8 2 3 5

8 2 3 5 0

2 3 5 0 0

3 5 0 0 0

5 0 0 0 0

Program 2

clc; clear all ; close all ; a=[1 8 2 3 5]; b=zeros(5,5); for i=1:5 for j=i:5 b(i,j)=a(1,j-i+1); end end b b =

1 8 2 3 5

0 1 8 2 3

0 0 1 8 2

0 0 0 1 8

Page 3: DSP for Begineers

0 0 0 0 1

Program 3

clc; clear all ; close all ; a=[1 8 2 3 5]; b=[]; for i=1:5 b=[b;a]; p=a(5); a(2:5)=a(1:4); a(1)=p; end b b =

1 8 2 3 5

5 1 8 2 3

3 5 1 8 2

2 3 5 1 8

8 2 3 5 1

Program :

clc; clear all ; close all ; a=[1 8 2 3 5]; b=[]; for i=1:5 b=[b;a]; a(1:4)=a(2:5); a(5)=0; end b b =

1 8 2 3 5

8 2 3 5 0

2 3 5 0 0

Page 4: DSP for Begineers

3 5 0 0 0

5 0 0 0 0

Program :

function xq= quantfunction(r,x,n) l=2^n; del=r/l; l1=[0:del:r]; q=[del/2:del:r]; m=length(x); xq=[]; for i=1:m for j=1:l if x(i)>l1(j) && x(i)<=l1(j+1) x(i)=q(j); elseif x(i)>r x(i)=q(l); end end xq=[xq x(i)]; end program for convolution

x = input( 'enter input sequnc = ' );

h = input( 'enter the input impulse= ' );

n = (length(x)+length(h)-1); x1 = [ zeros(1,length(h)-1) x]; h1 = [fliplr(h) zeros(1, length(x)-1)]; h2 = zeros(); h2(1,1:n) = h1(1,1:n); for i=2:n for j=2:n h2(i,j) = h2(i-1,j-1); end end y = (h2*x1'); stem(y) program :

clc; clear all ; close all ; r=input( 'enter value of range' ); n0=input( 'enter no of bits for quantization' ); x=input( 'enter values for x(n)' ); w=input( 'enter values for w(n)' ); c=length(n0); for s=1:c

Page 5: DSP for Begineers

w1=quantfn(r,n0(s),x); x1=quqntfn(r,n0(s),x); m=length(x1); n=length(w1); x2=zeros(1,n); for i=1:(m+n-1) if i<=m x2(1)=x1(i) else x2(1)=0; end y1=x2.*w1; y(i)=0; for j=1:n y(i)=y(i)+y1(j); end x2(2:n)=x2(1:n-1); end y=quantfn(r,n0(s),y); k=conv(x,w); e=k-y; p(s)=(e*e')/n; plot(n0,p); title( 'rounding error in quantization' ); xlable( 'no of bits' ); ylable( 'mean square error' ); program :

clc; close all ; clear all ; a=[1 8 2 3 5]; b=zeros(5,5); for i=1:5 for j=i:5 b(i,j)=a(1,j-i+1); end end b b =

1 8 2 3 5

0 1 8 2 3

0 0 1 8 2

0 0 0 1 8

0 0 0 0 1

Program:

clc;

Page 6: DSP for Begineers

close all ; clear all ; a=[1 8 2 3 5]; b=[]; for i=1:5 b=[b;a]; a(1:4)=a(2:5); a(5)=0; end b b =

1 8 2 3 5

8 2 3 5 0

2 3 5 0 0

3 5 0 0 0

5 0 0 0 0

Program:

a=[1 8 2 3 5]; d=[]; for i=1:5 d=[d;a]; a=fliplr(a); end d d =

1 8 2 3 5

5 3 2 8 1

1 8 2 3 5

5 3 2 8 1

1 8 2 3 5

Program:

a=[1 8 2 3 5]; e=[]; for i=1:5

Page 7: DSP for Begineers

e=[e;a]; k=a(5); a(2:5)=a(1:4); a(1)=k; end e e =

1 8 2 3 5

5 1 8 2 3

3 5 1 8 2

2 3 5 1 8

8 2 3 5 1

[rogram:

clc; clear all ; close all ; x=input( 'enter the sequence x(n)' ); n=length(x); xk=abs(fft(x)); p=[]; q=[]; for i=1:2 if i==1 for j=1:n p1=((x(j))^2); p=[p p1]; r=sum(p); end elseif i==2 for j=1:n p2=((xk(j))^2); q=[q p2]; s1=sum(q); s=((s1)/n); end end end disp( 'energy in time domain =' ); disp(r); disp( 'power in frequency domain =' ); disp(s); disp( 'Parsevals theoram has been proved' ); enter the sequence x(n)[1 2 3 4] energy in time domain = 30

Page 8: DSP for Begineers

power in frequency domain = 30 Parsevals theoram has been proved

Program : band pass clc; clear all ; close all ; w=-pi:0.1:pi; for i=1:length(w) if ((-2.5<w(i)&&w(i)<-1.2))||((1.2<w(i)&&w(i)<2.5)) y(i)=abs(exp(-j*w(i))); else y(i)=0; end end plot(w,y, 'color' , 'r' ); figure stem(w,y, 'color' , 'g' ); program :butter clc; clear all ; close all ; f=input( 'enter the value of maximun frequency range' ); fc=input( 'enter the value of cut off frequency' ); n=input( 'enter the value of order' ); y=[]; for k=1:n for i=0:f a=(1+((i/fc)^(2*k))); b=(1/((a)^0.5)); y=[y b]; end plot(y); hold on; y=[]; end

program :chebysev 2 clc; clear all ; close all ; fp=1000; fs=2000; e=0.5 f=[0:4000]; n=[ 3 4 ]; x=[]; y=[]; mag2=[]; for i=1:2 a=fs/fp; if abs(a)<=1 tn=cos(n(i)*acos(a));

Page 9: DSP for Begineers

else tn=cosh(n(i)*acosh(a)); end x=[x tn]; for j=1:length(f) b=fs/f(j); if abs(b)<=1 tn=cos(n(i)*acos(b)); else tn=cosh(n(i)*acosh(b)); end y=[y tn]; mag=1/(1+(e^2*(x/y(j))^2)); mag2=[mag2 mag]; end plot(f,mag2) hold on; mag2=[]; y=[];x=[]; end

program :chebysev 1 clc; clear all ; close all ; fp=1000; fs=2000; e=0.5 f=[0:4000]; n=[ 7 12 ]; x=[]; mag2=[]; for i=1:2 for j=1:length(f) a=f(j)/fp; if abs(a)<=1 tn=cos(n(i)*acos(a)); else tn=cosh(n(i)*acosh(a)); end x=[x tn]; mag=(1/(1+(e^2)*(x(j)^2))); mag2=[mag2 mag]; end subplot(1,2,i); plot(f,mag2); mag2=[]; x=[]; end

program :chebysev 1 clc; clear all ;

Page 10: DSP for Begineers

close all ; f=input( 'enter the value of maximun frequency range' ); fp=input( 'enter the value of pass band cut off frequency' ); n=input( 'enter the value of order' ); y=[]; for i=0:f x=(i/fp); if ((-1<x)&&(x<1)) q=cos(n*(acos(x))); a=(1/((1+(0.25*((q)^2)))^0.5)); else q=cosh(n*(acosh(x))); a=(1/((1+(0.25*((q)^2)))^0.5)); end y=[y a]; end plot(y)

program :chebysev 1 clc; clear all ; close all ; f=input( 'enter the value of maximun frequency range' ); fp=input( 'enter the value of pass band cut off frequency' ); fs=input( 'enter the value of stop band cut off frequency' ); n=input( 'enter the value of order' ); r=fs/fp; y=[]; for i=0:f x=(fs/i); if ((-1<x)&&(x<1)) if ((-1<r)&&(r<1)) p=cos(n*(acos(r))); else p=cosh(n*(acosh(r))); end q=cos(n*(acos(x))); a=(1/((1+(0.25*((p/q)^2)))^0.5)); else if ((-1<r)&&(r<1)) p=cos(n*(acos(r))); else p=cosh(n*(acosh(r))); end q=cosh(n*(acosh(x))); a=(1/((1+(0.25*((p/q)^2)))^0.5)); end y=[y a]; end plot(y)

program :convolution clc; clear all ; close all ;

Page 11: DSP for Begineers

x=input( 'enter the sequence of x(n)' ); h=input( 'enter the sequence of h(n)' ); a=length(x); b=length(h); c=a+b-1; hf=fliplr(h); hh=[zeros(1,a-1) hf zeros(1,a-1)]; yf=[]; for i= 1:c w(1:a)=h((2*a)-i:(3*a)-1-i); y=w*x'; yf=[yf y]; end disp( 'the linear convolution of x(n) and h(n) is ' ) disp(yf)

program :correlation clc; clear all ; close all ; x=input( 'enter first sequence' ); w=input( 'enter second sequence' ); m=length(x); n=length(w); y=[]; x=[x zeros(1,n-1)]; z= zeros(1,n); wf=fliplr(w); for i=1:m+n-1 z(2:n)=z(1:n-1); z(1)=x(i); p=z*wf'; y=[y p]; end disp( 'the corelation of x and w is ' ); disp( 'y' ); y; subplot(311); stem(x) subplot(312); stem(w) subplot(313); stem(y)

program :dft clc; clear all ; close all ; x=input( 'enter first sequence' ); y=input( 'enter second sequence' ); m=length(x); n=length(y); if m>n y=[y zeros(m-n)];

Page 12: DSP for Begineers

elseif n>m x=[x zeros(n-m)]; end a=fft(x); b=fft(y); c=a*b; d=conv(x,y); % c; % d=ifft(c); % d c,d

program :hpf clc;clear all ;close all ; w=-pi:0.1:pi; wc=input( 'cut off freq=' ); for i=1:length(w) if (-pi<w(i)&&w(i)<-wc||w(i)>wc&&w(i)<pi) H(i)=1; else H(i)=0; end end plot(w,H, 'color' , 'r' ); figure stem(w,H, 'color' , 'r' );

program: pi clc;clear all ;close all ; x=rand(10000,1000); y=rand(10000,1000); r=sqrt(x.^2+y.^2); a=0; b=0; for i=1:10000 for j=1:1000 if r(i,j)>=0 && r(i,j)<=1 a=a+1; elseif r(i,j)>1 b=b+1; end end end pi=4*a/(a+b); pi program : quanti function xq= quanti(r,x,n1) l=2^n1; del=r/l; l1=[0:del:r]; q=[del/2:del:r]; m=length(x); xq=[]; for i=1:m for j=1:l

Page 13: DSP for Begineers

if x(i)>l1(j) && x(i)<=l1(j+1) x(i)=q(j); elseif x(i)>r x(i)=q(l); end end xq=[xq x(i)]; end

program : clc; close all ; clear all ; rp=input( 'Enter the passband ripple' ); rs=input( 'Enter the stopband ripple' ); fp=input( 'Enter the passband frequency' ); fs=input( 'Enter the stopband frequency' ); f=input( 'Enter the sampling frequency' ); wp=2*(fp/f); ws=2*(fs/f); num=-20*log10(sqrt(rp*rs))-13; den=14.6*(fs-fp)/f; n=ceil(num/den); n1=n+1; if (rem(n,2)~=0) n1=n; n=n-1; end y=rectwin(n1); %%%%%%%%%%%%%%%LPF b=fir1(n,wp,y); [h,o]=freqz(b,1,256); m=20*log10(abs(h)); subplot(2,2,1); plot(o/pi,m); ylabel( 'gain in db--------->' ); xlabel( 'Normalised frequency-------------->' ); %%%%%%%%%%%%%%HPF b=fir1(n,wp, 'high' ,y); [h,o]=freqz(b,1,256); m=20*log10(abs(h)); subplot(2,2,2); plot(o/pi,m); ylabel( 'gain in db--------->' ); xlabel( 'Normalised frequency-------------->' ); %%%%%%%%%%%BPF wn =[wp ws] b=fir1(n,wn,y); [h,o]=freqz(b,1,256); m=20*log10(abs(h)); subplot(2,2,3);

Page 14: DSP for Begineers

plot(o/pi,m); ylabel( 'gain in db--------->' ); xlabel( 'Normalised frequency-------------->' ); %%%%%%%%%%%BSF b=fir1(n,wn, 'stop' ,y); [h,o]=freqz(b,1,256); m=20*log10(abs(h)); subplot(2,2,4); plot(o/pi,m); ylabel( 'gain in db--------->' ); xlabel( 'Normalised frequency-------------->' );