C12 · Lesson 26 · Embedded lab
Sine tables, DDS, thermistor tables, quantization and filters, in Python and C
The analog half of the course (the ADC, fixed point, filtering, PWM and the DAC) comes down to tables and integer arithmetic worked out in advance. Firmware stores a sine table instead of calling sin, a thermistor table instead of computing a logarithm, and a filter as a shift instead of a multiply. This lab generates those tables in Python, measures how accurate they are, checks the quantization limit of the converters, and then runs the integer smoother from the filtering lesson in C on the board.
- The curve is the error of a thermistor table with linear interpolation: the table holds the exact temperature every
stepADC codes, and readings in between are interpolated. - The error is zero at the table's points and largest between them, and worst at the ends of the range, where the thermistor's curve bends most.
- Halve the step: the error drops by about a factor of four, as linear interpolation's error should. A 17-entry table (step 256) is within about 0.74 °C from code 512 to 3584; 33 entries bring it to 0.23 °C.
Predict
A 12-bit converter is driven by a full-scale sine. What signal-to-quantization-noise ratio does a perfect one give?
dB for N = 12. Each extra bit is worth 6 dB. The lab measures it directly by quantizing a sine and comparing the error's power with the signal's.
Name it
The tables
- Sine table: entries, , spanning the DAC's 0 to 4095.
- DDS step: , giving exactly .
- Thermistor table: the exact temperature every
stepcodes; in between, linear interpolation, whose error grows with the square of the step. - Quantization: a full-scale sine on an ideal N-bit converter has an SNR of dB.
The thermistor here is the course's: 10 kΩ at 25 °C, B = 3950 K, from 3.3 V to the pin, with 10 kΩ to ground, so for code c.
Check
Write sine_table(n): n entries of for k = 0 to n − 1, and dds(f, fs, bits=32): return (step, actual frequency) for a phase accumulator of that many bits.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write adc_to_temp(code): the exact temperature in °C for the course's thermistor at ADC code code. Then max_table_error(lo, hi, step): build a table of exact temperatures at multiples of step and return the largest absolute error of linear interpolation for codes lo to hi inclusive.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write sqnr(bits, n=4096): quantize , k = 0 to n − 1, to bits bits over −1 to 1 (map to 0 to , round to the nearest code, map back), and return the ratio of the signal's power to the error's power in dB.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write smooth(xs, k): the integer exponential smoother with state s starting at xs[0] << k, updated by s += x - (s >> k) for every sample, outputting s >> k. Then settle(xs, k, target, tol): the index of the first output within tol of target that stays within it for the rest of the list, or None.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Run the smoother with k = 2 over these sixteen readings on the board: state starting at the first reading shifted left by 2, s += x - (s >> 2), output s >> 2. Print the outputs after the 8th, 12th and 16th readings.
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Explain why
Why does linear interpolation in a thermistor table get four times more accurate when the table's step is halved?
Between two table points, interpolation replaces the curve with a straight chord. The gap between a smooth curve and its chord is set by how much the curve bends over that interval: it's about one-eighth of the second derivative times the square of the interval's width. Halving the step halves the width, so the gap shrinks by a factor of four. That's why the error is worst where the thermistor's curve bends most, at the ends of the range, and why a modest increase in table size buys a large gain in accuracy.