can you help me to correct this code below?
Here I tried to do features extraction of ecg by calculating the mean frequency.
First, I read the audio with this code:
Fs, data = read('ecg_file.wav')
output from data: enter image description here
Then, doing FFT to the data
Y = np.abs(rfft(data))
output from the fft: enter image description here
Now, I want to apply this formula which is the formula of the mean frequency. enter image description here
From the reference that I have read, M is the length of the frequency bin. To find P, I use this code:
power_spectrum = Y**2
and I'm still confused to calculate the value of fj. Can you guys help to correct the code above?
Numpy has a nice operation to get the frequency values from a fourier transformation called fftfreq
or rfftfreq
for your example.
import numpy as np
# First get the frequency vector
freq = np.fft.rfftfreq(len(data), Fs)
# Then calculate (weighted) mean
mean_f = np.sum(freq * power_spectrum / np.sum(power_spectrum)) # This should equal to the formula in your image.
You don't need to do a elementwise multiplication with numpy arrays.