{"id":357,"date":"2022-12-15T13:14:20","date_gmt":"2022-12-15T05:14:20","guid":{"rendered":"http:\/\/www.huangrongzhen.ink\/?p=357"},"modified":"2023-03-30T17:02:36","modified_gmt":"2023-03-30T09:02:36","slug":"arm-dsp-%e5%ba%93%e4%b8%8b%e4%bd%bf%e7%94%a8-fft-%e5%ae%9e%e7%8e%b0%e8%87%aa%e7%9b%b8%e5%85%b3","status":"publish","type":"post","link":"https:\/\/www.huangrongzhen.ink\/?p=357","title":{"rendered":"ARM DSP \u5e93\u4e0b\u4f7f\u7528 FFT \u5b9e\u73b0\u81ea\u76f8\u5173"},"content":{"rendered":"<div class=\"wp-block-post-excerpt\"><p class=\"wp-block-post-excerpt__excerpt\">\u4f7f\u7528 FFT \u5b9e\u73b0\u81ea\u76f8\u5173 <\/p><\/div>\n\n\n<p>\u901a\u7528\u7684\u81ea\u76f8\u5173\u7b97\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">void MyXcorr(float* input, unsigned int len, float* output)\n{\n  unsigned int i, j;\n  double sum;\n  for(i = 0; i &lt; 2 * len; i++)\n  {\n    sum = 0;\n    for(j = 0; j &lt; len; j++)\n    {\n      sum = sum + input[j] * input[j + i];\n    }\n    sum = sum \/ len;\n    output[i] = sum;\n  }\n}<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528 FFT \u5b9e\u73b0\u7684\u81ea\u76f8\u5173\u7b97\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">\/\/\u6ce8\u610f\uff1a\u6700\u5927\u652f\u63012048\u4e2a\u70b9\nvoid XcorrWithFFT_f32(float* a, unsigned int aLen, float* b, unsigned int bLen, float* output)\n{\n#if 1 \/\/no flip, maybe with a bug, but faster\n  static arm_cfft_instance_f32 s_structCFFTF32;\n  float32_t* aSource = NULL;\n  float32_t* bSource = NULL;\n  float32_t* cMul    = NULL;\n  unsigned int totalLen, i;\n  \n  totalLen = aLen + bLen;\n  \n  \/\/malloc\n  aSource = MyMalloc(SRAMIN, 2 * totalLen * sizeof(float32_t));\n  bSource = MyMalloc(SRAMIN, 2 * totalLen * sizeof(float32_t));\n  cMul = MyMalloc(SRAMIN, 2 * totalLen * sizeof(float32_t));\n  if((NULL == aSource) || (NULL == bSource) || (NULL == cMul))\n  {\n    printf(\"XcorrWithFFT: fail to malloc\\r\\n\");\n    while(1){}\n  }\n  \n  \/\/copy and corver with zero for A\n  for(i = 0; i &lt; aLen; i++){aSource[(2 * i) + 0] = a[i]; aSource[(2 * i) + 1] = 0;}\n  while(i &lt; totalLen){aSource[(2 * i) + 0] = 0;aSource[(2 * i) + 1] = 0;i++;}\n  \n  \/\/copy and corver with zero for B\n  for(i = 0; i &lt; bLen; i++){bSource[(2 * i) + 0] = b[i]; bSource[(2 * i) + 1] = 0;}\n  while(i &lt; totalLen){bSource[(2 * i) + 0] = 0;bSource[(2 * i) + 1] = 0;i++;}\n  \n  \/\/A FFT\n  arm_cfft_init_f32(&amp;s_structCFFTF32, totalLen);\n  arm_cfft_f32(&amp;s_structCFFTF32, aSource, 0, 1);\n  \n  \/\/B FFT\n  arm_cfft_init_f32(&amp;s_structCFFTF32, totalLen);\n  arm_cfft_f32(&amp;s_structCFFTF32, bSource, 0, 1);\n  \n  \/\/MUX\n  arm_cmplx_mult_cmplx_f32(aSource, bSource, cMul, totalLen);\n  \n  \/\/IFFT\n  arm_cfft_init_f32(&amp;s_structCFFTF32, totalLen);\n  arm_cfft_f32(&amp;s_structCFFTF32, cMul, 1, 1);\n  \n  \/\/output, only real part, the imaginary part is always zero\n  \/\/arm_cmplx_mag_f32(cMul, output, totalLen);\n  for(i = 0; i &lt; totalLen; i++){output[i] = cMul[(2 * i) + 0];}\n  \n  \/\/free\n  MyFree(aSource);\n  MyFree(bSource);\n  MyFree(cMul);\n\n#else \/\/with flip\n  static arm_cfft_instance_f32 s_structCFFTF32;\n  float32_t* aSource = NULL;\n  float32_t* bSource = NULL;\n  float32_t* bConj   = NULL;\n  float32_t* cMul    = NULL;\n  unsigned int totalLen, i;\n  \n  totalLen = aLen + bLen;\n  \n  \/\/malloc\n  aSource = MyMalloc(SRAMIN, 2 * totalLen * sizeof(float32_t));\n  bSource = MyMalloc(SRAMIN, 2 * totalLen * sizeof(float32_t));\n  bConj = MyMalloc(SRAMIN, 2 * totalLen * sizeof(float32_t));\n  cMul = MyMalloc(SRAMIN, 2 * totalLen * sizeof(float32_t));\n  if((NULL == aSource) || (NULL == bSource) || (NULL == bConj) || (NULL == cMul))\n  {\n    printf(\"XcorrWithFFT: fail to malloc\\r\\n\");\n    while(1){}\n  }\n  \n  \/\/copy and corver with zero for A\n  for(i = 0; i &lt; aLen; i++){aSource[(2 * i) + 0] = a[i]; aSource[(2 * i) + 1] = 0;}\n  while(i &lt; totalLen){aSource[(2 * i) + 0] = 0;aSource[(2 * i) + 1] = 0;i++;}\n  \n  \/\/copy and corver with zero for B, and flip B\n  for(i = 0; i &lt; bLen; i++){bSource[(2 * i) + 0] = b[i]; bSource[(2 * i) + 1] = 0;}\n  while(i &lt; totalLen){bSource[(2 * i) + 0] = 0;bSource[(2 * i) + 1] = 0;i++;}\n  \n  \/\/A FFT\n  arm_cfft_init_f32(&amp;s_structCFFTF32, totalLen);\n  arm_cfft_f32(&amp;s_structCFFTF32, aSource, 0, 1);\n  \n  \/\/B FFT and conj\n  arm_cfft_init_f32(&amp;s_structCFFTF32, totalLen);\n  arm_cfft_f32(&amp;s_structCFFTF32, bSource, 0, 1);\n  arm_cmplx_conj_f32(bSource, bConj, totalLen);\n  \n  \/\/MUX\n  arm_cmplx_mult_cmplx_f32(aSource, bConj, cMul, totalLen);\n  \n  \/\/IFFT\n  arm_cfft_init_f32(&amp;s_structCFFTF32, totalLen);\n  arm_cfft_f32(&amp;s_structCFFTF32, cMul, 1, 1);\n  \n  \/\/output, only real part, zhe imaginary part is always zero\n  for(i = 0; i &lt; totalLen; i++){output[(i + (totalLen \/ 2)) % totalLen] = cMul[(2 * i) + 0];}\n  \n  \/\/free\n  MyFree(aSource);\n  MyFree(bSource);\n  MyFree(bConj);\n  MyFree(cMul);\n#endif\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 FFT \u5b9e\u73b0\u81ea\u76f8\u5173<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=\/wp\/v2\/posts\/357"}],"collection":[{"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=357"}],"version-history":[{"count":5,"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=\/wp\/v2\/posts\/357\/revisions"}],"predecessor-version":[{"id":1329,"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=\/wp\/v2\/posts\/357\/revisions\/1329"}],"wp:attachment":[{"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.huangrongzhen.ink\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}