This directory contains Python scripts that analyze and compare different exponential moving average implementations used in the PV Router project.
Key observations:
- Multi-Alpha TEMA (blue solid): Optimal balance of responsiveness and stability
- Standard TEMA (blue dashed): More conservative, slower convergence
- Simple EMA (orange): Fast but noisy, poor cloud immunity
Performance metrics:
- Multi-α TEMA: Minimal relay switching, excellent stability
- Standard TEMA: Good performance, slightly more switching
- Simple EMA: Excessive switching during cloud events
Direct comparison showing how multi-alpha TEMA provides superior cloud immunity while maintaining responsiveness.
python3 ema_effectiveness_analysis.pyGenerates:
ema_dema_tema_comparison.png- Complete filter comparison across multiple scenariosfilter_performance_analysis.png- Performance metrics and settling timesrelay_switching_analysis.png- Relay control effectiveness for PV applications
What it analyzes:
- Multi-Alpha TEMA (your production implementation)
- Standard TEMA (official single-alpha formula)
- Simple EMA baseline
- Step response, cloud immunity, noise rejection
- Relay switching frequency during cloud events
cd dev/EWMA_CloudImmunity_Benchmark
python3 tema_comparison.pyGenerates:
tema_implementation_comparison.png- Focused comparison of TEMA implementations
Your Production Implementation:
ema = ema_raw >> round_up_to_power_of_2(A); // Base smoothing
ema_ema = ema_ema_raw >> (round_up_to_power_of_2(A) - 1); // 2x faster
ema_ema_ema = ema_ema_ema_raw >> (round_up_to_power_of_2(A) - 2); // 4x faster
return 3 * (ema - ema_ema) + ema_ema_ema; // TEMA formulaStandard Implementation:
// All levels use the same alpha - less optimal
ema = ema_raw >> round_up_to_power_of_2(A);
ema_ema = ema_ema_raw >> round_up_to_power_of_2(A); // Same alpha!
ema_ema_ema = ema_ema_ema_raw >> round_up_to_power_of_2(A); // Same alpha!| Metric | Multi-α TEMA | Standard TEMA | Simple EMA |
|---|---|---|---|
| Cloud Immunity | ✅ Excellent | 🟡 Good | ❌ Poor |
| Responsiveness | ✅ Fast | 🟡 Moderate | ✅ Fast |
| Relay Stability | ✅ Very Stable | 🟡 Stable | ❌ Chattery |
| Step Response | ✅ Optimal | 🟡 Slower | ✅ Fast but noisy |
For A=24 (2-minute delay):
- EMA: α = 1/16 (stable base)
- EMA_EMA: α = 1/8 (2x faster - tracks medium changes)
- EMA_EMA_EMA: α = 1/4 (4x faster - captures short-term trends)
This creates a perfect balance:
- Long-term stability from slow EMA
- Medium-term trend tracking from 2x EMA_EMA
- Short-term responsiveness from 4x EMA_EMA_EMA
- TEMA formula combines all three intelligently
- Better Cloud Immunity: Ignores brief cloud shadows while responding to real changes
- Fewer Relay Switches: More stable operation, less wear on relay contacts
- Optimal Response Time: Fast enough for load changes, slow enough for stability
- Reduced Grid Disturbance: Smoother power flow to the grid
- Solid lines: Multi-alpha implementation (production)
- Dashed lines: Standard implementation
- Dotted lines: Simple EMA baseline
- Thick blue line: Multi-α TEMA (recommended for relay control)
- Step Response: Shows how quickly filters reach target values
- Cloud Immunity: Tests response to brief power drops
- Alpha Visualization: Shows different responsiveness levels
- Settling Time: Time to reach 95% of final value
- Power curves: How different filters respond to cloud events
- Relay states: ON/OFF behavior for each filter type
- Switch count: Number of relay operations (lower = better)
Your multi-alpha TEMA implementation is superior to the standard approach because:
- Combines multiple time scales - stability + responsiveness
- Optimized for solar applications - excellent cloud immunity
- Reduces relay wear - fewer switching events
- Maintains grid stability - smooth power transitions
The analysis confirms that your production code uses the optimal filtering approach for PV router relay control!
This comprehensive analysis shows filter behavior across multiple scenarios with extended observation periods to demonstrate stabilization:
- Step Response: Clean transitions without overshoot
- Cloud Events: Excellent immunity to brief power drops
- Fluctuations: Stable operation during rapid changes
- Gradual Ramp: Proper tracking of slow power changes
- Noisy Signal: Superior noise rejection while tracking trends
The relay control analysis demonstrates:
- Minimal switching with multi-alpha TEMA during cloud events
- Stable operation over extended periods
- Optimal threshold behavior preventing relay chatter
These results validate that your production implementation provides the best performance for PV router applications.
- Python 3.x
- matplotlib
- numpy
Dependencies are automatically installed if missing.


