C1226 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 step ADC 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?

Name it

The tables

  • Sine table: n entries, ⌊2047.5+2047.5sin⁡(2πk/n)+0.5⌋, spanning the DAC's 0 to 4095.
  • DDS step: step=round(f⋅232/fs), giving exactly step⋅fs/232.
  • Thermistor table: the exact temperature every step codes; 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 6.02N+1.76 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 R=10 kΩ×(4095−c)/c for code c.

Check

Write sine_table(n): n entries of ⌊2047.5+2047.5sin⁡(2πk/n)+0.5⌋ 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 sin⁡(2π⋅7k/n), k = 0 to n − 1, to bits bits over −1 to 1 (map to 0 to 2bits−1, 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?