Deconvolution/1D: Difference between revisions

Content added Content deleted
(→‎{{header|Java}}: fixed error in deconv method where input parameters were reversed; changed array types from double[] to int[]; updated output; removed "untested" banner.)
Line 555: Line 555:


=={{header|Java}}==
=={{header|Java}}==
{{untested|Java}}{{trans|Go}}
{{trans|Go}}
<lang java>import java.util.Arrays;
<lang java>import java.util.Arrays;


public class Deconvolution1D {
public class Deconvolution1D {
public static double[] deconv(double[] f, double[] g) {
public static int[] deconv(int[] g, int[] f) {
double[] h = new double[g.length - f.length + 1];
int[] h = new int[g.length - f.length + 1];
for (int n = 0; n < h.length; n++) {
for (int n = 0; n < h.length; n++) {
h[n] = g[n];
h[n] = g[n];
int lower = Math.max(n - f.length + 1, 0);
int lower = Math.max(n - f.length + 1, 0);
for (int i = lower; i < n; i++)
for (int i = lower; i < n; i++)
h[n] -= h[i] * f[n-i];
h[n] -= h[i] * f[n - i];
h[n] /= f[0];
h[n] /= f[0];
}
}
Line 572: Line 572:


public static void main(String[] args) {
public static void main(String[] args) {
double[] h = {-8, -9, -3, -1, -6, 7};
int[] h = { -8, -9, -3, -1, -6, 7 };
double[] f = {-3, -6, -1, 8, -6, 3, -1, -9, -9, 3, -2, 5, 2, -2, -7, -1};
int[] f = { -3, -6, -1, 8, -6, 3, -1, -9, -9, 3, -2, 5, 2, -2, -7, -1 };
double[] g = {24, 75, 71, -34, 3, 22, -45, 23, 245, 25, 52, 25, -67, -96,
int[] g = { 24, 75, 71, -34, 3, 22, -45, 23, 245, 25, 52, 25, -67, -96,
96, 31, 55, 36, 29, -43, -7};
96, 31, 55, 36, 29, -43, -7 };

System.out.println(Arrays.toString(h));
StringBuilder sb = new StringBuilder();
System.out.println(Arrays.toString(deconv(g, f)));
System.out.println(Arrays.toString(f));
sb.append("h = " + Arrays.toString(h) + "\n");
System.out.println(Arrays.toString(deconv(g, h)));
sb.append("deconv(g, f) = " + Arrays.toString(deconv(g, f)) + "\n");
sb.append("f = " + Arrays.toString(f) + "\n");
sb.append("deconv(g, h) = " + Arrays.toString(deconv(g, h)) + "\n");
System.out.println(sb.toString());
}
}
}</lang>
}</lang>
Output:
Output:
<pre>
<pre>
[-8.0, -9.0, -3.0, -1.0, -6.0, 7.0]
h = [-8, -9, -3, -1, -6, 7]
[-8.0, -9.0, -3.0, -1.0, -6.0, 7.0]
deconv(g, f) = [-8, -9, -3, -1, -6, 7]
[-3.0, -6.0, -1.0, 8.0, -6.0, 3.0, -1.0, -9.0, -9.0, 3.0, -2.0, 5.0, 2.0, -2.0, -7.0, -1.0]
f = [-3, -6, -1, 8, -6, 3, -1, -9, -9, 3, -2, 5, 2, -2, -7, -1]
[-3.0, -6.0, -1.0, 8.0, -6.0, 3.0, -1.0, -9.0, -9.0, 3.0, -2.0, 5.0, 2.0, -2.0, -7.0, -1.0]
deconv(g, h) = [-3, -6, -1, 8, -6, 3, -1, -9, -9, 3, -2, 5, 2, -2, -7, -1]
</pre>
</pre>