lab 1 code

Dependencies:   CMSIS-DSP_for_STM32F746G BSP_DISCO_F746NG

signal_processing.cpp

Committer:
bmazzeo
Date:
2020-01-01
Revision:
25:5412779376a7
Parent:
24:9ac1187f9012
Child:
27:0d65cfcc9001

File content as of revision 25:5412779376a7:

/**
  ******************************************************************************
  * @file    signal_processing.c
  * @author  Brian Mazzeo
  * @date    2020
  * @brief   This file provides a set of code for signal processing in 487.
  *          Parts are taken from example code from STMIcroelectronics
  ******************************************************************************
  * @attention
  *          This code was specifically developed for BYU ECEn 487 course 
  *          Introduction to Digital Signal Processing.
  *
  *
  ******************************************************************************
  */ 

#include "mbed.h"
#include "stm32746g_discovery_lcd.h"
#include "arm_math.h"


/* ---------------------------------------------------------------------- 
** Macro Defines  
** ------------------------------------------------------------------- */ 

#define AUDIO_BLOCK_SAMPLES             ((uint32_t)128)         // Number of samples (L and R) in audio block (each samples is 16 bits)
#define TEST_LENGTH_SAMPLES 320 
#define SNR_THRESHOLD_F32       140.0f 
#define BLOCK_SIZE                      32 
#define NUM_TAPS                        29 

/* ---------------------------------------------------------------------- 
** FIR Coefficients buffer generated using fir1() MATLAB function. 
** fir1(28, 6/24)
** ------------------------------------------------------------------- */ 

/* 
const float32_t firCoeffs32[NUM_TAPS] = { 
-0.0018225230f, -0.0015879294f, +0.0000000000f, +0.0036977508f, +0.0080754303f, +0.0085302217f, -0.0000000000f, -0.0173976984f, 
-0.0341458607f, -0.0333591565f, +0.0000000000f, +0.0676308395f, +0.1522061835f, +0.2229246956f, +0.2504960933f, +0.2229246956f, 
+0.1522061835f, +0.0676308395f, +0.0000000000f, -0.0333591565f, -0.0341458607f, -0.0173976984f, -0.0000000000f, +0.0085302217f, 
+0.0080754303f, +0.0036977508f, +0.0000000000f, -0.0015879294f, -0.0018225230f 
}; 
*/

/* ---------------------------------------------------------------------- 
** FIR Coefficients buffer generated using fdatool MATLAB function. 
** lowpass filter to passband 1 kHz, stopband 2 kHz
** ------------------------------------------------------------------- */ 

const float32_t firCoeffs32[29] = {
     0.0106548937,  0.00951616466, 0.008600869216, 0.002842310816,-0.007469337899,
   -0.01967334561, -0.02897886001,  -0.0296635218, -0.01692939363,  0.01112444047,
    0.05218170211,  0.09966956824,   0.1441507041,   0.1758090258,   0.1872722059,
     0.1758090258,   0.1441507041,  0.09966956824,  0.05218170211,  0.01112444047,
   -0.01692939363,  -0.0296635218, -0.02897886001, -0.01967334561,-0.007469337899,
   0.002842310816, 0.008600869216,  0.00951616466,   0.0106548937
};


/* ------------------------------------------------------------------- 
 * Declare State buffer of size (numTaps + blockSize - 1) 
 * ------------------------------------------------------------------- */ 

static float32_t firStateF32[BLOCK_SIZE + NUM_TAPS - 1]; 

/* ------------------------------------------------------------------- 
 * Declare Test output buffer 
 * ------------------------------------------------------------------- */ 

static float32_t testOutput[BLOCK_SIZE]; 
static float32_t output_array_F32[AUDIO_BLOCK_SAMPLES];

/* ------------------------------------------------------------------ 
 * Global variables for FIR LPF Example 
 * ------------------------------------------------------------------- */ 

uint32_t blockSize = BLOCK_SIZE; 
//uint32_t numBlocks = TEST_LENGTH_SAMPLES/BLOCK_SIZE; 
uint32_t numBlocks = AUDIO_BLOCK_SAMPLES/BLOCK_SIZE; 

/* Important to have the structure outside of the execution so it can be initialized */
arm_fir_instance_f32 S; 

/**
  * @brief  Initialize filter structures to be used in loops later
  * @retval None
  */
void initalize_signal_processing(void) {

  /* Call FIR init function to initialize the instance structure. */
  arm_fir_init_f32(&S, NUM_TAPS, (float32_t *)&firCoeffs32[0], &firStateF32[0], blockSize); 

}

/**
  * @brief  Process audio channel signals
  * @param  L_channel: Pointer to Left channel data (float)
  * @param  R_channel: Pointer to Right channel data (float)
  * @param  Signal_Length: length of data to process
  * @retval None
  */
void process_audio_channel_signals(float* L_channel, float* R_channel, uint16_t Signal_Length)
{
    char buf[40];
    
    BSP_LCD_SetTextColor(LCD_COLOR_CYAN);
    sprintf(buf, "Processing Signals" );
    BSP_LCD_DisplayStringAt(0, 150, (uint8_t *) buf, LEFT_MODE);
    
   
  uint32_t i; 
  arm_status status; 
  float32_t  *inputF32, *outputF32; 
 
  /* Initialize input and output buffer pointers */ 
  //inputF32 = &testInput_f32_1kHz_15kHz[0];       
  //outputF32 = &testOutput[0]; 
  inputF32 = L_channel;
  outputF32 = &output_array_F32[0];

 
  /* ---------------------------------------------------------------------- 
  ** Call the FIR process function for every blockSize samples  
  ** ------------------------------------------------------------------- */ 

  for(i=0; i < numBlocks; i++)  
    {    
      arm_fir_f32(&S, inputF32 + (i * blockSize), outputF32 + (i * blockSize), blockSize);  
    } 

    /* The following just passes things from the input to the output */
    //uint16_t i;
    float32_t* L_chan_mem_address;
    float32_t* R_chan_mem_address;
    float32_t L_audio_value;
    float32_t R_audio_value;
        
    L_chan_mem_address = L_channel;
    R_chan_mem_address = R_channel;
    
    for (i=0; i<Signal_Length; i++)
   {
        //L_audio_value = *L_chan_mem_address;
        L_audio_value = output_array_F32[i];
        *L_chan_mem_address = L_audio_value;
        L_chan_mem_address++;
        
        R_audio_value = *R_chan_mem_address;
        *R_chan_mem_address = R_audio_value;
        R_chan_mem_address++;
   }

}