analyse audio input

Dependencies:   PololuLedStrip mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PololuLedStrip.h"
00003 
00004 extern "C" void fftR4(short *y, short *x, int N);
00005 
00006 //***************Global Varity***************************
00007 #define BUF_LEN 1024
00008 #define SAMP_FREQ 48000
00009 #define LED_COUNT 60
00010 short samples[BUF_LEN];// store values from ADC
00011 short mx[BUF_LEN*2];// input data i6bit 4 byte aligned x0r,xoi,x1r,x1i,....
00012 short my[BUF_LEN*2];// output data 16bit 4byte aligned y0r,y0i, y1r,y1i,....
00013 float spectrum[BUF_LEN/2]; // frequency spectrum 
00014 float max1 = 0.0;
00015     float max2 = 0.0;
00016     float max3 = 0.0;
00017     float max4 = 0.0;
00018     float max5 = 0.0;
00019     int j1 = 0;
00020     int j2 = 0;
00021     int j3 = 0;
00022     int j4 = 0;
00023     int j5 = 0;
00024     int j11=0;
00025     int j22=0;
00026     int j33=0;
00027     int j44=0;
00028     int j55=0;
00029 rgb_color colors[LED_COUNT];
00030 //--------------End of Global Varity---------------------
00031 
00032 
00033 
00034 //************** Port Configuration *********************
00035 AnalogIn audio(p20);  // ADC pin must be biased at Vcc/2 using coupling capacitor and potential divider
00036 BusOut leds (LED1,LED2,LED3,LED4);
00037 PololuLedStrip ledStrip(p8);
00038 
00039 
00040 //*** Debug***
00041 Serial serial(USBTX,USBRX); //Real time debug USB port 
00042 LocalFileSystem local("local");     // Create the local filesystem under the name "local"
00043 
00044 //----------------- End of Port Configuration------------
00045 
00046 
00047 // **************SubFunctions Define *******************
00048 void initLED();
00049 void updateSamples();
00050 float magnitude (short y1, short y2);
00051 void doFFT();
00052 void ledBarGraph();
00053 void calcPeakFrequency ();
00054 void runLED();
00055 
00056 //--------------- END of Sub Define---------------------
00057 
00058 
00059 int main()
00060 {
00061     leds = 15;
00062     wait (1.0);
00063     leds =0;
00064     initLED();
00065     
00066     while(1) {
00067         ledBarGraph();
00068         updateSamples();
00069         doFFT();
00070         calcPeakFrequency ();
00071         runLED();
00072         
00073         }// End while
00074     
00075 
00076         
00077 }// End main
00078 
00079 //********************SubFunction Implementation****************
00080 
00081 void initLED(){
00082      for (int i = 0; i < 60; i++){
00083             colors[i] = (rgb_color){ 2,200,2  };
00084             }   
00085     
00086     ledStrip.write(colors, LED_COUNT);
00087     
00088     wait(1);
00089     for (int i = 0; i < 60; i++){
00090             colors[i] = (rgb_color){ 0,0,0 };
00091             }   
00092     
00093     ledStrip.write(colors, LED_COUNT);
00094     
00095     }
00096 
00097 
00098 
00099 
00100 
00101 
00102 
00103 //--------------------------------------
00104 void updateSamples (){
00105     
00106     for (int i=0; i< BUF_LEN; i++){
00107         samples[i]= (short) (audio.read_u16() - 0x8000);
00108         wait_us(1e6/SAMP_FREQ);
00109         
00110         }//End for
00111     
00112     }// End updateSamples
00113     
00114 void doFFT(){
00115     //clear buffers 
00116     for (int i=0; i<BUF_LEN*2; i++){
00117         mx[i]=0;
00118         my[i]=0;
00119         }// end for
00120         
00121     for (int i =0; i<BUF_LEN;i++){
00122         mx[i*2]=samples[i];
00123         }// end for
00124         
00125         fftR4(my,mx,BUF_LEN); // call FFT routine 
00126         
00127         int j=0;// counter number of next for loop
00128     
00129     for (int i= 0;i< BUF_LEN; i+=2 ){
00130         spectrum[j]= magnitude (my[i],my[i+i]); // get magnitude of FFT output to get spectrum data
00131         j++;
00132         }
00133     
00134     }// End doFFT
00135     
00136  //---------------------------   
00137 float magnitude (short y1, short y2){
00138     
00139      return sqrt(float (y1*y1+y2*y2));
00140     }
00141     
00142    //------------------------------------------ 
00143 void calcPeakFrequency (){
00144     float max = 0.0;
00145     
00146     int frequency = 0;
00147     int j = 0;
00148      max1 = 0.0;
00149      max2 = 0.0;
00150      max3 = 0.0;
00151      max4 = 0.0;
00152      max5 = 0.0;
00153      j1 = 0;
00154      j2 = 0;
00155      j3 = 0;
00156      j4 = 0;
00157      j5 = 0;
00158     
00159     
00160     for (int i=0; i<BUF_LEN; i+=2) {  // loop through spectrum and look for maximum value
00161         if (spectrum[j] > max) {
00162             max = spectrum[j];
00163             frequency = int(SAMP_FREQ/BUF_LEN/2*i);
00164         }
00165         j++;
00166     }
00167     
00168     for (int i=0; i<101; i+=2) {  
00169         if (spectrum[j1] > max1) {
00170             max1 = spectrum[j1];
00171             j11=j1;
00172         }
00173         j1++;
00174     }
00175    
00176    for (int i=102; i<204; i+=2) {  
00177         if (spectrum[j2] > max2) {
00178             max2 = spectrum[j2];
00179             j22=j2;
00180         }
00181         j2++;
00182     }
00183     
00184     for (int i=205; i<306; i+=2) {  
00185         if (spectrum[j3] > max3) {
00186             max3 = spectrum[j3];
00187             j33=j3;
00188         }
00189         j3++;
00190     }
00191     
00192     for (int i=307; i<408; i+=2) {  
00193         if (spectrum[j4] > max4) {
00194             max4 = spectrum[j4];
00195             j44=j4;
00196         }
00197         j4++;
00198     }
00199     
00200     for (int i=408; i<511; i+=2) {  
00201         if (spectrum[j5] > max5) {
00202             max5 = spectrum[j5];
00203             j55=j5;
00204         }
00205         j5++;
00206     }
00207     serial.printf("frq=%d\n",frequency);
00208     //serial.printf("j11-j55 = %d,%d,%d,%d,%d\n",j11,j22,j33,j44,j55);
00209     
00210     }//End calcPeakFrequency 
00211  //-------------------------------------------------   
00212     void ledBarGraph()
00213 {
00214     float rms = 0.0;  // initialse array
00215     for (int i = 0; i < BUF_LEN; i++) {
00216         rms+= samples[i]*samples[i];
00217     }
00218     // calc the sum of the squares
00219  
00220     rms/=BUF_LEN;     // get the mean
00221     rms = sqrt(rms);  // and root to get the RMS
00222     rms/= 16384.0;  // scale according to 16-bit signed maximum value
00223  
00224     // check value and update LEDs to show amplitude
00225     if (rms > 0.8) {
00226         leds = 15;
00227     } else if (rms > 0.6) {
00228         leds = 7;
00229     } else if (rms > 0.4) {
00230         leds = 3;
00231     } else if (rms > 0.2) {
00232         leds = 1;
00233     } else {
00234         leds = 0;
00235     }
00236  
00237     //serial.printf("RMS = %f\n",rms);
00238 }//End void ledBarGraph
00239 
00240 //-------------------------------------------
00241 
00242 
00243 
00244 void runLED(){
00245     // clear the ledStrip
00246     for (int i = 0; i < LED_COUNT; i++){
00247         colors[i] = (rgb_color){ 0, 0, 0 };  
00248         } 
00249         
00250         //if ((maxFreq)>60) maxFreq=60; // Limiting Length
00251         
00252         // max1
00253          for (int i = 0; i < (j11/10); i++){
00254             colors[i] = (rgb_color){ 200,2,2  };
00255             }  
00256         
00257         //max2
00258         for (int i = 21; i > (j22/10); i--){
00259             colors[i] = (rgb_color){ 2,200,2  };
00260             }   
00261         
00262         //max3
00263         for (int i = 24; i < (j33/10); i++){
00264             colors[i] = (rgb_color){ 2,200,200  };
00265         }
00266         //max4
00267         for (int i = 45; i > (j44/10); i--){
00268             colors[i] = (rgb_color){ 100,2,200  };
00269         }
00270         //max5
00271         for (int i = 48; i < (j55/10); i++){
00272             colors[i] = (rgb_color){ 200,2,200  };
00273           }  
00274              
00275              ledStrip.write(colors, LED_COUNT);
00276     }
00277 
00278 
00279 
00280 
00281 
00282 
00283 /*void runLED(){
00284     // clear the ledStrip
00285     for (int i = 0; i < LED_COUNT; i++){
00286         colors[i] = (rgb_color){ 0, 0, 0 };  
00287         } 
00288         
00289         //if ((maxFreq)>60) maxFreq=60; // Limiting Length
00290         
00291         // max1
00292          for (int i = 0; i < 9; i++){
00293             colors[i] = (rgb_color){ 200,2,2  };
00294             }  
00295         
00296         //max2
00297         for (int i = 21; i > 12; i--){
00298             colors[i] = (rgb_color){ 2,200,2  };
00299             }   
00300         
00301         //max3
00302         for (int i = 24; i < 33; i++){
00303             colors[i] = (rgb_color){ 2,200,200  };
00304         }
00305         //max4
00306         for (int i = 45; i > 36; i--){
00307             colors[i] = (rgb_color){ 100,2,200  };
00308         }
00309         //max5
00310         for (int i = 48; i < 57; i++){
00311             colors[i] = (rgb_color){ 200,2,200  };
00312           }  
00313              
00314              ledStrip.write(colors, LED_COUNT);
00315     }*/
00316 
00317 /*void runLED(){
00318     // clear the ledStrip
00319     for (int i = 0; i < LED_COUNT; i++){
00320         colors[i] = (rgb_color){ 0, 0, 0 };  
00321         } 
00322         
00323         //if ((maxFreq)>60) maxFreq=60; // Limiting Length
00324         
00325         // max1
00326          for (int i = 0; i < (j11/10); i++){
00327             colors[i] = (rgb_color){ 200,2,2  };
00328             }  
00329         
00330         //max2
00331         for (int i = 21; i < (21-((j22-101)/10)); i--){
00332             colors[i] = (rgb_color){ 2,200,2  };
00333             }   
00334         
00335         //max3
00336         for (int i = 24; i < (24+((j33-204)/10)); i++){
00337             colors[i] = (rgb_color){ 2,200,200  };
00338         }
00339         //max4
00340         for (int i = 45; i < (45-((j44-306)/10)); i--){
00341             colors[i] = (rgb_color){ 100,2,200  };
00342         }
00343         //max5
00344         for (int i = 48; i < (48+((j55-408)/10)); i++){
00345             colors[i] = (rgb_color){ 200,2,200  };
00346           }  
00347              
00348              ledStrip.write(colors, LED_COUNT);
00349     }*/