initial

Dependencies:   mbed BSP_DISCO_F746NG mbed-dsp

Committer:
justenmg
Date:
Mon Mar 02 23:33:16 2020 +0000
Revision:
3:51e15bd15778
Parent:
2:89234085faae
Child:
4:99de9b4005d2
March 2

Who changed what in which revision?

UserRevisionLine numberNew 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"
justenmg 3:51e15bd15778 23 #include "windowed.h"
justenmg 3:51e15bd15778 24 #include "signal_processing.h"
bmazzeo 0:c0f52e8223fe 25
bmazzeo 0:c0f52e8223fe 26
bmazzeo 0:c0f52e8223fe 27 /* ----------------------------------------------------------------------
bmazzeo 0:c0f52e8223fe 28 ** Defines for signal processing
bmazzeo 0:c0f52e8223fe 29 ** ------------------------------------------------------------------- */
bmazzeo 0:c0f52e8223fe 30
bmazzeo 0:c0f52e8223fe 31 #define AUDIO_BLOCK_SAMPLES ((uint32_t)128) // Number of samples (L and R) in audio block (each samples is 16 bits)
justenmg 1:103e3e426b55 32 #define BUFFER_LENGTH (WIN_NUM_TAPS + AUDIO_BLOCK_SAMPLES - 1)
bmazzeo 0:c0f52e8223fe 33
bmazzeo 0:c0f52e8223fe 34 /* For Lab Exercise */
justenmg 1:103e3e426b55 35 #define Lab_Execution_Type 2
justenmg 1:103e3e426b55 36
justenmg 1:103e3e426b55 37 float32_t lState[NUM_TAPS + AUDIO_BLOCK_SAMPLES - 1];
justenmg 1:103e3e426b55 38 float32_t rState[NUM_TAPS + AUDIO_BLOCK_SAMPLES - 1];
bmazzeo 0:c0f52e8223fe 39
justenmg 1:103e3e426b55 40 float32_t l_buf[BUFFER_LENGTH];
justenmg 1:103e3e426b55 41 float32_t r_buf[BUFFER_LENGTH];
bmazzeo 0:c0f52e8223fe 42
justenmg 3:51e15bd15778 43 float32_t* l_buf_head = l_buf;
justenmg 2:89234085faae 44 uint16_t l_buf_head_idx = 0;
justenmg 3:51e15bd15778 45 float32_t* r_buf_head = r_buf;
justenmg 2:89234085faae 46 uint16_t r_buf_head_idx = 0;
justenmg 1:103e3e426b55 47
justenmg 1:103e3e426b55 48 arm_fir_instance_f32 filter_left;
justenmg 1:103e3e426b55 49 arm_fir_instance_f32 filter_right;
bmazzeo 0:c0f52e8223fe 50
bmazzeo 0:c0f52e8223fe 51 /* FUNCTION DEFINITIONS BELOW */
bmazzeo 0:c0f52e8223fe 52
bmazzeo 0:c0f52e8223fe 53 /**
bmazzeo 0:c0f52e8223fe 54 * @brief Initialize filter structures to be used in loops later
bmazzeo 0:c0f52e8223fe 55 * @retval None
bmazzeo 0:c0f52e8223fe 56 */
bmazzeo 0:c0f52e8223fe 57 void initalize_signal_processing(void) {
bmazzeo 0:c0f52e8223fe 58
bmazzeo 0:c0f52e8223fe 59 switch (Lab_Execution_Type)
bmazzeo 0:c0f52e8223fe 60 {
bmazzeo 0:c0f52e8223fe 61 case 0: // Passthrough case
bmazzeo 0:c0f52e8223fe 62 break;
bmazzeo 0:c0f52e8223fe 63
bmazzeo 0:c0f52e8223fe 64 case 1: // FIR case (ARM)
justenmg 1:103e3e426b55 65 arm_fir_init_f32(&filter_left, NUM_TAPS, (float32_t *)&Filter_coeffs, (float32_t *)&lState, AUDIO_BLOCK_SAMPLES);
justenmg 1:103e3e426b55 66 arm_fir_init_f32(&filter_right, NUM_TAPS, (float32_t *)&Filter_coeffs, (float32_t *)&rState, AUDIO_BLOCK_SAMPLES);
bmazzeo 0:c0f52e8223fe 67 break;
bmazzeo 0:c0f52e8223fe 68
bmazzeo 0:c0f52e8223fe 69 case 2: // FIR case (student)
justenmg 1:103e3e426b55 70 arm_fir_init_f32(&filter_left, OUR_NUM_TAPS, (float32_t *)&our_Filter_coeffs, (float32_t *)&lState, AUDIO_BLOCK_SAMPLES);
justenmg 1:103e3e426b55 71 arm_fir_init_f32(&filter_right, OUR_NUM_TAPS, (float32_t *)&our_Filter_coeffs, (float32_t *)&rState, AUDIO_BLOCK_SAMPLES);
bmazzeo 0:c0f52e8223fe 72 break;
bmazzeo 0:c0f52e8223fe 73
bmazzeo 0:c0f52e8223fe 74 case 3: // FFT Overlap-add
justenmg 2:89234085faae 75 filter_init();
bmazzeo 0:c0f52e8223fe 76 break;
bmazzeo 0:c0f52e8223fe 77
bmazzeo 0:c0f52e8223fe 78 case 4: // FFT Overlap-add with real-imag efficiency
bmazzeo 0:c0f52e8223fe 79 break;
bmazzeo 0:c0f52e8223fe 80
bmazzeo 0:c0f52e8223fe 81
bmazzeo 0:c0f52e8223fe 82 }
bmazzeo 0:c0f52e8223fe 83 }
bmazzeo 0:c0f52e8223fe 84
bmazzeo 0:c0f52e8223fe 85 /**
bmazzeo 0:c0f52e8223fe 86 * @brief Process audio channel signals
bmazzeo 0:c0f52e8223fe 87 * @param L_channel_in: Pointer to Left channel data input (float32_t)
bmazzeo 0:c0f52e8223fe 88 * @param R_channel_in: Pointer to Right channel data input (float32_t)
bmazzeo 0:c0f52e8223fe 89 * @param L_channel_out: Pointer to Left channel data output (float32_t)
bmazzeo 0:c0f52e8223fe 90 * @param R_channel_out: Pointer to Right channel data output (float32_t)
bmazzeo 0:c0f52e8223fe 91 * @param Signal_Length: length of data to process
bmazzeo 0:c0f52e8223fe 92 * @retval None
bmazzeo 0:c0f52e8223fe 93 */
bmazzeo 0:c0f52e8223fe 94 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 95 {
bmazzeo 0:c0f52e8223fe 96 char buf[70];
bmazzeo 0:c0f52e8223fe 97 BSP_LCD_SetFont(&Font8);
bmazzeo 0:c0f52e8223fe 98 BSP_LCD_SetTextColor(LCD_COLOR_CYAN);
bmazzeo 0:c0f52e8223fe 99 sprintf(buf, "Processing Signals" );
bmazzeo 0:c0f52e8223fe 100 BSP_LCD_DisplayStringAt(0, 200, (uint8_t *) buf, LEFT_MODE);
bmazzeo 0:c0f52e8223fe 101
bmazzeo 0:c0f52e8223fe 102 switch(Lab_Execution_Type)
bmazzeo 0:c0f52e8223fe 103 {
bmazzeo 0:c0f52e8223fe 104 case 0: // Passthrough case
justenmg 2:89234085faae 105 arm_copy_f32(L_channel_in, L_channel_out, Signal_Length);
justenmg 2:89234085faae 106 arm_copy_f32(R_channel_in, R_channel_out, Signal_Length);
bmazzeo 0:c0f52e8223fe 107 break;
bmazzeo 0:c0f52e8223fe 108
bmazzeo 0:c0f52e8223fe 109 case 1: // FIR case (ARM)
justenmg 2:89234085faae 110 arm_fir_f32(&filter_left, L_channel_in, L_channel_out, Signal_Length);
justenmg 2:89234085faae 111 arm_fir_f32(&filter_right, R_channel_in, R_channel_out, Signal_Length);
bmazzeo 0:c0f52e8223fe 112 break;
bmazzeo 0:c0f52e8223fe 113
bmazzeo 0:c0f52e8223fe 114 case 2: // FIR case (student)
justenmg 2:89234085faae 115 arm_fir_f32(&filter_left, L_channel_in, L_channel_out, Signal_Length);
justenmg 2:89234085faae 116 arm_fir_f32(&filter_right, R_channel_in, R_channel_out, Signal_Length);
bmazzeo 0:c0f52e8223fe 117 break;
bmazzeo 0:c0f52e8223fe 118
bmazzeo 0:c0f52e8223fe 119 case 3: // FFT Overlap-add
justenmg 2:89234085faae 120 filter(l_buf, l_buf_head, l_buf_head_idx, L_channel_in, L_channel_out, Signal_Length, BUFFER_LENGTH);
justenmg 2:89234085faae 121 filter(r_buf, r_buf_head, r_buf_head_idx, R_channel_in, R_channel_out, Signal_Length, BUFFER_LENGTH);
bmazzeo 0:c0f52e8223fe 122 break;
bmazzeo 0:c0f52e8223fe 123
bmazzeo 0:c0f52e8223fe 124 case 4: // FFT Overlap-add with real-imag efficiency
bmazzeo 0:c0f52e8223fe 125 break;
bmazzeo 0:c0f52e8223fe 126
bmazzeo 0:c0f52e8223fe 127
bmazzeo 0:c0f52e8223fe 128 }
bmazzeo 0:c0f52e8223fe 129 /* Change font back */
bmazzeo 0:c0f52e8223fe 130 BSP_LCD_SetFont(&Font16);
bmazzeo 0:c0f52e8223fe 131 }
justenmg 1:103e3e426b55 132
justenmg 2:89234085faae 133 //buffer: pointer to the storage buffer for the filter output
justenmg 2:89234085faae 134 //buf_length: the length of the storage buffer (len_filter + len_batch - 1)
justenmg 2:89234085faae 135 void filter(float32_t* buffer_begin, float32_t* buffer_head, uint16_t buffer_head_idx, float32_t* d_in, float32_t* d_out, uint16_t sig_length, uint16_t buf_length)
justenmg 1:103e3e426b55 136 {
justenmg 2:89234085faae 137 float32_t* data_sample = d_in+sig_length-1;
justenmg 3:51e15bd15778 138 float32_t* filter_sample = win_filter_coeffs;
justenmg 1:103e3e426b55 139 float32_t result = 0;
justenmg 2:89234085faae 140 uint16_t conv_length = 0;
justenmg 2:89234085faae 141 float32_t* buffer_data_location = buffer_head;
justenmg 2:89234085faae 142
justenmg 2:89234085faae 143 //convolve and save to buffer
justenmg 2:89234085faae 144 for(uint16_t shift = 0; shift < buf_length; shift++)
justenmg 1:103e3e426b55 145 {
justenmg 2:89234085faae 146 //shift
justenmg 3:51e15bd15778 147 if(shift < sig_length)
justenmg 2:89234085faae 148 {
justenmg 2:89234085faae 149 conv_length = shift + 1;
justenmg 2:89234085faae 150 }
justenmg 3:51e15bd15778 151 else if(shift >= sig_length && shift < WIN_NUM_TAPS)
justenmg 2:89234085faae 152 {
justenmg 3:51e15bd15778 153 conv_length = sig_length;
justenmg 2:89234085faae 154 }
justenmg 3:51e15bd15778 155 else if(shift >= WIN_NUM_TAPS)
justenmg 2:89234085faae 156 {
justenmg 3:51e15bd15778 157 conv_length = sig_length - (shift - WIN_NUM_TAPS + 1);
justenmg 2:89234085faae 158 }
justenmg 2:89234085faae 159
justenmg 2:89234085faae 160 result = 0;
justenmg 2:89234085faae 161 //multiply-add
justenmg 3:51e15bd15778 162 for(int i=0; i<conv_length; i++)
justenmg 2:89234085faae 163 {
justenmg 2:89234085faae 164 result += (*filter_sample) * (*data_sample);
justenmg 2:89234085faae 165 filter_sample++;
justenmg 2:89234085faae 166 data_sample--;
justenmg 2:89234085faae 167 }
justenmg 2:89234085faae 168
justenmg 2:89234085faae 169 // save to the buffer
justenmg 2:89234085faae 170 *buffer_data_location += result;
justenmg 2:89234085faae 171 //increment, looping back to beginning of buffer
justenmg 2:89234085faae 172 if(buffer_data_location == (buffer_begin + buf_length - 1))
justenmg 2:89234085faae 173 {
justenmg 2:89234085faae 174 buffer_data_location = buffer_begin;
justenmg 2:89234085faae 175 }
justenmg 2:89234085faae 176 else
justenmg 2:89234085faae 177 {
justenmg 2:89234085faae 178 buffer_data_location++;
justenmg 2:89234085faae 179 }
justenmg 1:103e3e426b55 180 }
justenmg 2:89234085faae 181
justenmg 2:89234085faae 182 //copy from buffer to d_out
justenmg 2:89234085faae 183 buffer_data_location = buffer_head;
justenmg 3:51e15bd15778 184 for(int i=0; i < sig_length; i++)
justenmg 2:89234085faae 185 {
justenmg 2:89234085faae 186 d_out[i] = *buffer_data_location;
justenmg 2:89234085faae 187 *buffer_data_location = 0;
justenmg 2:89234085faae 188 //increment, looping back to beginning of buffer
justenmg 2:89234085faae 189 if(buffer_data_location + i == (buffer_begin + buf_length - 1))
justenmg 2:89234085faae 190 {
justenmg 2:89234085faae 191 buffer_data_location = buffer_begin;
justenmg 2:89234085faae 192 }
justenmg 2:89234085faae 193 else
justenmg 2:89234085faae 194 {
justenmg 2:89234085faae 195 buffer_data_location++;
justenmg 2:89234085faae 196 }
justenmg 2:89234085faae 197 }
justenmg 2:89234085faae 198 return;
justenmg 1:103e3e426b55 199 }
justenmg 1:103e3e426b55 200
justenmg 2:89234085faae 201 void filter_init()
justenmg 1:103e3e426b55 202 {
justenmg 2:89234085faae 203 for(int i=0; i < BUFFER_LENGTH; i++)
justenmg 1:103e3e426b55 204 {
justenmg 2:89234085faae 205 l_buf[i] = 0;
justenmg 2:89234085faae 206 r_buf[i] = 0;
justenmg 1:103e3e426b55 207 }
justenmg 3:51e15bd15778 208 return;
justenmg 1:103e3e426b55 209 }