double coef[] ={
0.000596975049640344917513401501452108278,
0.001540319638759950161485678954420563969,
0.00330643202765853278998764430696155614,
0.006111356348794970416715699457199661992,
0.010163018665706853582708646399623830803,
0.01556871821436695349505363594744267175,
0.022287913896844955413678590616655128542,
0.030101200648240900026308963788324035704,
0.038603810412670296059456376269736210816,
0.047231317739074019090850242719170637429,
0.055313182265383349645038180142364581116,
0.062153319101224388520954278192220954224,
0.067119721211364419266587333368079271168,
0.069732498345159057984155026588268810883,
0.069732498345159057984155026588268810883,
0.067119721211364419266587333368079271168,
0.062153319101224388520954278192220954224,
0.055313182265383349645038180142364581116,
0.047231317739074019090850242719170637429,
0.038603810412670296059456376269736210816,
0.030101200648240900026308963788324035704,
0.022287913896844955413678590616655128542,
0.01556871821436695349505363594744267175,
0.010163018665706853582708646399623830803,
0.006111356348794970416715699457199661992,
0.00330643202765853278998764430696155614,
0.001540319638759950161485678954420563969,
0.000596975049640344917513401501452108278
};
const int N_COEF = 28;
double buffer[N_COEF] = {0.0};
volatile bool flag = 0;
int indice_buffer = 0;
double y_filtrada = 0.0;
unsigned long T = 50;
hw_timer_t * timer = NULL; // Puntero al timer
#define ADC_PIN 35
// ISR (Interrupt Service Routine) - SE EJECUTA A 20kHz
void IRAM_ATTR onTimer() {
flag = 1;
}
void setup() {
// Configurar puerto serie
Serial.begin(115200);
timer = timerBegin(100000); // timer a 1MHz
timerAttachInterrupt(timer, &onTimer); // Ligar ISR
timerAlarm(timer, 1000, true,0); // Alarma cada 1000 mirosegundos -> 1ms
}
void loop() {
if (flag) {
// 2. Leer la nueva muestra y guardarla en el búfer circular
// (Sobrescribe la muestra más antigua, sin mover el array)
buffer[indice_buffer] = analogRead(ADC_PIN);
// 3. Aplicar la Ecuación del Filtro FIR (Convolución Circular)
int k_idx = indice_buffer; // 'k_idx' es nuestro puntero de lectura
y_filtrada = 0.0;
for (int i = 0; i < N_COEF; i++) {
// y[n] = b[0]*x[n] + b[1]*x[n-1] + ...
y_filtrada += coef[i] * buffer[k_idx];
// Mover el puntero de lectura hacia atras
k_idx--;
if (k_idx < 0) { k_idx = N_COEF - 1;} // Volver al final del buffer
}
indice_buffer++; // mover el índice del buffer
if (indice_buffer >= N_COEF) {
indice_buffer = 0; // Volver al inicio del buffer
}
}
if (millis()>T) {
T +=10;
Serial.print(analogRead(ADC_PIN));
Serial.print(", ");
Serial.println(y_filtrada);
}
}