rlogiacco / CircularBuffer

Arduino circular buffer library
GNU Lesser General Public License v3.0
312 stars 85 forks source link

severe calculating issue #29

Closed DO1TKN closed 4 years ago

DO1TKN commented 4 years ago

Hi! by testing with easy numbers, like 1000 for each etry of the buffer the calculation was wrong. I tryed to understand the following example:

CircularBuffer/examples/CircularBuffer/CircularBuffer.ino

please mention to change the line 26 from:

        avg += buffer[i] / buffer.size();

to:

        avg += buffer[i] / (float)buffer.size();

logfile and code with output:

11:01:27.395 -> 0 1000 0.00 24.00 1 1000 24.00 48.00 2 1000 48.00 72.00 3100072.0096.004100096.00120.0051000120.00144.0061000144.00168.0071000168.00192.0081000192.00216.0091000216.00240.00101000240.00264.00111000264.00288.00121000288.00312.00131000312.00336.00141000336.00360.00151000360.00384.00161000384.00408.00171000408.00432.00181000432.00456.00191000456.00480.00201000480.00504.00211000504.00528.00221000528.00552.00231000552.00576.00241000576.00600.00251000600.00624.00261000624.00648.00271000648.00672.00281000672.00696.00291000696.00720.00301000720.00744.00311000744.00768.00321000768.00792.00331000792.00816.00341000816.00840.00351000840.00864.00361000864.00888.00371000888.00912.00381000912.00936.00391000936.00960.00401000960.00984.00Average is 984.00

Average is 984.00

Avarage shall be 1000

after change of line 26:

11:25:56.682 ->

0 1000 0.00 24.39 1 1000 24.39 48.78 2 1000 48.78 73.17 3 1000 73.17 97.56 4100097.56121.9551000121.95146.3461000146.34170.7371000170.73195.1281000195.12219.5191000219.51243.90101000243.90268.29111000268.29292.68121000292.68317.07131000317.07341.46141000341.46365.85151000365.85390.24161000390.24414.63171000414.63439.02181000439.02463.41191000463.41487.80201000487.80512.20211000512.20536.59221000536.59560.98231000560.98585.37241000585.37609.76251000609.76634.15261000634.15658.54271000658.54682.93281000682.93707.32291000707.32731.71301000731.71756.10311000756.10780.49321000780.49804.88331000804.88829.27341000829.27853.66351000853.66878.05361000878.05902.44371000902.44926.83 38 1000 926.83 951.22 39 1000 951.22 975.61 40 1000 975.61 1000.00

Average is 1000.00

include

CircularBuffer<int, 41> buffer;

unsigned long timeI = 0;

define SAMPLE_PIN 12

void setup() { Serial.begin(115200); pinMode(SAMPLE_PIN, INPUT); timeI = millis(); }

void loop() { // samples A0 and prints the average of the latest hundred samples to console every 500ms //int reading = analogRead(SAMPLE_PIN); int reading = 1000; buffer.push(reading);

if (millis() - timeI >= 500) {
    timeI = millis();
    float avg = 0.00;
    // the following ensures using the right type for the index variable
    using index_t = decltype(buffer)::index_t;
    for (index_t i = 0; i < buffer.size(); i++) {
        Serial.print(i);
        Serial.print( buffer[i] );
  Serial.print(avg);
  avg += buffer[i] / (float)buffer.size();
  Serial.print(avg);
    }
    Serial.print("Average is ");
    Serial.println(avg);
}

}

//DO1TKN