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

Thread: Custom FFT Frequency Range

  1. #1
    Junior Member
    Join Date
    Aug 2018
    Posts
    6

    Custom FFT Frequency Range

    I have a project where I have to measure the frequencies of a mechanical device at low audio frequencies, mostly below 1KHz. The Audio Library's FFT1024 and 256 are set for a 22KHz frequency range with about 43Hz between each freq bin. For my project, I need a resolution down to about 5Hz or so...

    With the help of a couple other posts from JBeale and Frank B. I was able to code a generic FFT with variable frequency range.

    I am using the Teensy 3.2 @ 72MHz

    Here is the code I am using:

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    AudioInputAnalog         adc1;       
    AudioAnalyzeFFT1024      myFFT;
    AudioConnection          patchCord1(adc1, myFFT);
    AudioSynthWaveform       waveform1;      
    AudioOutputAnalog        dac1;           
    AudioConnection          patchCord2(waveform1, dac1);
    
    
    void setDACFreq(int freq) {  // change sampling rate of internal ADC and DAC
    const unsigned config = PDB_SC_TRGSEL(15) | PDB_SC_PDBEN | PDB_SC_CONT | PDB_SC_PDBIE | PDB_SC_DMAEN;
        PDB0_SC = 0; //<--add this line
        PDB0_IDLY = 1;
        PDB0_MOD = round((float)F_BUS / freq ) - 1;    
        PDB0_SC = config | PDB_SC_LDOK;
        PDB0_SC = config | PDB_SC_SWTRIG;
        PDB0_CH0C1 = 0x0101;    
    }
    
    const uint16_t dacOutputFreq = 500;//Hz - Test Frequency for FFT
    const uint16_t dacFreqMultiplier = 22;//If using FFT256 set to 44. This give the correct Output Frequency of the DAC
    
    void setup() {
      AudioMemory(12);
      AudioNoInterrupts();
      setDACFreq(2000);//For a 1KHz Frequency FFT Span
      myFFT.windowFunction(AudioWindowHanning1024);
      AudioInterrupts();
      
      Serial.begin(115200);
    
      //Using DAC Output to Test FFT input on A2
      waveform1.begin(1.0,(dacOutputFreq * dacFreqMultiplier),WAVEFORM_SINE);
      
      delay(1000);
    }
    
    void loop() {
      if (myFFT.available()) {
        for(int i = 0; i < 511; i++)
        {
          Serial.println((myFFT.read(i)*10),1);
        }
        Serial.println();
        delay(200);//Delay if using SerialPlot for Visual Output
      }
    }
    In order to test the program, I am using the Teensy's onboard DAC set to a specific frequency, but can run at any test frequency by setting it in code.
    Basically, the DAC output on the Teensy is connected to the A2 pin.

    Since I am changing the frequency of the update rate on the DAC/ADC there has to be a multiplier for the DAC output frequency. See code comment.

    This test program works with either the FFT1024 or FFT256.

    Here is a screenshot of the FFT output (1024) in SerialPlot which works well with the Serial output of the Teensy.

    Click image for larger version. 

Name:	Teensy FFT Example.png 
Views:	24 
Size:	84.8 KB 
ID:	14346

    SerialPlot can be downloaded here:

    http://https://bitbucket.org/hyOzd/serialplot

    I hope this help with other FFT projects...
    Last edited by tcontrada; 08-03-2018 at 06:17 PM.

  2. #2
    Senior Member
    Join Date
    Oct 2012
    Location
    Portland OR
    Posts
    570
    Thanks, very useful! BTW the link you have for serialplot did not work for me but this does: https://bitbucket.org/hyOzd/serialplot

  3. #3
    Junior Member
    Join Date
    Aug 2018
    Posts
    6
    That's strange about my link not working, but as long as folks know about SerialPlot as it's great for working with the Teensy, especially in FFT mode.

  4. #4
    Senior Member
    Join Date
    Oct 2012
    Location
    Portland OR
    Posts
    570
    ah- it is because in your link there is no colon : between the https and the //

  5. #5
    Junior Member
    Join Date
    Aug 2018
    Posts
    6
    Doh, I see it's a type-o when I copied the link...

Posting Permissions

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