# Prognosis Algorithm# Prognosis Algorithm This document explains the **Exponential Moving Average (EMA) + Trend Analysis** prognosis algorithm used for predictive fault detection in the Edge-AI Induction Motor Fault Detection System.This document explains how the TinyML model was trained using **Edge Impulse**, including dataset preparation, feature engineering, model architecture, optimizations, and export steps. This is a GitHub-friendly guideโ€”not a research paper version. ------ ## ๐Ÿ“Š 1. Overview## ๐Ÿ“ 1. Dataset Preparation All raw datasets are located in the project: The prognosis algorithm enables the system to **predict faults BEFORE they occur** by:``` - Smoothing noisy ML classification outputs using EMA - Tracking probability trends over timeData/ - Detecting rising fault probabilities earlyโ”œโ”€โ”€ Data Final.zip - Generating warnings before threshold crossingโ””โ”€โ”€ Motor LAB 2.0 (with load using dynamometer).zip This provides valuable lead time for maintenance actions, preventing unplanned downtime.``` Each dataset contains: ---- Threeโ€‘phase voltage signals - Threeโ€‘phase current signals ## ๐Ÿงฎ 2. Exponential Moving Average (EMA)- Temperature readings - Vibration sensor data ### What is EMA?- Labeled fault states EMA is a type of weighted moving average that gives more weight to recent values while still considering historical data. ### **Classes Used in Training** ### EMA Formula- **Healthy** ```- **Single Phase Fault** EMA_new = ฮฑ ร— value_current + (1 - ฮฑ) ร— EMA_previous- **Bearing Fault** ```- **Overcurrent / Overload** Where:### **Sampling Configuration** - `ฮฑ` (alpha) = smoothing factor (0.0 to 1.0)- **Sampling rate:** 200 Hz - Higher ฮฑ = more responsive to recent changes- **Window size:** 2 seconds (400 samples) - Lower ฮฑ = smoother, less reactive to noise- **7 total channels:** - Voltage A, Voltage B, Voltage C ### Configuration Used - Current A, Current B, Current C ```cpp - Vibration #define EMA_ALPHA 0.3f // 30% weight to new value, 70% to history ```--- ### Benefits## ๐Ÿ” 2. Importing Data Into Edge Impulse - Reduces false positives from noisy sensor data### Steps: - Provides stable trend for analysis1. Create a new Edge Impulse project - Maintains responsiveness to real changes2. Go to **Data Acquisition โ†’ Upload Data** 3. Upload all CSV/JSON/ZIP dataset files ---4. Assign correct labels (Healthy, Phase Fault, etc.) 5. Verify data plots for any anomalies ## ๐Ÿ“ˆ 3. Trend Analysis (Slope Detection) Your project dashboard should now show all classes with balanced sample counts. ### Purpose Detect when fault probability is **consistently rising** toward the fault threshold.--- ### Method: Linear Regression## ๐Ÿ“Š 3. Creating the Impulse The algorithm maintains a circular buffer of recent EMA values and calculates the trend slope using linear regression:Go to **Create Impulse**. ```cpp### Impulse Structure #define TREND_HISTORY_SIZE 10 // Track last 10 EMA values``` ``` [Time Series Data] โ†’ [Spectral Analysis] โ†’ [Neural Network] ### Slope Formula `````` slope = (nร—ฮฃxy - ฮฃxร—ฮฃy) / (nร—ฮฃxยฒ - (ฮฃx)ยฒ) ```### Settings - **Window size:** 2000 ms ### Interpretation- **Window increase:** 2000 ms - **Positive slope** = Rising probability (potential fault developing)- **Frequency:** 200 Hz - **Negative slope** = Decreasing probability (recovering) - **Near-zero slope** = Stable condition--- ---## ๐Ÿงฎ 4. Feature Engineering Edge Impulse automatically extracts: ## โš ๏ธ 4. Early Warning System ### **Timeโ€‘Domain Features** ### Warning Conditions- RMS An early warning is triggered when EITHER:- Mean - Peak 1. **Threshold Warning**: EMA probability is between 50% and 70%- Standard deviation ```cpp #define WARNING_THRESHOLD 0.5f // 50%### **Frequencyโ€‘Domain (FFT) Features** #define FAULT_THRESHOLD 0.7f // 70%- Magnitude spectrum ```- Energy distribution - Harmonic signatures 2. **Trend Warning**: Rising trend persists for multiple cycles AND probability > 30%- Important for: ```cpp - Bearing faults (200โ€“800 Hz range) #define TREND_SLOPE_WARNING 0.02f // Minimum slope to trigger - Phase faults (lowโ€‘frequency distortion) #define PROGNOSIS_CYCLES_WARNING 5 // Consecutive rising cycles needed ```All spectral parameters were tuned using Edge Impulseโ€™s visual FFT preview. ### Cycles-to-Fault Estimation--- When a rising trend is detected, the system estimates how many inference cycles until the fault threshold is crossed: ## ๐Ÿค– 5. Neural Network Model ```cppA compact neural network was chosen to fit within ESP32โ€‘S3 memory constraints. cycles_until_fault = (threshold - current_EMA) / trend_slope ```### Architecture ``` --- Input (FFT + Time features) ## ๐Ÿ”„ 5. Algorithm Flowโ†“ Dense 32 ```โ†“ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”Dense 16 โ”‚ ML INFERENCE COMPLETE โ”‚โ†“ โ”‚ (Every 5 seconds) โ”‚Dense 8 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ†“ โ”‚Dropout 0.2 โ–ผโ†“ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”Softmax Output (4 classes) โ”‚ UPDATE EMA FOR EACH FAULT TYPE โ”‚ โ”‚ - Bearing Fault โ”‚``` โ”‚ - Imbalance Fault โ”‚ โ”‚ - Misalignment Fault โ”‚### Training Config โ”‚ - Overload Fault โ”‚- **Epochs:** 50 โ”‚ - Normal Operation โ”‚- **Batch size:** 32 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜- **Learning rate:** 0.0005 โ”‚- **Loss:** Categorical Crossentropy โ–ผ- **Optimizer:** Adam โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ ADD TO HISTORY BUFFER โ”‚### Achieved Metrics โ”‚ (Circular buffer of last 10 EMA values) โ”‚| Metric | Value | โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜|--------|-------| โ”‚| Accuracy | **94.3%** | โ–ผ| Precision | 0.96 | โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”| Recall | 0.96 | โ”‚ CALCULATE TREND SLOPE โ”‚| F1โ€‘score | 0.96 | โ”‚ (Linear regression on history buffer) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜--- โ”‚ โ–ผ## ๐Ÿ”ง 6. Model Optimization โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”### Quantization Method โ”‚ CHECK WARNING CONDITIONS โ”‚The model was converted to: โ”‚ - Is EMA above warning threshold? โ”‚**TFLite Micro (int8 quantized)**. โ”‚ - Is trend rising consistently? โ”‚ โ”‚ - Estimate cycles until fault threshold โ”‚### Benefits โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜| Feature | Float32 Model | int8 Quantized Model | โ”‚|---------|----------------|------------------------| โ–ผ| Classifier Latency | 24 ms | **4 ms** | โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”| Flash Size | 551 KB | **164 KB** | โ”‚ UPDATE DISPLAY & GENERATE ALERTS โ”‚| RAM Usage | 22.7 KB | **8.2 KB** | โ”‚ - Show EMA bars on prediction screen โ”‚ โ”‚ - Display trend indicators (โ†‘) โ”‚Quantized model chosen for deployment due to faster inference and lower memory footprint. โ”‚ - Show warning banner if applicable โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜--- ``` ## ๐Ÿ“ฆ 7. Exporting the Model ---### Steps 1. Go to **Deployment** in Edge Impulse ## ๐Ÿ–ฅ๏ธ 6. Display Integration2. Select **Arduino Library** 3. Choose **Quantized (int8)** model variant ### Prediction Screen Shows:4. Download `.zip` 1. **Warning Banner** (top) - Color-coded status message5. Extract to your project: 2. **EMA Progress Bars** - Smoothed probability for each fault type``` 3. **Trend Indicators** - Arrow (^) shows rising trends 4. **Cycles Estimate** - Predicted time until fault thresholdlib/ei-model/ ### Color Coding``` | Status | Color | Meaning | |--------|-------|---------|All generated files will be inside: | GREEN | System Normal | No faults detected, normal operation |``` | CYAN | Monitoring | Gathering data, no significant trends | | YELLOW | Warning | Early warning, rising fault probability |lib/ei-model/src/ | RED | Fault | Fault threshold crossed | ``` ---These files are used directly by the firmware. ## ๐Ÿ“Š 7. Example Scenario--- ### Bearing Fault Degradation Test:## ๐Ÿงช 8. Testing the Model ```Using Edge Impulse **Model Testing**, run the test dataset. Cycle 1: Raw=15%, EMA=15%, Slope=0.00, Status: Normal Cycle 2: Raw=18%, EMA=16%, Slope=0.01, Status: NormalExpected output: Cycle 3: Raw=25%, EMA=19%, Slope=0.02, Status: Normal- Accuracy ~94% Cycle 4: Raw=32%, EMA=23%, Slope=0.03, Status: Trend Rising- Confusion matrix showing strong separation among 4 classes Cycle 5: Raw=38%, EMA=27%, Slope=0.03, Status: Trend Rising- Minor confusion only in early fault stages Cycle 6: Raw=45%, EMA=33%, Slope=0.04, Status: โš ๏ธ EARLY WARNING Est. cycles to 70%: ~9 cycles--- Cycle 7: Raw=52%, EMA=39%, Slope=0.04, Status: โš ๏ธ WARNING ...## ๐Ÿš€ 9. Onโ€‘Device Inference Cycle 15: Raw=75%, EMA=71%, Slope=0.02, Status: ๐Ÿ”ด FAULT DETECTEDModel runs on ESP32โ€‘S3 inside the FreeRTOS **TinyML Task**. ``` ### Inference Cycle **Result**: System provided ~9 cycles (~45 seconds) advance warning before fault threshold crossing.1. Collect 400 samples 2. Run feature extraction (FFT + timeโ€‘domain) ---3. Execute TFLM forward pass 4. Display classification result on TFT + Web UI ## โš™๏ธ 8. Configuration Parameters ### Average Inference Latency | Parameter | Value | Description |``` |-----------|-------|-------------| | `EMA_ALPHA` | 0.3 | EMA smoothing factor |287 ms | `TREND_HISTORY_SIZE` | 10 | EMA values tracked for trend | | `WARNING_THRESHOLD` | 0.5 (50%) | Probability for early warning |``` | `FAULT_THRESHOLD` | 0.7 (70%) | Probability for fault detection |Fits easily inside 2โ€‘second sampling window. | `TREND_SLOPE_WARNING` | 0.02 | Min slope for trend warning | | `PROGNOSIS_CYCLES_WARNING` | 5 | Consecutive rising cycles needed |--- ---## ๐Ÿ“ˆ 10. Prognostic Trend Analysis After each inference, Softmax probabilities are passed through: ## ๐Ÿ”ง 9. Code Implementation- **Exponential Moving Average (EMA)** - **Trend slope detection** ### Key Files: - `src/tinyMLStructures.h` - Data structures (FaultEMA, PrognosisResults)Predicts faults **before threshold crossing**. - `src/tinyMLTask.cpp` - EMA update, trend calculation, prognosis logic - `src/globalVariables.cpp` - Global prognosis results storage--- - `src/displayTasks.cpp` - Prediction screen rendering # โœ… Model Training Complete ### Key Functions:This model is ready for deployment via the ESP32โ€‘S3 TinyML runtime. ```cpp void initPrognosis(); // Initialize prognosis system void updatePrognosis(MLFaultResults*); // Main update function void updateFaultEMA(FaultEMA*, float); // Update single fault EMA float calculateTrendSlope(FaultEMA*); // Linear regression on history int estimateCyclesUntilThreshold(...); // Predict time to fault PrognosisResults getPrognosisResults(); // Get current prognosis state ``` --- ## ๐Ÿ 10. Benefits 1. **Early Detection**: Warns about developing faults before threshold crossing 2. **Noise Reduction**: EMA smoothing reduces false positives 3. **Predictive**: Estimates time until maintenance needed 4. **Actionable**: Provides clear visual feedback for operators 5. **Low Overhead**: Runs efficiently on ESP32-S3 microcontroller --- # โœ… Prognosis Algorithm Complete This algorithm enables predictive maintenance capabilities, giving operators valuable lead time to schedule repairs before motor failure occurs.