# Temporary Pacing Simulator: A Training Tool for Clinicians
## 2 Materials and Methods
### Generation of Pacing Modes Using Algorithms
#### 2.3.2 Atrial Pacing  
Atrial pacing is used to manage arrhythmias and support cardiac function in patients with impaired atrial conduction or atrial fibrillation with slow ventricular response. This subsection describes the algorithms used to simulate the following atrial pacing scenarios: AAland AOO.  
Similar to ventricular pacing, a specialized 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 atrial pacing mode introduced by the user.  
In practice, a TP in AAI pacing mode monitors the heart's natural rhythm and only delivers a pacing stimulus if it does not detect a P wave 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 AAI 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 P peaks in the baseline signal and randomly selecting 20% of the P peak indices as locations for the inhibited beats. The function then iterates through the P 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 PP 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 P 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 AOO mode, the pacing behavior in the code remains the same as in AAI, focusing on introducing pacing spikes at fixed intervals determined by the pacing rate, without considering the intrinsic ECG signal. This mode is typically used in situations where sensing atrial activity is unnecessary or could lead to complications.