1. ## Convolution in c

Hi, I have to do an assignment for my C programming class. I have to write a function that takes two arrays as arguments, calculates their discrete convolution and prints out the resulting function in 2 columns. It should do the calculation only for non-zero numbers. The problem is that I don't even know how a convolution works, i'm just taking calculus right now.
can anybody help? I would appreciate it

2. google for "discrete convolution". There is plenty of information out there.

3. well, I've done that already, the problem is how to make it into a C, I don't even know what the output is supposed to look like

4. Well, your first step is to understand what the output of a discrete convolution process is supposed to be. Once you understand that, you will be able to design an appropriate algorithm (description of logical steps to get from inputs to outputs). Once you understand the algorithm, implementing it in C should be simple.

But it is impossible to write code in any programming language if you cannot describe what that code is required to achieve.

5. So you managed to google "discrete convolution" but you couldn't manage "discrete convolution c source code" ?

> I don't even know what the output is supposed to look like
Two columns of numbers, according to your first post.

6. Do you know what a dot product is?

7. I've also heard dot product called an inner product too, I mean as opposed to a cross product. These were part of a complete college algebra, of course sometimes the conic section is so overwhelming that the class doesn't make it to them. I think the professor ended up calling those questions extra credit on the final anyway.

Best Regards,

8. sorry for the delay.
Can anybody give me an small example? using the convolution of matrices (i'm not asking for the answer) so maybe that can be more helpful

sorry for the delay.
Can anybody give me an small example? using the convolution of matrices (i'm not asking for the answer) so maybe that can be more helpful
You have two arrays of values which you are convolving together. For the sake of example, assume they are one-dimensional, though higher dimensions work the same way. One of the arrays is typically much larger than the other. The one which is larger is called the 'signal', by convention. The smaller one is called the 'kernel'. To convolve them, you take the kernel and slap it down on top of the signal somewhere. You take the dot product of the two, this produces a result. This is one data point of the convolution. Now you slide the kernel to the right (or left, whatever) by one sample, and do it again. That produces the next data point. And so on, until the kernel 'slides off' the end of the signal.

There are details that I won't go into, such as the kernel is the time-reversed impulse response of the filter, the length of the result is N + M - 1 where N is the signal length and M is the kernel length, etc. Since this is a programming assignment, I assume that those particular details are not really the point of the assignment.

The biggest hint that I can give you is that you are going to need one loop nested inside another loop.

10. Hi, it's been a while since I started this thread but I've been busy. I was working on my program and I got something but it gives me the wrong output. My professor said the output should be as follows.
f*f[0] = 0, f*f[i] = 0, f*f[2] = 16, f*f[3] = 64 and so on
my program compiles but it gives me outputs like this
nnindex: 0 convolution -1019395280
index: 0 convolution -1438002336
index: 1 convolution 0
index: 1 convolution -626327120

her is my source code if anyone would like to help me
Code:
```    #include<stdio.h>

void mult( int f[], int g[], int n){ /*function to multiply  first array by 4*/

int i;
for (i =0; i < n; i++){
g[i] = 4 * f[i];
printf("n");

}
}

void convo(int f[], int g[], int  h[], int n){  /*function to convolve the 2 arrays*/

int i, j;

for(i = 0; i < n; i++){
h[i] = 0;
for(j = 0; j < n; j++){
if (g[j]-f[i]>0){
h[i] += f[i]*(g[j]-f[i]);  /*convolve the two arrays*/
printf("index: %d\t convolution %d\n", i, h[i]);
}
}
}
}

int main()
{
int n;

printf("enter the number of elements in the array: ");
scanf("%d", &n);

int f[n];
int g[n];

mult(f, g, n);

int h[n];
convo(f, g, h, n);
return 0;

}```

11. > g[i] = 4 * f[i];
So what did you store in the f array between declaring it in main() and using it here in this function?

At the moment, it looks like garbage data.

12. Originally Posted by Salem
> g[i] = 4 * f[i];
So what did you store in the f array between declaring it in main() and using it here in this function?

At the moment, it looks like garbage data.
well the output is garbage, so the input must garbage too
im storing the numbers from 0 to n where n is an input from the keyboard

g[i] = 4* f[i]; is supposed to be part of the entire output, by this I mean the out put is suppose to be every 4 ( meaning every given interval, in this case 4)

the 2 functions given to create the program are
(f *g)[n] = integral of f(i)*g(j-i) in the interval from - infinity to positive in finity

the other function is f(n) = {4n: [0,N]
{0: otherwise
where n is an input from keyboard and
N defines the end of the interval to be computed

I understand that for the convolution works by taking an array and multiplying it by another array (in this case itself) whose elements are flipped and and slipped underneath the first array n positions to the left until you are at the position desired and then multiplying the 2 arrays to get the final value. (correct me if I'm wrong, or maybe just couldn't be more specific)

13. > well the output is garbage, so the input must garbage too
> im storing the numbers from 0 to n where n is an input from the keyboard
Feel free to post some code with a scanf call (or indeed any kind of input) to show us what you're doing.