Team Riedel - display

Dependencies:   LCD_fonts SPI_TFT_ILI9341 CMSIS_DSP_401_without_cm4 mbed-src SDFileSystem wavfile

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers audio_record.cpp Source File

audio_record.cpp

00001 
00002 #include "main.h"
00003 #include "stm32l476g_discovery_audio.h"
00004 //
00005 #include "SPI_TFT_ILI9341.h"
00006 #include <math.h>
00007 #include "stdio.h"
00008 #include "mbed.h"
00009 #include "string"
00010 #include "Arial12x12.h"
00011 #include "Arial24x23.h"
00012 #include "arm_math.h" 
00013 #include "SDFileSystem.h"
00014 //
00015 //GLOBAL VARIABLES 
00016 float32_t POWER[1024];
00017 float32_t PHS[1024];
00018 float32_t PWRMSE;
00019 float32_t PHSMSE;
00020 uint32_t n;  //Generic variable for "for" loops
00021 float32_t CONTROLPWR[1024]; //CONTROL 0 PWR
00022 float32_t CONTROLPHS[1024]; //CTONROL 0 PHS
00023 float32_t CONTROLPWRMSE;//CONTROL PWR MSE
00024 float32_t CONTROLPHSMSE;//CONTROL PHS MSE
00025 extern SPI_TFT_ILI9341 TFT;
00026 
00027                                                         
00028 void RECORD(void)
00029 {
00030     int row =0;
00031     uint32_t counter=0;
00032     int EXIT=0; //WHILE LOOP EXIT FLAG
00033 //    uint16_t PDMBUFF[1536];
00034     uint16_t RECORDBUFF[2048];
00035     float32_t FLOATBUFF[4096];
00036     const arm_cfft_instance_f32 *S=&arm_cfft_sR_f32_len2048;
00037     
00038     /* Initialize audio input */  
00039     pc.printf("\r\n");
00040     pc.printf("Initializing Audio...\r\n");                          
00041     if(Audio_Init()!= AUDIO_OK)
00042         {
00043         TFT.locate(10,row); TFT.printf("FAILED to initialize audio...\r\n"); row+=20; 
00044         //pc.printf("FAILED to initialize audio...\r\n");
00045         }
00046     //pc.printf("Initialization completed successfully.\r\n");
00047     else 
00048     {TFT.locate(10,row); TFT.printf("Initialization completed successfully.\r\n"); row+=20;}
00049   /* Start the audio record */
00050 
00051         if (Audio_Record(RECORDBUFF) != AUDIO_OK)
00052             {
00053                 TFT.locate(10,row); TFT.printf("FAILED to begin recording...\r\n"); row+=20;
00054 //                pc.printf("FAILED to begin recording...\r\n");
00055             } 
00056             
00057 //    pc.printf("DMA\tSPI\tRXNE\tBSY\tCRCERR\tOVR\r\n");
00058   
00059   /* PCM samples recording loop */
00060 //  RecordBufferOffset = BUFFER_OFFSET_NONE;
00061 //  while (RecordBufferOffset != BUFFER_OFFSET_FULL);
00062   
00063 //  while (EXIT != SET)
00064 //  {
00065 //        if (!(counter%1000000)) pc.printf("%d\t%d\t%d\t%d\t%d\t%d\r\n", getDMAState(), getSPIState(), getSPIFlagStatus(SPI_SR_RXNE), getSPIFlagStatus(SPI_SR_BSY), getSPIFlagStatus(SPI_SR_CRCERR), getSPIFlagStatus(SPI_SR_OVR));
00066 //        if (counter==10000000) //Approximately 3 seconds of record time
00067 //        {
00068 //            EXIT=1;
00069 //        }
00070 //        else
00071 //        {
00072 //            counter=counter++;
00073 //        }
00074 //  }
00075   /* Stop audio input */
00076     Audio_Stop();
00077     TFT.locate(10,row); TFT.printf("Audio recording processed successfully!\r\n"); row+=20;
00078 //    pc.printf("Audio recording processed successfully!\r\n");
00079     //myled = 0;
00080     WriteData(RECORDBUFF);
00081         
00082     for(n=0;n<2048;n++)
00083     {
00084         FLOATBUFF[n*2] = (float)RECORDBUFF[n];
00085         FLOATBUFF[(n*2)+1] = 0.0;
00086      }
00087      arm_cfft_f32(S, FLOATBUFF, 0,0); //Output of FFT is half the record buffer size
00088 
00089             for(n=0;n<1024;n++)
00090                 {PHS[n]=atan2(FLOATBUFF[(n*2)+1],FLOATBUFF[n*2]);}//Calculates control phase
00091             for(n=0;n<1024;n++)
00092                 {
00093                     POWER[n]=sqrt((FLOATBUFF[(n*2)+1]*FLOATBUFF[(n*2)+1])+(FLOATBUFF[n*2]*FLOATBUFF[n*2]));
00094                     } //calculates control power
00095 }
00096 
00097 void MSE(void)
00098 {
00099     float32_t compare[1024]; //BUFFER VARIABLE FOR MSE FUNCTIONS
00100     //MEAN SQUARED ERROR FOR SAMPLE MAGNITUDES
00101     PWRMSE=0;
00102     for(n=0;n<1024;n++)
00103         {compare[n]=(CONTROLPWR[n]-POWER[n])*(CONTROLPWR[n]-POWER[n]);} //Error between FFT magnitudes, stored in compare
00104     for(n=0;n<1024;n++)
00105         {PWRMSE=PWRMSE+compare[n];} //Takes the mean of the error, stores MSE in CONTROLMSE
00106     PWRMSE=PWRMSE/1024.0f;
00107     //MEAN SQUARED ERROR FOR SAMPLE PHASE
00108     PHSMSE=0;
00109     for(n=0;n<1024;n++)
00110         {compare[n]=(CONTROLPHS[n]-PHS[n])*(CONTROLPHS[n]-PHS[n]);} //Error between FFT magnitudes, stored in compare
00111     for(n=0;n<1024;n++)
00112         {PHSMSE=PHSMSE+compare[n];} //Takes the mean of the error, stores MSE in CONTROLMSE
00113     PHSMSE=PHSMSE/1024.0f;
00114 }
00115 
00116 void COMPARE(void)
00117 {
00118     float32_t TEST[4];
00119             TEST[0]=001; //LOW END OF ACCEPTED TOLERANCE RANGE
00120             TEST[1]=20;  //HIGH END OF ACCEPTED TOLERANCE RANGE
00121             TEST[2]=(PWRMSE/CONTROLPWRMSE); //The ratio of sample MSE and control MSE
00122             TEST[3]=(PHSMSE/CONTROLPHSMSE);
00123                         
00124                 //COMPARISON
00125                     //if(SAMPLEPHASEMSE>(10*PHSCONTROLMSE))
00126                         //{
00127                             //SEND ERROR MESSAGE
00128                             //CREATE EVENT
00129                         //}
00130                     if(TEST[2]>TEST[0])//MODEM FUNCTIONS
00131                         {
00132                             //SEND ERROR MESSAGE
00133                             //CREATE EVENT
00134                         }
00135                     if(TEST[2] < TEST[1])
00136                         {   
00137                             //SEND ERROR MESSAGE
00138                             //CREATE EVENT
00139                         }
00140 }