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

Thread: Custom FFT Frequency Range

Threaded View

  1. #1
    Junior Member
    Join Date
    Aug 2018

    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:

    #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() {
      setDACFreq(2000);//For a 1KHz Frequency FFT Span
      //Using DAC Output to Test FFT input on A2
      waveform1.begin(1.0,(dacOutputFreq * dacFreqMultiplier),WAVEFORM_SINE);
    void loop() {
      if (myFFT.available()) {
        for(int i = 0; i < 511; i++)
        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:	85 
Size:	84.8 KB 
ID:	14346

    SerialPlot can be downloaded here:


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

Posting Permissions

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