24小时热门版块排行榜    

查看: 2103  |  回复: 1
【奖励】 本帖被评价1次,作者baobiao007增加金币 0.6

baobiao007

木虫 (职业作家)


[资源] 【分享】线性卷积与循环卷积的比较c程序

#include
#include
#include
const int L1=4;
const int L2=6;
//普通线性卷积
void conv(float x[],int m,float y[],int n,float z[],int l)
{
        int i,j;
        for(i=0; i         {
                z=0.0;
                for(j=0; j                         if(i-j>=0&&i-j<=m-1)
                                z+=y[j]*x[i-j];
        }
}
//圆周移位,x1-原始有限长序列,x2-移位后的有限长序列,m-移动长度(>0右移,<0左移)
void CircleMove(float x1[],float x2[],int m,int num)
{
        int rr;
        for(int i=0; i         {
                x2=0.0;
                rr=(i-m)%num;
                if(r>=0)
                  x2=x1[rr];
                else
                  x2=x1[num+rr];
        }
}
//时域圆周卷积,两等长序列x,y圆周卷积后得到z
//三序列长度都是num
void CircleConvolution(float x[],float y[],float z[],int num)
{
        float *y1,t;
        int i,j;
        y1=(float *)calloc(num,sizeof(float));
        for(i=1; i<=num/2; i++)
        { t=y;y=y[num-i];y[num-i]=t;}
        for(j=0; j         { z[j]=0.0;
          CircleMove(y,y1,j,num);
          for(i=0; i                   z[j]+=x*y1;
        }
        free(y1);
}

void main()
{
        float x[L1]={1,1,1,1};
        float y[L2]={1,1,1,1,1,1};
        float z1[L1+L2-1],z2[L1+L2-1];
        //直接线性卷积
        conv(x,L1,y,L2,z1,L1+L2-1);

        //要使循环卷积与线性卷积的结果相同,首先要将x,y的
        //长度扩大为L1+L2-1
        float *x1,*y1;
        x1=(float *)calloc(L1+L2-1,sizeof(float));
    y1=(float *)calloc(L1+L2-1,sizeof(float));
        for(int i=0; i                 x1=x;
        for(i=0; i                 y1=y;
        CircleConvolution(x1,y1,z2,L1+L2-1);
        printf("线性卷积:       圆周卷积:\n";
        for(i=0; i            printf("z1[%d]=%f        z2[%d]=%f\n",i,z1,i,z2);

        free(x1);free(y1);
}
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

yanggis

木虫 (著名写手)


★★★ 三星级,支持鼓励

不错。。。。。。。。。。。。
2楼2011-02-10 22:06:22
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 baobiao007 的主题更新
☆ 无星级 ★ 一星级 ★★★ 三星级 ★★★★★ 五星级
普通表情 高级回复 (可上传附件)
信息提示
请填处理意见