Lightweight arrhythmia electrocardiosignal classification method deployed on stm32

By designing a lightweight convolutional neural network and combining model quantization and pruning techniques, an ECG signal classification model was deployed on an STM32 device, solving the computation and storage requirements for ECG signal classification on edge devices. This enabled fast and accurate arrhythmia classification, meeting the real-time diagnostic needs of low-power devices.

CN123138928APending Publication Date: 2026-11-20PUTIAN UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611402943.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-09-10
Publication Date
2026-11-20

AI Technical Summary

Technical Problem

Existing ECG signal classification methods suffer from high computational resource and memory storage requirements, insufficient model complexity and interpretability when deployed on low-power edge devices. Furthermore, traditional models are limited in performance when data quality is poor, making it difficult to achieve fast and accurate classification.

Method used

A lightweight convolutional neural network model was designed, combining model quantization and pruning techniques, and converted into a C language embedded executable file format. It was then deployed on an STM32 device. Through algorithm-device co-design, real-time classification of ECG signals of arrhythmias was achieved.

Benefits of technology

Fast and accurate five-class classification of arrhythmic ECG signals was achieved on STM32 devices, reducing computation and storage requirements, improving the real-time inference speed and classification accuracy of the model, and meeting the low latency requirements of edge devices.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123138928A_ABST
    Figure CN123138928A_ABST
Patent Text Reader

Abstract

The present application relates to a kind of light weight arrhythmia electrocardiosignal classification method deployed on STM32.First, by designing a light weight neural network consisting of only convolution layer, pooling layer and fully connected layer, for electrocardiosignal classification.Then, the parameter scale is reduced by using model quantization and pruning techniques, improve real-time inference speed while maintaining the classification accuracy as much as possible.Finally, the classification model trained on PyTorch framework is converted into C language embedded executable file format supported by edge device, to deploy on edge device STM32 to classify five common arrhythmia signal classification tasks.The present application can monitor and accurately classify electrocardiosignal in real time on edge device, and can be applied to mobile health software, smart bracelet, smart electrocardiosignal clothing, smart jewelry and other wearable electrocardiosignal devices, which is of great significance for the prevention and treatment of cardiovascular diseases.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of electrocardiogram (ECG) signal processing technology, and more specifically to a lightweight method for classifying ECG signals of arrhythmias deployed on an STM32 microcontroller. Background Technology

[0002] Excessive stress and unhealthy lifestyle habits in modern life can easily lead to emotional fluctuations, causing various heart diseases such as premature beats or tachycardia. Arrhythmia is a typical cardiovascular disease, mainly manifested as abnormal heart rate or rhythm. Normal heart rate is generally recorded as (N). Common types of arrhythmias include atrial premature beats (A), ventricular premature beats (V), left bundle branch block (L), and right bundle branch block (R). Arrhythmias often occur suddenly, and if not treated promptly, can lead to death. Fast and accurate ECG signal classification models for arrhythmias can be applied to portable / wearable (P / W) ECG monitors, helping doctors provide timely guidance and prevention. Therefore, ECG signal classification is crucial. Arrhythmia signal classification is one technique for studying ECG signal processing.

[0003] In recent years, researchers have proposed a series of electrocardiogram (ECG) signal classification methods. These methods can be broadly categorized into traditional machine learning methods and deep learning-based methods. Traditional machine learning methods have high requirements for both the quality and quantity of data; therefore, when the captured ECG data is of poor quality or insufficient, the performance of these algorithms may be limited. Furthermore, the complex neural network structures in these models pose challenges to their reliability and interpretability. Deep learning-based methods typically utilize complex model structures to improve classification accuracy, but they tend to neglect real-time performance and efficiency. Simultaneously, as the model size increases, the demands on computing resources and memory storage also rise, making deployment on low-power edge devices difficult. Therefore, developing lightweight, reliable, and accurate classification models that can be deployed on edge devices to optimize ECG signal classification for arrhythmias will become a future trend.

[0004] However, simply designing lightweight networks for deployment on edge devices is far from sufficient; model compression techniques are needed to further reduce model size. Model compression techniques mainly include quantization and pruning, which can significantly reduce the number of parameters and memory requirements, but may also lead to information loss, performance degradation, and reduced model interpretability. Therefore, choosing an appropriate compression method and performing precise tuning and validation to maintain model performance while significantly reducing storage requirements is crucial. Summary of the Invention

[0005] The purpose of this invention is to design a method for classifying arrhythmia electrocardiogram (ECG) signals that can be deployed on STM32 edge devices. This method can accurately classify ECG signals into five categories: normal heart rate (N) and common arrhythmia types, including atrial premature beats (A), ventricular premature beats (V), left bundle branch block (L), and right bundle branch block (R). It is suitable for real-time classification of ECG signals recorded by clinical subjects. By using lightweight networks and model quantization and pruning techniques, this invention can efficiently classify arrhythmia signals on edge devices, providing real-time diagnostic and analytical support for monitoring cardiac health anytime, anywhere.

[0006] To achieve the above objectives, the technical solution of this invention is: an algorithm-device co-design method for ECG signal classification of arrhythmias, deployable on edge devices. First, a simple, lightweight convolutional neural network model is designed for ECG signal classification. Next, model quantization and pruning are used to reduce the model parameter size, improving real-time inference speed while maintaining classification accuracy. Finally, the classification model is successfully deployed on an STM32 edge device using a method that converts the model trained on the PyTorch framework into a C-language-based embedded executable file format.

[0007] In one embodiment of the present invention, the specific implementation process of designing a simple and lightweight convolutional neural network model for ECG signal classification is as follows:

[0008] The classification model contains only two convolutional layers for feature extraction, one max-pooling layer for downsampling, and two fully connected layers for ECG signal classification. The kernel sizes of the two convolutional layers are 1 and 1 respectively. 3 and 1 5. The stride is always 2. This process can be defined as:

[0009]

[0010] in, Indicates the first The output of a convolutional layer. t represents the length of the input vector x, and k represents the kernel size. Let x be the (j+i)th element. Let and represent the weight and bias of the i-th element, respectively. It's worth noting that for the output of the first convolutional layer, we applied max pooling with a window size of 1. 3. The stride is 2. This process downsamples the features, thereby reducing the dimensionality of the input features and improving the computational efficiency of the model.

[0011] After obtaining the high-dimensional features extracted by the convolutional and pooling layers, they are planarized into one-dimensional feature vectors. These vectors are then fed into the first fully connected layer, which consists of 152 nodes. Next, a ReLU layer is used to enhance the network's non-linear expressive power. Finally, the features are fed into a second fully connected layer to obtain the classification result. The process of the fully connected layer can be represented as:

[0012]

[0013] in, This represents the output of the l-th fully connected layer. Similarly, This represents the i-th element of the input vector x. b and b represent the weight and bias of the i-th element, respectively.

[0014] In one embodiment of the present invention, the specific implementation process of reducing the model parameter size by using model quantization and pruning is as follows:

[0015] Quantization converts model parameters into a lower-precision representation, significantly reducing computational and storage costs while maintaining the model's original accuracy. In this invention, the ECG data format for arrhythmias is first converted from FP32 to Int16 as input. Then, quantization is performed on the first convolutional layer to accelerate computation. Finally, to maintain the accuracy of gradient update inference results, dequantization is performed after inference, converting the Int16 format weights back to FP32 format. This quantization strategy provides an effective solution for the fast and accurate training of classification network models on resource-constrained devices.

[0016] Furthermore, to further reduce the computational burden on edge devices, this invention employs an L1-norm-based pruning method to prune the second convolutional layer and the first fully connected layer of the network model, with a pruning ratio set to 0.4. First, the weights of the convolutional or fully connected layers are flattened into a one-dimensional weight vector. Then, the L1 norm of w is calculated using the following formula:

[0017]

[0018] in, Let n represent the L1 norm applied to the k-th layer. These represent the length of the weight w in the current layer and its i-th element, respectively.

[0019] Subsequently, based on the L1 norm Compared to the initial pruning ratio Further calculations determine the pruning threshold for the k-th layer. The specific formula is as follows:

[0020]

[0021] Finally, after obtaining the pruning threshold, the absolute value of each weight parameter vector is calculated and compared. If the absolute value of a weight element is less than or equal to the pruning threshold, it is set to zero. Since the weights set to zero no longer participate in subsequent model training, there is no need to calculate the corresponding weight update operation during backpropagation, greatly reducing the computational burden of model training.

[0022] In one embodiment of the present invention, the method further utilizes a method for converting a model trained on the PyTorch framework into an embedded executable file format based on the C language, thereby successfully deploying the classification model onto the STM32 edge device. The specific implementation process is as follows:

[0023] Since the STM32 microcontroller only supports some low-level implementation languages, it is necessary to convert the model trained using the PyTorch framework into an embedded executable file format expressed in C language. Taking convolution calculation as an example, the process of converting the convolutional layer operations encoded in the PyTorch framework into fixed-point computation functions supported by STM32 is as follows:

[0024] First, the input data includes weights, features, and kernel size in the PyTorch framework.

[0025] Secondly, the specific conversion steps can be described as follows:

[0026] Step S1: For the index N of the input feature, iterate through it sequentially from 1 to sizeof(feature):

[0027] (1) Initialize the variable value to 0;

[0028] (2) For the convolution kernel index M, iterate sequentially from 1 to kernel_size and update it according to the following formula:

[0029] value=value + weights[M]* features[N+M];

[0030] (3) Store the current calculation result, value, into the result array results[N];

[0031] Step S2: After traversing all input features, the output of the convolutional layer is obtained.

[0032] Finally, output the complete convolution calculation function code expressed in C language.

[0033] During the deployment of STM32 edge devices, the converted lightweight C language classification network model is stored in the STM32's Flash memory, while the Int16 format ECG data (MIT-BIH dataset) and the weights of the scaled-down model are stored on an SD card connected to the STM32. During inference, the STM32 first reads the pre-stored ECG data and model weights from the SD card and then calls the converted lightweight C language network program for real-time inference calculations, outputting the arrhythmia classification results via a serial interface. Comparison of the output results with those pre-calculated on a PC using PyTorch shows that, despite the hardware resource limitations of the microcontroller, the converted C language program can still achieve almost the same inference results on the STM32 as the PyTorch version. Furthermore, compared to the PyTorch version, the converted program significantly improves operational efficiency. This improvement enables it to meet the low-latency requirements of edge devices.

[0034] In summary, the technical effects and advantages of this invention are reflected in:

[0035] (1) Compared with existing classification methods, the classification neural network of the present invention has a simple structure, small model, low computational and memory requirements, and is an algorithm-device co-design framework with advantages such as high classification accuracy and low power consumption.

[0036] (2) By using quantization and pruning techniques, the parameter size of the model is reduced to a great extent while ensuring the classification accuracy of the model.

[0037] (3) The lightweight model is deployed on STM32 to achieve a fast and accurate five-class classification task of arrhythmia ECG signals. The whole process is highly practical. Attached Figure Description

[0038] Figure 1 This is an overall flowchart of the ECG signal classification method for arrhythmia described in this invention.

[0039] Figure 2 This is a diagram of the lightweight convolutional neural network structure for ECG signal classification of arrhythmias described in this invention.

[0040] Figure 3 This is a diagram illustrating the specific process of model quantization and pruning described in this invention.

[0041] Figure 4 This diagram illustrates the specific process of deploying the model described in this invention to an STM32 edge device.

[0042] Figure 5 This is a test graph showing the impact of different random seeds on the classification accuracy of the ECGCNet classification network described in this invention. Detailed Implementation

[0043] The technical solution of the present invention will now be described in detail with reference to the accompanying drawings.

[0044] like Figure 1 As shown, this invention provides a method for classifying electrocardiogram (ECG) signals for arrhythmias that can be deployed on edge devices. First, a lightweight neural network consisting only of simple convolutional layers, pooling layers, and fully connected layers is designed for ECG signal classification. Next, model quantization and pruning techniques are used to reduce the parameter size, improving real-time inference speed while maintaining classification accuracy as much as possible. Finally, the classification model trained on the PyTorch framework is converted into a C language embedded executable file format supported by edge devices for deployment on STM32 edge devices for classification.

[0045] like Figure 2 As shown, this invention designs a lightweight network model with a simple structure to achieve efficient classification. It contains only two convolutional layers for feature extraction, one max-pooling layer for downsampling, and two fully connected layers for ECG signal classification. The specific implementation process is as follows:

[0046] Two convolutional layers are used for feature extraction from the ECG signal, and their kernel sizes are 1 and 1 respectively. 3 and 1 5. The stride length is always 2. This process can be defined as follows:

[0047]

[0048] in, Indicates the first The output of a convolutional layer. t represents the length of the input vector x, and k represents the kernel size. Let x represent the j+i elements. Let and represent the weight and bias of the i-th element, respectively. It's worth noting that for the output of the first convolutional layer, we applied max pooling with a window size of 1. 3. The stride is 2. This process downsamples the features, thereby reducing the dimensionality of the input features and improving the computational efficiency of the model.

[0049] After obtaining the high-dimensional features extracted by the convolutional and pooling layers, they are planarized into one-dimensional feature vectors. These vectors are then fed into the first fully connected layer, which consists of 152 nodes. Next, a ReLU layer is used to enhance the network's non-linear expressive power. Finally, the features are fed into a second fully connected layer to obtain the classification result. The process of the fully connected layer can be represented as:

[0050]

[0051] in, This represents the output of the l-th fully connected layer. Similarly, This represents the i-th element of the input vector x. b and b represent the weight and bias of the i-th element, respectively.

[0052] like Figure 3 As shown, this invention employs a model quantization and pruning process.

[0053] Quantization converts model parameters into a lower-precision representation, significantly reducing computational and storage costs while maintaining the model's original accuracy. In this invention, the ECG data format for arrhythmias is first converted from FP32 to Int16 as input. Then, quantization is performed on the first convolutional layer to accelerate computation. Finally, to maintain the accuracy of gradient update inference results, dequantization is performed after inference, converting the Int16 format weights back to FP32 format. This quantization strategy provides an effective solution for the fast and accurate training of classification network models on resource-constrained devices.

[0054] Furthermore, to further reduce the computational burden on edge devices, this invention employs an L1-norm-based pruning method to prune the second convolutional layer and the first fully connected layer of the network model, with a pruning ratio set to 0.4. First, the weights of the convolutional or fully connected layers are flattened into a one-dimensional weight vector. Then, the L1 norm of w is calculated using the following formula:

[0055]

[0056] in, Let n represent the L1 norm applied to the k-th layer. These represent the length of the weight w in the current layer and its i-th element, respectively.

[0057] Subsequently, based on the L1 norm Compared to the initial pruning ratio Further calculations determine the pruning threshold for the k-th layer. The specific formula is as follows:

[0058]

[0059] Finally, after obtaining the pruning threshold, the absolute value of each weight parameter vector is calculated and compared. If the absolute value of a weight element is less than or equal to the pruning threshold, it is set to zero. Since the weights set to zero no longer participate in subsequent model training, there is no need to calculate the corresponding weight update operation during backpropagation, greatly reducing the computational burden of model training.

[0060] In one embodiment of the present invention, the method further utilizes a method for converting a model trained on the PyTorch framework into an embedded executable file format based on the C language, thereby successfully deploying the classification model onto the STM32F103 edge device. The specific implementation process is as follows:

[0061] Since microcontrollers (such as STM32) only support some low-level implementation languages, it is necessary to convert models trained using the PyTorch framework into embedded executable file formats expressed in C language. Taking convolution calculation as an example, the process of converting convolutional layer operations encoded in the PyTorch framework into fixed-point computation functions supported by STM32F103 is as follows:

[0062] Algorithm 1: Convolution Calculation Process for STM32F103 Input: weights, features, kernel size Output: The converted result Step S1: For the input feature index N, iterate from 1 to sizeof(feature) in sequence: (1) Initialize the variable value to 0; (2) For the convolution kernel index M, iterate from 1 to kernel_size in sequence and update according to the following formula: value=value + weights[M]* features[N+M]; (3) Store the current calculation result value into the result array results[N]; Step S2: After completing the traversal of all input features, obtain the output result of the convolution layer.

[0063] like Figure 4 The diagram illustrates the specific process of deploying the method of this invention to an STM32 edge device. To verify the effectiveness of model maintenance after deployment, the converted lightweight C language classification network program was deployed to a microcontroller (STM32 platform) for real-time inference classification. The STM32 mainly consists of Flash memory and an SD card. The Flash memory is used to store the converted C language-based network model, and the connected SD card is used to store Int16 format ECG data (MIT-BIH) and the weight parameters of the reduced model. During the verification process, the test program first reads the pre-stored data and model weights from the SD card. Then, it calls the converted C language-based model code to perform real-time inference calculations on the microcontroller. The calculation results are output through the microcontroller's serial interface and monitored in real-time using the development board's debugging software. Finally, by comparing the output results with the results pre-calculated on a PC using PyTorch, the results show that despite the limitations of the microcontroller's hardware resources, the converted C language-based program can still obtain almost the same inference results on the STM32F103 as under the PyTorch framework. Furthermore, compared to the PyTorch version, the converted program exhibits significantly improved performance. This improvement enables it to meet the low-latency requirements of edge devices.

[0064] To verify the accuracy of our proposed lightweight ECG signal classification method for arrhythmias (EEGCNet), which can be deployed on STM32, we quantitatively compared it with other high-level classification algorithms on the MIT-BIH benchmark dataset. The algorithms compared included Random Forest, Ensemble Subspace Discriminant, and Support Vector Machines (SVMs) with different kernels (linear, quadratic, and cubic kernels). As shown in Table 1, compared with state-of-the-art algorithms, our ECGCNet consistently outperforms all algorithms in classification accuracy, ranking first with a 0.41% improvement. Simultaneously, our method also boasts high time efficiency. Table 1 demonstrates that our lightweight ECG signal classification method for arrhythmias achieves a good balance between accuracy and time efficiency through scientifically sound quantization and pruning.

[0065] Table 1: Test results of ECGCNet and different ECG signal classification algorithms on the MIT-BIH dataset.

[0066] method Overall accuracy (%) Time (ns) RF 97.92 572 ESD 89.58 59 SVM Linear 97.06 41683 SVM Quadratic 93.75 29698 SVM Cubic 93.75 32193 ECGCNet 98.33 992

[0067] like Figure 5 As shown, this invention uses different random seeds to test the classification accuracy of the lightweight classification network (ECGCNet). To evaluate the stability and reliability of the lightweight ECG signal classification method for arrhythmias that can be deployed on STM32, we conducted more than 10 experiments with different random seeds. This avoids the biased results that may be introduced by using pseudo-random numbers. Figure 5 As shown, the overall accuracy is minimally affected by different random seeds and consistently remains above 98%. This indicates that the proposed method possesses good stability and reliability.

[0068] The above are preferred embodiments of the present invention. Any changes made to the technical solution of the present invention that do not exceed the scope of the technical solution of the present invention shall fall within the protection scope of the present invention.

Claims

1. A lightweight method for classifying arrhythmic electrocardiogram signals deployed on STM32, characterized in that, A lightweight network model for classifying ECG signals of arrhythmias was built based on the PyTorch framework. This lightweight network model is used to extract features and classify input ECG signals. Quantization and pruning techniques are applied to the lightweight network model to further reduce its storage and computational overhead. The shrunk lightweight network model is then converted into a C language embedded executable program suitable for running on STM32 and successfully deployed on resource-constrained STM32 devices to achieve real-time classification of ECG signals of arrhythmias.

2. The lightweight arrhythmia ECG signal classification method deployed on STM32 according to claim 1, characterized in that, The lightweight network model includes a first convolutional layer for feature extraction, a max-pooling layer for downsampling, a second convolutional layer for further feature extraction, and two fully connected layers for ECG signal classification. The two convolutional layers are used for ECG signal feature extraction, and their kernel sizes are 1... 3 and 1 5. The stride is 2; the calculation process of the convolution can be expressed as: in, Indicates the first The output of a convolutional layer, where t represents the length of the input vector x and k represents the kernel size. This represents the (j+i)th element of x. b and represent the weight and bias of the i-th element, respectively; the max pooling layer is used to downsample the output of the first convolutional layer, and its pooling window size is 1.

3. The stride is 2; the features extracted by the convolutional layer and max pooling layer are flattened into a 152-dimensional one-dimensional feature vector and input into the first fully connected layer, which contains 16 neurons. Its output is then processed non-linearly by a ReLU activation layer and input into the second fully connected layer, which contains 5 neurons and outputs the 5-class classification result of the arrhythmia ECG signal. The calculation process of the fully connected layer can be represented as follows: in, This represents the output of the l-th fully connected layer. This indicates the number of elements in the input vector. This represents the i-th element of the input vector x. b and b represent the weight and bias of the i-th element, respectively.

3. The lightweight arrhythmia ECG signal classification method deployed on STM32 according to claim 1, characterized in that, The model quantization and pruning techniques include converting the input arrhythmia ECG data from FP32 format to Int16 format, and quantizing the first convolutional layer of the lightweight network model to reduce storage and computational overhead during model inference. Simultaneously, to maintain the accuracy of gradient update inference results, dequantization is performed after inference, converting the Int16 format weights back to FP32 format. This quantization strategy provides an effective solution for fast and accurate training of classification network models on resource-constrained devices. The second convolutional layer and the first fully connected layer of the lightweight classification network model are pruned using an L1 norm-based pruning method, with a pruning ratio of 0.

4. During pruning, the weight parameters of the layer to be pruned are flattened into a one-dimensional weight vector, and the L1 norm of the weight vector is calculated according to the following formula: in, Let n represent the L1 norm applied to the k-th layer, and n and n' be the norm of the layer. Let w represent the length of the current layer's weight w and its i-th element, respectively; then, based on the L1 norm and the preset pruning ratio... Calculate and determine the pruning threshold for the k-th layer. The specific formula is as follows: Finally, weight parameters whose absolute values ​​are less than or equal to the pruning threshold are set to zero to reduce the number of weight parameters involved in subsequent calculations and the computational burden on the model.

4. The lightweight arrhythmia ECG signal classification method deployed on STM32 according to any one of claims 1 to 3, characterized in that, The process of converting a lightweight classification network model implemented based on the PyTorch framework into a C language embedded executable program includes converting the network operation format under the PyTorch framework into fixed-point computation functions supported by STM32. Taking the conversion process of convolution computation as an example, the data read includes the parameters (weights), features, and kernel size under the PyTorch framework. The specific steps are described as follows: Step S1: For the index N of the input feature, iterate through it sequentially from 1 to sizeof(feature): (1) Initialize the variable value to 0; (2) For the convolution kernel index M, iterate sequentially from 1 to kernel_size and update it according to the following formula: value=value + weights[M]* features[N+M]; (3) Store the current calculation result, value, into the result array results[N]; Step 2: After completing the traversal of all input features, the calculation results of the convolutional layer are stored in the result array; Finally, the converted lightweight C language classification network program is stored in the Flash memory of the STM32, and the ECG data and model weight parameters in Int16 format are stored in the SD card of the STM32. The STM32 reads the pre-stored ECG data and model weight parameters from the SD card, calls the lightweight C language classification network program to perform inference calculations, and outputs the classification results of arrhythmia ECG signals through the serial port.