NeuroKit icon indicating copy to clipboard operation
NeuroKit copied to clipboard

signal_psd() pesky warning

Open DominiqueMakowski opened this issue 2 years ago • 7 comments

When using signal_psd() with Welch method I pretty much always get the following warning, which is a bit obscure. We should somehow improve that

The duration of recording is too short to support a sufficiently long window for high frequency resolution. Consider using a longer recording or increasing the min_frequency

DominiqueMakowski avatar Oct 19 '21 00:10 DominiqueMakowski

Somewhat related to https://github.com/neuropsychology/NeuroKit/issues/466

DominiqueMakowski avatar Oct 19 '21 00:10 DominiqueMakowski

On top of that, I note that base R gives way nicer results that python:

signal = c(3, 3, 5, 7, 1, 3, 5, 3, 5, 6, 1, 3, 5, 3)
y <- stats::ts(signal, frequency = 100)
npad <- stats::nextn(length(signal), factors = 2)
psd  <- stats::spec.pgram(y, fast = FALSE, plot=FALSE, pad=npad, taper=0.5)
plot(psd$freq, psd$spec)

Created on 2021-10-23 by the reprex package (v2.0.1)

signal = np.array([3, 3, 5, 7, 1, 3, 5, 3, 5, 6, 1, 3, 5, 3])
sampling_rate = 100
psd = nk.signal_psd(signal, sampling_rate=sampling_rate, method = "fft")
plt.plot(psd["Frequency"], psd["Power"])

image

I suspect it might have to do with the stats::nextn(length(signal), factors = 2) setting for the npadding but I can't find any equivalence in Python...

DominiqueMakowski avatar Oct 23 '21 00:10 DominiqueMakowski

When using signal_psd() with Welch method I pretty much always get the following warning, which is a bit obscure. We should somehow improve that

Do you mean the warning is not meaningful enough? 😅 Basically, it has to do with the length of the windows for methods such as Welch that involve dividing the recording into smaller signals. We want to be able to capture at least 2 cycles of any frequency. However, if the frequency is too low and the recording is not long enough to cover 2 of its cycles, we return this warning. But the warning is turned off in higher-level functions like nk.hrv_frequency() and NaN is returned.

Should I rewrite to make it clearer?

Tam-Pham avatar Oct 24 '21 07:10 Tam-Pham

so min_frequency by default is 0. This is always below what welch can do, which means that the warning will always show if we don't manually change min_frequency. What we could go instead, is to simply return np.nan for the impossible frequencies instead of throwing warning?

DominiqueMakowski avatar Oct 24 '21 07:10 DominiqueMakowski

When min_frequency is 0, it's actually sanitized to 0.001 for methods like Welch. And of course, it will expect a long enough recording.

What it currently does is throw this warning and correct the length of the windows anyway so that the function still runs (albeit the result is of lower frequency resolution).

What we could go instead, is to simply return np.nan for the impossible frequencies instead of throwing warning?

This is what we did for the higher level functions where a range of min_frequency is provided. I personally think that nk.signal_psd(), being the lowest level function, should be as informative as possible. Though we can provide an option to silence the warning.

Tam-Pham avatar Oct 24 '21 07:10 Tam-Pham

why not sanitize the min freq to the minimum possible value (instead of 0.001)? Even at this level, I don't see why people would still want impossible values?

DominiqueMakowski avatar Oct 24 '21 07:10 DominiqueMakowski

This issue has been automatically marked as inactive because it has not had recent activity. It will eventually be closed if no further activity occurs.

stale[bot] avatar Apr 23 '22 12:04 stale[bot]

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] avatar Sep 08 '22 17:09 stale[bot]