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.

78 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. const int myInput = AUDIO_INPUT_LINEIN;
  18. //const int myInput = AUDIO_INPUT_MIC;
  19. // Create the Audio components. These should be created in the
  20. // order data flows, inputs/sources -> processing -> outputs
  21. //
  22. AudioInputI2S audioInput; // audio shield: mic or line-in
  23. AudioSynthWaveformSine sinewave;
  24. AudioAnalyzeFFT1024 myFFT;
  25. AudioOutputI2S audioOutput; // audio shield: headphones & line-out
  26. // Connect either the live input or synthesized sine wave
  27. AudioConnection patchCord1(audioInput, 0, myFFT, 0);
  28. //AudioConnection patchCord1(sinewave, 0, myFFT, 0);
  29. AudioControlSGTL5000 audioShield;
  30. void setup() {
  31. // Audio connections require memory to work. For more
  32. // detailed information, see the MemoryAndCpuUsage example
  33. AudioMemory(12);
  34. // Enable the audio shield and set the output volume.
  35. audioShield.enable();
  36. audioShield.inputSelect(myInput);
  37. audioShield.volume(0.5);
  38. // Configure the window algorithm to use
  39. myFFT.windowFunction(AudioWindowHanning1024);
  40. //myFFT.windowFunction(NULL);
  41. // Create a synthetic sine wave, for testing
  42. // To use this, edit the connections above
  43. sinewave.amplitude(0.8);
  44. sinewave.frequency(1034.007);
  45. }
  46. void loop() {
  47. float n;
  48. int i;
  49. if (myFFT.available()) {
  50. // each time new FFT data is available
  51. // print it all to the Arduino Serial Monitor
  52. Serial.print("FFT: ");
  53. for (i=0; i<40; i++) {
  54. n = myFFT.read(i);
  55. if (n >= 0.01) {
  56. Serial.print(n);
  57. Serial.print(" ");
  58. } else {
  59. Serial.print(" - "); // don't print "0.00"
  60. }
  61. }
  62. Serial.println();
  63. }
  64. }