# Reconstruct DWT for each cD1,cD2,cD3 and cA3 signals

This question must be basic for this forum, but I'm only start working with DWT recently, and I was working with CWT before.
I think I can decompose my signal to L level and then get the scalogram with the following scripts.
my question is how can I reconstruct the signal for each details{cD1,cD2,cD3} and approximation CA3? Here is what I have tried so far!

t=0:0.1:300;
f=[0.4 0.6 1];
y=sin(2*pi*f(2)*t)+sin(2*pi*f(2)*t)+sin(2*pi*f(3)*t);
len=length(y);level=10;
[c,l]=wavedec(y,level,'coif5');
cfd=zeros(level,len);
for k=1:level
d=detcoef(c,l,k);
d=d(ones(1,2^k),:);
cfd(k,:)=wkeep(d(:)',len);
end
image(flipud(wcodemat(cfd,255,'row')));


P.S: Actually I want to estimate the frequency components in a signal, so I thought I must decompose my signal to few levels then take FFT from the details to estimate the components that involve is the main signal. Is that right? or a better idea?
for the CWT case I was estimating the frequency components directly.

• As I understand now (it may be different in a few days): with a signal x of length $65536=2^{16}$, the DWT will NEVER give you more than 16 levels, ie 16 frequency bins... If you want to estimate precisely the frequencies of your signal, you must use CWT instead. – Basj Nov 23 '13 at 21:25
• it seems that way as someone answered, but there is one thing my signal length is 300. and its not $2^{N}$ so how many level is possible? and if we assume DWT will zeropadd the signal to 512 , it can decompose signal to maximum 9 level? I have done but the results was so bad @Basj – SAH Nov 23 '13 at 21:42
• Yes with signal length 300, DWT won't give you more then 9 levels. With so few scales, you cannot hope good frequency resolution. Have a look at the DWT plot here : mathworks.fr/fr/help/wavelet/ref/cwt.html (DWT has dyadic scales $a=2^k$) – Basj Nov 23 '13 at 21:47
• useful info, tnx, what frequency range each level shows? imagine sampling frequency is &f_s& and signal length is $N$ so what frequency range each level{1,2,..L} represents? @Basj – SAH Nov 23 '13 at 22:08
• Do you use Python? I'm currently doing a test : 1/ do the DWT of a fs=44100hz .wav audio file 2/ modify some coefficiens 3/ do the inverse DWT 4/ listen to the result. If you use Python, I can give you these tests, that are very interesting! Example : on my audio files of length approx $2^{16}$, I can clearly hear now that each level is linked to a musical octave range, ie [f0 hz, 2*f0 hz]... this is normal since the levels are dyadic ... – Basj Nov 23 '13 at 22:25

Alright, I'll try to take a shot at giving you an explanation of what's going on with the DWT.

So in the CWT, basically what you are doing is you are generating the wavelet coefficients by convolving the signal with each scale and shift of the mother wavelet function. However, the problem with doing this is that when you analyze the low frequency components of the signal, you are oversampling, or performing the wavelet analysis too often. Also, the relationship between time and frequency localization is much like the relationship between velocity and position localization in quantum mechanics, and when you perform the CWT this inequality is violated for some time/frequency scales. Essentially, with regard to the CWT, the transform is redundant at low frequency scales.

However the DWT doesn't have this problem because the DWT effectively downsamples the signal at each level. The DWT is basically just two filters, a highpass and a lowpass filter. The highpass filter produces the "detail" coefficients, and the lowpass filter produces the "average" coefficients. However, keep in mind that calculating the moving average of a signal is a form of lowpass filtering. Thus when we perform the lowpass filter and get the "average" DWT coefficients, those coefficients are basically just the low frequency components of the signal. And, since we know they are lower in frequency, we know that in the digital case we need less data points to represent a signal lower frequencies. The lowpass and highpass filtering is then repeated for each level of the DWT that can be computed. The level is dependent on the length of the signal. If a signal has $$k$$ data points, where $$k = 2^N$$ then the DWT can be computed to N levels.

I'll try to give a real life example. Lets say you have a signal of 1024 samples, sampled at a rate of $$4\pi \text{ Hz.}$$ Based on the Nyquist criterion, the highest frequency that can be resolved by the signal is $$2\pi \text{ Hz.}$$Computing the first level of the DWT will result in two vectors, each of length 512. One is from the highpass filter, the other from the lowpass (these vectors are sometimes called high/lowpass filter banks). The highpass coefficients are the cD1 coefficients, representing frequencies in the range of one to two pi, and the lowpass coefficients are the cA1 coefficients representing frequencies in the range of zero to one pi. The DWT is applied again to the lowpass coefficients resulting in two vectors each of length 256, and this time the lowpass filter bank represents frequencies in the range 0 to 1/2 pi. This process is repeated 10 times (for this case, 2^10 = 1024), as the last time will result in lowpass and highpass filter banks each of length 1.

Basically at higher scales of the DWT, we get better frequency resolution, but poorer time resolution. Lower scales have better time resolution but poorer frequency resolution. I'll also give you the link to a good tutorial on this stuff by Polikar : http://person.hst.aau.dk/enk/ST8/wavelet_tutotial.pdf

To reconstruct the signal, simply apply the inverse DWT for the number of levels which you want to reconstruct.

EDIT: Forgot to mention that the FFT is not needed, you are already filtering based on frequencies.

• Thanks for your comprehensive answer, but still I cannot estimate the frequency components of the signal by DWT or WPT. Here is sth I have attempt by WPT but I couldn't estimate the components finely. please look at my question here dsp.stackexchange.com/questions/11388/…, and see what I am doing wrong please, cheers @matt – SAH Nov 23 '13 at 7:40
• @Electricman The frequency estimation is dependent on your sampling rate. The highest frequency that your signal can resolve is sampling_rate/2 (Nyquist). Then each level of the DWT represents the components in the signal from different frequency ranges. After you calculate the first level, the difference coefficients represent the components of the signal within the frequency range of spr/2 to spr/4 and the average coefficients are within the range spr/4 to 0. Each time you apply the DWT to the average coefficients, you double the frequency resolution, but halve the time resolution. – themantalope Nov 23 '13 at 13:36
• Thanks, as you see the sampling rate is 10 in my application and I want to estimate the frequency in range of 0 to 2Hz so that's ok, but why I need decomposition of Level 6 to resolve the frequency components in the signal(why not 5), and also what is the other smaller peaks around the estimated frequencies in level 8,9,10,11; moreover Am I getting the spectrum right from the WPT or DWT. it seems pointless and very inaccurate to estimate frequencies with DWT. @matt – SAH Nov 23 '13 at 16:48
• 10 Hz? Yes, estimating frequencies are less accurate with a DWT, but the tradeoff is that the computation is much faster, and it is not redundant. As for estimating frequencies using the CWT, I suggest reading section 3 of the tutorial I sent you the link to. Frequency estimation is dependent on the sampling rate and the number of scales you decided to compute. @Electricman – themantalope Nov 24 '13 at 0:50
• Also, I'm in China and I can't see the pictures that you posted, but its difficult to tell if you are doing the computation correctly just from looking at the picture.@Electricman – themantalope Nov 24 '13 at 0:54