initial

Dependencies:   mbed BSP_DISCO_F746NG mbed-dsp

Revision:
2:89234085faae
Parent:
1:103e3e426b55
Child:
3:51e15bd15778
--- a/signal_processing.cpp	Wed Feb 12 01:03:05 2020 +0000
+++ b/signal_processing.cpp	Thu Feb 20 18:33:14 2020 +0000
@@ -39,7 +39,9 @@
 float32_t r_buf[BUFFER_LENGTH];
 
 float32_t* l_buf_head = &l_buf;
+uint16_t l_buf_head_idx = 0;
 float32_t* r_buf_head = &r_buf;
+uint16_t r_buf_head_idx = 0;
 
 arm_fir_instance_f32 filter_left;
 arm_fir_instance_f32 filter_right;
@@ -68,6 +70,7 @@
     break;
   
   case 3: // FFT Overlap-add 
+  filter_init();
     break;
     
   case 4: // FFT Overlap-add with real-imag efficiency
@@ -97,21 +100,23 @@
   switch(Lab_Execution_Type)
   {
   case 0: // Passthrough case
-    arm_copy_f32(L_channel_in, L_channel_out, AUDIO_BLOCK_SAMPLES);
-    arm_copy_f32(R_channel_in, R_channel_out, AUDIO_BLOCK_SAMPLES);
+    arm_copy_f32(L_channel_in, L_channel_out, Signal_Length);
+    arm_copy_f32(R_channel_in, R_channel_out, Signal_Length);
     break;
   
   case 1: // FIR case (ARM)
-  arm_fir_f32(&filter_left, L_channel_in, L_channel_out, AUDIO_BLOCK_SAMPLES);
-  arm_fir_f32(&filter_right, R_channel_in, R_channel_out, AUDIO_BLOCK_SAMPLES);
+  arm_fir_f32(&filter_left, L_channel_in, L_channel_out, Signal_Length);
+  arm_fir_f32(&filter_right, R_channel_in, R_channel_out, Signal_Length);
     break;
   
   case 2: // FIR case (student)
-  arm_fir_f32(&filter_left, L_channel_in, L_channel_out, AUDIO_BLOCK_SAMPLES);
-  arm_fir_f32(&filter_right, R_channel_in, R_channel_out, AUDIO_BLOCK_SAMPLES);
+  arm_fir_f32(&filter_left, L_channel_in, L_channel_out, Signal_Length);
+  arm_fir_f32(&filter_right, R_channel_in, R_channel_out, Signal_Length);
     break;
   
   case 3: // FFT Overlap-add 
+  filter(l_buf, l_buf_head, l_buf_head_idx, L_channel_in, L_channel_out, Signal_Length, BUFFER_LENGTH);
+  filter(r_buf, r_buf_head, r_buf_head_idx, R_channel_in, R_channel_out, Signal_Length, BUFFER_LENGTH);
     break;
     
   case 4: // FFT Overlap-add with real-imag efficiency
@@ -123,26 +128,79 @@
     BSP_LCD_SetFont(&Font16);
 }
 
-
-void filter(float32_t* buffer, float32_t* d_in, float32_t* d_out, uint16_t buf_length)
+//buffer: pointer to the storage buffer for the filter output
+//buf_length: the length of the storage buffer (len_filter + len_batch - 1)
+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)
 {
+    float32_t* data_sample = d_in+sig_length-1;
+    float32_t* filter_sample = filter;
     float32_t result = 0;
-    float32_t* data_sample = d_out;
-    for(i=0; i<buf_length; i++)
+    uint16_t conv_length = 0;
+    float32_t* buffer_data_location = buffer_head;
+    
+    //convolve and save to buffer
+    for(uint16_t shift = 0; shift < buf_length; shift++)
     {
-        *data_sample = convolve(d_in, win_filter_coeffs, AUDIO_BLOCK_SAMPLES, BUFFER_LENGTH);
-        data_sample++; //*******************************************************
+        //shift
+        if(shift < sig_lenth)
+        {
+            conv_length = shift + 1;
+        }
+        else if(shift >= sig_lenth && shift < filt_length)
+        {
+            conv_length = sig_lenth;
+        }
+        else if(shift >= filt_length)
+        {
+            conv_length = sig_lenth - (shift - filt_length + 1)
+        }
+        
+        result = 0;
+        //multiply-add
+        for(i=0; i<conv_length; i++)
+        {
+            result += (*filter_sample) * (*data_sample);
+            filter_sample++;
+            data_sample--;
+        }
+        
+        // save to the buffer
+        *buffer_data_location += result;
+        //increment, looping back to beginning of buffer
+        if(buffer_data_location == (buffer_begin + buf_length - 1))
+        {
+            buffer_data_location = buffer_begin;
+        }
+        else
+        {
+            buffer_data_location++;
+        }
     }
+    
+    //copy from buffer to d_out
+    buffer_data_location = buffer_head;
+    for(int i=0; i < sig_lenth; i++)
+    {
+        d_out[i] = *buffer_data_location;
+        *buffer_data_location = 0;
+        //increment, looping back to beginning of buffer
+        if(buffer_data_location + i == (buffer_begin + buf_length - 1))
+        {
+            buffer_data_location = buffer_begin;
+        }
+        else
+        {
+            buffer_data_location++;
+        }
+    }
+    return;
 }
 
-float32_t convolve(float32_t* data, float32_t* filter, uint16_t sig_length, uint16_t buf_length)
+void filter_init()
 {
-    float32_t* data_sample = data+buf_length-1;
-    float32_t* filter_sample = filter;
-    float32_t result = 0;
-    for(i=0; i<buf_length; i++)
+    for(int i=0; i < BUFFER_LENGTH; i++)
     {
-        result += (*filter_sample) * (*data_sample);
+        l_buf[i] = 0;
+        r_buf[i] = 0;
     }
-    return result;
 }