Team Riedel - display
Dependencies: LCD_fonts SPI_TFT_ILI9341 CMSIS_DSP_401_without_cm4 mbed-src SDFileSystem wavfile
audio_record.cpp@13:ab0285338951, 2015-12-14 (annotated)
- Committer:
- linx0576
- Date:
- Mon Dec 14 22:13:11 2015 +0000
- Revision:
- 13:ab0285338951
- Parent:
- 10:0986108f8aa3
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
EricLew | 9:5a860b9c8a6a | 1 | |
EricLew | 9:5a860b9c8a6a | 2 | #include "main.h" |
EricLew | 9:5a860b9c8a6a | 3 | #include "stm32l476g_discovery_audio.h" |
linx0576 | 10:0986108f8aa3 | 4 | // |
linx0576 | 10:0986108f8aa3 | 5 | #include "SPI_TFT_ILI9341.h" |
linx0576 | 10:0986108f8aa3 | 6 | #include <math.h> |
linx0576 | 10:0986108f8aa3 | 7 | #include "stdio.h" |
linx0576 | 10:0986108f8aa3 | 8 | #include "mbed.h" |
linx0576 | 10:0986108f8aa3 | 9 | #include "string" |
linx0576 | 10:0986108f8aa3 | 10 | #include "Arial12x12.h" |
linx0576 | 10:0986108f8aa3 | 11 | #include "Arial24x23.h" |
linx0576 | 10:0986108f8aa3 | 12 | #include "arm_math.h" |
linx0576 | 10:0986108f8aa3 | 13 | #include "SDFileSystem.h" |
linx0576 | 10:0986108f8aa3 | 14 | // |
EricLew | 9:5a860b9c8a6a | 15 | //GLOBAL VARIABLES |
EricLew | 9:5a860b9c8a6a | 16 | float32_t POWER[1024]; |
EricLew | 9:5a860b9c8a6a | 17 | float32_t PHS[1024]; |
EricLew | 9:5a860b9c8a6a | 18 | float32_t PWRMSE; |
EricLew | 9:5a860b9c8a6a | 19 | float32_t PHSMSE; |
EricLew | 9:5a860b9c8a6a | 20 | uint32_t n; //Generic variable for "for" loops |
EricLew | 9:5a860b9c8a6a | 21 | float32_t CONTROLPWR[1024]; //CONTROL 0 PWR |
EricLew | 9:5a860b9c8a6a | 22 | float32_t CONTROLPHS[1024]; //CTONROL 0 PHS |
EricLew | 9:5a860b9c8a6a | 23 | float32_t CONTROLPWRMSE;//CONTROL PWR MSE |
EricLew | 9:5a860b9c8a6a | 24 | float32_t CONTROLPHSMSE;//CONTROL PHS MSE |
linx0576 | 10:0986108f8aa3 | 25 | extern SPI_TFT_ILI9341 TFT; |
EricLew | 9:5a860b9c8a6a | 26 | |
EricLew | 9:5a860b9c8a6a | 27 | |
EricLew | 9:5a860b9c8a6a | 28 | void RECORD(void) |
EricLew | 9:5a860b9c8a6a | 29 | { |
linx0576 | 10:0986108f8aa3 | 30 | int row =0; |
EricLew | 9:5a860b9c8a6a | 31 | uint32_t counter=0; |
EricLew | 9:5a860b9c8a6a | 32 | int EXIT=0; //WHILE LOOP EXIT FLAG |
EricLew | 9:5a860b9c8a6a | 33 | // uint16_t PDMBUFF[1536]; |
EricLew | 9:5a860b9c8a6a | 34 | uint16_t RECORDBUFF[2048]; |
EricLew | 9:5a860b9c8a6a | 35 | float32_t FLOATBUFF[4096]; |
EricLew | 9:5a860b9c8a6a | 36 | const arm_cfft_instance_f32 *S=&arm_cfft_sR_f32_len2048; |
EricLew | 9:5a860b9c8a6a | 37 | |
EricLew | 9:5a860b9c8a6a | 38 | /* Initialize audio input */ |
EricLew | 9:5a860b9c8a6a | 39 | pc.printf("\r\n"); |
EricLew | 9:5a860b9c8a6a | 40 | pc.printf("Initializing Audio...\r\n"); |
EricLew | 9:5a860b9c8a6a | 41 | if(Audio_Init()!= AUDIO_OK) |
EricLew | 9:5a860b9c8a6a | 42 | { |
linx0576 | 10:0986108f8aa3 | 43 | TFT.locate(10,row); TFT.printf("FAILED to initialize audio...\r\n"); row+=20; |
linx0576 | 10:0986108f8aa3 | 44 | //pc.printf("FAILED to initialize audio...\r\n"); |
EricLew | 9:5a860b9c8a6a | 45 | } |
linx0576 | 10:0986108f8aa3 | 46 | //pc.printf("Initialization completed successfully.\r\n"); |
linx0576 | 10:0986108f8aa3 | 47 | else |
linx0576 | 10:0986108f8aa3 | 48 | {TFT.locate(10,row); TFT.printf("Initialization completed successfully.\r\n"); row+=20;} |
EricLew | 9:5a860b9c8a6a | 49 | /* Start the audio record */ |
EricLew | 9:5a860b9c8a6a | 50 | |
EricLew | 9:5a860b9c8a6a | 51 | if (Audio_Record(RECORDBUFF) != AUDIO_OK) |
EricLew | 9:5a860b9c8a6a | 52 | { |
linx0576 | 10:0986108f8aa3 | 53 | TFT.locate(10,row); TFT.printf("FAILED to begin recording...\r\n"); row+=20; |
linx0576 | 10:0986108f8aa3 | 54 | // pc.printf("FAILED to begin recording...\r\n"); |
EricLew | 9:5a860b9c8a6a | 55 | } |
EricLew | 9:5a860b9c8a6a | 56 | |
EricLew | 9:5a860b9c8a6a | 57 | // pc.printf("DMA\tSPI\tRXNE\tBSY\tCRCERR\tOVR\r\n"); |
EricLew | 9:5a860b9c8a6a | 58 | |
EricLew | 9:5a860b9c8a6a | 59 | /* PCM samples recording loop */ |
linx0576 | 10:0986108f8aa3 | 60 | // RecordBufferOffset = BUFFER_OFFSET_NONE; |
linx0576 | 10:0986108f8aa3 | 61 | // while (RecordBufferOffset != BUFFER_OFFSET_FULL); |
EricLew | 9:5a860b9c8a6a | 62 | |
EricLew | 9:5a860b9c8a6a | 63 | // while (EXIT != SET) |
EricLew | 9:5a860b9c8a6a | 64 | // { |
EricLew | 9:5a860b9c8a6a | 65 | // 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)); |
EricLew | 9:5a860b9c8a6a | 66 | // if (counter==10000000) //Approximately 3 seconds of record time |
EricLew | 9:5a860b9c8a6a | 67 | // { |
EricLew | 9:5a860b9c8a6a | 68 | // EXIT=1; |
EricLew | 9:5a860b9c8a6a | 69 | // } |
EricLew | 9:5a860b9c8a6a | 70 | // else |
EricLew | 9:5a860b9c8a6a | 71 | // { |
EricLew | 9:5a860b9c8a6a | 72 | // counter=counter++; |
EricLew | 9:5a860b9c8a6a | 73 | // } |
EricLew | 9:5a860b9c8a6a | 74 | // } |
EricLew | 9:5a860b9c8a6a | 75 | /* Stop audio input */ |
EricLew | 9:5a860b9c8a6a | 76 | Audio_Stop(); |
linx0576 | 10:0986108f8aa3 | 77 | TFT.locate(10,row); TFT.printf("Audio recording processed successfully!\r\n"); row+=20; |
linx0576 | 10:0986108f8aa3 | 78 | // pc.printf("Audio recording processed successfully!\r\n"); |
EricLew | 9:5a860b9c8a6a | 79 | //myled = 0; |
EricLew | 9:5a860b9c8a6a | 80 | WriteData(RECORDBUFF); |
EricLew | 9:5a860b9c8a6a | 81 | |
EricLew | 9:5a860b9c8a6a | 82 | for(n=0;n<2048;n++) |
EricLew | 9:5a860b9c8a6a | 83 | { |
EricLew | 9:5a860b9c8a6a | 84 | FLOATBUFF[n*2] = (float)RECORDBUFF[n]; |
EricLew | 9:5a860b9c8a6a | 85 | FLOATBUFF[(n*2)+1] = 0.0; |
EricLew | 9:5a860b9c8a6a | 86 | } |
EricLew | 9:5a860b9c8a6a | 87 | arm_cfft_f32(S, FLOATBUFF, 0,0); //Output of FFT is half the record buffer size |
EricLew | 9:5a860b9c8a6a | 88 | |
EricLew | 9:5a860b9c8a6a | 89 | for(n=0;n<1024;n++) |
EricLew | 9:5a860b9c8a6a | 90 | {PHS[n]=atan2(FLOATBUFF[(n*2)+1],FLOATBUFF[n*2]);}//Calculates control phase |
EricLew | 9:5a860b9c8a6a | 91 | for(n=0;n<1024;n++) |
EricLew | 9:5a860b9c8a6a | 92 | { |
EricLew | 9:5a860b9c8a6a | 93 | POWER[n]=sqrt((FLOATBUFF[(n*2)+1]*FLOATBUFF[(n*2)+1])+(FLOATBUFF[n*2]*FLOATBUFF[n*2])); |
EricLew | 9:5a860b9c8a6a | 94 | } //calculates control power |
EricLew | 9:5a860b9c8a6a | 95 | } |
EricLew | 9:5a860b9c8a6a | 96 | |
EricLew | 9:5a860b9c8a6a | 97 | void MSE(void) |
EricLew | 9:5a860b9c8a6a | 98 | { |
EricLew | 9:5a860b9c8a6a | 99 | float32_t compare[1024]; //BUFFER VARIABLE FOR MSE FUNCTIONS |
EricLew | 9:5a860b9c8a6a | 100 | //MEAN SQUARED ERROR FOR SAMPLE MAGNITUDES |
EricLew | 9:5a860b9c8a6a | 101 | PWRMSE=0; |
EricLew | 9:5a860b9c8a6a | 102 | for(n=0;n<1024;n++) |
EricLew | 9:5a860b9c8a6a | 103 | {compare[n]=(CONTROLPWR[n]-POWER[n])*(CONTROLPWR[n]-POWER[n]);} //Error between FFT magnitudes, stored in compare |
EricLew | 9:5a860b9c8a6a | 104 | for(n=0;n<1024;n++) |
EricLew | 9:5a860b9c8a6a | 105 | {PWRMSE=PWRMSE+compare[n];} //Takes the mean of the error, stores MSE in CONTROLMSE |
EricLew | 9:5a860b9c8a6a | 106 | PWRMSE=PWRMSE/1024.0f; |
EricLew | 9:5a860b9c8a6a | 107 | //MEAN SQUARED ERROR FOR SAMPLE PHASE |
EricLew | 9:5a860b9c8a6a | 108 | PHSMSE=0; |
EricLew | 9:5a860b9c8a6a | 109 | for(n=0;n<1024;n++) |
EricLew | 9:5a860b9c8a6a | 110 | {compare[n]=(CONTROLPHS[n]-PHS[n])*(CONTROLPHS[n]-PHS[n]);} //Error between FFT magnitudes, stored in compare |
EricLew | 9:5a860b9c8a6a | 111 | for(n=0;n<1024;n++) |
EricLew | 9:5a860b9c8a6a | 112 | {PHSMSE=PHSMSE+compare[n];} //Takes the mean of the error, stores MSE in CONTROLMSE |
EricLew | 9:5a860b9c8a6a | 113 | PHSMSE=PHSMSE/1024.0f; |
EricLew | 9:5a860b9c8a6a | 114 | } |
EricLew | 9:5a860b9c8a6a | 115 | |
EricLew | 9:5a860b9c8a6a | 116 | void COMPARE(void) |
EricLew | 9:5a860b9c8a6a | 117 | { |
EricLew | 9:5a860b9c8a6a | 118 | float32_t TEST[4]; |
EricLew | 9:5a860b9c8a6a | 119 | TEST[0]=001; //LOW END OF ACCEPTED TOLERANCE RANGE |
EricLew | 9:5a860b9c8a6a | 120 | TEST[1]=20; //HIGH END OF ACCEPTED TOLERANCE RANGE |
EricLew | 9:5a860b9c8a6a | 121 | TEST[2]=(PWRMSE/CONTROLPWRMSE); //The ratio of sample MSE and control MSE |
EricLew | 9:5a860b9c8a6a | 122 | TEST[3]=(PHSMSE/CONTROLPHSMSE); |
EricLew | 9:5a860b9c8a6a | 123 | |
EricLew | 9:5a860b9c8a6a | 124 | //COMPARISON |
EricLew | 9:5a860b9c8a6a | 125 | //if(SAMPLEPHASEMSE>(10*PHSCONTROLMSE)) |
EricLew | 9:5a860b9c8a6a | 126 | //{ |
EricLew | 9:5a860b9c8a6a | 127 | //SEND ERROR MESSAGE |
EricLew | 9:5a860b9c8a6a | 128 | //CREATE EVENT |
EricLew | 9:5a860b9c8a6a | 129 | //} |
EricLew | 9:5a860b9c8a6a | 130 | if(TEST[2]>TEST[0])//MODEM FUNCTIONS |
EricLew | 9:5a860b9c8a6a | 131 | { |
EricLew | 9:5a860b9c8a6a | 132 | //SEND ERROR MESSAGE |
EricLew | 9:5a860b9c8a6a | 133 | //CREATE EVENT |
EricLew | 9:5a860b9c8a6a | 134 | } |
EricLew | 9:5a860b9c8a6a | 135 | if(TEST[2] < TEST[1]) |
EricLew | 9:5a860b9c8a6a | 136 | { |
EricLew | 9:5a860b9c8a6a | 137 | //SEND ERROR MESSAGE |
EricLew | 9:5a860b9c8a6a | 138 | //CREATE EVENT |
EricLew | 9:5a860b9c8a6a | 139 | } |
EricLew | 9:5a860b9c8a6a | 140 | } |