有注释,代码如下:
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#define SEIRE_LEN 1000 //序列长度
#define KERNEL_LEN 7 //卷积核长度
#define RESULT_LEN SEIRE_LEN+KERNEL_LEN-1 //结果长度
int main()
{
int result[RESULT_LEN];
int f[SEIRE_LEN]; //声明数组,给定大小
int h[KERNEL_LEN]= {1,2,3,4,3,2,1}; //this is a low pass filter
int m,n;
srand((unsigned int)time(NULL)); //产生随机数的seed,用于使每次产生的随机序列不同
for(m=0;m<SEIRE_LEN;m++)
{
f[m] = rand()%10-5; //产生-5~5的随机数并赋给数组f
}
for(n=0;n<RESULT_LEN;n++) //卷积运算
{
for(m=0;m<SEIRE_LEN;m++)
{
if((n-m)>=0 && (n-m)<=KERNEL_LEN) //两个序列均从零开始,0之前的未定义,随意加一个判断条件
{
result[n]+=f[m]*h[n-m];
}
}
printf("%d ",result[n]); //打印出来结果
}
return 0;
}