This is the first time I have worked with any code so I am rather unfamiliar with all the lingo and processes involved. I am trying to convert a signal to the frequency domain and want to find the spectral power within the 0.5-1.0 frequency band. I have the code to produce the relative power within this band, but I know that above 10 Hz it is largely noise and I would like to remove it so it does not interfere with the relative power calculation. This is the code I have been using.

import numpy as np
data = np.loadtxt('Post_EO_Foam_CTRL.txt')

import matplotlib.pyplot as plt
import seaborn as sns

# Define sampling frequency and time vector
sf = 200.
time = np.arange(data.size) / sf

from scipy import signal
# Define window length (4 seconds)
win = 4* sf
freqs, psd = signal.welch(data, sf, nperseg=win)

# Define prop lower and upper limits
low, high = 0.5, 1
# Find intersecting values in frequency vector
idx_prop = np.logical_and(freqs >= low, freqs <= high)

from scipy.integrate import simps
# Frequency resolution
freq_res = freqs[1] - freqs[0]  # = 1 / 4 = 0.25
# Compute the absolute power by approximating the area under the curve
prop_power = simps(psd[idx_prop], dx=freq_res)
print('Absolute prop power: %.7f mV^2' % prop_power)

# Relative delta power (expressed as a percentage of total power)
total_power = simps(psd, dx=freq_res)
prop_rel_power = prop_power / total_power
print('Relative prop power: %.3f' % prop_rel_power)

  • $\begingroup$ What's your question? Does your code not work? $\endgroup$ – endolith Jul 12 at 18:38
  • $\begingroup$ The code works, but the relative power value includes the power of frequencies beyond 10 Hz, of which is largely noise. I would like to remove everything above 10 Hz so that the relative poser calculation can be more accurate. $\endgroup$ – Tau Lambda Jul 12 at 19:05
  • $\begingroup$ perhaps a more direct question would be, how to apply a 4th order butterworth to the data to cut off frequencies above 10 Hz. $\endgroup$ – Tau Lambda Jul 12 at 19:22
  • $\begingroup$ You could calculate power directly from the spectrum and just ignore everything outside of that range. gist.github.com/endolith/1257010 $\endgroup$ – endolith Jul 12 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.