Autocorrelation { public void evaluate(List<Datum> data) { double[] values = formatData(data); Complex[] fft = fftTran.transform(values, TransformType.FORWARD); for (int i = 0; i < fft.length; i ++) { fft[i] = fft[i].multiply(fft[i].conjugate()); } fft = fftTran.transform(fft, TransformType.INVERSE); correlations = new double[maxLag]; for (int i = 1; i < maxLag; i++) { correlations[i] = fft[i].getReal() / fft[0].getReal(); } } Autocorrelation(int maxLag, int metricIdx); void setMaxLag(int lag); void setCorrelationThreshold(double thresh); void evaluate(List<Datum> data); List<Integer> findPeaks(); public double[] correlations; public double maxACF; }
@Test public void testACF() throws Exception { int n = 1000; double[] test_input = new double[n]; for (int i = 0; i < n; i ++) { if (i % 2 == 0) { test_input[i] = 1; } else { test_input[i] = -1; } } List<Datum> data = new ArrayList<>(); for (int i = 0; i < n; i ++) { double[] value = new double[1]; value[0] = test_input[i]; Datum d = new Datum(new ArrayList<>(), new ArrayRealVector(value)); data.add(d); } Autocorrelation acf = new Autocorrelation(10, 0); acf.evaluate(data); double[] expected_return = {0, -0.999, 0.998, -0.997, 0.996, -0.995, 0.994, -0.993, 0.992, -0.991}; assertArrayEquals(expected_return, acf.correlations, 1e-5); }