How to determine the parameter of a Complementary Filter?

I know that the Complementary Filter has the functions of both LPF and HPF. But I think my understanding on the principal behind it is still unclear.

I am quite new on digital signal processing, and maybe some very fundamental explanations will help a lot.

Say I have a Complementary Filter as follows:

$$y =a\cdot y+(1-a)\cdot x$$

Then my parameter $a$ may be calculated by $$a=\frac{\text{time constant}}{\text{time constant}+\text{sample period}}$$ where the $\text{sample period}$ is simply the reciprocal of the $\text{sampling frequency}$.

The $\text{time constant}$ seems to be at my own choice.

My Questions:

1. What is the theory behind this calculation?
2. How do we choose the $\text{time constant}$ properly?

Note: I also posted this question on Stack Overflow, as the answers there are likely to be slightly different in emphasis.

• The difference equation you give for the complimentary filter isn't -- it's the difference equation for a low-pass filter. Aug 7 '13 at 21:00
• Multi-posted to Stack Overflow. Aug 7 '13 at 23:14
• Please don't post the same answer to multiple stack exchange sites @perfectionm1ng. If a question is on the wrong site it will be moved to a more suitable one. Aug 7 '13 at 23:17
• @TimWescott sorry could you please elaborate? Aug 8 '13 at 2:04
• @MarkBooth I am sorry for not knowing this rule. Aug 8 '13 at 2:05

The complementary filter you mentioned comprises of both a low-pass filter (which filters out, or attenuates, short term accelerometer fluctuations), as well as a high pass filter (which tries to negate the effect of drift on the gyroscope). A time constant $\tau$ with respect to first order filters describes at what point (the cut-off frequency $f_{c}$) information should start being rejected (LPF) or stop being rejected (HPF). With respect to a complementary filter the time-constant used is the same for both the LPF as well as the HPF. The time constant can be thought of as boundary between trusting the gyroscope, and trusting the accelerometer.

The value of $a$, or the time constant, should be a function of your gyroscope's drift and accelerometer noise. You can optimize to filter out gyroscopic drift (HPF with slow cut-off), but then your accelerometer will allow too much accelerometer noise (LPF with slow cut-off).

As with all filter design, calculate the filter coefficients, test it to confirm it doesn't work, and randomly tweak it till it does.

Refer to the following links for more in depth discussion:

• The block diagram, as given, suffers from pole-zero cancellation in the gyro signal path. The consequence of that, as drawn, will be undetected integrator windup and eventual numerical overflow. In practice you'd remember that in mathemagic land an integrator cascaded with a high-pass is just a low-pass, and here in the real world a low-pass filter is way more tractable than the cascade shown. Aug 7 '13 at 21:02
• Yeah I realise the graph might not be perfect, I was just trying to illustrate the two different signal paths (I copied the graph from the segway link). Aug 7 '13 at 21:18
• actually mine is to combine gyroscope and the magnetometer. but anw, should share the same rationale. e.g. my sampling frequency for gyroscope and magnetometer is same, 50Hz. Could you please show a sample calculation of a? Aug 8 '13 at 2:09
• Please refer to page 13 of the second link I posted (A simple solution for integrating...), but I think ultimately it would be best to test the filter practically. Aug 8 '13 at 7:51
• Oh and an example: given sampling rate of 50 hz (0.02 period), and a time constant of say 0.5 seconds, a = (0.5)/(0.5 + 0.02) = 0.96 Aug 8 '13 at 7:53