DSP TMS320C6657中FFT函数的调用与具体使用方法

首先明确一点,C6678中FFT的函数形式为:
在这里插入图片描述

N : length of FFT in complex samples ptr_x : pointer to complex data
input ptr_w : pointer to complex twiddle factor ptr_y : pointer to
complex output data brev : pointer to bit reverse table containing 64
entries n_min : should be 4 if N can be represented as Power of 4
else, n_min should be 2 offset : index in complex samples of sub-fft
from start of main fft n_max : size of main fft in complex
samples(可令n_max=N)

其中,64位bit反位表brev为: unsigned char brev[64] = { 0x00, 0x20, 0x10, 0x30,
0x08, 0x28, 0x18, 0x38, 0x04, 0x24, 0x14, 0x34, 0x0c, 0x2c, 0x1c,
0x3c, 0x02, 0x22, 0x12, 0x32, 0x0a, 0x2a, 0x1a, 0x3a, 0x06, 0x26,
0x16, 0x36, 0x0e, 0x2e, 0x1e, 0x3e, 0x01, 0x21, 0x11, 0x31, 0x09,
0x29, 0x19, 0x39, 0x05, 0x25, 0x15, 0x35, 0x0d, 0x2d, 0x1d, 0x3d,
0x03, 0x23, 0x13, 0x33, 0x0b, 0x2b, 0x1b, 0x3b, 0x07, 0x27, 0x17,
0x37, 0x0f, 0x2f, 0x1f, 0x3f };

旋转矩阵(旋转因子)ptr_w为: void tw_fft_gen (float *w, int n) { int i, j, k; for
(j = 1, k = 0; j <= n >> 2; j = j << 2) { for (i = 0; i < n >> 2; i +=
j) {
#ifdef _LITTLE_ENDIAN w[k] = (float) sin (2 * PI * i / n); w[k + 1] = (float) cos (2 * PI * i / n); w[k + 2] = (float) sin (4 * PI * i / n);
w[k + 3] = (float) cos (4 * PI * i / n); w[k + 4] = (float) sin (6 *
PI * i / n); w[k + 5] = (float) cos (6 * PI * i / n);
#else w[k] = (float) cos (2 * PI * i / n); w[k + 1] = (float) -sin (2 * PI * i / n); w[k + 2] = (float) cos (4 * PI * i / n); w[k + 3] = (float) -sin (4 * PI * i / n); w[k + 4] = (float) cos (6 * PI * i /
n); w[k + 5] = (float) -sin (6 * PI * i / n);
#endif k += 6; } } }

DSPF_sp_fftSPxSP函数的调用方式为:
1.下载库

将下载好的库“dsplib_c66x_3_1_0_0”复制到CCS的安装路径下

2.添加头文件路径 右键点击工程–>Properties–>Build–>c6000 Compiler–>Include Options 在这里插入图片描述

3.添加库文件和库文件路径 右键点击工程–>Properties–>Build–>C6000 Linker–>File Search Path添加库文件和库文件路径
在这里插入图片描述

完成以上的准备工作后便可进行DSPF_sp_fftSPxSP函数的调用了。

在DSPC6657上的实际测试程序

#include <stdio.h>
#include <stdlib.h>
#include <c6x.h>
#include <math.h>
#include <dsplib.h>

#define FFT_NUM                                1024             //单次FFT变换数据个数
#define Double_FFT_NUM                  FFT_NUM * 2 //单次FFT变换数据个数的两倍
#define PI                                             3.141592654  //圆周率

float sin_data[1024] =
{
 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, -0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000,
 -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809,
 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309,
 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309,
 -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809,
 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000,
 -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, -0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809,
 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309,
 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309,
 -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, -0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809,
 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000,
 -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, -0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809,
 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309,
 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309,
 -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, -0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, -0.000, 0.309, 0.588, 0.809,
 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000,
 -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809,
 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309,
 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, -0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309,
 -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, -0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, -0.000, 0.309, 0.588, 0.809,
 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000,
 -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809,
 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, -0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309,
 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309,
 -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809,
 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000,
 -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, -0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809,
 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309,
 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309,
 -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809,
 1.000, 0.951, 0.809, 0.588, 0.309, 0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, -0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000,
 -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809, 0.588, 0.309, -0.000, -0.309, -0.588, -0.809, -0.951, -1.000, -0.951, -0.809, -0.588, -0.309, 0.000, 0.309, 0.588, 0.809, 0.951, 1.000, 0.951, 0.809

};

//bit reverse table containing 64 entries 64位bit反位表 ,倒置数组
unsigned char brev[64] =
{
    0x00, 0x20, 0x10, 0x30, 0x08, 0x28, 0x18, 0x38,
    0x04, 0x24, 0x14, 0x34, 0x0c, 0x2c, 0x1c, 0x3c,
    0x02, 0x22, 0x12, 0x32, 0x0a, 0x2a, 0x1a, 0x3a,
    0x06, 0x26, 0x16, 0x36, 0x0e, 0x2e, 0x1e, 0x3e,
    0x01, 0x21, 0x11, 0x31, 0x09, 0x29, 0x19, 0x39,
    0x05, 0x25, 0x15, 0x35, 0x0d, 0x2d, 0x1d, 0x3d,
    0x03, 0x23, 0x13, 0x33, 0x0b, 0x2b, 0x1b, 0x3b,
    0x07, 0x27, 0x17, 0x37, 0x0f, 0x2f, 0x1f, 0x3f
};

// data array
float fft_output[Double_FFT_NUM];   //FFT变换输出数据
float fft_input[Double_FFT_NUM];     //FFT变换输入数据
float twiddle[Double_FFT_NUM];       //旋转矩阵(因子)(复数)

//旋转因子(复数)
void tw_fft_gen (float *w, int n)
{
int i, j, k;
for (j = 1, k = 0; j <= n >> 2; j = j << 2)
{
for (i = 0; i < n >> 2; i += j)
{
#ifdef _LITTLE_ENDIAN
w[k] = (float) sin (2 * PI * i / n);
w[k + 1] = (float) cos (2 * PI * i / n);
w[k + 2] = (float) sin (4 * PI * i / n);
w[k + 3] = (float) cos (4 * PI * i / n);
w[k + 4] = (float) sin (6 * PI * i / n);
w[k + 5] = (float) cos (6 * PI * i / n);
#else
w[k] = (float) cos (2 * PI * i / n);
w[k + 1] = (float) -sin (2 * PI * i / n);
w[k + 2] = (float) cos (4 * PI * i / n);
w[k + 3] = (float) -sin (4 * PI * i / n);
w[k + 4] = (float) cos (6 * PI * i / n);
w[k + 5] = (float) -sin (6 * PI * i / n);
#endif
k += 6;
}
}
}

//FFT变换
void fft()
{
    int8_t      radix = 0;
    uint32_t  i=0;

        //generate FFT twiddle factors
        tw_fft_gen(twiddle, FFT_NUM);

        // if FFT_NUM can be represented as Power of 4, radix=4, else radix=2
        if(FFT_NUM & 0x55555555) {
            radix = 4;
        } else {
            radix = 2;
        }

         FILE *fpWrite=fopen("E:\\DSP-c66\\FFT_CCS_matlab_Demo.zip\\CCS\\SIN_FFT_data.txt","w");
        // 将实数输入转化为复数输入
        for(i = 0; i < FFT_NUM; i++)
        {
            fft_input[2 * i]        =  (float)sin_data[i];
            fft_input[2 * i + 1] =  (float)0.0;
        }

        // fast fourier transform - FFT变换
        DSPF_sp_fftSPxSP(FFT_NUM, (float *)fft_input, (float *)twiddle, (float *)fft_output, brev, radix, 0, FFT_NUM);
        printf("FFT done\n");
        // output FFT data
        for(i=0;i<Double_FFT_NUM;i++)
        {
            fprintf(fpWrite,"%lf\n ",fft_output[i]);
         }
        printf("write down done\n");
            fclose(fpWrite);
}


int main(void)
{
    fft();

	return 0;
}

matlab可视化结果,对比DSP和MATLAB的运算结果:

clc
clear all
close all
Fs = 1000;     %采样率
data_length=1024;
n = 2^nextpow2(data_length);
Y=zeros(1,data_length);
data=load('E:\DSP-c66\FFT_CCS_matlab_Demo.zip\CCS\SIN_FFT_data.txt'); %DSP运算结果
data=data';
for j=1:data_length
    j1=2*j;
    Y(j)=data(1,j1-1)+data(1,j1)*1i;
end
Y=abs(Y);
Y = Y(1,1:n/2+1-100 );
F_resolution_1=(Fs/n).*(0:n/2-100 );
figure; 
plot(F_resolution_1,Y,'b');grid on                      
hold on                                                                   
title('经CCS  FFT变换后(正频域)');

T = 1/Fs;                     % Sampling period
L = 1024;                     % Length of signal
t = (0:L-1)*T;                % Time vector
x = cos(2*pi*50*t);          % First row wave
count = 1;
x_length = length(x);
figure
subplot(2,1,1);
plot(t(1:100),x(1,1:100));grid on                      
hold on 
title(['原始信号']);
n = 2^nextpow2(L);
Y_fft = abs(fft(x,n));
Y_fft = Y_fft(1,1:n/2+1 );
F_resolution_2=(Fs/n).*(0:n/2 );
subplot(2,1,2);                                                         
plot(F_resolution_2,Y_fft,'b');grid on                      
hold on 
title(['经matlab  FFT变换后'])
fid=fopen('D:\sinx.txt','wt');%打开文件
for j=1:x_length
    if(count <= 31)
        fprintf(fid,'%.3f, ',x(j));%输出
        count = count + 1;
    else
        fprintf(fid,'\n');%输出
        count = 1;
    end
    
end
fclose(fid); %关闭fid标示符的文件
  • 6
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhengky6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值