You are on page 1of 6

% Chapter 12 MATLAB code in sequence Note: this code requires the use of the custom M-files contained on this

CD. Make sure the custom M-files are placed in the MATLAB ork! director". To execute the following code, copy and paste it into the MATLAB command window. Note that some sections of code require the execution of prior sections. Each code section as it appears in the text is delimited y a !"# line. " """"""""""""""""""""""""""""""""""""" " " Tone Generation " $$ f%&'()* $$ f)&%)+,* $$ fs&'+++* $$ L&sin-).pi.f%/fs0* $$ 1&sin-).pi.f)/fs0* $$ aL&).cos-).pi.f%/fs0* $$ a1&).cos-).pi.f)/fs0* $$ &2+, L3 1,4-aL. 13a1. L0, L3 15* $$ a&2%,4-aL3a10,aL.a13),4-aL3a10,%5* " $$ x&2%,6eros-%,)+++05* "This is an impulse signal to start the oscillator $$ y&filter- ,a,x0* $$ dtft7demof-y,+,%8++,%+)8,'+++0* "The sampling frequency is ' 916 " " Rejection of Dial-Tone " $$ fs&'+++* " The system sampling frequency is ' 16 $$ f:(+&).pi.:(+/fs* " ;ompute the digital frequencies for each notch $$ f88+&).pi.88+/fs* $$ deltaf&).pi.(/fs* " Let the 4: dB width of each notch e ( 16 $$ r&%4deltaf/)* " ;ompute the pole radius $$ g:(+&a s-%4).r.cos-f:(+03r<)0/-).a s-%4cos-f:(+000* " ;ompute the !g# factors $$ g88+&a s-%4).r.cos-f88+03r<)0/-).a s-%4cos-f88+000* $$ :(+&g:(+.2%,4).cos-f:(+0,%5* " ;ompute the and a coefficients for each filter $$ 88+&g88+.2%,4).cos-f88+0,%5* $$ a:(+&2%,4).r.cos-f:(+0,r<)5* $$ a88+&2%,4).r.cos-f88+0,r<)5* $$ )&con=- :(+, 88+0* " ;on=ol=e the coefficient =ectors $$ a)&con=-a:(+,a88+0* $$ f=tool- ),a)0 " $$ x&analog-2:(+,88+,>,?,%)+,5,2%,%,%,%5,%+++,'+++0* $$ y&filter- ),a),x0*

) $$ su plot-),%,%0,dtft7demof-x,+,%8++,)+8','+++0* title-@Aigit % with Aial4Tone@0 $$ su plot-),%,)0,dtft7demof-y,+,%8++,)+8','+++0* title-@Aigit % with Aial4Tone Bemo=ed@0 " " Esti ation of !requenc" Content #sin$ the Goert%el Al$orith " $$ n&%C)+* $$ f%&).pi.)(+/)+++* $$ f)&).pi.(++/)+++* $$ x&sin-f%.n03sin-f).n0* $$ dtft7demo-x,+,).pi,(%)0* " ;ustom M4file for the ATDT $$ hold $$ 2E,omegax5&dft7demo-x0* " ;ustom M4file for the ADT $$ stem-omegax/pi,a s-E0, @9@0* $$ title-@)(+ and (++ 16, ATDT -;ur=e0 and ADT -Ftem0@0 $$ hold off " $$ dtft7demo-x,+,).pi,(%)0* $$ hold $$ stem-2omegax-80,omegax->05/pi,2a s-E-800,a s-E->005,@9@0 $$ title-@ATDT -;ur=e0 and Goert6el Besults -Ftem0@0 " $$ Eg&goert6el-x,28,>50* " The frequency index =alues are 8 and > $$ a s-Eg0 " Dind the magnitude of the ADT =alues "ans & " (.,)>% %+.+8,, " " MATLAB E ulation of the Goert%el !ilter Ban& " $$ f&2>,?,??+,'(),,8%,%)+,,%::>,%8??5* $$ g&goert6el7=ector-%:>,'+++,f0 "g & "%: %8 %( %? )) )8 )> " $$ n&+C%:(* $$ f%&).pi.>,?/'+++* $$ f)&).pi.%)+,/'+++* $$ tone%&sin-f%.n03sin-f).n0* " $$ E%&goert6el-tone%,g0* $$ a s-E%0* $$ stem-f,a s-E%00 $$ xla el-@16@0 $$ title-@Goert6el Algorithm Hutput for the Tone I%I ->,? and %)+, 160@0 "

: $$ n&+C%:(* $$ f%&).pi.,8%/'+++* $$ f)&).pi.%::>/'+++* $$ tone+&sin-f%.n03sin-f).n0* $$ E+&goert6el-tone+,g0* $$ stem-f,a s-E+00 $$ xla el-@16@0 $$ title-@Goert6el Algorithm Hutput for the Tone I+I -,8% and %::> 160@0 " $$ n&+C%:(* $$ f%off&).pi.%++%/'+++* $$ f)off&).pi.%:,>/'+++* $$ tone+off&sin-f%off.n03sin-f)off.n0* $$ E+off&goert6el-tone+off,g0* $$ stem-f,a s-E+off00 $$ xla el-@16@0 $$ title-@Goert6el Hutput for I+I Tones Hffset y 3>+ 16@0 " " Adapti'e !ilter Deter ination of #n&no(n )h"sical * pedances " $$ Bm&%++* $$ Bx&(+* $$ E&4%)(* $$ DJ&-Bx3K.E0/-Bm3Bx3K.E0* " This is the complex response of the physical " impedances $$ amp&a s-DJ0 " This gi=es the amplitude response "amp & " +.>',( $$ deg&angle-DJ0.%'+/pi " This gi=es the phase response in degrees "deg & "4)'.:,:+ " $$ n&+C88+,,* $$ omega&).pi.)++/88%++* "The digital frequency for )++ 16 at the "sampling rate $$ x&sin-omega.n0* " This is % second of the sampled excitation signal $$ y&amp.sin-omega.n3angle-DJ00* "This is the digital output signal with an " amplitude and phase that would e " produced y the un9nown system " response " $$ 2err,n7hat,w5&adapt)->,x,y0* "Bun the adapti=e filter routine $$ plot-err0* title-@Error =s. Famples@0* xla el-@Fample@0 $$ &w@* "Lut the estimated impulse response into a row =ector $$ 6&exp-K.omega0* "The complex unit circle =alue of the input frequency $$ 1&17e=aluate- ,%,60* "Mse the custom M4file 17e=aluate to compute the

8 " response of the system at the input frequency " ;ompute the real and imaginary parts of the un9nown " impedance

$$ Bm.1/-%410

"ans & "(.+++)e3++% 4%.)(++e3++)i " The estimate of the un9nown impedance Bx " 3KE " $$ 2err,n7hat,w5&adapt)-%+,x,y0* $$ plot-err0* title-@Error =s. Famples@0* xla el-@Fample@0 " " Transfor Co pression of * a$es " $$ tri78&triangle-80* $$ tri7%+&triangle-%+0* $$ tri7%++&triangle-%++0* $$ ms&+C.+%C8+* $$ su plot-%,:,%0,plot-ms,tri780,title-@84Term Triangle@0 $$ su plot-%,:,)0,plot-ms,tri7%+0,title-@%+4Term Triangle@0 $$ su plot-%,:,:0,plot-ms,tri7%++0,title-@%++4Term Triangle@0 " $$ err&tri7%+4tri7%++* $$ mean-err.<)0 "ans & ">.''8(e4++> " " Discrete Cosine Transfor of * a$es " $$ n&+C)+* $$ f&pi/8* $$ x&sin-f.n0* $$ yf&fft-x0* $$ yc&dct-x0* $$ omegaf&+C).pi/)+C).pi* $$ omegac&+Cpi/)+Cpi* $$ su plot-%,),%0,stem-omegaf/pi,a s-yf00,title-@Aiscrete Dourier Transform@0 $$ xla el-@units of pi@0 $$ su plot-%,),)0,stem-omegac/pi,yc0,title-@Aiscrete ;osine Transform@0 $$ xla el-@units of pi@0 " " + all Bloc& E,a ple of the -)EG Method " $$ N&imread-@pout.tif@0* $$ si6e-N0 $$ loc9&N-%+%C%+',%+%C%+'0 $$ )&im)dou le- loc90 " ;on=ert the matrix =alues to storage class !dou le# "

( $$ :& )4.( " Fu tract +.( from each matrix =alue to create 3/4 % pixel =alues " $$ c%&dct)- :0 " $$ mas9&mas9'-80 " This is a custom M4file for mas9 generation. " Nt will select a triangle4shaped portion of the low index " =alues of the A;T matrix. " $$ c)&c%..mas9 " Multiply the A;T matrix y the mas9 element4 y4element " $$ r%&idct)-c)0 " $$ r)&r%3.( " $$ )4r) " $$ mean)--- )4r)0./ )0.%++0 " " Codin$ the Mas&ed Bloc&s for Co pressed +tora$e " $$ c) $$ c)=ector&66-c)0* $$ length-c)=ector0 $$ c)=ector-%C%+0@ " $$ de66-c)=ector0 " " E,a ples of -)EG Co pression and Reconstruction " $$ mas9&mas9'-)0* $$ Kpeg7demo-N,mas90* $$ N&imread-@moon.tif@0* $$ mas9&mas9'-80* $$ Kpeg7demo-N,mas90* " $$ dctdemo " " -)EG Co pression of a !in$erprint * a$e " $$ load detfingr.mat $$ whos " $$ DL w&E/-max-max-E000* $$ imshow-DL w0,title-@Mncompressed B/O Nntensity Dingerprint Nmage@0 " $$ mas97)&mas9'-)0* "Fet a A;T mas9 to )%4to4% compression

> $$ mas97:&mas9'-:0* "Fet a A;T mas9 to %%4to4% compression $$ DLr7%%&Kpeg7demo-DL w,mas97:0* "Generate a reconstructed image %%4tto4% $$ close ";lose the figure created y Kpeg7demo $$ DLr7)%&Kpeg7demo-DL w,mas97)0* " Generate a reconstructed image )%4to4% $$ close ";lose the figure created y Kpeg7demo $$ imshow-DLr7%%0,title-@Beconstructed PLEG %%4to4% ;ompression@0 $$ figure,imshow-DLr7)%0,title-@Beconstructed PLEG )%4to4% ;ompression@0 " " .a'elet Co pression of !in$erprint * a$es " $$ load detfingr.mat* $$ map& one-%)'0* $$ imshow-E/max-max-E000, colormap-map0* " Aisplay an intensity image with map $$ title-@Mncompressed Dingerprint Nmage@0 " $$ wa=&@ ior8.8@* "Fet the wa=elet family to e used $$ le=el&:* " Fet the decomposition le=el $$ sorh&@h@* " Fet the thresholding method to !hard# $$ 9eepapp&%* " Ao not threshold the approximation coefficients $$ thr&8+* " Fet the thresholding le=el and use glo ally -Qg lR0 $$ 2E;,;E;,LE;,LEBD+,LEBDL)5& wdencmp-@g l@,E,wa=,le=el,thr,sorh,9eepapp0* $$ imshow-E;/max-max-E;000,colormap- one-%)'00* $$ title-@)+4to4% ;ompression, ior8.8 Oa=elet, Threshold & 8+@0 $$ LEBD+ "LEBD+ & ",8.'>,, " $$ load detfingr.mat* " Load the image $$ wa=&@haar@* " Fet the wa=elet family $$ le=el&:* " Fet the decomposition le=el $$ thresh&8+* " Fet the thresholding for compression $$ 2E;,cmp7ratio,energy,thr5&Kpeg)+++7demo-E,wa=,le=el,thresh0* $$ close " ;lose the figure created y Kpeg)+++7demo $$ imshow-E;/max-max-E;000,colormap- one-%)'00* "Aisplay intensity image $$ title-@%,4to4% ;ompression, 1aar Oa=elet, Threshold &8+@0 $$ cmp7ratio " ;ompression ratio is %,4to4% for this example "cmp7ratio & "%, " """"""""""""""""""""""""""""""""

You might also like