This collection of files to perform an inverse continuous wavelet transform is an extension of the wavelet software package originally written by Torrence and Compo (http://paos.colorado.edu/research/wavelets/software.html)
The main functions are:
1. contwt.m: (continuous wavelet transform). This is essentially Torrence and Compo's wavelet.m with a few modifications (more inputs and outputs for easier access)
2. invcwt.m: inverse continuous wavelet transform.
3. example_invcwt.m: Demo/example usage. This is a template for building simple sine wave, setting wavelet parameters, and comparing original and reconstructed signal.
Please see the help in each function for details and usage.
jon erickson (2020). continuous wavelet transform and inverse (https://www.mathworks.com/matlabcentral/fileexchange/20821-continuous-wavelet-transform-and-inverse), MATLAB Central File Exchange. Retrieved .
Awesome examples, I worked with my input data and the results were surprising. Very well written code.
Jose Ali - This is a good question. The answer is that the centerfrq() function takes care of the Fourier factor. Note that the centerfreq() function does what you'd want and expect - e.g. "maximizing the Fourier transform of the wavelet modulus is Fc. centfrq computes the center frequency for a specified wavelet." (copied and pasted from https://www.mathworks.com/help/wavelet/ref/scal2frq.html).
if "Each mother wavelet has a corresponding "center frequency", and the relation is given as:
f = centerfrq(mother)/(a*delta).
a = scale
delta = sampling period."
Where will fourier factor take into account?
drissi sana - see the paos.colorado link above. specifically, download the wave_matlab.zip
That is the package this inverse CWT implementation is designed to work with.
hwo has a code matlab of the wavelet transform
ahmad Saad: You can hard threshold coefficients, setting the value to 0 for those outside of the band pass filter limits. This leaves you with non-zero coefficients only in the frequency band you care about. Then just reconstruct using the modified set of coefficients.
how i can use wavelet transform as a band pass filter to reconstruct a time series between certain scales??
How is it possible to design a filterbank for wavelet to introduce a mother wavelet in wavemngr function in matlab?
can you guide me?
How i can use the inverse wavelet to obtain a reconstructed time series between selected scales ???
Y and DT are the input signal and the sampling period. There is no default per se - it's whatever signal you want to analyze and the period at which it was sampled. Hope this clarifies. Good luck. -Jon
Thank you for this code! I am a first semester graduate student and I will be using this to retrieve features from some audio files. My question is regarding the contwt file and these parameters contwt(Y,DT,PAD,DJ,S0,J1,MOTHER,PARAM). What are the typical/default values used for Y and DT?
I have a modis NDVI data of 250m resolution from 2000-2015. I wanted to do CWT for time frequency analysis followed by linear regression to derive slope at each pixel level that will infer the trend during 2000-2015. I am new to MATLAB. Could you please provide your valuable inputs in order to start the matlab program .
My Problem is solved. Lot of thanks Jon.
Hi Partha Das,
You spotted a bug with 'param(nn)'. The indexing nn should not have been there (vestigial remnant of looping over various paramater values). My apologies for not having caught that bug earlier. I've re-uploaded a corrected version to the file exchange, v1.4. Or you can just delete off the (nn) indexing in the file you have.
The meaning/usage of the 'param' input depends on the mother wavelet you are using. From >>help contwt.m:
% PARAM = the mother wavelet parameter.
% For 'MORLET' this is k0 (wavenumber), default is 6.
% For 'PAUL' this is m (order), default is 4.
% For 'DOG' this is m (m-th derivative), default is 2.
Hope this helps resolve your questions. Good luck! -Jon
In example_invcwt.m at line 21 i.e.
[wave, period, scale, coi, dj, paramout, k] = contwt(x,dt, pad, dj, so, j1, mother, param(nn));
what is param(nn)? & what will be the value of 'nn'.
Thank you very much Jon! I'll try that out following your tips. Happy festive.
Hi Paul C. Thanks for your comments. They prompted me to update the example included so that you and others who have asked similar questions can play around a bit. Basically, the answer lies in the wavelet bases--and how parameters are set--relative to the original signal. I've updated the screenshot example to show 20 s of a omega = 2 (f = pi) signal. Previously it was 10 s long, which was essentially too short to have a Morlet wavelet trying to oscillate 6 times within the Gaussian envelope to really properly capture that behavior. With 20 s, one can imagine in the mind's eye how the Morlet wavelet better 'fits', thus the reconstruction is better. Keep in mind also that the parameters set for smallest scale, max number of scales to compute, the mother wavelet all affect how well the original signal can be reconstructed. Probably the best intuition I can off specifically as it relates to the example shown: Given we have a pure sinusoid at a frequency of f_o, we are unlikely to be able to *exactly* reconstruct because we'll have wavelets at various scales (pseudo-freuqencies), none of which exactly match f_o. The average error is indicated by the variable 'dE' in the figure and in the example script. In summary, reconstruction can be better or worse depending on how well the underlying wavelets at various scales describe the original signal. Hope this helps and good luck! -Jon
Very nice code. Could you please explain a bit why the reconstruction doesn't match the original signal (notably in the example visible here with the Morlet wavelet)?
nour yousfi: the output variable wave is a matrix with wavelet coefficients. So you can do imagesc(wave) to see the result in time-frequency space. If you want to see what's happening for a single frequency across time, just select the subset of coefficients that match the frequency (scale) you care about.
I have cwt of signal how will i recover the signal which is unknown to me?
how can I plot cwt vs time in fixed frequency(natural frequency)
That so very good day.
Let me turn to you asking for your kind help in order to get the code in matlab for the fourier transform of continuous-time (FFT) directly and inversely, since my knowledge in matlab are not very good.
I appreciate who I can help and collaborate.
Excuse my bad English and my native language is Spanish
Yousef - Glad you got the example code to run. The limitation you mentioned arises because I adapted Torrence and Compo's original code, which only had a small family of wavelet bases included. A few more could easily be added in the future simply by adding more in wave_bases.m.
I could run the code with the "example" you posted, but here is my main problem:
Why is this limited to only three family of functions. How about "db10" or "cgau8"?
I am trying to run the code. But there is an error:
Unable to read file ref.txt
where can I find ref.txt
Clean, concise, simple to use, excellent results. Thank you very much! If a few more wavelets could be added in the future, would be much appreciated.
If you want to isolate a single scale (frequency) in the reconstruction, then zero out all coefficients for all scales and all time, except the selected scale. Then reconstruct.
Trevor: Yes, you are correct. Filling in zeros for gaps ensures those terms (time points) ensure the sums remain unchanged.
Just add 'haar' in the list of available wave bases, as you suggest.
Thanks for this fantastic code!
A question regarding time series with gaps:
As I understand, the Haar wavelet will ignore gaps, as long as they are replaced by zeros and the time series is zero-meaned. If this is correct, could you include (either here or in the wave_bases.m file) code for the Haar wavelet?
The example you have provided works fine. But, when I apply to my data, the reconstructed signal seems to be scaled down.
Secondly, could you plz tell me how to reconstruct only a particular periodic signal (frequency) from the wavelet coefficients..?
hey friends Im working with myoelectric signal and i need to use the wavelet transform for processing the signal and i need some like a tutorial for get some knowledge
Alexandre: MATLAB did not used to have the inverse cwt function in their wavelet toolbox as of 2008. Hence, at that time, it was filling a gap--that matlab hadn't yet implemented. I have not used the newer version of matlab wavelet toolbox, but my guess is that it is the same concept, but less full-fledged.
Zahra: CWT does not share the same notion of details and approximations--that is DWT only.
When I decompose the signal with the CWT, how can i reconstruct the details coefficients and the approximations coefficients, like in the discrete wavelet when we use the matlab function (wrcoef)?
Could you please explain how this differ from the Wavelet Toolbox (cwt.m function) ? Thanks.
If by "instantaneous frequency" you mean the "equivalent Fourier frequency" (i.e. sine wave in infinite time domain at oscillating at a single frequency), then here's the answer. The CWT "pseudofrequency" depends on the mother wavelet you use for the transform. Each mother wavelet has a corresponding "center frequency", and the relation is given as:
f = centerfrq(mother)/(a*delta).
a = scale
delta = sampling period.
I strongly encourage you to read up on wavelet theory, in order to become a competent user of this code.
Thank you very much Jon for the great files. Could you please explain how some one could obtain the instantaneous frequency from the transformed wavlet. I found a lot of literature on how to do so for a signal but not its transformed wavlet... I suspect it is rather easy, but i am somewhat lost... Thanks in advance!
Please note that the reconstructed signal is always centered. That is, the reconstruction process does not/cannot account for any 'dc offset' in the original signal.
For people who cannot reconstruct the signal, please check your downloaded 'invcwt.m'. In Line 34, if it is calling the function 'wave_bases', change it to 'wave_bases_rec'. Rerun your program and see if it works...
requested example (what's missing there?) Could be a documentation problem?
%% test invcwt(contwt(y))
% default is MORLET according to documentation:
Ajay and Ravi,
I have never experienced similar issues.
Please provide more specifics on your problem, and I'll try to respond. E.g., please copy/paste the commands you used. It would also be helpful if you posted an image of your original signal and the (erroneous) output.
Hi Jon, I am also having trouble reconstructing the signal back. I get a vector having all values NAN.
I meant does not seem to perform perfect reconstruction.
The inverse continuous transform does seem to perform a perfect reconstruction. Is there a way to retrieve the original signal back.
The link above is correct if you remove the extra ')' which was accidentally made part of the hyperlink. Sorry about that.
worked as of this afternoon when I tested it.
It would be nice if you had more documentation for the software and attached the data file for the test function.
The link you provided does not work.
fixed bug in example_invcwt.m
LAST UPDATED DEC 19 2016: added more extensive example in example_invcwt.m
Default number of scales computed is the minimum sufficient number for accurate reconstruction. Previously, depending on the number of samples in the original signal, the continuous wavelet transform computed used one scale to few.
-Added example usage.