滑動移動均值濾波簡單代碼實例

滑動移動均值濾波簡單代碼實例

    private  float[] mBufout = null;
    private final int mWindowSize = 2;

    static float filter_buf[]=  {10,20,30,40,50,60,70,80,90};

    // 移動均值濾波,原始數組最後的mwindowSize-1 個元素沒有處理。
    public float[] movingAverageFilter(float[] buf) {

        float[] winArray = new float[mWindowSize];

         mBufout = new float[buf.length];
         int OIndex = 0;

         System.arraycopy(buf, 0, mBufout, 0,
                     buf.length);

        for (int i = 0; i < buf.length; i++) {

            int wIndex = 0;


        if ((i + mWindowSize) > buf.length) break;

        for (int j = i; j < (mWindowSize + i); j++) {
            winArray[wIndex] = buf[j];
            wIndex ++;
        }

        mBufout[OIndex] = mean(winArray);
        OIndex ++;

    }

        return mBufout;

    }


    public static float mean(float[] array) {
        long sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        return (float) (sum / array.length);
    }




  public static void main(String[] args)
  {
    test A = new test();

     System.out.println("hi test");
     float[] ta;
     ta =     A.movingAverageFilter(filter_buf);

     for(int i =0;i<9;i++) {
       System.out.println("after filted:"+ta[i]);
     }
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章