Edge AI AFib Detection: From Research to Reality

About the Project

This project, Edge AI AFib Detection, demonstrates a novel approach to detecting Atrial Fibrillation (AFib) directly on an embedded device. By leveraging the STMicroelectronics B-L475E-IOT01A2 Discovery kit (powered by an Arm Cortex-M4 processor), we have successfully ported a complex Deep Learning model to run entirely on the edge.

The system is designed to function as a wearable-class medical device, capable of analyzing heart rate variability (RR intervals) in real-time without relying on cloud processing. To facilitate testing and demonstration, the project includes a custom Flutter-based mobile application that mimics commercial heart rate sensors (like the Polar H10), injecting clinical test data into the device via Bluetooth Low Energy (BLE).

Inspiration

The core inspiration for this project comes from the research paper:

Atrial Fibrillation Detection on the Embedded Edge: Energy-Efficient Inference on a Low-Power Microcontroller
Yash Akbari, et al.
Sensors (MDPI), 2025

The goal was to bridge the gap between theoretical deep learning research and practical, life-saving embedded implementations. Traditional AFib detection often requires heavy computation or cloud connectivity, which introduces latency and privacy concerns. We wanted to prove that complex LSTM (Long Short-Term Memory) networks could be optimized sufficiently to run on standard, low-power IoT microcontrollers.

How We Built It

The project architecture consists of two main components:

  1. The Edge Device (Firmware):

    • Built on the STM32L475 microcontroller.
    • Uses X-CUBE-AI to convert and optimize the Keras/TensorFlow model for the Arm Cortex-M4.
    • Implements a custom BLE GATT profile to receive data.
  2. The Simulator (Mobile App):

    • Developed in Flutter.
    • Acts as a data injector, streaming pre-recorded RR interval sequences to the board.
    • Receives and displays the inference results (Normal vs. AFib) in real-time.

The Mathematical Model

The core of the detection logic relies on analyzing the sequence of RR intervals. If we define the time of the $i$-th heartbeat as $t_i$, the RR interval $RR_i$ is defined as:

$$ RR_i = t_{i+1} - t_i $$

The LSTM network processes a sliding window of these intervals:

$$ X = [RR_{t}, RR_{t-1}, \dots, RR_{t-n}] $$

Where $X$ is the input vector fed into the neural network to predict the probability $P(y)$ of Atrial Fibrillation.

Challenges & Learnings

Our journey was not without significant hurdles. The primary challenge was the resource constraint of the microcontroller.

  • The TFLM Roadblock: We initially attempted to use TensorFlow Lite for Microcontrollers (TFLM). However, we quickly discovered that TFLM lacked support for Bi-directional LSTMs, which were crucial to our original model's accuracy. Even when attempting to simplify the model to a unidirectional variant, we faced severe RAM allocation issues that caused the system to crash.

  • The X-CUBE-AI Solution: This forced us to pivot and explore ST's X-CUBE-AI. This tool proved to be the game-changer. It successfully mapped our LSTM model to the device's memory map, optimizing the weights and activation buffers to fit within the limited RAM of the STM32L4.

Through this process, we learned the critical importance of hardware-aware model design. It is not enough to just train a model with high accuracy; one must constantly validate that the architecture is compatible with the target embedded runtime.

Built With

Share this project:

Updates