From: Ivan Goremiko on
I am trying to implement db4 DWT with periodazed wavelets.
However,my results is slightly different what matlab does.
As I noticed my code works fine for one-level decomposition,but
and higher level I've got details coefficients different from matlab code.
Approximation coefficients are calculating fine.
My algorithm in general:

read signal

apply circular convolution to low pass filter
apply circular convolution to high pass filter
shift last element to first (??)I have no idea why it is needed in Matlab
signal = details coefficients
repeat --.

Can everyone tell me what is wrong with this approach and what matlab doing with
dwtmode set to ''per'?
From: Wayne King on
"Ivan Goremiko" <merlushatrash(a)gmail.com> wrote in message <hv6ch4$fek$1(a)fred.mathworks.com>...
> I am trying to implement db4 DWT with periodazed wavelets.
> However,my results is slightly different what matlab does.
> As I noticed my code works fine for one-level decomposition,but
> and higher level I've got details coefficients different from matlab code.
> Approximation coefficients are calculating fine.
> My algorithm in general:
>
> read signal
>
> apply circular convolution to low pass filter
> apply circular convolution to high pass filter
> shift last element to first (??)I have no idea why it is needed in Matlab
> signal = details coefficients
> repeat --.
>
> Can everyone tell me what is wrong with this approach and what matlab doing with
> dwtmode set to ''per'?

Hi Ivan, are you iterating on the lowpass filter output? Are you downsampling? Perhaps you're sampling a different phase of the wavelet coefficients than Matlab.

Compare the following:

% getting a reproducible signal
reset(RandStream.getDefaultStream)
x = randn(8,1);

% Daubechies 4 extremal phase wavelet
[Lo,Hi] = wfilters('db4');

% Level 1
extend = length(Lo)/2;
y = wextend('1D','per',x,extend);
A1 = conv2(y(:)',Lo(:)','valid');
A1 = A1(2:2:length(x));
D1 = conv2(y(:)',Hi(:)','valid');
D1 = D1(2:2:length(x));

% A1 are the level one approximation and D1 the level one details

% Level 2

% input is A1 the output of the lowpass filter downsampled
y = wextend('1D','per',A1,extend);
A2 = conv2(y(:)',Lo(:)','valid');
A2 = A2(2:2:length(x)/2);
D2 = conv2(y(:)',Hi(:)','valid');
D2 = D2(2:2:length(x)/2);

% Compare
[A2 D2 D1]

with

[C,L] = wavedec(x,2,'db4');
C'


Finally, as far as "shift last element to first (??)I have no idea why it is needed in Matlab"

I think you're just talking about ordering the C vector output of wavedec(). There is no magical reason why you have to do that. That is simply the way the developers decided to order the vector of coefficients.

Hope that helps,
Wayne
From: Wayne King on
"Ivan Goremiko" <merlushatrash(a)gmail.com> wrote in message <hv6ch4$ff5$1(a)fred.mathworks.com>...
> I am trying to implement db4 DWT with periodazed wavelets.
> However,my results is slightly different what matlab does.
> As I noticed my code works fine for one-level decomposition,but
> and higher level I've got details coefficients different from matlab code.
> Approximation coefficients are calculating fine.
> My algorithm in general:
>
> read signal
>
> apply circular convolution to low pass filter
> apply circular convolution to high pass filter
> shift last element to first (??)I have no idea why it is needed in Matlab
> signal = details coefficients
> repeat --.
>
> Can everyone tell me what is wrong with this approach and what matlab doing with
> dwtmode set to ''per'?

See my reply to one of multiple posts.
From: Wayne King on
"Ivan Goremiko" <merlushatrash(a)gmail.com> wrote in message <hv6cbi$4vo$1(a)fred.mathworks.com>...
> I am trying to implement db4 DWT with periodazed wavelets.
> However,my results is slightly different what matlab does.
> As I noticed my code works fine for one-level decomposition,but
> and higher level I've got details coefficients different from matlab code.
> Approximation coefficients are calculating fine.
> My algorithm in general:
>
> read signal
>
> apply circular convolution to low pass filter
> apply circular convolution to high pass filter
> shift last element to first (??)I have no idea why it is needed in Matlab
> signal = details coefficients
> repeat --.
>
> Can everyone tell me what is wrong with this approach and what matlab doing with
> dwtmode set to ''per'?

See my reply to one of your multiple posts.
From: Wayne King on
"Wayne King" <wmkingty(a)gmail.com> wrote in message <hv7l6c$ekk$1(a)fred.mathworks.com>...
> "Ivan Goremiko" <merlushatrash(a)gmail.com> wrote in message <hv6ch4$fek$1(a)fred.mathworks.com>...
> > I am trying to implement db4 DWT with periodazed wavelets.
> > However,my results is slightly different what matlab does.
> > As I noticed my code works fine for one-level decomposition,but
> > and higher level I've got details coefficients different from matlab code.
> > Approximation coefficients are calculating fine.
> > My algorithm in general:
> >
> > read signal
> >
> > apply circular convolution to low pass filter
> > apply circular convolution to high pass filter
> > shift last element to first (??)I have no idea why it is needed in Matlab
> > signal = details coefficients
> > repeat --.
> >
> > Can everyone tell me what is wrong with this approach and what matlab doing with
> > dwtmode set to ''per'?
>
> Hi Ivan, are you iterating on the lowpass filter output? Are you downsampling? Perhaps you're sampling a different phase of the wavelet coefficients than Matlab.
>
> Compare the following:
>
> % getting a reproducible signal
> reset(RandStream.getDefaultStream)
> x = randn(8,1);
>
> % Daubechies 4 extremal phase wavelet
> [Lo,Hi] = wfilters('db4');
>
> % Level 1
> extend = length(Lo)/2;
> y = wextend('1D','per',x,extend);
> A1 = conv2(y(:)',Lo(:)','valid');
> A1 = A1(2:2:length(x));
> D1 = conv2(y(:)',Hi(:)','valid');
> D1 = D1(2:2:length(x));
>
> % A1 are the level one approximation and D1 the level one details
>
> % Level 2
>
> % input is A1 the output of the lowpass filter downsampled
> y = wextend('1D','per',A1,extend);
> A2 = conv2(y(:)',Lo(:)','valid');
> A2 = A2(2:2:length(x)/2);
> D2 = conv2(y(:)',Hi(:)','valid');
> D2 = D2(2:2:length(x)/2);
>
> % Compare
> [A2 D2 D1]
>
> with
>
> [C,L] = wavedec(x,2,'db4');
> C'
>
>
> Finally, as far as "shift last element to first (??)I have no idea why it is needed in Matlab"
>
> I think you're just talking about ordering the C vector output of wavedec(). There is no magical reason why you have to do that. That is simply the way the developers decided to order the vector of coefficients.
>
> Hope that helps,
> Wayne