running average vs lowpass filter

J

Jamie Morken

Jan 1, 1970
0
Hi,

I have a 40Hz -3dB RC lowpass filter and I would like to digitize it to
a 1Hz -3dB lowpass filter. I see two methods to do this, by using an
80Hz or greater sampling of the 40Hz signal, and then doing a running
average on it down to 2Hz, and then feeding that 2Hz signal into the
digital filter to get the 1Hz -3dB output. Or the other method would be
to run the filter at an 80Hz sampling rate, which would take a lot more
computations, and (I am using an 8bit microcontroller!). Does the
100Hz filter method have any advantages over the 100Hz running average
method?

cheers,
Jamie
 
J

John Popelish

Jan 1, 1970
0
Jamie said:
Hi,

I have a 40Hz -3dB RC lowpass filter and I would like to digitize it to
a 1Hz -3dB lowpass filter. I see two methods to do this, by using an
80Hz or greater sampling of the 40Hz signal, and then doing a running
average on it down to 2Hz, and then feeding that 2Hz signal into the
digital filter to get the 1Hz -3dB output. Or the other method would be
to run the filter at an 80Hz sampling rate, which would take a lot more
computations, and (I am using an 8bit microcontroller!). Does the
100Hz filter method have any advantages over the 100Hz running average
method?

cheers,
Jamie

You need to digitize the signal fast enough to prevent aliasing
(creating low beats of higher frequency components. But you can
simulate a 1 Hz roll off RC filter by recycling a stored value through
a scaled sum with the samples.

The relations between time constant and sine wave frequency is a 1 Hz
corner corresponds to a 1Hz/(2*pi) time constant or about 1/6th
second. So if you take 100 samples per second, and every time one
comes in, you add 1/17th of it with 16/17ths of the stored result (and
the sum becomes the stored result), the stored result is the filtered
value with about (1/100)*17 sec = .17 sec = 1.07 Hz roll off. The
scaling and addition will need to be done with at least 13 bits of
precision, so assume two byte math. Not too hard to fit into 1/100
sec for lots of processors.
 
J

John Larkin

Jan 1, 1970
0
Hi,

I have a 40Hz -3dB RC lowpass filter and I would like to digitize it to
a 1Hz -3dB lowpass filter. I see two methods to do this, by using an
80Hz or greater sampling of the 40Hz signal, and then doing a running
average on it down to 2Hz, and then feeding that 2Hz signal into the
digital filter to get the 1Hz -3dB output. Or the other method would be
to run the filter at an 80Hz sampling rate, which would take a lot more
computations, and (I am using an 8bit microcontroller!). Does the
100Hz filter method have any advantages over the 100Hz running average
method?

cheers,
Jamie


If your analog presampling filter isn't good enough, it can let
through stuff that sampling will alias down into the passband. If you
expect any signal content near 40 Hz, sampling at 80 isn't enough.

So sample at a higher frequency if you can.

Running average is a lot of work. You can simulate a first-order RC
lowpass very simply:

Out = Out + (In - Out) / K

which can be done in integer math. The divide-by-K can just be a
signed arithmetic right shift. You'd need to use a long enough integer
size to encompass your ADC bit length plus room for the shift, plus a
bit or two more for luck. Be careful about overflows.

Let's see.. if you sample at, say, 128 Hz and use K = 128 (right-shift
7 bits) you get a 1-second equivalent time constant so it acts like an
RC lowpass of omega=1, so the 3 dB freq is 0.16 Hz. Oops, too slow. So
shift less, 4 bits maybe. You get the idea.

It's late. Where's that damned cat? EE by day, doorman to a cat by
night.

John
 
R

Robert Lacoste

Jan 1, 1970
0
Hi Jamie,

A running average filter has a sin(x)/x response : Basically it is not a
good filter for signals at frequencies like 1,5Fs, 2,5Fs, etc, where Fs is
the inverse of the averaging total period. It is quite easy to understand in
fact : If you input signal is a sine at a frequency which is an integral
multiple of the averaging frequency, then the averaging is perfectly
working, howver if the frequency is in between then the averaging is not so
good...

Definitely a good low pass filter (FIR, IIR, etc) is far better but more
expensive than an averaging. An intermediate solution exists : See Cascaded
Integrate Comb filter (CIC).

Friendly yours,

--
Robert Lacoste
ALCIOM - The mixed signal experts
www.alciom.com

To contact us, thanks to click on the antispam link below :
http://www.cerbermail.com/?dCSHUxvwpw
 
F

Fred Bloggs

Jan 1, 1970
0
Jamie said:
Hi,

I have a 40Hz -3dB RC lowpass filter and I would like to digitize it to
a 1Hz -3dB lowpass filter. I see two methods to do this, by using an
80Hz or greater sampling of the 40Hz signal, and then doing a running
average on it down to 2Hz, and then feeding that 2Hz signal into the
digital filter to get the 1Hz -3dB output. Or the other method would be
to run the filter at an 80Hz sampling rate, which would take a lot more
computations, and (I am using an 8bit microcontroller!). Does the
100Hz filter method have any advantages over the 100Hz running average
method?

It depends- usually at ultra low frequencies you only want the 1Hz
bandwidth while the signal remains within say 10% of the bandlimited
average but you want a more rapid response to large deviations like 25%
or more. This type of thing falls under the IIR data smoothing- which
can be a single multiply and add- but you drop the smoothed output and
start over.
 
K

Ken Smith

Jan 1, 1970
0
John Larkin said:
Out = Out + (In - Out) / K

which can be done in integer math. The divide-by-K can just be a
signed arithmetic right shift. You'd need to use a long enough integer
size to encompass your ADC bit length plus room for the shift, plus a
bit or two more for luck. Be careful about overflows.

You can get other factors in the form of N/K where K is a power of two and
N has a low number of bit set in it. You do it sort of like this:

A = X
A = A SHR 1
A = A + X
A = A SHR 3

This gives you a 3/16 factoring.
 
K

Ken Smith

Jan 1, 1970
0
Hi, [...]

average on it down to 2Hz, and then feeding that 2Hz signal into the
digital filter to get the 1Hz -3dB output.

A simple average sliding along the data is called a "box-car filter"

A Bartlett filter is a lot better than a running average and only takes a
bit more computing power.


X = input
Y = output
Z = other variable
n = Now
K = Width of 1/2 filter

Assuming the X values go into a circular buffer in incrementing order.
You do this:

Z = Z + X(n) - 2*X(n-K) + X(n-2K)
Y = Y + Z

The trick here is to remember to zero everything out at start up and this
only works for integers. Floaters will drift away.

You can do even better if you cascade two stages of filter-decimate code.
The advantage is that you can make the width of the box-car or Bartlett
filters place the zeros at exactly where you want the aliased signals not
to be trouble.


An IIR filter takes less room in memory for a given amount of lowpassing.
It has the disadvantage of a recovery tail.
 
R

Rob Gaddi

Jan 1, 1970
0
I've just been working on a similair project, and I'll second the
recommendation for using a substantially higher sampling rate than
needed and then running a CIC filter to decimate it down to a useful
data rate. In my application I'm then running a short FIR filter after
that to place zeroes specifically on top of 60 Hz and the alias of 120 Hz.

Check out Donadio 2000 on the subject at
http://users.snip.net/~donadio/cic.pdf , it's substantially more
accessable than Hogeneur 1981 (yes, these are all cluttering my desk
ATM). One thing to watch out for, that neither paper makes very clear,
is just what problems you can run into with the integrator overflow.
The short answer is that the the total that you add into the integration
register between each decimation grab has to be less than the maximum
value of the register. So if you're using 8 bit data, and a 16 bit
integration register for each stage, than you can safely decimate 256x
in one stage. Add in a second stage and you can only decimate 16x: 4
bits of gain in each stage.

If anyone would like I've got a quick and dirty Java program I threw
together to do some modeling on this that I can post.

-- Rob
 
G

Gregory L. Hansen

Jan 1, 1970
0
Hi,

I have a 40Hz -3dB RC lowpass filter and I would like to digitize it to
a 1Hz -3dB lowpass filter. I see two methods to do this, by using an
80Hz or greater sampling of the 40Hz signal, and then doing a running
average on it down to 2Hz, and then feeding that 2Hz signal into the
digital filter to get the 1Hz -3dB output. Or the other method would be
to run the filter at an 80Hz sampling rate, which would take a lot more
computations, and (I am using an 8bit microcontroller!). Does the
100Hz filter method have any advantages over the 100Hz running average
method?

You have a 40 Hz analog filter, and you would like to create a signal from
it that's filtered with a time constant of 1 Hz as if the 40 Hz filtering
didn't happen? So in other words you want to unfilter the input, and
then filter it differently? Do I have that right?

Would you be averaging it digitally? If so, I don't see a gain in
averaging instead of filtering.

A digital low-pass filter that mimics an RC filter is

V_out = (V_new + (T/dt)*V_last) / (1 + T/dt)
V_last = V_out

where V_new is the new sample, V_last holds the previous output value, dt
is the sampling time, and T is the RC filter time constant. And in the
interest of saving cycles, somewhere outside of the loop you should
calculate the constants once,

A = 1/(1 + T/dt)
B = (T/dt)/(1 + T/dt)

Then V_out = A*V_new + B*V_last.

If you want to unfilter the incoming signal, you might try an initial
filter time constant of -0.025 seconds, but I've never tried it that way.
 
Top