[DemoCode下载] FFT算法实现

[复制链接]
11200|30
 楼主| jiekou001 发表于 2023-6-20 16:29 | 显示全部楼层 |阅读模式
EC_NUC121_FFT_Demo_V1.00.zip (1.36 MB, 下载次数: 7)
快速傅立叶变换,用于将时间域的离散讯号转换至频率域表示,用户得以观察该讯号的频率组成成分与分布情形,是常见的讯号分析手段。此范例将示范如何透过 ADC 与 TIMER 的搭配使用,取样出一段离散讯号,并进行快速傅立叶变换,最后观察频谱的数值分布是否符合原始输入讯号的特性。支持三种讯号长度范例,分别为 128 取样点,256取样点,以及512取样点。
首先利用波形产生器输出频率固定为 60 Hz 的正弦波,模拟一般应用情境下欲分析的讯号来源,给予微控制器使用 ADC 进行取样。使用者可根据应用上的需求自行调整取样频率,注意其数值会直接影响频谱的带宽与分辨率。每次的 TIMER 逾时事件,硬件便会自动触发 ADC 进行一次取样动作,软件则须于 ADC 转换完毕后的中断服务内,将数据以位反转的排序写入缓冲区当中。
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     fft_lut.h
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * [url=home.php?mod=space&uid=247401]@brief[/url]    Fast Fourier Transform Parameter Look-up Table
  5. *
  6. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2019 Nuvoton Technology Corp. All rights reserved.
  7. *****************************************************************************/
  8. #ifndef __FFT_LUT_H__
  9. #define __FFT_LUT_H__

  10. /* This example provides 3 types of selectable data length */
  11. #define FFT_128_POINT               (0)
  12. #define FFT_256_POINT               (1)
  13. #define FFT_512_POINT               (2)

  14. /* User can select one of the length setting here. */
  15. #define FFT_N_POINT_SEL             (FFT_512_POINT)


  16. /* Assign the corresponding look-up table according to the length setting.
  17.    Do not modify the contents here. */
  18. #if (FFT_N_POINT_SEL == FFT_128_POINT)
  19. #define SEQUENCE_LEN                (128)
  20. #define RESCALE(x)                  (x>>7)
  21. #define COS_LUT                     COS_SCALED_LUT_128
  22. #define SIN_LUT                     SIN_SCALED_LUT_128
  23. #define REV_LUT                     REV_LUT_128

  24. #elif (FFT_N_POINT_SEL == FFT_256_POINT)
  25. #define SEQUENCE_LEN                (256)
  26. #define RESCALE(x)                  (x>>8)
  27. #define COS_LUT                     COS_SCALED_LUT_256
  28. #define SIN_LUT                     SIN_SCALED_LUT_256
  29. #define REV_LUT                     REV_LUT_256

  30. #elif (FFT_N_POINT_SEL == FFT_512_POINT)
  31. #define SEQUENCE_LEN                (512)
  32. #define RESCALE(x)                  (x>>9)
  33. #define COS_LUT                     COS_SCALED_LUT_512
  34. #define SIN_LUT                     SIN_SCALED_LUT_512
  35. #define REV_LUT                     REV_LUT_512

  36. #else
  37. #define "configuration error"
  38. #endif



  39. #if (FFT_SEQUENCE_LEN == FFT_SEQUENCE_LEN_128)
  40. #define COS_SCALED_LUT_128                                                                                      \
  41. {                                                                                                               \
  42.   127,   127,   126,   126,   125,   123,   122,   120,   117,   115,   112,   109,   106,   102,    98,    94, \
  43.    90,    85,    81,    76,    71,    65,    60,    54,    49,    43,    37,    31,    25,    19,    12,     6, \
  44.     0,    -6,   -12,   -19,   -25,   -31,   -37,   -43,   -49,   -54,   -60,   -65,   -71,   -76,   -81,   -85, \
  45.   -90,   -94,   -98,  -102,  -106,  -109,  -112,  -115,  -117,  -120,  -122,  -123,  -125,  -126,  -126,  -127, \
  46. -127,  -127,  -126,  -126,  -125,  -123,  -122,  -120,  -117,  -115,  -112,  -109,  -106,  -102,   -98,   -94, \
  47.   -90,   -85,   -81,   -76,   -71,   -65,   -60,   -54,   -49,   -43,   -37,   -31,   -25,   -19,   -12,    -6, \
  48.     0,     6,    12,    19,    25,    31,    37,    43,    49,    54,    60,    65,    71,    76,    81,    85, \
  49.    90,    94,    98,   102,   106,   109,   112,   115,   117,   120,   122,   123,   125,   126,   126,   127, \
  50. }

  51. #define SIN_SCALED_LUT_128                                                                                      \
  52. {                                                                                                               \
  53.     0,     6,    12,    19,    25,    31,    37,    43,    49,    54,    60,    65,    71,    76,    81,    85, \
  54.    90,    94,    98,   102,   106,   109,   112,   115,   117,   120,   122,   123,   125,   126,   126,   127, \
  55.   127,   127,   126,   126,   125,   123,   122,   120,   117,   115,   112,   109,   106,   102,    98,    94, \
  56.    90,    85,    81,    76,    71,    65,    60,    54,    49,    43,    37,    31,    25,    19,    12,     6, \
  57.     0,    -6,   -12,   -19,   -25,   -31,   -37,   -43,   -49,   -54,   -60,   -65,   -71,   -76,   -81,   -85, \
  58.   -90,   -94,   -98,  -102,  -106,  -109,  -112,  -115,  -117,  -120,  -122,  -123,  -125,  -126,  -126,  -127, \
  59. -127,  -127,  -126,  -126,  -125,  -123,  -122,  -120,  -117,  -115,  -112,  -109,  -106,  -102,   -98,   -94, \
  60.   -90,   -85,   -81,   -76,   -71,   -65,   -60,   -54,   -49,   -43,   -37,   -31,   -25,   -19,   -12,    -6, \
  61. }

  62. #define REV_LUT_128                                                                                             \
  63. {                                                                                                               \
  64.     0,    64,    32,    96,    16,    80,    48,   112,     8,    72,    40,   104,    24,    88,    56,   120, \
  65.     4,    68,    36,   100,    20,    84,    52,   116,    12,    76,    44,   108,    28,    92,    60,   124, \
  66.     2,    66,    34,    98,    18,    82,    50,   114,    10,    74,    42,   106,    26,    90,    58,   122, \
  67.     6,    70,    38,   102,    22,    86,    54,   118,    14,    78,    46,   110,    30,    94,    62,   126, \
  68.     1,    65,    33,    97,    17,    81,    49,   113,     9,    73,    41,   105,    25,    89,    57,   121, \
  69.     5,    69,    37,   101,    21,    85,    53,   117,    13,    77,    45,   109,    29,    93,    61,   125, \
  70.     3,    67,    35,    99,    19,    83,    51,   115,    11,    75,    43,   107,    27,    91,    59,   123, \
  71.     7,    71,    39,   103,    23,    87,    55,   119,    15,    79,    47,   111,    31,    95,    63,   127, \
  72. }

  73. #endif


  74. #if (FFT_SEQUENCE_LEN == FFT_SEQUENCE_LEN_256)
  75. #define COS_SCALED_LUT_256                                                                                      \
  76. {                                                                                                               \
  77.   255,   255,   255,   254,   254,   253,   252,   251,   250,   249,   247,   246,   244,   242,   240,   238, \
  78.   236,   233,   231,   228,   225,   222,   219,   215,   212,   208,   205,   201,   197,   193,   189,   185, \
  79.   180,   176,   171,   167,   162,   157,   152,   147,   142,   136,   131,   126,   120,   115,   109,   103, \
  80.    98,    92,    86,    80,    74,    68,    62,    56,    50,    44,    37,    31,    25,    19,    13,     6, \
  81.     0,    -6,   -13,   -19,   -25,   -31,   -37,   -44,   -50,   -56,   -62,   -68,   -74,   -80,   -86,   -92, \
  82.   -98,  -103,  -109,  -115,  -120,  -126,  -131,  -136,  -142,  -147,  -152,  -157,  -162,  -167,  -171,  -176, \
  83. -180,  -185,  -189,  -193,  -197,  -201,  -205,  -208,  -212,  -215,  -219,  -222,  -225,  -228,  -231,  -233, \
  84. -236,  -238,  -240,  -242,  -244,  -246,  -247,  -249,  -250,  -251,  -252,  -253,  -254,  -254,  -255,  -255, \
  85. -255,  -255,  -255,  -254,  -254,  -253,  -252,  -251,  -250,  -249,  -247,  -246,  -244,  -242,  -240,  -238, \
  86. -236,  -233,  -231,  -228,  -225,  -222,  -219,  -215,  -212,  -208,  -205,  -201,  -197,  -193,  -189,  -185, \
  87. -180,  -176,  -171,  -167,  -162,  -157,  -152,  -147,  -142,  -136,  -131,  -126,  -120,  -115,  -109,  -103, \
  88.   -98,   -92,   -86,   -80,   -74,   -68,   -62,   -56,   -50,   -44,   -37,   -31,   -25,   -19,   -13,    -6, \
  89.     0,     6,    13,    19,    25,    31,    37,    44,    50,    56,    62,    68,    74,    80,    86,    92, \
  90.    98,   103,   109,   115,   120,   126,   131,   136,   142,   147,   152,   157,   162,   167,   171,   176, \
  91.   180,   185,   189,   193,   197,   201,   205,   208,   212,   215,   219,   222,   225,   228,   231,   233, \
  92.   236,   238,   240,   242,   244,   246,   247,   249,   250,   251,   252,   253,   254,   254,   255,   255, \
  93. }

  94. #define SIN_SCALED_LUT_256                                                                                      \
  95. {                                                                                                               \
  96.     0,     6,    13,    19,    25,    31,    37,    44,    50,    56,    62,    68,    74,    80,    86,    92, \
  97.    98,   103,   109,   115,   120,   126,   131,   136,   142,   147,   152,   157,   162,   167,   171,   176, \
  98.   180,   185,   189,   193,   197,   201,   205,   208,   212,   215,   219,   222,   225,   228,   231,   233, \
  99.   236,   238,   240,   242,   244,   246,   247,   249,   250,   251,   252,   253,   254,   254,   255,   255, \
  100.   255,   255,   255,   254,   254,   253,   252,   251,   250,   249,   247,   246,   244,   242,   240,   238, \
  101.   236,   233,   231,   228,   225,   222,   219,   215,   212,   208,   205,   201,   197,   193,   189,   185, \
  102.   180,   176,   171,   167,   162,   157,   152,   147,   142,   136,   131,   126,   120,   115,   109,   103, \
  103.    98,    92,    86,    80,    74,    68,    62,    56,    50,    44,    37,    31,    25,    19,    13,     6, \
  104.     0,    -6,   -13,   -19,   -25,   -31,   -37,   -44,   -50,   -56,   -62,   -68,   -74,   -80,   -86,   -92, \
  105.   -98,  -103,  -109,  -115,  -120,  -126,  -131,  -136,  -142,  -147,  -152,  -157,  -162,  -167,  -171,  -176, \
  106. -180,  -185,  -189,  -193,  -197,  -201,  -205,  -208,  -212,  -215,  -219,  -222,  -225,  -228,  -231,  -233, \
  107. -236,  -238,  -240,  -242,  -244,  -246,  -247,  -249,  -250,  -251,  -252,  -253,  -254,  -254,  -255,  -255, \
  108. -255,  -255,  -255,  -254,  -254,  -253,  -252,  -251,  -250,  -249,  -247,  -246,  -244,  -242,  -240,  -238, \
  109. -236,  -233,  -231,  -228,  -225,  -222,  -219,  -215,  -212,  -208,  -205,  -201,  -197,  -193,  -189,  -185, \
  110. -180,  -176,  -171,  -167,  -162,  -157,  -152,  -147,  -142,  -136,  -131,  -126,  -120,  -115,  -109,  -103, \
  111.   -98,   -92,   -86,   -80,   -74,   -68,   -62,   -56,   -50,   -44,   -37,   -31,   -25,   -19,   -13,    -6, \
  112. }

  113. #define REV_LUT_256                                                                                             \
  114. {                                                                                                               \
  115.     0,   128,    64,   192,    32,   160,    96,   224,    16,   144,    80,   208,    48,   176,   112,   240, \
  116.     8,   136,    72,   200,    40,   168,   104,   232,    24,   152,    88,   216,    56,   184,   120,   248, \
  117.     4,   132,    68,   196,    36,   164,   100,   228,    20,   148,    84,   212,    52,   180,   116,   244, \
  118.    12,   140,    76,   204,    44,   172,   108,   236,    28,   156,    92,   220,    60,   188,   124,   252, \
  119.     2,   130,    66,   194,    34,   162,    98,   226,    18,   146,    82,   210,    50,   178,   114,   242, \
  120.    10,   138,    74,   202,    42,   170,   106,   234,    26,   154,    90,   218,    58,   186,   122,   250, \
  121.     6,   134,    70,   198,    38,   166,   102,   230,    22,   150,    86,   214,    54,   182,   118,   246, \
  122.    14,   142,    78,   206,    46,   174,   110,   238,    30,   158,    94,   222,    62,   190,   126,   254, \
  123.     1,   129,    65,   193,    33,   161,    97,   225,    17,   145,    81,   209,    49,   177,   113,   241, \
  124.     9,   137,    73,   201,    41,   169,   105,   233,    25,   153,    89,   217,    57,   185,   121,   249, \
  125.     5,   133,    69,   197,    37,   165,   101,   229,    21,   149,    85,   213,    53,   181,   117,   245, \
  126.    13,   141,    77,   205,    45,   173,   109,   237,    29,   157,    93,   221,    61,   189,   125,   253, \
  127.     3,   131,    67,   195,    35,   163,    99,   227,    19,   147,    83,   211,    51,   179,   115,   243, \
  128.    11,   139,    75,   203,    43,   171,   107,   235,    27,   155,    91,   219,    59,   187,   123,   251, \
  129.     7,   135,    71,   199,    39,   167,   103,   231,    23,   151,    87,   215,    55,   183,   119,   247, \
  130.    15,   143,    79,   207,    47,   175,   111,   239,    31,   159,    95,   223,    63,   191,   127,   255, \
  131. }

  132. #endif


  133. #if (FFT_SEQUENCE_LEN == FFT_SEQUENCE_LEN_512)
  134. #define COS_SCALED_LUT_512                                                                                      \
  135. {                                                                                                               \
  136.   511,   511,   511,   511,   510,   510,   510,   509,   509,   508,   507,   506,   505,   505,   503,   502, \
  137.   501,   500,   499,   497,   496,   494,   492,   491,   489,   487,   485,   483,   481,   479,   477,   474, \
  138.   472,   470,   467,   465,   462,   459,   456,   454,   451,   448,   445,   441,   438,   435,   432,   428, \
  139.   425,   421,   418,   414,   410,   407,   403,   399,   395,   391,   387,   383,   379,   374,   370,   366, \
  140.   361,   357,   352,   348,   343,   338,   334,   329,   324,   319,   314,   309,   304,   299,   294,   289, \
  141.   284,   279,   273,   268,   263,   257,   252,   246,   241,   235,   230,   224,   218,   213,   207,   201, \
  142.   196,   190,   184,   178,   172,   166,   160,   154,   148,   142,   136,   130,   124,   118,   112,   106, \
  143.   100,    94,    87,    81,    75,    69,    63,    56,    50,    44,    38,    31,    25,    19,    13,     6, \
  144.     0,    -6,   -13,   -19,   -25,   -31,   -38,   -44,   -50,   -56,   -63,   -69,   -75,   -81,   -87,   -94, \
  145. -100,  -106,  -112,  -118,  -124,  -130,  -136,  -142,  -148,  -154,  -160,  -166,  -172,  -178,  -184,  -190, \
  146. -196,  -201,  -207,  -213,  -218,  -224,  -230,  -235,  -241,  -246,  -252,  -257,  -263,  -268,  -273,  -279, \
  147. -284,  -289,  -294,  -299,  -304,  -309,  -314,  -319,  -324,  -329,  -334,  -338,  -343,  -348,  -352,  -357, \
  148. -361,  -366,  -370,  -374,  -379,  -383,  -387,  -391,  -395,  -399,  -403,  -407,  -410,  -414,  -418,  -421, \
  149. -425,  -428,  -432,  -435,  -438,  -441,  -445,  -448,  -451,  -454,  -456,  -459,  -462,  -465,  -467,  -470, \
  150. -472,  -474,  -477,  -479,  -481,  -483,  -485,  -487,  -489,  -491,  -492,  -494,  -496,  -497,  -499,  -500, \
  151. -501,  -502,  -503,  -505,  -505,  -506,  -507,  -508,  -509,  -509,  -510,  -510,  -510,  -511,  -511,  -511, \
  152. -511,  -511,  -511,  -511,  -510,  -510,  -510,  -509,  -509,  -508,  -507,  -506,  -505,  -505,  -503,  -502, \
  153. -501,  -500,  -499,  -497,  -496,  -494,  -492,  -491,  -489,  -487,  -485,  -483,  -481,  -479,  -477,  -474, \
  154. -472,  -470,  -467,  -465,  -462,  -459,  -456,  -454,  -451,  -448,  -445,  -441,  -438,  -435,  -432,  -428, \
  155. -425,  -421,  -418,  -414,  -410,  -407,  -403,  -399,  -395,  -391,  -387,  -383,  -379,  -374,  -370,  -366, \
  156. -361,  -357,  -352,  -348,  -343,  -338,  -334,  -329,  -324,  -319,  -314,  -309,  -304,  -299,  -294,  -289, \
  157. -284,  -279,  -273,  -268,  -263,  -257,  -252,  -246,  -241,  -235,  -230,  -224,  -218,  -213,  -207,  -201, \
  158. -196,  -190,  -184,  -178,  -172,  -166,  -160,  -154,  -148,  -142,  -136,  -130,  -124,  -118,  -112,  -106, \
  159. -100,   -94,   -87,   -81,   -75,   -69,   -63,   -56,   -50,   -44,   -38,   -31,   -25,   -19,   -13,    -6, \
  160.     0,     6,    13,    19,    25,    31,    38,    44,    50,    56,    63,    69,    75,    81,    87,    94, \
  161.   100,   106,   112,   118,   124,   130,   136,   142,   148,   154,   160,   166,   172,   178,   184,   190, \
  162.   196,   201,   207,   213,   218,   224,   230,   235,   241,   246,   252,   257,   263,   268,   273,   279, \
  163.   284,   289,   294,   299,   304,   309,   314,   319,   324,   329,   334,   338,   343,   348,   352,   357, \
  164.   361,   366,   370,   374,   379,   383,   387,   391,   395,   399,   403,   407,   410,   414,   418,   421, \
  165.   425,   428,   432,   435,   438,   441,   445,   448,   451,   454,   456,   459,   462,   465,   467,   470, \
  166.   472,   474,   477,   479,   481,   483,   485,   487,   489,   491,   492,   494,   496,   497,   499,   500, \
  167.   501,   502,   503,   505,   505,   506,   507,   508,   509,   509,   510,   510,   510,   511,   511,   511, \
  168. }

  169. #define SIN_SCALED_LUT_512                                                                                      \
  170. {                                                                                                               \
  171.     0,     6,    13,    19,    25,    31,    38,    44,    50,    56,    63,    69,    75,    81,    87,    94, \
  172.   100,   106,   112,   118,   124,   130,   136,   142,   148,   154,   160,   166,   172,   178,   184,   190, \
  173.   196,   201,   207,   213,   218,   224,   230,   235,   241,   246,   252,   257,   263,   268,   273,   279, \
  174.   284,   289,   294,   299,   304,   309,   314,   319,   324,   329,   334,   338,   343,   348,   352,   357, \
  175.   361,   366,   370,   374,   379,   383,   387,   391,   395,   399,   403,   407,   410,   414,   418,   421, \
  176.   425,   428,   432,   435,   438,   441,   445,   448,   451,   454,   456,   459,   462,   465,   467,   470, \
  177.   472,   474,   477,   479,   481,   483,   485,   487,   489,   491,   492,   494,   496,   497,   499,   500, \
  178.   501,   502,   503,   505,   505,   506,   507,   508,   509,   509,   510,   510,   510,   511,   511,   511, \
  179.   511,   511,   511,   511,   510,   510,   510,   509,   509,   508,   507,   506,   505,   505,   503,   502, \
  180.   501,   500,   499,   497,   496,   494,   492,   491,   489,   487,   485,   483,   481,   479,   477,   474, \
  181.   472,   470,   467,   465,   462,   459,   456,   454,   451,   448,   445,   441,   438,   435,   432,   428, \
  182.   425,   421,   418,   414,   410,   407,   403,   399,   395,   391,   387,   383,   379,   374,   370,   366, \
  183.   361,   357,   352,   348,   343,   338,   334,   329,   324,   319,   314,   309,   304,   299,   294,   289, \
  184.   284,   279,   273,   268,   263,   257,   252,   246,   241,   235,   230,   224,   218,   213,   207,   201, \
  185.   196,   190,   184,   178,   172,   166,   160,   154,   148,   142,   136,   130,   124,   118,   112,   106, \
  186.   100,    94,    87,    81,    75,    69,    63,    56,    50,    44,    38,    31,    25,    19,    13,     6, \
  187.     0,    -6,   -13,   -19,   -25,   -31,   -38,   -44,   -50,   -56,   -63,   -69,   -75,   -81,   -87,   -94, \
  188. -100,  -106,  -112,  -118,  -124,  -130,  -136,  -142,  -148,  -154,  -160,  -166,  -172,  -178,  -184,  -190, \
  189. -196,  -201,  -207,  -213,  -218,  -224,  -230,  -235,  -241,  -246,  -252,  -257,  -263,  -268,  -273,  -279, \
  190. -284,  -289,  -294,  -299,  -304,  -309,  -314,  -319,  -324,  -329,  -334,  -338,  -343,  -348,  -352,  -357, \
  191. -361,  -366,  -370,  -374,  -379,  -383,  -387,  -391,  -395,  -399,  -403,  -407,  -410,  -414,  -418,  -421, \
  192. -425,  -428,  -432,  -435,  -438,  -441,  -445,  -448,  -451,  -454,  -456,  -459,  -462,  -465,  -467,  -470, \
  193. -472,  -474,  -477,  -479,  -481,  -483,  -485,  -487,  -489,  -491,  -492,  -494,  -496,  -497,  -499,  -500, \
  194. -501,  -502,  -503,  -505,  -505,  -506,  -507,  -508,  -509,  -509,  -510,  -510,  -510,  -511,  -511,  -511, \
  195. -511,  -511,  -511,  -511,  -510,  -510,  -510,  -509,  -509,  -508,  -507,  -506,  -505,  -505,  -503,  -502, \
  196. -501,  -500,  -499,  -497,  -496,  -494,  -492,  -491,  -489,  -487,  -485,  -483,  -481,  -479,  -477,  -474, \
  197. -472,  -470,  -467,  -465,  -462,  -459,  -456,  -454,  -451,  -448,  -445,  -441,  -438,  -435,  -432,  -428, \
  198. -425,  -421,  -418,  -414,  -410,  -407,  -403,  -399,  -395,  -391,  -387,  -383,  -379,  -374,  -370,  -366, \
  199. -361,  -357,  -352,  -348,  -343,  -338,  -334,  -329,  -324,  -319,  -314,  -309,  -304,  -299,  -294,  -289, \
  200. -284,  -279,  -273,  -268,  -263,  -257,  -252,  -246,  -241,  -235,  -230,  -224,  -218,  -213,  -207,  -201, \
  201. -196,  -190,  -184,  -178,  -172,  -166,  -160,  -154,  -148,  -142,  -136,  -130,  -124,  -118,  -112,  -106, \
  202. -100,   -94,   -87,   -81,   -75,   -69,   -63,   -56,   -50,   -44,   -38,   -31,   -25,   -19,   -13,    -6, \
  203. }

  204. #define REV_LUT_512                                                                                             \
  205. {                                                                                                               \
  206.     0,   256,   128,   384,    64,   320,   192,   448,    32,   288,   160,   416,    96,   352,   224,   480, \
  207.    16,   272,   144,   400,    80,   336,   208,   464,    48,   304,   176,   432,   112,   368,   240,   496, \
  208.     8,   264,   136,   392,    72,   328,   200,   456,    40,   296,   168,   424,   104,   360,   232,   488, \
  209.    24,   280,   152,   408,    88,   344,   216,   472,    56,   312,   184,   440,   120,   376,   248,   504, \
  210.     4,   260,   132,   388,    68,   324,   196,   452,    36,   292,   164,   420,   100,   356,   228,   484, \
  211.    20,   276,   148,   404,    84,   340,   212,   468,    52,   308,   180,   436,   116,   372,   244,   500, \
  212.    12,   268,   140,   396,    76,   332,   204,   460,    44,   300,   172,   428,   108,   364,   236,   492, \
  213.    28,   284,   156,   412,    92,   348,   220,   476,    60,   316,   188,   444,   124,   380,   252,   508, \
  214.     2,   258,   130,   386,    66,   322,   194,   450,    34,   290,   162,   418,    98,   354,   226,   482, \
  215.    18,   274,   146,   402,    82,   338,   210,   466,    50,   306,   178,   434,   114,   370,   242,   498, \
  216.    10,   266,   138,   394,    74,   330,   202,   458,    42,   298,   170,   426,   106,   362,   234,   490, \
  217.    26,   282,   154,   410,    90,   346,   218,   474,    58,   314,   186,   442,   122,   378,   250,   506, \
  218.     6,   262,   134,   390,    70,   326,   198,   454,    38,   294,   166,   422,   102,   358,   230,   486, \
  219.    22,   278,   150,   406,    86,   342,   214,   470,    54,   310,   182,   438,   118,   374,   246,   502, \
  220.    14,   270,   142,   398,    78,   334,   206,   462,    46,   302,   174,   430,   110,   366,   238,   494, \
  221.    30,   286,   158,   414,    94,   350,   222,   478,    62,   318,   190,   446,   126,   382,   254,   510, \
  222.     1,   257,   129,   385,    65,   321,   193,   449,    33,   289,   161,   417,    97,   353,   225,   481, \
  223.    17,   273,   145,   401,    81,   337,   209,   465,    49,   305,   177,   433,   113,   369,   241,   497, \
  224.     9,   265,   137,   393,    73,   329,   201,   457,    41,   297,   169,   425,   105,   361,   233,   489, \
  225.    25,   281,   153,   409,    89,   345,   217,   473,    57,   313,   185,   441,   121,   377,   249,   505, \
  226.     5,   261,   133,   389,    69,   325,   197,   453,    37,   293,   165,   421,   101,   357,   229,   485, \
  227.    21,   277,   149,   405,    85,   341,   213,   469,    53,   309,   181,   437,   117,   373,   245,   501, \
  228.    13,   269,   141,   397,    77,   333,   205,   461,    45,   301,   173,   429,   109,   365,   237,   493, \
  229.    29,   285,   157,   413,    93,   349,   221,   477,    61,   317,   189,   445,   125,   381,   253,   509, \
  230.     3,   259,   131,   387,    67,   323,   195,   451,    35,   291,   163,   419,    99,   355,   227,   483, \
  231.    19,   275,   147,   403,    83,   339,   211,   467,    51,   307,   179,   435,   115,   371,   243,   499, \
  232.    11,   267,   139,   395,    75,   331,   203,   459,    43,   299,   171,   427,   107,   363,   235,   491, \
  233.    27,   283,   155,   411,    91,   347,   219,   475,    59,   315,   187,   443,   123,   379,   251,   507, \
  234.     7,   263,   135,   391,    71,   327,   199,   455,    39,   295,   167,   423,   103,   359,   231,   487, \
  235.    23,   279,   151,   407,    87,   343,   215,   471,    55,   311,   183,   439,   119,   375,   247,   503, \
  236.    15,   271,   143,   399,    79,   335,   207,   463,    47,   303,   175,   431,   111,   367,   239,   495, \
  237.    31,   287,   159,   415,    95,   351,   223,   479,    63,   319,   191,   447,   127,   383,   255,   511, \
  238. }

  239. #endif
  240. #endif






 楼主| jiekou001 发表于 2023-6-20 16:30 | 显示全部楼层
  1. /**************************************************************************//**
  2. * @file     main.c
  3. * @version  V1.00
  4. * @brief    Perform Fast Fourier Transform with ADC samples.
  5. *
  6. * @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  7. *****************************************************************************/
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include "NuMicro.h"
  11. #include "fft_lut.h"

  12. /* Select the macro FFT_N_POINT_SEL defined in fft_lut.h.
  13. * The look-up table for fft algorithm requires user indicates specific data length.
  14. */

  15. /* System clock frequency */
  16. #define PLL_CLOCK                   (48000000)

  17. /* The parameter SAMPLE_RATE is configurable. It is based on your application.
  18. * The unit of SAMPLE_RATE is hertz.
  19. */
  20. #define SAMPLE_RATE                 (1024)

  21. /* The parameter SAMPLE_DATA_RSHIFT is configurable, the purpose is to truncate
  22. * the data size by right shifting the raw ADC data, and it can prevent the buffer
  23. * from overflow when calculating a large amount N point of FFT. The longer data
  24. * length is selected, the higher SAMPLE_DATA_RSHIFT is required. If the spectrum
  25. * buffer is overflow, try to increase the SAMPLE_DATA_RSHIFT parameter.
  26. */
  27. #define SAMPLE_DATA_RSHIFT          (5)
  28. #define SHRINK(x)                   ((x) >> SAMPLE_DATA_RSHIFT)


  29. /* Macros to simplify the math presentations for FFT algorithm */
  30. #define COS(angle_index)            (g_ai16cos[angle_index])
  31. #define SIN(angle_index)            (g_ai16sin[angle_index])
  32. #define REV(origin_index)           (g_ai16rev[origin_index])

  33. /*----------------------------------------------------------------------------------------------------------*/
  34. /* Define Function Prototypes                                                                               */
  35. /*----------------------------------------------------------------------------------------------------------*/
  36. void SYS_Init(void);
  37. void UART0_Init(void);
  38. void TIMER0_Init(void);
  39. void ADC_Init(void);
  40. void Remove_DC_Component(int *pi32DataBuffer, uint32_t u32Len);
  41. void NuFFT(int *pi32RealBuffer, int *pi32ImageBuffer, uint32_t u32Len);
  42. void Display_Spectrum(int *pi32Spectrum, uint32_t u32Len, uint32_t u32SampleRate);
  43. void Display_AdcData(int *pi32Data, uint32_t u32Len);

  44. /*----------------------------------------------------------------------------------------------------------*/
  45. /* Define global variables and constants                                                                    */
  46. /*----------------------------------------------------------------------------------------------------------*/
  47. const uint8_t g_u8AdcChannel = 2;

  48. /* Look-up table for FFT twiddle factors and the bit-reversal index */
  49. const int16_t g_ai16cos[SEQUENCE_LEN] = COS_LUT;
  50. const int16_t g_ai16sin[SEQUENCE_LEN] = SIN_LUT;
  51. const int16_t g_ai16rev[SEQUENCE_LEN] = REV_LUT;

  52. /* The data buffer for storing the ADC data samples and the output spectrum */
  53. int g_i32RealBuffer [SEQUENCE_LEN];
  54. int g_i32ImageBuffer[SEQUENCE_LEN];
  55. int g_i32Spectrum   [SEQUENCE_LEN];

  56. /* Global variables for handing ADC conversions */
  57. volatile uint32_t g_u32AdcConvNum = 0;


  58. /*----------------------------------------------------------------------------------------------------------*/
  59. /*  Function:    SYS_Init                                                                                   */
  60. /*  Parameters:  None.                                                                                      */
  61. /*  Returns:     None.                                                                                      */
  62. /*  Description: System and periphial module clock setting.                                                 */
  63. /*----------------------------------------------------------------------------------------------------------*/
  64. void SYS_Init(void)
  65. {
  66.     /* Enable HIRC clock (Internal RC 48MHz) */
  67.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  68.     /* Enable HIRC clock (Internal RC 48MHz) */
  69.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN);

  70.     /* Wait for HIRC clock ready */
  71.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  72.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  73.     /* Set core clock as PLL_CLOCK from PLL (From HXT if HXT is enabled)*/
  74.     CLK_SetCoreClock(PLL_CLOCK);

  75.     /* Enable UART module clock */
  76.     CLK_EnableModuleClock(UART0_MODULE);

  77.     /* Select UART module clock source as HIRC/2 and UART module clock divider as 1 */
  78.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HIRC_DIV2, CLK_CLKDIV0_UART(1));

  79.     /* Enable TMR0 module clock */
  80.     CLK_EnableModuleClock(TMR0_MODULE);

  81.     /* Set TMR0 module clock source as HIRC/2 */
  82.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, 0);

  83.     /* Enable ADC module clock */
  84.     CLK_EnableModuleClock(ADC_MODULE);

  85.     /* Set ADC clock source to PCLK0=48MHz, set divider to 3, ADC clock will be 16 MHz */
  86.     CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADCSEL_PCLK0, CLK_CLKDIV0_ADC(3));

  87.     /* Update core clock */
  88.     SystemCoreClockUpdate();


  89.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  90.     SYS->GPB_MFPL = SYS_GPB_MFPL_PB0MFP_UART0_RXD | SYS_GPB_MFPL_PB1MFP_UART0_TXD;

  91.     /* Set PD.2 and PD.3 to input mode */
  92.     PD->MODE &= ~(GPIO_MODE_MODE2_Msk | GPIO_MODE_MODE3_Msk);

  93.     /* Set PD2 and PD3 to ADC mode for ADC input channel 2 and 3 */
  94.     SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD2MFP_Msk | SYS_GPD_MFPL_PD3MFP_Msk);
  95.     SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD2MFP_ADC_CH2 | SYS_GPD_MFPL_PD3MFP_ADC_CH3);

  96.     /* Disable the digital input paths of ADC analog pins */
  97.     GPIO_DISABLE_DIGITAL_PATH(PD, BIT2 | BIT3);
  98. }

  99. /*----------------------------------------------------------------------------------------------------------*/
  100. /*  Function:    UART0_Init                                                                                 */
  101. /*  Parameters:  None.                                                                                      */
  102. /*  Returns:     None.                                                                                      */
  103. /*  Description: Initialize UART0 module, setting buadrate is 115200 bps                                    */
  104. /*----------------------------------------------------------------------------------------------------------*/
  105. void UART0_Init(void)
  106. {
  107.     /* Reset UART0 */
  108.     SYS_ResetModule(UART0_RST);

  109.     /* Configure UART0 and set UART0 baud rate */
  110.     UART_Open(UART0, 115200);
  111. }

  112. /*----------------------------------------------------------------------------------------------------------*/
  113. /*  Function:    TIMER0_Init                                                                                */
  114. /*  Parameters:  None.                                                                                      */
  115. /*  Returns:     None.                                                                                      */
  116. /*  Description: Initialize TIMER0 module. Setup timeout trigger ADC.                                       */
  117. /*----------------------------------------------------------------------------------------------------------*/
  118. void TIMER0_Init(void)
  119. {
  120.     uint32_t u32Freq;

  121.     /* Set TIMER0 working frequency to SAMPLE_RATE */
  122.     u32Freq = TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, SAMPLE_RATE);

  123.     if (u32Freq != SAMPLE_RATE)
  124.     {
  125.         printf("# Warning. Sample rate does not equal to the pre-defined setting.\n\r");
  126.     }

  127.     /* Enable timer timeout interrupt trigger ADC */
  128.     TIMER0->CTL |= TIMER_TRGSRC_TIMEOUT_EVENT | TIMER_TRG_TO_ADC | TIMER_CTL_INTEN_Msk;

  129.     printf(" CTL:0x%08X\n\r", TIMER0->CTL);
  130.     printf(" CMP:0x%08X\n\r", TIMER0->CMP);

  131. }

  132. /*----------------------------------------------------------------------------------------------------------*/
  133. /*  Function:    ADC_Init                                                                                   */
  134. /*  Parameters:  None.                                                                                      */
  135. /*  Returns:     None.                                                                                      */
  136. /*  Description: Initialize ADC module channel 2. Enable TIMER trigger ADC function.                        */
  137. /*----------------------------------------------------------------------------------------------------------*/
  138. void ADC_Init(void)
  139. {
  140.     /* Set the ADC operation mode as single, input mode as single-end and enable the analog input channel 2 */
  141.     ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_SINGLE, 0x1 << g_u8AdcChannel);

  142.     /* Enable TIMER Trigger ADC */
  143.     ADC_EnableTimerTrigger(ADC, ADC_ADCR_TRGS_TIMER, 0);

  144.     /* Power on ADC module */
  145.     ADC_POWER_ON(ADC);

  146.     /* Clear the A/D interrupt flag for safe */
  147.     ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

  148.     /* Enable the ADC interrupt */
  149.     ADC_EnableInt(ADC, ADC_ADF_INT);
  150.     NVIC_EnableIRQ(ADC_IRQn);
  151. }

  152. /*----------------------------------------------------------------------------------------------------------*/
  153. /*  Function:    Remove_DC_Component                                                                        */
  154. /*  Parameters:  pi32DataBuffer is the raw data buffer pointer.                                             */
  155. /*  Parameters:  u32Len is the length of the raw data buffer.                                               */
  156. /*  Returns:     None.                                                                                      */
  157. /*  Description: None.                                                                                      */
  158. /*----------------------------------------------------------------------------------------------------------*/
  159. void Remove_DC_Component(int *pi32DataBuffer, uint32_t u32Len)
  160. {
  161.     uint32_t i, u32Mean, u32Accumulate = 0;

  162.     for (i = 0; i < u32Len; i++)
  163.     {
  164.         u32Accumulate += pi32DataBuffer[i];
  165.     }

  166.     u32Mean = u32Accumulate / u32Len;

  167.     for (i = 0; i < u32Len; i++)
  168.     {
  169.         pi32DataBuffer[i] -= u32Mean;
  170.     }
  171. }

  172. /*----------------------------------------------------------------------------------------------------------*/
  173. /*  Function:    NuFFT                                                                                      */
  174. /*  Parameters:  pi32RealBuffer  is the pointer to the real  part of the data buffer.                       */
  175. /*  Parameters:  pi32ImageBuffer is the pointer to the image part of the data buffer.                       */
  176. /*  Parameters:  u32Len indicates the length of the data sequence.                                          */
  177. /*  Returns:     None.                                                                                      */
  178. /*  Description: Fast Fourier Transform Algorithm. Transform the time-domain data                           */
  179. /*               sequence into a frequency-domain spectrum.                                                 */
  180. /*----------------------------------------------------------------------------------------------------------*/
  181. void NuFFT(int *pi32RealBuffer, int *pi32ImageBuffer, uint32_t u32Len)
  182. {
  183.     const uint32_t N = u32Len;
  184.     const uint32_t u32TotalStage = log(N) / log(2);

  185.     uint32_t u32Stage, u32Span, u32Node, u32Twiddle, u32Angle;
  186.     int32_t i32X1Real, i32X1Image, i32X2Real, i32X2Image;

  187.     /* Iterations for log2(N) FFT butterfly stages */
  188.     for (u32Stage = 0; u32Stage < u32TotalStage; u32Stage++)
  189.     {
  190.         /* Span indicates the buffer index for constituting a butterfly matrix */
  191.         u32Span = pow(2, u32Stage);

  192.         for (u32Twiddle = 0; u32Twiddle < u32Span; u32Twiddle++)
  193.         {
  194.             u32Angle = (N >> 1) / u32Span * u32Twiddle;

  195.             for (u32Node = u32Twiddle; u32Node < N; u32Node += 2 * u32Span)
  196.             {
  197.                 /* Get the real and image part of the input variable X1 */
  198.                 i32X1Real  = pi32RealBuffer [u32Node];
  199.                 i32X1Image = pi32ImageBuffer[u32Node];

  200.                 /* Get the real and image part of the input variable X2 */
  201.                 i32X2Real  = pi32RealBuffer [u32Node + u32Span];
  202.                 i32X2Image = pi32ImageBuffer[u32Node + u32Span];

  203.                 /* Y1 = X1 + X2 * twiddle factor
  204.                  * The real part is   real * cos() + image * sin()
  205.                  * The image part is -real * sin() + image * cos()
  206.                  */
  207.                 pi32RealBuffer [u32Node] = i32X1Real  + RESCALE(i32X2Real * COS(u32Angle)) + RESCALE(i32X2Image * SIN(u32Angle));
  208.                 pi32ImageBuffer[u32Node] = i32X1Image - RESCALE(i32X2Real * SIN(u32Angle)) + RESCALE(i32X2Image * COS(u32Angle));

  209.                 /* Y2 = X1 - X2 * twiddle factor
  210.                  * The real part is  -real * cos() - image * sin()
  211.                  * The image part is  real * sin() - image * cos()
  212.                  */
  213.                 pi32RealBuffer [u32Node + u32Span] = i32X1Real  - RESCALE(i32X2Real * COS(u32Angle)) - RESCALE(i32X2Image * SIN(u32Angle));
  214.                 pi32ImageBuffer[u32Node + u32Span] = i32X1Image + RESCALE(i32X2Real * SIN(u32Angle)) - RESCALE(i32X2Image * COS(u32Angle));
  215.             }
  216.         }
  217.     }
  218. }

  219. /*----------------------------------------------------------------------------------------------------------*/
  220. /*  Function:    Display_Spectrum                                                                           */
  221. /*  Parameters:  pi32Spectrum is the spectrum buffer pointer.                                               */
  222. /*  Parameters:  u32Len is the length of the spectrum buffer.                                               */
  223. /*  Parameters:  u32SampleRate is the frequency of TIMER trigger EADC.                                      */
  224. /*  Returns:     none.                                                                                      */
  225. /*  Description: Show the detail informations from the spectrum bffer.                                      */
  226. /*----------------------------------------------------------------------------------------------------------*/
  227. void Display_Spectrum(int *pi32Spectrum, uint32_t u32Len, uint32_t u32SampleRate)
  228. {
  229.     const float  fconstResolution = (float)u32SampleRate / (float)u32Len;

  230.     int i, i32Base, i32Accumulate = 0;

  231.     printf("+-----------------------------------------------------------+\n\r");
  232.     printf("Spectrum Parameters         \n\r");
  233.     printf("# Sample rate: %d Hz        \n\r", u32SampleRate);
  234.     printf("# Data length: %d samples   \n\r", u32Len);
  235.     printf("# Resolution : %.2f Hz      \n\r", fconstResolution);
  236.     printf("+-----------------------------------------------------------+\n\r");
  237.     printf("#Index     #Frequency(Hz)     #Amplitude     #Ratio \n\r");

  238.     for (i = 0; i < u32Len; i++)
  239.     {
  240.         i32Accumulate += pi32Spectrum[i];
  241.     }

  242.     for (i = 0; i < u32Len; i++)
  243.     {
  244.         i32Base = (i <= u32Len / 2) ? (i) : (i - u32Len);

  245.         printf(" %-7d    %-15.2f    %-11d    %.2f%%\n\r", i, (float)(fconstResolution * i32Base), pi32Spectrum[i], (float)pi32Spectrum[i] / (float)i32Accumulate * 100);

  246.     }

  247.     printf("+-----------------------------------------------------------+\n\r");
  248.     printf("FFT complete\n\r");
  249. }

  250. /*----------------------------------------------------------------------------------------------------------*/
  251. /*  Function:    Display_AdcData                                                                            */
  252. /*  Parameters:  pi32Spectrum is the Time Domian Data buffer pointer.                                       */
  253. /*  Parameters:  u32Len is the length of the Data.                                                          */
  254. /*  Returns:     none.                                                                                      */
  255. /*  Description: Show the detail informations from the spectrum bffer.                                      */
  256. /*----------------------------------------------------------------------------------------------------------*/
  257. void Display_AdcData(int *pi32Data, uint32_t u32Len)
  258. {
  259.     int i;
  260.     printf("+-----------------------------------------------------------+\n\r");
  261.     printf("| Display Time-Domain Data                                  +\n\r");
  262.     printf("+-----------------------------------------------------------+\n\r");
  263.     printf("#Index before reversal    #Index after reversal    #ADC Data \n\r");

  264.     for (i = 0; i < u32Len; i++)
  265.     {
  266.         printf(" %-22d    %-21d    %d\n\r", i, REV(i), pi32Data[REV(i)]);
  267.     }
  268. }

  269. /*----------------------------------------------------------------------------------------------------------*/
  270. /*  Function:    ADC_IRQHandler                                                                             */
  271. /*  Parameters:  None.                                                                                      */
  272. /*  Returns:     None.                                                                                      */
  273. /*  Description: Interrupt service routine for EADC interrupt.                                              */
  274. /*----------------------------------------------------------------------------------------------------------*/
  275. void ADC_IRQHandler(void)
  276. {
  277.     /* clear the A/D conversion flag */
  278.     ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

  279.     /* Toggle PB.0 to indicate an ADC data conversion is complete */
  280.     PB0 ^= 1;

  281.     /* Place the time-domain signal into the real part buffer */
  282.     g_i32RealBuffer [REV(g_u32AdcConvNum)] = SHRINK(ADC_GET_CONVERSION_DATA(ADC, g_u8AdcChannel));

  283.     /* The image part buffer is initialized to zeros */
  284.     g_i32ImageBuffer[REV(g_u32AdcConvNum)] = 0;

  285.     /* Increase the buffer index by 1 */
  286.     g_u32AdcConvNum++;
  287. }

  288. /*----------------------------------------------------------------------------------------------------------*/
  289. /*  Function:    main                                                                                       */
  290. /*  Parameters:  None.                                                                                      */
  291. /*  Returns:     None.                                                                                      */
  292. /*  Description: main routine of the example.                                                               */
  293. /*----------------------------------------------------------------------------------------------------------*/
  294. int32_t main(void)
  295. {
  296.     int i;

  297.     /* Unlock protected registers */
  298.     SYS_UnlockReg();

  299.     /* Init System, peripheral clock and multi-function I/O */
  300.     SYS_Init();

  301.     /* Lock protected registers */
  302.     SYS_LockReg();

  303.     /* Init UART0 for printf */
  304.     UART0_Init();

  305.     /* Init TIMER0 to trigger ADC sampling later. */
  306.     TIMER0_Init();

  307.     /* Init ADC channel 2.  */
  308.     ADC_Init();

  309.     printf("\n");
  310.     printf("+-----------------------------------------------------------+\n\r");
  311.     printf("|            Fast Fourier Transform Example Code            |\n\r");
  312.     printf("+-----------------------------------------------------------+\n\n\r");
  313.     printf("# Demonstrate the decimate-in-time(DIT) radix-2 FFT          \n\r");
  314.     printf("# This example code supports 3 kinds of data length:         \n\r");
  315.     printf("    * 128 samples                                            \n\r");
  316.     printf("    * 256 samples                                            \n\r");
  317.     printf("    * 512 samples                                            \n\r");
  318.     printf("# Please confirm the selected length of fft sequence is [%d].\n\r", SEQUENCE_LEN);
  319.     printf("    Otherwise, change the fft sequence length settings and   \n\r");
  320.     printf("    re-compile again to acquire the correct look-up table.   \n\r");
  321.     printf("# Connect PD.2 to the signal source.                         \n\r");
  322.     printf("# Press any key to start ADC capturing....\n\n\r");
  323.     getchar();


  324.     /* Reset the ADC numbers of conversion and Start A/D conversion */
  325.     g_u32AdcConvNum = 0;

  326.     PB0 = 0;
  327.     GPIO_SetMode(PB, BIT0, GPIO_MODE_OUTPUT);

  328.     /* Start capturing data. */
  329.     TIMER_Start(TIMER0);

  330.     while (g_u32AdcConvNum != SEQUENCE_LEN)
  331.     {
  332.         /* Wait for the number of ADC data samples reach SEQUENCE_LEN samples. */
  333.         __NOP();
  334.     }

  335.     /* Stop TIMER trigger EADC */
  336.     TIMER_Stop(TIMER0);

  337.     /* Disable EDC module */
  338.     ADC_Close(ADC);

  339.     /* Disable ADC IP clock */
  340.     CLK_DisableModuleClock(ADC_MODULE);

  341.     /* Disable TIMER0 IP clock */
  342.     CLK_DisableModuleClock(TMR0_MODULE);

  343.     /* Disable External Interrupt */
  344.     NVIC_DisableIRQ(ADC_IRQn);

  345.     Display_AdcData(g_i32RealBuffer, SEQUENCE_LEN);

  346.     /* Remove the DC component from the spectrum by zero-mean the time-domain signal before
  347.        performing FFT. Otherwise, it will remain a high DC component at the 0 Hz point. */
  348.     Remove_DC_Component(g_i32RealBuffer, SEQUENCE_LEN);

  349.     /* Transform the time domain signal into frequency domain */
  350.     NuFFT(g_i32RealBuffer, g_i32ImageBuffer, SEQUENCE_LEN);

  351.     for (i = 0; i < SEQUENCE_LEN ; i++)
  352.     {
  353.         /* The amplitude of the spectrum could be presented as the sum of square of the real part and the complex part. */
  354.         g_i32Spectrum[i] = (((g_i32RealBuffer[i] * g_i32RealBuffer[i]) + (g_i32ImageBuffer[i] * g_i32ImageBuffer[i])));
  355.     }

  356.     Display_Spectrum(g_i32Spectrum, SEQUENCE_LEN, SAMPLE_RATE);

  357.     while (1);
  358. }


youtome 发表于 2023-7-8 17:59 | 显示全部楼层
FFT算法要求输入数据长度为2的幂次方。如果输入数据长度不符合要求,需要进行补零或截断操作。
vivilyly 发表于 2023-7-8 18:28 | 显示全部楼层
选择适当的数据类型和精度来存储输入数据和计算结果。
nomomy 发表于 2023-7-9 10:07 | 显示全部楼层
在实现 FFT 算法时,需要根据具体的应用需求和硬件条件,选择合适的算法实现方式和开发工具
nomomy 发表于 2023-7-9 13:40 | 显示全部楼层
可以利用多线程或并行处理器来加速计算过程。
benjaminka 发表于 2023-7-9 14:25 | 显示全部楼层
如果采样点数太少,会导致 FFT 算法的效率和准确性降低;如果采样点数太多,会导致 FFT 算法的计算量增加,从而影响算法的效率。
gygp 发表于 2023-7-9 15:30 | 显示全部楼层
FFT算法是一种高效的算法,但在具体实现中,要注意优化计算过程以降低时间复杂度。
yeates333 发表于 2023-7-9 16:32 | 显示全部楼层
在实现 FFT 算法时,可以通过调整窗口大小来改善算法的效率和准确性。
bestwell 发表于 2023-7-10 14:02 | 显示全部楼层
FFT算法在计算过程中可能会引入一定的数值误差。要注意处理舍入误差、截断误差和级联误差等问题
plsbackup 发表于 2023-7-10 14:47 | 显示全部楼层
非常耗时的算法,可以采用并行计算的方式来提高算法的效率
abotomson 发表于 2023-7-10 15:41 | 显示全部楼层
在应用FFT算法之前,通常需要对输入数据进行预处理。这可能包括零填充、加窗(如汉宁窗)等操作,以减少频谱泄漏和提高频谱分辨率。
ccook11 发表于 2023-7-10 16:44 | 显示全部楼层
FFT算法可以通过递归或迭代的方式进行实现。递归实现通常较为简洁,但可能会占用较多的栈空间。
mmbs 发表于 2023-7-10 17:23 | 显示全部楼层
FFT是一种高效的算法,用于将时域信号转换为频域信号。了解算法的基本原理和步骤是非常重要的。
hudi008 发表于 2023-7-10 18:11 | 显示全部楼层
FFT有多种实现方式,如Cooley-Tukey算法、Radix-2算法等。
kkzz 发表于 2023-7-10 18:45 | 显示全部楼层
FFT算法在计算结果中可能存在舍入误差或截断误差。在应用中需要考虑这些误差对结果的影响,并选择适当的精度和误差控制策略。
plsbackup 发表于 2023-7-10 19:18 | 显示全部楼层
FFT算法中使用了复数运算,需要确保编程语言或工具支持复数类型。在实现过程中,要注意正确地进行复数的加法、减法、乘法和除法等运算。
ulystronglll 发表于 2023-7-10 20:03 | 显示全部楼层
合理选择算法参数和调整算法细节,可以提高计算效率。

mmbs 发表于 2023-7-10 20:43 | 显示全部楼层
在实现FFT算法后,进行验证和测试是必要的。
sdCAD 发表于 2023-7-10 21:18 | 显示全部楼层
FFT算法函数的实现需要注意性能和精度的平衡,尤其是涉及到浮点数运算时需要注意精度误差。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

147

主题

1539

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部