Skip to content

Instantly share code, notes, and snippets.

@jcbrenes
Created May 14, 2017 21:03
Show Gist options
  • Save jcbrenes/2c836dd136dff6f77ff19cf5bb29419c to your computer and use it in GitHub Desktop.
Save jcbrenes/2c836dd136dff6f77ff19cf5bb29419c to your computer and use it in GitHub Desktop.
Código arduino para muestrear un grupo de frecuencias. Usa la biblioteca FFT.h
/* prueba de la libreria de FFT
toma 256 muestras con fmuestreo variable, y a continuacion
calcula la FFT.
Luego envia 128 muestras por el puerto serie a 9600 baudios
*/
# define LIN_OUT 1 //salida lineal en 16 bits de resolucion
# define FFT_N 256 //transformada de 256 puntos
#include<FFT.h> //incluimos la libreria
long antes;
float fmuestreo;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // ajusta el puerto serie
}
void loop() {
antes=millis();
// put your main code here, to run repeatedly:
for (int i = 0 ; i < 512 ; i += 2) { // 256 muestras, complejas
fft_input[i]= analogRead(A0); //Muestreamos el canal 0 (parte real)
fft_input[i+1]= 0; //parte imaginaria
delay(4); //muestreo cada 4 ms (aprox. a 250)Hz
}
float fmuestreo = 256.0 /(millis()-antes);
//ahora llamamos a la rutina de FFT
//fft_window(); // window the data for better frequency response
fft_reorder(); // reorder the data before doing the fft
fft_run(); // process the data in the fft
fft_mag_lin(); // take the output of the fft en formato lineal
//Serial.println("START ");
//Serial.print("fsample(Hz): ");Serial.println(1000.0*fmuestreo,2);
//Serial.print("fbin(Hz): ");Serial.println(1000.0*fmuestreo/256.0,2);
//ahora enviamos el vector al puerto serie
for (byte i = 0 ; i < FFT_N/2 ; i++) { //solo recibimos N/2 bins
Serial.print(i); // indice
Serial.print(";"); // separador
Serial.print(fft_input[2*i]); // parte real
Serial.print(";"); // separador
Serial.print(fft_input[2*i+1]); // parte imaginaria
Serial.print(";"); // separador
Serial.print(fft_lin_out[i]); // modulo
Serial.println(";"); // separador
}
delay(2000); //esperamos 2 segundos y vuelta a empezar
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment