利用移動平均濾波器對列向量y進行平滑處理,返回與y等長的列向量yy。移動平均濾波器的默認窗寬為5,yy中元素的計算方法如下:
yy(1) = y(1)
yy(2)=(y(1) + y(2) + y(3))/3
yy(3) = (y(1) 十y(2) 十y(3) + y(4)十y(5))/5
yy(4) = (y(2) + y(3)十y(4) + y(5) + y(6))/5
yy(5) = (y(3) + y(4)十y(5)十y(6) +y(7))/5
...
java實現
/**
* <p>
* Title: smooth
* </p>
* <p>
* Description: 數組平滑處理
* </p>
*
* @param d
* @return
*/
public static double[] smooth(double[] d) {
int length = d.length;
double[] dbRt = new double[length];
if (length == 1) {
/** 前置與后置無元素 index=0與index=length-1 */
dbRt[0] = d[0];
dbRt[length - 1] = d[length - 1];
}
if (length == 2) {
/** 前置與后置無元素 index=0與index=length-1 */
dbRt[0] = d[0];
dbRt[length - 1] = d[length - 1];
}
if (length == 3) {
/** 前置與后置無元素 index=0與index=length-1 */
dbRt[0] = d[0];
dbRt[length - 1] = d[length - 1];
/** 前置與后置只有一個元素 index=1 與index=length-2 */
dbRt[1] = (d[0] + d[1] + d[2]) / 3;
}
if (length == 4) {
/** 前置與后置無元素 index=0與index=length-1 */
dbRt[0] = d[0];
dbRt[length - 1] = d[length - 1];
/** 前置與后置只有一個元素 index=1 與index=length-2 */
dbRt[1] = (d[0] + d[1] + d[2]) / 3;
dbRt[2] = (d[1] + d[2] + d[3]) / 3;
}
if (length >= 5) {
/** 前置與后置無元素 index=0與index=length-1 */
dbRt[0] = d[0];
/** 前置與后置只有一個元素 index=1 與index=length-2 */
dbRt[1] = (d[0] + d[1] + d[2]) / 3;
for (int x = 2; x < length - 2; x++) {
/** 前置與后置均由兩個元素情況 d[n] n-2>=0且n+2<=length-1 */
dbRt[x] = (d[x - 2] + d[x - 1] + d[x] + d[x + 1] + d[x + 2]) / 5;
}
dbRt[length - 2] = (d[length - 3] + d[length - 2] + d[length - 1]) / 3;
dbRt[length - 1] = d[length - 1];
}
return dbRt;
}
public static void main(String[] args) {
double[] dbPara = { 1, 2, 100, 10, 50, 20 };
double[] dbRt = smooth(dbPara);
for (double dbIndex : dbRt) {
System.out.println(dbIndex);
}
}