给你个C语言的中值滤波法:
#define N 10
typedef unsigned int u16
u16 voltage_filter(void)
{
char count,i,j
static u16 value_buf[N]
u16 sum=0,temp
for(count=0count<Ncount++)
{
value_buf[count] = GetADCValue(5)
}
for(j=0j<N-1j++)
{
for(i=0i<N-ji++)
{
if(value_buf[i]>value_buf[i+1])
{
temp = value_buf[i]
value_buf[i]=value_buf[i+1]
value_buf[i+1]=temp
}
}
}
for(count=1count<N-1count++)
sum+=value_buf[count]
return (sum/(N-2))
}
上面u(k)=kpe(k)-u1(k)应该是有问题的,应该是u(k)=kpe(k)+u1(k)程序照着写就可以了。
double U,U1
double Um
double Kp,Ki
void Control(double e)
{
U1+=Ki*e
if(U1>Um)U=Um
else if(U1<-Um)U=-Um
else
{
U+=Kp*e+U1
if (U>Um)U=Um
if(u<-Um)U=-Um
}
}
short h[], short y[]){
int i, j, sum for (j = 0j <100j++) {
sum = 0
for (i = 0i <32i++)
sum += x[i+j] * h[i]
y[j] = sum >>15
}
}
2
void fir(short x[], short h[], short y[])
{
int i, j, sum0, sum1
short x0,x1,h0,h1 for (j = 0j <100j+=2) {
sum0 = 0
sum1 = 0
x0 = x[j]
for (i = 0i <32i+=2){
x1 = x[j+i+1]
h0 = h[i]
sum0 += x0 * h0
sum1 += x1 * h0
x0 = x[j+i+2]
h1 = h[i+1]
sum0 += x1 * h1
sum1 += x0 * h1
}
y[j] = sum0 >>15
y[j+1] = sum1 >>15
}
}
3
void fir(short x[], short h[], short y[])
{
int i, j, sum0, sum1
short x0,x1,x2,x3,x4,x5,x6,x7,h0,h1,h2,h3,h4,h5,h6,h7 for (j = 0j <100j+=2) {
sum0 = 0
sum1 = 0
x0 = x[j]
for (i = 0i <32i+=8){
x1 = x[j+i+1]
h0 = h[i]
sum0 += x0 * h0
sum1 += x1 * h0
x2 = x[j+i+2]
h1 = h[i+1]
sum0 += x1 * h1
sum1 += x2 * h1
x3 = x[j+i+3]
h2 = h[i+2]
sum0 += x2 * h2
sum1 += x3 * h2
x4 = x[j+i+4]
h3 = h[i+3]
sum0 += x3 * h3
sum1 += x4 * h3
x5 = x[j+i+5]
h4 = h[i+4]
sum0 += x4 * h4
sum1 += x5 * h4
x6 = x[j+i+6]
h5 = h[i+5]
sum0 += x5 * h5
sum1 += x6 * h5
x7 = x[j+i+7]
h6 = h[i+6]
sum0 += x6 * h6
sum1 += x7 * h6
x0 = x[j+i+8]
h7 = h[i+7]
sum0 += x7 * h7
sum1 += x0 * h7
}
y[j] = sum0 >>15
y[j+1] = sum1 >>15
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)