MATLAB Answers

Adding custom wavelets to cwt

56 views (last 30 days)
Jakob Sørensen
Jakob Sørensen on 11 May 2013
Answered: amin on 18 Dec 2017
Hi there,
Summary: How do I take a custom signal y and turn it into a wavelet for cwt?
Long version: I'm looking into analysis of otoacoustic emissions, using wavelet transformation, meaning that I need to use the cwt function in MATLAB. That is not the problem, the problem is that none of the standard wavelets are any good for otoacoustic emissions. So I'm trying to add a new custom wavlet, using wavemngr. So far, I've come up with the following code...
% Create the pattern signal
t = linspace(0, 1, 1024);
y = (1./(1+(4*(t-0.5)).^4)).*cos(20*(t-0.5));
% Create a polynomial fit of the pattern signal
[psi,tval,nc] = pat2cwav(y, 'polynomial', 10, 'continuous') ;
% Plot the pattern compared to the polynomial fit
plot(t,y,'-',tval,nc*psi,'--'),
title('Original Pattern and Adapted Wavelet (dashed line)')
save('mother', 'nc', 'psi', 'tval');
wavemngr('del','CosWave');
wavemngr('add','CosWave','cosw',4,'','mother');
This however only gives rise to an error:
>> makeWavelet
******************************************************
ERROR ...
------------------------------------------------------
wavemngr ---> Invalid Wavelet Family (Short) Name !
******************************************************
****
ERROR ...
----
wavemngr --->
Add New Wavelet FAILED !!
Invalid number of arguments !
****
I'm probably doing it all wrong, but I really don't get the documentation, so can anyone help me figure out how to get my signal y turned into a wavelet that can be used for cwt?
Best regards and have a nice weekend!

  1 Comment

Jakob Sørensen
Jakob Sørensen on 11 May 2013
Note:
I also tried implementing the function value (array) into cwt directly like this:
scales = 1:32;
t = linspace(0,1,512);
MotherWave = (1./(1+(4*(t-0.5)).^4)).*cos(20*(t-0.5));
Coeffs = cwt(y, scales, MotherWave);
While this works, it seems to give an incorrect wavelet transformation, with no apparent relation to the signal.

Sign in to comment.

Answers (4)

Honglei Chen
Honglei Chen on 13 May 2013
I think you need to define a MATLAB function for the custom wavelet and then pass it to the wavemngr. The link below has a little more details:
www.mathworks.com/help/wavelet/ug/adding-your-own-wavelets.html
There is a section talking about how to build that function.
HTH

  1 Comment

Alexey
Alexey on 7 Jan 2016
I think User-Defined-Wavelets is a much clearer tutorial than adding-your-own you mentioned (should it be removed altogether?)

Sign in to comment.


jan
jan on 5 Sep 2013
I think you forgott one value
wavemngr('add','CosWave','cosw',1,'1','mother');
the second 1 should`t be empty. That how it worked for me even if you only have one Wavelet in your wavelet family (it can still be a happy family)
Greatings from Eindhoven Jan

  3 Comments

Ahmed Fadhil Hassoney
Ahmed Fadhil Hassoney on 18 Oct 2013
Hello please can you please explain an example of how to use new designed wavelet to analyze another signal
thanks
TG
TG on 27 Feb 2015
@above: The most common way to use a new wavelet to analyze a signal is
wtrans = cwt(signal,no_scales,'newwavelet','plot');
where 'newwavelet' is your newly designed wavelet.
Alexey
Alexey on 7 Jan 2016
@jan: your advice didn't work for me: if you look at wavemngr.m line 216, you'll see it's looking for 'no' and converts it to empty string ''. So if you supply empty string (string of length 0), you should be fine.
Note that the space character ' ' (as per wavemngr help) doesn't work for me either - it gives error
wavemngr('add', 'CosWave', 'cosw', 1, ' ', 'mother');
Index exceeds matrix dimensions.
Error in wavemngr (line 460)
k0 = index1(1);
Error in wavemngr (line 719)
wavemngr('create');
460 k0 = index1(1);
I wrote to mathworks so that they would correct the manual but didn't hear back yet.
Your advice probably works if you invoke your wavelet as 'cosw1', but I prefer to keep it without any numbers and hence I think
wavemngr('add', 'CosWave', 'cosw', 1, '', 'mother');
is more correct and the wavelet should be called afterwords with
wname = 'cosw';
Also, I kept trying different things and my wavemngr eventually got really corrupted ('del' and 'restore' and 'clear' wouldn't work). If you're in the same boat, delete
wavelets.asc
wavelets.prv
wavelets.inf
in your work directory. Then do
wavemngr('restore')

Sign in to comment.


Jean-Luc Bruyelle
Jean-Luc Bruyelle on 15 Jul 2015
Edited: Jean-Luc Bruyelle on 15 Jul 2015
Hi
I had the same problem and it comes from the "save" step.
First, when you use "save" you have to save in a .mat file. Than , when you correctly save your pattern , I had an error message and when i opened the script of the wavemngr function and execute step by step it stopped when an other function was called because of the name of the variables that i registered in my .mat file, so I had to call xval and psi X and Y and after having changed their names my code worked.
Here is the code I suggest you, you can try and comment me if it corked fine, that the code i used.
% Create the pattern signal t = linspace(0, 1, 1024); y = (1./(1+(4*(t-0.5)).^4)).*cos(20*(t-0.5));
% Create a polynomial fit of the pattern signal [Y,X,nc] = pat2cwav(y, 'polynomial', 10, 'continuous') ;
% Plot the pattern compared to the polynomial fit plot(t,y,'-',tval,nc*Y,'--'), title('Original Pattern and Adapted Wavelet (dashed line)') save('mother.mat', 'Y', 'X');
% you dont need the nc value to be save as by convention all wavelets saved are of energy = 1 (the integral =1) and this variable is not used in cwt or wavemngr, it is only useful to pass from your pattern to the adapted pattern
wavemngr('del','CosWave');
wavemngr('add','CosWave','cosw',4,'','mother.mat');

  2 Comments

Alexey
Alexey on 7 Jan 2016
Jean-Luc,
This looks good, just couple pointers that made it work for me:
  1. some lines got squished together - please use the code formatting button on top
  2. tval is not defined, X should be used instead
  3. wavemngr is missing one argument - B (1x2 support interval)
corrected code that works for me (the 'del' statement is not necessary for a clean machine, but if you have CosWave defined, then it's good to delete it):
clear; clc; format compact
% Create the pattern signal
t = linspace(0, 1, 1024);
y = (1./(1+(4*(t-0.5)).^4)).*cos(20*(t-0.5));
% Create a polynomial fit of the pattern signal
[Y,X,nc] = pat2cwav(y, 'polynomial', 10, 'continuous') ;
% Plot the pattern compared to the polynomial fit
plot(t,y,'-',X,nc*Y,'--'),
title('Original Pattern and Adapted Wavelet (dashed line)')
save('mother.mat', 'Y', 'X');
% you dont need the nc value to be save as by convention all wavelets saved are of energy = 1 (the integral =1) and this variable is not used in cwt or wavemngr, it is only useful to pass from your pattern to the adapted pattern
wavemngr('del','CosWave');
wavemngr('add','CosWave','cosw',4,'','mother.mat',[0 1]);
Bahar Mohseni
Bahar Mohseni on 11 Jun 2016
Hi Alexey,
I am trying to add a new wavelet to the wavemngr and I use the corrected code which worked for you but I still get these errors. Can you please help me with this?
Index exceeds matrix dimensions.
Error in wavemngr (line 460)
k0 = index1(1);
Error in wavemngr (line 719)
wavemngr('create');

Sign in to comment.


amin
amin on 18 Dec 2017
Hello I used this code as an example for defining a new mother wavelet. but it does not work with wpdec(input,1,'cosw','shannon') how does the new wavelet should be used? following you can see the error when I use it: ERROR ... ----------------------------------------------- wfilters ---> The wavelet cosw is not valid!
Kind Regards Amin

  0 Comments

Sign in to comment.

Sign in to answer this question.