2013-08-21 58 views
0

我開發了一個步驟檢測應用程序起訴android。我已經過濾了我的傳感器數據(加速度計數據x,y,z),並需要使用5點平滑算法來平滑信號。我在一份研究報告中看到了它。我有關於該算法的搜索,並找不到可以使用android(java)的適當資源。任何人都可以給我一個幫助,我可以做到這一點。平滑算法與Android

這裏是我的代碼來獲得加速計數據和低通濾波器

package com.android.gait; 

import org.achartengine.GraphicalView; 

import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorListener; 

import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.view.Menu; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

public class MainActivity extends Activity implements SensorEventListener{ 

    static final float ALPHA = 0.15f; 
    private int count=0; 
    private static GraphicalView view; 
    private LineGraph line = new LineGraph(); 
    private static Thread thread; 
    private SensorManager mSensorManager; 
    private Sensor mAccelerometer; 
    TextView title,tv,tv1,tv2; 
    RelativeLayout layout; 
    private double a; 
    static float m = 0; 
    private float p,q,r; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     //get the sensor service 
     mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
     //get the accelerometer sensor 
     mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
     //get layout 
     layout = (RelativeLayout)findViewById(R.id.relative); 
     LinearLayout layout = (LinearLayout) findViewById(R.id.layoutC); 
     view= line.getView(this); 
     layout.addView(view); 
     //get textviews 
     title=(TextView)findViewById(R.id.name); 
     tv=(TextView)findViewById(R.id.xval); 
     tv1=(TextView)findViewById(R.id.yval); 
     tv2=(TextView)findViewById(R.id.zval); 

     thread = new Thread(){ 
      int iniX=0; 
      public void run() 
      { 
       while(true) 
       { 

        try { 
         Thread.sleep(1); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        iniX=+1; 

        line.addNewPoint(iniX,m); 
        view.repaint(); 
       } 
      } 


     }; 

     thread.start(); 


    } 

    public final void onAccuracyChanged(Sensor sensor, int accuracy) 
    { 
     // Do something here if sensor accuracy changes. 
    } 
    @Override 
    public final void onSensorChanged(SensorEvent event) 
    { 
     count=+1; 
     // Many sensors return 3 values, one for each axis. 


     float x = event.values[0]; 
     float y = event.values[1]; 
     float z = event.values[2]; 



     float[] first={x,y,z}; 
     float[] larst={p,q,r}; 

    larst= lowPass(first,larst); 
    //double FY= b.Filter(y); 
    //double FZ= b.Filter(z); 

    //get merged value 
      m = (float) Math.sqrt(larst[0]*larst[0]+larst[1]*larst[1]+larst[2]*larst[2]); 



     //display values using TextView 
     title.setText(R.string.app_name); 
     tv.setText("X axis" +"\t\t"+larst[0]); 
     tv1.setText("Y axis" + "\t\t" +larst[1]); 
     tv2.setText("Z axis" +"\t\t" +larst[2]); 
    } 

    @Override 
    protected void onResume() 
    { 
     super.onResume(); 
     mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); 
    } 
    @Override 
    protected void onPause() 
    { 
     super.onPause(); 
     mSensorManager.unregisterListener(this); 
    } 

    public void LineGraphHandler(View view){ 



    } 

    //Low pass filter 
    protected float[] lowPass(float[] input, float[] output) { 
     if (output == null) return input; 

     for (int i=0; i<input.length; i++) { 
      output[i] = output[i] + ALPHA * (input[i] - output[i]); 
     } 
     return output; 
    } 
    /*@Override 
    public void onStart(){ 
     super.onStart();  
     view= line.getView(this); 
     setContentView(view); 
    }*/ 

} 
+0

您錯誤地將「last」拼寫爲「larst」。 – stackoverflowuser2010

+0

它只是一個變量 – dit1679

+0

另一個錯字:它的,而不是它的 – stackoverflowuser2010

回答

0

N點平滑算法簡單地返回當前存儲的n個數據點的平均過濾。

就你而言,n = 5。假設你已經存儲到最新的放在最古老的秩序5個數據點:

目前的數據:1,3,5,4,9(其中最近讀的數據是9)

進一步假設你現在讀入數據點7.所有的數據現在被向左推動一個位置,最老的數據點被完全移除。數據點現在是:

當前數據:3,5,4,9,7

現在計算這些值的平均值,(3 + 5 + 4 + 9 + 7)/ 5 = 5.6,這是新的平滑價值。每次添加新值時,都應計算並讀取新的平滑值。你閱讀的數據流都是平滑的。注意事實上,如果您正在執行數據點和1/5係數之間的卷積,實際上,一個離散FIR濾波器(您提到的)將執行精確的平滑(平均)操作。也就是說,我上面描述的平均值(3 + 5 + 4 + 9 + 7)/ 5與(3,5,4,9,7)和(1/5,1)的卷積完全相同/ 5,1/5,1/5,1/5)。卷積計算爲3 * 1/5 + 5 * 1/5 + 4 * 1/5 + 9 * 1/5 + 7 * 1/5 = 5.6

+0

這將返回最後5個值的平均值。這可能不是OP應用程序的最佳平滑功能。 而不是使用值(1/5,1/5,1/5,1/5,1/5),OP可能想要更加重視更新的值。說(1/3,1/4,1/6,1/6,1/12)或其他一些更高度加權最近值的卷積。這會以不太平滑的數據爲代價提高UI的響應速度。在他的應用程序中效果最好的只能通過試驗和錯誤來確定。 –