Wow, that's great info Robert, thanks. Actually, you reminded me that it's not as simple as I first described, because I will be including a temperature sensor on the VCXO. But you've given me a great start, and I'll get that working before I try to get it to compensate for temperature as well. Neil On 10/20/05, Robert A LaBudde wrote: > At 05:41 PM 10/20/2005, Neil wrote: > >Can anyone point me to some info about implementing a simple Kalman filter > >in a PIC or other microcontroller? > > > >I'm building a disciplined oscillator controlled by a PIC. So, it will > >receive intermittent frequency samples from WWV or LORAN, and will generate > >a voltage to tune a VCXO. 1 input, 1 output. > > > >I've seen lots of stuff about Kalman filters on the web, but it's all way > >too complex for me. Can anyone simplify it? > > A Kalman filter is most useful when you have a vector set of differential > equations that govern the plant model. In this case, this would include the > drift and scale errors, effects of temperature fluctuations, etc., of your > controlled oscillator. This complex electromechanical model typically > allows an increase of accuracy of about 10x when it's included. > > It sounds more that what you are looking for is a simple scalar Kalman > filter tracker, similar to a digital phase-locked-loop. > > First, a good starting point is the asymptotic (in time) result of a scalar > Kalman filter, called an Exponentially-weighted Moving Average filter > ("EWMA"). In this case, the formula is particularly simple: > > Prediction (n+1) = Prediction (n) + factor * [Data(n) - Prediction > (n)] > > The "factor" is typically a value > 0 and less than 0.2. It's optimal > choice is related to the first-order autocorrelation of the data, or, in > Kalman filter terms, the "ideal" factor is > > Factor = (P + Q ) / (1 + P + Q) > > where P is the propagation matrix element and Q is the error factor. As > time gets large, this asymptotically becomes > > Factor ~ Q / (1 + Q) > > which leads to the EWMA. > > Because you don't know Q to start with (unless you have done a prior > capability study), usually a guess works just as well. Typical values for > "factor" are 0.05 to 0.2 (low number for longer averaging, shorter numbers > for quicker response). > > It takes about 1/factor recursions to get to where EWMA settles down to the > asymptotic response. A simple algorithm is to use a straight average for > the first 1/factor terms, and then switch to the EWMA thereafter. This > gives very close to the Kalman filter optimal response for the equivalent > value of Q. The straight average can be implemented with only a cumulative > sum and a count, so it is low memory as well. > > Another advantage of the EWMA is that the single multiply is constant and > can be hard-wired in binary, so a fully arithmetic package is not needed. > > The simplest implementation: > > Prediction (n+1) = Prediction (n) + [Data(n) - Prediction(n)]/K > > where K = 4 to 16 (done by shifting). Just realize the first K to 2*K > predictions will be heavily biased until the tracker locks on. > > ================================================================ > Robert A. LaBudde, PhD, PAS, Dpl. ACAFS e-mail: ral@lcfltd.com > Least Cost Formulations, Ltd. URL: http://lcfltd.com/ > 824 Timberlake Drive Tel: 757-467-0954 > Virginia Beach, VA 23464-3239 Fax: 757-467-2947 > > "Vere scire est per causas scire" > ================================================================ > > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > -- http://www.pixpopuli.com -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist