# Online Algorithm for Deriving Heart Rate Variability Components and Their Time-Frequency Analysis
## 2 Materials and Methods
### Online Algorithm  
The proposed online algorithm based on VMD (VMDon) differs significantly from the offline one. HRV is analyzed in windows moving by one sample, containing the latest measured value and several older ones. The window length for each component is different and, by design, is equal to the period corresponding to the lower limit of the given frequency range. Such a window length allows to capture all the frequencies of interest. In practice, after preprocessing, HRV was resampled to 2 Hz, so the window lengths were rounded by 0.5 s. Another issue is the lack of a defined lower-frequency limit for ULF; in this case, the length of the window was chosen empirically as the shortest one, allowing the effective extraction of the lowest synthetic frequencies. In summary, the window lengths for the HF, LF, VLF and ULF components were, respectively, T\({}_{\text{HF}}\) = 6.5 s, T\({}_{\text{LF}}\) = 25 s, T\({}_{\text{VLF}}\) = 303 s, and T\({}_{\text{ULF}}\) = 1800 s.  
In the analyzed window, samples are affected by lower frequencies from outside the range of the component. For this reason, the linear trend within the window is first removed to eliminate this influence. Since only one component is expected in the remaining part of the signal, VMD returning only one IMF is employed, directly being the estimated frequency component for that particular window. Then, the analyzing window moves by one sample with a new HRV value, and the calculations are repeated, which means that for a given moment of time, many different estimates of a given component sample are calculated. The final estimate is the average of the estimates for that sample calculated within all window positions covering it. Averaging is performed at the very beginning of the window (the oldest sample) because, only for that sample, all the estimates from the previous locations of the window are accessible (Figure 1).  
Each time the higher-frequency component is obtained, it is subtracted from the original HRV to form the input signal for extracting the lower-frequency component. Thus, only the searched component is expected in the window since the lower frequencies are eliminated by trend removal, and the higher frequencies have already been extracted. This causes additional delay because the extraction of the lower frequencies requires ready-made results for the higher ones (Figure 1).  
The entire procedure for one VMDon iteration is shown as a pseudocode in Algorithm 1. The extraction of lower-frequency components is carried out in the same way as in the case of HF. Additionally, ULF calculated as a last waveform is increased by the value of the trend previously subtracted before its extraction, as it represents the basal HR. The additional flowchart of the VMDon is presented in Figure 2.  
```
1:whiledo
2: collect new \(HRV[t]\) sample
3:\(HRV_{HF}\ =\ HRV[t-T_{HF}:\ t]\leftarrow\) shift HF window
4:\(HRV_{HF}\ =\ HRV_{HF}-\text{trend}(HRV_{HF})\leftarrow\) remove trend
5:\(IMF_{t}\ =\ V\text{VMD}(HRV_{HF})\leftarrow\) extract one IMF from current HF window
6:\(HF[t-T_{HF}]\ =\ \frac{1}{\Pi_{HF}}\sum_{t=t-T_{HF}}^{t}IMF_{t}[t-i]\leftarrow\) extract a single HF sample as the average of all IMFs for that specific point in time
7:\(HRV_{LF}\ =\ HRV-HF\leftarrow\) remove HF from HRV before LF extraction
8: extract LF, VLF and ULF repeating steps 4-7 in analogous way
9:\(ULF\ =\ ULF+\text{trend}(ULF)\leftarrow\) restore basal HRV
10: wait until the next HRV sample is measured
11:endwhile
```  
**Algorithm 1** Variational Mode Decomposition Online (VMDon)  
Figure 1: The principle of operation of the VMDon algorithm (window lengths of virtual components A (3 samples) and B (5 samples) are set only for demonstration of the algorithm).