void fft(double *vReal, double *vImag, uint16_t samples) {
double c1 = 0.5; double c2 = -0.5; double h1r, h1i, h2r, h2i; double wr, wi, wpr, wpi, temp; double theta = 3.14159265358979323846 / (samples>>1); uint16_t i, j, k, x; for (i = j = 0; i < samples - 1; i++) { if (i < j) { temp = vReal[i]; vReal[i] = vReal[j]; vReal[j] = temp; temp = vImag[i]; vImag[i] = vImag[j]; vImag[j] = temp; } k = (samples>>1); while (k <= j) { j -= k; k >>= 1; } j += k; } for (i = 1; i < samples; i <<= 1) { wr = cos(theta); wi = sin(theta); wpr = 1.0; wpi = 0.0; for (j = 0; j < i; j++) { for (k = j; k < samples; k += (i<<1)) { h1r = vReal[k] + vReal[k + i]; h1i = vImag[k] + vImag[k + i]; h2r = vReal[k] - vReal[k + i]; h2i = vImag[k] - vImag[k + i]; vReal[k] = h1r; vImag[k] = h1i; vReal[k + i] = h2r*wpr - h2i*wpi; vImag[k + i] = h2r*wpi + h2i*wpr; } wtemp = wpr; wpr = wtemp*wr - wpi*wi; wpi = wtemp*wi + wpi*wr; } theta = theta * 0.5; } }#define SAMPLES 512 // tamaño de muestra
#define SAMPLING_FREQUENCY 10000 // frecuencia de muestreo
double vReal[SAMPLES];
double vImag[SAMPLES];
void setup() {
Serial.begin(115200);
}
void loop() {
// tomar muestras de audio
for (int i = 0; i < SAMPLES; i++) {
vReal[i] = analogRead(A0); // leer señal de audio desde el pin A0
vImag[i] = 0;
}
// realizar FFT
fft(vReal, vImag, SAMPLES);
// calcular magnitud de las componentes complejas
for (int i = 0; i < SAMPLES; i++) {
vReal[i] = sqrt(vReal[i]*vReal[i] + vImag[i]*vImag[i]);
vImag[i] = 0;
}
// imprimir resultados de la FFT en la consola serial
for (int i = 0; i < (SAMPLES/2); i++) {
Serial.print(vReal[i]);
Serial.print(",");
}
Serial.println();
delay(1000);
}
void fft(double *vReal, double *vImag, uint16_t samples) {
double c1 = 0.5;
double c2 = -0.5;
Comentarios
Publicar un comentario