Skip to content

moving average filters implementations

License

xLowZ/moving-average-filters

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Moving Average Filters

This repository provides implementations of two moving average filters for signal smoothing in embedded systems:

  • Simple Moving Average (SMA) Filter: A basic averaging filter that maintains a fixed-size window of past values.
  • Exponential Moving Average (EWMA) Filter: A weighted average filter where recent samples are given more importance.

Both filters are implemented in a modular way, allowing users to choose the most suitable approach for their application.

Features

  • Designed to be compatible with C89 (GNU extensions)
  • Two filtering methods: Simple Moving Average (SMA) and Exponential Moving Average (EWMA)
  • Static instances rather than dynamic allocation
  • Configurable parameters:
    • FILTER_WINDOW_SIZE: Defines the number of samples for the SMA filter (must be a power of 2 to enable bitwise optimizations and improve performance).
    • MAX_FILTERS: Limits the number of simultaneous filter instances.
    • ADC_RES: The device's ADC resolution (in bits), which must be specified by the user. This value is used internally to determine the appropriate accumulator type and avoid overflow in the SMA filter.

Main Files

  • sma_filter.c/h - SMA filter with instance-based management.
  • sma_fnc.c/h - Standalone SMA function (no instances required).
  • ewma_filter.c/h - EWMA filter with instance-based management.
  • ewma_fnc.c/h - Standalone EWMA function (no instances required).
  • fconfig.h - Configuration file for filter parameters.

Test Files

  • MovAvg_tests.ino - Additional Arduino Uno (Atmega328p) test file including all filter implementations at the same time (adapted). -Additional test modes using a circuit with a potentiometer (see example in the Circuit folder).
    • Has some #defines to select and global variables for the tests:
      • PLOT: Formats output for better visualization in the Serial Plotter.
      • VALS: Formats output for better readability in the Serial Monitor.
      • TESTS: Runs actual performance tests and measures execution time.
  • sma_tests.c - Simple MA tests file.
  • ewma_test.c - Exponential MA test file.

Example:

gcc -std=gnu89 -O0 sma_tests.c sma_filter.c -o sma_tests

Usage

Users can select the preferred filtering approach by including the corresponding headers and calling the appropriate functions. The provided Arduino test file (MovAvg_tests.ino) demonstrates all available filters, allowing users to decide which one best fits their needs.

License

This project is open-source and available under the MIT License.

About

moving average filters implementations

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published