E E - 2 7 4 Introduction to Wavelets & Filter Banks Spring Semester 29 HOMEWORK 5 DENOISING SIGNALS USING GLOBAL THRESHOLDING One-Dimensional Analysis Using the Command Line This example involves a real-world signal - electrical consumption measured over the course of 3 days. This signal is particularly interesting because of noise introduced when a defect developed in the monitoring equipment as the measurements were being made. We will see how well wavelet analysis removes the noise. Do the following: Figure 1: Electrical consumption signal >> load leleccum; >> s = leleccum(1:392) ; l s = length(s) ; Now do a single level wavelet decomposition using the db1 wavelet. First examine the four FIR filters associated with the db1 wavelet in the 2-channel PRFB. >> help wfilters >> waveinfo( db ) >> [a,b,c,d] = wfilters( db1 ) Then obtain the level-1 approx.coeffs. ca1, and detail coeffs. cd1.
>> [ca1,cd1] = dwt(s, db1 ) >> size(ca1) >> size(cd1) Now synthesize the approximation to the signal using ca1 and cd1 >> A1 = upcoef( a,ca1, db1,1,l s); D1 = upcoef( d,cd1, db1,1,l s); or we can use the inverse discrete wavelet transform function >> A1 = idwt(ca1,[], db1,l s); D1 = idwt([],cd1, db1,l s); Now we can display the approximation to the original signal, using (i) the ca1 wavelet coefficients and reconstruction of signal using (ii) the cd1 coefficients. To display the results for this 1-level decomposition, do the following: >> subplot(1,2,1); plot(a1); title( Approximation A1 ) >> subplot(1,2,2); plot(d1); title( Detail D1 ) 55 Approximation A1 25 Detail D1 5 2 45 15 4 1 35 3 5 5 25 1 2 15 15 2 1 1 2 3 4 25 1 2 3 4 Figure 2: Reconstruction from level-1 LP and HP coefficients We can also regenerate the full signal using the Inverse Wavelet Transform and determine the error between the original and reconstructed signal. >> A = idwt(ca1,cd1, db1,l s); >> err = max(abs(s-a))
and we get: err = 2.2737e-13 Now let us do a multilevel wavelet decomposition of the signal. For a 3-level decomposition, (again using the db1 wavelet): >> [C,L] = wavedec(s,3, db1 ); The coefficients of all the components of a 3-level decomposition (that is, the third-level approximation and the three levels of detail) are returned concatenated into one vector, C. Vector L gives the lengths of each component. S ca 1 cd 1 ca 2 cd 2 C ca 3 cd 3 cd 2 cd 1 ca 3 cd 3 7 Extract approximation and detail coefficients. Figure 3: 3-level decomposition showing detail and approx. coefficients To extract the level 3 approximation coefficients from C, >> ca3 = appcoef(c,l, db1,3); To extract the levels 3, 2, and 1 detail coefficients from C, >> cd3 = detcoef(c,l,3); cd2 = detcoef(c,l,2); cd1 = detcoef(c,l,1); or >> [cd1,cd2,cd3] = detcoef(c,l,[1,2,3]); Results are displayed in the figure below, which contains the signal s, the approximation coefficients at level 3 (ca3), and the details coefficients from level 3 to 1 (cd3, cd2 and cd1) from top to bottom. Now we can reconstruct the signal from the level-3 approximation coefficients as also from the level-1,-2, and-3 detail coefficients. To reconstruct the level-3 approximation from C, >> A3 = wrcoef( a,c,l, db1,3);
1 Original signal s and coefficients. 5 2 5 1 15 2 25 3 35 4 1 5 1 1 5 5 5 5 5 1 5 5 1 15 2 Figure 4: Signal, approximation and detail coefficients at three levels To reconstruct the signal from the detail coefficients at levels 1, 2 and 3 >> D1 = wrcoef( d,c,l, db1,1); D2 = wrcoef( d,c,l, db1,2); >> D3 = wrcoef( d,c,l, db1,3); Now display the results of a multilevel decomposition. >> subplot(2,2,1); plot(a3); title( Approximation A3 ) subplot(2,2,2); plot(d1); >> title( Detail D1 ) subplot(2,2,3); plot(d2); title( Detail D2 ) subplot(2,2,4); >> plot(d3); title( Detail D3 )
title('detail D3') 6 Approximation A3 4 Detail D1 5 2 4 3 2 2 1 1 2 3 4 4 1 2 3 4 4 Detail D2 4 Detail D3 2 2 2 2 4 1 2 3 4 4 1 2 3 4 Figure 5: Signal reconstruction using 1 approximation and 3 detail coefficients We can also reconstruct the original signal from all the coefficients obtained using level-3 decomposition. >> A = waverec(c,l, db1 ); err = max(abs(s-a)) and get: err = 4.5475e-13 CRUDE DE-NOISING OF A SIGNAL Removing noise from a signal is an old problem. Wavelets have been used for de-noising. This is still a popular area of research. Basically, one tries to identify which components contain the noise, and then reconstruct the signal without those components. In this example, we note that successive reconstructions (using only the low pass coefficients at various levels) show a less and less noisy signal as more and more high-frequency information is filtered out of the signal. Level 3 approximation, A3, is quite clean as compared to the original signal. To compare the two, do the following: >> subplot(2,1,1); plot(s) ; title( Original ); axis off >> subplot(2,1,2); plot(a3); title( De-noised ); axis off Of course, in discarding all the high-frequency information, we have also lost many of the original signal s sharpest features. Better de-noising requires a more subtle approach. One such approach is called thresholding. This involves discarding only the portion of the details that exceeds a certain limit.
5 Original 4 3 2 2 4 6 8 1 12 14 16 18 5 De noised 4 3 2 2 4 6 8 1 12 14 16 18 Figure 6: Comparison of signal and level-3 approximation coeffs. reconstruction REMOVING NOISE BY THRESHOLDING Let us look again at the details of our level 3 analysis. To display the details D1, D2, and D3, type >> subplot(3,1,1); plot(d1); title( Detail Level 1 ); axis off >> subplot(3,1,2); plot(d2); title( Detail Level 2 ); axis off >> subplot(3,1,3); plot(d3); title( Detail Level 3 ); axis off Typically, most of the noise will occur in the first few levels of decomposition. Note that cd1, cd2, and cd3 are just MATLAB vectors, so we could directly manipulate each vector, setting each element to some fraction of the vectors peak or average value. Then we could reconstruct new detail signals D1, D2, and D3 from the thresholded coefficients. In MATLAB, to de-noise the signal, use the ddencmp command to calculate the default parameters and the wdencmp command to perform the actual de-noising. >> [thr,sorh,keepapp] = ddencmp( den, wv,s); >> clean = wdencmp( gbl,c,l, db1,3,thr,sorh,keepapp); Note that wdencmp uses the results of the decomposition (C and L) that we calculated earlier. We also specify that we used the db1 wavelet to perform the original analysis (and therefore wish to use it to reconstruct). We also specify global thresholding option gbl. See ddencmp and wdencmp in the reference pages for more information about the use of these commands or just use the help function. To display both the original and de-noised signals: >> subplot(2,1,1); plot(s(2:392)); title( Original ) subplot(2,1,2); >> plot(clean(2:392)); title( De-noised
Detail Level 1 Setting a threshold Detail Level 2 Detail Level 3 Figure 7: Detail coefficients at the 3 levels 5 Original 4 3 2 2 4 6 8 1 12 14 16 18 5 De noised 4 3 2 2 4 6 8 1 12 14 16 18 Figure 8: Original and denoised signal using global thresholding
Observe that only the noisy, latter part of the signal has been plotted. Note that the noise has been removed while still preserving the sharp details of the original signal. That is the objective. Comment on the following: (a) In the DWT, only a small fraction of the coefficients are non-zero. Note that the position where they are non-zero cluster around the discontinuities and spatial inhomegeneity of the function f. This is an instance of the data compression properties of the transform. Indeed, norms are preserved, but in the DWT, there are a much smaller number of coefficients. (b) Examine the reconstructed de-noised signal and see if there is much of the random oscillation one associates with noise. Secondly, verify that the signal sharp features have stayed sharp in the reconstruction. (c) Add non-gaussian noise (such as impulsive noise) to a signal and then denoise. Are the results as good as that with Gaussian noise? (d) Comment on effects of (a)small threshold (b) large threshold.