单元测试
APstack
#include <stdio.h>
#define APstacksize 10 //stack size 和滤波时间要不要关联起来
typedef unsigned int Uint32;
typedef unsigned short int Uint16;
//要求一段时间的变化量,还是必须有个栈能保存这段时间的样本的
//需要自己内部构造一个采样器
typedef struct
{
Uint16 samplefreq; //采样频率
Uint16 sample;
Uint16 samplepre; //上一次采样值
Uint16 DeltaSamplePst; //每两次采样间隔样本变化量
Uint16 DeltaSampleAT;
Uint16 samplefiltered;
Uint16 filterTime; //数据量累计的时间
}ST_lpf;
Uint16 samplestack[APstacksize];
Uint16 p21st,p2last;
void lpfinit(ST_lpf *lpf,Uint16 frq,Uint16 T);
void AimposLpf(ST_lpf *lpf);
int main(void) {
static stackfullflag;
static ST_lpf stlpf_Aimpos;
Uint16 runtime;
Uint16 Tperiod = 0;
Uint16 Tcount = 0;
Uint16 filtereddata[100];
stackfullflag = 0;
lpfinit(&stlpf_Aimpos,2,5);
Tperiod = 20*stlpf_Aimpos.samplefreq;
for(runtime=0;runtime<100;runtime++)
{
if(0 == runtime%stlpf_Aimpos.samplefreq)
{
Tcount++;
if(Tcount >= APstacksize-1)
{
Tcount = APstacksize-1;
}
stlpf_Aimpos.samplepre = stlpf_Aimpos.sample;
stlpf_Aimpos.sample = runtime<<3;
stlpf_Aimpos.DeltaSamplePst = stlpf_Aimpos.sample - stlpf_Aimpos.samplepre;
samplestack[p2last] = stlpf_Aimpos.sample;
stlpf_Aimpos.DeltaSampleAT = samplestack[p2last]-samplestack[p21st];
stlpf_Aimpos.DeltaSamplePst = stlpf_Aimpos.DeltaSampleAT*100/Tperiod;
filtereddata[runtime] = stlpf_Aimpos.samplefiltered;
// printf("%d %d %d %d\n", stlpf_Aimpos.sample,p21st,p2last,samplestack[p2last]);
printf("%d %d %d %d %d %d\n", runtime,samplestack[p2last],samplestack[p21st],stlpf_Aimpos.DeltaSampleAT,stlpf_Aimpos.DeltaSamplePst,stlpf_Aimpos.samplefiltered);
p2last++;
if(p2last < APstacksize - 1 && Tcount >= APstacksize-1 ) //注意与matlab的区别
{
p21st = p2last+1;
}
else if(p2last == APstacksize - 1)
{
p21st = 0;
}
else if(p2last > APstacksize - 1)
{
p2last = 0;
p21st = p2last+1;
}
}
AimposLpf(&stlpf_Aimpos);
}
return 0;
}
void lpfinit(ST_lpf *lpf,Uint16 frq,Uint16 T)
{
lpf->samplefreq = frq;
lpf->sample = 0;
lpf->samplepre = 0;
lpf->DeltaSamplePst = 0;
lpf->DeltaSampleAT=0;
lpf->samplefiltered = 0;
lpf->filterTime = T;
}
void AimposLpf(ST_lpf *lpf)
{
lpf->samplefiltered = lpf->samplefiltered + lpf->DeltaSamplePst;
}