Hi
I need to smooth data after some processing, this line and
readings[readIndex] = (vReal[40] / 20, 1);
the results on pic, numbers
so I did changes for smoothing,
// readings[readIndex] = analogRead(inputPin);
readings[readIndex] = (vReal[40] / 20, 1);
//Serial.println(vReal[40], 1);
Serial.println(average);
the results on pic, numbers 2
What am I doing wrong?
Why I am receiving average = 1 instead 23600 ?
#include "arduinoFFT.h"
#define SAMPLES 128
#define SAMPLING_FREQUENCY 40000
int i;
arduinoFFT FFT = arduinoFFT();
unsigned int sampling_period_us;
unsigned long microseconds;
double vReal[SAMPLES];
double vImag[SAMPLES];
/////////////////////////////////
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
//////////////////////////////////
void setup() {
// Serial.begin(115200);
Serial.begin(9600);
sampling_period_us = round(1000000 * (1.0 / SAMPLING_FREQUENCY));
////////////////////////////
for (int thisReading = 0; thisReading < numReadings; thisReading++)
{
readings[thisReading] = 0;
}
///////////////////////////
}
void loop() {
/*SAMPLING*/
for (int i = 0; i < SAMPLES; i++)
{
microseconds = micros(); //Overflows after around 70 minutes!
vReal[i] = analogRead(0);
vImag[i] = 0;
while (micros() < (microseconds + sampling_period_us)) {
}
}
/*FFT*/
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
//////////
// readings[readIndex] = analogRead(inputPin);
readings[readIndex] = (vReal[40] / 20, 1);
//////////
// 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;
// Serial.println(vReal[40], 1);
Serial.println(average);
}