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