smoothing

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);
 
}

After you do this...

FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);

then vReal[40] no longer contains a sample of the original data. It's the computed real component of the FFT. (Then there's more code after this which changes it yet again.)

You want to average or smooth the output of the FFT? That doesn't make a lot of sense.

What am I doing wrong?

First, clearly describe what you are doing.

Then we can talk about what you might be doing wrong.

I am reading amplitude of 12 kHz signal , which corresponds to fft bin #40, because of ftt noise the numbers are not stable, they vary between 23 558 - 23 838, so the average is around 23 670 and this is what I want to be printed by "Serial.println(average);