# Temporary Pacing Simulator: A Training Tool for Clinicians
## 2 Materials and Methods
### Generation of Pacing Modes Using Algorithms
#### 2.3.1 Ventricular Pacing  
Ventricular pacing is used to manage arrhythmias and support cardiac function in patients with impaired ventricular conduction or complete heart block. This subsection describes the back end algorithms used to simulate the following ventricular pacing scenarios: VVI and VOO.  
Figure 4: The main window of the TCPS panel before the user starts the TP settings selection.  
To simulate different ventricular pacing modes, a specialised function was implemented to modify the original ECG, ABP, and CVP signals collected from patients' baseline rhythms. This function incorporates pacing spikes and adjusts the intervals between heartbeats according to the specified pacing mode and pacing rate introduced by the user, effectively replicating the behaviors of various pacing modes.  
The function begins by calculating the interval between heartbeats in terms of data points. This interval is determined based on the user-specified pacing rate and the sampling frequency of the signals. The R peaks and P waves of the ECG signals are then identified using the NeuroKit2 package [13]. Subsequently, the amplitude of the pacing spikes is calculated as one-third of the highest point of the ECG signal to ensure the spikes are proportional to the amplitude of the input signals. In the next stage, the function starts to simulate the selected ventricular pacing mode introduced by the user.  
In practice, a TP in VVI mode monitors the heart's natural rhythm and only delivers a pacing stimulus if it does not detect a heartbeat within a predefined interval. The code simulates this by ensuring pacing spikes are introduced at regular intervals unless intrinsic beats are detected and paced heartbeats are inhibited as needed. To create a realistic VVI scenario, where some pacing spikes are inhibited due to intrinsic conduction, we generate a list of random positions for the inhibited beats, referencing the total number of R peaks in the baseline signal and randomly selecting 20% of the R peak indices as locations for the inhibited beats. The function then iterates through the R peaks and adjusts the intervals between beats by resampling segments to match the pacing interval. When the heartbeats are inhibited, the pacing spikes are not generated and a shorter RR interval can be seen as a result of the intrinsic conduction. The pacing spikes are introduced before each segment using a function that takes the index position of the R peak as input. However, the positions of the inhibited segments do not get a pacing spike introduced and the code simulates intrinsic heartbeats by adding additional segments to lengthen the intrinsic heartbeat and TP wait time. It is important to note that we do not consider the inhibited beats when the pacing rate introduced by the user is lower than 70 bpm, as in patients following cardiac surgery, the intrinsic heart rate is often below 70 bpm.  
In VOO mode, the pacing behavior in the code remains the same as in VVI, focusing on introducing pacing spikes at fixed intervals determined by the pacing rate, without considering the intrinsic ECG signal. VOO mode is typically employed when the TP is malfunctioning or in emergency situations where immediate ventricular pacing is necessary, as it can be rapidly deployed without the need for fine-tuning sensing parameters.