Skip to content

rr

RR

BVP_to_RR_signal(bvp_signal, fps, minHz=0.15, maxHz=0.4, calc_method='fft')

Calculates the RR (R-R interval) signal from the BVP (Blood Volume Pulse) signal.

bvp_signal (numpy.ndarray): BVP signal. fps (float): Sample rate of the BVP signal in Hz. minHz (float, optional): Lower cutoff frequency for band-pass filtering. Defaults to 0.15. maxHz (float, optional): Upper cutoff frequency for band-pass filtering. Defaults to 0.4. calc_method (str, optional): Method for RR calculation ('fft', 'welch', 'periodogram'). Defaults to 'fft'.

list: List of RR intervals calculated from the BVP signal.

Source code in redesign_pipeline/vital_calculations/rr.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def BVP_to_RR_signal(bvp_signal, fps, minHz=0.15, maxHz=0.4, calc_method='fft'):
    '''
    Calculates the RR (R-R interval) signal from the BVP (Blood Volume Pulse) signal.

    Parameters:
    bvp_signal (numpy.ndarray): BVP signal.
    fps (float): Sample rate of the BVP signal in Hz.
    minHz (float, optional): Lower cutoff frequency for band-pass filtering. Defaults to 0.15.
    maxHz (float, optional): Upper cutoff frequency for band-pass filtering. Defaults to 0.4.
    calc_method (str, optional): Method for RR calculation ('fft', 'welch', 'periodogram'). Defaults to 'fft'.

    Returns:
    list: List of RR intervals calculated from the BVP signal.

    '''

    if bvp_signal.shape[0] == 0:
        return np.float32(0.0)

    if len(bvp_signal.shape) == 1:
        bvp_signal = bvp_signal[np.newaxis,:]


    bvp_filtered = butter_bandpass_filter(bvp_signal, 
                                    lowcut = minHz,
                                    highcut = maxHz,
                                    order = 5,
                                    fps=fps)

    rr_lm = []

    if calc_method == 'fft':

        for bvp_lm in bvp_filtered:

            len_data = len(bvp_lm)
            frequency = np.fft.fftfreq(len_data,d=1/fps)
            frequency = frequency[range(int(len_data/2))] # only positive frequencies 
            psd_var = np.fft.fft(bvp_lm)/len_data
            psd_var = psd_var[range(int(len_data/2))]
            psd = np.power(np.abs(psd_var),2)
            rr = frequency[np.argmax(psd)] * 60
            if rr < 6 or rr > 24:
                logger.critical(f'Wrong rr prediction fft {rr}')
            rr_lm.append(rr)

    elif calc_method == 'welch':

        for bvp_lm in bvp_filtered:
            frequency,psd = welch(bvp_lm, fs=fps,nperseg=len(bvp_lm))
            rr = frequency[np.argmax(psd)] * 60
            if rr < 6 or rr > 24:
                logger.critical(f'Wrong rr prediction welch {rr}')
            rr_lm.append(rr)

    elif calc_method == 'periodogram':

        for bvp_lm in bvp_filtered:
            frequency,psd = periodogram(bvp_lm, fs=fps)
            rr = frequency[np.argmax(psd)] * 60
            if rr < 6 or rr > 24:
                logger.critical(f'Wrong rr prediction periodo {rr}')
            rr_lm.append(rr)

    return rr_lm

RR_from_RGB(rgb_signal, bvp_multimethod, fps, minHz=0.15, maxHz=0.4, calc_method='fft')

Calculates RR intervals from an RGB signal using multiple BVP methods.

rgb_signal (numpy.ndarray): RGB signal. bvp_multimethod (object): Object with methods to calculate BVP signals from the RGB signal. fps (float): Sample rate of the RGB signal in Hz. minHz (float, optional): Lower cutoff frequency for band-pass filtering. Defaults to 0.15. maxHz (float, optional): Upper cutoff frequency for band-pass filtering. Defaults to 0.4. calc_method (str, optional): Method for RR calculation ('fft', 'welch', 'periodogram'). Defaults to 'fft'.

dict: Dictionary containing RR intervals for each BVP method.

Source code in redesign_pipeline/vital_calculations/rr.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def RR_from_RGB(rgb_signal, bvp_multimethod, fps, minHz=0.15, maxHz=0.4, calc_method='fft'):
    '''
    Calculates RR intervals from an RGB signal using multiple BVP methods.

    Parameters:
    rgb_signal (numpy.ndarray): RGB signal.
    bvp_multimethod (object): Object with methods to calculate BVP signals from the RGB signal.
    fps (float): Sample rate of the RGB signal in Hz.
    minHz (float, optional): Lower cutoff frequency for band-pass filtering. Defaults to 0.15.
    maxHz (float, optional): Upper cutoff frequency for band-pass filtering. Defaults to 0.4.
    calc_method (str, optional): Method for RR calculation ('fft', 'welch', 'periodogram'). Defaults to 'fft'.

    Returns:
    dict: Dictionary containing RR intervals for each BVP method.

    '''

    bvp_raw = bvp_multimethod.calculate_BVP(rgb = rgb_signal, fps=fps)

    rr_per_method = {}

    for method,bvp in bvp_raw.items():
        rr_per_method[method] = BVP_to_RR_signal(bvp, fps, minHz, maxHz, calc_method=calc_method)

    return rr_per_method