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?
Lower: it's 8.957 kΩ. A thermistor's resistance falls exponentially with temperature, a curve that bends upward (it's convex), so the straight line between two points on it always lies above the curve. The error is 0.7% here, about 0.16 °C. Interpolating the logarithm of the resistance, which is nearly straight, does far better.
Name it
Linear interpolation
Between two table points and , the straight line gives
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 , against for the β model, where the curve becomes a straight line;
- a quantity spanning decades: interpolate its logarithm;
- a filter's response: interpolate decibels against .
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 every 5 °C from the β model ( = 10 kΩ, β = 3950 K). Estimate at 27.5 °C by interpolating linearly, then by interpolating against .
Show the solution
- Build the table and interpolate both ways. Linearly in , and linearly in against .
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)
- The straight line in R. It gives 9018.6 Ω, 0.69% above the true 8956.9 Ω.
- 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.
- Why the arrays are reversed. falls as rises, and
np.interpwants increasing x values.
Answer the true value is 8956.9 Ω; interpolating gives 9018.6 Ω (0.69% high), and interpolating against 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
- Look up the resistance in the reversed table. Resistance falls with temperature, so both arrays are reversed to make the resistances increasing.
- Invert the β model for the exact answer.
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
- Compare. The table lookup gives 27.04 °C against the exact 26.89 °C: 0.15 °C off, from the same curvature as before.
- Improve it. A finer table or interpolating 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
- 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]
- 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.
- Why resample. Neither logger sampled at whole seconds, so without resampling there's no pair of readings taken at the same moment to compare.
- 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 ( = 10 kΩ, β = 3950 K) from 0 to 100 °C every step = 5 °C. At T = 27.5 °C, set r_lin by interpolating linearly in temperature, r_log by interpolating against (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, .
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 = 4.7 at = 20 and = 5.8 at = 25. What does linear interpolation give at = 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 against beat interpolating against for a thermistor table, using the same table?
Linear interpolation is exact when the data really is a straight line between the points and wrong by however much it curves. A thermistor follows , which curves strongly in against , so straight chords miss it. Taking logs gives , a straight line in , so the same table points joined by straight lines in those coordinates trace the model exactly. Any quantity whose behavior you know can be transformed until it's nearly straight before interpolating.
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.