# C/C++ : Convolution Source Code

# C/C++ : Convolution Source Code

In mathematics and, in particular, functional analysis, **convolution** is a mathematical operation on two functions *f* and *g*, producing a third function that is typically viewed as a modified version of one of the original functions (from wikipedia.com).

*g*) is first reflected about τ = 0 and then offset by

*t*, making it

*g*(

*t*− τ). The area under the resulting product gives the convolution at

*t*. The horizontal axis is τ for

*f*and

*g*, and t for .

I get this picture **convolution ilustration** from wikipedia. We can ilustrated the convolution process with number. Please check the picture below, I try to create a **convolution ilustration**. So, we can create **C/C++ : Convolution Source Code** from this convolution ilustration :

This is a simple** convolution ilustration** with input two vector. From this ilustration, we can create a **C/C++ : Convolution Source Code**. This is a function to** compute convolution 1D** with C/C++ :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
//convolution algorithm float *conv(float *A, float *B, int lenA, int lenB, int *lenC) { int nconv; int i, j, i1; float tmp; float *C; //allocated convolution array nconv = lenA+lenB-1; C = (float*) calloc(nconv, sizeof(float)); //convolution process for (i=0; i<nconv; i++) { i1 = i; tmp = 0.0; for (j=0; j<lenB; j++) { if(i1>=0 && i1<lenA) tmp = tmp + (A[i1]*B[j]); i1 = i1-1; C[i] = tmp; } } //get length of convolution array (*lenC) = nconv; //return convolution array return(C); } |

we can running this code with command :

**./convolution length_vect_1 length_vect_2**

example :

**./convolution 4 6**

This is sample output from **C/C++ : Convolution Source Code**:

1 2 3 4 5 6 7 8 9 |
toto@toto-laptop:~/Documents$ ./convolution 4 6 Vector 'A' , Size=4 1.000 2.000 3.000 4.000 Vector 'B' , Size=6 5.000 6.000 7.000 8.000 9.000 10.000 Vector 'C' , Size=9 5.000 16.000 34.000 60.000 70.000 80.000 79.000 66.000 40.000 |

You can download the complete **C/C++ : Convolution Source Code **with testing program at here.

thanks for this great function in matlab 😀

Hello,

I’m working with the ambiguity function, this is used in signals…. I have to implement the ambiguity function in C language, but is very difficult to me. I have some implementations in Matlab but I need it in C.

I see that you have a C code of convolution, that is more or less that I have to implement, I would like if you can share your C source code, I will be very gratefull with you.

My email eticoa@hotmail.com

pd. Sorry for my english…

Hi, Please check the below from my post. I am attach a complete source code for this convolution problem.

Thank you