[MCU] Qmath做FFT

[复制链接]
1088|22
 楼主| xuanhuanzi 发表于 2019-10-31 23:04 | 显示全部楼层 |阅读模式
fft, AN, TI, se, TE
  1. /* --COPYRIGHT--,BSD
  2. * Copyright (c) 2015, Texas Instruments Incorporated
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * *  Redistributions of source code must retain the above copyright
  10. *    notice, this list of conditions and the following disclaimer.
  11. *
  12. * *  Redistributions in binary form must reproduce the above copyright
  13. *    notice, this list of conditions and the following disclaimer in the
  14. *    documentation and/or other materials provided with the distribution.
  15. *
  16. * *  Neither the name of Texas Instruments Incorporated nor the names of
  17. *    its contributors may be used to endorse or promote products derived
  18. *    from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. * --/COPYRIGHT--*/
  32. //*****************************************************************************
  33. // QmathLib_signal_FFT_ex4: Qmath signal generator and complex FFT example.
  34. //
  35. // Generate an input signal based on an array of wave descriptors. Each wave
  36. // descriptor is composed of a frequency, amplitude and phase angle. The
  37. // input signal is constructed with a size of SAMPLES and assumes a sample
  38. // frequency defined by SAMPLE_FREQUENCY. The real component of the input
  39. // consists of the summation of all the waves at that time index and the
  40. // imaginary component is set to zero.
  41. //
  42. // The input array is passed into the complex FFT function which performs
  43. // the FFT in-place using radix-2. The result of the cFFT is stored in the
  44. // input array and is scaled by SAMPLES.
  45. //
  46. // The result is used to calculate the magnitude and phase angle at each
  47. // frequency bin up to SAMPLES/2 (Nyquist frequency). The magnitude and phase
  48. // angles are stored in data memory and should approximate the original
  49. // signal composition. Because the input signal did not have any imaginary
  50. // components the magnitude will be halved. The results can be printed with
  51. // the printf function if ALLOW_PRINTF is defined.
  52. //
  53. // B. Peterson
  54. // Texas Instruments Inc.
  55. // May 2014
  56. // Built with CCS version 6.0.0.00190 and IAR Embedded Workbench version 6.10.1
  57. //*****************************************************************************
  58. #include "msp430.h"
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <stdint.h>

  62. /* Select the global Q value and include the Qmath header file. */
  63. #define GLOBAL_Q    12
  64. #include "QmathLib.h"

  65. /* Specify the sample size and sample frequency. */
  66. #define SAMPLES                 64      // <= 256, power of 2
  67. #define SAMPLE_FREQUENCY        8192    // <= 16384

  68. /* Access the real and imaginary parts of an index into a complex array. */
  69. #define RE(x)           (((x)<<1)+0)    // access real part of index
  70. #define IM(x)           (((x)<<1)+1)    // access imaginary part of index

  71. /*
  72. * Input and result buffers. These can be viewed in memory or printed by
  73. * defining ALLOW_PRINTF.
  74. */
  75. _q qInput[SAMPLES*2];                   // Input buffer of complex values
  76. _q qMag[SAMPLES/2];                     // Magnitude of each frequency result
  77. _q qPhase[SAMPLES/2];                   // Phase of each frequency result

  78. /* Misc. definitions. */
  79. #define PI      3.1415926536

  80. /* Structure that describes a single wave to be used to construct the signal */
  81. typedef struct wave {
  82.     int16_t     frequency;              // Frequency in Hz
  83.     _q          amplitude;              // Amplitude of the signal
  84.     _q          phase;                  // Phase angle in radians
  85. } wave;

  86. /*
  87. * Specify wave structures that will be used to construct the input signal to
  88. * the complex FFT function.
  89. */
  90. const wave signals[] = {
  91. /*   Frequency (Hz)     Magnitude       Phase angle (radians) */
  92.     {128,               _Q(0.5),        _Q(PI/2)},
  93.     {512,               _Q(2.0),        _Q(0)},
  94.     {2048,              _Q(1.333),      _Q(-PI/2)}
  95. };

  96. /* Calculate the number of wave structures that have been provided. */
  97. #define NUM_WAVES       (sizeof(signals)/sizeof(wave))

  98. //#define ALLOW_PRINTF                    // allow usage of printf to print results
  99. #ifdef ALLOW_PRINTF
  100.     char cMagBuffer[10];                // Character buffer for printing magnitude
  101.     char cPhaseBuffer[10];              // Character buffer for printing phase
  102.     char cFrequencyBuffer[10];          // Character buffer for printing frequency
  103. #endif

  104. extern void cFFT(_q *input, int16_t n);

  105. int main(void)
  106. {
  107.     int16_t i, j;                       // loop counters
  108.     _q qWaveCurrentAngle[NUM_WAVES];    // input angles for each signal
  109.    
  110.     /* Disable WDT. */
  111.     WDTCTL = WDTPW + WDTHOLD;
  112.    
  113.     /* Set the initial input angles. */
  114.     for (i = 0; i < NUM_WAVES; i++) {
  115.         qWaveCurrentAngle[i] = signals[i].phase;
  116.     }
  117.    
  118.     /* Construct the input signal from the wave structures. */
  119.     for (i = 0; i < SAMPLES; i++) {
  120.         qInput[RE(i)] = 0;
  121.         qInput[IM(i)] = 0;
  122.         for (j = 0; j < NUM_WAVES; j++) {
  123.             /*
  124.              * input[RE] += cos(angle)*amplitude
  125.              * angle += 2*pi*freq/sample_freq
  126.              */
  127.             qInput[RE(i)] += _Qmpy(_Qcos(qWaveCurrentAngle[j]), signals[j].amplitude);
  128.             qWaveCurrentAngle[j] += _Qmpy(_Q(2*PI), _Qdiv(signals[j].frequency, SAMPLE_FREQUENCY));
  129.             if (qWaveCurrentAngle[j] > _Q(PI)) {
  130.                 qWaveCurrentAngle[j] -= _Q(2*PI);
  131.             }
  132.         }
  133.     }
  134.    
  135.     /*
  136.      * Perform a complex FFT on the input samples. The result is calculated
  137.      * in-place and will be stored in the input buffer.
  138.      */
  139.     cFFT(qInput, SAMPLES);
  140.    
  141.     /* Calculate the magnitude and phase angle of the results. */
  142.     for (i = 0; i < SAMPLES/2; i++) {
  143.         qMag[i] = _Qmag(qInput[RE(i)], qInput[IM(i)]);
  144.         qPhase[i] = _Qatan2(qInput[IM(i)], qInput[RE(i)]);
  145.     }
  146.    
  147.     /* Print the results. */
  148. #ifdef ALLOW_PRINTF
  149.     for (i = 0; i < SAMPLES/2; i++) {
  150.         _Qtoa(cMagBuffer, "%2.4f", qMag[i]);
  151.         _Qtoa(cPhaseBuffer, "%2.4f", qPhase[i]);
  152.         _Q1toa(cFrequencyBuffer, "%5.0f", _Q1mpyI16(_Q1(SAMPLE_FREQUENCY/SAMPLES), i));
  153.         printf("%sHz: mag = %s, phase = %s radians\n",
  154.                cFrequencyBuffer, cMagBuffer, cPhaseBuffer);
  155.     }
  156. #endif
  157.    
  158.     return 0;
  159. }

  160. extern void cBitReverse(_q *input, int16_t n);

  161. /*
  162. * Perform in-place radix-2 DFT of the input signal with size n.
  163. *
  164. * This function has been written for any input size up to 256. This function
  165. * can be optimized by using lookup tables with precomputed twiddle factors for
  166. * a fixed sized FFT, using Q15 format for the twiddle factors and inlining the
  167. * multiplication steps with direct access to the MPY32 hardware peripheral.
  168. */
  169. void cFFT(_q *input, int16_t n)
  170. {
  171.     int16_t s, s_2;                     // step
  172.     uint16_t i, j;                      // loop counters
  173.     _q qTAngle;                         // twiddle factor angle
  174.     _q qTIncrement;                     // twiddle factor increment
  175.     _q qTCos, qTSin;                    // complex components of twiddle factor
  176.     _q qTempR, qTempI;                  // temp result complex pair
  177.    
  178.     /* Bit reverse the order of the inputs. */
  179.     cBitReverse(input, n);
  180.    
  181.     /* Set step to 2 and initialize twiddle angle increment. */
  182.     s = 2;
  183.     s_2 = 1;
  184.     qTIncrement = _Q(-2*PI);
  185.    
  186.     while (s <= n) {
  187.         /* Reset twiddle angle and halve increment factor. */
  188.         qTAngle = 0;
  189.         qTIncrement = _Qdiv2(qTIncrement);
  190.         
  191.         for (i = 0; i < s_2; i++) {
  192.             /* Calculate twiddle factor complex components. */
  193.             qTCos = _Qcos(qTAngle);
  194.             qTSin = _Qsin(qTAngle);
  195.             qTAngle += qTIncrement;
  196.             
  197.             for (j = i; j < n; j += s) {
  198.                 /* Multiply complex pairs and scale each stage. */
  199.                 qTempR = _Qmpy(qTCos, input[RE(j+s_2)]) - _Qmpy(qTSin, input[IM(j+s_2)]);
  200.                 qTempI = _Qmpy(qTSin, input[RE(j+s_2)]) + _Qmpy(qTCos, input[IM(j+s_2)]);
  201.                 input[RE(j+s_2)] = _Qdiv2(input[RE(j)] - qTempR);
  202.                 input[IM(j+s_2)] = _Qdiv2(input[IM(j)] - qTempI);
  203.                 input[RE(j)] = _Qdiv2(input[RE(j)] + qTempR);
  204.                 input[IM(j)] = _Qdiv2(input[IM(j)] + qTempI);
  205.             }
  206.         }
  207.         /* Multiply step by 2. */
  208.         s_2 = s;
  209.         s = _Qmpy2(s);
  210.     }
  211. }

  212. /*
  213. * Perform an in-place bit reversal of the complex input array with size n.
  214. * Use a look up table to speed up the process. Valid for size of 256 and
  215. * smaller.
  216. */
  217. void cBitReverse(_q *input, int16_t n)
  218. {
  219.     uint16_t i, j;                      // loop counters
  220.     int16_t i16BitRev;                  // index bit reversal
  221.     _q qTemp;
  222.    
  223.     extern const uint8_t ui8BitRevLUT[256];
  224.    
  225.     /* In-place bit-reversal. */
  226.     for (i = 0; i < n; i++) {
  227.         i16BitRev = ui8BitRevLUT[i];
  228.         for (j = n; j < 256; j <<= 1) {
  229.             i16BitRev >>= 1;
  230.         }
  231.         if (i < i16BitRev) {
  232.             /* Swap inputs. */
  233.             qTemp = input[RE(i)];
  234.             input[RE(i)] = input[RE(i16BitRev)];
  235.             input[RE(i16BitRev)] = qTemp;
  236.             qTemp = input[IM(i)];
  237.             input[IM(i)] = input[IM(i16BitRev)];
  238.             input[IM(i16BitRev)] = qTemp;
  239.         }
  240.     }
  241. }

  242. /* 8-bit reversal lookup table. */
  243. const uint8_t ui8BitRevLUT[256] = {
  244.     0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
  245.     0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
  246.     0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
  247.     0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
  248.     0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
  249.     0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
  250.     0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
  251.     0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
  252.     0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
  253.     0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
  254.     0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
  255.     0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
  256.     0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
  257.     0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
  258.     0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
  259.     0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
  260. };


deadtime 发表于 2019-11-2 16:36 | 显示全部楼层
感谢楼主分享!
gygp 发表于 2019-11-15 13:17 | 显示全部楼层
有没有会用TI提供的fftlib库的?  
chenci2013 发表于 2019-11-15 13:18 | 显示全部楼层

请问用DSP编写FFT要怎么做?
biechedan 发表于 2019-11-15 13:18 | 显示全部楼层
DSP计算512点fft到底需要多少时间  
wangdezhi 发表于 2019-11-15 13:18 | 显示全部楼层
如何用matlab实现fft   
isseed 发表于 2019-11-15 13:20 | 显示全部楼层
这个主要是看速度 。      
xietingfeng 发表于 2019-11-15 13:20 | 显示全部楼层
28335 做2048点FFT花多少时间?  
suzhanhua 发表于 2019-11-15 13:21 | 显示全部楼层
如何用FFT得到谐波幅值频率和相位  
mituzu 发表于 2019-11-15 13:21 | 显示全部楼层
为什么FFT需要做倒序运算  
hellosdc 发表于 2019-11-15 13:21 | 显示全部楼层
FFT的定点DSP怎样实现的?
uiint 发表于 2019-11-15 13:21 | 显示全部楼层
FFT和FFT的反变换代码?     
suzhanhua 发表于 2019-11-15 13:22 | 显示全部楼层
FFT里stb.h头文件怎么找  
gygp 发表于 2019-11-15 13:22 | 显示全部楼层
有没有人能够提供自己写的例程  
xietingfeng 发表于 2019-11-15 13:22 | 显示全部楼层
如何使用CCS编程实现FFT   
chenci2013 发表于 2019-11-15 13:22 | 显示全部楼层
听说TI有FFT库函数   
isseed 发表于 2019-11-15 13:22 | 显示全部楼层
Qmath做FFT可行吗?     
biechedan 发表于 2019-11-15 13:22 | 显示全部楼层
用FPGA实现FFT?      
wangdezhi 发表于 2019-11-15 13:22 | 显示全部楼层
如何使用DSP里面的DSP算法库FFT和IFFT  
uiint 发表于 2019-11-15 13:22 | 显示全部楼层
FFT已经听闻已久了   
您需要登录后才可以回帖 登录 | 注册

本版积分规则

183

主题

2331

帖子

3

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