Re: [bascom] LOW PAS FILTER ROUTINE
Peter
Here are the details for a first-order (RC-type) filter, which I have used
in actuator control. Transfer function implemented:
Y(s)/U(s) = F(s) = 1/(Ts + 1), where T = RC = time constant,
which translates to the difference equation (with some assumptions):
dY[k] = (dt/T)(U[k]-Y[k])
where: dY[k] is calculated amount to increment Y, the output
dt is scan time
T is time constant (as before)
U[k] is presently available input data
Y[k] is presently available output data
For general purpose low-pass noise filtering, I implement the factor (dt/T)
as an integral number of right shifts of
(U[k]-Y[k]), it saves computation time.
For example,
Requirements:
- First-order IIR filter time constant T = RC = 1.5sec
- Scan time of 0.05sec (read-calculate-write at this rate)
- Input and output at 10-bit, unsigned
Initialisation implementation:
- Accumulator 16-bit, top 10 bits are the output value
Real-time code:
- Read input U[k], into 16-bit register pair
- Shift left 6 times (so MSbit of data is in MSb bit of accum.)
- Subtract existing accumulator value from this U[k]*2^6 value
- Remember sign, by setting a flag
- Get absolute value (don't want a "1" in MSB of 16-bit difference)
- (dt/T)=0.05/1.5~=2^-5, so we need 5 right-shifts, do it.
- if sign of difference was negative, subtract it from accumulator, else add
the difference to the accumulator
- Ouput is top 10 bits of accumulator as output, prepared by making a copy
of it, and right-shifting 6 times.
It is worthwhile expanding on what I previously wrote:
* If (dt/T) is small, then the time constant you actually get is closer to
T, but the more right-shifts you need, and the larger your accumulator must
be, to retain the effect of a single bit difference between current input
and previous output.
* The trick is then obviously to balance scan-time and time constants. I
don't go less than 3 right shifts, because the accuracy of time constant
estimation deteriorates too much (also digital filter becomes too fast to be
effective, and my anti-aliasing filter time constant is then dominant) and I
don't go more than 6 right shifts, so that my 10-bit IIR filters retain IIR
characteristics down to 1-bit difference between input and output, within
16-bit accumulator.
* It is essential to look after anti-aliasing, if you plan to do effective
digital low pass filtering of noise. As a simple rule, provide an analogue
first order filter with time constant RC=1/(2*pi*(scan frequency)*2^-4) then
you guarantee 18dB reduction in aliases of noise frequencies higher than
(scan frequency)/2
There is no reason why the same approach may not be used for other forms of
fixed-parameter transfer functions. I am sure this approach would be more
than good enough for bandpass filtering of audio, though not hi-fi quality I
suspect, on the 8MIPS 8-bit AVRs.
Hope this helps! If you need more explanation, I could do it off-list. Also,
you consult your local technical library for texts which deal with digital
filtering for instrumentation and measurement.
John Stiekema
----- Original Message -----
From: Vangrieken, Peter <VANGRIP@bp.com>
To: <bascom@grote.net>
Sent: 26 January, 2001 2:42 PM
Subject: RE: [bascom] LOW PAS FILTER ROUTINE
> A little more information would be really appreciated! I'm also very
> interested in digital audio filters. Do you have a website or a few links
to
> share?
>
> regards
> Peter Van Grieken
> Team1 Electronics
> www.team1electronics.com
>
>
> > ----------
> > From: John Stiekema[SMTP:j.stiekema@ee.wits.ac.za]
> > Reply To: bascom@grote.net
> > Sent: vrijdag 26 januari 2001 11:39
> > To: bascom@grote.net
> > Subject: Re: [bascom] LOW PAS FILTER ROUTINE
> >
> > I have done this many times, in assembler and BASCOM, but IIR filter. Is
> > this any use to you, any reason you need FIR?
> >
> > The IIR algorithm acts just like a sampled analogue RC filter, provided
> > you apply correct anti-aliasing analogue filtering also, and provided
you
> > implement it with a large enough accumulator register (the larger the
> > difference between sampling time and filter time constant, the larger
the
> > accumulator needed for the IIR filter)
> >
> > John
> >
> > ----- Original Message -----
> > From: michele
> > To: bascom@grote.net
> > Sent: 25 January, 2001 3:30 PM
> > Subject: [bascom] LOW PAS FILTER ROUTINE
> >
> > - Who can suggest me a low pass filter routine on analog input (FIR
> > filter) ?
> >
> >
> >
> >
>