AnsweredAssumed Answered

Filtre arm-biquad_didn't work

Question asked by nawel.frikha on Apr 15, 2016

Je veux intégrer le filtre arm biquad casquade dans la demo loopback de st mais je n’arrive pas . voici  le code :

/**
******************************************************************************
* @file    BSP/Src/audio_loopback.c
* @author  MCD Application Team
* @version V1.0.2
* @date    18-November-2015
* @brief   This example code shows how to use the audio feature in the
*          stm32746g_discovery driver
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*   1. Redistributions of source code must retain the above copyright notice,
*      this list of conditions and the following disclaimer.
*   2. Redistributions in binary form must reproduce the above copyright notice,
*      this list of conditions and the following disclaimer in the documentation
*      and/or other materials provided with the distribution.
*   3. Neither the name of STMicroelectronics nor the names of its contributors
*      may be used to endorse or promote products derived from this software
*      without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/

/* Includes ——————————————————————*/
#include “main.h”
#include <stdio.h>
#include “string.h”

#define ARM_MATH_CM7
#include “core_cm7.h”
#include “arm_math.h”
#include “stm32f7xx.h”

#include “stdbool.h”

/** @addtogroup STM32F7xx_HAL_Examples
* @{
*/

/** @addtogroup BSP
* @{
*/

/* Private typedef ———————————————————–*/
typedef enum
{
BUFFER_OFFSET_NONE = 0,
BUFFER_OFFSET_HALF = 1,
BUFFER_OFFSET_FULL = 2,
}BUFFER_StateTypeDef;

#define AUDIO_BLOCK_SIZE   ((uint32_t)512)
#define BLOCKSIZE            ((uint32_t)128)
#define AUDIO_BUFFER_IN    AUDIO_REC_START_ADDR     /* In SDRAM */  //0x0800=2048
#define AUDIO_BUFFER_FILTRE   (AUDIO_REC_START_ADDR + (AUDIO_BLOCK_SIZE * 2)) /* In SDRAM */
#define AUDIO_BUFFER_OUT   (AUDIO_REC_START_ADDR + (AUDIO_BLOCK_SIZE * 4)) /* In SDRAM */
#define AUDIO_BUFFER_IN1    (AUDIO_REC_START_ADDR + (AUDIO_BLOCK_SIZE * 6))     /* In SDRAM */  //0x0800=2048
#define AUDIO_BUFFER_FILTRE1   (AUDIO_REC_START_ADDR + (AUDIO_BLOCK_SIZE * 8)) /* In SDRAM */

/* Private define ————————————————————*/
/* Private macro ————————————————————-*/
/* Private variables ———————————————————*/
extern uint32_t  audio_rec_buffer_state;

/* Private function prototypes ———————————————–*/
static void AudioLoopback_SetHint(void);
/* Private functions ———————————————————*/

#define numStages    13

//arm_biquad_cascade_df2T_instance_f32 filtreprec;
float32_t  pState[26];
float32_t  pCoeffs[65]={6.47838194538537,-5.63833463639715,0,-0.697402314363736,0,
1,-0.0334812348452013,-0.895962777668104,-0.0338083801197804,-0.896266347094799,
1,-1.59962148024071,0.695262684259369,-1.37413982115687,0.543870194931503,
1,-1.20422320134904,0.630533020513583,-1.1429432143631,0.586066508474033,
1,-1.07986712377598,0.729560256531613 ,-1.02677035697368,0.702540386918896,
1,-1.58794295912004,0.776219823791875,-1.55113669346191,0.743501114906827,
1,-0.921004723865756,0.762537481494472,-0.912100983564361,0.767008612923214,
1,-1.51850631095467,0.836089114425888,-1.51833646579472,0.835031151490734,
1,-0.847512904105293,0.840794074256209,-0.847412122419245,0.843572002136111,
1,-1.84248957262001,0.864944791885131,-1.83138216512215,0.85357067407514,
1,-1.24162379879317,0.865238218278357,-1.24264949505176,0.86575255109575,
1,-0.802309608367053,0.905107118813219,-0.802582204542893,0.905433385638613,
1,-0.775924227958755,0.984244432624096,-0.775924823409485,0.984247415819846};

uint32_t numBlocks = AUDIO_BLOCK_SIZE/BLOCKSIZE;
uint32_t i;
bool firstStart = false;
/**
* @brief  Audio Play demo
* @param  None
* @retval None
*/
void AudioLoopback_demo (void)
{
AudioLoopback_SetHint();

/* Initialize Audio Recorder */
if (BSP_AUDIO_IN_OUT_Init(INPUT_DEVICE_DIGITAL_MICROPHONE_2, OUTPUT_DEVICE_HEADPHONE, 90, DEFAULT_AUDIO_IN_FREQ) == AUDIO_OK)
{
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() – 95, (uint8_t *)”  AUDIO RECORD INIT OK  “, CENTER_MODE);
}
else
{
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() – 95, (uint8_t *)”  AUDIO RECORD INIT FAIL”, CENTER_MODE);
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() – 80, (uint8_t *)” Try to reset board “, CENTER_MODE);
}

/* Display the state on the screen */
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() – 80, (uint8_t *)”Microphones sound streamed to headphones”, CENTER_MODE);

/* Initialize SDRAM buffers */
memset((int16_t*)AUDIO_BUFFER_IN, 0, AUDIO_BLOCK_SIZE*2);
memset((int16_t*)AUDIO_BUFFER_FILTRE, 0, AUDIO_BLOCK_SIZE*2);
memset((float32_t*)AUDIO_BUFFER_IN1, 0, AUDIO_BLOCK_SIZE*2);
memset((float32_t*)AUDIO_BUFFER_FILTRE1, 0, AUDIO_BLOCK_SIZE*2);
memset((int16_t*)AUDIO_BUFFER_OUT, 0, AUDIO_BLOCK_SIZE*2);
audio_rec_buffer_state = BUFFER_OFFSET_NONE;

/* Initialize filtre */
//
if (firstStart == false)
{   arm_biquad_cascade_df2T_init_f32(&filtreprec, numStages,pCoeffs,pState);
firstStart = true;
}
//
arm_biquad_cascade_df2T_instance_f32 filtreprec = {numStages, pState, pCoeffs};

/* Start Recording */
BSP_AUDIO_IN_Record((int16_t*)AUDIO_BUFFER_IN, AUDIO_BLOCK_SIZE);

/*  filtrer le signal de parole */
for(i=0; i < numBlocks; i++)
{
arm_q15_to_float((int16_t*)AUDIO_BUFFER_IN+(i*BLOCKSIZE),(float32_t*)AUDIO_BUFFER_IN1, AUDIO_BLOCK_SIZE*2);
arm_biquad_cascade_df2T_f32(&filtreprec,(float32_t*)AUDIO_BUFFER_IN1,(float32_t*)AUDIO_BUFFER_FILTRE1, AUDIO_BLOCK_SIZE);
arm_float_to_q15((float32_t*)AUDIO_BUFFER_FILTRE1,(int16_t*)AUDIO_BUFFER_FILTRE+(i*BLOCKSIZE),AUDIO_BLOCK_SIZE*2);

}

/* Start Playback */
BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02);
BSP_AUDIO_OUT_Play((int16_t*)AUDIO_BUFFER_OUT, AUDIO_BLOCK_SIZE * 2);

while (1)
{
/* Wait end of half block recording */
while(audio_rec_buffer_state != BUFFER_OFFSET_HALF)
{
if (CheckForUserInput() > 0)
{
/* Stop Player before close Test */
BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW);
/* Stop Player before close Test */
BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW);
return;
}
}
audio_rec_buffer_state = BUFFER_OFFSET_NONE;

/* Copy recorded 1st half block */
memcpy((int16_t *)(AUDIO_BUFFER_OUT),
(int16_t *)(AUDIO_BUFFER_FILTRE),
AUDIO_BLOCK_SIZE);

/* Wait end of one block recording */
while(audio_rec_buffer_state != BUFFER_OFFSET_FULL)
{
if (CheckForUserInput() > 0)
{
/* Stop Player before close Test */
BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW);
/* Stop Player before close Test */
BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW);
return;
}
}
audio_rec_buffer_state = BUFFER_OFFSET_NONE;

/* Copy recorded 2nd half block */
memcpy((int16_t *)(AUDIO_BUFFER_OUT + (AUDIO_BLOCK_SIZE)),
(int16_t *)(AUDIO_BUFFER_FILTRE + (AUDIO_BLOCK_SIZE)),
AUDIO_BLOCK_SIZE);

if (CheckForUserInput() > 0)
{
/* Stop recorder */
BSP_AUDIO_IN_Stop(CODEC_PDWN_SW);
/* Stop Player before close Test */
BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW);
return;
}
}
}

/**
* @brief  Display Audio Record demo hint
* @param  None
* @retval None
*/
static void AudioLoopback_SetHint(void)
{
/* Clear the LCD */
BSP_LCD_Clear(LCD_COLOR_WHITE);

/* Set Audio Demo description */
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), 90);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
BSP_LCD_SetFont(&Font24);
BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)”AUDIO LOOPBACK EXAMPLE”, CENTER_MODE);
BSP_LCD_SetFont(&Font12);
BSP_LCD_DisplayStringAt(0, 30, (uint8_t *)”Press User button for next menu”, CENTER_MODE);

/* Set the LCD Text Color */
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_DrawRect(10, 100, BSP_LCD_GetXSize() – 20, BSP_LCD_GetYSize() – 110);
BSP_LCD_DrawRect(11, 101, BSP_LCD_GetXSize() – 22, BSP_LCD_GetYSize() – 112);

}

/**
* @}
*/

/**
* @}
*/

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Outcomes