本文介绍了相当于MATLAB的bandpower()的python numpy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Python numpy's ftt.ftt() method to generate the fourier transform of a signal. However I want to calculate the bandpower over a range of frequencies. MATLAB has the method bandpower(x,fs,freqrange), I am trying to simulate specifically this syntax of the function. Source: https://www.mathworks.com/help/signal/ref/bandpower.html

它看起来不像numpy具有等效功能,但是有人知道我可以用来模拟bandpower(x,fs,freqrange)的代码片段吗?我不清楚函数幕后到底发生了什么.

It doesn't look like numpy has an equivalent function, but does anyone know a code snippet I can use to mimic bandpower(x,fs,freqrange)? It's not clear to me what exactly is going on behind the scenes in the function.

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

Note: If you know some non-Python pseudocode that would achieve the Matlab function, that would also be helpful.

推荐答案

以下用于计算[fmin,fmax]频段功率的代码段对我有用:

The following snippet for computing the power in the band [fmin, fmax] worked for me:

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的bandpower()的python numpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-16 06:40