NeuroKit
NeuroKit copied to clipboard
signal_psd() pesky warning
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
Somewhat related to https://github.com/neuropsychology/NeuroKit/issues/466
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"])
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...
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?
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?
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.
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?
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.
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.