Signal.welch 函数:f 和 pxx 结果为 0
Signal.welch function: f and pxx outcome is 0
我有一个BOLD数组activity,长度为240,每2秒记录一次(你可以下载一部分数据here (文件可以正常打开.txt 编辑器)。我想用 signal.welch 函数分析这个时间序列。
f, pxx = signal.welch(data, fs=0.5, window='hanning', nperseg=50, noverlap=25, scaling='density', average='mean')
它给我以下错误
ValueError: noverlap must be less than nperseg.
当我设置noverlap = None
时,没有出现错误,但是f等于0,pxx是一个0数组。
非常感谢您的建议!!
如果我读取数据文件,例如,
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
沿输入数组的 last 轴应用。如果该数组的形状为 (240, 1),welch
会尝试将计算应用于二维数组的每一“行”。但是每行的长度为 1,这对于给定的 nperseg
和 noverlap
值来说太小了,这会导致(有点神秘的)错误。
你没有展示你是如何读取文件和创建 data
,但我怀疑它正在创建一个数组(或其他一些数据结构,例如 Pandas DataFrame)形状 (240, 1).
要解决此问题,您可以将数据“扁平化”为一维数组(例如,将 data.ravel()
传递给 welch
),或者将参数 axis=0
传递给 welch
告诉它沿着第一个维度而不是最后一个维度行事。如果您选择后者,请注意 pxx
的形状将是 (26, 1),而不是当 data
具有形状 (240,) 时得到的形状 (26,)。
我有一个BOLD数组activity,长度为240,每2秒记录一次(你可以下载一部分数据here (文件可以正常打开.txt 编辑器)。我想用 signal.welch 函数分析这个时间序列。
f, pxx = signal.welch(data, fs=0.5, window='hanning', nperseg=50, noverlap=25, scaling='density', average='mean')
它给我以下错误
ValueError: noverlap must be less than nperseg.
当我设置noverlap = None
时,没有出现错误,但是f等于0,pxx是一个0数组。
非常感谢您的建议!!
如果我读取数据文件,例如,
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
沿输入数组的 last 轴应用。如果该数组的形状为 (240, 1),welch
会尝试将计算应用于二维数组的每一“行”。但是每行的长度为 1,这对于给定的 nperseg
和 noverlap
值来说太小了,这会导致(有点神秘的)错误。
你没有展示你是如何读取文件和创建 data
,但我怀疑它正在创建一个数组(或其他一些数据结构,例如 Pandas DataFrame)形状 (240, 1).
要解决此问题,您可以将数据“扁平化”为一维数组(例如,将 data.ravel()
传递给 welch
),或者将参数 axis=0
传递给 welch
告诉它沿着第一个维度而不是最后一个维度行事。如果您选择后者,请注意 pxx
的形状将是 (26, 1),而不是当 data
具有形状 (240,) 时得到的形状 (26,)。