您好我正在尝试对加速度计数据使用低通滤波器,这将进一步用于更新对象的位置。最初我将加速度计数据存储在arraylist中,即sensordata。然后使用检索到的加速度计数据执行一些简单的数学计算,结果将存储在accelsum arraylist中。结果在accelsum中我想执行低通滤波并将其存储在低通道arraylist中,因此我实现了低通滤波方法。
现在的问题是,当我打印arraylist即低通量值时,输出是一个空列表。有人可以帮助我。感谢。
以下是代码
/*Implemented to do some action upon sensor changed*/
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
gyro_x = event.values[0];
gyro_y = event.values[1];
gyro_z = event.values[2];
} else if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
double x = event.values[0];
double y = event.values[1];
double z = event.values[2];
long timestamp = System.currentTimeMillis();
AccelData data = new AccelData(timestamp, x, y, z);
sensorData = new ArrayList();
sensorData.add(data);//sensordata arraylist contains the x,y,z values of accelerometer
for(int k=0;k<sensorData.size();k++){
accelsum.add((float) data.sum(x, y, z));//accelsum arraylist contains the power of x,y and z accelerometer values
for (int i = 0; i < accelsum.size(); i++) {
System.out.println("sum: " + accelsum.get(i));
}
for (int i = 0; accelsum.size() > 10; i++) {
lowPassVals = lowPass(accelsum, lowPassVals);
}
for(int i=0;i<10;i++){
System.out.println("lowpassvals: " +lowPassVals);
}
}
}
}
protected List<Float> lowPass(List<Float> input, List<Float> output) {
for (int i = 0; i < input.size(); i++) {
if (output.isEmpty()) {
return input;
}
output.set(i, output.get(i) + ALPHA * (input.get(i) - output.get(i)));
}
return output;
}
这是处理加速度计数据的acceldata类。
public class AccelData {
private long timestamp;
private double x;
private double y;
private double z;
/* public AccelData(SensorEvent event) {
this.timestamp = timestamp;
this.x = x;
this.y = y;
this.z = z;
this.x=event.values[0];
this.y=event.values[1];
this.z=event.values[2];
this.timestamp=event.timestamp;
}*/
public AccelData(long timestamp, double x, double y, double z) {
this.timestamp = timestamp;
this.x = x;
this.y = y;
this.z = z;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
public double sum(double x,double y,double z){
double acc_x=Math.pow(x,2);
double acc_y=Math.pow(y,2);
double acc_z=Math.pow(z,2);
double accsum = acc_x+acc_y+acc_z;
return accsum;
}
public String toString() {
return "t="+timestamp+", x="+x+", y="+y+", z="+z;
}
}
}
答案 0 :(得分:0)
原因是这一行
for (int i = 0; accelsum.size() > 10; i++)
您永远不会进入此循环,因为条件永远不会有效。