切换到宽版
  • 1404阅读
  • 0回复

线性卷积与循环卷积比较c程序 [复制链接]

上一主题 下一主题
离线baobiao007
 
发帖
29
财富
1
威望
0
交易币
0
只看楼主 倒序阅读 使用道具 0楼 发表于: 2011-02-13 | 石油求职招聘就上: 阿果石油英才网
— 本帖被 阿果 从 石油地质 移动到本区(2011-09-29) —
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
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<l; i++)
        {
                z=0.0;
                for(j=0; j<n; 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<num; 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<num; j++)
        { z[j]=0.0;
          CircleMove(y,y1,j,num);
          for(i=0; i<num; 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<L1; i++)
                x1=x;
        for(i=0; i<L2; i++)
                y1=y;
        CircleConvolution(x1,y1,z2,L1+L2-1);
        printf("线性卷积:       圆周卷积:\n");
        for(i=0; i<L1+L2-1; i++)
             printf("z1[%d]=%f        z2[%d]=%f\n",i,z1,i,z2);
        free(x1);free(y1);
}
本文来自: 小木虫论坛 http://emuch.net/bbs/viewthread.php?tid=2846206&fpage=1
评价一下你浏览此帖子的感受

精彩

感动

搞笑

开心

愤怒

无聊

灌水

网站事务咨询:QQ:1392013 | 26189883
阿果石油网为免费个人网站,为石油人提供免费的在线即时技术交流场所,拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论和行为!
如有言论或会员共享的资料涉及到您的权益,请立即通知网站管理员,本站将在第一时间给予配合处理,谢谢!