Part_3_02_Fourier_Transform Distorts Line In

RichardW

New member
Hi Forum,
(NOTE: All references to "Strat" refer to an electric guitar.)
Noob here trying to get a FFT from my Strat. My setup is a Teensy 4.1 with Audio Shield Rev.D. Strat is directly connected to Line In and Line Out is to my desktop speakers. With Part_2_04_Microphone_Check sketch with AUDIO_INPUT_LINEIN selected the Strat comes through loud & clear.
I loaded Part_3_02_Fourier_Transform and entered the requisite design in the Design Tool. Everything runs and sounds normal. In the design Tool I replaced the playSdWav object with an i2s2 input object:

C++:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioPlayMemory          playMem1;       //xy=378.33331298828125,214
AudioInputI2S            i2s2;           //xy=379.3333282470703,135.3333282470703
AudioSynthWaveform       waveform1;      //xy=408.33331298828125,286
AudioMixer4              mixer1;         //xy=663.3333129882812,187
AudioAnalyzeFFT1024      fft1024_1;      //xy=885.3333129882812,270
AudioOutputI2S           i2s1;           //xy=894.3333129882812,147
AudioConnection          patchCord1(playMem1, 0, mixer1, 2);
AudioConnection          patchCord2(i2s2, 0, mixer1, 0);
AudioConnection          patchCord3(i2s2, 1, mixer1, 1);
AudioConnection          patchCord4(waveform1, 0, mixer1, 3);
AudioConnection          patchCord5(mixer1, 0, i2s1, 0);
AudioConnection          patchCord6(mixer1, 0, i2s1, 1);
AudioConnection          patchCord7(mixer1, fft1024_1);
AudioControlSGTL5000     sgtl5000_1;     //xy=178.33331298828125,470
// GUItool: end automatically generated code

Then commented the 4 references to playSdWav1:

C++:
  //playSdWav1.play("SDTEST1.WAV");
    // playSdWav1.play(filenames[fileNumber]);
    //playSdWav1.stop();
    //playSdWav1.stop();

Now when I press button0 for Line In I get terrible distortion from the Strat. Pressing button1 for the Guitar Sample the playback is still nice and clear. Interestingly button2 for the generated sine wave is also distorted so anyone should be able to duplicate this

Any suggestions would be greatly appreciated.

Thanks!,
RichardW
 
Post your code? A couple of snippets aren't enough...
BTW a line input will low pass filter a guitar output as guitars want 1M input impedance typically.
 
Thanks for the reply Mark & thanks for the tip on the input impedance.
Here's the code:
C++:
// Advanced Microcontroller-based Audio Workshop
//
// http://www.pjrc.com/store/audio_tutorial_kit.html
// https://hackaday.io/project/8292-microcontroller-audio-workshop-had-supercon-2015
//
// Part 3-2: Fourier Transform

#include <Bounce.h>
#include "AudioSampleGuitar.h"
Bounce button0 = Bounce(0, 15);
Bounce button1 = Bounce(1, 15);  // 15 = 15 ms debounce time
Bounce button2 = Bounce(2, 15);



///////////////////////////////////
// copy the Design Tool code here
///////////////////////////////////
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioPlayMemory          playMem1;       //xy=378.33331298828125,214
AudioInputI2S            i2s2;           //xy=382.3333282470703,135.3333282470703
AudioSynthWaveform       waveform1;      //xy=408.33331298828125,286
AudioMixer4              mixer1;         //xy=663.3333129882812,187
AudioAnalyzeFFT1024      fft1024_1;      //xy=885.3333129882812,270
AudioOutputI2S           i2s1;           //xy=894.3333129882812,147
AudioConnection          patchCord1(playMem1, 0, mixer1, 2);
AudioConnection          patchCord2(i2s2, 0, mixer1, 0);
AudioConnection          patchCord3(i2s2, 1, mixer1, 1);
AudioConnection          patchCord4(waveform1, 0, mixer1, 3);
AudioConnection          patchCord5(mixer1, 0, i2s1, 0);
AudioConnection          patchCord6(mixer1, 0, i2s1, 1);
AudioConnection          patchCord7(mixer1, fft1024_1);
AudioControlSGTL5000     sgtl5000_1;     //xy=178.33331298828125,470
// GUItool: end automatically generated code

// Use these with the Teensy Audio Shield
#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7   // Teensy 4 ignores this, uses pin 11
#define SDCARD_SCK_PIN   14  // Teensy 4 ignores this, uses pin 13

// Use these with the Teensy 3.5 & 3.6 & 4.1 SD card
//#define SDCARD_CS_PIN    BUILTIN_SDCARD
//#define SDCARD_MOSI_PIN  11  // not actually used
//#define SDCARD_SCK_PIN   13  // not actually used

// Use these for the SD+Wiz820 or other adaptors
//#define SDCARD_CS_PIN    4
//#define SDCARD_MOSI_PIN  11
//#define SDCARD_SCK_PIN   13

void setup() {
  Serial.begin(9600);
  AudioMemory(10);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  mixer1.gain(0, 0.5);
  mixer1.gain(1, 0.5);
  mixer1.gain(2, 0.0);
  mixer1.gain(3, 0.0);
  // Uncomment one these to try other window functions
  // fft1024_1.windowFunction(NULL);
  // fft1024_1.windowFunction(AudioWindowBartlett1024);
  // fft1024_1.windowFunction(AudioWindowFlattop1024);
  delay(1000);
  //playSdWav1.play("SDTEST1.WAV");

}


int fileNumber = 0;
const char * filenames[4] = {
  "SDTEST1.WAV", "SDTEST2.WAV", "SDTEST3.WAV", "SDTEST4.WAV"
};

int noteNumber = 0;
const float noteFrequency[12] = {
  220.00,  // A3
  233.08,  // A#3
  246.94,  // B3
  261.63,  // C4
  277.18,  // C#4
  293.66,  // D4
  311.13,  // D#4
  329.63,  // E4
  349.23,  // F4
  369.99,  // F#4
  392.00,  // G4
  415.30   // G#4
};



void loop() {

  // print Fourier Transform data to the Arduino Serial Monitor
  if (fft1024_1.available()) {
    
    Serial.print("FFT: ");
    for (int i=0; i<30; i++) {  // 0-25  -->  DC to 1.25 kHz
      float n = fft1024_1.read(i);
      printNumber(n);
    }
    Serial.println();
  }
 
  button0.update();
  button1.update();
  button2.update();

  // Left button enables line-in
  if (button0.fallingEdge()) {
    mixer1.gain(2, 0.0);
    mixer1.gain(3, 0.0);
    // fileNumber = fileNumber + 1;
    // if (fileNumber >= 4) fileNumber = 0;
    playMem1.stop();
    // playSdWav1.play(filenames[fileNumber]);
    mixer1.gain(0, 0.5);
    mixer1.gain(1, 0.5);
  }

  // Middle button plays Guitar sample
  if (button1.fallingEdge()) {
    mixer1.gain(0, 0.0);
    mixer1.gain(1, 0.0);
    mixer1.gain(3, 0.0);
    //playSdWav1.stop();
    playMem1.play(AudioSampleGuitar);
    mixer1.gain(2, 1.0);
  }

  // Right button plays a pure sine wave tone
  if (button2.fallingEdge()) {
    mixer1.gain(0, 0.0);
    mixer1.gain(1, 0.0);
    mixer1.gain(2, 0.0);
    //playSdWav1.stop();
    playMem1.stop();
    waveform1.begin(1.0, noteFrequency[noteNumber], WAVEFORM_SINE);
    noteNumber = noteNumber + 1;
    if (noteNumber >= 12) noteNumber = 0;
    mixer1.gain(3, 1.0);
  }
  if (button2.risingEdge()) {
    waveform1.amplitude(0);
  }
/*
  if (fft1024_1.available()) {
    // each time new FFT data is available
    // print to the Arduino Serial Monitor
    Serial.print("FFT: ");
    printNumber(fft1024_1.read(0));
    printNumber(fft1024_1.read(1));
    printNumber(fft1024_1.read(2,3));
    printNumber(fft1024_1.read(4,6));
    printNumber(fft1024_1.read(7,10));
    printNumber(fft1024_1.read(11,15));
    printNumber(fft1024_1.read(16,22));
    printNumber(fft1024_1.read(23,32));
    printNumber(fft1024_1.read(33,46));
    printNumber(fft1024_1.read(47,66));
    printNumber(fft1024_1.read(67,93));
    printNumber(fft1024_1.read(94,131));
    printNumber(fft1024_1.read(132,184));
    printNumber(fft1024_1.read(185,257));
    printNumber(fft1024_1.read(258,359));
    printNumber(fft1024_1.read(360,511));
    Serial.println();
  }
*/

}


void printNumber(float n) {
 
  if (n >= 0.004) {
    Serial.print(n, 3);
    Serial.print(" ");
  } else {
    Serial.print("   -  "); // don't print "0.00"
  }
 
  /*
  if (n > 0.25) {
    Serial.print("***** ");
  } else if (n > 0.18) {
    Serial.print(" ***  ");
  } else if (n > 0.06) {
    Serial.print("  *   ");
  } else if (n > 0.005) {
    Serial.print("  .   ");
  }
  */
}
 
Back
Top