AccelerometerFragment extends Fragment implements
        View.OnClickListener, SensorEventListener { static double[] findShifts(List<Entry> phoneEntries, List<Entry> waltEntries) { double[] phoneTimes = new double[phoneEntries.size()]; double[] phoneValues = new double[phoneEntries.size()]; double[] waltTimes = new double[waltEntries.size()]; double[] waltValues = new double[waltEntries.size()]; for (int i = 0; i < phoneTimes.length; i++) { phoneTimes[i] = phoneEntries.get(i).getX(); phoneValues[i] = phoneEntries.get(i).getY(); } for (int i = 0; i < waltTimes.length; i++) { waltTimes[i] = waltEntries.get(i).getX(); waltValues[i] = waltEntries.get(i).getY(); } double[] shiftCorrelations = new double[401]; for (int i = 0; i < shiftCorrelations.length; i++) { double shift = i / 10.; final double[] shiftedPhoneTimes = new double[phoneTimes.length]; for (int j = 0; j < phoneTimes.length; j++) { shiftedPhoneTimes[j] = phoneTimes[j] - shift; } final double[] interpolatedValues = interp(shiftedPhoneTimes, waltTimes, waltValues); double sum = 0; for (int j = 0; j < shiftedPhoneTimes.length; j++) { sum += Math.pow(phoneValues[j] * interpolatedValues[j], 2); } shiftCorrelations[i] = sum; } return shiftCorrelations; } AccelerometerFragment(); @Override View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState); @Override void onResume(); @Override void onPause(); void appendLogText(String msg); @Override void onClick(View v); @Override void onSensorChanged(SensorEvent event); @Override void onAccuracyChanged(Sensor sensor, int accuracy);  }
@Test public void testFindShifts() { Random rand = new Random(5678); List<Entry> phoneEntries = new ArrayList<>(); List<Entry> waltEntries = new ArrayList<>(); for (int i = 0; i < 1000; i++) { if (i % 3 == 0) phoneEntries.add(new Entry(i, (float) Math.sin((i - 12)*Math.PI/100))); waltEntries.add(new Entry(i, (float) Math.sin(i*Math.PI/100)*rand.nextFloat() + rand.nextFloat()*0.2f - 0.1f)); } final double[] shifts = AccelerometerFragment.findShifts(phoneEntries, waltEntries); for (double d : shifts) { System.out.println(d); } assertEquals(12, Utils.argmax(shifts)/10d, 1e-9); }