Calculate energy for each frequency band around frequency F of interest to Python

I am new to signal processing, in this question I want to ask how to get energy for each frequency band around the frequency of interest F. I found a formula, but I do not know how to implement it in Python. This is the formula and graph of the Fourier transform: enter image description here

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. 

enter image description here

+6
source share
2 answers

Using the signal processing module from Searching for local highs / lows with Numpy in a 1D numpy array , you should do the following:

 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 
0
source

You are almost like Mike , but here is a different approach that is easier to understand. You can set a variable containing the filtered signal and return a 1d array Af, then apply the above formula, which is quite simple (the square of the sum of these amplitudes)

Filter out such signals

 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 it’s assumed that y is your original signal, and you need 5 Hz of energy in the signal,

  #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]) 
0
source

Source: https://habr.com/ru/post/988553/


All Articles