Signal.welch 函数:f 和 pxx 结果为 0

发布于 2025-01-12 01:09:26 字数 533 浏览 0 评论 0原文

我有一个长度为 240 的 BOLD 活动数组。每 2 秒进行一次记录(您可以下载部分数据 这里(该文件可以用.txt编辑器正常打开)。我想要用 signal.welch 函数分析这个时间序列,

f, pxx = signal.welch(data, fs=0.5, window='hanning', nperseg=50, noverlap=25, scaling='density', average='mean')

它给了我以下错误

ValueError:noverlap 必须小于 nperseg。

当我设置 noverlap = None 时,没有显示错误,但 f 等于 0 并且 pxx 是一个 0 数组。

非常感谢您的建议!!

I have an array of BOLD activity of length 240. A recording took place every 2 seconds (you can download a part of the data here (the file can be opend normally with a .txt editor). I want to analyse this time series with the signal.welch function.

f, pxx = signal.welch(data, fs=0.5, window='hanning', nperseg=50, noverlap=25, scaling='density', average='mean')

It gives me the following error

ValueError: noverlap must be less than nperseg.

When I set noverlap = None, no error shows up, but f equals 0 and pxx is an array of 0s.

Thank you very much for your suggestions!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

躲猫猫 2025-01-19 01:09:26

例如,如果我读取数据文件,

data = np.loadtxt('rest1_LeftInsula.1D')

data 将是一个形状为 (240,) 的一维 numpy 数组,并且调用

f, pxx = signal.welch(data, fs=0.5, window='hanning', nperseg=50, noverlap=25, scaling='density', average='mean')

不会出现错误。

如果我将 data 重塑为形状为 (240, 1) 的数组,并将其传递给 welch (这可以通过将其索引为 data[: , None] 或使用 reshape 方法 data.reshape((240, 1))),我得到与您报告的相同的错误:

In [11]: f, pxx = signal.welch(data[:,None], fs=0.5, window='hanning', nperseg=5, 0, noverlap=25, scaling='density', average='mean')
[...]/scipy/signal/spectral.py:1964: UserWarning: nperseg = 50 is greater than input length  = 1, using nperseg = 1
  warnings.warn('nperseg = {0:d} is greater than input length '
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-0e5235350cfd> in <module>
----> 1 f, pxx = signal.welch(data[:,None], fs=0.5, window='hanning', nperseg=50, noverlap=25, scaling='density', average='mean')

[...]/scipy/signal/spectral.py in welch(x, fs, window, nperseg, noverlap, nfft, detrend, return_onesided, scaling, axis, average)
    446 
    447     """
--> 448     freqs, Pxx = csd(x, x, fs=fs, window=window, nperseg=nperseg,
    449                      noverlap=noverlap, nfft=nfft, detrend=detrend,
    450                      return_onesided=return_onesided, scaling=scaling,

[...]/scipy/signal/spectral.py in csd(x, y, fs, window, nperseg, noverlap, nfft, detrend, return_onesided, scaling, axis, average)
    580 
    581     """
--> 582     freqs, _, Pxy = _spectral_helper(x, y, fs, window, nperseg, noverlap, nfft,
    583                                      detrend, return_onesided, scaling, axis,
    584                                      mode='psd')

[...]/scipy/signal/spectral.py in _spectral_helper(x, y, fs, window, nperseg, noverlap, nfft, detrend, return_onesided, scaling, axis, mode, boundary, padded)
   1756         noverlap = int(noverlap)
   1757     if noverlap >= nperseg:
-> 1758         raise ValueError('noverlap must be less than nperseg.')
   1759     nstep = nperseg - noverlap
   1760 

ValueError: noverlap must be less than nperseg.

问题是,默认情况下,welch 沿着输入数组的最后轴。如果该数组的形状为 (240, 1),welch 会尝试将计算应用于二维数组的每个“行”。但每行的长度为 1,对于给定的 nperseg 和 noverlap 值来说太小,这会导致(有点神秘)错误。

您还没有展示如何读取文件并创建 data,但我怀疑它正在创建形状为 (240, 1) 的数组(或其他一些数据结构,例如 Pandas DataFrame) 。

要解决此问题,您可以将数据“展平”为一维数组(例如将 data.ravel() 传递给 welch),或传递参数 axis=0welch 告诉它沿着第一个维度而不是最后一个维度行动。如果您选择后者,请注意 pxx 的形状将为 (26, 1),而不是当 data 具有形状时获得的形状 (26,) (240,)。

If I read the data file with, for example,

data = np.loadtxt('rest1_LeftInsula.1D')

data will be a 1-d numpy array with shape (240,), and the call

f, pxx = signal.welch(data, fs=0.5, window='hanning', nperseg=50, noverlap=25, scaling='density', average='mean')

works with no errors.

If I reshape data to be an array with shape (240, 1), and pass that to welch (which can be done by indexing it as data[:, None] or with the reshape method data.reshape((240, 1))), I get the same error that you reported:

In [11]: f, pxx = signal.welch(data[:,None], fs=0.5, window='hanning', nperseg=5, 0, noverlap=25, scaling='density', average='mean')
[...]/scipy/signal/spectral.py:1964: UserWarning: nperseg = 50 is greater than input length  = 1, using nperseg = 1
  warnings.warn('nperseg = {0:d} is greater than input length '
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-0e5235350cfd> in <module>
----> 1 f, pxx = signal.welch(data[:,None], fs=0.5, window='hanning', nperseg=50, noverlap=25, scaling='density', average='mean')

[...]/scipy/signal/spectral.py in welch(x, fs, window, nperseg, noverlap, nfft, detrend, return_onesided, scaling, axis, average)
    446 
    447     """
--> 448     freqs, Pxx = csd(x, x, fs=fs, window=window, nperseg=nperseg,
    449                      noverlap=noverlap, nfft=nfft, detrend=detrend,
    450                      return_onesided=return_onesided, scaling=scaling,

[...]/scipy/signal/spectral.py in csd(x, y, fs, window, nperseg, noverlap, nfft, detrend, return_onesided, scaling, axis, average)
    580 
    581     """
--> 582     freqs, _, Pxy = _spectral_helper(x, y, fs, window, nperseg, noverlap, nfft,
    583                                      detrend, return_onesided, scaling, axis,
    584                                      mode='psd')

[...]/scipy/signal/spectral.py in _spectral_helper(x, y, fs, window, nperseg, noverlap, nfft, detrend, return_onesided, scaling, axis, mode, boundary, padded)
   1756         noverlap = int(noverlap)
   1757     if noverlap >= nperseg:
-> 1758         raise ValueError('noverlap must be less than nperseg.')
   1759     nstep = nperseg - noverlap
   1760 

ValueError: noverlap must be less than nperseg.

The problem is that, by default, welch is applied along the last axis of the input array. If that array has shape (240, 1), welch attempts to apply the calculation to each "row" of the 2-d array. But each row has length 1, which is too small for the given values of nperseg and noverlap, and that leads to the (somewhat cryptic) error.

You haven't shown how you read the file and created data, but I suspect it is creating an array (or some other data structure, such as a Pandas DataFrame) that has shape (240, 1).

To fix this, you can "flatten" the data into a 1-d array (e.g. pass data.ravel() to welch), or pass the argument axis=0 to welch to tell it to act along the first dimension instead of the last. If you do the latter, be aware that the shape of pxx will be (26, 1), instead of the shape (26,) that you get when data has shape (240,).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文