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