Intelligent control of cardiac rhythms using artificial neural networks

Cardiac rhythms are related to heart electrical activity, being an essential aspect of the cardiovascular physiology. Usually, these rhythms are represented by electrocardiograms (ECGs) that are useful to detect cardiac pathologies. This paper investigates the control of cardiac rhythms in order to induce normal rhythms from pathological responses. The strategy is based on the electrocardiograms and considers different pathologies. An intelligent controller is proposed considering the ECG as the observable variable. In order to allow the assessment of the control performance, synthetic ECGs are produced from a reduced-order mathematical model that presents close agreement with experimental measurements. The adopted model comprises a network of oscillators formed by sinoatrial node, atrioventricular node and His-Purkinje complex. Three nonlinear oscillators are employed to represent each one of these nodes that are connected by delayed couplings. The controller considers the control variable at the His-Purkinje complex. To evaluate the ability of the control law to deal with both intra- and interpatient variability, the heart model is assumed to be not available to the controller designer, being used only in the simulator to assess the control performance. The incorporation of artificial neural networks into a Lyapunov-based control scheme, however, allows the presented intelligent approach to compensate for unknown cardiac dynamics. Results show that abnormal rhythms can be avoided by applying the proposed control scheme, turning the electrocardiogram closer to the expected normal behavior and preventing critical cardiac responses.


Introduction
The physiological functioning of the cardiac system is essentially based on the electrical activity of the heart, and on this basis, the electrocardiogram (ECG) is a classic measurement that characterizes the heart functioning. In fact, the ECG allows one to analyze the heart behavior, evaluating normal and pathological rhythms. Figure 1 presents a schematic picture of an ECG measurement related to a normal cardiac cycle. P wave, QRS complex and T wave can be identified, and each of them is associated with a specific activity of heart  Fig. 1 Schematic view of the heart, with its distinct waveforms for the corresponding specialized cells, and a normal ECG, in which the T and P waves, PQ and ST segments, RR interval and QRS complex are represented physiology. The P wave is associated with the sinoatrial node (SA), the natural pacemaker; the QRS complex is related to the ventricular contraction, being characterized by the atrioventricular node (AV) activity; and the T wave reflects ventricular repolarization, corresponding to the His-Purkinje complex (HP) activity. In this regard, the behavior of the cardiac system can be understood as a complex network and its mathematical modeling is a challenging topic, being the objective of different research efforts. The use of coupled nonlinear oscillators is an interesting approach for a reducedorder description. The description of the behavior of the natural pacemaker is based on the pioneering work of van der Pol and van der Mark [39], who showed the importance of considering a nonlinear dissipation represented by the classical van der Pol oscillator (vdP). Afterward, Grudziński andŻebrowski [23] proposed a modified van der Pol (mvdP) oscillator including stiffness nonlinearities.
Furthermore, concerning the description of cardiac physiology, it is essential to consider the adoption of coupled nonlinear oscillators. Dos Santos et al. [14] for instance took two asymmetrically coupled mvdP oscillators [23] into account to represent sinoatrial and atrioventricular nodes. The characterization of the ECG signal is possible by considering three-coupled oscillators, as proposed by Gois and Savi [22] who considered three heart nodes: SA, AV and HP. Once again, modified van der Pol oscillators are employed, assuming bidirectional and asymmetric time-delayed couplings. Recently, Cheffer et al. [13] improved the threecoupled oscillator model considering different coupling terms. These models are able to describe different heart rhythms, from normal to pathological behaviors. Moreover, the transitions between different rhythms can be explained by non-deterministic aspects. In this case, it is important to mention the analysis of random connections between oscillators [10][11][12].
It is noteworthy that mathematical models of heart dynamics can play a key role in the development of rhythm management devices such as artificial pacemakers and implantable cardioverter-defibrillators. In this regard, the control of cardiac rhythms has been investigated by means of different approaches. Garfinkel et al. [19,20] presented the classical chaos control OGY method [32] on rabbit cardiac muscle. Afterward, more sophisticated chaos control methods were employed for this purpose. Ferreira et al. [17] employed time-delayed feedback control for a natural pacemaker described by the modified van der Pol oscillator [22]. A more com-plete analysis was developed by Ferreira et al. [18] who treated ECG signals described by the three-coupled oscillator model [22]. The results showed the possibility of avoiding critical physiological situations by promoting the stabilization of unstable periodic orbits embedded in chaotic attractors.
In addition, Lounis et al.
[30] employed a high-order control method to treat a heartbeat dynamics described by the model proposed by Quiroz-Juarez et al. [35]. Khan and Nigar [27] proposed a Lyapunov-based active controller considering synchronization by means of a fractional-order chaotic system model.
The controller design represents a major challenge since it must deal with all nonlinearities inherent in the cardiac system, as well as modeling inaccuracies and external disturbances. State observers can handle the first task [21] but may not be a suitable choice for the other issues. Machine learning, in turn, can offer some appealing options to deal with it. Artificial neural networks (ANNs), for instance, have been used to identify and classify heart rhythm diseases [1,16,37,40]. Due to their learning and approximation capabilities, neural networks have also been used in control applications to compensate model uncertainties and disturbances [3,15,26,28]. This paper deals with the intelligent control of cardiac rhythms by means of the feedback linearization approach with an embedded artificial neural network. The boundedness and convergence properties of the control error are proven by means of the Lyapunov stability theory. Following reference [3], a radial basis function (RBF) network with a single hidden layer and only one input neuron is adopted. The chosen network architecture allows universal approximation [33] and avoids the issues related to the curse of dimensionality [2]. The aforementioned features minimize the computational complexity of the resulting intelligent controller, making it light enough to be deployed in cardiac rhythm management devices. Furthermore, by assuming that the mathematical model is not available to the control system designer and adopting an online approach to update the weights of the ANNs, the neural network is able to adapt to different individuals and continuously approximate their cardiac dynamics, denoting the capacity of the proposed scheme to deal with both inter-and intrapatient variability. Heart dynamics is represented by synthetic electrocardiograms (ECGs) produced by a reduced-order model considering a three-oscillator model with delayed coupling terms [13,22]. In this regard, it should be pointed out that the mathematical model is only employed to simulate the cardiac response to the control signal. Different pathological responses are investigated showing that the intelligent controller is able to stabilize a desired rhythm and avoid undesired ones.

Mathematical modeling
Heart rhythms are commonly inferred from the electrical activity of the cardiac system, which in turn can be described by means of three coupled oscillators [13,22]. This work investigates the control of heart electrical activity considering synthetic ECGs produced by this mathematical model. Figure 2 shows the conceptual diagram adopted for the cardiac system, including the HP complex and both sinoatrial and atrioventricular nodes, as well as all possible couplings and external excitations. Now, modified van der Pol oscillators [23] are chosen to represent the HP complex and the sinoatrial and atrioventricular nodes, with time-delayed couplings being assumed to properly tune the transmission time between these oscillators. Each coupling can be individually defined, which allows for asymmetrical and bidirectional characteristics. The introduction of external stimuli in the model enables the representation of abnormal rhythms, in addition to the self-excitatory behavior typically generated by the central nervous system. The resulting cardiac system is then described by the following equations [12]: where k m−n and k τ m−n are the coupling coefficients between m and n nodes, with m and n standing for SA, AV or HP; x τ m−n i = x i (t − τ m−n ) represent the delayed terms, with τ m−n being the time delay; and the external excitation F m (t) = ρ m sin(ω m t) is introduced as a lumped model of spatiotemporal aspects.
Moreover, it is worth noting that the periodic behavior associated with atrial fibrillation (AF) is captured by the harmonic form of F m [25,36], which in fact, due to its explicit dependence on time, ends up increasing the dimension of the system.
The ECG can be represented by incorporating the signals of the three oscillators, being expressed as a linear combination of the state variables [22]: with β 0 , β 1 , β 2 and β 3 being parameters, so that the derivative of the ECG with respect to t becomeṡ Equations (4) and (5) can be used to represent the ECG phase space, favoring a qualitative assessment of cardiac cycle.
Since governing equations are presented in dimensionless form, it is interesting to define a dimensional timet[s]:t = β t t, where β t is expressed in seconds and can be estimated by the ratio between real RR interval, RR exp , and numerical RR interval, RR num , β t = mean(RR exp )/mean(RR num ).

Cardiac rhythms
In order to assess the model's ability to represent cardiac dynamics, six different rhythms are investigated: normal rhythm, ventricular flutter, two different ventricular fibrillation (with and without external stimulus), atrial flutter, and atrial fibrillation. The dynamic model is numerically implemented in C++ using the fourth order Runge-Kutta method with sampling rate of 1 kHz. The model parameters are presented in the Table 1. The numerical results obtained with the adopted model are compared with the real ECG data and are shown in Figs. 3,4, 5, 6, 7 and 8, with the real data provided by the PhysioNet Databases [34]. For all simulations, it is considered that β 0 = 1 mV, Both model parameters and initial conditions were chosen according to [11,13] in order to ensure the proper emulation of typical cardiac rhythms.
The expected normal heart rhythm is presented in Fig. 3 showing a close agreement between the real ECG signal and the simulated one, respectively ( Fig. 3a and b). It should be pointed out that simulations capture the main features of the real ECG signal, characterized by the P and T waves and the QRS complex. Numerical results can also be easily represented in phase space form (Fig. 3c), allowing a better visualization of normal cardiac cycle, which facilitates comparison with abnormal rhythms. Ventricular flutter is a pathological rhythm associated with high-frequency (300 bpm) ventricular contraction and typically without clear distinction between QRS complex, ST segment and T wave, as seen in actual ECG data (Fig. 4a), and corresponding simulated signal (Fig. 4b).
Figures 5 and 6 are related to the ventricular fibrillation pathology. Their irregular and chaotic behavior can be manifested in several variations with the P waves, QRS complex, and T waves that are not recognized. Regarding its mathematical description, basically, two different approaches can be employed [12]. The first one takes into account an external stimulus in the HP oscillator, as shown in Fig. 5, and the second does not consider it, as shown in Fig. 6. In both cases, it is noticeable that the model is capable of representing this type of arrhythmia.   The ventricular fibrillation with and without stimulus are labeled as (1) and (2), respectively Atrial flutter is a rhythmic disorder characterized by an increase in heart rate, usually 300 bpm, but which can range from 240 to 430 bpm [9]. Figure 7 presents typical results related to this pathology, such as a comparison between the real ECG signal (Fig. 7a), and the corresponding simulation results (Fig. 7b), as well as the numerically obtained phase space (Fig. 7c). Note the qualitative agreement between the real and simulated results, particularly seen in typical f waves and clear "sawtooth" shaped P waves.
Atrial fibrillation is a pathology characterized by an irregular RR interval and multiple random impulses [31]. Figure 8 shows this abnormal rhythm, presenting a comparative analysis between real and simulated ECG data, respectively, as shown in Fig. 8(a) and (b). It is worth mentioning that once again the numerical model is able to capture the general behavior of the real ECG signal. The numerically obtained phase space is depicted in Fig. 8(c).
In the next section, in order to turn these pathological rhythms into normal ones, an intelligent controller is introduced. Nevertheless, it is important to emphasize that, even though the adopted model has an essential role in the simulation of heart rhythms, it is assumed  3) and their corresponding parameters are not used in the control law, but only in the simulator, which in turn is needed to evaluate the performance of the control.

Intelligent controller
Intelligent control schemes are able to adapt, learn from experience and predict plant dynamics. They have been successfully employed in the control of chaotic [4,6], robotic [7,29], underactuated [8,28] and other uncertain nonlinear systems [5,15]. Thus, in view of the design of a control system for the heart dynamics, the adopted mathematical model is rewritten in the following form where v is the control signal, assumed to be applied to the HP complex, f represents the part of the vector field (1)-(3) that is assumed to be known by the controller designer, d stands for unmodeled dynamics and Following the feedback linearization approach [38], the control law for a system represented by equation (6) can be designed as follows: withf andd being, respectively, estimates for f and d, x = x − x d representing the tracking error associated with the desired state x d , and λ being a strictly positive constant.
Applying the control law (7) to (6) and assuming that all modeling uncertainties are properly represented by d, i.e. f =f is well known, we geẗ  From (9), it can be seen that in the case of perfect estimation, i.e.d = d, the combined error s and therefore the tracking errorx converges to zero. Perfect estimation, however, is quite hard to obtain in real-world applications. In these cases, the closed-loop dynamics is driven by the approximation errord. As a matter of fact, it suggests that the signal s may also represent a reasonable metric and help to compute an estimatê d(s).
Thus, a RBF network, as depicted in Fig. 9, is adopted to computed:   The chosen input to the neural network is the combined error measure s, which merges the errorx and its time derivativeẋ into a single signal. By adopting s as the single input, instead of the two state errors, we are able to reduce the complexity order of the neural network and make it light enough to be deployed in cardiac rhythm management devices.
The boundedness and convergence properties of the closed-loop signals s and w can be investigated by means of a Lyapunov-like stability analysis. First, let the difference between the actual and optimal weight vector, respectively, w and w * , be defined as δ = w − w * .
Remembering thatd = d −d, with d =d * + and d = w ψ, Eq. (9) becomeṡ By conveniently rewriting the above equation, we geṫ s = −λs − δ ψ + (11) which clearly shows that the closed-loop dynamics is driven by approximation errors. This confirms the importance of adopting a neural network with universal approximation capability [33] and whose weights can be learned online. Now, let a positive-definite function V be defined as (12) where η is a strictly positive constant. It is important to note that the adopted Lyapunov function is globally positive definite, V ≥ 0, being equal to zero when s = 0 and δ = 0 (w = w * ). Now, sinceδ =ẇ, the time derivative of V becomeṡ Hence, by updating w according toẇ = ηsψ,V becomeṡ However, Eq. (13) implies that the bounds of w cannot be guaranteed when |s| ≤ ε/λ. To overcome this issue, the projection algorithm [24] can be evoked to ensure that w will remain within a convex region W = {w ∈ R n : w w ≤ μ 2 }: if w 2 < μ or if w 2 = μ and ηsw ψ < 0 I − ww w w ηsψ otherwise (14) where μ is the desired upper bound of w 2 .

Rhythm control
The proposed intelligent controller is now evaluated by means of numerical simulations at a sampling rate of 100 Hz. The desired states are extracted from a expected normal heart cycle, which means that the controller's main goal is to achieve a normal rhythm while avoiding pathological behavior. Equations (1)-(5) are used to simulate heart dynamics by computing x anḋ x. Four different pathologies are investigated: ventricular flutter, two different ventricular fibrillation cases, atrial flutter, and atrial fibrillation. First, the intelligent control law is compared to the conventional one in the case of ventricular flutter. Afterward, the intelligent controller is employed to prevent the other abnormal rhythms. Figures 10,11,12,13,14,and 15 show the obtained results.
The controller parameter is set to λ = 10. Assuming that no prior knowledge about the heart model is available to the control system designer, i.e.f = 0, the ability ofd to handle all neglected dynamical effects is investigated. Six neurons with Gaussian functions are chosen for the RBF network: . . , 6. The centers and widths are chosen as follows c = [−φ/2; −φ/8; −φ/16; φ/16; φ/8; φ/2] and σ = [φ/2; φ/3; φ/6; φ/6; φ/3; φ/2], respectively, with φ = 20 being a tuning parameter. The weight vector is initialized as w = 0 and updated according to (14), with a learning rate η = 100. It is worth mentioning that the conventional controller used in comparative analysis is easily obtained by setting the learning rate to zero, which completely eliminates the neural network contribution to the control law. Figures 10 and 11 show a comparison between conventional and intelligent schemes applied to the control of ventricular flutter pathology. As can be seen, after turning on the controller at t = 16 s, the intelligent approach is able to stabilize the expected normal rhythm (Fig. 10b), while the conventional one fails (Fig. 10a). The intelligent controller (Fig. 10(b)), allows the expected normal rhythm to be achieved, considerably decreasing the heart rate to around 70 bpm. It should also be noted that the proposed scheme drastically reduces the control error (Fig. 10c), being able to provide an ECG signal with clearly evident QRS complex and both P and T waves (Fig. 10b), while in the conventional approach these characteristics are not properly distinguishable (Fig. 10a), although it seems to at least try to approximate the PQ segment. Another point to be highlighted is that the intelligent scheme, due to the capacity of the adaptive neural network to approximate heart dynamics, provides a very reduced level of control effort (Fig. 10d), when compared to the conventional approach.
The phase spaces depicted in Fig. 11 for both conventional and intelligent approaches also clearly emphasize the strongly improved performance of the proposed control scheme and its ability to turn a pathological cardiac rhythm into a normal one. Therefore, considering the inefficacy of the conventional controller to stabilize the expected normal rhythm, only the proposed intelligent scheme is considered for the other pathologies.
The control of ventricular fibrillation is evaluated by taking into account two different situations: with and without external stimulus, as shown in Fig. 12 and Fig. 13, respectively. As can be seen in Figs. 12(a) and 13(a), the intelligent controller is once again able to stabilize an expected normal rhythm from a pathological one. In both circumstances, with and without external stimulus, the QRS complex and both P and T waves can be clearly observed, as expected in normal behavior. It is also important to highlight the ability of the proposed scheme to deal with the R and S peaks, even if it requires a stronger response from the controller, as can be seen in Figs. 12(b) and 13(b). The corresponding phase spaces, shown in Figs. 12(c) and 13(c), confirm the ability of the proposed scheme to stabilize a normal rhythm. It is worth noting the accurate tracking of the inner loop around the origin, which indeed agrees with Atrial flutter arrhythmia is now in focus (Fig. 14), showing that the proposed intelligent controller is able to normalize heart rate. By inspecting Fig. 14(a), it can be seen that the Q-S time duration is reduced, the RR interval is regulated and the f waves are eliminated. The QRS complex is easily recognized, even with over-shoots at the R vertices, but with irregular peaks associated with P and T waves. In fact, due to this behavior, the controller is quite demanded (Fig. 14b), making its actuation more pronounced when compared to other simulations. When considering the phase plane representation, Fig. 14(c), it can be observed that the intelligent controller changes the loop detected on the left side in the pathological behavior, reducing its ampli- Finally, atrial fibrillation is also taken into account, with results shown in Fig. 15. This abnormal rhythm is characterized by an irregular RR interval, but the intelligent controller is able to turn it into normal behavior, Fig. 15(a). The stabilized ECG shows P, QRS and T waves, but some overshoots are recognized in the R peaks, which can be correlated with the corresponding peaks in the control signal, Fig. 15(b). The phase plane is shown in Fig. 15(c), showing that the controller is able to approximate the inner loop, which emphasizes the tracking of P and T waves. The outer loop, however, is not perfectly captured, which can be confirmed by the small residual error associated with the QRS complex. It is also worth mentioning that, by settingf = 0 in the control law, it is assumed that the heart dynamics is fully compensated byd, the output of the artificial neural network. As a matter of fact, since the Eqs. (1)- (3) and their corresponding parameters are not considered in the control law, the robustness of the proposed intelligent controller against unmodeled dynamics can be corroborated by the presented results. For healthcare applications, a robust controller is crucial to deal with both inter-and intrapatient variability.

Concluding remarks
This paper investigates the control of the electrical activity of the heart. A mathematical model based on three coupled nonlinear oscillators is employed to describe cardiac rhythms, being able to represent both normal and pathological behaviors. An intelligent controller is then proposed to avoid pathological behaviors, by stabilizing the heart dynamics in a normal rhythm. Considering its universal approximation property, a radial basis function network is embedded in the control law to compensate for unmodeled dynamics. Furthermore, a learning scheme designed by means of the Lyapunov stability analysis allows the weights of the neural network to be adjusted online. These features, in fact, allow the adopted neural network to adapt to different individuals and continuously approach their cardiac dynamics, which confers the ability to deal with intra-and interpatient variability. Numerical simulations are carried out considering four different pathologies: ventricular flutter, two different ventricular fibrillation cases, atrial flutter, and atrial fibrillation. The obtained results show that the controller is able to perform rhythm control, avoiding critical cardiac behaviors.
Funding Open Access funding provided by University of Turku (UTU) including Turku University Central Hospital. The authors acknowledge the support of the Brazilian research agencies CNPq, CAPES and FAPERJ.

Data availability
The computer code used in the simulations, including both model and control parameters, can be accessed at https://github.com/RoboteamUFRN/HeartRhythm-Intelligent Control/.

Declarations
Conflict of interest All authors declare that they have no conflicts of interest.
Open Access This article is licensed under a Creative Commons Attribution 4.0 International License, which permits use, sharing, adaptation, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons licence, and indicate if changes were made. The images or other third party material in this article are included in the article's Creative Commons licence, unless indicated otherwise in a credit line to the material. If material is not included in the article's Creative Commons licence and your intended use is not permitted by statutory regulation or exceeds the permitted use, you will need to obtain permission directly from the copyright holder. To view a copy of this licence, visit http://creativecommons.org/licenses/ by/4.0/.