Simvik

Oversample for extra bits

Gain resolution from a noisy ADC by summing and shifting. A medium embedded C problem (ADC), run and graded in the browser.

medium · ADC · adc · oversampling · resolution

Averaging only reduces noise. Oversampling can also add resolution: summing 4^n samples and shifting the sum right by n gives an ADC reading with n extra bits, provided there is some noise to dither the input.

count is always 4 to the power extra_bits. Return the oversampled reading. The result for a 12-bit ADC with 4 extra bits fills 16 bits.

Starter code

uint16_t oversample(const uint16_t *samples, size_t count, uint8_t extra_bits) {
  uint16_t sum = 0;
  for (size_t i = 0; i < count; i++) sum += samples[i];
  return sum / count;
}

Example tests

One extra bit from four samples

uint16_t s[4] = { 100, 101, 101, 102 };
printf("%u\n", oversample(s, 4, 1));

Expected output:

202

Two extra bits at full scale

uint16_t s[16];
for (int i = 0; i < 16; i++) s[i] = 4095;
printf("%u\n", oversample(s, 16, 2));

Expected output:

16380

Learn it first

  • ADC · Resolution, sampling, noise, oversampling and DMA.

More ADC problems