You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.2KB

  1. // FFT Test
  2. //
  3. // Compute a 1024 point Fast Fourier Transform (spectrum analysis)
  4. // on audio connected to the Left Line-In pin. By changing code,
  5. // a synthetic sine wave can be input instead.
  6. //
  7. // The first 40 (of 512) frequency analysis bins are printed to
  8. // the Arduino Serial Monitor. Viewing the raw data can help you
  9. // understand how the FFT works and what results to expect when
  10. // using the data to control LEDs, motors, or other fun things!
  11. //
  12. // This example code is in the public domain.
  13. #include <Audio.h>
  14. #include <Wire.h>
  15. #include <SPI.h>
  16. #include <SD.h>
  17. #include <SerialFlash.h>
  18. const int myInput = AUDIO_INPUT_LINEIN;
  19. //const int myInput = AUDIO_INPUT_MIC;
  20. // Create the Audio components. These should be created in the
  21. // order data flows, inputs/sources -> processing -> outputs
  22. //
  23. AudioInputI2S audioInput; // audio shield: mic or line-in
  24. AudioSynthWaveformSine sinewave;
  25. AudioAnalyzeFFT1024 myFFT;
  26. AudioOutputI2S audioOutput; // audio shield: headphones & line-out
  27. // Connect either the live input or synthesized sine wave
  28. AudioConnection patchCord1(audioInput, 0, myFFT, 0);
  29. //AudioConnection patchCord1(sinewave, 0, myFFT, 0);
  30. AudioControlSGTL5000 audioShield;
  31. void setup() {
  32. // Audio connections require memory to work. For more
  33. // detailed information, see the MemoryAndCpuUsage example
  34. AudioMemory(12);
  35. // Enable the audio shield and set the output volume.
  36. audioShield.enable();
  37. audioShield.inputSelect(myInput);
  38. audioShield.volume(0.5);
  39. // Configure the window algorithm to use
  40. myFFT.windowFunction(AudioWindowHanning1024);
  41. //myFFT.windowFunction(NULL);
  42. // Create a synthetic sine wave, for testing
  43. // To use this, edit the connections above
  44. sinewave.amplitude(0.8);
  45. sinewave.frequency(1034.007);
  46. }
  47. void loop() {
  48. float n;
  49. int i;
  50. if (myFFT.available()) {
  51. // each time new FFT data is available
  52. // print it all to the Arduino Serial Monitor
  53. Serial.print("FFT: ");
  54. for (i=0; i<40; i++) {
  55. n = myFFT.read(i);
  56. if (n >= 0.01) {
  57. Serial.print(n);
  58. Serial.print(" ");
  59. } else {
  60. Serial.print(" - "); // don't print "0.00"
  61. }
  62. }
  63. Serial.println();
  64. }
  65. }