Custom FFT Frequency Range

Status
Not open for further replies.

tcontrada

Member
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.

Teensy FFT Example.png

SerialPlot can be downloaded here:

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

I hope this help with other FFT projects...
 
Last edited:
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.
 
Status
Not open for further replies.
Back
Top