我正在尝试实现 Tom Nichols 的低通滤波器。我的方法是这样的,但我一直得到一个越界异常,即使我已经提防它:
private float filterValue(LinkedList<Float> listOfItems)
{
final float ALPHA = 0.2f;
float filtered;
int marker = listOfItems.size();
if (listOfItems.size()>2)
marker = listOfItems.size() - 1;
float current = listOfItems.pollLast();
float previous = listOfItems.get(marker); <<-- line 282
filtered = (float) previous + ALPHA*(current - previous);
return filtered;
}
错误日志:
12-16 15:54:51.148: E/AndroidRuntime(2055): FATAL EXCEPTION: main
12-16 15:54:51.148: E/AndroidRuntime(2055): java.lang.IndexOutOfBoundsException
12-16 15:54:51.148: E/AndroidRuntime(2055): at java.util.LinkedList.get(LinkedList.java:519)
12-16 15:54:51.148: E/AndroidRuntime(2055): at se.macke.velocitysensor.SensorActivity$MySensorEventListener.filterValue(SensorActivity.java:282)
12-16 15:54:51.148: E/AndroidRuntime(2055): at se.macke.velocitysensor.SensorActivity$MySensorEventListener.getAccelerometer(SensorActivity.java:249)
12-16 15:54:51.148: E/AndroidRuntime(2055): at se.macke.velocitysensor.SensorActivity$MySensorEventListener.onSensorChanged(SensorActivity.java:231)
12-16 15:54:51.148: E/AndroidRuntime(2055): at android.hardware.SystemSensorManager$ListenerDelegate$1.handleMessage(SystemSensorManager.java:204)
12-16 15:54:51.148: E/AndroidRuntime(2055): at android.os.Handler.dispatchMessage(Handler.java:99)
建议,请。
/米