concorr.c

来自「DSP课程课堂作业程序」· C语言 代码 · 共 86 行

C
86
字号

#include <stdio.h>   
#include <math.h>

#define Nx 5
#define Nh 3
#define Length  Nx+Nh-1

/************计算两个数的乘积****************************/
double double_multiply(double m,double n)
{ 
   double z;
   z=m*n;
      
   return z;
}  

/*************计算两个序列的线性卷积*******************************/
void convolution(int x[Nx] , int h[Nh], int y[Length] )
{
int i,k;
for (k=0;k<=Length-1; k++) 
  {
int temp=0;
for(i=0; i<Nx; i++)
{ if ((k-i)>=0 && (k-i) <=Nh-1) 
        temp=temp+x[i]*h[k-i];
    }
     y[k]=temp;
}
}
/*****************计算两个序列x[k]和h[k]卷积的子程序**************/
 void correlation(int x[Nx], int h[Nh],int r[Length] )
{
int i,k,n; 
int temp;
for (k=0;k<=Length-1;k++) 
   {
       n=(Nh-1)-k;
       temp=0;
       
       for(i=0; i<Nx; i++)
       { 
          if ((n+i)>=0 && (n+i)<=(Nh-1))
                temp=temp+x[i]*h[n+i];
            
        }
    r[k]=temp;
   }
}


 /*************************************************************/
 
//以下是主程序部分 
  double a=1,b=3;
  int x[Nx]={1,1,1,1,1};
  int h[Nh]={1,1,2};
  int y[Length];
  int r[Length];

void main()
{ 
int k;
double w;
  //调用乘积程序
w=double_multiply(a,b);
printf("the result of double_multiply is %f  \n ",w);
 //调用卷积程序

convolution(x,h,y);
printf("  convolution of x and h  is(y=):  " );
for(k=0;k<=Length-1;k++)
 printf("%i  ",y[k]); printf("\n  ");
 
 //调用相关程序
correlation(x,h,r);
printf("correlation of x and h is(r=):  " );
for(k=0;k<=Length-1;k++)
 printf("%i  ",r[k]);
 
 

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?