当前位置:   article > 正文

STM32-MEMS麦克风采集、高级音频处理和音频输出_stm32的音频采集和播放

stm32的音频采集和播放

 FP-AUD-SMARTMIC1简介

        FP-AUD-SMARTMIC1是一个STM32Cube功能包。该软件包实现了一个完整的应用程序,目标是MEMS麦克风阵列的高级处理,包括数字MEMS麦克风采集、波束成形、源定位和回声消除。处理后的音频被发送到USB主机和连接到相关扩展板的扬声器。该功能包基于STM32Cube软件技术,可轻松在不同的STM32微控制器之间进行移植。

        此示例实现支持两种系统:STM32 NUCLEO-F446RE开发板,配备X-NUCLEO-CA01M1或X-NUCLEO-CA02M2扩展板和STEVAL-MIC001V1、STEVAL-MIC002V1、STEVAL-MIC003V1或STEVAL-MIC 005V1数字麦克风评估板或BlueCoin

        该软件也可以在GitHub上获得,用户可以通过“问题”和“拉取请求”选项卡发出错误信号并提出新想法。

所有功能

STM32Cube的软件扩展:

AcousticBF实时波束形成

AcousticEC实时声学回声消除

AcousticSL实时声源定位

完整的应用程序,包括单个示例应用程序中的所有声学功能软件图形用户界面,可从主机PC轻松控制参数和算法当连接到X-NUCLEO-CA01M1和X-NUCLEO-CA02M2扩展板时,NUCLEO-F446RE开发板上可用的示例实现

官网网站:FP-AUD-SMARTMIC1 - 专为MEMS麦克风采集、高级音频处理和音频输出打造的STM32Cube功能包 - 意法半导体STMicroelectronicsicon-default.png?t=N7T8https://www.st.com/zh/embedded-software/fp-aud-smartmic1.htmlGithub:

GitHub - STMicroelectronics/fp-aud-smartmic1: FP-AUD-SMARTMIC1 provides a firmware running on STM32 which acquires audio signals of four digital MEMS microphones, elaborates them by means of embedded DSP libraries and streams the processed audio to both an USB host and a loudspeaker connected to the relevant expansion board.icon-default.png?t=N7T8https://github.com/STMicroelectronics/fp-aud-smartmic1


AcousticSL实时声源定位

本文针对该功能包中的声源定位算法进行探索研究,相关算法文件的可以从Github中下载

文件夹路径fp-aud-smartmic1-main\Middlewares\ST\STM32_AcousticSL_Library

重点文件内容

acoustic_sl.h
  1. /*
  2. 此文件包含声学声源定位库定义。
  3. */
  4. #ifndef __ACOUSTIC_SL_H
  5. #define __ACOUSTIC_SL_H
  6. #include "stdint.h"
  7. /*算法类型定义*/
  8. #define ACOUSTIC_SL_ALGORITHM_XCORR ((uint32_t)0x00000001)
  9. #define ACOUSTIC_SL_ALGORITHM_GCCP ((uint32_t)0x00000002)
  10. #define ACOUSTIC_SL_ALGORITHM_BMPH ((uint32_t)0x00000004)
  11. /*错误类型*/
  12. #define ACOUSTIC_SL_ALGORITHM_ERROR ((uint32_t)0x00000001)
  13. #define ACOUSTIC_SL_PTR_CHANNELS_ERROR ((uint32_t)0x00000002)
  14. #define ACOUSTIC_SL_CHANNEL_NUMBER_ERROR ((uint32_t)0x00000004)
  15. #define ACOUSTIC_SL_SAMPLING_FREQ_ERROR ((uint32_t)0x00000008)
  16. #define ACOUSTIC_SL_RESOLUTION_ERROR ((uint32_t)0x00000010)
  17. #define ACOUSTIC_SL_THRESHOLD_ERROR ((uint32_t)0x00000020)
  18. #define ACOUSTIC_SL_DISTANCE_ERROR ((uint32_t)0x00000040)
  19. #define ACOUSTIC_SL_NUM_OF_SAMPLES_ERROR ((uint32_t)0x00000080)
  20. #define ACOUSTIC_SL_PROCESSING_ERROR ((uint32_t)0x00000100)
  21. #ifndef ACOUSTIC_LOCK_ERROR
  22. #define ACOUSTIC_LOCK_ERROR ((uint32_t)0x10000000)
  23. #endif
  24. /**/
  25. #define ACOUSTIC_SL_NO_AUDIO_DETECTED -100
  26. /*输出参数结构体*/
  27. typedef struct
  28. {
  29. /*算法类型选择,即上述定义的XCORR,GCCP和BMPH*/
  30. uint32_t algorithm;
  31. /*设置采样频率*/
  32. uint32_t sampling_frequency;
  33. /*设置通道数,2通道进行180°定位,4通道进行360°定位*/
  34. uint32_t channel_number;
  35. /*麦克风通道选择*/
  36. uint8_t ptr_M1_channels;
  37. uint8_t ptr_M2_channels;
  38. uint8_t ptr_M3_channels;
  39. uint8_t ptr_M4_channels;
  40. /*麦克风1,2之间的距离(mm),默认150*/
  41. uint16_t M12_distance;
  42. /*麦克风3,4之间的距离(mm),默认150*/
  43. uint16_t M34_distance;
  44. /*内部存储空间大小设置,采用AcousticSL_getMemorySize()函数*/
  45. uint32_t internal_memory_size;
  46. /*指向用户分配的内存*/
  47. uint32_t * pInternalMemory;
  48. /*指向单次处理的样本数*/
  49. int16_t samples_to_process;
  50. } AcousticSL_Handler_t;
  51. typedef struct
  52. {
  53. /*指定语音响度阈值,低于阈值算法不起作用,阈值范围从0到1000,默认值为24*/
  54. uint16_t threshold;
  55. /*算法的角度分辨率,默认值为4.如果使用XCORR,则忽略*/
  56. uint32_t resolution;
  57. } AcousticSL_Config_t;
  58. /*内存分配函数*/
  59. uint32_t AcousticSL_getMemorySize(AcousticSL_Handler_t * pHandler);
  60. /*初始化函数*/
  61. uint32_t AcousticSL_Init(AcousticSL_Handler_t * pHandler);
  62. /*数据输入函数*/
  63. uint32_t AcousticSL_Data_Input(void *pM1, void *pM2, void *pM3, void *pM4, AcousticSL_Handler_t * pHandler);
  64. /*音频处理函数*/
  65. uint32_t AcousticSL_Process(int32_t * Estimated_Angle, AcousticSL_Handler_t * pHandler);
  66. /*设置阈值与分辨率的值*/
  67. uint32_t AcousticSL_setConfig(AcousticSL_Handler_t * pHandler, AcousticSL_Config_t * pConfig);
  68. /*获取参数*/
  69. uint32_t AcousticSL_getConfig(AcousticSL_Handler_t * pHandler, AcousticSL_Config_t * pConfig);
  70. /*获取版本信息*/
  71. uint32_t AcousticSL_GetLibVersion(char *version);
  72. #endif

该功能可以选择三种不同的声源定位算法,分别是XCORR(时域互相关),GCCP(频域变换广义互相关),BMPH(The block-matching pursuit with hangover

XCORR算法应用在时域,需要的资源较少,分辨率低,对麦克风放置距离有要求

GCCP算法应用在频域,需要的资源脚多,分辨率高,对麦克风放置距离无要求

BMPH算法应用在频域,需要的资源适中,对麦克风放置距离无要求,需要执行一个遗传的步骤,增强稳定性

双通道麦克风放置示意图

四通道麦克风放置示意图

对于XCORR算法分辨率取决于采样频率与麦克风之间的距离,如下式所示(floor下取整)

以双通道放置为例,麦克风间距10cm,采样频率100kHz,声速343.1m/s,可以计算得到角度分辨率为3°。如需要更高精度,则需要提高麦克风间距与采样频率。

AcousticSL.c
  1. #include "libSoundSourceLoc.c"
  2. /*初始化函数*/
  3. uint32_t AcousticSL_Init(AcousticSL_Handler_t * pHandler)
  4. {
  5. return libSoundSourceLoc_Init(pHandler);
  6. }
  7. /*数据输入函数*/
  8. uint32_t AcousticSL_Data_Input(void *pM1, void *pM2, void *pM3, void *pM4, AcousticSL_Handler_t * pHandler)
  9. {
  10. return libSoundSourceLoc_Data_Input((int16_t *)pM1, (int16_t *)pM2, (int16_t *)pM3, (int16_t *)pM4, pHandler);
  11. }
  12. /*音频分析函数*/
  13. uint32_t AcousticSL_Process(int32_t * Estimated_Angle, AcousticSL_Handler_t * pHandler)
  14. {
  15. return libSoundSourceLoc_Process(Estimated_Angle, pHandler);
  16. }
  17. /*参数设置函数*/
  18. uint32_t AcousticSL_setConfig(AcousticSL_Handler_t * pHandler, AcousticSL_Config_t * pConfig)
  19. {
  20. return libSoundSourceLoc_setConfig(pHandler, pConfig);
  21. }
  22. /*获取参数函数*/
  23. uint32_t AcousticSL_getConfig(AcousticSL_Handler_t * pHandler, AcousticSL_Config_t * pConfig)
  24. {
  25. return libSoundSourceLoc_getConfig(pHandler, pConfig);
  26. }
  27. /*内存配置函数*/
  28. uint32_t AcousticSL_getMemorySize(AcousticSL_Handler_t * pHandler)
  29. {
  30. return libSoundSourceLoc_getMemorySize(pHandler);
  31. }
  32. /*获取版本信息*/
  33. uint32_t AcousticSL_GetLibVersion(char *version)
  34. {
  35. return libSoundSourceLoc_GetLibVersion(version);
  36. }

可以看到在AcousticSL.c文件中的所有函数都是调用libSoundSourceLoc.c中的函数,之后我们仔细阅读该函数中的库

libSoundSourceLoc.c
  1. #ifndef __LIB_SOUNDSOURCELOC_H
  2. #define __LIB_SOUNDSOURCELOC_H
  3. #include "acoustic_sl.h"
  4. #include "arm_math.h"
  5. #include "math.h"
  6. struct _libSoundSourceLoc_Handler_Internal;
  7. typedef struct {
  8. float32_t (*SourceLocFunction)(struct _libSoundSourceLoc_Handler_Internal *SLocInternal, int32_t * out_angles);
  9. uint32_t (*CheckEventFunction)(struct _libSoundSourceLoc_Handler_Internal *SLocInternal);
  10. }libSoundSourceLoc_Handler_Callbacks;
  11. typedef struct _libSoundSourceLoc_Handler_Internal{
  12. /*采样频率*/
  13. uint32_t sampling_frequency;
  14. /*算法类型*/
  15. uint32_t Type;
  16. /**/
  17. uint32_t Buffer_State;
  18. /*采样数目*/
  19. uint32_t Sample_Number_To_Process;
  20. /*采样数目(每毫秒)*/
  21. uint16_t Sample_Number_Each_ms;
  22. /*输入数据数目*/
  23. uint16_t Input_Counter;
  24. /**/
  25. uint16_t EVENT_THRESHOLD;
  26. /*麦克风配置*/
  27. uint16_t Mic_Number;
  28. uint8_t ptr_M1_channels;
  29. uint8_t ptr_M2_channels;
  30. uint8_t ptr_M3_channels;
  31. uint8_t ptr_M4_channels;
  32. /*麦克风之间的距离和TAUD*/
  33. float32_t M12_distance;
  34. float32_t M34_distance;
  35. int32_t M12_TAUD;
  36. int32_t M34_TAUD;
  37. libSoundSourceLoc_Handler_Callbacks Callbacks;
  38. /*音频分析数据*/
  39. int32_t Estimated_Angle_12;
  40. int32_t Estimated_Angle_34;
  41. uint32_t resolution;
  42. void * M1_Data;
  43. void * M2_Data;
  44. void * M3_Data;
  45. void * M4_Data;
  46. arm_rfft_fast_instance_f32 * SFast;
  47. float32_t * FFT_Out;
  48. float32_t * PowerSpectrum;
  49. float32_t * Phase;
  50. float32_t * sumArray;
  51. int32_t * SourceLocFilterArray;
  52. int32_t * SourceLocFilterArray1;
  53. float32_t * window;
  54. //dz
  55. uint32_t Sample_Number_To_Store;
  56. uint8_t * remap_mic; // 4
  57. uint16_t num_of_freq;
  58. uint16_t num_of_angles;
  59. uint16_t freq_range_min;
  60. uint16_t freq_range_max;
  61. float32_t * mics_distance; // AUDIO_CHANNELS
  62. float32_t * mics_angle; // AUDIO_CHANNELS
  63. float32_t * theta; // NUM_ANGLES
  64. float32_t phi;//
  65. uint16_t mics_write_offset; //starting point in every mic \in {0,16,32,...} \subseteq [0 MIC_LEN)
  66. uint16_t mics_read_offset; //starting point in every mic \in 0,MIC_LEN
  67. int16_t * score_theta;// RESOLUTION
  68. uint8_t reactivity;
  69. uint8_t stability;
  70. uint8_t array_type;// CIRCULAR_ARRAY
  71. uint8_t local_stabilizer;
  72. float32_t * mic_tmp;//[DFT_LEN]
  73. float32_t * ptr_freq_energies;// ptr
  74. float32_t * s;//[2*AUDIO_CHANNELS*NUMOF_FREQ]
  75. int16_t * frequencies_under_analysis; // NUM OF FREQ
  76. int16_t * sources; // 2* OUTPUT SOURCES
  77. float32_t * steering_tau;
  78. } libSoundSourceLoc_Handler_Internal;
  79. #define FFT_POINTS 512
  80. #define LIB_VERSION ((uint32_t)0x00030000)
  81. #define FILTER_MIN 10
  82. #define FILTER_STEP_MAIN 10
  83. #define FILTER_MAX_MAIN 50
  84. #define FILTER_STEP_SEC 8
  85. #define FILTER_MAX_SEC 20
  86. #define FACTOR_INDEX_2_HZ (uint16_t)(SLocInternal->sampling_frequency/SLocInternal->Sample_Number_To_Process)
  87. #define MIN_RESOLUTION 4U
  88. #define MAX_NUM_OF_ANGLES (360U/MIN_RESOLUTION)
  89. #define MAX_NUM_OF_FREQUENCIES 16U
  90. #define MAX_AUDIO_CHANNELS 8U
  91. #define MAX_THRESHOLD 1000U
  92. #define NUM_OUTPUT_SOURCES 4U
  93. #define CIRCULAR_ARRAY 1U
  94. #define LINEAR_ARRAY 2U
  95. #ifndef SOUND_SPEED
  96. #define SOUND_SPEED (float32_t)343.1f
  97. #endif
  98. #ifndef UNUSED
  99. #define UNUSED(X) (void)X /* To avoid gcc/g++ warnings */
  100. #endif
  101. #include "doa_via_block_sparsity.c"
  102. #define Abs(x) ( (((float32_t)x) < 0.0f) ? (-((float32_t)x)) : ((float32_t)x))
  103. #define SaturaLH(N, L, H) (((N)<(L))?(L):(((N)>(H))?(H):(N)))
  104. #define SaturaL(N, L) (((N)<(L))?(L):(N))
  105. #define SaturaH(N, H) (((N)>(H))?(H):(N))
  106. static uint32_t CheckEvent(libSoundSourceLoc_Handler_Internal * SLocInternal);
  107. static float32_t XCORR_GetAngle(libSoundSourceLoc_Handler_Internal * SLocInternal, int32_t * out_angles);
  108. static float32_t GCC_GetAngle(libSoundSourceLoc_Handler_Internal * SLocInternal, int32_t * out_angles);
  109. static int32_t get_max_pos(libSoundSourceLoc_Handler_Internal * SLocInternal,int32_t length,int32_t step);
  110. static void FilterAngle(int32_t *SourceAngle, int32_t* LedStatus, uint16_t max_value, uint16_t A, uint16_t SatA, uint16_t B, uint16_t SatB);
  111. /*版本信息*/
  112. static uint32_t libSoundSourceLoc_GetLibVersion(char *version)
  113. {
  114. char str1[35] = "ST AcousticSL v3.0.0";
  115. (void)strcpy(version, str1);
  116. return strlen(str1);
  117. }
  118. /*数据输入*/
  119. static uint32_t libSoundSourceLoc_Data_Input(void *pM1, void *pM2, void *pM3, void *pM4, AcousticSL_Handler_t * pHandler)
  120. {
  121. libSoundSourceLoc_Handler_Internal * SLocInternal = (libSoundSourceLoc_Handler_Internal *)(pHandler->pInternalMemory);
  122. uint8_t ret = 0;
  123. uint32_t i;
  124. /*如果XCORR和BMPH算法,输入数据格式为int16;如果GCCP算法,输入数据格式为float32*/
  125. /*兼容双通道麦克风与四通道麦克风*/
  126. if((SLocInternal->Type == ACOUSTIC_SL_ALGORITHM_XCORR) || (SLocInternal->Type == ACOUSTIC_SL_ALGORITHM_BMPH) )
  127. {
  128. for (i = 0; i < SLocInternal->Sample_Number_Each_ms; i ++)
  129. {
  130. ((int16_t *)(SLocInternal->M1_Data))[SLocInternal->Input_Counter] = ((int16_t *)(pM1))[i*SLocInternal->ptr_M1_channels];
  131. ((int16_t *)(SLocInternal->M2_Data))[SLocInternal->Input_Counter] = ((int16_t *)(pM2))[i*SLocInternal->ptr_M2_channels];
  132. if (SLocInternal->Mic_Number == 4U)
  133. {
  134. ((int16_t *)(SLocInternal->M3_Data))[SLocInternal->Input_Counter] = ((int16_t *)(pM3))[i*SLocInternal->ptr_M3_channels];
  135. ((int16_t *)(SLocInternal->M4_Data))[SLocInternal->Input_Counter] = ((int16_t *)(pM4))[i*SLocInternal->ptr_M4_channels];
  136. }
  137. SLocInternal->Input_Counter ++;
  138. }
  139. }
  140. else if(SLocInternal->Type == ACOUSTIC_SL_ALGORITHM_GCCP )
  141. {
  142. for (i = 0; i < SLocInternal->Sample_Number_Each_ms; i ++)
  143. {
  144. ((float32_t *)(SLocInternal->M1_Data))[SLocInternal->Input_Counter] = (float32_t)((int16_t *)(pM1))[i*SLocInternal->ptr_M1_channels];//todo interleaving
  145. ((float32_t *)(SLocInternal->M2_Data))[SLocInternal->Input_Counter] = (float32_t)((int16_t *)(pM2))[i*SLocInternal->ptr_M2_channels];
  146. if (SLocInternal->Mic_Number == 4U)
  147. {
  148. ((float32_t *)(SLocInternal->M3_Data))[SLocInternal->Input_Counter] = (float32_t)((int16_t *)(pM3))[i*SLocInternal->ptr_M3_channels];
  149. ((float32_t *)(SLocInternal->M4_Data))[SLocInternal->Input_Counter] = (float32_t)((int16_t *)(pM4))[i*SLocInternal->ptr_M4_channels];
  150. }
  151. SLocInternal->Input_Counter ++;
  152. }
  153. }
  154. else
  155. {
  156. /* no other use cases are handled */
  157. }
  158. /*如果算法是BMPH,需要设置mics_read_offset参数,对于双通道为0,四通道为Sample_Number_To_Store*/
  159. if(SLocInternal->Type == ACOUSTIC_SL_ALGORITHM_BMPH)
  160. {
  161. if(SLocInternal->Input_Counter == SLocInternal->Sample_Number_To_Store)
  162. {
  163. ret = 1;
  164. SLocInternal->Buffer_State = 1;
  165. SLocInternal->mics_read_offset= 0;
  166. }
  167. if(SLocInternal->Input_Counter == (SLocInternal->Sample_Number_To_Store * 2U))
  168. {
  169. ret = 1;
  170. SLocInternal->Buffer_State = 2;
  171. SLocInternal->Input_Counter = 0;
  172. SLocInternal->mics_read_offset = (uint16_t)SLocInternal->Sample_Number_To_Store;
  173. }
  174. }
  175. else
  176. {
  177. if(SLocInternal->Input_Counter == SLocInternal->Sample_Number_To_Store)
  178. {
  179. ret = 1;
  180. SLocInternal->Buffer_State = 1;
  181. }
  182. if(SLocInternal->Input_Counter == (SLocInternal->Sample_Number_To_Store * 2U))
  183. {
  184. ret = 1;
  185. SLocInternal->Buffer_State = 2;
  186. SLocInternal->Input_Counter = 0;
  187. }
  188. }
  189. return ret;
  190. }
  191. /*音频处理函数*/
  192. static uint32_t libSoundSourceLoc_Process(int32_t * Estimated_Angle, AcousticSL_Handler_t * pHandler)
  193. {
  194. libSoundSourceLoc_Handler_Internal * SLocInternal = (libSoundSourceLoc_Handler_Internal *)(pHandler->pInternalMemory);
  195. /*计算寄存变量*/
  196. int32_t Estimated_temp_360[2];
  197. /*Buffer_State不为0,即完成数据输入*/
  198. if(SLocInternal->Buffer_State!=0U)
  199. {
  200. /*如果BMPH算法,或者调用Check函数返回1,则采用定位函数。否则寄存变量1变为-1*/
  201. if((SLocInternal->Type==ACOUSTIC_SL_ALGORITHM_BMPH) || (SLocInternal->Callbacks.CheckEventFunction(SLocInternal)==1))
  202. {
  203. SLocInternal->Callbacks.SourceLocFunction(SLocInternal,Estimated_temp_360);
  204. }
  205. else
  206. {
  207. Estimated_temp_360[0]=-1;
  208. }
  209. }
  210. /*如果BMPH算法,调用定位函数之后,如果寄存变量1为-500,则会重新赋值-100*/
  211. if (SLocInternal->Type == ACOUSTIC_SL_ALGORITHM_BMPH)
  212. {
  213. if (Estimated_temp_360[0]==-500)
  214. {
  215. Estimated_temp_360[0]=-100;
  216. }
  217. }
  218. /*如果不是BMPH算法,但是四通道输入*/
  219. else if(SLocInternal->Mic_Number==4U)
  220. {
  221. STEP
  222. /*角度计算前需要进行滤波处理,滤波器参数设计(360,STEPM,MAXM,STEPS,MAXS)*/
  223. FilterAngle((int32_t *)&Estimated_temp_360[0], SLocInternal->SourceLocFilterArray, 360 , FILTER_STEP_MAIN , FILTER_MAX_MAIN, FILTER_STEP_SEC ,FILTER_MAX_SEC);
  224. /*如果寄存变量1为-1,寄存变量1重新赋值-100*/
  225. if(Estimated_temp_360[0]==-1)
  226. {
  227. Estimated_temp_360[0]=-100;
  228. }
  229. }
  230. /*如果不是BMPH算法,且双通道输入*/
  231. else
  232. {
  233. /*角度计算前需要进行滤波处理,滤波器固定参数(1801050820*/
  234. FilterAngle((int32_t *)&Estimated_temp_360[0], SLocInternal->SourceLocFilterArray, 180 , 10 , 50, 8 ,20);
  235. /*如果寄存变量1为-1,寄存变量1重新赋值-100,否则寄存变量1值减90*/
  236. if(Estimated_temp_360[0]==-1)
  237. {
  238. Estimated_temp_360[0]=-100;
  239. }
  240. else
  241. {
  242. Estimated_temp_360[0]-=90;
  243. }
  244. }
  245. /*计算角度参数1为寄存变量1*/
  246. Estimated_Angle[0] = Estimated_temp_360[0];
  247. return 0;
  248. }
重点算法
XCORR_GetAngle()
  1. static float32_t XCORR_GetAngle(libSoundSourceLoc_Handler_Internal * SLocInternal, int32_t * out_angles)
  2. {
  3. /*tau时延*/
  4. int32_t tau, k;
  5. float32_t delta_t24 = 0.0f;
  6. float32_t delta_t13 = 0.0f;
  7. float32_t test = 0.0f;
  8. uint32_t correlation, MAX13, MAX24;
  9. if(SLocInternal->Mic_Number >= 2U)
  10. {
  11. MAX13=0x80000000U; //-inf
  12. /*遍历所有离散时域信号延迟*/
  13. for(tau=-SLocInternal->M12_TAUD;tau<=SLocInternal->M12_TAUD;tau++)
  14. {
  15. correlation=0;
  16. /*互相关计算*/
  17. for(k=SLocInternal->M12_TAUD; k<((int32_t)SLocInternal->Sample_Number_To_Process-SLocInternal->M12_TAUD);k++)
  18. {
  19. correlation = correlation + (((uint32_t)((int16_t *)(SLocInternal->M2_Data))[((SLocInternal->Buffer_State-1U)*SLocInternal->Sample_Number_To_Process)+(uint32_t)k] * (uint32_t)((int16_t *)(SLocInternal->M1_Data))[((SLocInternal->Buffer_State-1U)*SLocInternal->Sample_Number_To_Process)+(uint32_t)k+(uint32_t)tau])/256U);
  20. }
  21. /*寻找互相关计算的最大值与对应的时延值*/
  22. if (correlation > MAX13)
  23. {
  24. MAX13 = correlation;
  25. delta_t13=(float32_t)tau;
  26. }
  27. }
  28. }
  29. /*针对四通道,格外计算M34之间的互相关*/
  30. if(SLocInternal->Mic_Number == 4U)
  31. {
  32. MAX24=0x80000000U; //-inf
  33. for(tau=-SLocInternal->M34_TAUD;tau<=SLocInternal->M34_TAUD;tau++)
  34. {
  35. correlation=0;
  36. for(k=SLocInternal->M34_TAUD; k<((int32_t)SLocInternal->Sample_Number_To_Process-SLocInternal->M34_TAUD); k++)
  37. {
  38. correlation = correlation + (((uint32_t)((int16_t *)(SLocInternal->M4_Data))[((SLocInternal->Buffer_State-1U)*SLocInternal->Sample_Number_To_Process)+(uint32_t)k] * (uint32_t)((int16_t *)(SLocInternal->M3_Data))[((SLocInternal->Buffer_State-1U)*SLocInternal->Sample_Number_To_Process)+(uint32_t)k+(uint32_t)tau])/256U);
  39. }
  40. if (correlation > MAX24)
  41. {
  42. MAX24 = correlation;
  43. delta_t24=(float32_t)tau;
  44. }
  45. }
  46. }
  47. /*通过计算得到的互相关最大时的时延和最大时延进行角度计算*/
  48. SLocInternal->Buffer_State=0;
  49. if(SLocInternal->Mic_Number == 2U)
  50. {
  51. test = (delta_t13+(float32_t)SLocInternal->M12_TAUD )* (180.0f/((float32_t)SLocInternal->M12_TAUD*2.0f));
  52. }
  53. else if(SLocInternal->Mic_Number == 4U)
  54. {
  55. test=(float32_t)atan2(delta_t13, delta_t24);
  56. test= test * (360.0f / (3.14f*2.0f) );
  57. if(test<0.0f)
  58. {
  59. test+=360.0f; /* in [0 360] -> TODO: Optimize the whole angle computation from the maximum phase index*/
  60. }
  61. }
  62. else
  63. {
  64. /* no other use cases are handled */
  65. }
  66. out_angles[0]=(int32_t)test;
  67. return test;
  68. }

使用XCORR算法,以双通道输入为例,基于采集到的麦克风数据,通过增加不同的时延,调整两个序列时间差进行互相关的计算,当补充时延到两个麦克风采集到的信号相同时互相关计算值最大。即可通过补充的时延值判断声源的角度。

SLocInternal->M12_TAUD = (int32_t)SLocInternal->M12_distance * ((int32_t)SLocInternal->sampling_frequency/(int32_t)SOUND_SPEED);

SLocInternal->M34_TAUD =(int16_t)floor((float64_t)SLocInternal->M34_distance * ((float64_t)SLocInternal->sampling_frequency/(float64_t)SOUND_SPEED));

GCC_GetAngle()
  1. static float32_t GCC_GetAngle(libSoundSourceLoc_Handler_Internal * SLocInternal, int32_t * out_angles)
  2. {
  3. uint32_t j;
  4. float32_t fi = 0.0f;;
  5. uint32_t buffer_offset = (SLocInternal->Buffer_State-1U)*SLocInternal->Sample_Number_To_Process;
  6. float32_t * M1_Data = (float32_t *)SLocInternal->M1_Data;
  7. float32_t * M2_Data = (float32_t *)SLocInternal->M2_Data;
  8. float32_t * M3_Data = (float32_t *)SLocInternal->M3_Data;
  9. float32_t * M4_Data = (float32_t *)SLocInternal->M4_Data;
  10. float32_t * Power_Spectrum = (float32_t *)SLocInternal->PowerSpectrum;
  11. float32_t * FFT_Out = (float32_t *)SLocInternal->FFT_Out;
  12. float32_t * hanning = (float32_t *)SLocInternal->window;
  13. float32_t tempMag = 1e-7f;
  14. /*WINDOWING*/
  15. /*处理用的窗函数*/
  16. for(j=0;j<SLocInternal->Sample_Number_To_Process;j++)
  17. {
  18. if(SLocInternal->Mic_Number >= 2U)
  19. {
  20. M1_Data[buffer_offset + j] *= hanning[j];
  21. M2_Data[buffer_offset + j] *= hanning[j];
  22. }
  23. if(SLocInternal->Mic_Number == 4U)
  24. {
  25. M3_Data[buffer_offset + j] *= hanning[j];
  26. M4_Data[buffer_offset + j] *= hanning[j];
  27. }
  28. }
  29. /*FFTs*/
  30. /*进行傅里叶变换,得到频域数据*/
  31. if(SLocInternal->Mic_Number >= 2U)
  32. {
  33. /*M1 FFT*/
  34. arm_rfft_fast_f32(SLocInternal->SFast,&M1_Data[buffer_offset],(float32_t *)FFT_Out,0);
  35. for (j=0;j<SLocInternal->Sample_Number_To_Process;j++)
  36. {
  37. M1_Data[buffer_offset + j]=((float32_t *)FFT_Out)[j];
  38. }
  39. /*M2 FFT*/
  40. arm_rfft_fast_f32(SLocInternal->SFast,&M2_Data[buffer_offset],(float32_t *)FFT_Out,0);
  41. for (j=0;j<SLocInternal->Sample_Number_To_Process;j++)
  42. {
  43. M2_Data[buffer_offset + j]=((float32_t *)FFT_Out)[j];
  44. }
  45. }
  46. if(SLocInternal->Mic_Number == 4U)
  47. {
  48. /*M3 FFT*/
  49. arm_rfft_fast_f32(SLocInternal->SFast,&M3_Data[buffer_offset],(float32_t *)FFT_Out,0);
  50. for (j=0;j<SLocInternal->Sample_Number_To_Process;j++)
  51. {
  52. M3_Data[buffer_offset + j]=((float32_t *)FFT_Out)[j];
  53. }
  54. /*M4 FFT*/
  55. arm_rfft_fast_f32(SLocInternal->SFast,&M4_Data[buffer_offset],(float32_t *)FFT_Out,0);
  56. for (j=0;j<SLocInternal->Sample_Number_To_Process;j++)
  57. {
  58. M4_Data[buffer_offset + j]=((float32_t *)FFT_Out)[j];
  59. }
  60. }
  61. /*FILTERING*/
  62. if(SLocInternal->Mic_Number >= 2U)
  63. {
  64. M1_Data[buffer_offset]=0.0f;
  65. M2_Data[buffer_offset]=0.0f;
  66. M1_Data[buffer_offset+1U]=0.0f;
  67. M2_Data[buffer_offset+1U]=0.0f;
  68. }
  69. if(SLocInternal->Mic_Number == 4U)
  70. {
  71. M3_Data[buffer_offset]=0.0f;
  72. M4_Data[buffer_offset]=0.0f;
  73. M3_Data[buffer_offset+1U]=0.0f;
  74. M4_Data[buffer_offset+1U]=0.0f;
  75. }
  76. if(SLocInternal->Mic_Number >= 2U)
  77. {
  78. /*POWER SPECTRUM*/
  79. for(j=0; j<(SLocInternal->Sample_Number_To_Process/8U); j++)
  80. {
  81. Power_Spectrum[(j*2U)] = (M2_Data[buffer_offset+(j*2U)] * M1_Data[buffer_offset+(j*2U)]) + (M2_Data[buffer_offset+(j*2U)+1U] * M1_Data[buffer_offset+(j*2U)+1U]);
  82. Power_Spectrum[(j*2U)+1U] = (-((M2_Data[buffer_offset+(j*2U)])*(M1_Data[buffer_offset+(j*2U)+1U]))) + (M2_Data[buffer_offset+(j*2U)+1U] * M1_Data[buffer_offset+(j*2U)]);
  83. float32_t arg_sqrtf = (Power_Spectrum[(j*2U)]*Power_Spectrum[(j*2U)]) + (Power_Spectrum[(j*2U)+1U]*Power_Spectrum[(j*2U)+1U]);
  84. if (arg_sqrtf >= 0.0f)
  85. {
  86. tempMag=sqrtf(arg_sqrtf);
  87. if(tempMag<(1e-7f))
  88. {
  89. tempMag=(1e-7f);
  90. }
  91. }
  92. Power_Spectrum[(j*2U)]=(Power_Spectrum[(j*2U)])/tempMag;
  93. Power_Spectrum[(j*2U)+1U]=(Power_Spectrum[(j*2U)+1U])/tempMag;
  94. }
  95. int32_t angle;
  96. float32_t anglesNum=(180.0f/(float32_t)SLocInternal->resolution);
  97. for(angle=0;angle<(int32_t)anglesNum;angle++)
  98. {
  99. float32_t idftSum=0.0f;
  100. int32_t bin;
  101. float32_t theta;
  102. float32_t cosineTerm;
  103. float32_t sineTerm;
  104. float32_t theta_term= -2.0f*PI*(float32_t)SLocInternal->sampling_frequency*SLocInternal->M12_distance*(float32_t)arm_cos_f32(PI - ((PI*(float32_t)angle)/(anglesNum-1.0f)))/((float32_t)SLocInternal->Sample_Number_To_Process*SOUND_SPEED);
  105. for(bin=0; bin<((int32_t)SLocInternal->Sample_Number_To_Process/8); bin++)
  106. {
  107. theta = theta_term*(float32_t)bin;
  108. cosineTerm=arm_cos_f32(theta);
  109. sineTerm=arm_sin_f32(theta);
  110. idftSum+=(Power_Spectrum[2*bin]*cosineTerm)-(Power_Spectrum[(2*bin)+1]*sineTerm);
  111. }
  112. SLocInternal->Phase[angle]=idftSum;
  113. }
  114. SLocInternal->Estimated_Angle_12=get_max_pos(SLocInternal,(int32_t)anglesNum,(((int32_t)anglesNum/40)+1));
  115. SLocInternal->Estimated_Angle_12= 180-(int32_t)floor((180.0/((float64_t)anglesNum*2.0))+((float64_t)SLocInternal->Estimated_Angle_12*(float64_t)(SLocInternal->resolution)));
  116. }
  117. if(SLocInternal->Mic_Number == 4U)
  118. {
  119. /*POWER SPECTRUM*/
  120. for(j=0; j<(SLocInternal->Sample_Number_To_Process/8U); j++)
  121. {
  122. Power_Spectrum[(j*2U)] = (M4_Data[buffer_offset+(j*2U)] * M3_Data[buffer_offset+(j*2U)]) + (M4_Data[buffer_offset+(j*2U)+1U] * M3_Data[buffer_offset+(j*2U)+1U]);
  123. Power_Spectrum[(j*2U)+1U] = (-((M4_Data[buffer_offset+(j*2U)])*(M3_Data[buffer_offset+(j*2U)+1U]))) + (M4_Data[buffer_offset+(j*2U)+1U] * M3_Data[buffer_offset+(j*2U)]);
  124. float32_t arg_sqrtf = (Power_Spectrum[(j*2U)]*Power_Spectrum[(j*2U)]) + (Power_Spectrum[(j*2U)+1U]*Power_Spectrum[(j*2U)+1U]);
  125. if (arg_sqrtf >= 0.0f)
  126. {
  127. tempMag=sqrtf(arg_sqrtf);
  128. if(tempMag<(1e-7f))
  129. {
  130. tempMag=(1e-7f);
  131. }
  132. }
  133. Power_Spectrum[(j*2U)]=(Power_Spectrum[(j*2U)])/tempMag;
  134. Power_Spectrum[(j*2U)+1U]=(Power_Spectrum[(j*2U)+1U])/tempMag;
  135. }
  136. int32_t angle;
  137. float32_t anglesNum=(180.0f/(float32_t)SLocInternal->resolution);
  138. for(angle=0;angle<(int32_t)anglesNum;angle++)
  139. {
  140. float32_t idftSum=0.0f;
  141. int32_t bin;
  142. float32_t theta;
  143. float32_t cosineTerm;
  144. float32_t sineTerm;
  145. float32_t theta_term= -2.0f*PI*(float32_t)SLocInternal->sampling_frequency*SLocInternal->M34_distance*(float32_t)arm_cos_f32(PI - ((PI*(float32_t)angle)/(anglesNum-1.0f)))/((float32_t)SLocInternal->Sample_Number_To_Process*SOUND_SPEED);
  146. for(bin=0; bin<((int32_t)SLocInternal->Sample_Number_To_Process/8); bin++)
  147. {
  148. theta = theta_term*(float32_t)bin;
  149. cosineTerm=arm_cos_f32(theta);
  150. sineTerm=arm_sin_f32(theta);
  151. idftSum+=(Power_Spectrum[2*bin]*cosineTerm)-(Power_Spectrum[(2*bin)+1]*sineTerm);
  152. }
  153. SLocInternal->Phase[angle]=idftSum;
  154. }
  155. SLocInternal->Estimated_Angle_34=get_max_pos(SLocInternal,(int32_t)anglesNum,(((int32_t)anglesNum/40)+1));
  156. SLocInternal->Estimated_Angle_34= 180-(int32_t)floor((180.0/((float64_t)anglesNum*2.0))+((float64_t)SLocInternal->Estimated_Angle_34*(float64_t)(SLocInternal->resolution)));
  157. }
  158. float32_t angle_out_f = 0.0f;
  159. if(SLocInternal->Mic_Number == 2U)
  160. {
  161. angle_out_f=(float32_t)SLocInternal->Estimated_Angle_12;
  162. }
  163. else if (SLocInternal->Mic_Number == 4U)
  164. {
  165. if((SLocInternal->Estimated_Angle_12 + SLocInternal->Estimated_Angle_34) < (90 - ((int32_t)SLocInternal->resolution * 2)))
  166. {
  167. out_angles[0]=-1;
  168. out_angles[1]=-1;
  169. return -1.0f;
  170. }
  171. else
  172. {
  173. float32_t angle1_f = arm_cos_f32((((float32_t)SLocInternal->Estimated_Angle_12)/180.0f)*3.14f);
  174. float32_t angle2_f = arm_cos_f32((((float32_t)SLocInternal->Estimated_Angle_34)/180.0f)*3.14f);
  175. float32_t cos_fi = 0.0f;
  176. float32_t arg_cos_fi = 1.0f - (angle1_f*angle1_f) - (angle2_f*angle2_f);
  177. if (arg_cos_fi >= 0.0f)
  178. {
  179. cos_fi = sqrtf(arg_cos_fi);
  180. }
  181. angle_out_f = atan2f((angle1_f),(angle2_f));
  182. angle_out_f= angle_out_f * (360.0f / (3.14f*2.0f));
  183. if(angle_out_f < 0.0f)
  184. {
  185. angle_out_f+=360.0f; /* in [0 360] -> TODO: Optimize the whole angle computation from the maximum phase index*/
  186. }
  187. if ((cos_fi <= 1.0f) && (cos_fi >= -1.0f))
  188. {
  189. fi = acosf(cos_fi)*180.0f/3.14f;
  190. }
  191. }
  192. }
  193. SLocInternal->Buffer_State=0;
  194. out_angles[0]=(int32_t)angle_out_f;
  195. if(isnan(fi)!= 0U)
  196. {
  197. out_angles[1]=(int32_t)fi;
  198. }
  199. else
  200. {
  201. out_angles[1]=-1;
  202. }
  203. return angle_out_f;
  204. }

(更新中)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/854111
推荐阅读
相关标签
  

闽ICP备14008679号