pythonpandasmedicaldownsampling

How to convert two ECG measurments to HR with frequency 20 hz


I have data from two ECG sensors taken at a frequency of 50hz. I want to convert this to an HR signal with a frequency of 20hz. I have tried a solution with heartpy, but I can't get good values for HR at low frequency. Does anybody have an example of how I can implement this in python?

The data looks like this: Pandas dataframe of signal input


Solution

  • I have made an example in python using neurokit and their example for heartbeats and simply taking the mean of the two ECG examples. Afterwards, I use a simple downsampling function to get the signal to 20 hz.

    Code example:

     ecg = df[['ECG_1', 'ECG_2']].mean(axis=1).to_numpy()
    
    # Automatically process the (raw) ECG signal
    ecg_signals, info = nk.ecg_process(ecg, 
     sampling_rate=sample_rate)
    # plot = nk.ecg_plot(ecg_signals, sampling_rate=sample_rate)
    df.insert(loc=len(df.columns)-1, column='HR (bpm)', value=(ecg_signals['ECG_Rate']))
    # Remove ECG signals
    df = df.drop(columns=['ECG_1', 'ECG_2'])
    
      # Downsampling of the whole dataframe:
    df_20hz = pd.DataFrame(columns=df.columns)
    ds_rate = 50/20
    df_20hz = df.groupby(np.arange(len(df))//ds_rate).mean()
    # Remove overlapping classes: label that is not a whole number
    df_20hz = df_20hz[df_20hz.activity_id % 1  == 0]
    
    return df_20hz