0

I have a function beat detect. First I detect the r-peaks in the ecg signal with find peaks and store the peaks in an array. Then the function finds all peaks of the ppg signals that are in the range of the ecg peak and stores them also in an array. Now I have several measurements of several probands in which my peaks are to be detected. My code loops through all measurements and executes the code one by one for each measurement. Then my program has interrupted measurement 3 at proband 2 and the following error message appeared.Output of the error in the console

ValueError: negative dimensions are not allowed

def beat_detect (ECG, PPG_red_interp, PPG_ir_interp, PPG_red2_interp, PPG_ir2_interp):
  ecg_peaks = signal.find_peaks(ECG, height=np.mean(ECG)+2*np.std(ECG), prominence=np.mean(ECG)+np.std(ECG))
  ecg_beats = np.zeros((len(ecg_peaks[0]) - 2, 110))
  ppg_red_beats = np.zeros((len(ecg_peaks[0]) - 2, 110))
  ppg_ir_beats = np.zeros((len(ecg_peaks[0]) - 2, 110))
  ppg_red2_beats = np.zeros((len(ecg_peaks[0]) - 2, 110))
  ppg_ir2_beats = np.zeros((len(ecg_peaks[0]) - 2, 110))
    
  for i in range(1, len(ecg_peaks[0])-1):
      ecg_beats[i-1] = ECG[ecg_peaks[0][i]-55:ecg_peaks[0][i]+55]
      ppg_red_beats[i-1] = PPG_red_interp[ecg_peaks[0][i]-55:ecg_peaks[0][i]+55]
      ppg_ir_beats[i-1] = PPG_ir_interp[ecg_peaks[0][i]-55:ecg_peaks[0][i]+55]
      ppg_red2_beats[i-1] = PPG_red2_interp[ecg_peaks[0][i]-55:ecg_peaks[0][i]+55]
      ppg_ir2_beats[i-1] = PPG_ir2_interp[ecg_peaks[0][i]-55:ecg_peaks[0][i]+55]
        
  return ecg_peaks, ecg_beats, ppg_red_beats, ppg_ir_beats, ppg_red2_beats,  ppg_ir2_beats

I thought it is because my find peaks function does not find any peaks in the ecg signal and so i have no values in the array ecg_peaks. So for the ecg signal where the error occurs I detected the peaks with find peaks. All peaks were found. After that I exchanged my ecg signal file and then the error occurred with proband 7, measurement 4. Then I exchanged the ecg signal file again and the error occurred only at proband 17, measurement 1. However, replacing the file again did not bring any improvement. So it can't be due to the peak detection or the ecg signal data, can it? How can I solve the problem, any ideas?

I would be very pleased about suggestions or solutions.

Kcodx
  • 1

1 Answers1

0

You assigned

    ... = np.zeros((len(ecg_peaks[0]) - 2, 110))

This puts a constraint on the input .shape -- it must have more than one peak entry.

So check it:

    assert len(ecg_peaks[0]) >= 2, ecg_peaks[0]
    ... = np.zeros((len(ecg_peaks[0]) - 2, 110))

That will help you chase the problem upstream in the data flow, to identify the processing stage that failed to identify enough ECG peaks. For example, you may wish to graphically plot the input and then adjust your height= and prominence= thresholds.

J_H
  • 17,926
  • 4
  • 24
  • 44
  • Thank you very much. The program interrupts and I now get as error **AssertionError: []** So I have to adjust my parameters in find peaks as you have already written. – Kcodx Apr 05 '23 at 16:59