# raghavsai-tirupati/emg architecture

> Architecture diagram of the GitHub repository raghavsai-tirupati/emg, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/raghavsai-tirupati/emg
- Repository: https://github.com/raghavsai-tirupati/emg

## Overview

This firmware recognizes five hand gestures from forearm EMG and reports the label, confidence, and features over BLE and serial. Its principal workflow is 1 kHz acquisition, 256-sample window processing, feature extraction, and nearest-centroid classification. A separate control task supports serial-driven calibration, with the model persisted in NVS. The README documents these runtime stages, but the main sketch was not sampled, so internal task wiring beyond documented responsibilities is not inferred.

Main entry points: [`emg_firmware/emg_firmware.ino`](https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/emg_firmware.ino) (Feature extraction), [`emg_firmware/classifier.h`](https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/classifier.h) (Gesture classifier), [`emg_firmware/fft.h`](https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/fft.h) (Radix-2 FFT)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_acquisition["Signal acquisition"]
  node_sampler["Sampler task<br/>[emg_firmware.ino]"]
  node_timer["Sampling timer ISR<br/>[emg_firmware.ino]"]
  node_windows["Sample windows<br/>[emg_firmware.ino]"]
end

subgraph group_recognition["Gesture recognition"]
  node_dsp["DSP task<br/>[emg_firmware.ino]"]
  node_features["Feature extraction<br/>[emg_firmware.ino]"]
  node_fft["Radix-2 FFT<br/>[fft.h]"]
  node_classifier["Gesture classifier<br/>[classifier.h]"]
end

subgraph group_interaction["Calibration and output"]
  node_control["Control task<br/>[emg_firmware.ino]"]
  node_calibration["Calibration model<br/>[classifier.h]"]
  node_ble["BLE task<br/>[emg_firmware.ino]"]
end

node_sensor(("MyoWare sensor"))
node_serial(("Serial console"))
node_client(("BLE client"))
node_nvs[("NVS storage")]

node_sensor -->|"provides signal"| node_sampler
node_timer -->|"clocks sampling"| node_sampler
node_sampler -->|"fills windows"| node_windows
node_windows -->|"supplies samples"| node_dsp
node_dsp -->|"extracts features"| node_features
node_features -->|"computes spectrum"| node_fft
node_features -->|"classifies vector"| node_classifier
node_control -->|"reads commands"| node_serial
node_control -->|"collects samples"| node_calibration
node_calibration -->|"provides model"| node_classifier
node_classifier -->|"saves or loads"| node_nvs
node_ble -->|"notifies results"| node_client

click node_sampler "https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/emg_firmware.ino"
click node_timer "https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/emg_firmware.ino"
click node_windows "https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/emg_firmware.ino"
click node_dsp "https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/emg_firmware.ino"
click node_features "https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/emg_firmware.ino"
click node_fft "https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/fft.h"
click node_classifier "https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/classifier.h"
click node_control "https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/emg_firmware.ino"
click node_calibration "https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/classifier.h"
click node_ble "https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/emg_firmware.ino"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_sampler,node_timer,node_windows,node_client toneBlue
class node_dsp,node_features,node_fft,node_classifier,node_nvs toneAmber
class node_control,node_calibration,node_ble toneMint
class node_sensor,node_serial toneIndigo
```

## Components

### Signal acquisition

- [Sampler task](https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/emg_firmware.ino): `emg_firmware/emg_firmware.ino`
- [Sampling timer ISR](https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/emg_firmware.ino): `emg_firmware/emg_firmware.ino`
- [Sample windows](https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/emg_firmware.ino): `emg_firmware/emg_firmware.ino`

### Gesture recognition

- [DSP task](https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/emg_firmware.ino): `emg_firmware/emg_firmware.ino`
- [Feature extraction](https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/emg_firmware.ino): `emg_firmware/emg_firmware.ino`
- [Radix-2 FFT](https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/fft.h): `emg_firmware/fft.h`
- [Gesture classifier](https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/classifier.h): `emg_firmware/classifier.h`

### Calibration and output

- [Control task](https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/emg_firmware.ino): `emg_firmware/emg_firmware.ino`
- [Calibration model](https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/classifier.h): `emg_firmware/classifier.h`
- [BLE task](https://github.com/raghavsai-tirupati/emg/blob/main/emg_firmware/emg_firmware.ino): `emg_firmware/emg_firmware.ino`

### Other components

- **MyoWare sensor**
- **Serial console**
- **BLE client**
- **NVS storage**

## Connections

- MyoWare sensor → Sampler task: provides signal
- Sampling timer ISR → Sampler task: clocks sampling
- Sampler task → Sample windows: fills windows
- Sample windows → DSP task: supplies samples
- DSP task → Feature extraction: extracts features
- Feature extraction → Radix-2 FFT: computes spectrum
- Feature extraction → Gesture classifier: classifies vector
- Control task → Serial console: reads commands
- Control task → Calibration model: collects samples
- Calibration model → Gesture classifier: provides model
- Gesture classifier → NVS storage: saves or loads
- BLE task → BLE client: notifies results
