STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
noise_analyzer.h
Go to the documentation of this file.
1#pragma once
2#include <Arduino.h>
3
5public:
6 static const size_t WINDOW_SIZE =
7 1000; // larger window for better statistics
8
10
11 // Reset the analyzer (clear all samples)
12 void reset();
13
14 // Push a new raw ADC code (signed or unsigned)
15 void addSample(int32_t code);
16
17 // True once we've collected WINDOW_SIZE samples
18 bool ready() const {
19 return _count >= WINDOW_SIZE;
20 }
21
22 // Get current number of samples collected
23 size_t sampleCount() const {
24 return _count;
25 }
26
27 // === Noise Metrics ===
28
29 // RMS noise in LSB (standard deviation of codes)
30 float rmsNoiseLsb() const;
31
32 // Peak-to-peak noise in LSB (max - min over window)
33 float peakToPeakNoiseLsb() const;
34
35 // Noise-free bits (based on RMS noise)
36 // This is the number of bits that are not affected by noise
37 float noiseFreeBits(uint8_t adcBits) const;
38
39 // Effective Number of Bits (ENOB) - per ADC datasheet
40 // ENOB = log2(FSR / Vn,rms) = adcBits - log2(RMS_noise_LSB)
41 // Standard metric for ADC performance
42 float enob(uint8_t adcBits) const;
43
44 // Mean ADC code over the window
45 double mean() const;
46
47 // Check if signal appears stable (coefficient of variation < threshold)
48 // Returns true if the noise is small relative to signal magnitude
49 bool isStable(float cvThreshold = 0.01f) const;
50
51private:
52 int32_t _buf[WINDOW_SIZE];
53 size_t _count;
54 size_t _index;
55
56 // Cached statistics (computed on-demand)
57 mutable bool _statsDirty;
58 mutable double _cachedMean;
59 mutable double _cachedStdDev;
60 mutable int32_t _cachedMin;
61 mutable int32_t _cachedMax;
62
63 // Update cached statistics if needed
64 void _updateStats() const;
65};
double mean() const
float rmsNoiseLsb() const
size_t sampleCount() const
static const size_t WINDOW_SIZE
void _updateStats() const
float peakToPeakNoiseLsb() const
void addSample(int32_t code)
float noiseFreeBits(uint8_t adcBits) const
int32_t _buf[WINDOW_SIZE]
bool ready() const
float enob(uint8_t adcBits) const
bool isStable(float cvThreshold=0.01f) const