   sht30.begin();
   PDM.onReceive(onPDMdata);
   PDM.begin(1, 16000);
+
+  // init hann window
+  for (int i = 0; i < HANN_SIZE ; i++) {
+    float sinVal = sin(PI * (i + HANN_SIZE) / (2.0f * HANN_SIZE));
+    hannWindow[i] = sinVal * sinVal;
+  }
+
 }
 
 void loop(void) {

#include <PDM.h>
#include <KickFFT.h>

#define ACC_SIZE 128
#define FFT_SIZE 256

#define LED_PIN 13




void addToFFT(float val) {
  val = abs(val); // rectify val
  float windowedVal = 0;
  for (int i = HANN_SIZE - 1; i > 0; i--) {
    incomingWindow[i] = incomingWindow[i - 1];
    windowedVal += incomingWindow[i] * hannWindow[i];
  }
  windowedVal += val;
  incomingWindow[0] = val;
  
  for (int i = FFT_SIZE - 1; i > 0; i--) {
    fftBuffer[i] = fftBuffer[i-1];
  }

  fftBuffer[0] = max(0, windowedVal - prevWindowedVal); // half recitify difference

  prevWindowedVal = windowedVal;

  float mag[END_INDEX];
  float phase[END_INDEX];
  uint16_t startIndex, endIndex;
  // KickFFT<int32_t>::fft(fs, 0, 4, FFT_SIZE, fftBuffer, mag, startIndex, endIndex);
  fft_phase(FS, 0, F2, FFT_SIZE, fftBuffer, mag, phase, startIndex, endIndex);
  updatePhase(mag, phase, startIndex, endIndex);
  float hz = FS * _phaseRateAverage / 2 / PI;
  Serial.print("endIndex: "); Serial.println(endIndex);
  Serial.print("END_INDEX: "); Serial.println(END_INDEX);
  // Serial.print("BPM: "); Serial.println(hz * 60);
  //Serial.print("radsPerUpdate: "); Serial.println(_phaseRateAverage);

