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

scale_to_freq returns repeated frequencies. Which one is the correct scale for the frequency? #118

Open
SDevrajK opened this issue Jan 28, 2025 · 0 comments

Comments

@SDevrajK
Copy link

SDevrajK commented Jan 28, 2025

In a previous issue, a commenter pointed out that the scale_to_freq function returns repetitive frequencies. From what I understand, you have successfully reduced the number of repetitive frequencies, but not removed them entirely. In my application, the first 5 frequencies (the lowest scales) are all repeats, and for the last few frequencies (the highest scales) they are all repeated many times.

For applications that need one value of power for each frequency, which scale is the "correct" scale for the stated frequency? The power values returned for frequencies which are repeated are not the same. Is there any further advice you can give for reducing the issue of repeated frequencies?

code to reproduce

import numpy as np
from ssqueezepy import cwt
from ssqueezepy.experimental import scale_to_freq

duration = 100 # seconds
sampling_rate = 2 # Hz
time = np.arange(0, duration, 1/sampling_rate)
rri_signal = 1 + 0.1 * np.sin(2 * np.pi * 0.1 * time)

wavelet_type = 'morlet'

Wx, scales = cwt(rri_signal, fs=sampling_rate, wavelet=wavelet_type, padtype=None)
freqs = scale_to_freq(scales, wavelet_type, len(rri_signal), sampling_rate, padtype=None)

print(freqs)

Thanks for the report. Reproduced (scale_to_freq problem), fixed in #114.

Now it's 11%. scales='log-piecewise' is already a significant reduction (relative to 'log'), but its handling isn't flawless. It's a fundamental continuous-discrete limitation with CWT. There is a workaround, though not pure-CWT based, and not part of ssqueezepy.

But also, the repetition's in the peak center frequency (what scale_to_freq returns). Other measures won't always agree. Visually inspecting, only a few wavelets are duplicate-ish:

visuals code
from ssqueezepy.visuals import plot



freqs, repeats = [], []

for f in freqs_cwt:

    if f in freqs and f != 2048:  # avoid 2048 to avoid large gap in plot

        repeats.append(f)

    else:

        freqs.append(f)

plot(wavelet._Psih[-66:, :50].T, color='tab:blue',

     vlines=(repeats, {'color': 'tab:red', 'linewidth': 1}))

Patch note: I expect that most affected were the lowest frequencies, and most frequencies were minorly affected. One could measure as |1 - f_new/f_old| vs freq index, with both f_new and f_old computed using the new function, but f_old using scale_to_freq(, padtype=None).

Concerning MATLAB and the rest, I consider it out of scope. But if you can show inaccuracy in scale_to_freq, I'll take it as a bug report.

Originally posted by @OverLordGoldDragon in #113

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

No branches or pull requests

1 participant