Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Teensy 4.0 with modified FFTs Tduino 1.5.1 >> 1.5.7

  1. #1
    Junior Member
    Join Date
    Jan 2023
    Posts
    3

    Teensy 4.0 with modified FFTs Tduino 1.5.1 >> 1.5.7

    Hello,

    I created a 4096 point AudioAnalyzeFFT4096 .h & .cpp based on the FFT1024 and stuff from around the web. I also modified the associated files data_windows.c, keywords.txt and Audio.h.

    This worked fine when using the Teensy 4.0 board with the Arduino 1.8.12 IDE and Teensyduino 1.5.1 (installed on Windows 10 pc). I have attached these files.

    Moving forward to Arduino IDE 2.0.3 and Teensyduino 1.5.7 and making the same modifications, the FFT4096 no longer works to the extent that if I select the FFT from within the program the Teensy 4.0 becomes unstable and buzzes continuously on all outputs until reset.

    I can't work out why the FFT no longer works with the later Ard 2.0.3/Tduino 1.5.7.

    What am I missing ? Are there other files I need to modify in Tduino 1.5.7 or has memory allocation changed or what ?

    Any ideas gratefully received.

    Thanks
    Mark
    Attached Files Attached Files

  2. #2
    Junior Member
    Join Date
    Jan 2023
    Posts
    3
    I thinned out my program and the FFT4096 does work so the problem lies with the rest of my code.

    To use the FFT4096, here's an example.

    With the serial monitor type 10F to do the FFT (10 is the number of ffts to execute) and then type 20Y to get the frequency bins and 30Y to get the levels in dBs.

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <math.h>
    
    
    AudioInputI2S           i2sIn;
    AudioAnalyzeFFT4096      fft4096_1;      //xy=920,302
    AudioSynthNoiseWhite     noise1;
    AudioOutputI2S          i2sOut;
    AudioConnection          patchCord0(i2sIn, 1, fft4096_1, 0);
    AudioConnection          patchCord1(noise1, 0, i2sOut,    0);
    AudioConnection          patchCord2(noise1, 0, i2sOut,    1);
    AudioControlSGTL5000    codec;
    
    
    int16_t k = 0;
    
    long serial_input_number;   // Used to build number from serial stream
    short num ;
    char incoming_char;
    
    int NfftPts = 4096;
    int nloops = 0;
    int nbands = 31;
    float averagelevel[2048];//[512];
    float magsum[2048];
    float binfc[2048];//[512];
    float level[2048];//[512];
    
    const double Pi = 3.14159265;
    
    void setup(void) {
      Serial.begin(57600);
      AudioMemory(160);
      codec.enable();
      codec.volume(0.8); //
      codec.inputSelect(AUDIO_INPUT_LINEIN);
      codec.lineInLevel(0);
      codec.lineOutLevel(13);
      noise1.amplitude(0.5);
      fft4096_1.windowFunction(AudioWindowHanning4096);
      serial_input_number = 0;
    }
    
    void loop(void) {
      if (Serial.available() > 0) {
        incoming_char = Serial.read();
        switch (incoming_char) {
          case '0':
          case '1':
          case '2':
          case '3':
          case '4':
          case '5':
          case '6':
          case '7':
          case '8':
          case '9':
            serial_input_number = serial_input_number * 10 + (incoming_char - '0');
            break;
          case 'A':
            num = (int)serial_input_number;
            
            serial_input_number = 0;
            break;
          case 'B':
            num = (int)serial_input_number;
            
            serial_input_number = 0;
            break;
          case 'F':
            num = (int)serial_input_number;
            nloops = num;
            fft(NfftPts, nloops);
            Serial.println("Done");
            serial_input_number = 0;
            break;
          case 'G':
            num = (int)serial_input_number;
            serial_input_number = 0;
            break;
          case 'H':
            serial_input_number = 0;
            break;
          case 'I':
            serial_input_number = 0;
            break;
          case 'J':
            serial_input_number = 0;
            break;
          case 'K':
            serial_input_number = 0;
            break;
          case 'L': //input and output filters
            num = (int)serial_input_number;
            serial_input_number = 0;
            break;
          case 'M':
            num = (int)serial_input_number;
            serial_input_number = 0;
            break;
          case 'N':
            serial_input_number = 0;
            break;
          case 'O': //input and output filters
            num = (int)serial_input_number;        
            serial_input_number = 0;
            break;
          case 'P':
    
            serial_input_number = 0;
            break;
          case 'R':
            serial_input_number = 0;
            break;
          case 'S':
            num = (int)serial_input_number;
            serial_input_number = 0;
            break;
          case 'T':
            num = (int)serial_input_number;
            serial_input_number = 0;
            break;
    
          case 'W': //Read RH part of bridge
            num = (int)serial_input_number;
            serial_input_number = 0;
            break;
          case 'X': //Read RH part of bridge
            num = (int)serial_input_number;
            serial_input_number = 0;
            break;
    
          case 'Y': //send data to PC
            num = (int)serial_input_number;
            for (int i = 0; i < NfftPts / 2; i++) {
                if (num == 10) Serial.println(level[i]);  //fft
                if (num == 20) Serial.println(binfc[i]);  //bin frequency
                if (num == 30) Serial.println(averagelevel[i]);     //response value either measured or ocrrected
            }        
            serial_input_number = 0;
            break;
          case 'Z': //Read RH part of bridge
            num = (int)serial_input_number;        
            serial_input_number = 0;
            break;
        }
      }
    
    }
    
    void fft(int npts, int loops ) {
    
      for (int k = 0; k < npts / 2; k++) {
        magsum[k] = 0;
        averagelevel[k] = 0;
        binfc[k] = (float)k * 44100 / npts;
      }
      binfc[0] = 1;
    
    fftstart:
    
      if (fft4096_1.available()) {
        delay(100);
        for (int j = 0; j < loops; j++) {//-fft()
          delay(100);
          for (int i = 0; i < npts / 2; i++) {
            level[i] = 0;
            level[i] = fft4096_1.read(i); //
            level[i] = 1.63 * level[i]; //Hanning Scaling
            if (level[i] < 1E-6) level[i] = 1E-6;
          }
          for (int m = 0; m < npts / 2; m++) {
            averagelevel[m] = averagelevel[m] + (0.1 * sq(level[m])); //dt*p^2
          }
        }
        for (int k = 0; k < npts / 2; k++) {
          averagelevel[k] = averagelevel[k] / (loops * 0.1) ;
          averagelevel[k] = 10 * log10(averagelevel[k]);
        }
    
      }
      else {
        goto fftstart;
      }
    }
    Last edited by KurtE; 01-20-2023 at 06:48 PM. Reason: code tags

  3. #3
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    11,382
    Hope you don't mind, but I added code tags to make it easier to read

  4. #4
    Junior Member
    Join Date
    Jan 2023
    Posts
    3
    Not at all. Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •