2016-10-11 97 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