On Tue, 10 Aug 2010 08:19:32 -0700 (PDT), "kris duff" said: > My colleage told me about a smoothing/lpf formula Try thinking for yourself instead of going by heresay. Since you mention "LFP", you obviously have some idea what this is about. Didn't looking up LFP or "low pass filter" yield more than enough relevant information? > which is : > a(n) =3D {a(n-1) * param + ADC} / {param + 1} That's the standard digital signal processing single pole low pass filter formula, although cleverly obfuscated. It would be silly to implement this filter as written, so let's morph it into something that shows the process as it would be most efficiently done on a digital processor. First, turn this from a equation into a algorithm. Are you really going to keep all samples around for all time in a large array? I didn't think so, so we'll lose the array. I'll also change the name of ADC to NEW. We are deriving a low pass filter. The algorithm will work whether the input is from a analog to digital converter or something else: FILT <-- (FILT * param + NEW) / (param + 1) This shows that FILT is a single variable, and is the only state required. It is updated each iteration to a new value. We need get rid of the unnecessary divide. The first step in loosing the denominator is to note that this is really two terms: param * FILT NEW FILT <-- ------------ + --------- param + 1 param + 1 Now we note that PARAM is just a arbitrary constant, and therefore so is PARAM+1. To clean up the algorigthm, we define K =3D PARAM+1, which also means PARAM =3D K-1: (K-1) * FILT NEW FILT <-- ------------ + --- K K This can be rewritten so that the only divide is between constants, which we can easily eliminate later by chosing different constants: FILT <-- (1 - 1/K)FILT + (1/K)NEW The true algorithm is starting to become obvious, and it's also clear that K is a bad choice of constant. 1/K would make things simpler, so we define FF (filter fraction) =3D 1/K, which also means K =3D 1/FF: FILT <-- (1 - FF)FILT + FF*NEW We're almost there in that the algorithm is now clear, although you don't want to do the computation this way on most processors. You can see that this is a linear blending between FILT and NEW. One term gets weighted by FF and the other by 1-FF, so the total weight is always 1. Now we convert this algorithm that requires two multiplies and two adds into the same thing that only requires one multiply and two adds: FILT <-- FILT + FF(NEW - FILT) And voila, we have the basic standard digital low pass filter algorithm from Digital Filtering for Dummies Made Easy 101. This also shows a different way of looking at the same algorithm. NEW - FILT is the amount that needs to be added to FILT each iteration such that it would follow NEW, which is exactly what happens when FF =3D 1. When FF is less than 1, FILT will go only part way to NEW each iteration. When FF is 1/2, then it will go exactly half the way to NEW each iteration. If FILT starts out at 1 and NEW is then 0, you get the familiar 1/2, 1/4, 1/8, 1/16 sequence. In any case it should be clear you get a exponential decay towards NEW, with FF controlling the exponent. This is why I use the name FF, or "filter fraction". A value of 1 is no filter at all since the output just follows the input. A value of 0 is a infinitely heavy filter in that the output never changes. Useful values are obviously in between, with lower FF causing longer time constants. To complete the analisys back to your original equation, remember that we changed the definition of the arbitrary constant twice. First K was substituted for PARAM+1, then FF for 1/K. Therefore 1 FF =3D --------- PARAM + 1 So if you insist on thinking in terms of PARAM, you can convert it to FF at build time since it's a constant. In any case, you should now be able to easily implement a low pass filter on a digital processor. I do this routinely for most real world measured values. It's usually useful to sample a analog data stream more quickly than necessary, then apply a couple of poles or so of low pass filtering as shown here. Note that when FF =3D 1 / 2**N, that the multiply by FF can be accomplished by a right shift of N bits. This makes low pass filtering tractable even on small processors without multiply hardware, like a PIC 16. ******************************************************************** Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products (978) 742-9014. Gold level PIC consultants since 2000. --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .