ENع
→Smoothing readings
الدرس 23 · Analog: ADC, DAC and sensors

Smoothing noisy readings

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة8 خطوات5 أسئلةIntermediate
عن هذا الدرس

Hold a knob still and analogRead still returns a different number each time: 502, 497, 503, 498. That jitter is noise, and firmware tames it by averaging. Follow an Arduino Uno R3 as it keeps the last 4 readings in a ring buffer with a running total, so each new reading costs just two sums. Then meet the exponential moving average, which needs one variable, see why every smoother lags behind a real change, and watch a median of three throw away a spike that fools both averages. The Signals lessons on moving averages and IIR filters explain the maths behind these filters; here we write them in firmware.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 23 · تفاعلي
01 / 08

Same knob, different numbers

The knob on A0 is held still, yet analogRead returns 502, 497, 503, 498. That jitter is noise: interference picked up by the wires and the supply, plus the converter's own error of up to ±2 steps.

board     Arduino Uno R3 (ATmega328P), knob on A0
ADC       10 bits: 0..1023, about 100 µs per analogRead
readings  502  497  503  498   (knob not moving)
noise     wires and supply pick up interference;
          the ADC itself is accurate to ±2 LSB (steps)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Same knob, different numbers

    The knob on A0 is held still, yet analogRead returns 502, 497, 503, 498. That jitter is noise: interference picked up by the wires and the supply, plus the converter's own error of up to ±2 steps.

    board     Arduino Uno R3 (ATmega328P), knob on A0
    ADC       10 bits: 0..1023, about 100 µs per analogRead
    readings  502  497  503  498   (knob not moving)
    noise     wires and supply pick up interference;
              the ADC itself is accurate to ±2 LSB (steps)
  2. Keep the last four readings

    Each reading goes into an array, buf, at slot i. Then i moves on: 0, 1, 2, 3 and back to 0, so the newest reading always overwrites the oldest. This is called a ring buffer.

    line 4    buf[i] = x;          store the reading in slot i
    line 5    i = (i + 1) % N;     % is the remainder: 3 + 1 -> 0
    buf       [502, 497, 503, 498]
    i         0 -> 1 -> 2 -> 3 -> 0
    ring buffer: a fixed array whose write slot wraps around
  3. Add them up, divide by four

    Line 3 kept a running total: 502 + 497 + 503 + 498 = 2000. Line 6 divides by N: avg = 500. Noise pushes some readings up and some down, so in a sum they partly cancel.

    line 3    total += x - buf[i];  (the empty slots held 0)
    total     502 + 497 + 503 + 498 = 2000
    line 6    avg = total / N;
    avg       2000 / 4 = 500
  4. A new reading pushes one out

    Reading 506 arrives. Line 3 takes the oldest, 502, out of the total and adds 506: 2000 − 502 + 506 = 2004. Line 4 overwrites slot 0, and the average becomes 501. Two sums per reading, however big N is.

    line 2    x = 506
    line 3    total = 2000 - buf[0] + x = 2000 - 502 + 506 = 2004
    line 4    buf[0] = 506       buf = [506, 497, 503, 498]
    line 5    i = 1
    line 6    avg = 2004 / 4 = 501   (whole numbers: 501 -> 501)
  5. A calmer line

    More readings stream in. The raw numbers wander from 496 to 503; the average stays between 499 and 501. Averaging N readings cuts random noise by about √N: four readings halve it, sixteen cut it to a quarter.

    readings  499  501  496  503
    avg       501  501  500  499
    noise     random noise shrinks by about √N:
              N = 4 -> 1/2,  N = 16 -> 1/4,  N = 100 -> 1/10
  6. One number instead of four

    Line 7 keeps an exponential moving average, ema. Each reading moves it a quarter of the way toward itself: reading 498 takes ema from 500.4 to 499.8. It needs no array, just one variable.

    line 7    ema += (x - ema) / 4;
    reading 498:  500.4 + (498 - 500.4) / 4 = 499.8
    same as   ema = 0.25 * x + 0.75 * ema   (a single pole low-pass filter)
    memory    one float, whatever the smoothing
  7. The price: lag

    Now the knob turns and readings jump to about 540. avg climbs 510, 519, 531, 540: four readings, 0.4 s, to catch up. ema is slower still: 511, 517, 524, 528. Smoother always means slower.

    readings  541  538  543  539   knob turned
    avg       510  519  531  540   caught up after N = 4 readings
    ema       511  517  524  528   about 2/3 of the way after 4
    time      one reading per 100 ms: 4 readings = 0.4 s
    bigger N (or a smaller fraction): smoother and slower
  8. A spike fools both averages

    One glitch reads 600. avg jumps to 555 for four readings; ema jumps to 550 and fades slowly. A median of the last three readings takes the middle one, 542: the spike simply disappears.

    readings  600  541  539  542
    avg       555  555  555  555
    ema       550  548  546  545
    median 3  542  542  541  541
    
    int median3(int a, int b, int c) {   // the middle of three
      if ((a <= b) == (b <= c)) return b;
      if ((b <= a) == (a <= c)) return a;
      return c;
    }

اختبار يتاح بعد الخطوة الأخيرة.