Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/main/java/com/thealgorithms/maths/Convolution.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,21 @@ public static double[] convolution(double[] a, double[] b) {
double[] convolved = new double[a.length + b.length - 1];

/*
The discrete convolution of two signals A and B is defined as:

A.length
C[i] = Σ (A[k]*B[i-k])
k=0

It's obvious that: 0 <= k <= A.length , 0 <= i <= A.length + B.length - 2 and 0 <= i-k <=
B.length - 1 From the last inequality we get that: i - B.length + 1 <= k <= i and thus we get
the conditions below.
* Discrete convolution formula:
* C[i] = Σ A[k] * B[i - k]
* where k ranges over valid indices so that both A[k] and B[i-k] are in bounds.
*/

for (int i = 0; i < convolved.length; i++) {
convolved[i] = 0;
int k = Math.max(i - b.length + 1, 0);
double sum = 0;
int kStart = Math.max(0, i - b.length + 1);
int kEnd = Math.min(i, a.length - 1);

while (k < i + 1 && k < a.length) {
convolved[i] += a[k] * b[i - k];
k++;
for (int k = kStart; k <= kEnd; k++) {
sum += a[k] * b[i - k];
}

convolved[i] = sum;
}

return convolved;
Expand Down
Loading