Go Down

Topic: FHT Library Frequency Output  (Read 518 times) previous topic - next topic


Hey, this is probably going to be a rookie question, but I am using the Arduino FHT library from http://wiki.openmusiclabs.com/wiki/ArduinoFHT in order to do frequency detection. I understand that you can output the data to a spectrum analyzer using a 3rd party application, but all I want to do is get like an integer value for the frequency.

Whenever I run the code below, I get a lot of data in the serial monitor, but nothing I can make sense of. (the code is mostly straight from one of the examples and I've been playing around with it)

Any thoughts on how I would simply get the frequency value? Or is my understanding flawed somehow?


Code: [Select]
#define LOG_OUT 1 // use the log output function
#define FHT_N 256 // set to 256 point fht

#include <FHT.h> // include the library

void setup() {
  Serial.begin(115200); // use the serial port
  TIMSK0 = 0; // turn off timer0 for lower jitter
  ADCSRA = 0xe5; // set the adc to free running mode
  ADMUX = 0x40; // use adc0
  DIDR0 = 0x01; // turn off the digital input for adc0

void loop() {
  while(1) { // reduces jitter
    cli();  // UDRE interrupt slows this way down on arduino1.0
    for (int i = 0 ; i < FHT_N ; i++) { // save 256 samples
      while(!(ADCSRA & 0x10)); // wait for adc to be ready
      ADCSRA = 0xf5; // restart adc
      byte m = ADCL; // fetch adc data
      byte j = ADCH;
      int k = (j << 8) | m; // form into an int
      k -= 0x0200; // form into a signed int
      k <<= 6; // form into a 16b signed int
      fht_input[i] = k; // put real data into bins
    //Serial.println(fht_input[0], fht_input[1]);
    fht_window(); // window the data for better frequency response
    fht_reorder(); // reorder the data before doing the fht
    fht_run(); // process the data in the fht
    Serial.println(fht_input[0], fht_input[1]);
    //fht_mag_log(); // take the output of the fht
    //Serial.write(255); // send a start byte
    //Serial.write(fht_log_out, FHT_N/2); // send out the data


What frequency value are you looking for? What is the input you are trying to analyze?

Read up on how Serial.print and Serial.write work.

Don't send me technical questions via Private Message.


Of course... Knew it would probably something stupid like that. Thanks for the guidance.


I get a lot of data in the serial monitor, but nothing I can make sense of.
Each number you print out is the contents of a frequency bin. You have 256 bins, and each bin covers a frequency range which depends on your sampling rate.

So the input before it gets into the Arduino needs to be filtered so that you do not get any frequencies through it that will not have at least two samples per cycle. Otherwise all bets are off and you get rubbish numbers called aliasing.

Then in the top bin you get a number corresponding to a frequency that is half the sample rate, the next bin down is one quarter the sample rate, the next down 1/8 the sample rate then 1/16 and so on. So each bin is a different range of frequencies and they get wider the lower you go.

but all I want to do is get like an integer value for the frequency.
Using an FFT to get the frequency is not a very good technique. What you would have to do here is to look at all the bins and return the bin number that has the biggest number in it. That assumes that the fundamental frequency of the sound you have measured is the biggest harmonic. 


Determining the dominant frequency and measuring a spectrum are related, but definitely different operations.

Auto-correlation is a useful operation for frequency determination for instance, that's solely in the time
domain.  Simple zero-crossing analysis can be used too.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up