Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swt normalization and option to trim the approximation coefficients #476

Merged
merged 9 commits into from
Jul 31, 2019

Conversation

grlee77
Copy link
Contributor

@grlee77 grlee77 commented Mar 16, 2019

This PR adds two optional arguments to swt, swt2 and swtn that change the scaling of the coefficients and control which coefficients are retained.

Option to discard unecessary approximation coefficients
The first option, trim_approx can be set true in order to keep only the approximation coefficients from the final level of decomposition. These are the only ones used by the inverse transform, so discarding the rest reduces memory use. There are also a couple of additional benefits:

1.) when trim_approx=True the format of the coefficients returned is exactly the same as for wavedec, wavedec2 and wavedecn
2.) Because of 1.), it becomes possible to also use the existing pywt.ravel_coeffs and pywt.unravel_coeffs on the output of the SWT transforms.

Option to use energy-preserving normalization

If we rescale the wavelet analysis filterbank by 1/sqrt(2), then the set of forward transform coefficients has the same energy as the original input. Additionally the sum of the variances of the individual coefficient arrays equals the variance of the input signal.

This makes the behavior very similar to the multiple-overlap DWT (MODWT) proposed by Percival and Walden in their book on time-series analysis. With norm=True and trim_approx=True swt can be used to recreate the type of things done with matlab's modwt and in the R package waveslim.

Each commit here is well separated, so it might be easiest to review them sequentially.

@grlee77 grlee77 changed the title ENH: swt normalization and add an option to have swt coefficient formats match wavedec ENH: swt normalization and option to trim the approximation coefficients Mar 16, 2019
@rgommers rgommers added this to the v1.1 milestone Mar 17, 2019
pywt/_multilevel.py Outdated Show resolved Hide resolved
@rgommers
Copy link
Member

Looks like a very interesting enhancement.

With norm=True and trim_approx=True swt can be used to recreate the type of things done with matlab's modwt and in the R package waveslim.

If you have an example lying around already, that would be useful to add I think (perhaps as a demo instead of docstring example).

@grlee77 grlee77 force-pushed the swt_enhancements branch 2 times, most recently from 7bf0544 to 5150c22 Compare March 17, 2019 23:41
@grlee77
Copy link
Contributor Author

grlee77 commented Mar 17, 2019

I added a demo using the existing ecg signal. The variance of the coefficients is demonstrated to sum to the same value as the original signal and plots of the normalized coefficients and variance as a function of signal decomposition level are shown:

fig1

fig2

@grlee77 grlee77 changed the title ENH: swt normalization and option to trim the approximation coefficients swt normalization and option to trim the approximation coefficients Jul 25, 2019
If trim_approx is set to True, we will retain approximation coefficients
only for the final level of decomposition as is currently done for the
DWT functions.

add trim_approx option to all SWT transforms
TST: verify perfect round trip transform when norm=True as well
1.) energy preservation
2.) variance partition
allow output formats of 'swt', 'swt2' and 'swtn' (equivalent to 'wavedec', 'wavedec2', 'wavedecn')
fix links to Modes documentation
@grlee77
Copy link
Contributor Author

grlee77 commented Jul 26, 2019

I rebased this and added a commit fixing up the docstrings as requested. If there are no further comments, I will plan to merge in a few days.

@grlee77 grlee77 merged commit f5c4e26 into PyWavelets:master Jul 31, 2019
@grlee77
Copy link
Contributor Author

grlee77 commented Aug 1, 2019

Somehow the demo shown above got dropped from this PR at some point. I found it here and will make a separate PR to add it back in.

@mohammadAbdolhosseiniMoghaddam
Copy link

mohammadAbdolhosseiniMoghaddam commented Sep 26, 2019

Is trim_approx available in swt ?
I am using 1.0.3 version. The following is the thrown error:
TypeError: swt() got an unexpected keyword argument 'trim_approx'
Any help is appreciated.

@grlee77
Copy link
Contributor Author

grlee77 commented Sep 26, 2019 via email

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

Successfully merging this pull request may close these issues.

3 participants