1
$\begingroup$

I am new to signal processing and currently doing a school project in wavelet transform using Python. I want to extract all details and approximations (Example: cA2, cA1, cD2, cD1), pywt could return the cA2, cD2, and cD1 except cA1. I noticed that there are the functions named appcoef and detcoef from MATLAB to return all the approximations and details. How can I do the same using pywt?

$\endgroup$

1 Answer 1

1
$\begingroup$

From the matlab docs https://www.mathworks.com/help/wavelet/ref/appcoef2.html the appcoef function does: "If N = NMAX, then a simple extraction is done; otherwise, appcoef2 computes iteratively the approximation coefficients using the inverse wavelet transform."

import pywt

def appcoef(coeffs, wavelet, level, **kwargs):
    max_level = len(coeffs) - 1
    if level == max_level:
        return coeffs[0]
    # this function also calculates the IDWT (kinda confusing API)
    approx = pywt.waverec2(coeffs[:-level], wavelet, **kwargs)
    # not sure why PyWavelets sometimes gives duplicate final rows/columns
    if np.all(approx[-1] == approx[-2]):
        approx = approx[:-1]
    if np.all(approx[:, -1] == approx[:, -2]):
        approx = approx[:, :-1]
    return approx

I tested against matlab on a single grayscale image for a couple levels, and the normalized results matched to 1e-15; but of course there could be some issue I am missing for other cases.

Oh also detcoef: detcoef = lambda coeffs, level: coeffs[-level]

$\endgroup$

Your Answer

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

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