FFT 实现和使用它的输出

FFT Implementation and using its Output

我目前正在做一个项目,其中 PRO Trinket 对音频输入进行 64 点 FFT,并将箱子的 Output/Magnitude 发送到控制 RGB 矩阵的 arduino。

我从 http://wiki.openmusiclabs.com/wiki/ArduinoFFT 推导出我的 fft 代码 我似乎无法弄清楚如何准确地使用输出 (fft_log_out) 并发送数据,尽管我认为我的代码接近工作。在第 73 行中,我得到这个错误:'freq_array' 未在此范围内声明 如果能得到一些帮助就太好了,我已经为此工作了几个星期:(

/*
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[i] = 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: 'freq_array' 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);


}

 }
}

您遇到错误

ERROR: 'freq_array' was not declared in this scope

因为 freq_arraysetup() 函数范围内被声明为局部变量,因此在您尝试使用它的范围之外无法访问。

要解决此错误,您只需将声明移至 setup() 函数主体之外,从而在文件范围内声明 freq_array

int freq_array[32];
void setup() { ...