首先进入TI官网,搜索C2000 wave,进行下载安装。
安装完成后,在2000 wave的安装目录下,进入以下目录:C2000Ware_4_02_00_00\libraries\dsp\FPU\c28
以我本地的安装目录为例:E:\ti\c2000\C2000Ware_4_02_00_00\libraries\dsp\FPU\c28
复制include、source文件夹到新建工程中。再根据选用的DSP型号对文件夹内容进行删减。如:我使用的是TMS320F28335,FPU是32位的,故仅保留include、source文件夹中的fpu32文件夹以及dsp.h头文件。
工程中,可以见有以下文件(仅进行滤波可删除FFT相关文件)
同时记得添加头文件路径
至此,对于我们进行滤波所需的函数库已经移植完毕。
测试部分:
首先使用MATLAB生成输入信号。
MATLAB函数式文件:
- %生成固定点数固定频率的正弦信号
- %fs为采样率
- %f为多频信号频率
- %N为采样点数
- %amp为信号幅值
- %phase为信号初相位
- function [input,t] = mult_freq_signal_test(fs,f,N,amp,phase)
- dt = 1/fs; %采样周期
- t = dt*N; %采样时间
- temp = (0:dt:(N-1)*dt)';
- y = amp.*sin(2*pi*f.*temp+phase/180*pi);
- Num = size(f,2); %获取f频率个数
- input = y*ones(Num,1);
- subplot(1,2,1);
- plot(temp/dt,input);
- title('信号时域');
- xlabel('点数 N');ylabel('幅值 A');
- subplot(1,2,2);
- Y = fftshift(fft(input));%做傅里叶变换并把零频点移到频谱中心
- F = (-N/2:N/2-1)*(fs/N);%设置横坐标
- plot(F,abs(Y)/max(abs(Y)));
- title('信号频域');
- xlabel('频率 f');ylabel('幅值 A')
- end
MATLAB命令式文件:
- %与mult_freq_signal_test.m函数式文件配合使用,将生成的输出信号保存到本地
- fs=1e5; %设采样率为100K
- f = [1e3,5e3,1e4]; %频率矩阵(1*N)测试信号频率为1K,5K,10K
- phase = [0,0,0]; %初相位均为0
- amp = [0.3,1.0,0.5];
- N=512; %采样点数
-
- y=mult_freq_signal_test(fs,f,N,amp,phase);
-
- y=reshape(y,[4,N/4]);
- y=y';
-
- fileID = fopen('record.txt','w');
- for i=1:1:N/4
- fprintf(fileID,'%.6f,%.6f,%.6f,%.6f,\n',y(i,1),y(i,2),y(i,3),y(i,4));
- end
- fclose(fileID);%将生成的初始信号保存到.m文件路径下的record.txt
生成波形如下的输入信号:
将record.txt文件内容复制到INPUT.c文件中,如下:
- //#############################################################################
- //! \file input.c
- //! \brief Input Vector (1024)
- //! \author Vishal Coelho
- //! \date 16-Sep-2016
- //!
- //
- // Group: C2000
- // Target Family: $DEVICE$
- //
- //#############################################################################
- //
- //
- // $Copyright: Copyright (C) 2022 Texas Instruments Incorporated -
- // http://www.ti.com/ ALL RIGHTS RESERVED $
- //#############################################################################
- float test_input[512] = {
- 0.000000,0.621747,1.100913,1.340760,
- 1.319556,1.092705,0.767601,0.461223,
- 0.256783,0.175872,0.176336,0.176103,
- 0.093107,-0.114798,-0.426010,-0.757295,
- -0.991651,-1.021653,-0.791865,-0.323977,
- 0.285317,0.893485,1.358000,1.582180,
- 1.544357,1.300000,0.956572,0.631123,
- 0.406943,0.305699,0.285317,0.263809,
- 0.159191,-0.070597,-0.403866,-0.757295,
- -1.013795,-1.065855,-0.857949,-0.411682,
- 0.176336,0.763658,1.207840,1.412279,
- 1.355387,1.092705,0.731771,0.389703,
- 0.149857,0.033962,0.000000,-0.033962,
- -0.149857,-0.389703,-0.731771,-1.092705,
- -1.355387,-1.412279,-1.207840,-0.763658,
- -0.176336,0.411682,0.857949,1.065855,
- 1.013795,0.757295,0.403866,0.070597,
- -0.159191,-0.263809,-0.285317,-0.305699,
- -0.406943,-0.631123,-0.956572,-1.300000,
- -1.544357,-1.582180,-1.358000,-0.893485,
- -0.285317,0.323977,0.791865,1.021653,
- 0.991651,0.757295,0.426010,0.114798,
- -0.093107,-0.176103,-0.176336,-0.175872,
- -0.256783,-0.461223,-0.767601,-1.092705,
- -1.319556,-1.340760,-1.100913,-0.621747,
- -0.000000,0.621747,1.100913,1.340760,
- 1.319556,1.092705,0.767601,0.461223,
- 0.256783,0.175872,0.176336,0.176103,
- 0.093107,-0.114798,-0.426010,-0.757295,
- -0.991651,-1.021653,-0.791865,-0.323977,
- 0.285317,0.893485,1.358000,1.582180,
- 1.544357,1.300000,0.956572,0.631123,
- 0.406943,0.305699,0.285317,0.263809,
- 0.159191,-0.070597,-0.403866,-0.757295,
- -1.013795,-1.065855,-0.857949,-0.411682,
- 0.176336,0.763658,1.207840,1.412279,
- 1.355387,1.092705,0.731771,0.389703,
- 0.149857,0.033962,0.000000,-0.033962,
- -0.149857,-0.389703,-0.731771,-1.092705,
- -1.355387,-1.412279,-1.207840,-0.763658,
- -0.176336,0.411682,0.857949,1.065855,
- 1.013795,0.757295,0.403866,0.070597,
- -0.159191,-0.263809,-0.285317,-0.305699,
- -0.406943,-0.631123,-0.956572,-1.300000,
- -1.544357,-1.582180,-1.358000,-0.893485,
- -0.285317,0.323977,0.791865,1.021653,
- 0.991651,0.757295,0.426010,0.114798,
- -0.093107,-0.176103,-0.176336,-0.175872,
- -0.256783,-0.461223,-0.767601,-1.092705,
- -1.319556,-1.340760,-1.100913,-0.621747,
- -0.000000,0.621747,1.100913,1.340760,
- 1.319556,1.092705,0.767601,0.461223,
- 0.256783,0.175872,0.176336,0.176103,
- 0.093107,-0.114798,-0.426010,-0.757295,
- -0.991651,-1.021653,-0.791865,-0.323977,
- 0.285317,0.893485,1.358000,1.582180,
- 1.544357,1.300000,0.956572,0.631123,
- 0.406943,0.305699,0.285317,0.263809,
- 0.159191,-0.070597,-0.403866,-0.757295,
- -1.013795,-1.065855,-0.857949,-0.411682,
- 0.176336,0.763658,1.207840,1.412279,
- 1.355387,1.092705,0.731771,0.389703,
- 0.149857,0.033962,0.000000,-0.033962,
- -0.149857,-0.389703,-0.731771,-1.092705,
- -1.355387,-1.412279,-1.207840,-0.763658,
- -0.176336,0.411682,0.857949,1.065855,
- 1.013795,0.757295,0.403866,0.070597,
- -0.159191,-0.263809,-0.285317,-0.305699,
- -0.406943,-0.631123,-0.956572,-1.300000,
- -1.544357,-1.582180,-1.358000,-0.893485,
- -0.285317,0.323977,0.791865,1.021653,
- 0.991651,0.757295,0.426010,0.114798,
- -0.093107,-0.176103,-0.176336,-0.175872,
- -0.256783,-0.461223,-0.767601,-1.092705,
- -1.319556,-1.340760,-1.100913,-0.621747,
- 0.000000,0.621747,1.100913,1.340760,
- 1.319556,1.092705,0.767601,0.461223,
- 0.256783,0.175872,0.176336,0.176103,
- 0.093107,-0.114798,-0.426010,-0.757295,
- -0.991651,-1.021653,-0.791865,-0.323977,
- 0.285317,0.893485,1.358000,1.582180,
- 1.544357,1.300000,0.956572,0.631123,
- 0.406943,0.305699,0.285317,0.263809,
- 0.159191,-0.070597,-0.403866,-0.757295,
- -1.013795,-1.065855,-0.857949,-0.411682,
- 0.176336,0.763658,1.207840,1.412279,
- 1.355387,1.092705,0.731771,0.389703,
- 0.149857,0.033962,-0.000000,-0.033962,
- -0.149857,-0.389703,-0.731771,-1.092705,
- -1.355387,-1.412279,-1.207840,-0.763658,
- -0.176336,0.411682,0.857949,1.065855,
- 1.013795,0.757295,0.403866,0.070597,
- -0.159191,-0.263809,-0.285317,-0.305699,
- -0.406943,-0.631123,-0.956572,-1.300000,
- -1.544357,-1.582180,-1.358000,-0.893485,
- -0.285317,0.323977,0.791865,1.021653,
- 0.991651,0.757295,0.426010,0.114798,
- -0.093107,-0.176103,-0.176336,-0.175872,
- -0.256783,-0.461223,-0.767601,-1.092705,
- -1.319556,-1.340760,-1.100913,-0.621747,
- 0.000000,0.621747,1.100913,1.340760,
- 1.319556,1.092705,0.767601,0.461223,
- 0.256783,0.175872,0.176336,0.176103,
- 0.093107,-0.114798,-0.426010,-0.757295,
- -0.991651,-1.021653,-0.791865,-0.323977,
- 0.285317,0.893485,1.358000,1.582180,
- 1.544357,1.300000,0.956572,0.631123,
- 0.406943,0.305699,0.285317,0.263809,
- 0.159191,-0.070597,-0.403866,-0.757295,
- -1.013795,-1.065855,-0.857949,-0.411682,
- 0.176336,0.763658,1.207840,1.412279,
- 1.355387,1.092705,0.731771,0.389703,
- 0.149857,0.033962,0.000000,-0.033962,
- -0.149857,-0.389703,-0.731771,-1.092705,
- -1.355387,-1.412279,-1.207840,-0.763658,
- -0.176336,0.411682,0.857949,1.065855,
- 1.013795,0.757295,0.403866,0.070597,
- -0.159191,-0.263809,-0.285317,-0.305699,
- -0.406943,-0.631123,-0.956572,-1.300000,
- -1.544357,-1.582180,-1.358000,-0.893485,
- -0.285317,0.323977,0.791865,1.021653,
- 0.991651,0.757295,0.426010,0.114798,
- -0.093107,-0.176103,-0.176336,-0.175872,
- -0.256783,-0.461223,-0.767601,-1.092705,
- -1.319556,-1.340760,-1.100913,-0.621747,
- 0.000000,0.621747,1.100913,1.340760,
- 1.319556,1.092705,0.767601,0.461223,
- 0.256783,0.175872,0.176336,0.176103,
-
- };
-
-
- // End of File
借助MATLAB自带的滤波器设计工具filter designer,生成滤波器系数(滤波器截止频率选择1500Hz,滤除高频噪声,指定阶数为80阶)。
再点击目标,选择生成到CCS IDE中
28335FPU为32位,故选择单精度浮点型
点击生成到CCS新建工程中即可。
生成的fadcoefs.h如下:
- /*
- * Filter Coefficients (C Source) generated by the Filter Design and Analysis Tool
- * Generated by MATLAB(R) 9.11 and Signal Processing Toolbox 8.7.
- * Generated on: 13-Jan-2023 10:06:26
- */
-
- /*
- * 离散时间 FIR 滤波器(实数)
- * ----------------
- * 滤波器结构 : 直接型 FIR
- * 滤波器长度 : 81
- * 稳定 : 是
- * 线性相位 : 是 (Type 1)
- */
-
- /* General type conversion for MATLAB generated C-code */
- #include "tmwtypes.h"
- /*
- * Expected path to tmwtypes.h
- * E:\Program Files\MATLAB\R2021b\extern\include\tmwtypes.h
- */
- /*
- * Warning - Filter coefficients were truncated to fit specified data type.
- * The resulting response may not match generated theoretical response.
- * Use the Filter Design & Analysis Tool to design accurate
- * single-precision filter coefficients.
- */
- const int BL = 81;
- const float coeffs[81] = {
- -0,-7.111737887e-06,-2.438249976e-05,-4.471002831e-05,-5.97600847e-05,
- -6.013489474e-05,-3.557757736e-05,2.479158684e-05,0.0001322122553,0.0002980003483,
- 0.0005332503351, 0.000848530326, 0.001253577648, 0.001757002436, 0.002366006607,
- 0.003086125944, 0.003921000753, 0.004872185644, 0.005938995164, 0.007118403912,
- 0.008404986933, 0.009790921584, 0.01126603596, 0.01281791367, 0.01443205494,
- 0.01609207876, 0.01777997613, 0.01947640628, 0.02116102912, 0.02281285636,
- 0.02441063337, 0.0259332303, 0.02736003883, 0.02867136523, 0.02984880283,
- 0.03087559529, 0.0317369625, 0.03242038563, 0.03291586414, 0.03321611136,
- 0.03331669047, 0.03321611136, 0.03291586414, 0.03242038563, 0.0317369625,
- 0.03087559529, 0.02984880283, 0.02867136523, 0.02736003883, 0.0259332303,
- 0.02441063337, 0.02281285636, 0.02116102912, 0.01947640628, 0.01777997613,
- 0.01609207876, 0.01443205494, 0.01281791367, 0.01126603596, 0.009790921584,
- 0.008404986933, 0.007118403912, 0.005938995164, 0.004872185644, 0.003921000753,
- 0.003086125944, 0.002366006607, 0.001757002436, 0.001253577648, 0.000848530326,
- 0.0005332503351,0.0002980003483,0.0001322122553,2.479158684e-05,-3.557757736e-05,
- -6.013489474e-05,-5.97600847e-05,-4.471002831e-05,-2.438249976e-05,-7.111737887e-06,
- -0
- };
需注意,生成的fadcoefs.h文件需包含tmwtypes.h文件
在生成的fadcoefs.h中包含tmwtypes.h的文件路径,将其添加到工程头文件路径即可。
至此已完成了大部分准备工作,接下来开始编写函数进行滤波操作。
仿照TI官方例程,完成了以下函数的编写。
- /*
- * FIR.c
- *
- * Created on: 2023年1月12日
- * Author: yang
- */
-
-
- #include <FIR.h>
- #include "dsp.h"
- #include "fpu_filter.h"
- #include <math.h>
- #include "complex.h"
- #include "fdacoefs.h"
-
- #define pi 3.14159
- #define FIR_SIZE (512U)
- #define FIR_ORDER (80U) // ORDER = NUM_TAPS - 1,ORDER为滤波器阶数
-
- #pragma DATA_SECTION(coeffs, "FIR_buffer0");
- #pragma DATA_SECTION(FIR_output, "FIR_buffer1");
-
- // FIR_f32 object
- FIR_f32 fir;
- // Handle to the FIR_f32 object
- FIR_f32_Handle hnd_fir = &fir;
-
- // The filter coefficients are tacked on to the end of the
- // MATLAB generated input
-
- // The delay line buffer
- float delayLine[FIR_ORDER+1];
-
- float FIR_output[FIR_SIZE];
-
- extern float test_input[];
- extern const float coeffs[];
- //*****************************************************************************
- // the function definitions
- //*****************************************************************************
- void FIR_Init(void)
- {
- // Configure the object
- FIR_f32_setCoefficientsPtr(hnd_fir, coeffs);
- FIR_f32_setDelayLinePtr(hnd_fir, delayLine);
- FIR_f32_setOrder(hnd_fir, FIR_ORDER);
- FIR_f32_setInitFunction(hnd_fir, (v_pfn_v)FIR_f32_init);
- FIR_f32_setCalcFunction(hnd_fir, (v_pfn_v)FIR_f32_calc);
-
- // Copy the coefficients from test input into the "coeffs" array
- //memcpy(&coeffs, &test_input[FIR_SIZE], (FIR_ORDER + 1U)*sizeof(float));
-
- // Run the initialization function
- hnd_fir->init(hnd_fir);
-
- }
-
- void FIR_filter_run(void)
- {
- // Locals
- uint16_t i;
- float32u_t in, out;
-
- for(i = 0U; i < FIR_SIZE; i++)
- {
- in.f32 = test_input[i];
- out.f32 = FLT_MAX;
- FIR_f32_setInput(hnd_fir, in.f32);
- FIR_f32_setOutput(hnd_fir, out.f32);
-
- // Call the calculation routine
- hnd_fir->calc(hnd_fir);
-
- out.f32 = FIR_f32_getOutput(hnd_fir);
- FIR_output[i] = out.f32;
- }
- }
-
- // End of File
-
-
-
-
-
-
-
-
-
cmd文件如下
- /*
- // TI File $Revision: /main/3 $
- // Checkin $Date: July 9, 2008 14:12:45 $
- //###########################################################################
- //
- // FILE: 28335_RAM_lnk.cmd
- //
- // TITLE: Linker Command File For IQmath examples that run out of RAM
- //
- // NOTE; The example project uses memory protected by the
- // Code Security Module (CSM). Make sure the CSM is
- // unlocked before you load the project. One quick way
- // to do this on an erased device is to open a memory
- // window to the CSM password locations. If these locations
- // read back 0xFFFF (or non-zero), then the CSM is unlocked:
- //
- // Device Password locations
- // 28335: 0x33FFF8 - 0x33FFFF
- //
- //###########################################################################
- //
- //
- // $Copyright: Copyright (C) 2014-2022 Texas Instruments Incorporated -
- // http://www.ti.com/ ALL RIGHTS RESERVED $
- //###########################################################################
- */
-
-
-
-
- MEMORY
- {
- PAGE 0 :
- BEGIN : origin = 0x000000, length = 0x000002 /* Boot to M0 will go here */
- BOOT_RSVD : origin = 0x000002, length = 0x00004E /* Part of M0, BOOT rom will use this for stack */
-
- RAML0 : origin = 0x008000, length = 0x000800
- RAML1L2 : origin = 0x008800, length = 0x004800
- ZONE7A : origin = 0x200000, length = 0x00FC00 /* XINTF zone 7 - program space */
-
- FLASHH : origin = 0x300000, length = 0x008000 /* on-chip FLASH */
- FLASHG : origin = 0x308000, length = 0x008000 /* on-chip FLASH */
- FLASHF : origin = 0x310000, length = 0x008000 /* on-chip FLASH */
- FLASHE : origin = 0x318000, length = 0x008000 /* on-chip FLASH */
- FLASHD : origin = 0x320000, length = 0x008000 /* on-chip FLASH */
- FLASHC : origin = 0x328000, length = 0x008000 /* on-chip FLASH */
- FLASHA : origin = 0x338000, length = 0x007F80 /* on-chip FLASH */
-
-
- CSM_RSVD : origin = 0x33FF80, length = 0x000076 /* Part of FLASHA. Program with all 0x0000 when CSM is in use. */
- CSM_PWL : origin = 0x33FFF8, length = 0x000008 /* Part of FLASHA. CSM password locations in FLASHA */
- OTP : origin = 0x380400, length = 0x000400 /* on-chip OTP */
- ADC_CAL : origin = 0x380080, length = 0x000009
-
- IQTABLES : origin = 0x3FE000, length = 0x000b50 /* IQ Math Tables in Boot ROM */
- IQTABLES2 : origin = 0x3FEB50, length = 0x00008c /* IQ Math Tables in Boot ROM */
- FPUTABLES : origin = 0x3FEBDC, length = 0x0006A0 /* FPU Tables in Boot ROM */
- ROM : origin = 0x3FF27C, length = 0x000D44 /* Boot ROM */
-
- RESET : origin = 0x3FFFC0, length = 0x000002
- VECTORS : origin = 0x3FFFC2, length = 0x00003E /* part of boot ROM */
-
-
-
-
-
-
-
- PAGE 1 :
- BOOT_RSVD : origin = 0x000002, length = 0x00004E /* Part of M0, BOOT rom will use this for stack */
- RAMM0 : origin = 0x000050, length = 0x0003B0 /* on-chip RAM block M0 */
- RAMM1 : origin = 0x000400, length = 0x000400 /* on-chip RAM block M1 */
- RAML3 : origin = 0x00D000, length = 0x001000
- RAML4 : origin = 0x00E000, length = 0x001000
- RAML5 : origin = 0x00F000, length = 0x001000
- ZONE7B : origin = 0x20FC00, length = 0x000400 /* XINTF zone 7 - data space */
- FLASHB : origin = 0x330000, length = 0x008000 /* on-chip FLASH */
-
- }
-
-
- SECTIONS
- {
- FPUmathTables : > FPUTABLES, PAGE = 0, TYPE = NOLOAD
- /* Setup for "boot to SARAM" mode:
- The codestart section (found in DSP28_CodeStartBranch.asm)
- re-directs execution to the start of user code. */
-
- codestart : > BEGIN, PAGE = 0
-
- #ifdef __TI_COMPILER_VERSION__
- #if __TI_COMPILER_VERSION__ >= 15009000
- .TI.ramfunc : {} LOAD = RAML0,
- RUN = RAML0,
- LOAD_START(_RamfuncsLoadStart),
- LOAD_END(_RamfuncsLoadEnd),
- RUN_START(_RamfuncsRunStart),
- LOAD_SIZE(_RamfuncsLoadSize),
- PAGE = 0
- #else
- ramfuncs : LOAD = RAML0,
- RUN = RAML0,
- LOAD_START(_RamfuncsLoadStart),
- LOAD_END(_RamfuncsLoadEnd),
- RUN_START(_RamfuncsRunStart),
- LOAD_SIZE(_RamfuncsLoadSize),
- PAGE = 0
- #endif
- #endif
-
- .text : {*(.text)}>> RAML1L2|RAML0 PAGE = 0
- .cinit : > RAML0, PAGE = 0
- .pinit : > RAML0, PAGE = 0
- .switch : > RAML0, PAGE = 0
-
- .stack : > RAMM1, PAGE = 1
- .ebss : > RAML3, PAGE = 1
- .econst : > RAML3, PAGE = 1
- .sysmem : > RAML3, PAGE = 1
- .esysmem : > RAML3, PAGE = 1
-
- .sysmem : > RAML3, PAGE = 1
-
- FIR_buffer0: > RAML4, PAGE = 1
- FIR_buffer1: > RAML5, PAGE = 1
-
-
- .cio : > RAML3, PAGE = 1
- ZONE7DATA : > ZONE7B, PAGE = 1
-
- DMARAML4 : > RAML4, PAGE = 1
- DMARAML5 : > RAML5, PAGE = 1
-
- .reset : > RESET, PAGE = 0, TYPE = DSECT /* not used */
- csm_rsvd : > CSM_RSVD PAGE = 0, TYPE = DSECT /* not used for SARAM examples */
- csmpasswds : > CSM_PWL PAGE = 0, TYPE = DSECT /* not used for SARAM examples */
-
- /* Allocate ADC_cal function (pre-programmed by factory into TI reserved memory) */
- .adc_cal : load = ADC_CAL, PAGE = 0, TYPE = NOLOAD
-
- }
-
- /*
- //===========================================================================
- // End of file.
- //===========================================================================
- */
再在主函数中调用FIR_Init(); FIR_filter_run();函数即可完成滤波操作。
借助CCS的Graph画图工具测试滤波效果如下:
原始信号:
滤波后信号:
测试结束,完成了FIR滤波。
本人为TMS320F28335学习小白,如有错误,请大佬多多指正。
本文参考了以下文章:
[1]TMS320F28335调用官方库进行FFT频谱分析_PeepFuture橙子的博客-CSDN博客
[2]https://blog.csdn.net/weixin_42216236/article/details/127375580