Flat output for FFT example (Teensy 3.6)

Status
Not open for further replies.

justin39

New member
I can't seem to get the FFT example running with audio line in; it just spits out flat/empty output.

Hardware:
- Teensy 3.6
- Audio Shield (should be rev. C)
- TRRS jack for input (T to left, R1 to right, R2 to gnd)

Code changes:
The default FFT example, but with AUDIO_INPUT_LINEIN and the proper AudioConnection to wire the input to the FFT. Volume set to 0.9 instead of 0.5. Configuration looks as following:

Code:
const int myInput = AUDIO_INPUT_LINEIN;
//const int myInput = AUDIO_INPUT_MIC;

// Create the Audio components.  These should be created in the
// order data flows, inputs/sources -> processing -> outputs
//
AudioInputI2S          audioInput;         // audio shield: mic or line-in
AudioSynthWaveformSine sinewave;
AudioAnalyzeFFT1024    myFFT;
AudioOutputI2S         audioOutput;        // audio shield: headphones & line-out

// Connect either the live input or synthesized sine wave
AudioConnection patchCord1(audioInput, 0, myFFT, 0);
AudioConnection patchCord2(audioInput, 0, audioOutput, 0);
AudioConnection patchCord3(audioInput, 1, audioOutput, 1);
// AudioConnection patchCord1(sinewave, 0, myFFT, 0);

Rest of the snippet is unchanged from FFT example (aside from volume 0.9).

Some troubleshooting:
- The output sounds fine (wired audioInput to audioOutput, headphones to the headphone jack); music piped through the teensy sounds clear and normal
- The FFT module works fine with the generated sinewave if I swap to it (commenting out line in, uncommenting out sinewave)
- Dropping some debug prints in the FFT code itself seems to indicate that it's not reading any meaningful data from line-in (i.e. dump the buffer and all the values are between negative hundreds and positive hundreds). When eyeballing the output with line-in set, I can't tell when the AUX is plugged/unplugged, but it's extremely clear with the generated sine wave when values spike to the thousands or ten thousands.

Not sure what obvious mistake I'm missing here, but everything seems to work in isolation.
 
Try increasing AudioMemory. FFT consumes several buffers. Maybe you're running out?

If that doesn't do it, please post the complete program so I can copy it into Arduino without splicing it into the example code. Yeah, I know it seems redundant, but don't leave any typo to chance. I'll copy the whole thing into a blank window and upload it to a Teensy 3.6 with audio shield.
 
Reasonable request! Here's the code:

Code:
// FFT Test
//
// Compute a 1024 point Fast Fourier Transform (spectrum analysis)
// on audio connected to the Left Line-In pin.  By changing code,
// a synthetic sine wave can be input instead.
//
// The first 40 (of 512) frequency analysis bins are printed to
// the Arduino Serial Monitor.  Viewing the raw data can help you
// understand how the FFT works and what results to expect when
// using the data to control LEDs, motors, or other fun things!
//
// This example code is in the public domain.

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

const int myInput = AUDIO_INPUT_LINEIN;
//const int myInput = AUDIO_INPUT_MIC;

// Create the Audio components.  These should be created in the
// order data flows, inputs/sources -> processing -> outputs
//
AudioInputI2S          audioInput;         // audio shield: mic or line-in
AudioSynthWaveformSine sinewave;
AudioAnalyzeFFT1024    myFFT;
AudioOutputI2S         audioOutput;        // audio shield: headphones & line-out

// Connect either the live input or synthesized sine wave
AudioConnection patchCord1(audioInput, 0, myFFT, 0);
AudioConnection patchCord2(audioInput, 0, audioOutput, 0);
AudioConnection patchCord3(audioInput, 1, audioOutput, 1);
// AudioConnection patchCord1(sinewave, 0, myFFT, 0);

AudioControlSGTL5000 audioShield;

void setup() {
  // Audio connections require memory to work.  For more
  // detailed information, see the MemoryAndCpuUsage example
  AudioMemory(120);

  // Enable the audio shield and set the output volume.
  audioShield.enable();
  audioShield.inputSelect(myInput);
  audioShield.volume(0.9);

  // Configure the window algorithm to use
  myFFT.windowFunction(AudioWindowHanning1024);
  //myFFT.windowFunction(NULL);

  // Create a synthetic sine wave, for testing
  // To use this, edit the connections above
  sinewave.amplitude(0.8);
  sinewave.frequency(1034.007);
}

void loop() {
  float n;
  int i;

  if (myFFT.available()) {
    // each time new FFT data is available
    // print it all to the Arduino Serial Monitor
    Serial.print("FFT: ");
    for (i=0; i<512; i++) {
      n = myFFT.read(i);
      if (n >= 0.01) {
        Serial.print(n);
        Serial.print(" ");
      } else {
        Serial.print("  -  "); // don't print "0.00"
      }
    }
    Serial.println();
  }
}

Output is just this forever:
Code:
FFT:   -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -

Bumping AudioMemory to 120 didn't seem to have an effect on the output, unfortunately. Sure hope there's a typo somewhere!
 
It will be hard to get ALL spectral values printed in REAL TIME to serial terminal.

To start I would only printout 1 to 10 spectra per second and throw the others away.
 
The example actually only printed the first 25 values now that you mention it, forgot about that!

Can confirm it doesn't help the null printout though - think I changed it to check if all the values were zero. Sounds like a good idea to change it back now that I've confirmed though.
 
What kind of audio are you using for line input? If you're playing from a phone or a PC, have you turned up the audio enough?
When I play audio from PC to line-in there's rarely any non-zero output even though I can hear it well in the headphones. It has to be turned up higher to register on the output.

You could also try using FFT256 (and the corresponding 256 window) and print the first 20 bins. The output on your screen will then cover a lot more of the input spectrum.

Pete
 
You got it! I'm playing to the line-in TSSR jack from my phone, and at max volume the FFT starts pumping out some output. Surprisingly simple fix, appreciate the tip!
 
Status
Not open for further replies.
Back
Top