If you want some simple smoothing, a single-pole exponential moving average filter can be used.
It can be implemented very efficiently, and it smooths alright, but it has a nonlinear phase response, which means it distorts the signal.
You can find a full discussion of the exponential moving average filter and its characteristics here:
Exponential Moving Average
You can find implementation details and an Arduino example here:
EMA: C++ implementation
The EMA class is included in the Arduino Filters library:
EMA documentation
The implementations use inverses of powers of two for the filter coefficients (as mentioned by aarg). If you want finer control over the cut-off frequency, you could implement it using more general coefficients, using fixed-point integer divisions or floats but there are two things to keep in mind:
- It's often easier (and more efficient) to vary the sampling rate rather than the filter coefficients.
- The cut-off frequency of this filter is quite arbitrary anyway, since the roll-off is so slow (≃20dB/dec).
For more serious filtering, Butterworth filters are worth looking into. They have three main advantages: 1. the order can easily be increased for sharper roll-off, 2. the magnitude response in the pass-band is almost perfectly flat (by design) and 3. they have an almost linear phase response in the pass band (i.e. less distortion of the signal).
A flat pass band means that none of the frequencies below 100Hz will be amplified or attenuated.
The disadvantage is that they're more expensive to compute. You'll probably need floating point math, and the higher the order, the more calculations you need to carry out.
You can find a mathematical derivation of Butterworth filters here:
Butterworth filters
Butterworth filters are defined in the analog domain, so you have to discretize it in order to use it for digital filters:
Discretization of Butterworth filters
If you're not interested in all the mathematics, you can just use the implementation of the Arduino Filters library:
Butterworth filter: Arduino example
Simply enter the desired sampling rate and cut-off frequency, and the library will handle prewarping and filter design for you. By default, it uses a BiQuad implementation, so it should be numerically stable.
As mentioned by MrMark, an 8-bit AVR at 16 MHz like the ATmega2560 on an Arduino Mega will be able to handle a sampling rate of a little over 4 kHz for a second-order filter, 3.5 kHz for a fourth-order filter, 2.8 kHz for a sixth-order filter.
If the analog signal you want to filter has components with a frequency higher than half of the sampling rate, you'll need an anti-aliasing filter before your ADC (in hardware), see Nyquist-Shannon sampling theorem.
Pieter