Skip to content

Alignment of Filters in dwt and wavedec #795

Open

Description

@tycohen

I am trying to implement a matrix form of the DWT (for MCMC sampling reasons) and am comparing to pywt.dwt and wavedec (v 1.3.0) to confirm that I have constructed my matrix correctly. I am encountering a shift in the coefficients from dwt that I did not expect.

nsamp = 64
wavelet = "db4"
level = 1.
signal = np.zeros(nsamp)
signal[0] = 1.
cA_pywt, cD_pywt = pywt.dwt(signal, wavelet, mode="per")
# compute matrix W
# w_k = W @ signal
# cA_matrix = w_k[: nsamp // (2 ** (level + 1))]
# cD_matrix = w_k[nsamp // (2 ** (level + 1)) : nsamp // (2 ** level)]
# plot cA and cD

Image

I would have thought that for a delta function, the peak would be at cA_pywt[0], not cA_pywt[1]. Are the filters not aligned so that at k=0, wavelet.dec_lo[0] is aligned with signal[0]? Also, during decimation, is every even or every odd sample selected?

Activity

  1. tycohen commented on Mar 31, 2025

    @tycohen
    Author

    I've dug through the C code and done some tests to attempt to answer my own question. Please correct me if I'm wrong. Using mode="periodization", the signal is shifted so that the center of the filter is aligned with the first sample. So in my example above, for "db4", wavelet.dec_lo[0] is aligned with signal[4]. So I'll modify my question: Why is this the convention?

    As for the decimation, with mode="periodization" if the filter is of even length then the even samples are kept and if the filter is of odd length the odd samples are kept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions