The worksheetDownload the PDF
Answers

U4.5 The price of filtering

Noise and filtering · University · about 30 min

BugBotLab

What this lesson is about

Every filter delays. Measuring the delay, and what it does to a loop closed around it.

Questions 6 marks in all

  1. [1 mark]An exponential filter with alpha = 0.2 runs in a 0.1 s loop. About how many seconds does it delay the signal?

    Answer: 0.4 (accept within 0.01). The delay is (1 - alpha) / alpha = 0.8 / 0.2 = 4 steps, and 4 x 0.1 s = 0.4 s.
  2. [1 mark]What does this print?

    DT = 0.1
    for a in (0.5, 0.25, 0.1):
        steps = (1 - a) / a
        print(a, round(steps, 2), round(steps * DT, 2))
    Answer:
    0.5 1.0 0.1
    0.25 3.0 0.3
    0.1 9.0 0.9

    (1 - alpha) / alpha gives 1, 3 and 9 steps, so 0.1 s, 0.3 s and 0.9 s at a 0.1 s loop. Halving alpha more than doubles the delay.

  3. [1 mark]A controller holds a gap to the wall. Why should the filter go on the measurement rather than on the error?

    1. AFiltering the error also delays the response to a change in the target
    2. BFiltering the error removes more noise
    3. CThe error has no noise in it
    4. DFiltering the measurement has no delay
    Answer: A. The error contains the setpoint as well as the measurement, so filtering it slows the reaction to a new target. Only the measurement is noisy.
  4. [1 mark]In a PID controller, which term usually needs the filter?

    1. AThe D term
    2. BThe P term
    3. CThe I term
    4. DAll three equally
    Answer: A. Differentiating amplifies noise, so D is where filtering pays. The I term already averages, and P usually tolerates the raw noise.
  5. [1 mark]The plant has a time constant of 0.25 s and the loop runs at 0.1 s. By the lesson's rule of thumb, which filter is acceptable in the loop?

    1. Aalpha = 0.5, a delay of 0.1 s
    2. Balpha = 0.1, a delay of 0.9 s
    3. Calpha = 0.05, a delay of 1.9 s
    4. DAny alpha, since delay does not affect stability
    Answer: A. The filter delay must be small compared with tau. 0.1 s against 0.25 s is acceptable; 0.9 s and 1.9 s are longer than the thing being controlled.
  6. [1 mark]A controller was twitchy, so its sensor filter was made heavier twice. It now oscillates slowly. What is the right fix?

    1. AFilter less, because the added delay is now causing the oscillation
    2. BFilter more, to smooth out the oscillation
    3. CRaise the gain so it responds faster
    4. DFilter the error as well as the measurement
    Answer: A. Delay in the feedback path turns negative feedback into positive feedback. Same symptom as before, opposite fix: take delay out.

The task: measure the lag

Drive steadily at the wall, filter the distance, plot raw and filtered, and print lag:, the seconds by which the filtered signal trails the raw one.

from bugbot import *
connect()

ALPHA = 0.2
DT = 0.1
filtered = distance()

The hint students can ask for: Drive steadily at the wall so the distance falls in a straight line, then compare the two signals: the filtered one reaches any given value later. The theory says the delay is about (1 - alpha) over alpha, times the loop period. Check it against what you measure.

A solution

from bugbot import *
connect()

ALPHA = 0.2
DT = 0.1
filtered = distance()
raws, filts = [], []
forward(50)
for i in range(60):
    raw = distance()
    filtered = ALPHA * raw + (1 - ALPHA) * filtered
    raws.append(raw)
    filts.append(filtered)
    plot("raw", raw)
    plot("filtered", filtered)
    if raw < 30:
        break
    wait(DT)
stop()

# how much later does the filtered signal reach the value the raw one reached?
target = raws[len(raws) // 2]
def first_below(series):
    for i, v in enumerate(series):
        if v <= target:
            return i
    return len(series)

lag = (first_below(filts) - first_below(raws)) * DT
print("theory:", round((1 - ALPHA) / ALPHA * DT, 2))
print("lag:", round(max(lag, 0.0), 2))

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.