initial

Dependencies:   mbed BSP_DISCO_F746NG mbed-dsp

Committer:
justenmg
Date:
Thu Feb 20 18:33:14 2020 +0000
Revision:
2:89234085faae
Parent:
1:103e3e426b55
Child:
3:51e15bd15778
Feb 20

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