It WAS a typo. I actually meant Filt =3D (Total + (Factor/2)) / Factor,=20 which rounds the number rather than truncates. If truncating is good=20 enough, then you can use Filt =3D Total / Factor. Kerry Sean Breheny wrote: > I think you perhaps meant: > > Filt=3D(Total)/Factor > > In that case, I often use the structure you are talking about. It > allows you to do only one division for both the output computation and > the internal filter computation, while avoiding resolution issues with > fixed-point math. > > For example, if we have a filter factor of 16, with Olin's example, > and using integer math, we have: > > FILT<--FILT + (NEW-FILT)/16 > > If the absolute value of the difference NEW-FILT is less than 16, > nothing ever happens. > > With Kerry's version: > > Total<--Total+New-Filt > > Any nonzero value of New-Filt causes a change in Total. To scale the > output, we just divide Total by 16 (filt=3DTotal/16). However, that also > gives us the value Filt which we need in order to compute the filter's > difference equation, too. > > Sean > > > On Tue, Nov 23, 2010 at 9:30 AM, Kerry Wentworth > wrote: > =20 >> I would normally implement such a filter as: >> >> Total =3D Total + New - Filt >> Filt =3D (Total+1) / Factor >> And I usually make Factor =3D 2^N so I can divide by right shifting. >> This keeps everything as integers, and if you want the extra resolution, >> you can use Total. >> >> Kerry >> >> >> Olin Lathrop wrote: >> =20 >>> RussellMc wrote: >>> >>> =20 >>>> Simplistic example. If random noise is added to a signal and it reads >>>> 145 mV 60% of the time and 146 mV 40 % of the time then actual voltage >>>> MAY be about 145.4 (not 145.6) mV. Take care when arttempting to apply >>>> this magic. It does work, but ... >>>> >>>> =20 >>> It's pretty easy to apply on the firmware end. Just low pass filter >>> multiple A/D readings. >>> >>> When you have the cycles and real time available, it's a good idea to >>> oversample with the A/D, then low pass filter in firmware anyway. It g= ets >>> you a little extra resolution if the right kind of input noise is prese= nt as >>> you said, but it also reduces quantization noise and relaxes anti-alias= ing >>> filtering requirements on the external hardware. >>> >>> Fortunately a single pole low pass filter is easy to compute: >>> >>> FILT <-- FILT + FF(NEW - FILT) >>> >>> where FILT is the low pass filter being updated with input value NEW ea= ch >>> iteration. FF is the "filter fraction". FF =3D 1 is just a pass thru = filter, >>> and FF =3D 0 is a infinitely heavy filter. Useful values are obviously= in >>> between. In a small system with limited math capabilities, you often >>> arrange FF =3D 1/(2**N), so that the multiply by FF becomes a right shi= ft by >>> N. >>> >>> >>> ******************************************************************** >>> Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products >>> (978) 742-9014. Gold level PIC consultants since 2000. >>> =20 >> >> =20 > > =20 --=20 Internal Virus Database is out-of-date. Checked by AVG Anti-Virus. Version: 7.0.289 / Virus Database: 267.11.13 - Release Date: 10/6/05 --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .