# Adaptive Estimation Algorithm for Photoplethysmographic Heart Rate Based on Finite State Machine
## 2 Algorithm
### Spectral Tracking
#### 2.3.2 Adaptive Peak Tracking  
Spectral peak tracking is also an important part of the process of estimating heart rate. Most algorithms perform spectral peak tracking based on the fact that the heart rate does not change significantly in the neighboring windows. However, this situation is prone to over-reliance on historical heart rate, leading to gradual deviation from the true heart rate. Therefore, the algorithm adopts a heuristic heart rate tracking method.  
Initialization  
In this stage, users are required to maintain a resting state and initialize the state to a steady state. The spectrogram of the band-pass filtered PPG signal can be analyzed directly in this state, and the position of the highest peak in the resting heart rate interval is selected as the heart rate position \(\text{Loc}_{\text{cur}}\) in the current window so as to derive the estimated heart rate \(\text{BPM}_{\text{cur}}\) as follows:  
\[BPM_{cur}=\frac{L\text{{{{loc}}}}_{cur}-1}{N_{FFT}}\times 60\times F_{\text{S}} \tag{13}\]  
Spectral peak tracking  
From Section 2.3.1, there are a total of three possible states for each window, and each state corresponds to a different spectral peak screening rule, which performs adaptive peak screening based on the state it is in.  
Stable state  
A signal in the steady state satisfies both the CF condition and the distance condition, and the reliability of the spectral information at this point can be considered to be high. The position, \(\text{Loc}_{\text{cur}}\), where the spectral peak with the largest amplitude is located, is selected in the spectral interval corresponding to 60-180 bpm.  
The CF condition and distance condition of the signal in the unstable state are not satisfied, and the two factors of distance and amplitude should be considered as comprehensively as possible when performing the screening of spectral peaks. The corresponding framework is shown in Figure 9. When the state of the previous window is a stable state, all the spectral peaks are found in the interval of \(\left[\text{{{Loc}}}_{\text{pre}}-10,\text{{{Loc}}}_{\text{pre}}+10\right]\), and the position of the spectral peak closest to \(\text{Loc}_{\text{pre}}\) is found as the position \(\text{Loc}_{\text{cur}}\) among all the spectral peaks. When the last window is a transitional state, all spectral peaks are foundin the interval of \([\text{Loc\_pre}-5,\text{Loc\_pre}+5]\), and when the last window is an unstable state, the filtering interval is \([\text{Loc\_pre}-10,\text{Loc\_pre}+10]\), and then the peaks are selected with reference to the method of integrating the amplitude and distance mentioned in the literature [15]. All the magnitudes are first normalized by dividing the maximum value, and then the proximity C(k) of each spectral peak is calculated based on the distance:  
\[\text{C(k)}=1-\frac{|peaks[\text{k}]-Loc\_prev|}{\max(|peaks-Loc\_prev|)} \tag{14}\]  
Figure 8: Block diagram of spectral peak tracking with current state as transitional state.  
Figure 9: Block diagram of spectral peak tracking with current state as unstable state.  
When the distance is closer, the corresponding proximity will also be larger, and its size is limited to \([0,1]\). A weighted average of the magnitude and proximity is performed:  
\[Cand=\beta_{1}\cdot peak_{amp}+\beta_{2}\cdot C \tag{15}\]  
where, \(\beta_{1}=0.7\) and \(\beta_{2}=0.3\), and finally, the spectral peak corresponding to the maximum value is found.