[英]Calculate energy for each frequency band around frequency F of interest in Python
I am newbie in signal processing, in this question, i want to ask how to obtain energy for each frequency band around interested frequency F. I have found a formula, but I dont know how to implement it in Python. 我是信号处理的新手,在这个问题中,我想问一下如何为感兴趣的频率F周围的每个频段获取能量。我找到了一个公式,但我不知道如何在Python中实现它。 This is the formula and my Fourier transform plot:
这是公式和我的傅里叶变换图:
x = np.linspace(0,5,100)
y = np.sin(2*np.pi*x)
## fourier transform
f = np.fft.fft(y)
## sample frequencies
freq = np.fft.fftfreq(len(y), d=x[1]-x[0])
plt.plot(freq, abs(f)**2) ## will show a peak at a frequency of 1 as it should.
You are almost there as Mike pointed but here is a different approach which is simpler to understand.you can set a variable that holds the filtered signal and return a 1d array of Af, then apply the above formula which is quite simple (squared sum of these amplitudes) 你几乎就像麦克指出的那样,但是这里有一个不同的方法,它更容易理解。你可以设置一个变量来保存滤波后的信号并返回一个Af的1d数组,然后应用上面的公式很简单(平方和这些幅度)
Filter out the signals like this 过滤掉这样的信号
from scipy.signal import butter, lfilter
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
y = lfilter(b, a, data)
return y
now assuming y
is your original signal and you need energy of 5Hz component in signal , 现在假设
y
是你的原始信号,你需要信号中5Hz分量的能量,
#let fs = 250
#let order = 5
oneD_array_of_amps_of_fiveHz_component = butter_bandpass_filter(y, 4, 6, 250, 5)
#calculate energy like this
energy_of_fiveHz_comp = sum([x*2 for x in oneD_array_of_amps_of_fiveHz_component])
Using the signal processing module from Finding local maxima/minima with Numpy in a 1D numpy array you would do the following: 使用在1D numpy数组中使用Numpy查找局部最大值/最小值的信号处理模块,您将执行以下操作:
from scipy.signal import argrelextrema
import numpy as np
delta = 0.1
F = 1
X_delta = abs(freq - F) < delta
A_f_delta = abs(f[X_delta])**2
maximum_ix = argrelextrema(A_f, np.greater)
E_F_delta = np.sum(A_f[maximum_ix])/2
E_F_delta
2453.8235776144866
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.