diff --git a/src/main/java/com/thealgorithms/maths/Convolution.java b/src/main/java/com/thealgorithms/maths/Convolution.java index 93e103f8c7cf..a86ecabce933 100644 --- a/src/main/java/com/thealgorithms/maths/Convolution.java +++ b/src/main/java/com/thealgorithms/maths/Convolution.java @@ -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;
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: