From 434ab50ff4d34c50e19a39f4a4d1f3a4df459e22 Mon Sep 17 00:00:00 2001 From: Oleksandr Klymenko Date: Wed, 16 Jul 2025 18:05:13 +0300 Subject: [PATCH] refactor: `Convolution` (#6382) refactor: Convolution Co-authored-by: Deniz Altunkapan <93663085+DenizAltunkapan@users.noreply.github.com> --- .../com/thealgorithms/maths/Convolution.java | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Convolution.java b/src/main/java/com/thealgorithms/maths/Convolution.java index 93e103f8c..a86ecabce 100644 --- a/src/main/java/com/thealgorithms/maths/Convolution.java +++ b/src/main/java/com/thealgorithms/maths/Convolution.java @@ -23,24 +23,21 @@ public final class Convolution { 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); - while (k < i + 1 && k < a.length) { - convolved[i] += a[k] * b[i - k]; - k++; + for (int i = 0; i < convolved.length; i++) { + double sum = 0; + int kStart = Math.max(0, i - b.length + 1); + int kEnd = Math.min(i, a.length - 1); + + for (int k = kStart; k <= kEnd; k++) { + sum += a[k] * b[i - k]; } + + convolved[i] = sum; } return convolved;