initial
Dependencies: mbed BSP_DISCO_F746NG mbed-dsp
signal_processing.cpp@1:103e3e426b55, 2020-02-12 (annotated)
- Committer:
- justenmg
- Date:
- Wed Feb 12 01:03:05 2020 +0000
- Revision:
- 1:103e3e426b55
- Parent:
- 0:c0f52e8223fe
- Child:
- 2:89234085faae
feb 11
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bmazzeo | 0:c0f52e8223fe | 1 | /** |
bmazzeo | 0:c0f52e8223fe | 2 | ****************************************************************************** |
bmazzeo | 0:c0f52e8223fe | 3 | * @file signal_processing.c |
bmazzeo | 0:c0f52e8223fe | 4 | * @author Brian Mazzeo |
bmazzeo | 0:c0f52e8223fe | 5 | * @date 2020 |
bmazzeo | 0:c0f52e8223fe | 6 | * @brief This file provides a set of code for signal processing in 487. |
bmazzeo | 0:c0f52e8223fe | 7 | * Parts are taken from example code from STMIcroelectronics |
bmazzeo | 0:c0f52e8223fe | 8 | ****************************************************************************** |
bmazzeo | 0:c0f52e8223fe | 9 | * @attention |
bmazzeo | 0:c0f52e8223fe | 10 | * This code was specifically developed for BYU ECEn 487 course |
bmazzeo | 0:c0f52e8223fe | 11 | * Introduction to Digital Signal Processing. |
bmazzeo | 0:c0f52e8223fe | 12 | * |
bmazzeo | 0:c0f52e8223fe | 13 | * |
bmazzeo | 0:c0f52e8223fe | 14 | ****************************************************************************** |
bmazzeo | 0:c0f52e8223fe | 15 | */ |
bmazzeo | 0:c0f52e8223fe | 16 | |
bmazzeo | 0:c0f52e8223fe | 17 | #include "mbed.h" |
bmazzeo | 0:c0f52e8223fe | 18 | #include "stm32746g_discovery_lcd.h" |
bmazzeo | 0:c0f52e8223fe | 19 | #include "arm_math.h" |
bmazzeo | 0:c0f52e8223fe | 20 | #include "arm_const_structs.h" |
bmazzeo | 0:c0f52e8223fe | 21 | #include "filter_coefficients.h" |
justenmg | 1:103e3e426b55 | 22 | #include "our_filter.h" |
bmazzeo | 0:c0f52e8223fe | 23 | |
bmazzeo | 0:c0f52e8223fe | 24 | |
bmazzeo | 0:c0f52e8223fe | 25 | /* ---------------------------------------------------------------------- |
bmazzeo | 0:c0f52e8223fe | 26 | ** Defines for signal processing |
bmazzeo | 0:c0f52e8223fe | 27 | ** ------------------------------------------------------------------- */ |
bmazzeo | 0:c0f52e8223fe | 28 | |
bmazzeo | 0:c0f52e8223fe | 29 | #define AUDIO_BLOCK_SAMPLES ((uint32_t)128) // Number of samples (L and R) in audio block (each samples is 16 bits) |
justenmg | 1:103e3e426b55 | 30 | #define BUFFER_LENGTH (WIN_NUM_TAPS + AUDIO_BLOCK_SAMPLES - 1) |
bmazzeo | 0:c0f52e8223fe | 31 | |
bmazzeo | 0:c0f52e8223fe | 32 | /* For Lab Exercise */ |
justenmg | 1:103e3e426b55 | 33 | #define Lab_Execution_Type 2 |
justenmg | 1:103e3e426b55 | 34 | |
justenmg | 1:103e3e426b55 | 35 | float32_t lState[NUM_TAPS + AUDIO_BLOCK_SAMPLES - 1]; |
justenmg | 1:103e3e426b55 | 36 | float32_t rState[NUM_TAPS + AUDIO_BLOCK_SAMPLES - 1]; |
bmazzeo | 0:c0f52e8223fe | 37 | |
justenmg | 1:103e3e426b55 | 38 | float32_t l_buf[BUFFER_LENGTH]; |
justenmg | 1:103e3e426b55 | 39 | float32_t r_buf[BUFFER_LENGTH]; |
bmazzeo | 0:c0f52e8223fe | 40 | |
justenmg | 1:103e3e426b55 | 41 | float32_t* l_buf_head = &l_buf; |
justenmg | 1:103e3e426b55 | 42 | float32_t* r_buf_head = &r_buf; |
justenmg | 1:103e3e426b55 | 43 | |
justenmg | 1:103e3e426b55 | 44 | arm_fir_instance_f32 filter_left; |
justenmg | 1:103e3e426b55 | 45 | arm_fir_instance_f32 filter_right; |
bmazzeo | 0:c0f52e8223fe | 46 | |
bmazzeo | 0:c0f52e8223fe | 47 | /* FUNCTION DEFINITIONS BELOW */ |
bmazzeo | 0:c0f52e8223fe | 48 | |
bmazzeo | 0:c0f52e8223fe | 49 | /** |
bmazzeo | 0:c0f52e8223fe | 50 | * @brief Initialize filter structures to be used in loops later |
bmazzeo | 0:c0f52e8223fe | 51 | * @retval None |
bmazzeo | 0:c0f52e8223fe | 52 | */ |
bmazzeo | 0:c0f52e8223fe | 53 | void initalize_signal_processing(void) { |
bmazzeo | 0:c0f52e8223fe | 54 | |
bmazzeo | 0:c0f52e8223fe | 55 | switch (Lab_Execution_Type) |
bmazzeo | 0:c0f52e8223fe | 56 | { |
bmazzeo | 0:c0f52e8223fe | 57 | case 0: // Passthrough case |
bmazzeo | 0:c0f52e8223fe | 58 | break; |
bmazzeo | 0:c0f52e8223fe | 59 | |
bmazzeo | 0:c0f52e8223fe | 60 | case 1: // FIR case (ARM) |
justenmg | 1:103e3e426b55 | 61 | arm_fir_init_f32(&filter_left, NUM_TAPS, (float32_t *)&Filter_coeffs, (float32_t *)&lState, AUDIO_BLOCK_SAMPLES); |
justenmg | 1:103e3e426b55 | 62 | arm_fir_init_f32(&filter_right, NUM_TAPS, (float32_t *)&Filter_coeffs, (float32_t *)&rState, AUDIO_BLOCK_SAMPLES); |
bmazzeo | 0:c0f52e8223fe | 63 | break; |
bmazzeo | 0:c0f52e8223fe | 64 | |
bmazzeo | 0:c0f52e8223fe | 65 | case 2: // FIR case (student) |
justenmg | 1:103e3e426b55 | 66 | arm_fir_init_f32(&filter_left, OUR_NUM_TAPS, (float32_t *)&our_Filter_coeffs, (float32_t *)&lState, AUDIO_BLOCK_SAMPLES); |
justenmg | 1:103e3e426b55 | 67 | arm_fir_init_f32(&filter_right, OUR_NUM_TAPS, (float32_t *)&our_Filter_coeffs, (float32_t *)&rState, AUDIO_BLOCK_SAMPLES); |
bmazzeo | 0:c0f52e8223fe | 68 | break; |
bmazzeo | 0:c0f52e8223fe | 69 | |
bmazzeo | 0:c0f52e8223fe | 70 | case 3: // FFT Overlap-add |
bmazzeo | 0:c0f52e8223fe | 71 | break; |
bmazzeo | 0:c0f52e8223fe | 72 | |
bmazzeo | 0:c0f52e8223fe | 73 | case 4: // FFT Overlap-add with real-imag efficiency |
bmazzeo | 0:c0f52e8223fe | 74 | break; |
bmazzeo | 0:c0f52e8223fe | 75 | |
bmazzeo | 0:c0f52e8223fe | 76 | |
bmazzeo | 0:c0f52e8223fe | 77 | } |
bmazzeo | 0:c0f52e8223fe | 78 | } |
bmazzeo | 0:c0f52e8223fe | 79 | |
bmazzeo | 0:c0f52e8223fe | 80 | /** |
bmazzeo | 0:c0f52e8223fe | 81 | * @brief Process audio channel signals |
bmazzeo | 0:c0f52e8223fe | 82 | * @param L_channel_in: Pointer to Left channel data input (float32_t) |
bmazzeo | 0:c0f52e8223fe | 83 | * @param R_channel_in: Pointer to Right channel data input (float32_t) |
bmazzeo | 0:c0f52e8223fe | 84 | * @param L_channel_out: Pointer to Left channel data output (float32_t) |
bmazzeo | 0:c0f52e8223fe | 85 | * @param R_channel_out: Pointer to Right channel data output (float32_t) |
bmazzeo | 0:c0f52e8223fe | 86 | * @param Signal_Length: length of data to process |
bmazzeo | 0:c0f52e8223fe | 87 | * @retval None |
bmazzeo | 0:c0f52e8223fe | 88 | */ |
bmazzeo | 0:c0f52e8223fe | 89 | void process_audio_channel_signals(float32_t* L_channel_in, float32_t* R_channel_in, float32_t* L_channel_out, float32_t* R_channel_out, uint16_t Signal_Length) |
bmazzeo | 0:c0f52e8223fe | 90 | { |
bmazzeo | 0:c0f52e8223fe | 91 | char buf[70]; |
bmazzeo | 0:c0f52e8223fe | 92 | BSP_LCD_SetFont(&Font8); |
bmazzeo | 0:c0f52e8223fe | 93 | BSP_LCD_SetTextColor(LCD_COLOR_CYAN); |
bmazzeo | 0:c0f52e8223fe | 94 | sprintf(buf, "Processing Signals" ); |
bmazzeo | 0:c0f52e8223fe | 95 | BSP_LCD_DisplayStringAt(0, 200, (uint8_t *) buf, LEFT_MODE); |
bmazzeo | 0:c0f52e8223fe | 96 | |
bmazzeo | 0:c0f52e8223fe | 97 | switch(Lab_Execution_Type) |
bmazzeo | 0:c0f52e8223fe | 98 | { |
bmazzeo | 0:c0f52e8223fe | 99 | case 0: // Passthrough case |
bmazzeo | 0:c0f52e8223fe | 100 | arm_copy_f32(L_channel_in, L_channel_out, AUDIO_BLOCK_SAMPLES); |
bmazzeo | 0:c0f52e8223fe | 101 | arm_copy_f32(R_channel_in, R_channel_out, AUDIO_BLOCK_SAMPLES); |
bmazzeo | 0:c0f52e8223fe | 102 | break; |
bmazzeo | 0:c0f52e8223fe | 103 | |
bmazzeo | 0:c0f52e8223fe | 104 | case 1: // FIR case (ARM) |
justenmg | 1:103e3e426b55 | 105 | arm_fir_f32(&filter_left, L_channel_in, L_channel_out, AUDIO_BLOCK_SAMPLES); |
justenmg | 1:103e3e426b55 | 106 | arm_fir_f32(&filter_right, R_channel_in, R_channel_out, AUDIO_BLOCK_SAMPLES); |
bmazzeo | 0:c0f52e8223fe | 107 | break; |
bmazzeo | 0:c0f52e8223fe | 108 | |
bmazzeo | 0:c0f52e8223fe | 109 | case 2: // FIR case (student) |
justenmg | 1:103e3e426b55 | 110 | arm_fir_f32(&filter_left, L_channel_in, L_channel_out, AUDIO_BLOCK_SAMPLES); |
justenmg | 1:103e3e426b55 | 111 | arm_fir_f32(&filter_right, R_channel_in, R_channel_out, AUDIO_BLOCK_SAMPLES); |
bmazzeo | 0:c0f52e8223fe | 112 | break; |
bmazzeo | 0:c0f52e8223fe | 113 | |
bmazzeo | 0:c0f52e8223fe | 114 | case 3: // FFT Overlap-add |
bmazzeo | 0:c0f52e8223fe | 115 | break; |
bmazzeo | 0:c0f52e8223fe | 116 | |
bmazzeo | 0:c0f52e8223fe | 117 | case 4: // FFT Overlap-add with real-imag efficiency |
bmazzeo | 0:c0f52e8223fe | 118 | break; |
bmazzeo | 0:c0f52e8223fe | 119 | |
bmazzeo | 0:c0f52e8223fe | 120 | |
bmazzeo | 0:c0f52e8223fe | 121 | } |
bmazzeo | 0:c0f52e8223fe | 122 | /* Change font back */ |
bmazzeo | 0:c0f52e8223fe | 123 | BSP_LCD_SetFont(&Font16); |
bmazzeo | 0:c0f52e8223fe | 124 | } |
justenmg | 1:103e3e426b55 | 125 | |
justenmg | 1:103e3e426b55 | 126 | |
justenmg | 1:103e3e426b55 | 127 | void filter(float32_t* buffer, float32_t* d_in, float32_t* d_out, uint16_t buf_length) |
justenmg | 1:103e3e426b55 | 128 | { |
justenmg | 1:103e3e426b55 | 129 | float32_t result = 0; |
justenmg | 1:103e3e426b55 | 130 | float32_t* data_sample = d_out; |
justenmg | 1:103e3e426b55 | 131 | for(i=0; i<buf_length; i++) |
justenmg | 1:103e3e426b55 | 132 | { |
justenmg | 1:103e3e426b55 | 133 | *data_sample = convolve(d_in, win_filter_coeffs, AUDIO_BLOCK_SAMPLES, BUFFER_LENGTH); |
justenmg | 1:103e3e426b55 | 134 | data_sample++; //******************************************************* |
justenmg | 1:103e3e426b55 | 135 | } |
justenmg | 1:103e3e426b55 | 136 | } |
justenmg | 1:103e3e426b55 | 137 | |
justenmg | 1:103e3e426b55 | 138 | float32_t convolve(float32_t* data, float32_t* filter, uint16_t sig_length, uint16_t buf_length) |
justenmg | 1:103e3e426b55 | 139 | { |
justenmg | 1:103e3e426b55 | 140 | float32_t* data_sample = data+buf_length-1; |
justenmg | 1:103e3e426b55 | 141 | float32_t* filter_sample = filter; |
justenmg | 1:103e3e426b55 | 142 | float32_t result = 0; |
justenmg | 1:103e3e426b55 | 143 | for(i=0; i<buf_length; i++) |
justenmg | 1:103e3e426b55 | 144 | { |
justenmg | 1:103e3e426b55 | 145 | result += (*filter_sample) * (*data_sample); |
justenmg | 1:103e3e426b55 | 146 | } |
justenmg | 1:103e3e426b55 | 147 | return result; |
justenmg | 1:103e3e426b55 | 148 | } |