Audio Spectrum Analyzer (FFT 32 points and LCD 20x4)

Nothing special, an example for using the library of FFT, Audio Spectrum Analyzer with LCD:

» Import this program

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  }

» Import this programFFT_Example

Example Audio Spectrum Analyzer

/media/uploads/Suky/fft.png

/media/uploads/Suky/imagen0091.jpg

» Import this library into a programFFT

FFT, Audio, Spectrum, Analyzer


5 comments on Audio Spectrum Analyzer (FFT 32 points and LCD 20x4):

19 Oct 2011

This is a very great project, thanks for posting it!

I have a question about your analog input stage... doesn't it harm the op-amps when the incoming analog audio signal is constantly dropping below 0V (since it is beyond the "negative" rail, which in your design is 0V?).

Will this circuit perhaps degrade over time? Sorry if this seems like a beginner question, I just thought that normally the signal should be rectified first... (I am a beginner in analog design)

Thanks again!

19 Oct 2011

R1 and R2 as well as R3 and R4 form a voltage divider which lifts the input(s) to half the powersupply voltage (3.3V/2) so as long as the input signal is smaller than 3.3V top/top it will be fine.. Furthermore the input resistors R5 and R7 will limit the current from the input signal anyway.

Interesting stuff:

Voltage divider http://en.wikipedia.org/wiki/Voltage_divider

and this one also:

OpAmp http://en.wikipedia.org/wiki/Opamp

19 Oct 2011

Good thinking Ale, Everyone should note that this is a linear display ASA. The amplifiers would need to be converted to a LOG function to show the output in db. You can either add external parts to this existing design or use LOG Amp ICs like the AD8307 or AD8310

...kevin

19 Oct 2011

user Kevin Braun wrote:

You can either add external parts to this existing design or use LOG Amp ICs like the AD8307 or AD8310

Could you not simply use a logarithmic function on the values you read from the A/D converter ?

20 Oct 2011

My Bad, Yes you can. I had my hardware engineer hat on yesterday. ...kevin

Please login to post comments.