Smoothing

Hi
Smoothing is working Ok until input amplitude is less than 0.6V after that red line is going to negative values ?
Blue line is without smoothing

// Set things up
 
#include <Goertzel.h>
//int sensorPin = A0;
int led = 13;
//const float TARGET_FREQUENCY = 500; 
const float TARGET_FREQUENCY = 5000; 
const int N = 100; 
//const float THRESHOLD = 4000;
const float THRESHOLD = 400;
// Nyquist says the highest frequency we can target is SAMPLING_FREQUENCY/2 
const float SAMPLING_FREQUENCY = 8900; 
// Create the filter
Goertzel goertzel = Goertzel(TARGET_FREQUENCY, N, SAMPLING_FREQUENCY);
//////////////////////////////////////////
const int numReadings = 10;
 
int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average
int inputPin = A0;
////////////////////////////////////////
void setup(){
  pinMode(led, OUTPUT);     
 // Serial.begin(9600); 
   Serial.begin(115200);
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}
 
void loop()
{
 
// Take samples  
  // goertzel.sample(sensorPin);
     goertzel.sample(inputPin);
// Check samples for tone at target
   float magnitude = goertzel.detect();
    /////////////////////////
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  //readings[readIndex] = analogRead(inputPin);
   readings[readIndex] = magnitude;
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;
 
  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }
 
  // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
// Serial.println(average);
  delay(1);        // delay in between reads for stability
 
// if the tone is present, light LED
   if(magnitude>THRESHOLD)
     digitalWrite(led, HIGH);
   else
     digitalWrite(led, LOW);
       Serial.print(magnitude);
  // Serial.print("\t"); 
     // Serial.print(currentaverage);
   Serial.print("\t"); 
       Serial.println(average);
}

can you post your raw data: each sample, readIndex and corresponding total and average

Typically a smoother would be a rolling average of n readings over a sample of say X readings. Where X is kinda' large? Is this what you are trying to do? Because it looks like your averaging all your readings. Not sure, hard to follow what your up to.

-jim lee

int total = 0;                  // the running total

try

long total = 0;                  // the running total

The variable total is overflowing. Declare it as long.

Thanks
long total = 0 = solved the problem