Skip to content

Commit 0a91a58

Browse files
FredM67claude
andcommitted
test: add DC offset filter unit tests with old/new comparison
Add comprehensive test suite for the DC offset filter changes in PR #127: - 20 unit tests covering initialization, tracking, edge cases, precision - 5 comparison tests proving equivalence with old filter implementation - Documents int16_t overflow at ADC max (1023) - safe range is 0-1022 - Verifies new filter tracks same as old (within 5 ADC counts) - Documents slower time constant (~400 vs ~200 cycles) for better noise immunity Key findings: - Mathematical equivalence confirmed between old and new implementations - New filter has no explicit limits (vs old 412-612 range) - Normal 230V operation uses ADC ~96-928, well within safe range Co-Authored-By: Claude (global.anthropic.claude-opus-4-5-20251101-v1:0) <noreply@anthropic.com>
1 parent 91ae0d9 commit 0a91a58

2 files changed

Lines changed: 1040 additions & 0 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# DC Offset Filter Tests
2+
3+
Tests for the DC offset filter with left-aligned ADC (PR #127).
4+
5+
## Tests
6+
7+
### Basic Functionality (6 tests)
8+
9+
| Test | Description |
10+
|------|-------------|
11+
| `test_initialization_values` | Verifies nominal filter values |
12+
| `test_left_aligned_adc` | Validates left-aligned ADC conversion |
13+
| `test_rounding_behavior` | Checks `\| 32U` rounding behavior |
14+
| `test_sample_minus_dc_basic` | DC offset subtraction for various ADC values |
15+
| `test_int16_overflow_edge_case` | Documents int16_t overflow at ADC max |
16+
| `test_safe_operating_range` | Verifies safe ADC range (0-1022) |
17+
18+
### Filter Tracking (3 tests)
19+
20+
| Test | Description |
21+
|------|-------------|
22+
| `test_filter_tracks_positive_offset` | Filter tracks upward DC drift |
23+
| `test_filter_tracks_negative_offset` | Filter tracks downward DC drift |
24+
| `test_filter_stability_centered_waveform` | Filter stability with AC waveform |
25+
26+
### Edge Cases (4 tests)
27+
28+
| Test | Description |
29+
|------|-------------|
30+
| `test_adc_stuck_at_zero` | Fault condition: ADC stuck low |
31+
| `test_adc_stuck_at_max` | Fault condition: ADC stuck high (overflow) |
32+
| `test_accumulator_no_wrap_normal_operation` | No accumulator wrap in normal use |
33+
| `test_large_step_change_recovery` | Filter recovery after DC step |
34+
35+
### Numerical Precision (2 tests)
36+
37+
| Test | Description |
38+
|------|-------------|
39+
| `test_filter_time_constant` | Filter convergence rate |
40+
| `test_q15_precision` | Q15 fixed-point extraction |
41+
42+
### Old vs New Filter Comparison (5 tests)
43+
44+
| Test | Description |
45+
|------|-------------|
46+
| `test_compare_filters_same_dc_level` | Both track to same DC with equivalent input |
47+
| `test_compare_filters_ac_waveform` | Both stable with sinusoidal AC |
48+
| `test_compare_filters_step_response` | Step change tracking comparison |
49+
| `test_compare_sample_minus_dc_equivalence` | Sample-DC calculation equivalence |
50+
| `test_compare_filter_limits_behavior` | New filter behavior vs old limits |
51+
52+
## Key Findings
53+
54+
### Old vs New Filter Equivalence
55+
- **Same DC Level**: Both filters track to the same offset (within 5 ADC counts)
56+
- **AC Waveform**: Both maintain stability with sinusoidal input
57+
- **Sample Calculation**: Mathematically equivalent (accounting for scaling)
58+
59+
### Behavioral Differences
60+
61+
| Aspect | Old Filter | New Filter |
62+
|--------|------------|------------|
63+
| ADC alignment | Right (0-1023) | Left (0-65472) |
64+
| Scaling | x256 | x64 |
65+
| Update method | Accumulate & reset | Continuous integration |
66+
| Time constant | Faster (~200 cycles) | Slower (~400 cycles) |
67+
| DC limits | Explicit (412-612) | No limits |
68+
| Noise immunity | Good | Better (slower response) |
69+
70+
### int16_t Overflow at ADC Max
71+
When ADC is at maximum (1023), the sample-minus-DC calculation overflows:
72+
- `(65472 | 32) - 32704 = 32800`
73+
- 32800 wraps to -32736 as int16_t
74+
75+
**Safe operating range: ADC 0-1022**
76+
77+
In practice, 230V systems use ADC ~112-912, well within safe bounds.
78+
79+
### Filter Limits Comparison
80+
- Old filter clamps to 412-612 ADC (±100 from 512)
81+
- New filter has no explicit limits but tracks the actual DC level
82+
- This allows the new filter to handle miscalibrated systems better
83+
84+
## Running
85+
86+
```bash
87+
pio test -e native -f "*test_dc_offset_filter*"
88+
```

0 commit comments

Comments
 (0)