diff options
Diffstat (limited to 'examples/test-session.cpp')
-rw-r--r-- | examples/test-session.cpp | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/examples/test-session.cpp b/examples/test-session.cpp index 97d5754..ea75410 100644 --- a/examples/test-session.cpp +++ b/examples/test-session.cpp @@ -12,6 +12,9 @@ // Default USART baud rate #define BAUD 9600 +// Number of times to sample a pin per ADC noise measurement +#define N_ADC_NOISE_MEASUREMENTS 40 + uint8 gpio_state[BOARD_NR_GPIO_PINS]; const char* dummy_data = ("qwertyuiopasdfghjklzxcvbnmmmmmm,./1234567890-=" @@ -768,21 +771,23 @@ void measure_adc_noise(uint8 pin) { // Variance algorithm from Welford, via Knuth, by way of Wikipedia: // http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#On-line_algorithm - for (int i = 1; i <= N; i++) { - x = analogRead(pin); - delta = x - mean; - mean += delta / i; - M2 = M2 + delta * (x - mean); - } - - SerialUSB.print("header: D"); - SerialUSB.print(pin, DEC); - SerialUSB.print("\tn: "); - SerialUSB.print(N, DEC); - SerialUSB.print("\tmean: "); - SerialUSB.print(mean); - SerialUSB.print("\tvariance: "); - SerialUSB.println(M2 / (float)(N-1)); + for (int sample = 0; sample < N_ADC_NOISE_MEASUREMENTS; sample++) { + for (int i = 1; i <= N; i++) { + x = analogRead(pin); + delta = x - mean; + mean += delta / i; + M2 = M2 + delta * (x - mean); + } + SerialUSB.print("header: D"); + SerialUSB.print(pin, DEC); + SerialUSB.print("\tn: "); + SerialUSB.print(N, DEC); + SerialUSB.print("\tmean: "); + SerialUSB.print(mean); + SerialUSB.print("\tvariance: "); + SerialUSB.println(M2 / (float)(N-1)); + } + pinMode(pin, OUTPUT); } |