Autocorrelation { public List<Integer> findPeaks() { List<Integer> peaks = new ArrayList<>(); int max = 1; maxACF = 0; if (correlations.length > 1) { boolean positive = (correlations[1] > correlations[0]); for (int i = 2; i < correlations.length; i++) { if (!positive && correlations[i] > correlations[i - 1]) { max = i; positive = !positive; } else if (positive && correlations[i] > correlations[max]) { max = i; } else if (positive && correlations[i] < correlations[i - 1]) { if (max > 1 && correlations[max] > ACF_THRESH) { peaks.add(max); if (correlations[max] > maxACF) { maxACF = correlations[max]; } } positive = !positive; } } } return peaks; } 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 testFindPeak() throws Exception { double[] test_correlations = {0, 0.9, 0.6, 0.7, 0.8, 0.5, 0, 0.1, 0.2, -0.5, -0.1, 0, -0.5, 0.8, 0, 0.8, 0.7, 0.6, 0.5, 0.4, 0.5}; Autocorrelation acf = new Autocorrelation(test_correlations.length, 0); acf.correlations = new double[test_correlations.length]; for (int i = 0; i < test_correlations.length; i ++) { acf.correlations[i] = test_correlations[i]; } List<Integer> peaks = acf.findPeaks(); int[] expected_peaks = {4, 13, 15}; assertEquals(expected_peaks.length, peaks.size()); for (int i = 0; i < peaks.size(); i ++) { assertTrue(expected_peaks[i] == peaks.get(i)); } }