# Temporary Pacing Simulator: A Training Tool for Clinicians
## 2 Materials and Methods
### Generation of Pacing Modes Using Algorithms
#### 2.3.3 Dual Pacing  
Dual pacing is used to coordinate atrial and ventricular pacing to ensure proper atrioventricular synchrony and support cardiac function in patients with various types of heart block or other conduction abnormalities. This subsection describes the algorithms used to simulate the following dual pacing scenarios: DOO and DDI.  
To simulate dual pacing modes, 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 dual pacing mode introduced by the user.  
In practice, a DDI TP monitors the heart's natural rhythm and delivers pacing stimuli to both the atrium and ventricle, but it inhibits pacing if intrinsic beats are detected within predefined intervals. 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 DDI 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 and R peaks in the baseline signal and randomly selecting 20% of the P and R peak indices as locations for the inhibited beats. The function then iterates through the P and R peaks and adjusts the intervals between beats by resamplingsegments to match the pacing interval. When the heartbeats are inhibited, the pacing spikes are not generated, and shorter PP and RR intervals 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 and R peaks 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.  
In DOO mode, the pacing behavior in the code remains focused on introducing pacing spikes at fixed intervals for both the atrium and ventricle, without considering the intrinsic ECG signals. This mode is typically used when asynchronous pacing is needed, such as during surgeries or in emergency situations where immediate dual-chamber pacing is necessary, and precise sensing is not critical. The code simulates this by ensuring pacing spikes are introduced at regular intervals for both chambers, effectively creating a consistent pacing rhythm that does not respond to intrinsic heartbeats. This allows for a straightforward and reliable pacing setup without the need for sensing adjustments, making it particularly useful in specific clinical scenarios where rapid deployment and stable pacing are required.  
The adjusted ECG, ABP, and CVP signals generated by these function provide a realistic simulation of dual pacing scenarios, including the effects of various pacing failures, enabling thorough analysis and testing of TP algorithms and performance.