Skip to content

Instantly share code, notes, and snippets.

@mhusme
Forked from anastasiia-kornilova/gaussFilter1d.java
Created September 13, 2022 13:57
Show Gist options
  • Save mhusme/6bf94e255d972b67b47f41f4b3abed98 to your computer and use it in GitHub Desktop.
Save mhusme/6bf94e255d972b67b47f41f4b3abed98 to your computer and use it in GitHub Desktop.
This is the simplest implementation of gaussian_filter1d from Python scipy library.
import static java.lang.Math.*;
public class GaussSmooth {
private static double gauss(double sigma, double x) {
double expVal = -0.5 * (pow(x, 2) / pow(sigma, 2));
return exp(expVal);
}
private static double[] gaussKernel1d(double sigma, int lw) {
double[] weights = new double[lw * 2 + 1];
double sum = 0.0;
// count weights
for (int i = -lw; i <= lw; i++) {
weights[lw + i] = gauss(sigma, -i);
sum += weights[lw +i];
}
// normalize (sum != 0)
for (int i = -lw; i <= lw; i++) {
weights[lw + i] /= sum;
}
return weights;
}
private static double[] correlate1d(double[] input, double[] weights) {
double[] output = new double[input.length];
for (int i = 0; i < input.length; i++) {
double sum = 0.0;
int radius = weights.length / 2;
for (int j = 0; j < weights.length; j++) {
if (i - radius + j < 0) {
sum += input[-(i - radius + j) - 1] * weights[j];
} else if (i - radius + j >= input.length) {
sum += input[2 * input.length - (i - radius + j) - 1] * weights[j];
} else {
sum += input[i - radius + j] * weights[j];
}
}
output[i] = sum;
}
return output;
}
public static double[] gaussian_filter1d(double[] array, float sigma, float truncate) {
int lw = (int)(truncate * sigma + 0.5f);
return correlate1d(array, gaussKernel1d(sigma, lw));
}
}
@mhusme
Copy link
Author

mhusme commented Sep 13, 2022

Nice work :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment