A446 of 56
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

A4 · Lesson 46 · Interpolation and lookup tables

Reading between the rows of a table

A thermistor's datasheet gives its resistance every 5 °C, and your reading falls between two rows. Two data loggers sampled the same test at different moments, and to compare them you need both at the same times. A sensor's calibration is a short table measured on the bench. Lesson 45's fitting finds a smooth model through noisy points; interpolation does something different: it trusts the table's points exactly and estimates what lies between them. How you draw the line between two points decides how wrong the answer is.

Predict

A 10 kΩ thermistor's table gives 10.000 kΩ at 25 °C and 8.037 kΩ at 30 °C. A straight line between them gives 9.019 kΩ at 27.5 °C. Is the true resistance at 27.5 °C higher or lower?

Name it

Linear interpolation

Between two table points (x0,y0) and (x1,y1), the straight line gives

y=y0+(y1−y0)x−x0x1−x0

np.interp(x, xp, fp) does this for a whole table: it finds the interval holding each x and applies the formula. Two rules: xp must be increasing, and outside the table it clamps, returning the first or last fp instead of extrapolating.

Interpolate the right quantity

Linear interpolation is exact for data that's linear and nearly exact for data that's nearly linear. So transform first:

  • an exponential like a thermistor: interpolate ln⁡R, against 1/T for the β model, where the curve becomes a straight line;
  • a quantity spanning decades: interpolate its logarithm;
  • a filter's response: interpolate decibels against log⁡f.

Then transform back. The table is the same; only the line you draw between its points changes.

Inverse lookup

To go from a measured resistance back to a temperature, swap the roles: interpolate temperature as a function of resistance. np.interp needs the x values increasing, and a thermistor's resistance falls with temperature, so reverse both arrays: np.interp(R, Rs[::-1], Ts[::-1]). This only works if the table is monotonic: one temperature for each resistance.

Resampling

To compare two records sampled at different times, interpolate both onto one common grid of times, then subtract. Resample onto a grid inside both records' time spans, or np.interp's clamping will invent flat ends.

When to fit instead

Interpolation assumes the table is right. If the points are noisy measurements, interpolating passes through every bit of noise; fit a model instead (lesson 45). And interpolating with one high-degree polynomial through many points is a trap: it wiggles wildly between them near the ends. Piecewise straight lines, or a transformed quantity, are safer.

Worked example

A thermistor table lists R every 5 °C from the β model (R25 = 10 kΩ, β = 3950 K). Estimate R at 27.5 °C by interpolating R linearly, then by interpolating ln⁡R against 1/T.

Show the solution
  1. Build the table and interpolate both ways. Linearly in R, and linearly in ln⁡R against 1/T.
import numpy as np
Ts = np.arange(0, 101, 5.0)
Rs = 10e3 * np.exp(3950 * (1 / (Ts + 273.15) - 1 / 298.15))

lin = np.interp(27.5, Ts, Rs)                        # 9018.6
u = 1 / (Ts + 273.15)                                # falls as T rises
lg = np.exp(np.interp(1 / 300.65, u[::-1], np.log(Rs)[::-1]))   # 8956.9
print(lin, lg)
  1. The straight line in R. It gives 9018.6 Ω, 0.69% above the true 8956.9 Ω.
  2. The straight line in ln R against 1/T. It gives 8956.9 Ω, right to the last digit, because the β model is exactly a straight line in those coordinates.
  3. Why the arrays are reversed. 1/T falls as T rises, and np.interp wants increasing x values.

Answer the true value is 8956.9 Ω; interpolating R gives 9018.6 Ω (0.69% high), and interpolating ln⁡R against 1/T gives 8956.9 Ω.

Worked example

The same thermistor reads 9.200 kΩ. Use the 5 °C table to find the temperature, and compare with the exact β-model answer.

Show the solution
  1. Look up the resistance in the reversed table. Resistance falls with temperature, so both arrays are reversed to make the resistances increasing.
  2. Invert the β model for the exact answer. 1T=1298.15+ln⁡(R/R25)β
T_est = np.interp(9200, Rs[::-1], Ts[::-1])                # 27.04 °C
T_exact = 1 / (1 / 298.15 + np.log(9200 / 10e3) / 3950) - 273.15   # 26.89 °C
  1. Compare. The table lookup gives 27.04 °C against the exact 26.89 °C: 0.15 °C off, from the same curvature as before.
  2. Improve it. A finer table or interpolating ln⁡R would shrink it, and for a firmware lookup table that's usually the trade: a little memory for a lot of accuracy.

Answer the table gives 27.04 °C, 0.15 °C above the exact 26.89 °C.

Worked example

Logger A recorded 1.00, 1.18, 1.42, 1.60, 1.76, 2.04 V at 0, 0.9, 2.1, 3.0, 3.8, 5.2 s. Logger B recorded 1.11, 1.29, 1.52, 1.68, 1.91 V at 0.5, 1.5, 2.5, 3.5, 4.5 s. Compare them at 1, 2, 3 and 4 s.

Show the solution
  1. Interpolate both loggers onto a common grid. Whole seconds from 1 to 4 s, then subtract.
ta = np.array([0, 0.9, 2.1, 3.0, 3.8, 5.2]); va = np.array([1.00, 1.18, 1.42, 1.60, 1.76, 2.04])
tb = np.array([0.5, 1.5, 2.5, 3.5, 4.5]);    vb = np.array([1.11, 1.29, 1.52, 1.68, 1.91])
g = np.arange(1, 5.0)
print(np.interp(g, ta, va) - np.interp(g, tb, vb))   # [0, -0.005, 0, 0.005]
  1. Read the grid values. A reads 1.20, 1.40, 1.60, 1.80 V and B reads 1.20, 1.405, 1.60, 1.795 V.
  2. Why resample. Neither logger sampled at whole seconds, so without resampling there's no pair of readings taken at the same moment to compare.
  3. Stay inside the data. The grid stays inside both records (B starts at 0.5 s and ends at 4.5 s).

Answer at 1, 2, 3 and 4 s the loggers differ by 0, −5, 0 and +5 mV, so they agree within 5 mV.

Check

Write interp(x, xs, ys) for a single x inside the range of an increasing list xs: find the interval xs[i] <= x <= xs[i + 1] with bisect.bisect_right, then apply the straight-line formula. Set y = [interp(x, xs, ys) for x in queries] for the starter's table and queries.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Build a β-model thermistor table (R25 = 10 kΩ, β = 3950 K) from 0 to 100 °C every step = 5 °C. At T = 27.5 °C, set r_lin by interpolating R linearly in temperature, r_log by interpolating ln⁡R against 1/T (kelvins), and err_lin, err_log to their errors in percent against the exact value.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Using a 5 °C table of the same thermistor from 0 to 100 °C, set T_est to the temperature for a measured resistance of Rm = 6500 Ω by interpolating temperature against resistance, and T_exact from the β model, T=1/(1/298.15+ln⁡(R/R25)/β)−273.15.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

The starter gives two loggers' records of the same signal at irregular times, (ta, va) and (tb, vb). Resample both onto a grid of whole seconds that lies inside both records, set g to that grid, d to the differences A minus B on it, and rms to the RMS of d.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

A table holds y = 4.7 at x = 20 and y = 5.8 at x = 25. What does linear interpolation give at x = 21?

Check

np.interp(150, [0, 50, 100], [1.0, 2.0, 2.5]) asks for a value beyond the table. What does it return?

Explain why

Why does interpolating ln⁡R against 1/T beat interpolating R against T for a thermistor table, using the same table?

Practice

Keep practicing

7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.