haar wavelet

280 views
Skip to first unread message

vangalli maruthi

unread,
May 21, 2020, 4:53:53 AM5/21/20
to PyWavelets
how can we generate the haar wavelet transform waveform by using python? The data containing time and power in excel sheet
s27.xlsx

Gregory Lee

unread,
May 21, 2020, 3:45:18 PM5/21/20
to pywav...@googlegroups.com
For reading data from excel I would do either of the following.
1.) Pandas can read directly from excel. You can then convert the resulting DataFrame to a NumPy array.
2.) Alternatively, if you save the .xlsx file in the .csv format, then you can read it directly with NumPy's genfromtxt, setting delimiter=','

Once you have the data in a NumPy array you are ready to use PyWavelets. See pywt.dwt for a single level transform or pywt.wavedec for a multi-level one. You can set wavelet='haar' in either. There is also pywt.swt for the undecimated transform.


On Thu, May 21, 2020 at 4:53 AM vangalli maruthi <marut...@gmail.com> wrote:
how can we generate the haar wavelet transform waveform by using python? The data containing time and power in excel sheet

--
You received this message because you are subscribed to the Google Groups "PyWavelets" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pywavelets+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pywavelets/10417735-8576-43f8-b258-47d584f748f9%40googlegroups.com.

Jean-Eric Campagne

unread,
May 21, 2020, 4:46:19 PM5/21/20
to pywav...@googlegroups.com
Dear Professor Lee
Is there a way to compute the smooth approximation of a sampled fonction using only the scaling function at a given scale 2^J ?
Best regards
Jean-Eric

>
>

Gregory Lee

unread,
May 21, 2020, 7:55:17 PM5/21/20
to pywav...@googlegroups.com
Jean-Eric,

    If I understand your question correctly, the safest way to do that is to run the J-level decomposition, manually set all detail coefficient arrays to zeros and then perform the inverse transform.

You might also see for example the timecourse figures at the top of the following pull request: https://github.com/PyWavelets/pywt/pull/527
I think the panel labeled "A5" in the left column (title MRA) is what you are looking for? That is a reconstruction from only the level 5 scaling coefficients.
 
That `pywt.mra` function has not been released yet, but it is pure-python, so you could copy the mra function from within mra.py in that PR for local use until we have a new release including it.

Hopefully this helps!

Cheers,
Greg

--
You received this message because you are subscribed to the Google Groups "PyWavelets" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pywavelets+...@googlegroups.com.

jeaneric...@gmail.com

unread,
May 25, 2020, 3:23:30 AM5/25/20
to pywav...@googlegroups.com

Hi Greg,

Thanks for your answer. I have tweaked what you mean by the "A5" panel (I was looking for a Github one...).

Well, in fact I've realized that (/latest/ version)

a) in the "wavedec" (DWT 1D multi-level) the returns are [cAn, cDn, ..., cD2, cD1]

 b) in the "swt"  (SWT 1D multilevel) the returns is exactly the same as above IF trim_approx=TRUE

otherwise by default it returns [(cAn, cDn), ..., (cA2, cD2), (cA1, cD1)] that is to say ALL the coefficients obtained during the pyramid algorithm.

=>

1) is there a way to uniformise the two outputs of these two functions ?

2) is there a way that one cab select the desired coefficients: as for instance only the Approx ones [cAn,..., cA2, cA1]

Thanks

Jean-Eric

Gregory Lee

unread,
May 25, 2020, 4:14:43 AM5/25/20
to pywav...@googlegroups.com
On Mon, May 25, 2020 at 3:23 AM jeaneric...@gmail.com <jeaneric...@gmail.com> wrote:

Hi Greg,

Thanks for your answer. I have tweaked what you mean by the "A5" panel (I was looking for a Github one...).

Well, in fact I've realized that (/latest/ version)

a) in the "wavedec" (DWT 1D multi-level) the returns are [cAn, cDn, ..., cD2, cD1]

 b) in the "swt"  (SWT 1D multilevel) the returns is exactly the same as above IF trim_approx=TRUE

otherwise by default it returns [(cAn, cDn), ..., (cA2, cD2), (cA1, cD1)] that is to say ALL the coefficients obtained during the pyramid algorithm.

=>

1) is there a way to uniformise the two outputs of these two functions ?

Well, as you mention, the format is the same when SWT specifies trim_approx=True. In that mode, only the minimal set needed for reconstruction of the original signal is retained.

I guess you are asking for the API for both functions to be the same? You can open a GitHub Issue related to this if you want (at https://github.com/PyWavelets/pywt/issues). It is better to discuss changes to API and/or feature requests there rather than on this list.

That said, I want to keep the default for DWT as it currently is, but we could potentially add a `trim_approx` argument that defaults to False. Changing the default to False in SWT to make things consistent would require some deprecation cycles over a release or two to avoid abruptly breaking user's existing code.
 

2) is there a way that one cab select the desired coefficients: as for instance only the Approx ones [cAn,..., cA2, cA1]

For DWT, if your data is 1D you can compute only the approximation coefficients using something like:

approx = [pywt.downcoef('a', data, wavelet)]
for n in range(1, level):
    approx = [pywt.downcoef('a', approx[-1], wavelet)] + approx  # prepend next decomposition level to list

For SWT, I don't think there is an equivalent. It will be easiest to just call with trim_approx=False and then rebuild the coefficients, discarding the details:
    coeffs = [c[0] for c in coeffs]  # keep only approximation coeffs

Similarly for n-dimensional DWT, you would have to manually discard the unwanted detail coefficients.

 Thanks
Reply all
Reply to author
Forward
0 new messages