Back to: Multiplier Event Luxembourg
The pulse-sensing values tend to be noisy or inconsistent, with some readings being too high and others too low. To address this, we smooth the data by calculating an average of ten readings taken as fast as possible. This process helps reduce noise and ensures more stable and accurate results, known as oversampling.
# Define the number of light readings taken per sample. NUM_OVERSAMPLE = 10
# The NUM_OVERSAMPLE number of light readings is taken really fast. oversample = 0 # Sum up all the oversamples (light readings taken per sample). for o in range(NUM_OVERSAMPLE): oversample += cp.light # Calculate the average of the oversamples, and save the value. samples[s] = oversample / NUM_OVERSAMPLE
The readings should be centred around zero. Since the original samples are all positive values, their average is calculated, and then all values are shifted downward by this average to align them around zero. Consequently, instead of consistently being greater than zero, the values will vary above and below zero, resulting in an average equal to zero. This technique is “removing the DC bias” from the signal.

(https://learn.adafruit.com/assets/53938)
Only the most recent twenty samples are retained for calculating the average. When a new sample is received, the oldest sample is dropped, and the average is recalculated using the twenty most recent values. This method is referred to as a “moving average”.
# Define the number of samples taken to calculate the average. NUM_SAMPLES = 20
# Cycle through a range of NUM_SAMPLES values. for s in range(NUM_SAMPLES):
# Calculate the average of the samples. # When the next sample comes along, we drop the oldest one and # recompute the average of the NUM_SAMPLES most recent values. mean = sum(samples) / len(samples) # Subtract the average of the samples from the sampled value # * to center the light readings around zero, or # * to remove the DC bias, i.e., the mean value of the waveform. pulse = samples[s] - mean