Complementary filter: from angular rate to a fused angle

Practise a one-axis complementary filter, separating rotation rate from angle, checking dt and alpha, with a worked example, a worksheet, answers and the limits of simulated data.

Last updated:

Concept image of a Sensor Fusion study desk with an IMU module, a microcontroller board, paper axis templates, a graph notebook and a drone frame with the propellers removed.
A conceptual mock-up created with AI; it is not a screenshot or a device model with confirmed support.

Start the worked example · Back to the IMU applications lesson

Read on: the lesson on angles crossing ±180°, with an example to read angle differences that cross the range boundary.

Objectives and prerequisites

Practise explaining how the gyro helps predict the angle, how the accelerometer helps correct the angle, and why the fusion weight must relate to the time interval. Begin with a PC calculator or paper. No board is needed. Read IMU calibration and sampling rate before doing this.

The photorealistic image at the start of the lesson was created with AI to show the learning context. The notebook and devices in the image are not measurement results or a verified circuit. This kit uses fictitious numbers for one roll axis and is not flight firmware or a complete 3D attitude simulation.

Fuse values that have meaning and the same unit

The gyro gives the rotation rate such as deg/s, while the accelerometer gives specific force such as g or m/s², so the two kinds of raw values are not averaged together. The data must first be converted into angle form under a matching axis definition. See the concepts of prediction and weighting in the AHRS Complementary Filter document

For the roll-only problem, the same body FRD frame is used: X forward, Y right, Z down, with positive angles by the right-hand rule. When level and still, the specific force is [0, 0, −1] g and define acc_angle = atan2(−ay, −az) then convert rad to degrees. For example, ay = −0.5 g and az = −√3/2 g give +30° when no motion disturbs it. This formula is limited to the posture/axes of the problem; do not copy signs from a library that uses a different coordinate frame.

This lesson starts from an acc_angle that has already been declared; it does not use raw counts and does not compute yaw from the accelerometer. Estimating an angle from gravity must account for forces from motion. Read the assumptions of ADI AN-1057: inclination sensing and check the PX4 v1.16 coordinate frames before connecting to a real driver.

Reading the two steps of the formula

Use the unit degree, deg/s, second throughout the problem. If a real system uses rad/s, make the units match before substituting into the formula.

  1. Predict: prediction = previous_estimate + gyro_rate × dt with gyro_rate held constant over the whole interval, which is one-axis Euler integration for teaching.
  2. Fuse: estimate = alpha × prediction + (1 − alpha) × acc_angle use the previous round's fused result as the next round's previous_estimate.

The prediction and acc_angle must be in degrees and the same axis definition. alpha has no unit. The problem sets alpha = tau / (tau + dt) where tau and dt are in seconds and tau > 0, dt > 0. The two weights sum to one, but this does not confirm that the input data are correct. An alpha near one gives the prediction more weight; near zero gives acc_angle more weight. It is not the probability that either side is right.

Set tau = 0.5 s for calculation practice: dt = 0.01 s gives alpha = 50/51 ≈ 0.980392; dt = 0.02 s gives alpha = 25/26 ≈ 0.961538. Do not reuse the earlier alpha while ignoring dt when the interval changes. This alpha formula is the form chosen for the lesson; it is not an API or a value suitable for every flight stack.

Calculate three rounds and read where the angle is adjusted

Assumptions: start at t = 0 ms, estimate = 0°, tau = 0.5 s, gyro_rate = 10 deg/s in every interval, with no bias/clipping/quantization, and the accelerometer angles declared in the table. Use the sample timestamps from a single clock, not the USB read time. The problem declares no true angle, so there is no RMSE or conclusion about accuracy.

Time and weights of each round:

Sample (ms) dt (ms) alpha
10 10 50/51
20 10 50/51
40 20 25/26

Predicted and fused angles:

Sample (ms) Predicted (°) Accel angle (°) Fused (°)
10 0.100000 0.00 0.098039
20 0.198039 0.00 0.194156
40 0.394156 3.00 0.494381

First round: prediction = 0 + 10 × 0.01 = 0.1°; estimate = (50/51) × 0.1 + (1/51) × 0 = 0.098039° The second round must start from this estimate, not from zero again.

The third round has dt = 20 ms and uses alpha = 25/26 and the second round's previous_estimate. When acc_angle changes to 3°, the fused result is pulled above the prediction. This change shows only the effect of the input data; it does not prove that the drone really tilted or that the accelerometer is wrong. Keep full decimals during calculation and round only the displayed values. The table uses Euler, unlike the earlier PC kit, which integrates the gyro with the trapezoid rule, so it does not replace the earlier RMSE results.

A graph of three rounds of the Complementary Filter from fictitious numbers, separating the prediction, the estimate and the accelerometer angle, with a diagram of feeding the fused result back to the next round

Created with Matplotlib from the declared formulas. The lines joining points are there to show the order; they are not a continuous waveform that was actually measured. Open the full-size IMU fusion graph and diagram

Time, disturbing forces, and the limitations to check

dt must match the interval of the data used. If the timestamp does not advance, the data are duplicated, or the clocks differ so that time cannot yet be compared, separate the reason and the handling method before calculating. Do not use dt = 0 or a negative value in place of a new sample. For a longer time gap, replacing the gyro_rate with a single value throughout the interval is an added assumption; it does not recover motion that was not measured.

The accelerometer does not separate motion forces from gravity by itself. Checking that the norm is near g is only one condition and does not confirm that the angle is usable every time. Do not increase the accelerometer weight just to make the graph look smoother; check units, axes, bias, freshness, data rate and the motion state.

This model does not include angles crossing ±180°, pitch/yaw or the singularities of Euler angles. Linear fusion may be wrong at the angle boundary, and the three-axis body rotation rates do not equal the derivatives of the Euler angles in every posture. A 3D system uses a suitable attitude representation and estimator. Study PX4 EKF v1.16 following on from the meaning of the input data; do not assume that switching to an EKF makes all errors disappear.

Practice steps and starting advice

  1. Read the Complementary Filter kit guide then make a copy of the blank IMU fusion planning sheet and write the axis, units, clock, integration method and initial values.
  2. Do the three-round angle fusion problem before opening the answers and limitations Calculate dt in seconds before finding alpha, and separate the prediction from the estimate. There is no new score in the original assessment form.
  3. Compare with the earlier IMU practice kit on a PC read the protocol and integration method of that kit before comparing results. Do not replace the original data or answers with this three-round table.
  4. Write down what the problem has not confirmed, such as accuracy against the true angle, latency, and behaviour under disturbing forces. Read data age checking to manage freshness before using the results.
  5. When equipment is available, connect the real-board preparation guide and keep the raw data, config, reference, temperature and an independent measurement set. This lesson has no IMU measurement results or flight tuning values.

Starting advice: change one factor at a time, keep the source data, and explain the reason for differences before adjusting tau. A nice graph, a matching calculation, or finishing one lesson does not confirm stable flight.

Provenance and file checks: Equations and references of the fusion kit · Hash list of the fusion kit

Back to the MCU overview · Choose MCU and Sim activities

Used in courses

How to cite this page

Drone Institute, Rangsit University. (2026). Complementary filter: from angular rate to a fused angle. In UAS Technology Knowledge Hub (B.Tech.). https://rsu-uat.pk-research.work/en/kb/knowledge/microcontrollers/applied/fusion/

Original from Drone-Hub: https://drone.pk-research.work/knowledge/microcontrollers/applied/fusion/ · © PK-Research