On Fri, 10 Oct 2008 17:11:25 -0400, Jerry Avins wrote:
> Manfred wrote:
>> Hi all!
>>
>> Tim,
>>
>>> You want a simple first-order lowpass IIR filter. You can implement
>>> it
>>
>> in one line of C:
>>
>>> output = gain * (input - output);
>>
>> Either there is some mistake in this, or I'm doing something wrong, but
>> I'm not getting any attenuation of high frequency components with this.
>
> It puzzled me too, but I've learned to question myself before I question
> Tim. Try the exponential averager I described. Making k small produces
> the greatest reduction of highs. You might start with k = 1/2. All
> fractions of the form (2^n - 1)/2^n, where (for 16 bit words) 1 < k < 15
> allows efficient computation.
>
If Jerry doesn't understand it, I didn't explain it well.
Take gain = 0.1, input is constant 1, and output starts at 0. Then you
should get:
step output
0 0
1 0.1
2 0.19
3 0.271
n 1 - 0.9^n
>>> A 20Hz cutoff is pretty high for a sampling rate of 50Hz -- you're not
>>> going to get much attenuation of your noise.
>>
>> The sampling rate is 100Hz. I might lower the cutoff further. But the
>> previous implementation of this controller, done in the analog world,
>> used 20 Hz with good results. But that was a third order filter.
>
It's still high, even for a 100Hz sampling rate -- but not nearly as bad.
> To measure entire periods, the sample rate should be 50 Hz. There is no
> assurance that alternate half cycles have the same duration.
>
Gin up a filter with a transfer function (z + 1)/(z - d), with d close to
1. That'll let you update at 100Hz while rejecting the noise at 50Hz,
yet still giving you good response at somewhat lower frequencies.
>>> Keep in mind that as you lower the filter bandwidth you'll have to
>>> back
>>
>>> off your pro****tional and integral gains to keep the loop stable.
>>
>> I know. That's also why I don't want to lower the cutoff frequency too
>> much.
>>
>>> You may also find that you need to use more than 16 bits in the filter
>> to
>>> maintain enough precision
>>
>> That would be a problem, because in the programming language I'm using,
>> I only have 16 bit integers, and no carry bit.
Then get another programming language! If it's C, and it's remotely ANSI
C compatible, then 'long' will be 32 bits.
> The assembly routine to filter in extended precision is rather
> straightforward. The technique we call fraction saving around here may
> allow you to limit the amount of code that needs extended precision.
> http://www.dspguru.com/comp.dsp/tricks/alg/dc_block.htm
describes a
> high-pass filter with fraction saving.
>
> ...
You can extend precision in most any programming language if you can
stand the tedium -- but it's often easier to do things in assembly.
--
Tim Wescott
Wescott Design Services
http://www.wescottdesign.com
Do you need to implement control loops in software?
"Applied Control Theory for Embedded Systems" gives you just what it says.
See details at http://www.wescottdesign.com/actfes/actfes.html


|