ECG data classification method based on RNN and Mama sequence model
By combining the method of fusing RNN and Mamba sequence models with time-frequency domain features, the shortcomings of traditional models in capturing long-term dependencies of ECG data are solved, and the classification accuracy of ECG data and the ability to identify arrhythmic signal patterns are achieved.
Patent Information
- Application Number
- CN202510095407.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-21
- Publication Date
- 2025-04-11
- Estimated Expiration
- 2045-01-21
AI Technical Summary
Traditional RNN and LSTM models have shortcomings in capturing long-term dependencies of ECG data, resulting in insufficient recognition of abnormal signal patterns across multiple heart beats.
The ECG data classification method based on RNN and Mamba sequence models is adopted, and the time-frequency domain characteristics of ECG data are extracted and fused, and the time-frequency domain characteristics are captured using the RNN and Mamba models, and the frequency domain characteristics are obtained through continuous wavelet transformation and discrete Fourier transformation, and finally classified by ResNetECG.
It improves the accuracy of ECG data classification, and can more effectively capture the long-term dependence relationship and time-frequency domain characteristics of ECG data, thereby improving the ability to identify abnormal signal patterns such as arrhythmia.
Smart Images

Figure CN119807853A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer vision deep learning and medical data processing, and specifically to an ECG data classification method based on RNN and Mamba sequence model. Background Art
[0002] ECG (electrocardiogram) data is the heart's electrical activity data recorded by electrodes on the skin. It is often used to monitor the health of the heart and identify abnormal rhythms. It is also suitable for arrhythmia detection. ECG signals usually include different waveforms (P wave, QRS complex, T wave, etc.), which reflect the different stages of the heart's electrical activity. Arrhythmia is a heart rhythm disorder caused by abnormal electrical activity of the heart, which may manifest as a fast, slow or irregular heartbeat. The severity of arrhythmias varies, from harmless symptoms to potentially fatal heart problems. It is of great significance to study the abnormal detection of arrhythmias.
[0003] Traditionally, the detection of arrhythmias relies on electrocardiograms, where doctors observe the ECG waveform to determine whether there are abnormalities and thus determine whether it is an arrhythmia. Although manual analysis can accurately determine certain types of arrhythmias in the ECG, this method is time-consuming and relies on the doctor's experience, and is not suitable for processing large amounts of complex ECG data. With the increase in data and the improvement of computer performance, the use of deep learning methods to study ECG data has important clinical value and necessity, because it can effectively process large amounts of complex data and extract key information, thereby improving the accuracy and efficiency of heart disease diagnosis and treatment.
[0004] ECG data contains important time domain and frequency domain features, but traditional deep learning models (such as convolutional neural networks (CNNs) and recurrent neural networks (RNNs)) usually have difficulty in effectively capturing both features at the same time. Most models only focus on time domain information and ignore frequency domain features, which may lead to inaccurate detection of certain ECG events (such as abnormal rhythms). At the same time, ECG signals have long-term dependencies (such as the timing relationship between different bands), but traditional RNN and LSTM models are not capable of capturing long-term dependencies, which may result in the model being unable to correctly identify abnormal signal patterns across multiple heartbeats. Summary of the invention
[0005] In order to overcome the problem that the RNN and LSTM models are not capable of capturing long-term dependencies, the present invention proposes an ECG data classification method based on the RNN and Mamba sequence model, which improves the accuracy of the classification results by extracting and fusing the time-frequency domain features of the ECG data and then performing classification processing.
[0006] In order to achieve the above object, the technical solution specifically adopted by the present invention is as follows:
[0007] An ECG data classification method based on RNN and Mamba sequence model includes the following steps:
[0008] S1. Obtain an ECG data set and annotate the data therein;
[0009] S2. Use the Encoder to construct the feature expression of the ECG signal and add the position code;
[0010] S3, extracting time domain features through the main branch based on RNN and Mamba sequence model;
[0011] S4, obtaining frequency domain features and enhancing time domain features through auxiliary branches;
[0012] S5. Classify the enhanced time domain features based on ResNetECG.
[0013] Furthermore, in step S1, the ECG data set is a public data set downloaded from the PhysioNet website, and the data in the data set are labeled using a python script, and the labeled data is saved in a file in json format.
[0014] Furthermore, in the step S2, the ECG signal is preliminarily processed based on the Encoder encoder to extract the preliminary features of the input signal, generate a potential feature representation, and then add a position code to the potential feature table to inject absolute or relative position information.
[0015] Furthermore, in the step S3, the time domain features of the signal are obtained based on the timing of the ECG signal through the RNN and Mamba sequence models, the RNN module obtains the short-term dependency features of the signal and performs weighted combination of the short-term features between each time step through the Self-Attention mechanism to obtain weighted short-term features; the Mamba module obtains the long-term dependency features of the signal by processing the weighted short-term features, which is equivalent to obtaining the long-term dependency features based on the short-term features, thereby obtaining a new time domain feature representation that contains both short-term feature information and long-term feature information.
[0016] Furthermore, in the step S4, the frequency-time information of the signal is obtained by CWT continuous wavelet transform, and the amplitude and frequency information of the signal is obtained by DFT discrete Fourier transform, and the two different forms of frequency domain features obtained are integrated into the time domain features obtained by the main branch at different times by adding the corresponding element positions represented by features of equal size, thereby enhancing the time domain features.
[0017] Furthermore, in step S5, ResNetECG is used for classification tasks, the end of the network is a FC fully connected layer, and the number of nodes represents the number of categories of the ECG data set.
[0018] The present invention has the following characteristics and beneficial effects:
[0019] 1) The present invention provides a time domain feature extraction method based on RNN and Mamba sequence model: the RNN sequence model obtains the short-term dependency features of the ECG signal and performs weighted combination of the short-term features between each time step through the Self-Attention mechanism; and the Mamba module obtains the long-term dependency features of the signal based on the short-term features, thereby obtaining a new time domain feature representation containing both short-term feature information and long-term feature information, achieving the effect of having a longer dependency than the LSTM model.
[0020] 2) The frequency domain features of the ECG signal are obtained through continuous wavelet transform and discrete Fourier transform, and they are used to enhance the previous time domain features: the frequency-time information of the signal is obtained through CWT continuous wavelet transform, and the amplitude and frequency information of the signal is obtained through DFT discrete Fourier transform; these two types of information are used to extract the frequency domain features of the ECG signal through two branches, and integrate them into the time domain features, thereby achieving an enhancement effect on the time domain features for better classification. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] Figure 1 The figure is a flow chart of an ECG data classification method based on RNN and Mamba sequence model according to an embodiment of the present invention.
[0022] Figure 2 The overall network structure diagram of the embodiment of the present invention.
[0023] Figure 3 A structural diagram of an RNN module and a Mamba module for extracting time domain features provided in an embodiment of the present invention.
[0024] Figure 4 This is a diagram showing the effects of continuous wavelet transform and discrete Fourier transform before and after the embodiment of the present invention.
[0025] Figure 5 This is a diagram of the Encoder and ResNetECG network structure provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0026] The present invention is described in detail below in conjunction with specific embodiments. The following embodiments will help those skilled in the art to further understand the present invention, but are not intended to limit the present invention in any form. It should be noted that, in the absence of conflict, the embodiments of the present invention and the features in the embodiments can be combined with each other.
[0027] like Figure 1 As shown, an ECG data classification method based on RNN and Mamba sequence model includes the following steps:
[0028] S1. Obtain the data set and label the samples in it;
[0029] For the purpose of the present invention, a public data set downloaded from the PhysioNet website, namely the MIT-BIH arrhythmia database, is used, which mainly contains 48 30-minute long two-lead electrocardiogram (ECG) records with a sampling frequency of 360 Hz. There are a total of 20 labels, and the most common 5 labels and all other labels are selected in the experiment: N (normal beat), L (left bundle branch block beat), R (right bundle branch block beat), V (ventricular premature beat), A (atrial premature beat), other + (all other 15 labels).
[0030] Since the ECG signals in the original data set do not correspond one to one with the labels, data annotation is required. Data annotation is implemented in the present invention by writing a Python script file, and the annotation data is saved in a json format file.
[0031] S2. Construct the feature expression of the data through the Encoder encoder and add position encoding
[0032] The Encoder is used to preliminarily process the original ECG signal, extract the preliminary features of the input signal, and generate a potential feature representation. Figure 5 As shown in the figure, the input ECG data size is (B, 3600, 1), where B represents the batch size, 3600 represents the time step, and 1 is the feature dimension under the time step. The first one-dimensional convolution layer has a kernel size of 7, a step size of 5, and a padding of 1. The second one-dimensional convolution layer has a kernel size of 5, a step size of 3, and a padding of 1. After convolution, batch normalization, and Relu activation, the input data changes from a size of (B, 3600, 1) to a size of (B, 240, 1). The purpose of the batch normalization layer BatchNorm1d is to standardize the data to speed up training. The Relu activation function transforms the linear mapping into a nonlinear mapping to enhance the diversity of features. At the same time, due to the characteristics of the Relu activation function itself, the gradient explosion is suppressed to a certain extent. The formula expression is:
[0033] y = Relu(BN(Conv(x)))
[0034] Among them, Conv represents the convolution operation, BN represents the batch normalization operation, and Relu table is the activation function.
[0035] According to the temporal nature of ECG data, there is a sequence between each time step, and after the time step is compressed by the above-mentioned encoder (the time step changes from 3600 to 240), the order between the time steps disappears. In order to use the order information of the ECG signal, positional encoding is added to the feature representation to inject absolute or relative position information. In the present invention, fixed position encoding based on sine function and cosine function is used.
[0036] Assume that the input ECG signal X is of size (L, D), where L represents the time step and D represents the embedding dimension. The position encoding uses the position embedding matrix P of the same shape to output X+P. The elements in the i-th row, 2j-th column and 2j+1-th column of the matrix are:
[0037]
[0038] where p i,2j ,p i,2j+1 Represent the encoded values at the 2jth column of the i-th row and the 2j+1th column respectively.
[0039] S3: Obtain the time domain characteristics of the signal based on the timing of the ECG signal through the main branch
[0040] The main branch is implemented based on the RNN and Mamba sequence models, and is mainly composed of the RNN model, the Mamba model and the feature fusion module. The RNN model is used to obtain the short-term dependency features of the signal and perform weighted combination of the short-term features between each time step through the Self-Attention mechanism to obtain the weighted short-term features; the Mamba module obtains the long-term dependency features of the signal by processing the weighted short-term features, which is equivalent to obtaining the long-term dependency features based on the short-term features, thereby obtaining a new time domain feature representation that contains both short-term feature information and long-term feature information.
[0041] Specifically, Figure 3 As shown, combined with Figure 2The main branch (middle branch) structure of the RNN model is a multi-layer cyclic structure. The number of layers is set as a hyperparameter, and the number of hidden units is set according to the complexity of the input data. According to the output result of the Encoder encoder, for the ECG signal of 240 time steps, the present invention uses an RNN model with 3 layers and 64 hidden units. In the expanded structure diagram of the RNN model, x1, x2, ... x n is the time step, is the hidden layer state, y1,y2,...y n is the output of the RNN model at each time step. After the RNN model, the input data changes from a size of (B, L, D) to (B, H, L). Among them, B represents the batch size, L is the time step size of the signal, D is the feature dimension of the input, and H is the hidden layer size set in the RNN. RNN can memorize the information of the previous time step through the hidden state h, and use this historical information to affect the output of the current time step. In this way, it can capture the timing dependency in the sequence data and extract the timing characteristics of the ECG signal. The formula is as follows:
[0042] h t =tanh(W ih ·x t +W hh ·h t-1 +b n )
[0043] Among them, h t is the hidden state of the current time step, W ih is the input weight matrix, acting on the input x at the current time step t Up, W hh is the recurrent weight matrix acting on the hidden state h of the previous time step t-1 Up. b h is the bias term. tanh is the activation function.
[0044] The self-attention mechanism is the core module in Transformer. Its function is to establish associations between different positions in the same sequence, helping the model to better understand long-distance dependencies. In the present invention, the relationship between different time steps of the input ECG signal itself is established by using the self-attention mechanism, and a weighted combination is performed. The formula is as follows:
[0045]
[0046] Among them, Q refers to the query sequence, K refers to the key sequence, and V refers to the value sequence. In the present invention, Q, K, and V are all the same ECG signal sequence. Q and K are calculated to obtain attention weights, and multiplied with V to obtain a new sequence after considering the context. Self-Attention uses attention weights to learn how each time step is associated with other time steps, dynamically adjusts the weights, and generates new feature representations that consider the relationship between different time steps.
[0047] The Mamba model is a sequence model, which can also be regarded as an RNN cycle model in essence. Different from the traditional RNN structure, it implements parallel computing through one-dimensional convolution operations during training, and uses the SSM state space model during inference. The SSM state space model is a model that maps the input sequence x(t) to the potential state representation h(t) and outputs the predicted sequence y(t). The formula is as follows:
[0048] h′(t)=Ah(t)+Bx(t)
[0049] y(t)=Ch(t)+Dx(t)
[0050] Among them, x(t) is the input sequence, h(t) is the potential state representation at any time t, and y(t) is the output sequence. A is the state transfer matrix, B is the matrix from input sequence to state representation, C is the matrix from state representation to output sequence, and D is the matrix directly from input sequence to output sequence. The reason why the Mamba model can obtain the long-term dependency information of the signal lies in the setting of the A matrix. By using the HiPPO matrix to generate a potential state representation that can remember long-term historical information, and combining it with the C matrix to pass it on, the long-distance dependency information of the sequence (signal) is obtained.
[0051] Combination Figure 3 Explain the Mamba structure. For the input sequence x, first use RMSNorm to perform normalization operations, and change the size of the sequence through two linear mapping linear layers. Then one branch is processed by one-dimensional convolution conv1d, activation Silu, and state space model SSM to obtain a potential state representation a; the other branch is only processed by Relu activation function to obtain another potential state representation b; then a and b are multiplied and mapped again through the linear layer to obtain the output sequence y. The input sequence x and the output sequence y have the same size.
[0052] S4. Obtain frequency domain features and enhance time domain features through auxiliary branches
[0053] Based on the characteristics of ECG data waves, the frequency-time information of ECG signals is obtained through CWT continuous wavelet transform, and the amplitude and frequency information of ECG signals is obtained through DFT discrete Fourier transform; then, the frequency-time information and the amplitude and frequency information are integrated into the time domain features obtained by the main branch, thereby achieving an enhancement effect on the time domain features. Specifically:
[0054] like Figure 4 As shown, continuous wavelet transform CWT is a process of decomposing a signal into wavelet basis functions of different frequency bands (scales), which can provide localized information in frequency and time, and is suitable for processing ECG signals. The principle is to obtain a coefficient matrix at each scale by scaling (diffusion or contraction) and displacing (translation in time) the wavelet function. The matrix contains the characteristics of the signal in different frequency ranges. Each layer of the feature matrix represents different frequency sizes, and different frequencies are assigned different weight coefficients through the channel attention SENet module, thereby obtaining a frequency-time feature with more reference significance. In the present invention, the input ECG data size is (B, 240, 1), and a data matrix of size (B, 64, 240) is obtained after CWT transformation, and then different weight coefficients are assigned to different channels (64 represents the number of channels) through SENet, thereby obtaining a new frequency domain feature.
[0055] like Figure 4 As shown, the discrete Fourier transform DFT decomposes the input ECG signal into a weighted sum of sinusoidal waves of different frequencies. That is, assuming you have a discrete sequence signal x[n] of length N, DFT converts this signal to the frequency domain to obtain its amplitude and phase information at different frequencies. Combining the amplitude and phase information can obtain the amplitude at different frequencies, thereby obtaining the corresponding relationship between amplitude and frequency. In the present invention, the input ECG data size is (B, 240, 1) and after DFT transformation, two vector features of equal size are obtained, namely, an amplitude vector and a frequency vector of size (240,). After that, after a fusion (weighted summation) operation, the amplitude and frequency information are contained in each time step, which also obtains a new frequency domain feature.
[0056] The two different forms of frequency domain features obtained above are integrated into the time domain features obtained by the main branch at different times by adding the corresponding element positions of feature representations of the same size, thereby achieving the effect of enhancing the time domain features.
[0057] 2. Classification based on ResNetECG network
[0058] ResNetECG is a residual network used for classification tasks. The end of the network is a fully connected layer. The number of nodes represents the number of categories of the ECG dataset, that is, the enhanced time domain features are classified through the network.
[0059] like Figure 5 As shown in the figure, the input data size is known to be (B, 1, 240, 64), where B represents the batch, 1 represents the number of channels, and the size is 240x64. The first convolution layer is the input layer, which aims to extract low-level features. It uses a larger convolution kernel (7x7) and a two-dimensional convolution with a stride of 2. Then, it undergoes BatchNorm2d batch normalization and Relu activation function, and finally becomes a feature representation suitable for input to the ResNet residual block. Next is the residual block structure, which has a total of 4 residual blocks. Multiple residual blocks are used to gradually extract deeper features. The difference between the residual blocks is the number of channels, which are 64, 128, 256, and 512, respectively. Each residual block consists of two convolutional layers, a batch normalization layer (Batch Normalization), an activation function (ReLU), and a residual connection. Each residual block first performs a convolution operation, then performs batch normalization and activation, and finally adds the input to the convolution output. In the residual connection, in order to ensure that the feature representations under different branches can be added later, a convolution operation with a convolution kernel of 1x1 and a batch normalization operation are added to the shortcut branch. After the residual structure, the spatial dimension of each feature map is compressed into a single value using global average pooling GlobalAveragePooling2D. The last structure is a simple fully connected layer linear, which also represents the output layer. Its function is to combine the features obtained after convolution and pooling through the fully connected layer, and finally map them to the target category. In the present invention, the output category is 6.
[0060] Before using the sequence model based on RNN and Mamba of the present invention for model training, some hyperparameters need to be set, specifically as follows: batch size is 20, initial learning rate is 0.001, learning rate decrement cycle is 30, GPU is used for training, data set input path, model training result saving path, etc. The deep learning method of the present invention is implemented based on the pytorch framework, and the tool is the integrated development environment pycharm. The experiment was carried out on a single GPU, and the operating system was Ubuntu.
[0061] Through experiments, the classification results are shown in Table 1.
[0062] Table 1
[0063] Epochs Training set accuracy Test set accuracy 50 96.9457 96.4699 100 97.3178 96.9907
[0064] It can be seen from the training set accuracy values recorded in Table 1 that the method extracted by the present invention can realize the classification of ECG data, and the test set accuracy values recorded in Table 1 illustrate that this method has excellent classification performance and can correctly classify most ECG data.
[0065] The above shows and describes the basic principles, main features and advantages of the present invention. Those skilled in the art should understand that the present invention is not limited by the above embodiments. The above embodiments and descriptions are only preferred examples of the present invention and are not intended to limit the present invention. Without departing from the spirit and scope of the present invention, the present invention may have various changes and improvements, which fall within the scope of the present invention. The scope of protection of the present invention is defined by the attached claims and their equivalents.
Claims
1. An ECG data classification method based on RNN and Mamba sequence model, characterized in that: The steps include: S1. Obtain an ECG data set and annotate the data therein; S2. Use the Encoder to construct the feature expression of the ECG signal and add the position code; S3, extracting time domain features through the main branch based on RNN and Mamba sequence model; S4, obtaining frequency domain features and enhancing time domain features through auxiliary branches; S5. Classify the enhanced time domain features based on ResNetECG.
2. The ECG data classification method based on RNN and Mamba sequence model as claimed in claim 1, characterized in that: In step S1, the ECG data set is a public data set downloaded from the PhysioNet website, and the data in the data set are marked using a python script.
3. The ECG data classification method based on RNN and Mamba sequence model as claimed in claim 1, characterized in that: In the step S2, the ECG signal is preliminarily processed based on the Encoder encoder, the preliminary features of the input signal are extracted, a potential feature representation is generated, and then a position code is added to the potential feature table to inject absolute or relative position information.
4. The ECG data classification method based on RNN and Mamba sequence model as claimed in claim 1, characterized in that: In the step S3, the time domain features of the signal are obtained based on the timing of the ECG signal through the RNN and Mamba sequence models. The RNN module obtains the short-term dependency features of the signal and performs weighted combination of the short-term features between each time step through the Self-Attention mechanism to obtain weighted short-term features; the Mamba module obtains the long-term dependency features of the signal by processing the weighted short-term features, thereby obtaining a time domain feature representation containing both short-term feature information and long-term feature information.
5. The ECG data classification method based on RNN and Mamba sequence model as claimed in claim 1, characterized in that: In the step S4, the frequency-time information of the signal is obtained by CWT continuous wavelet transform, and the amplitude and frequency information of the signal is obtained by DFT discrete Fourier transform, and the obtained frequency-time information, amplitude and frequency information are integrated into the time domain features obtained by the main branch at different times by adding the positions of corresponding elements represented by features of equal size, thereby enhancing the time domain features.
6. The ECG data classification method based on RNN and Mamba sequence model as claimed in claim 1, characterized in that: In step S5, ResNetECG is used for classification tasks, the end of the network is a FC fully connected layer, and the number of nodes represents the number of categories of the ECG data set.
Citation Information
Patent Citations
Industrial internet prediction method and system based on frequency domain and long and short term feature fusion
CN115907154A
Bearing diagnosis method and system based on variable working condition multi-modal data fusion
CN118332488A
Enhanced electrocardiogram (ECG) classification method based on improved LSTM
CN118845033A
Physiological signal self-supervision representation learning method and system based on time-frequency reconstruction
CN119089378A