Riadul Islam Rabbi
Riadul Islam Rabbi

Reputation: 1

Feature extract from rr_intervals to lf_power, hf_power, and lf_hf_ratio using PSD library (Python)

below is my code using python library. The problem is , I am supposed to get different values l_power, hf_power, and ratio but all the values are same, I don't know where I went wrong.

Output Screenshot as below:

enter image description here

# Convert NN intervals to seconds
nn_intervals_sec = df_f['rr_intervals']/1000

# Create a time series for interpolation
time = np.cumsum(nn_intervals_sec)

# Ensure time has a standard numerical index
time = pd.Series(time.values, index=np.arange(len(time)))  # Reset index

# Resample to fixed rate (e.g., 64 Hz)
resampled_time = np.arange(0, time.iloc[-1], 1/8) # Access last element using iloc
resampled_nn = np.interp(resampled_time, time.index, time.values) # Use index and values

# Welch's method for Power Spectral Density (PSD)
freqs, psd = welch(resampled_nn, fs=8, nperseg=128)

# Define frequency bands
lf_band = (0.04, 0.15)
hf_band = (0.15, 0.4)

window_size = 5
df['lf_power'] = df['rr_intervals'].rolling(window=window_size).apply(lambda x: np.trapz(psd[(freqs >= lf_band[0]) & (freqs <= lf_band[1])], freqs[(freqs >= lf_band[0]) & (freqs <= lf_band[1])]))
df['hf_power'] = df['rr_intervals'].rolling(window=window_size).apply(lambda x: np.trapz(psd[(freqs >= hf_band[0]) & (freqs <= hf_band[1])], freqs[(freqs >= hf_band[0]) & (freqs <= hf_band[1])]))
df['lf_hf_ratio'] = df['lf_power']/df['hf_power']

Upvotes: 0

Views: 14

Answers (0)

Related Questions