const float sample_freq = 800; int rawData[] = {-20, 24, 10, 10, 0, 8, 16, 14, 26, 16, 6, 10, 0, 0, 16, -14, 0, 8, 18, 4, 18, 24, 8, 40, 48, 36, 18, 6, 8, 4, 0, 22, 62, 18, 16, 30, 34, 18, 8, 0, 12, 10, -2, 0, 0, 0, 18, 12, 16, 14, 10, 22, 32, 48, 0, 10, 0, -8, -2, -6, -12, 16, 0, 16, 8, 26, 16, 24, 32, 20, 0, -8, 0, -30, 4, 20, 6, 16, 6, 4, 20, 20, 24, 30, 20, 12, 40, -8, 32, 2, -48, -6, -14, 0, 16, 4, 6, 30, 8, 20, 32, 0, 6, 0, 16, -6, -12, 0, -16, 22, 22, 8, 12, 18, 10, 56, 30, 16, 32, 20, -6, 0, -8, -10, -10, 2, 0, 32}; const int len = sizeof(rawData)/sizeof(rawData[0]); long sum, sum_old; int thresh = 0; byte pd_state = 0; void setup() { Serial.begin(115200); sum = 0; pd_state = 0; int period = 0; // Autocorrelation for(int i = 0; i < len; i++) { sum_old = sum; sum = 0; for(int k = 0; k < len - i; k++) { sum += rawData[k] * rawData[k+i]; } // Peak Detect State Machine if (pd_state == 2 && (sum-sum_old) <= 0) { period = i; pd_state = 3; } if (pd_state == 1 && (sum > thresh) && (sum - sum_old) > 0) { pd_state = 2; } if (i == 0) { thresh = sum * 0.5; pd_state = 1; } } // Frequency identified in Hz if (period > 0 && period < INT_MAX) { Serial.println("Wohooo, here is your frequency:"); Serial.println(sample_freq/period); } else { Serial.println("Yeah, too much noise ... "); } } void loop() { }