【问题标题】:Python numpy equivalent of bandpower() from MATLABPython numpy 相当于 MATLAB 中的 bandpower()
【发布时间】:2023-03-17 13:00:01
【问题描述】:

我正在使用 Python numpy 的 ftt.ftt() 方法来生成信号的傅立叶变换。但是我想计算一个频率范围内的带宽。 MATLAB有方法bandpower(x,fs,freqrange),我试图专门模拟这个函数的语法。来源:https://www.mathworks.com/help/signal/ref/bandpower.html

看起来 numpy 没有等效的功能,但是有人知道我可以用来模拟 bandpower(x,fs,freqrange) 的代码 sn-p 吗?我不清楚该函数的幕后究竟发生了什么。

注意:如果您知道一些可以实现 Matlab 功能的非 Python 伪代码,那也会有所帮助。

【问题讨论】:

  • 您是要模拟所有功能还是只使用一种输入参数语法?
  • 只是 bandpower(x,fs,freqrange) 语法
  • 什么是带宽?
  • 不知道朋友,我完全忘记了这是怎么回事,哈哈。

标签: python matlab numpy fft


【解决方案1】:

以下用于计算频带 [fmin, fmax] 中的功率的 sn-p 对我有用:

import scipy 

def bandpower(x, fs, fmin, fmax):
    f, Pxx = scipy.signal.periodogram(x, fs=fs)
    ind_min = scipy.argmax(f > fmin) - 1
    ind_max = scipy.argmax(f > fmax) - 1
    return scipy.trapz(Pxx[ind_min: ind_max], f[ind_min: ind_max])

【讨论】:

  • 为什么使用默认缩放='密度'而不是缩放='光谱'
  • 说得好!根据 matlab 的实现,这可能会偏离一个常数因子。我刚刚检查了 matlab 文档,它们不像 scipy 那样明确地说明单位。我不再有要验证的 matlab 许可证。您能否将 matlab 的输出与此 scipy 实现进行比较,以验证 scaling = 'spectrum' 或 scaling = 'density' 是否与 matlab 匹配?
【解决方案2】:
def bandpower(x, fs, fmin, fmax, time):
    # f, Pxx = scipy.signal.periodogram(x, fs=fs)
    f, Pxx = signal.welch(x, fs, nperseg=fs*time)
    ind_min = np.argmax(f > fmin) - 1
    ind_max = np.argmax(f > fmax) - 1
    return np.trapz(Pxx[ind_min: ind_max], f[ind_min: ind_max])

【讨论】:

  • 根据 John Maidens 的最后回答
猜你喜欢
  • 2012-05-21
  • 1970-01-01
  • 1970-01-01
  • 2011-03-13
  • 2010-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多