FFT Implementation, str??

Im currently working on a project where a PRO Trinket does a 64 point FFT of an Audio Input and sends the Output/Magnitude of the bins to arduino which controlls a RGB Matrix.

I derived my fft code from ArduinoFFT - Open Music Labs Wiki I cant seem to figure out how to exactly use the output (fft_log_out) and send out the data although I think that my code is close to working. In Line 73 I get this ERROR: ‘str’ was not declared in this scope
It would be great to get some help on this, I have been working on this for weeks :frowning:
I think it the str command that C doesnt know, but how would I do this in C then?

Code: (also as download)

/*
fft_adc.pde
guest openmusiclabs.com 8.18.12
example sketch for testing the fft library.
it takes in data on ADC0 (Analog0) and processes them
with the fft. the data is sent out over the serial
port at 115.2kb. there is a pure data patch for
visualizing the data.
*/

#define LOG_OUT 1 // use the log output function
#define FFT_N 64 // set to 64 point fft

#include <FFT.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
int freq_array [32];
}

void loop() {
while(1) { // reduces jitter
cli(); // UDRE interrupt slows this way down on arduino1.0
for (int i = 0 ; i < 124 ; i += 2) { // save 64 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
fft_input = k; // put real data into even bins

  • fft_input[i+1] = 0; // set odd bins to 0*
    }
    fft_window(); // window the data for better frequency response
    fft_reorder(); // reorder the data before doing the fft
    fft_run(); // process the data in the fft
    fft_mag_log(); // take the output of the fft
    // Amplitude Ranges if else tree
    *for(int j=0; j<32; j++){ *
    if (fft_log_out[j] < 2000 && fft_log_out[j] > 180){freq_array[j] = 16;}
    else{ if (fft_log_out[j] <= 180 && fft_log_out[j] > 160){freq_array[j] = 15;}
    else{ if (fft_log_out[j] <= 160 && fft_log_out[j] > 130){freq_array[j] = 14;}
    else{ if (fft_log_out[j] <= 130 && fft_log_out[j] > 110){freq_array[j] = 13;}
    else{ if (fft_log_out[j] <= 110 && fft_log_out[j] > 90){freq_array[j] = 12;}
    else{ if (fft_log_out[j] <= 90 && fft_log_out[j] > 70){freq_array[j] = 11;}
    else{ if (fft_log_out[j] <= 70 && fft_log_out[j] > 60){freq_array[j] = 10;}
    else{ if (fft_log_out[j] <= 60 && fft_log_out[j] > 50){freq_array[j] = 9;}
    else{ if (fft_log_out[j] <= 50 && fft_log_out[j] > 40){freq_array[j] = 8;}
    else{ if (fft_log_out[j] <= 40 && fft_log_out[j] > 30){freq_array[j] = 7;}
    else{ if (fft_log_out[j] <= 30 && fft_log_out[j] > 20){freq_array[j] = 6;}
    else{ if (fft_log_out[j] <= 20 && fft_log_out[j] > 15){freq_array[j] = 5;}
    else{ if (fft_log_out[j] <= 15 && fft_log_out[j] > 11){freq_array[j] = 4;}
    else{ if (fft_log_out[j] <= 11 && fft_log_out[j] > 8){freq_array[j] = 3;}
    else{ if (fft_log_out[j] <= 8 && fft_log_out[j] > 5){freq_array[j] = 2;}
    else{ if (fft_log_out[j] <= 5 && fft_log_out[j] > 2){freq_array[j] = 1;}
    else{ if (fft_log_out[j] <= 2 && fft_log_out[j] > 0){freq_array[j] = 0;}
    }}}}}}}}}}}}}}}}}
    sei();
    String sta = “M”;
    String aa = str(freq_array[0]); //ERROR: ‘str’ was not declared in this scope
    String bb = str(freq_array[1]);
    String cc = str(freq_array[2]);
    String dd = str(freq_array[3]);
    String ee = str(freq_array[4]);
    String ff = str(freq_array[5]);
    String gg = str(freq_array[6]);
    String hh = str(freq_array[7]);
    String ii = str(freq_array[8]);
    String jj = str(freq_array[9]);
    String kk = str(freq_array[10]);
    String ll = str(freq_array[11]);
    String mm = str(freq_array[12]);
    String nn = str(freq_array[13]);
    String oo = str(freq_array[14]);
    String pp = str(freq_array[15]);
    String qq = str(freq_array[16]);
    String rr = str(freq_array[17]);
    String ss = str(freq_array[18]);
    String tt = str(freq_array[19]);
    String uu = str(freq_array[20]);
    String vv = str(freq_array[21]);
    String ww = str(freq_array[22]);
    String xx = str(freq_array[23]);
    String yy = str(freq_array[24]);
    String zz = str(freq_array[25]);
    String aaa = str(freq_array[26]);
    String bbb = str(freq_array[27]);
    String ccc = str(freq_array[28]);
    String ddd = str(freq_array[28]);
    String eee = str(freq_array[30]);
    String fff = str(freq_array[31]);
    String com = “,”;
    String newl = “\n”;
    String send1 = sta + aa + com + bb + com + cc + com + dd + com + ee + com + ff + com + gg + com + hh + com + ii + com + jj + com + kk + com + ll + com + mm + com + nn + com + oo + com + pp + com + qq + com + rr + com + ss + com + tt + com + uu + com + vv + com + ww + com + xx + com + yy + com + zz + com + aaa + com + bbb + com + ccc + com + ddd + com + eee + com + fff + newl;
    Serial.write(send1);
    }
    }
    }
    fft_adc.ino (4.52 KB)

Please use code tags.

Yep, str is not a C command.

I suggest you ditch the Strings and do it all with char arrays.

No reason to create all the temporary strings, just print them as you create them.

I will guess that the original code did not have italics or smileys :slight_smile: in it.

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.
  4. If you already posted without code tags, you may add the code tags by
    editing your post. Do not change your existing posts in any other way.
    You may make additional posts as needed.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!

vaj4088:
I will guess that the original code did not have italics or smileys :slight_smile: in it.

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.
  4. If you already posted without code tags, you may add the code tags by
    editing your post. Do not change your existing posts in any other way.
    You may make additional posts as needed.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!

That is real nice cookie cutter answer.
Is it in public domain? Say as Open source ?
Like to use it next time, can I? Please.
JIm

OP you are missing a str function. Let me take a peak at you code.
I did fft about two years ago , intent was to analyze video stream.
Lots of math “fun” and typos everywhere!

It's the crazy syntax - create an instance of class String passing constructor ( int) ans return class variable
String of value "X".

String aa = str(freq_array[0]); //ERROR: 'str' was not declared in this scope String aa = str(freq_array[0]); //ERROR: 'str' was not declared in this scope

String aa = String(freq_array[0]); // convert int to string using class String

I would heed the suggestion and send the FFT coefficients after String converts them from int to String.
You alrady have an array of ints , you do not need an array of strings.

The actual conversion cannot mess things up - if the coefficients are wrong the conversion cannot fix them so why storing them ? Just send them down the line in the loop.

Jim

BTW why you need 32 coefficients? They get meaningless after few, but that depends on application.

julyjim:
BTW why you need 32 coefficients? They get meaningless after few, but that depends on application.

It all depends on where the signal is. Any given coefficent can have signal.

julyjim wrote (in part):

That is real nice cookie cutter answer.
Is it in public domain? Say as Open source ?
Like to use it next time, can I? Please.

Well, I figured that anything I posted here was free and open source and public domain, but if you want my explicit permission then you have it. I think that anything that gets people to post in a better way is a good thing. Thanks for asking!

I do have to warn you, though...
This text is still evolving. I made the latest change less than a week ago.

KeithRB:
It all depends on where the signal is. Any given coefficent can have signal.

It does not matter to OP or subject , but FFT coefficients reflects the value of signal harmonics , hence they will always get smaller from fundamental frequency, independent of signal.
Jim

It does not matter to OP or subject , but FFT coefficients reflects the sampled waveform. That can be one signal with harmonics, or multiple signals whose amplitudes have no relationship with each other. If you have only a first year familiarity with FFT's, you have a lot to learn.

FFT coefficients reflects the value of signal harmonics , hence they will always get smaller from fundamental frequency, independent of signal.

Wrong. Please do not confuse the other beginners with such nonsense.

Check out this spectrum:
Not exactly monotonic.

TT spectrum.png

KeithRB:
It does not matter to OP or subject , but FFT coefficients reflects the sampled waveform. That can be one signal with harmonics, or multiple signals whose amplitudes have no relationship with each other. If you have only a first year familiarity with FFT's, you have a lot to learn.

KeithRB:
It does not matter to OP or subject , but FFT coefficients reflects the sampled waveform. That can be one signal with harmonics, or multiple signals whose amplitudes have no relationship with each other. If you have only a first year familiarity with FFT's, you have a lot to learn.

Yes, apparently from qualified and patient teachers on this forum.
We are talking about FFT on single signal, not signals, right? .
I think I''ll use blink without delay, that is as complicated as this beginner want to get out of software.
Thanks.

We have no idea about the OP's signal, though it sounds like music, which for everyone but Phillip Glass and John Cage means multiple tones.