CMP-272 · Lesson 5 of 12 · 23 min

Differential and difference equations

Convolution tells you the output of an LTI system once you know its impulse response, but where does the impulse response come from? For circuits, mechanical systems and most digital filters, it comes from an equation that relates the output to the input and their rates of change. Continuous-time systems built from resistors, capacitors, inductors, masses and springs obey linear differential equations with constant coefficients. Digital filters obey linear difference equations, which are recipes for computing each new output from the latest input and a few earlier values. This lesson shows how to set up both kinds of equation, how to solve them, and how their solutions split into a part set by the system itself and a part set by the input.

A first-order circuit

Return to the RC low-pass filter on the microphone channel. The input voltage x(t) drives a resistor R in series with a capacitor C, and the output y(t) is the capacitor voltage. The current through the resistor is (x − y)/R, and it all flows into the capacitor, whose current is C dy/dt. Setting the two equal gives the circuit's differential equation:

\[RC\,\frac{dy(t)}{dt} + y(t) = x(t)\]

This is a first-order linear differential equation with constant coefficients. Linear means y and its derivatives appear only to the first power and are never multiplied together; constant coefficients means R and C do not change with time. Those two features are exactly what make the system linear and time-invariant, provided it starts at rest. Higher-order systems have higher derivatives: a series RLC circuit is second order, and the general Nth-order equation is

\[\sum_{k=0}^{N} a_k \frac{d^k y(t)}{dt^k} = \sum_{k=0}^{M} b_k \frac{d^k x(t)}{dt^k}\]

Natural and forced responses

The complete solution of a linear differential equation is the sum of two parts. The natural, or homogeneous, response y_h(t) solves the equation with the input set to zero. It depends only on the system and on its starting energy, such as the initial charge on the capacitor. The forced, or particular, response y_p(t) is any one solution that follows the input. For the RC circuit, setting x = 0 leaves RC dy/dt + y = 0, which an exponential e^(λt) solves if RCλ + 1 = 0. That equation in λ is the characteristic equation, and its root λ = −1/RC sets the natural response, Ae^(−t/RC). The constant A is found from the initial condition after the forced part has been added.

For a constant input x(t) = V applied at t = 0, try a constant forced response y_p = K. Substituting gives K = V. The complete response is y(t) = V + Ae^(−t/RC), and if the capacitor starts at y(0) = y₀, then A = y₀ − V:

\[y(t) = V + (y_0 - V)\,e^{-t/RC}, \qquad t \ge 0\]

The output starts at y₀ and moves exponentially toward V, covering 63 % of the remaining distance in each time constant. After five time constants it is within 1 % of V. The natural response dies away because its root is negative; a system whose characteristic roots all have negative real parts is stable, a link that lesson 11 makes precise.

Left, a circuit with input voltage x of t, a series resistor R and a capacitor C to ground, with output y of t across the capacitor. Right, a plot of the step response rising from 0 toward 1 along an exponential curve, with dashed markers showing 63 percent at one time constant and about 99 percent at five time constants.
The RC low-pass filter and its step response. Each time constant RC closes 63 % of the remaining gap to the final value.

Zero-input and zero-state responses

A second way to split the solution is often more useful for systems. The zero-input response is what the system does from its initial conditions with no input; the zero-state response is what the input produces with the system starting at rest. The complete response is their sum. The zero-state response is the one given by convolution with h(t), and it is the part that is linear in the input. The zero-input response is why a system with stored energy is not strictly linear in its input: doubling the input does not double the part caused by the initial charge. When engineers say a system is LTI, they mean its zero-state behaviour.

Worked problem

Switching on with a charged capacitor

Amara's anti-aliasing filter has RC = 0.16 ms. When the controller powers up, the microphone bias sets the input to x(t) = 1.65 V for t ≥ 0, but the capacitor still holds y(0) = 0.4 V from the last shutdown. Find y(t) and the zero-input and zero-state parts.

Show solution
  1. The complete response for a constant input V with initial value y₀ is:
  2. \[y(t) = 1.65 + (0.4 - 1.65)e^{-t/0.16\,\text{ms}} = 1.65 - 1.25\,e^{-t/0.16\,\text{ms}}\ \text{V}\]
  3. The zero-input response decays from the initial value with no input:
  4. \[y_{zi}(t) = 0.4\,e^{-t/0.16\,\text{ms}}\]
  5. The zero-state response is the step response scaled by 1.65 V:
  6. \[y_{zs}(t) = 1.65\big(1 - e^{-t/0.16\,\text{ms}}\big)\]

Answer: y(t) = 1.65 − 1.25e^(−t/0.16 ms) V, which equals y_zi + y_zs. The input reaches its final value within 1 % after about 5RC = 0.8 ms, so Amara's firmware ignores the first millisecond of samples after power-up.

Second-order systems

A series RLC circuit, a mass on a spring with damping and a motor with load inertia all give second-order equations. Written in standard form with natural frequency ω_n and damping ratio ζ, the characteristic equation is λ² + 2ζω_nλ + ω_n² = 0, with roots

\[\lambda = -\zeta\omega_n \pm \omega_n\sqrt{\zeta^2 - 1}\]

If ζ > 1 the roots are real and negative and the step response creeps up without overshoot, an overdamped system. If ζ = 1 it is critically damped, the fastest response with no overshoot. If ζ < 1 the roots are complex, and the natural response is a decaying sinusoid at the damped frequency ω_d = ω_n√(1 − ζ²): the system rings. A sensor mount on the pump that rings at its natural frequency is one reason Daniel sees bursts of noise in the tachometer signal. ELE-390 studies second-order systems in depth; here the point is that the roots of the characteristic equation tell you the shape of the natural response.

Difference equations

A digital filter computes each output from the current input and stored past values. The general linear constant-coefficient difference equation is

\[\sum_{k=0}^{N} a_k\,y[n-k] = \sum_{k=0}^{M} b_k\,x[n-k]\]

With a₀ = 1, it can be rearranged into a recursion that computes y[n] directly. If all the a coefficients after a₀ are zero, the output depends only on inputs, the impulse response has M + 1 terms and the filter is FIR; the moving averages of lesson 3 are examples. If any earlier output is fed back, the impulse response goes on forever and the filter is IIR.

Daniel's favourite IIR smoother is the exponential moving average:

\[y[n] = (1 - \alpha)\,y[n-1] + \alpha\,x[n], \qquad 0 < \alpha \le 1\]

Each new output moves a fraction α of the way from the previous output toward the new reading. It needs only one stored value instead of a whole window, which matters on a small microcontroller. Feeding in an impulse gives h[0] = α, h[1] = α(1 − α), h[2] = α(1 − α)², and in general h[n] = α(1 − α)ⁿu[n], a decaying geometric sequence. The filter is stable when |1 − α| < 1, and its DC gain is Σh[n] = 1, so steady readings pass unchanged. The characteristic root of the recursion is 1 − α, the discrete-time counterpart of λ: the closer it is to 1, the slower the response.

Worked problem

Choosing the smoothing factor

The tachometer produces a reading every 50 ms. Daniel wants the exponential moving average to cover 63 % of a sudden change in about 0.5 s. What α should he choose?

Show solution
  1. After n samples, the step response of the filter is 1 − (1 − α)ⁿ. Reaching 63 % means (1 − α)ⁿ ≈ e^(−1).
  2. For small α, (1 − α)ⁿ ≈ e^(−nα), so 63 % is reached after about n ≈ 1/α samples.
  3. \[n = \frac{0.5\ \text{s}}{0.05\ \text{s}} = 10 \quad\Rightarrow\quad \alpha \approx \frac{1}{10} = 0.1\]
  4. Check exactly: (1 − 0.1)¹⁰ = 0.349, so the response after 10 samples is 0.651, close to the target.

Answer: α ≈ 0.1. The filter then behaves like a sampled RC filter with a time constant of about 10 samples, or 0.5 s.

From a differential equation to a difference equation

A firmware engineer often has to implement an analog prototype as a digital filter. The simplest method replaces the derivative with a difference over one sample interval T. Using the backward difference dy/dt ≈ (y[n] − y[n − 1])/T in RC dy/dt + y = x gives

\[y[n] = \frac{RC}{RC + T}\,y[n-1] + \frac{T}{RC + T}\,x[n]\]

which is the exponential moving average with α = T/(RC + T). That is no coincidence: the exponential moving average is a digital RC filter. The approximation is good when T is much smaller than RC. Lesson 12 meets a better conversion, the bilinear transform, for when it is not.

Drawing a difference equation

Difference equations map directly onto block diagrams built from three parts: multipliers that scale a signal by a coefficient, adders, and unit delays that hold a value for one sample, drawn as a box labelled z⁻¹ for reasons that lesson 12 explains. The exponential moving average needs one delay that stores y[n − 1], one multiplier by 1 − α in the feedback path, one multiplier by α on the input and one adder. The diagram is also the program: each delay is a variable that keeps its value between readings, each multiplier is one multiplication, and the number of delays is the number of values the firmware must remember. That count, the order of the filter, sets its memory and its cost.

A backward-difference RC filter compared with the exact step response, and the exponential moving average on a tachometer step for two smoothing factors.
import math

RC, T = 0.16e-3, 0.02e-3            # time constant and sample interval, seconds
alpha = T / (RC + T)
y, out = 0.0, []
for n in range(60):                  # unit step input from n = 0
    y = (1 - alpha) * y + alpha * 1.0
    out.append(y)
for n in (8, 16, 40):
    exact = 1 - math.exp(-n * T / RC)
    print(f"t = {n*T*1e3:.2f} ms: digital {out[n-1]:.4f}  exact {exact:.4f}")

# Exponential moving average on tachometer readings every 50 ms
readings = [1200] * 5 + [1500] * 25
for a in (0.1, 0.3):
    y = readings[0]; hist = []
    for r in readings:
        y = (1 - a) * y + a * r
        hist.append(y)
    n63 = next(n for n in range(5, 30) if hist[n] >= 1200 + 0.63 * 300) - 4
    print(f"alpha = {a}: 63 % of the step after {n63} readings ({n63*50} ms)")
Output
t = 0.16 ms: digital 0.6103  exact 0.6321
t = 0.32 ms: digital 0.8481  exact 0.8647
t = 0.80 ms: digital 0.9910  exact 0.9933
alpha = 0.1: 63 % of the step after 10 readings (500 ms)
alpha = 0.3: 63 % of the step after 3 readings (150 ms)

The digital filter lags the exact exponential by about two percentage points in the middle of the rise and catches up as both settle. The lag has a simple explanation: the backward-difference filter behaves like an RC filter whose time constant is RC + T instead of RC, one eighth longer here, so a smaller T would make the match closer. On the tachometer step, α = 0.1 reaches 63 % after 10 readings, as the worked example predicted, and α = 0.3 after 3 readings. A larger α follows changes faster but passes more noise, the same trade-off as the moving-average length in lesson 3.

Common mistakes

  • MistakeUsing the initial condition to find A before adding the forced response.

    InsteadAdd y_h and y_p first, then apply y(0) to the complete solution.

  • MistakeCalling a system with initial stored energy linear in its input.

    InsteadOnly the zero-state response is linear in the input; the zero-input response is added on top.

  • MistakeAssuming a first-order system can overshoot.

    InsteadA real first-order system approaches its final value exponentially; overshoot needs complex roots, at least second order.

  • MistakeTreating α in the exponential moving average as a time constant.

    InsteadThe time constant is about 1/α samples, not α.

  • MistakeDiscretizing with a sample interval close to the time constant.

    InsteadBackward differences are accurate only when T is much smaller than the fastest time constant.

Summary and key terms

  • LTI systems built from fixed components obey linear differential equations with constant coefficients; digital LTI filters obey linear difference equations.
  • The complete response is natural plus forced, or equivalently zero-input plus zero-state; convolution gives the zero-state part.
  • The roots of the characteristic equation set the shape of the natural response: real negative roots decay, complex roots ring.
  • An RC circuit moves toward its final value with time constant RC, reaching 63 % after one RC and 99 % after five.
  • FIR filters use only inputs; IIR filters feed back past outputs, and their impulse responses never end.
  • The exponential moving average y[n] = (1 − α)y[n − 1] + αx[n] is a digital RC filter with a time constant of about 1/α samples.
Characteristic equation
The equation in λ obtained by substituting e^(λt) into the homogeneous differential equation.
Natural response
The part of the response set by the system and its initial conditions, with no input.
Forced response
The part of the response that follows the input.
Zero-state response
The response to the input with the system starting at rest; the convolution of the input with h.
Damping ratio
The parameter ζ of a second-order system that decides whether its step response overshoots.
Exponential moving average
The IIR smoother y[n] = (1 − α)y[n − 1] + αx[n].
Difference equation
A recursion relating present and past outputs to present and past inputs.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. Write the differential equation for a series RL circuit with input voltage x(t) and output current y(t).

    Show answer

    L dy/dt + Ry = x.

  2. An RC circuit has RC = 2 ms and starts at 0 V. A 5 V step is applied. What is the output at t = 4 ms?

    Show answer

    5(1 − e^(−2)) ≈ 4.32 V.

  3. Find the characteristic roots of y'' + 4y' + 13y = x.

    Show answer

    λ = −2 ± j3; the system rings at 3 rad/s while decaying with rate 2.

  4. Is y[n] = x[n] + x[n − 1] + 0.5y[n − 1] FIR or IIR?

    Show answer

    IIR, because it feeds back y[n − 1].

  5. Find the impulse response of y[n] = 0.6y[n − 1] + x[n].

    Show answer

    h[n] = 0.6ⁿu[n].

  6. Convert RC = 1 ms with T = 0.1 ms into an exponential moving average. What is α?

    Show answer

    α = T/(RC + T) = 0.1/1.1 ≈ 0.091.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz