#ifndef FILTERS_H
#define FILTERS_H

#include <Arduino.h>
#include <math.h>

// High-pass filter (removes gravity/drift)
struct HighPass {
  float a, x1, y1;
  HighPass(float coef) : a(coef), x1(0), y1(0) {}
  inline float step(float x) {
    float y = (x - x1) + a * y1; x1 = x; y1 = y; return y;
  }
};

// Exponential smoothing (EMA)
struct LowPass {
  float y, alpha;
  LowPass(float a) : y(0), alpha(a) {}
  inline float step(float x) {
    y += alpha * (x - y); return y;
  }
};

// Hann window for FFT
inline void makeHann(float *w, uint16_t n) {
  for (uint16_t i=0;i<n;i++)
    w[i] = 0.5f*(1.0f-cosf(2.0f*PI*i/(float)(n-1)));
}

inline void applyWindow(float *x,const float*w,uint16_t n){
  for(uint16_t i=0;i<n;i++) x[i]*=w[i];
}

#endif