Problem 212020 · easy · Level 02 Linear Data Structures

Footsteps on a Footbridge

impulse response · superposition · linearity · time invariance · lists

An engineer checking a footbridge strikes it once with a soft hammer and records the vibration with an accelerometer, one sample every 10 ms. That recording, h, is the bridge's impulse response: its answer to a single short tap of strength 1 at sample 0. For small vibrations a bridge behaves as a linear, time-invariant (LTI) system:

  • a tap twice as strong gives a vibration twice as big (scaling),
  • a tap k samples later gives the same vibration k samples later (time invariance),
  • several taps give the sum of their separate vibrations (superposition).

So the impulse response is a fingerprint: from it alone you can predict the bridge's answer to any pattern of taps. Footsteps are such taps. A tap of strength s at sample k adds s * h[j] to sample k + j of the output, for every j in h.

Write bridge_response(h, taps, n) where taps is a list of pairs (k, s) (the sample number and the strength of one footstep). Return the predicted vibration as a list of n floats, samples 0 to n - 1, starting from a bridge at rest. Vibration that would come after sample n - 1 is not part of the record. Two footsteps may land on the same sample. Press Run with plot(bridge_response(h, taps, n), kind="stem") to see the copies overlap.

Examples

Input:  h = [0, 4, 2, 1], taps = [(0, 1), (2, 0.5)], n = 6
Output: [0.0, 4.0, 2.0, 3.0, 1.0, 0.5]
Explanation: the first step gives [0, 4, 2, 1] from sample 0; the second gives
half of h from sample 2: [0, 2, 1, 0.5] at samples 2 to 5. Add them sample by sample.

Input:  h = [3, 1], taps = [(4, 2)], n = 5
Output: [0.0, 0.0, 0.0, 0.0, 6.0]
Explanation: the step at sample 4 gives 6 there; its 2 at sample 5 falls after the record.

Goals

  • Read an impulse response as the system's answer to one short tap
  • Build the answer to several taps from shifted, scaled copies of it
  • Keep a fixed-length record: what happens after the end falls off
Starting Python…