Example Audio Spectrum Analyzer

Dependencies:   mbed FFT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Nothing special, an example for using the library of FFT, Audio Spectrum Analyzer with LCD
00002 #include "mbed.h"
00003 #include "FFT.h"
00004 #include "LCD_Serial.h"
00005 // **********************************************************************************************
00006 #define N 32
00007 float Data[N*2];  
00008 unsigned char PowerInt[N/2];
00009 volatile int kbhit;
00010 float Average;
00011 
00012 AnalogIn    ChannelR(p16);
00013 AnalogIn    ChannelL(p17);
00014 AnalogIn    ChannelC(p18);
00015 LCDSerial myLcd(p21,p22,p23,p24); // Data, Clock, Enable, Back
00016 // **********************************************************************************************
00017 void vUpdateDataAnalogs(float Analogs[]);
00018 void vGraphicDisplay(unsigned char *Data);
00019 // **********************************************************************************************
00020 int main() {
00021     myLcd.vSetBacklight(1);
00022     myLcd.printf("\f FFT with mbed!!!");
00023     wait(2.0);
00024     myLcd.printf("\f");
00025     
00026     Average=0;
00027     for(int k=0;k<64;k++){
00028         Average+=ChannelC.read_u16();
00029         wait_us(100);
00030     }
00031     Average/=64;
00032    
00033     while(1){
00034         vUpdateDataAnalogs(Data);
00035         vFFT(Data-1,N);
00036         vCalPowerInt(Data,PowerInt,N/2);
00037         vGraphicDisplay(PowerInt);
00038         wait_ms(30);
00039     }
00040 }
00041 // **********************************************************************************************
00042 void vUpdate(void){
00043     kbhit=1;
00044 }
00045 void vUpdateDataAnalogs(float Analogs[]){
00046     unsigned short TempReadR[32],TempReadL[32];
00047     Ticker Period;
00048     
00049     kbhit=0;
00050     Period.attach_us(&vUpdate,25); // 25 us, 40.000 Hz
00051     for(int k=0;k<32;k++){
00052         while(kbhit==0);
00053         kbhit=0;
00054         TempReadR[k]=ChannelR.read_u16();
00055     }
00056     for(int k=0;k<32;k++){
00057         while(kbhit==0);
00058         kbhit=0;
00059         TempReadL[k]=ChannelL.read_u16();
00060     }
00061     Period.detach();
00062     for(int k=0,j=0;k<32;k++,j++){
00063         Analogs[j]=(TempReadR[k]+TempReadL[k])/2;   // Promedio (x1[n]+x2[n]=X1[k]+X2[k])
00064         Analogs[j]=((float)(Analogs[j]-Average)/10);   // Desplazo y aplico escala.-
00065         Analogs[++j]=0;
00066     }
00067     return;
00068 }
00069 // **********************************************************************************************
00070 void vGraphicDisplay(unsigned char *Data){
00071      unsigned char k;
00072  
00073      myLcd.vGotoxy(3,1);
00074      for(k=0;k<16;k++,Data++){
00075          if(*Data>192){myLcd.vPutc((*Data-193)/8);}else{myLcd.vPutc(' ');}
00076      }
00077      myLcd.vGotoxy(3,2);
00078      Data-=16;
00079     for(k=0;k<16;k++,Data++){
00080          if((*Data>128)&&(*Data<193)){myLcd.vPutc((*Data-129)/8);}else if(*Data>192){myLcd.vPutc(0x07);}else{myLcd.vPutc(' ');}
00081      }
00082      myLcd.vGotoxy(3,3);
00083      Data-=16;
00084      for(k=0;k<16;k++,Data++){
00085          if((*Data>64)&&(*Data<129)){myLcd.vPutc((*Data-65)/8);}else if(*Data>128){myLcd.vPutc(0x07);}else{myLcd.vPutc(' ');}
00086      }
00087      myLcd.vGotoxy(3,4);
00088      Data-=16;
00089      for(k=0;k<16;k++,Data++){
00090          if((*Data>0)&&(*Data<65)){myLcd.vPutc((*Data-1)/8);}else if(*Data>64){myLcd.vPutc(0x07);}else{myLcd.vPutc(' ');}
00091      }
00092  
00093  }