Problem 284434 · easy · Level 02 Linear Data Structures

Where Does the Scale's Reading Settle?

difference equation · final value · steady-state gain · stability

A kitchen scale smooths its load-cell reading with a digital filter, the same kind of difference equation as the ride-height filter:

y[k] = a1 * y[k-1] + a2 * y[k-2] + b0 * x[k] + b1 * x[k-1]

Put a bag of flour on the scale and the input becomes a constant, x[k] = load (in kg) for every sample. A stable filter (one whose output does not grow without limit) then settles at a final value: once it has settled, y[k], y[k-1] and y[k-2] are all the same number y, and x[k] = x[k-1] = load. Substituting gives one equation with one unknown:

y = a1 * y + a2 * y + b0 * load + b1 * load

The ratio final / load is the filter's steady-state gain (or DC gain): it says how much the filter scales a constant input. A scale needs a gain of exactly 1, or it shows the wrong weight; a designer who rounds the coefficients carelessly gets a scale that is 2 % off.

Write settled_reading(a1, a2, b0, b1, load) that returns a tuple (gain, final): the steady-state gain and the reading the scale settles at for the given load. The coefficients always describe a stable filter.

Examples

Input:  a1 = 1.5, a2 = -0.56, b0 = 0.03, b1 = 0.03, load = 2
Output: (1.0, 2.0)
Explanation: y * (1 - 1.5 + 0.56) = 0.06 * 2, so 0.06 * y = 0.12 and y = 2.0: the scale reads true.

Input:  a1 = 0.9, a2 = 0, b0 = 0.1, b1 = 0.05, load = 4
Output: (1.5, 6.0)
Explanation: the gain is 0.15 / 0.1 = 1.5: this scale shows 6 kg for 4 kg of flour.

Constraints

  • the filter is stable: abs(a2) < 1, a1 + a2 < 1 and a2 - a1 < 1
  • answers are compared with a tolerance of 1e-6; do not round

Goals

  • Find the final value of a difference equation without simulating it
  • Read the steady-state gain of a filter from its coefficients
  • Use a model to check whether a filter changes the size of a constant signal
Starting Python…