0% found this document useful (0 votes)
47 views

Convolution C

This document summarizes a C program that performs linear convolution on two input vectors X and Y with lengths lenx and leny respectively. It calculates the resulting convolved vector Z by performing a straightforward calculation in a for loop over Z's length (lenx + leny - 1). The routine was written by Clay S. Turner to calculate z=x convolve y.

Uploaded by

Canbruce Luwang
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Convolution C

This document summarizes a C program that performs linear convolution on two input vectors X and Y with lengths lenx and leny respectively. It calculates the resulting convolved vector Z by performing a straightforward calculation in a for loop over Z's length (lenx + leny - 1). The routine was written by Clay S. Turner to calculate z=x convolve y.

Uploaded by

Canbruce Luwang
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 1

// Routine peforms linear convolution by straight forward calculation

// calculates z= x convolve y
// Written by Clay S. Turner
//
// inputs:
// X array of data comprising vector #1
// Y array of data comprising vector #2
// Z pointer to place to save resulting data - needs to be lenx
// leny-1 long
// lenx # of items in vector 1
// leny # of items in vector 2
#include<stdio.h>

int main()
{
double X[6]={ 1.2,2.1,1.3,3.4,6.8,9.2};
double Y[6]={ 1.2,2.1,1.3,3.4,6.8,9.2};
double Z;
LinearConvolution(X,Y,Z,6,6);
return 0;

void LinearConvolution(double X[],double Y[], double Z[], int lenx,int leny)


{
double *zptr,s,*xp,*yp;
int lenz;
int i,n,n_lo,n_hi;

lenz=lenx+leny-1;

zptr=Z;

for (i=0;i<lenz;i++) {
s=0.0;
n_lo=0>(i-leny+1)?0:i-leny+1;
n_hi=lenx-1<i?lenx-1:i;
xp=X+n_lo;
yp=Y+i-n_lo;
for (n=n_lo;n<=n_hi;n++) {
s+=*xp * *yp;
xp++;
yp--;
}
*zptr=s;
zptr++;
}
}

You might also like