Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 478 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
478
Dung lượng
2,81 MB
Nội dung
DIGITAL SIGNAL PROCESSING ® Using MATLAB and Wavelets Michael Weeks Designed for upper division engineering and computer science students as well as practicing engineers, Digital Signal Processing Using MATLAB and Wavelets emphasizes the practical applications of signal processing Over 100 MATLAB examples and wavelet techniques provide the latest applications of DSP, including image processing, games, filters, transforms, networking, parallel processing, and sound The book also provides the mathematical processes and techniques needed to ensure an understanding of DSP theory Designed to be incremental in difficulty, the book will benefit readers who are unfamiliar with complex mathematical topics or those limited in programming experience Beginning with an introduction to MATLAB programming, it moves through filters, sinusoids, sampling, the Fourier transform, the z-transform and other key topics An entire chapter is dedicated to the discussion of wavelets and their applications A CD-ROM (platform independent) accompanies the book and contains source code, projects for each chapter, and the figures contained in the book FEATURES: ■ Contains over 100 short examples in MATLAB used throughout the book ■ Includes an entire chapter on the wavelet transform ■ Designed for the reader who does not have extensive math and programming experience ■ Accompanied by a CD-ROM containing MATLAB examples, source code, projects, and figures from the book ■ Contains modern applications of DSP and MATLAB project ideas BRIEF TABLE OF CONTENTS: Introduction MATLAB Filters Sinusoids Sampling The Fourier Transform The Number e The z-Transform The Wavelet Transform 10 Applications Appendix A Constants and Variables B Equations C DSP Project Ideas D About the CD Answers Glossary Index ABOUT THE AUTHOR: Shelving: Engineering / Computer Science Level: Intermediate to Advanced ISBN: 0-9778582-0-0 U.S $69.95 / Canada $85.50 INFINITY SCIENCE PRESS WEEKS All trademarks and service marks are the property of their respective owners Cover design: Tyler Creative weeks_DSP.indd Using MATLAB and Wavelets ® Michael Weeks ® 11 Leavitt Street Hingham, MA 02043 (781) 740-4487 (781) 740-1677 FAX info@infinitysciencepress.com www.infinitysciencepress.com DIGITAL SIGNAL PROCESSING Using MATLAB and Wavelets Michael Weeks is an associate professor at Georgia State University where he teaches courses in Digital Signal Processing He holds a PhD in computer engineering from the University of Louisiana at Lafayette and has authored or co-authored numerous journal and conference papers DIGITAL SIGNAL PROCESSING Although DSP has long been considered an EE topic, recent developments have also generated significant interest from the computer science community DSP applications in the consumer market, such as bioinformatics, the MP3 audio format, and MPEG-based cable/satellite television have fueled a desire to understand this technology outside of hardware circles E L E C T R I C A L EN G I N E E R I N G SE R I E S 8/11/06 1:15:29 PM Digital Signal Processing Using MATLAB
r and Wavelets License, Disclaimer of Liability, and Limited Warranty The CD-ROM that accompanies this book may only be used on a single PC This license does not permit its use on the Internet or on a network (of any kind) By purchasing or using this book/CD-ROM package (the “Work”), you agree that this license grants permission to use the products contained herein, but does not give you the right of ownership to any of the textual content in the book or ownership to any of the information or products contained on the CD-ROM Use of third party software contained herein is limited to and subject to licensing terms for the respective products, and permission must be obtained from the publisher or the owner of the software in order to reproduce or network any portion of the textual material or software (in any media) that is contained in the Work Infinity Science Press LLC (“ISP” or “the Publisher”) and anyone involved in the creation, writing or production of the accompanying algorithms, code, or computer programs (“the software”) or any of the third party software contained on the CD-ROM or any of the textual material in the book, cannot and not warrant the performance or results that might be obtained by using the software or contents of the book The authors, developers, and the publisher have used their best efforts to insure the accuracy and functionality of the textual material and programs contained in this package; we, however, make no warranty of any kind, express or implied, regarding the performance of these contents or programs The Work is sold “as is” without warranty (except for defective materials used in manufacturing the disc or due to faulty workmanship); The authors, developers, and the publisher of any third party software, and anyone involved in the composition, production, and manufacturing of this work will not be liable for damages of any kind arising out of the use of (or the inability to use) the algorithms, source code, computer programs, or textual material contained in this publication This includes, but is not limited to, loss of revenue or profit, or other incidental, physical, or consequential damages arising out of the use of this Work The sole remedy in the event of a claim of any kind is expressly limited to replacement of the book and/or the CD-ROM, and only at the discretion of the Publisher The use of “implied warranty” and certain “exclusions” vary from state to state, and might not apply to the purchaser of this product Digital Signal Processing Using MATLAB
r and Wavelets Michael Weeks Georgia State University Infinity Science Press LLC Hingham, Massachusetts Copyright 2007 by Infinity Science Press LLC All rights reserved This publication, portions of it, or any accompanying software may not be reproduced in any way, stored in a retrieval system of any type, or transmitted by any means or media, electronic or mechanical, including, but not limited to, photocopy, recording, Internet postings or scanning, without prior permission in writing from the publisher Publisher: David F Pallai Infinity Science Press LLC 11 Leavitt Street Hingham, MA 02043 Tel 877-266-5796 (toll free) Fax 781-740-1677 info@infinitysciencepress.com www.infinitysciencepress.com This book is printed on acid-free paper Michael Weeks Digital Signal Processing Using MATLAB and Wavelets ISBN: 0-9778582-0-0 The publisher recognizes and respects all marks used by companies, manufacturers, and developers as a means to distinguish their products All brand names and product names mentioned in this book are trademarks or service marks of their respective companies Any omission or misuse (of any kind) of service marks or trademarks, etc is not an attempt to infringe on the property of others Library of Congress Cataloging-in-Publication Data Weeks, Michael Digital signal processing using MATLAB and Wavelets / Michael Weeks p cm Includes index ISBN 0-9778582-0-0 (hardcover with cd-rom : alk paper) Signal processing–Digital techniques–Mathematics MATLAB Title TK5102.9.W433 2006 621.382’2–dc22 2006021318 Wavelets (Mathematics) I 678954321 Our titles are available for adoption, license or bulk purchase by institutions, corporations, etc For additional information, please contact the Customer Service Dept at 877-266-5796 (toll free) Requests for replacement of a defective CD-ROM must be accompanied by the original disc, your mailing address, telephone number, date of purchase and purchase price Please state the nature of the problem, and send the information to Infinity Science Press, 11 Leavitt Street, Hingham, MA 02043 The sole obligation of Infinity Science Press to the purchaser is to replace the disc, based on defective materials or faulty workmanship, but not based on the operation or functionality of the product I dedicate this book to my wife Sophie Je t’aimerai pour toujours Contents Preface xxi Introduction 1.1 Numbers 1.1.1 Why Do We Use a Base 10 Number System? 1.1.2 Why Do Computers Use Binary? 1.1.3 Why Do Programmers Sometimes Use Base 16 (Hexadecimal)? 1.1.4 Other Number Concepts 1.1.5 Complex Numbers 1.2 What Is a Signal? 1.3 Analog Versus Digital 1.4 What Is a System? 1.5 What Is a Transform? 1.6 Why Do We Study Sinusoids? 1.7 Sinusoids and Frequency Plots 1.8 Summations 1.9 Summary 1.10 Review Questions MATLAB 2.1 Working with Variables 2.2 Getting Help and Writing Comments 2.3 MATLAB Programming Basics 2.3.1 Scalars, Vectors, and Matrices 2.3.2 Number Ranges 2.3.3 Output 2.3.4 Conditional Statements (if) 2.3.5 Loops vii 1 2 10 14 19 20 22 24 26 27 27 29 30 31 32 33 35 35 36 39 viii DSP Using MATLAB and Wavelets 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12 2.13 2.14 2.15 2.3.6 Continuing a Line Arithmetic Examples Functions How NOT to Plot a Sinusoid Plotting a Sinusoid Plotting Sinusoids a Little at a Time Calculating Error Sometimes Is Not Exactly 2.10.1 Comparing Numbers with a Tolerance 2.10.2 Rounding and Truncating MATLAB Programming Tips MATLAB Programming Exercises Other Useful MATLAB Commands Summary Review Questions Filters 3.1 Parts of a Filter 3.2 FIR Filter Structures 3.3 Causality, Linearity, and Time-Invariance 3.4 Multiply Accumulate Cells 3.5 Frequency Response of Filters 3.6 IIR Filters 3.7 Trends of a Simple IIR Filter 3.8 Correlation 3.9 Summary 3.10 Review Questions Sinusoids 4.1 Review of Geometry and Trigonometry 4.2 The Number π 4.3 Unit Circles 4.4 Principal Value of the Phase Shift 4.5 Amplitudes 4.6 Harmonic Signals 4.7 Representing a Digital Signal as a Sum of Sinusoids 4.8 Spectrum 4.9 Summary 39 39 52 53 56 60 63 64 65 69 70 71 81 83 83 85 89 91 98 103 104 111 113 115 128 130 133 133 134 136 138 139 140 145 152 156 ix Contents 4.10 Review Questions 156 Sampling 5.1 Sampling 5.2 Reconstruction 5.3 Sampling and High-Frequency Noise 5.4 Aliasing 5.4.1 Aliasing Example 5.4.2 Folding 5.4.3 Locations of Replications After Sampling 5.5 Nyquist Rate 5.6 Bandpass Sampling 5.7 Summary 5.8 Review Questions The 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 6.10 6.11 6.12 Fourier Transform Fast Fourier Transform Versus the Discrete Fourier The Discrete Fourier Transform Plotting the Spectrum Zero Padding DFT Shifting Theory The Inverse Discrete Fourier Transform Forward and Inverse DFT Leakage Harmonics and Fourier Transform Sampling Frequency and the Spectrum Summary Review Questions The Number e 7.1 Reviewing Complex Numbers 7.2 Some Interesting Properties of j 7.2.1 Rotating Counterclockwise 7.2.2 Rotating Clockwise √ 7.2.3 Removing j from −a 7.3 Where Does e Come from? 7.4 Euler’s Formula 7.5 Alternate Form of Euler’s Equation 7.6 Euler’s Inverse Formula 7.7 Manipulating Vectors 159 160 162 162 164 165 168 171 175 176 182 183 Transform 187 190 191 196 202 203 204 207 212 214 219 221 221 225 225 228 228 229 230 230 233 235 236 238 Answers to Selected Review Questions 423 3.13 a Examining the indices of the inputs versus the output, we see that the output’s index is as large (or larger) than all the indices used for the inputs Therefore, we conclude that this system is causal b This system is linear c This system is time-invariant Chapter 4.1 The cos function has a maximum of +1 and a minimum of −1 Therefore, the minimum and maximum values for x(t) are and −3, respectively 4.3 a >> >> >> >> >> >> >> >> >> >> t = 0:0.000001:0.001; x1 = 3*cos(2*pi*2000*t + pi/4); x2 = 2*cos(2*pi*5000*t); x3 = cos(2*pi*11000*t - pi/7); subplot(3,1,1); plot(t, x1); title('3cos(2\pi 2000 t + \pi/4)') subplot(3,1,2); plot(t, x2); title('2cos(2\pi 5000 t)') subplot(3,1,3); plot(t, x3); title('cos(2\pi 11000 t - \pi/7)') b >> t = 0:0.000001:0.001; >> x = 3*cos(2*pi*2000*t + pi/4) + 2*cos(2*pi*5000*t) + cos(2*pi*11000*t - pi/7); >> plot(t, x) c The greatest common value between 2000, 5000, and 11,000 is 1000, thus f0 = 1000 Hz So we can represent x as: x(t) = cos(2π2(f0 )t + π/4) + cos(2π5(f0 )t) + cos(2π11(f0 )t − π/7) For amplitudes, we know that a2 = 3, a5 = 2, and a11 = All other amplitudes are For the phase angles, we see that φ = π/4, φ5 = 0, and φ11 = −π/7 All other phase angles are 424 DSP Using MATLAB and Wavelets 4.5 Amplitude is a quantity without units, could be positive or negative Magnitude is a quantity without units, always positive When we have a function between vertical bars, it means to find the magnitude It is implemented in MATLAB as the abs function; the absolute value 4.7 Spectrum 4.9 Technically, amplitudes can be positive or negative Practically, a negative amplitude does not change the signal, except to shift it in time We can change the phase angle to get the same effect Thus, we not need negative amplitudes Chapter 5.1 1/0.008 = 125 Hz 5.3 Undersampling occurs when we not take samples often enough We not want to this, since we will not be able to faithfully recreate the sampled signal 5.5 Converting a digital signal to an analog one, so named because the signal was typically analog to begin with 5.7 Ideally, yes, the output should exactly match the input But there are practical things to consider that would make the two signals different The circuitry making up the ADC and DAC may not be fast enough to handle all possible analog signal values 5.9 The frequency 800 Hz is clearly greater than f s (500 samples/sec), so it will have an alias at (800 - fs ) = 300 Hz Note that the 300 Hz frequency component will have a replica at −300 Hz, which has a replica at fs + (−300) = 200 Hz 5.11 B (bandwidth) is between 18 and 24 kHz B = 24-18 = kHz fc (center frequency) = (18+24)/2 = 21 kHz m = number of replicas = 2B = 12 kHz (Nyquist), so fs in the range of 16.67 to 18 kHz is OK 5.13 2B = 10 MHz (Nyquist), so fs in the range of 15 to 20 MHz is OK Also, f s = 10 MHz is OK Anything beyond m = will not work Answers to Selected Review Questions 425 5.15 a x[n] = cos(2πn0.2 + π/4) + cos(2πn0.5) + cos(2πn1.1 − π/7) b This can be done in MATLAB n = 1:100; x = 3*cos(2*pi*n*0.2 + pi/4) + 2*cos(2*pi*n*0.5) + cos(2*pi*n*1.1 - pi/7); plot(n, x) c The kHz frequency has aliases every ±10 kHz, i.e., at -20 kHz, -10 kHz, -5 kHz, 15 kHz, 25 kHz, etc Each of these also appears mirrored around Hz, i.e., +20 kHz, +10 kHz, +5 kHz, -15 kHz, -25 kHz, etc d This is times the bandwidth Bandwidth = 11, 000 − 2000 = 9000 Hz, so the critical Nyquist frequency is 18,000 Hz The sampling rate must be at least 18 kHz e Bandpass sampling uses a sampling frequency at least 2B, and locates a replica between the original frequency content and Hz The problem here is that a replica cannot fit; there is no way a kHz band will fit between and kHz Since there will be overlap between the original frequency content and the replica, we should not use bandpass sampling here 5.17 a t = 0:0.00001:0.001; x1 = 6*cos(2*pi*7000*t + pi/2); x2 = 4*cos(2*pi*8000*t); x3 = 2*cos(2*pi*6000*t); x1_n = 6*cos(-2*pi*7000*t - pi/2); x2_n = 4*cos(-2*pi*8000*t); x3_n = 2*cos(-2*pi*6000*t); subplot(3,1,1); plot(t, x1, 'b', t, x1_n, 'r') subplot(3,1,2); plot(t, x2, 'b', t, x2_n, 'r') subplot(3,1,3); plot(t, x3, 'b', t, x3_n, 'r') 426 DSP Using MATLAB and Wavelets b The signals x1 and x1_n are the same, as are x2 and x2_n, and x3 and x3_n Therefore, the cosine function returns the same value whether or not the argument is negated c No, the sine function does not return the same value for a negated argument Actually, it returns a negated value for a negated argument, i.e., sin(−θ) = − sin(θ) This comes from the definitions of sin and cos, i.e., on a graph, x is positive whether the angle is π/2 radians or −π/2 radians 5.19 Xsize = 10; Ts = 1/8000; % Make our X signal n=0:Xsize-1; x(n+1) = cos(2*pi*1000*n*Ts)/2 + cos(2*pi*2000*n*Ts + pi/4); sprintf('The first %d samples are : ',Xsize) x 5.21 a Yes, it does The 100 kHz noise will have a replica at 100 − 44 − 44 = 22 kHz The 102 kHz noise will have a replica at 102 − 44 − 44 − 44 = −30 kHz, which will also appear at −30 + 44 = 14 kHz b The sampling rate must be at least twice the Bandwidth This sampling rate violates the Nyquist criterion Chapter 6.1 We have a DC component (0 Hz) of 10 units, and at analysis frequencies: f (3) = 3*5000/250 = 60 Hz f (5) = 5*5000/250 = 100 Hz f (9) = 9*5000/250 = 180 Hz we have sinusoids of magnitudes 4.47, 1, and 4.47, respectively (there are no units for these numbers) These sinusoids have phases of 1.107 radians (63.4 degrees), 0, and −1.107 radians (−63.4 degrees), respectively 6.3 a There are 10 samples total We have a DC component (0 Hz) of units b At analysis frequencies: f (1) = 1*100/10 = 10 Hz f (2) = 2*100/10 = 20 Hz f (3) = 3*100/10 = 30 Hz Amount: sqrt(20) units = 4.5 units Amount: units Amount: sqrt(34) units = 5.8 units Answers to Selected Review Questions f (4) = 4*100/10 = 40 Hz Amount: units f (5) = 5*100/10 = 50 Hz Amount: units (We will stop here.) The frequency components rank in this order (largest to smallest): 30 Hz, 10 Hz, and 20 Hz c >> X = [3, 2+4j, 1, 5-3j, 0, 0, 0, 5+3j, 1, 2-4j]; >> x = ifft(X); >> disp(x(1:5)) 1.9000 0.4768 -1.6607 -0.2899 0.4476 >> disp(x(6:10)) -0.9000 0.2468 6.5 1.9371 x = round(rand(1, 20)*100); h1 = [0.5, 0.5, 0.5, 0.5, 0.5]; h2 = [0.1, 0.3, 0.5, 0.3, 0.1]; h3 = [0.9, 0.7, 0.5, 0.7, 0.9]; out1 = conv(x, h1); out2 = conv(x, h2); out3 = conv(x, h3); subplot(3,1,1); plot(out1); title('output 1'); subplot(3,1,2); plot(out2); title('output 2'); subplot(3,1,3); plot(out3); xlabel('filter outputs'); title('output 3'); figure(2); Out1 = fft(out1); Out2 = fft(out2); Out3 = fft(out3); subplot(3,1,1); plot(abs(Out1)); title('FMR of output 1'); subplot(3,1,2); plot(abs(Out2)); title('FMR of output 2'); subplot(3,1,3); plot(abs(Out3)); 0.5663 0.2760 427 428 DSP Using MATLAB and Wavelets title('FMR of output 3'); The second filter produces a smoother version of the original, while still retaining the original’s characteristics The coefficients h [k] have the smoothest transition (with 0.1 at each end) The filter coefficients in h [k] have the most transition (first and last values), and are visibly the worst of the three 6.7 3e−j2π0.2 = 0.9270 − j2.8533 Chapter 7.1 3.6055 e0.9828j 7.3 6ejφ = 6(cos(2π) + j sin(2π)) 6ejφ = 6(1 + j0) = Since the complex part is zero, this vector reduces to a real number 7.5 cos(2π100t + 3π/8) + j2 sin(2π100t + 3π/8) 7.7 ω tells us the speed of rotation, and direction If ω is positive, it rotates counterclockwise If is it negative, then it rotates clockwise Yes, we CAN have a positive frequency or a negative frequency φ gives the initial position, i.e., where the vector is at time 7.9 e−jφ 7.11 We observe that the vector is rotated counterclockwise by 90 degrees each time we multiply by j The four vectors are: + 5j 4j + 5(−1) = −5 + 4j −5j + 4(−1) = −4 − 5j −4j − 5(−1) = − 4j Answers to Selected Review Questions 429 The angles (in degrees) are: >> v = [4 + 5j, -5 + 4j, -4 - 5j, - 4j]; >> disp(angle(v)*360/(2*pi)) 51.3402 141.3402 -128.6598 -38.6598 If we add 360 degrees to the last two angles, we get 231.3402 and 321.3402 Clearly, each angle is 90 degrees plus the previous angle 7.13 We can this in MATLAB, to check the hand-drawn plot We put the point (0,0) between the zn values so there is a line going to the origin z_1 = exp(-j*pi/5); z_2 = 2*exp(j*pi/6); z_3 = z_1*z_2; x = real([z_1, 0, z_2, 0, z_3]); y = imag([z_1, 0, z_2, 0, z_3]); plot(x,y,'b') 7.15 There are a couple of different ways that we could plot these rotating vectors Below, we plot the real values on the x-axis, and the imaginary ones on the y-axis t = z_1 z_2 z_3 0:0.01:1; = 4*exp(j*(2*pi*25*t - 5*pi/4)); = 5*exp(j*(2*pi*33*t + pi/6)); = z_1 + z_2; % put between each value z0_1(1:length(z_1)*2) = 0; z0_2(1:length(z_2)*2) = 0; z0_3(1:length(z_3)*2) = 0; for k = 1:length(z_1) z0_1(k*2 -1) = z_1(k); z0_2(k*2 -1) = z_2(k); z0_3(k*2 -1) = z_3(k); end % the plots figure(1); plot(real(z0_1), imag(z0_1), 'b'); 430 DSP Using MATLAB and Wavelets figure(2); plot(real(z0_2), imag(z0_2), 'b'); figure(3); plot(real(z0_3), imag(z0_3), 'b'); % Now plot them as frequencies figure(4); plot(1:length(z_1), abs(fft(z_1)), 'r', 1:length(z_2), abs(fft(z_2)), 'g', 1:length(z_3), abs(fft(z_3)), 'b'); We see from the plots that adding the two together produces a new signal that contains both frequencies, just like when we add sinusoids (but without the mirror image on the frequency plot) Chapter 8.1 X(z) = 3z + 2z −1 + 4z −2 8.3 First we find the frequency response h = [1, 1]; % simulate impulse function imp = zeros(1,1000); imp(10) = 1; % get frequency response H = fft(conv(h, imp)); half = 1:ceil(length(H)/2); plot(abs(H(half))) We see that it is a lowpass filter Now we find the zeros H(z) = + z −1 z(1 + z −1 ) = z + Set z + = We see that it has a zero at z = −1 Finally, we simulate a sampled signal for the input to this filter Answers to Selected Review Questions 431 n = 1:1000; x = cos(2*pi*100*n/200); % Now put through filter y = conv(x, h); plot(y) We see that y is zero, except for the beginning and end The frequency of 100 Hz is very high (fs /2) when the sampling rate is 200 samples/second Since this is lowpass filtered, we expect the signal to be attenuated 8.5 a H(z) = −17 + 29z −1 + 107z −2 + 62z −3 b H(z) = −17 + 29z −1 + 107z −2 + 62z −3 − (4z −1 − 7z −2 − 26z −3 − 15z −4 ) 8.7 H(2ejπ/6 ) = − (2ejπ/6 )−1 + (2ejπ/6 )−2 =1− (1.7321 − j) (2 − 3.4641j) + (1.7321 + j)(1.7321 − j) (2 + 3.4641j)(2 − 3.4641j) = 0.6920 + 0.0335j >> z = 2*exp(j*pi/6); >> H = - z^(-1) + z^(-2) H = 0.6920 + 0.0335i Chapter 9.1 Down-sampling removes every other value, to give z d [n] = {4, -2, -1.5, -2, -4.5} Down-sampling removes every other value, to give w d [n] = {4, 2, 4.5, 4, 4.5} 9.3 Multiresolution is the recursive repetition of a transform (and later repeating the inverse transform accordingly) See the multiresolution figures in the text for an example 432 DSP Using MATLAB and Wavelets 9.5 First, we will use the conv function to the convolution Then we simulate down-sampling Next, we simulate up-sampling, and the inverse transform Finally, we add the results of the two channels to find y Notice how we make sure that x and y have the same length (since filtering makes the output longer) % find forward transform channel1 = conv(x, [1, 1]); channel2 = conv(x, [-1, 1]); % down-sample ch1_out = channel1(2:2:length(channel1)); ch2_out = channel2(2:2:length(channel2)); clear channel1 channel2 % now undo the transform % up-sample channel1(1:length(ch1_out)*2) = 0; channel2(1:length(ch2_out)*2) = 0; for k = 1:length(ch1_out) channel1(k*2 -1) = ch1_out(k); channel2(k*2 -1) = ch2_out(k); end % find inverse transform y1 = conv(channel1, [-1, -1]); y2 = conv(channel2, [-1, 1]); % Add result Trim extra size y = y1(1:length(x)) + y2(1:length(x)); plot(1:length(x), x, 'b', 1:length(y), y, 'g') When we examine the relationship of y to x, we find that y[n] = −2x[n] 9.7 No, this does not work in general Not only is the pattern important, but the filter values are carefully chosen We cannot use just any values for the filter coefficients 9.9 We need to convolve the Daubechies coefficients with the input, then downsample the results In the code below, ch1_out is the lowpass output, while ch2_out is the highpass output % get coefficients d0 = (1-sqrt(3))/(4*sqrt(2)); Answers to Selected Review Questions 433 d1 = -(3-sqrt(3))/(4*sqrt(2)); d2 = (3+sqrt(3))/(4*sqrt(2)); d3 = -(1+sqrt(3))/(4*sqrt(2)); LPF = [ d0, -d1, d2, -d3 ]; HPF = [ d3, d2, d1, d0 ]; % find forward transform channel1 = conv(x, LPF); channel2 = conv(x, HPF); % down-sample ch1_out = channel1(2:2:length(channel1)); ch2_out = channel2(2:2:length(channel2)); 9.11 x = [6, 1, 3, 7, 2, 5, 8, 10]; z = conv(x, [0.5, -0.5]); w = conv(x, [0.5, 0.5]); y1 = conv(z, [-0.5, 0.5]); y2 = conv(w, [0.5, 0.5]); y = y1 + y2; We see that y is the same signal as x, except that it has an extra zero at each end 9.13 The actual length of the outputs depends on the filter size, that is, the db4 filter adds a few more outputs than the db2 does We will instead answer in terms of the general pattern With the down-samplers, each octave has (approximately) half the detail outputs as the octave before it Therefore, we would have: octave ≈ 512 outputs octave ≈ 256 outputs octave ≈ 128 outputs octave ≈ 64 outputs The approximate outputs would be the same size as the details But we not need to keep the approximations from the first octaves Therefore, the total number of outputs is ≈ 64 + 64 + 128 + 256 + 512, or 1024 outputs If up/down-sampling is not used, then each channel would output the same number of inputs The total number of outputs would be ≈ 1024 + 1024 + 1024 + 1024 + 1024, or 5120 outputs 9.15 We will not consider up/down-samplers as part of this answer, since their 434 DSP Using MATLAB and Wavelets effect will only scale our results To find the top channel’s effect on the input, we convolve h0 with g0 , and find the z-transform of the result Our result is aa, 2ab, 2ac + bb, 2(ad + bc), 2bd + cc, 2cd, dd The z-transform is: aa + 2abz −1 + (2ac + bb)z −2 + 2(ad + bc)z −3 + (2bd + cc)z −4 + 2cdz −5 + ddz −6 Now we find the bottom channel’s effect on the input, we convolve h with g1 , and find the z-transform of the result The z-transform of the result is: −aa + 2abz −1 − (2ac + bb)z −2 + 2(ad + bc)z −3 − (2bd + cc)z −4 + 2cdz −5 − ddz −6 When we add the two z-transforms together, to get Y (z), we get: 2(2ab)z −1 + 2(2(ad + bc))z −3 + 2(2cd)z −5 Every other term cancelled out Our goal is to have Y (z) with exactly one term To satisfy this requirement, two of the three terms must be zero, i.e., ab = 0, and cd = The only way we can have ab = is for a or b to be zero Thus, we conclude that this will not work, except for the trivial case where two of the four coefficients are zero That would not be different from a filter with taps Chapter 10 10.1 Multiplying the vector x with a fraction will reduce every value in it The resulting sound will be of a lower volume Using 1.1 as a multiplying factor boosts the sound It will sound louder, but the sound values could exceed +1, and therefore be clipped 10.3 Assume that we have sound arrays already in memory, x1 and x2 The code below will find the concatenation of two sound arrays, assuming they both have channels [r1, c1] = size(x1); [r2, c2] = size(x2); y = x1; y(r1+1:r1+r2, 1:c2) = x2; sound(y, fs) The sound should be stored as column vectors If they are stored as row vectors, then we need to alter the line before the sound command Answers to Selected Review Questions 435 10.5 We will read a sound file, and store it in variable x Assuming that x has channels, with sound in column vectors, we will only use the first channel, and make the result a row vector [x, fs, b] = wavread('test_file.wav'); x = x(:,1).'; X = fft(x); % Shift indices by 50 Xshifted = [X(50:length(X)), X(1:49)]; % convert to time-domain xshifted = ifft(Xshifted); % play result sound(real(xshifted), fs) The code above plays the sound with a “beating”-like noise This shifts the whole spectrum, instead of just the first half Since the halves would be symmetrical, it makes sense to work on the first half only, then mirror the results % Shift indices in first half only Xhalf = X(1:ceil(length(X)/2)); firstHalf = [Xhalf(50:length(Xhalf)), Xhalf(1:49)]; % now mirror this half onto the second half secondHalf = firstHalf(length(Xhalf):-1:1); Xshifted = [firstHalf, secondHalf]; xshifted = ifft(Xshifted); sound(real(xshifted), fs) This still sounds strange The beating noise is gone, but the sound has an odd timing issue No, we will not be able to replicate this by simply changing the sampling frequency (Though this might work if the signal is a single frequency.) Now let’s reverse the spectrum: % Shift indices in first half only midpoint = ceil(length(X)/2); firstHalf = X(midpoint:-1:1); % now mirror this half onto the second half secondHalf = firstHalf(midpoint:-1:1); Xreversed = [firstHalf, secondHalf]; xreversed = ifft(Xreversed); sound(real(xreversed), fs) 436 DSP Using MATLAB and Wavelets The sounds are barely audible, and at very high frequencies 10.7 To find the answer to a problem like this, first try it yourself with a small matrix (say by 4) Once you see the desired outcome, try to isolate a row (or column) of the solution, then replicate it for the other rows (or columns) x = imread('dog256x256.gif'); % Rotate the image and show it % This should work for any image, regardless of dimensions [MAX_ROWS, MAX_COLS] = size(x); for r=1:MAX_COLS cclockwise(:,r) = x(r, MAX_ROWS:-1:1); end imshow(cclockwise); 10.9 The program would not work We get an error message from MATLAB, since we must change each call to only the name, without parameters, i.e., sudoku_search instead of sudoku_search(puzzle) Assuming that we take care of this, the program still will not work The function keyword means that the computer will create its own copies of the parameters, and get rid of them after the function finishes Removing this keyword means that there will be only one copy of the puzzle Once the puzzle has a guess added to it, the programs will not be able to “undo” the guess later 10.11 >> disp(sprintf('%tx',9.0938)) 41118034 >> disp(sprintf('%tx',9.34)) 411570a4 10.13 We can modify the filter_test2.m program from the CD-ROM, to get rid of the things we not need, and to add in several windows % get different window values w1 = window(@gausswin, taps); w2 = window(@hamming, taps); w3 = window(@rectwin, taps); Answers to Selected Review Questions 437 w4 = window(@triang, taps); % now combine window with filter coeffs coeffs1 = w1.' * c2; coeffs2 = w2.' * c2; coeffs3 = w3.' * c2; coeffs4 = w4.' * c2; From plotting the frequency magnitude responses, we can tell right away that the plots corresponding to the rectangle and triangle windows are not very good The other two are both good, and we see a classic filter design trade-off While the Gaussian window has smaller side-lobes, the Hamming window has a narrower main lobe The Gaussian window’s frequency response appears to be just a little bit better than the Hamming window’s response 10.15 a There are (22 ) distinct values {0, −1, 3, 9}, so we would need bits to store each Thus, we need 12 × = 24 bits to store the above sequence, plus a table to let us know how to decode the bits b Let = 00, −1 = 01, 3=10, 9=11 (this is not the same as storing them in binary!) Also, there are several other ways of making this table; the mapping here is somewhat arbitrary c The above signal would be stored as: 00 01 11 01 11 11 00 10 01 10 00 00 d The encoded signal takes 24 bits We would also need to store the decoding table We need a table length, say 16 bits for this We could use 16 bits per value, then a byte to encode the bit pattern and count (i.e., 0010 0000 for the first value) Total: 16 bits + (16 + 8) + 24 = 136 bits This is a small example where the encoding table takes up a large amount of space, but we are able to save it in 70% of the space