2
$\begingroup$

I am trying to implement DWT (Discrete Wavelet Transform) on a given signal, without using the dwt function in Matlab. My approach was to get the synthesis filter coefficients using,

[LoD, HiD, LoR, HiR] = wfilters('db4')

and then get the approximation and detail coefficients by convolving my input signal with the filters and then downsampling by 2

a = downsample(conv(x,LoD),2);
d = downsample(conv(x,HiD),2);

But the output for this doesn't match the output I get after using the Matlab function.

[a,d] = dwt(x,LoD,HiD)

Do I need to pad my signal for it to match the output? I have tried symmetric padding but it didn't help much.

$\endgroup$
0

1 Answer 1

2
$\begingroup$

MATLAB uses, by default, a symmetric padding of the signal.
So the procedure is something like:

  1. Pad the signal with symmetric extension.
  2. Apply convolution with valid shape.
  3. Downsample the result by factor 2.

The downsampling is basically something like: vA(firstIdx:2:lastIdx);.
Where firstIdx = 2; and lastIdx = length(vX) + length(vF) - 1;.

Where vX is the input signal and vF is the filter used.

This is an example how to replicate:

clear();

% Parameters
numSamples  = 27;
wavType     = 'db4';

% Data
vA = randn(numSamples, 1);
[vLoD, vHiD, vLoR, vHiR] = wfilters(wavType); %<! Filters
numCoeff = length(vLoD); %<! All filters same length

firstIdx = 2;
lastIdx  = numSamples + numCoeff - 1;

% Padding
vB = padarray(vA, numCoeff - 1, 'symmetric', 'both');

% LoD
vY = conv(vB, vLoD, 'valid');
vYLoD = vY(firstIdx:2:lastIdx);

% HiD
vY = conv(vB, vHiD, 'valid');
vYHiD = vY(firstIdx:2:lastIdx);

% Reference
[vYLoDRef, vYHiDRef] = dwt(vA, wavType);

max(abs(vYLoD - vYLoDRef))
max(abs(vYHiD - vYHiDRef))

It will generate practically zero difference.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.