Wavelets, sample rate, and replicating Matlab's default call

Hi,

I’m trying to get the same output as with a default cwt(x,fs) Matlab function call, where x is a signal and fs its sample rate. Could anyone help me? Here’s what I have done so far, but I don’t know how to include sample rate here for instance:

using ContinuousWavelets, Wavelets
function getcwt(d)
    n = length(d)
    c = wavelet(Morlet())
    freqs = getMeanFreq(computeWavelets(n, c)[1])
    a = cwt(d,c)
    return(a,freqs)
end

Any help would be greatly appreciated! :slightly_smiling_face:

typing help cwt in my Matlab 2023b tells me there is no such default function.

>> help cwt
--- cwt not found. Showing help for cat instead. ---
...

We can help you if you include a MWE and the matlab function that you want to translate. Make sure it is not from some proprietary toolbox (i.e. make sure you are allowed to share it)

Hi Boris,

maybe you didn’t add add the wavelet toolbox when you installed Matlab or didn’t have the license for it?

https://www.mathworks.com/products/wavelet.html

Here’s the function doc btw: https://www.mathworks.com/help/wavelet/ref/cwt.html

Alternatively, I’m trying to have a wavelet transform that has very high frequency resolution for low frequencies. I’m interested in anything happening between 0.5 and 10 hz, but I don’t seem to get that region well represented with the function above (or maybe I’m doing something wrong?)

Yes, I do not have it, because I didn’t buy it.

Matlab’s toolboxes are not “default” functions, they are commercial products that Matlab is selling. People using the academic license are sometimes unaware of that because most universities get the academic discount and have all toolboxes available.

That is why I asked for the code, because one cannot translate a function that doesn’t see/have. But if it is a licensed product it is not OK to share I guess.

Are you trying to translate it to learn Julia or do you need this function in Julia to do some further analysis? If it is the latter, there are several packages that give you wavelets and Morse’s wavelet. Have a look at GeneralizedMorseWavelets.jl or JuliaDSP.

Offotpic, but, I don’t know about the wavelet toolbox per se, but for the econometrics toolbox I can say that there is not one function there that is fast, so if you need performance critical code, consider writing your own functions in matlab.

I’m trying to compute cwt in Julia in such a way that low frequency components are well represented, as mentioned in my message above :slight_smile:

And did you look in the packages that I mentioned?

Julia DSP is not a package, but it does have Wavelets.jl which ContinuousWavelets.jl is based on. GeneralizedMorseWavelets.jl could be more documented IMHO, but when following the example in the documentation it yields a visualisation that makes sense. If anyone knows how to get a complex CWT (not a real one) using GeneralizedMorseWavelets please let me know. :slight_smile:

Hi @mahmah took a look into your question about complex wavelets sample rate

" .. where x is a signal and fs its sample rate. > Could anyone help me? Here’s what I have done so far, but I don’t know how to include sample rate here for instance:"

FILE: /.julia/packages/NeuroAnalyzer/r2XpB/docs/src/index.md

NeuroAnalyzer.cmax(x::Vector{<:Complex})
NeuroAnalyzer.cmin(x::Vector{<:Complex})

Adding NOTE Regarding Julia Wavelet Sample Rate appears to be fs as per fs::Int64: sampling rate

NeuroAnalyzer.generate_morlet(fs::Int64, f::Real, t::Real=1; ncyc::Int64=5, complex::Bool=false)
NeuroAnalyzer.generate_morlet_fwhm(fs::Int64, f::Real, t::Real=1; h::Float64=0.25)

Example Julia code in FILE: /.julia/packages/NeuroAnalyzer/r2XpB/benchmarks/na_benchmarking.jl

@time fconv(e10, kernel=generate_morlet(256, 1, 32, complex=true))
print(rpad("Filter: moving average", 36))
NeuroAnalyzer.filter_mavg(e10, k=2)

Example Julia code in FILE: /.julia/packages/NeuroAnalyzer/r2XpB/src/analyze/spectrogram.jl
kernel = generate_morlet(fs, sf[frq_idx], 1, ncyc=ncyc[frq_idx], complex=true)

## RE Sample Rate - - fs::Int64: sampling rate


p = zeros(length(f))
    @inbounds for frq_idx in 1:nfrq
		## Marc TIP Adding NOTE Regarding Sample Rate appears to be fs per `fs::Int64`: sampling rate
        kernel = generate_morlet(fs, f[frq_idx], 1, ncyc = ncyc[frq_idx], complex = true)
        w_conv = fconv(s .* w, kernel = kernel, norm = false)
        p[frq_idx] = median(abs2.(w_conv))
    end

Hope that helps you. And, I find wavelets interesting so please let me know
what your Julia code snippet / MWE (Minimum Working Example) was finally.
I’m also interested to know a bit more about
what you want your wavelet application to accomplish?
and why using Complex wavelets?

Thank you @Marc.Cox ! This looks promising, I’ll have a look; I just wanted to compute wavelet magnitudes. I could have also used e.g. STFT for this, but I prefer to specify wavelet over window parameters :slight_smile:.