LpcFeaturesExtractor extends WindowedFeaturesExtractor<double[]> { @Override public double[] extractFeatures(double[] voiceSample) { double[] voiceFeatures = new double[poles]; double[] audioWindow = new double[windowSize]; int counter = 0; int halfWindowLength = windowSize / 2; for (int i = 0; (i + windowSize) <= voiceSample.length; i += halfWindowLength) { System.arraycopy(voiceSample, i, audioWindow, 0, windowSize); windowFunction.applyFunction(audioWindow); double[] lpcCoeffs = lpc.applyLinearPredictiveCoding(audioWindow)[0]; for (int j = 0; j < poles; j++) { voiceFeatures[j] += lpcCoeffs[j]; } counter++; } if (counter > 1) { for (int i = 0; i < poles; i++) { voiceFeatures[i] /= counter; } } return voiceFeatures; } LpcFeaturesExtractor(float sampleRate, int poles); @Override double[] extractFeatures(double[] voiceSample);  }
@Test public void lpcExtractorUsesWindowsOfCorrectLengthForMostUsedSampleRates() { final int[] i = new int[1]; new MockUp<WindowFunction>() { @Mock void applyFunction(double[] window) { assertThat(window.length, is(equalTo(WINDOW_LENGTHS[i[0]]))); } }; while(i[0] < SAMPLE_RATES.length) { lpc = new LpcFeaturesExtractor(SAMPLE_RATES[i[0]], POLES); lpc.extractFeatures(new double[1024]); i[0]++; } }
@Test public void lpcFeaturesAreAveragesOnTheLpcCoefficientsReturnedByLinearPredictiveCodingAlgorithm() { lpc = new LpcFeaturesExtractor(DEFAULT_SAMPLE_RATE, POLES); double[] voiceSample = new double[4096]; new MockUp<LinearPredictiveCoding>() { private int value = 1; @Mock double[][] applyLinearPredictiveCoding(double[] window) { double[][] result = new double[2][20]; Arrays.fill(result[0], value); value++; return result; } }; double[] reference = new double[20]; Arrays.fill(reference, 8.0d); double[] features = lpc.extractFeatures(voiceSample); assertThat(features, is(equalTo(reference))); }