From 5478c61a471f0a6b0668aeaecb027609effdee90 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Wed, 31 Aug 2011 11:35:27 -0400 Subject: test-session.cpp: Multiple ADC noise measurements by default. Each call to measure_adc_noise() now does N_ADC_NOISE_MEASUREMENTS (currently 40) samples, instead of just 1. --- examples/test-session.cpp | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) (limited to 'examples') 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); } -- cgit v1.2.3