Hi
I have a program to remove DC offset from signal, it is working OK itself, when I added it to another program a big delay occurred, over 15 sec. After uploading the sketch serial plotter is dead for 30 sec. then signal is appearing. When I turn signal ON/OFF the blue line is responding instantly (input signal) from 0 to 70 000 on scale but DC removed signal is changing slowly (red line), to get to 0 it is take over 15 sec.
It is possible to fix it ?
#include "arduinoFFT.h"
#define SAMPLES1 128 //Must be a power of 2
#define SAMPLING_FREQUENCY 40000 //Hz, must be less than 10000 due to ADC
arduinoFFT FFT = arduinoFFT();
unsigned int sampling_period_us;
unsigned long microseconds;
double vReal[SAMPLES1];
double vImag[SAMPLES1];
///////////////////////////////
int sample = vReal[40];
//int sample = 0;
int last_sample = 0;
//long shifted_filter = -10000;
long shifted_filter = -1;
//////////////////////////////
void setup() {
Serial.begin(115200);
sampling_period_us = round(1000000 * (1.0 / SAMPLING_FREQUENCY));
}
void loop() {
/*
/////////////////////////
last_sample = sample;
sample = vReal[40];
long shiftedFCL = shifted_filter + (long)((sample - last_sample) << 8);
//long shiftedFCL = shifted_filter + (long)((vReal[40] - last_sample) << 8);
shifted_filter = shiftedFCL - (shiftedFCL >> 8);
long filtered_value = (shifted_filter + 128) >> 8;
///////////////////////////
*/
/*SAMPLING*/
for (int i = 0; i < SAMPLES1; i++)
{
microseconds = micros(); //Overflows after around 70 minutes!
vReal[i] = analogRead(PA7);
vImag[i] = 0;
while (micros() < (microseconds + sampling_period_us)) {
}
}
/*FFT*/
FFT.Windowing(vReal, SAMPLES1, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES1, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES1);
double peak = FFT.MajorPeak(vReal, SAMPLES1, SAMPLING_FREQUENCY);
///////////////////////////////
last_sample = sample;
sample = vReal[40];
long shiftedFCL = shifted_filter + (long)((sample - last_sample) << 8);
//long shiftedFCL = shifted_filter + (long)((vReal[40] - last_sample) << 8);
shifted_filter = shiftedFCL - (shiftedFCL >> 8);
long filtered_value = (shifted_filter + 128) >> 8;
///////////////////////////////
/*PRINT RESULTS*/
Serial.print(vReal[40]); //blue
/////////////////
//Serial.print(' ');
//Serial.print(sample); //red
Serial.print(' ');
Serial.println(filtered_value);// green
////////////////
}