Skip to content

Latest commit

 

History

History
253 lines (184 loc) · 8.73 KB

File metadata and controls

253 lines (184 loc) · 8.73 KB

Real-Time Anomaly Detection in C for Bearing Vibration Monitoring

This repository is my student project on real-time anomaly detection for rotating machinery using vibration signals. I built it to show my understanding of digital signal processing, signal analysis, and statistical signal processing in a practical and interpretable way.

Instead of starting with a black-box machine learning model, this project focuses on a transparent DSP/SSP pipeline written in C. The idea is to take vibration samples, process them step by step, extract meaningful features, compare them with healthy behavior, and generate anomaly alarms that can be explained.


Why This Project

Many anomaly-detection projects focus only on model accuracy. In this project, I wanted to focus on engineering understanding.

My main goals were:

  • Implement DSP blocks in C
  • Analyze vibration signals from bearing data
  • Build a statistical reference-based monitoring method
  • Show the full monitoring chain from raw signal to alarm
  • Keep the pipeline understandable enough for embedded or real-time use This project is based on CWRU bearing vibration data and is designed more as a signal-processing and monitoring system than as a pure machine-learning project.

Project Idea

The basic idea is simple:

  1. Start from digital vibration samples
  2. Condition the signal
  3. Split the signal into windows
  4. Extract time and frequency features
  5. Compare those features to healthy reference behavior
  6. Smooth the anomaly score over time
  7. Trigger alarms if the signal looks abnormal consistently So the project is not just classification — it is a monitoring pipeline.

Architecture

The processing chain is:

raw vibration signal
  → signal conditioning
  → window extraction
  → Hann windowing
  → FFT and spectral analysis
  → time-domain feature extraction
  → frequency-domain feature extraction
  → envelope and bearing-fault indicator extraction
  → monitoring feature vector construction
  → healthy reference scoring
  → EWMA smoothing
  → N-of-M alarm decision

Repository Structure

.
├── native/          # C implementation of the DSP/SSP monitoring engine
├── src/             # Python-side utilities and dataset preparation
├── data/            # Dataset manifests and exported signal files
└── notebooks/       # Exploration and analysis notebooks

The native C side is the main focus of the project.


Main Native Applications

build_reference_profile

Builds the healthy baseline.

  • Reads healthy training and validation manifests
  • Loads vibration signals and applies signal conditioning
  • Extracts monitoring features from windows
  • Estimates healthy mean and standard deviation per feature
  • Computes a score threshold from healthy validation data
  • Saves the result as healthy_reference.json This is the calibration stage of the whole system.

analyze_dataset

Evaluates the method on a labeled test set.

  • Loads the healthy reference profile
  • Reads the test manifest and runs the DSP/SSP pipeline on each file
  • Prints window-level scores and features
  • Reports file-level metrics: accuracy, precision, recall, and F1 This is the offline evaluation stage.

run_monitor

Runs the monitoring pipeline on one signal file.

  • Loads the healthy reference profile
  • Processes one vibration file window by window
  • Computes and prints scores, alarms, and summary information This is the closest program to a deployment-style monitoring demo.

Example Workflow

# 1. Prepare vibration signals in .f32 format and generate manifests
#    → train.csv, val.csv, test.csv
 
# 2. Build the healthy reference profile
./cmake-build-debug/build_reference_profile \
    data/manifests/train.csv \
    data/manifests/val.csv \
    profiles/healthy_reference.json
 
# 3. Evaluate on the labeled test set
./cmake-build-debug/analyze_dataset \
    profiles/healthy_reference.json \
    data/manifests/test.csv
 
# 4. Inspect one file with the monitoring tool
./cmake-build-debug/run_monitor \
    profiles/healthy_reference.json \
    data/exported_f32/97_de.f32 \
    1796

In short:

  • build_reference_profile — learns healthy behavior
  • analyze_dataset — tests the method
  • run_monitor — demonstrates real monitoring behavior

Build and Run

Configure and build the native project:

cmake -S native -B native/cmake-build-debug
cmake --build native/cmake-build-debug

DSP Content

This project demonstrates DSP knowledge through actual implementation choices.

Signal conditioning:

  • DC removal
  • Cascaded SOS biquad bandpass filtering (Direct Form II transposed)
  • RMS normalization and ADC quantization simulation Spectral analysis:
  • Hann windowing
  • FFT-based spectral analysis with dual hardware backends (CMSIS-DSP for ARM Cortex-M; portable Cooley-Tukey for host)
  • Band energy ratios, spectral centroid, spectral entropy, dominant FFT peak frequency Time-domain features:
  • RMS, peak amplitude, crest factor
  • Kurtosis, skewness, zero-crossing rate Bearing-specific analysis:
  • BPFI, BPFO, BSF, and FTF derived analytically from bearing geometry and shaft RPM
  • Envelope-based narrowband energy ratios at each fault frequency

Statistical Signal Processing Content

This project demonstrates SSP ideas, not only raw DSP.

  • Reference profile estimation — per-feature mean and inverse standard deviation fitted from healthy training windows
  • Feature normalization — features scored relative to healthy behavior
  • Diagonal Mahalanobis distance — anomaly score computed as squared normalized deviation across the full feature vector
  • EWMA smoothing — configurable forgetting factor α for temporal score smoothing
  • Threshold calibration — score threshold set from a quantile of healthy validation scores
  • N-of-M alarm logic — alarm fires only if N of the last M windows exceed the threshold, suppressing single-window false alarms

Example Output

Reference profile build:

$ ./build_reference_profile data/manifests/train.csv data/manifests/val.csv profiles/healthy_reference.json
healthy_train_windows=1234
healthy_val_windows=456
threshold=8.731245

Single-file monitoring:

$ ./run_monitor profiles/healthy_reference.json data/exported_f32/97_de.f32 1796
window_id,start_idx,raw_score,smooth_score,alarm,rms,peak_abs,crest_factor,...
0,0,1.284,1.284,0,0.102,0.331,3.245,...
1,512,1.193,1.270,0,0.098,0.315,3.214,...
2,1024,4.881,1.812,0,0.145,0.601,4.144,...

Dataset evaluation summary:

file_level: tp=10 tn=3 fp=1 fn=2 precision=0.9091 recall=0.8333 f1=0.8696 accuracy=0.8125

Why This Project Demonstrates DSP and SSP Skills

I want this project to show more than just "I used data and got an output."

I want it to show that I understand:

  • How to preprocess real vibration signals for digital analysis
  • How to choose interpretable time-domain and spectral features
  • How to reason about bearing fault frequencies from physical geometry
  • How to build a reference-based statistical monitoring method
  • How to smooth noisy sequential outputs with EWMA
  • How to turn signal features into robust monitoring alarms in C That is why this project is organized as a DSP/SSP system rather than only as a model-training repository.

Current Limitations

This is a student engineering project and there are parts I still want to improve:

  • Some modules can be strengthened for embedded robustness (filter state persistence, stack allocation)
  • The portable FFT backend will be replaced with a proper Cooley-Tukey implementation
  • Envelope analysis will be upgraded from rectification to Hilbert transform
  • More automated tests and cleaner benchmarking would improve reproducibility
  • The results table above should be updated with final measured values

Future Improvements

  • Replace portable FFT backend with Cooley-Tukey radix-2 implementation
  • Upgrade envelope extraction to Hilbert transform (analytic signal magnitude)
  • Add harmonic energy features at 2× and 3× fault frequencies
  • Add CUSUM change detection alongside EWMA
  • Strengthen test coverage and benchmark reproducibility
  • Move closer to a true streaming version of the pipeline

Project Motivation

I made this project to practice building a complete monitoring system, not just isolated algorithms. I wanted to connect signal conditioning, spectral analysis, feature design, and statistical monitoring into one workflow that I could explain clearly.

For me, the most valuable part of this project is that it demonstrates practical understanding of DSP, signal analysis, and SSP in C through an end-to-end engineering example — from raw samples to a calibrated alarm decision.