Signal.welch 函数:f 和 pxx 结果为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
例如,如果我读取数据文件,
data
将是一个形状为 (240,) 的一维 numpy 数组,并且调用不会出现错误。
如果我将
data
重塑为形状为 (240, 1) 的数组,并将其传递给welch
(这可以通过将其索引为data[: , None]
或使用 reshape 方法data.reshape((240, 1))
),我得到与您报告的相同的错误:问题是,默认情况下,
welch
沿着输入数组的最后轴。如果该数组的形状为 (240, 1),welch 会尝试将计算应用于二维数组的每个“行”。但每行的长度为 1,对于给定的 nperseg 和 noverlap 值来说太小,这会导致(有点神秘)错误。您还没有展示如何读取文件并创建
data
,但我怀疑它正在创建形状为 (240, 1) 的数组(或其他一些数据结构,例如 Pandas DataFrame) 。要解决此问题,您可以将数据“展平”为一维数组(例如将
data.ravel()
传递给welch
),或传递参数axis=0
到welch
告诉它沿着第一个维度而不是最后一个维度行动。如果您选择后者,请注意pxx
的形状将为 (26, 1),而不是当data
具有形状时获得的形状 (26,) (240,)。If I read the data file with, for example,
data
will be a 1-d numpy array with shape (240,), and the callworks with no errors.
If I reshape
data
to be an array with shape (240, 1), and pass that towelch
(which can be done by indexing it asdata[:, None]
or with the reshape methoddata.reshape((240, 1))
), I get the same error that you reported: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 ofnperseg
andnoverlap
, 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()
towelch
), or pass the argumentaxis=0
towelch
to tell it to act along the first dimension instead of the last. If you do the latter, be aware that the shape ofpxx
will be (26, 1), instead of the shape (26,) that you get whendata
has shape (240,).