2016-10-11 209 views
0

我目前正在PRO项目中进行一个项目,PRO Trinket对音频输入进行64点FFT,并将输出/幅度的分档发送给控制RGB矩阵的arduino。FFT的实现和使用其输出

我从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); 


} 

} 
} 

回答

0

你所得到的错误

ERROR: 'freq_array' was not declared in this scope 

因为freq_array被声明为setup()功能范围的局部变量,因而不在此范围内,你要使用它外部访问。

为了解决这个错误,你只需要十二月通过将声明移动到setup()函数体外部,在文件范围内运行freq_array

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

好点。这解决了错误,但现在它说“str”没有声明,可能是因为即时通讯编写C而不是C++?我以前从未使用过str。我将如何在C中实现这一点? – FLITSCHI

+0

确实'str'不是C.你可以尝试'char aa [BUF_SIZE];为每个变量提供适当缓冲区大小的sprintf(aa,“%.0f”,freq_array [idx])'(或者为整个字符串设置缓冲区大小,1024对于所示情况可能就足够了)。 – SleuthEye