Stereo Analog Input with FFT

Status
Not open for further replies.

ksmith

Member
Hello,

I did some searching but I was not able to find an answer to this question, so hopefully, I am not duplicating the question. I'm trying to use ADC Stereo input with the 256 pt FFT from the audio library on a Teensy 4.1. My drawing from the Audio Library tool looks like the following:

TeensyAudioDrawing.JPG

I have the audio going through the recommended hardware from the library to shift the voltage to the input levels required for the ADC. I then am sending the same signal to A2 and A3 (for now, eventually, I will use the actual stereo input but I'm just debugging at this point).

The issue is that the code below does not function. I get from the serial interface the "FFT Test" text, but then no output from the FFT since the FFT is not available. If I change the MONO to 1, and use just a single ADC input the code works fine and I get results and the proper frequencies are showing in the correct bins. The same is true if I use A3 instead of the default A2 in the mono case. Looking for any thoughts would I could be missing on why the stereo input does not work. Below is the code I am using. Appreciate your time.

-Kevin


Code:
/********************************************************************
* Includes                                                          *
********************************************************************/
/* Arduino Libaries */
#include <Audio.h>

#define MONO 0

#if (MONO == 1)
// GUItool: begin automatically generated code
AudioInputAnalog         adc1;           //xy=301,309
AudioMixer4              mixer1;         //xy=455,333
AudioOutputI2S2          i2s2_1;         //xy=627,424
AudioAnalyzeFFT256       fft256_1;       //xy=636,362
AudioConnection          patchCord1(adc1, 0, mixer1, 0);
AudioConnection          patchCord2(mixer1, fft256_1);
AudioConnection          patchCord3(mixer1, 0, i2s2_1, 0);
AudioConnection          patchCord4(mixer1, 0, i2s2_1, 1);
// GUItool: end automatically generated code
#else
// GUItool: begin automatically generated code
AudioInputAnalogStereo   adcs1;          //xy=223,320
AudioMixer4              mixer1;         //xy=455,333
AudioOutputI2S2          i2s2_1;         //xy=634,404
AudioAnalyzeFFT256       fft256_1;       //xy=636,362
AudioConnection          patchCord1(adcs1, 0, mixer1, 0);
AudioConnection          patchCord2(adcs1, 1, mixer1, 1);
AudioConnection          patchCord3(mixer1, fft256_1);
AudioConnection          patchCord4(mixer1, 0, i2s2_1, 0);
AudioConnection          patchCord5(mixer1, 0, i2s2_1, 1);
// GUItool: end automatically generated code
#endif

const int ledPin = LED_BUILTIN;
int ledState = LOW;
IntervalTimer myTimer;

void setup(void)
{
  system_StartupSequence();

  AudioMemory(60);
  mixer1.gain(0, 1.0);
  mixer1.gain(1, 1.0);

  myTimer.begin(blinkLED, 500000);
  pinMode(ledPin, OUTPUT);
  while (!Serial) ; // wait for Arduino Serial Monitor
  Serial.println("FFT test");
}

void loop(void) 
{
  if (fft256_1.available()) {
    for (int i=0; i < 40; i++) {  // print the first 20 bins
      Serial.print(fft256_1.read(i), 3);
      Serial.print(" ");
    }
    Serial.println();
  }
}

void blinkLED() {
  if (ledState == LOW){
    ledState = HIGH;
  } else {
    ledState = LOW;
  }
  digitalWrite(ledPin, ledState);
}
 
Normally, the sum of the gains into a mixer should be 1 - unless you have controlled the inputs to the mixer to ensure that its output won't exceed the range [-32768,32767]. Try this:
Code:
  mixer1.gain(0, 0.5);
  mixer1.gain(1, 0.5);

Pete
 
Normally, the sum of the gains into a mixer should be 1 - unless you have controlled the inputs to the mixer to ensure that its output won't exceed the range [-32768,32767]. Try this:
Code:
  mixer1.gain(0, 0.5);
  mixer1.gain(1, 0.5);

Pete

Hi Pete. Thanks for the suggestion. Unfortunately, I am still getting the same result where the FFT available function is never coming true and I just see the "FFT test" string after the setup function executes.

-Kevin
 
I've just compiled and ran the code. The change to the mixer gains doesn't help. I removed the call to system_StartupSequence() - what does it do?

Pete
 
I've just compiled and ran the code. The change to the mixer gains doesn't help. I removed the call to system_StartupSequence() - what does it do?

Pete

My apologies I forgot to comment that line out that from my original code snippet when I posted. Currently, it does nothing it is just an empty function. I'm porting code over from a dsPIC project to the teensy and that function was setting up all the GPIO pins and directions on the PIC.

-Kevin
 
I knew there was something special about AudioInputAnalogStereo. It is only defined for Teensy 3.2, 3.5 and 3.6.

You'll have to use two separate AudioInputAnalog as input to the mixer.

Pete
 
I knew there was something special about AudioInputAnalogStereo. It is only defined for Teensy 3.2, 3.5 and 3.6.

You'll have to use two separate AudioInputAnalog as input to the mixer.

Pete


Thanks Pete. However, I did try this before, and that brings up a different problem. I updated the code to this from the Audio Design tool. While I get results now from the FFT, the issue is that only one of the ADC channels is working. In the code below, pin A2 is the only pin looked at. If i disconnect it, A3 gives no results. Likewise, if I switch it, A3 will work and A2 does nothing. I'm assuming there is something in the library that I cannot declare two instances of the AnalogInput. If that is the case, then that is fine, but wanted to clarify in case I am making a silly mistake.

Code:
// GUItool: begin automatically generated code
AudioInputAnalog         adc2(A3);           //xy=309,350
AudioInputAnalog         adc1(A2);           //xy=310,312
AudioMixer4              mixer1;         //xy=455,333
AudioOutputI2S2          i2s2_1;         //xy=634,404
AudioAnalyzeFFT256       fft256_1;       //xy=636,362
AudioConnection          patchCord1(adc2, 0, mixer1, 1);
AudioConnection          patchCord2(adc1, 0, mixer1, 0);
AudioConnection          patchCord3(mixer1, fft256_1);
AudioConnection          patchCord4(mixer1, 0, i2s2_1, 0);
AudioConnection          patchCord5(mixer1, 0, i2s2_1, 1);
// GUItool: end automatically generated code
 
The audio design GUI says that AudioInputAnalog for T4.0 and T4.1 is "experimental". Perhaps you are stretching things a bit too far? I'm afraid I haven't used it.

Pete
 
The audio design GUI says that AudioInputAnalog for T4.0 and T4.1 is "experimental". Perhaps you are stretching things a bit too far? I'm afraid I haven't used it.

Pete

No worries Pete, I had that concern as well. I haven't had a chance to dive into the belly of this controller yet. Items like the ADC, DMA, and the other peripherals I haven't had a chance to fully read up on to understand if I can see in the code where these issues may pop up or if there is a local patch I can do to get around it to try and go around the current library limitations. Having stereo audio isn't important to me and I know and can simply make it mono with a simple summing circuit, so I may just go that route for now and try again later on when I have more understanding in the library or if it no longer becomes experimental.

Thanks again for your time and assistance. Cheers!
-Kevin
 
Hi Kevin,
This is just a wild guess, but try moving one of the ADC to pin 38 (A14). I think it is on the other ADC. Perhaps the library can handle two ADCs on T4.1 if one is on ADC1 and the other on ADC2.

Pete
 
Hi Pete,

Loved the idea, sadly same result.

Code:
AudioInputAnalog         adc1(A2);           //xy=310,312
AudioInputAnalog         adc2(A14);           //xy=309,350
A14 drives the FFT, and if you flip the order, A2 will drive it.

I also tried this:
Code:
AudioInputAnalogStereo   adcs1(A2,A14);          //xy=306,318
AudioMixer4              mixer1;         //xy=455,333
AudioOutputI2S2          i2s2_1;         //xy=634,404
AudioAnalyzeFFT256       fft256_1;       //xy=636,362
AudioConnection          patchCord1(adcs1, 0, mixer1, 0);
AudioConnection          patchCord2(adcs1, 1, mixer1, 1);
AudioConnection          patchCord3(mixer1, fft256_1);
AudioConnection          patchCord4(mixer1, 0, i2s2_1, 0);
AudioConnection          patchCord5(mixer1, 0, i2s2_1, 1);
But this gives the same result with the FFT not being available (which is what I expected from our previous messages on this topic).

-Kevin
 
Another interesting thing I have noticed. Perhaps this is because the driver is still experimental but figured maybe someone with more knowledge may understand this a bit better than I.

If I am just using a single analog interface (in this case A2 as the default). The PIN A14 (or digital pin 38) is also impacted. If I set pin 38 to output, for example, my ADC on pin A2 no longer functions. If I do not touch pin 38, then everything appears to work fine. Because of this, I tried moving both the stereo and two single ADC inputs to use A15 but still get the same results above. However, I am curious as to why pin 38 has the influence it does on pin A2 being a single input. Does anyone have any thoughts?

Thanks again for the assistance and for helping me understand.
-Kevin
 
Status
Not open for further replies.
Back
Top