public class Smooth {
private static ArrayList<double[]> resultlist = new ArrayList<>();//平滑后集合
public Smooth(ArrayList<double[]> resultlist) {
this.resultlist = resultlist;
}
public static ArrayList<double[]> getResultlist() {
return resultlist;
}
/* X = {n∧i} -M<=n<=M,0<=i<=N */
public static RealMatrix getXMatrix(int n, int k) {//辅助矩阵 X矩阵,(2M+1)行(N+1)列
int step = (n - 1) / 2;
RealMatrix realMatrix = MatrixUtils.createRealMatrix(n, k-1 );
for (int i = 0; i < realMatrix.getRowDimension(); i++) {
for (int j = 0; j < k-1 ; j++) {
realMatrix.setEntry(i, j, Math.pow(i - step, j));
}
}
return realMatrix;
}
/* B = X*(XT*X)(-1)*XT */
public static RealMatrix filterCoeff(int n,int k) {//B为滤波系数矩阵,由且仅由X矩阵决定,B矩阵为(2m+1)×(2m+1)阶矩阵, n=2m+1
RealMatrix realMatrix = getXMatrix(n,k);
RealMatrix B = realMatrix.multiply(inverse(realMatrix.transpose().
multiply(realMatrix))).multiply(realMatrix.transpose());
return B;
}
/* s = B*Y
步骤:1.判断是否为n是否为合法(为奇数),不合法即跳出
2.将数据进行处理
3.判断窗口是否在两端,若为两端,拟合结果为平滑结果;若不在两端,仅取中间点作为拟合数据
4.输出
* s 平滑后列矩阵
* Y 待平滑列矩阵*/
public static Smooth SGResult(int n, int k, ArrayList<double[]> testlist){//resultlist为平滑后集合
ArrayList<double[]> resultlist = new ArrayList<double[]>();
for (int i = 1; i < testlist.get(1).length; i++){//列,数据类数
for (int j = (n-1/2)-1; j < testlist.size()-n; j++){//行,数据数
for (int m = j; m <= j+n; m++){//窗口
double[][]y = new double[n][1];
for (int count = 0;count < n;count++){
y[count][0] = testlist.get(m)[i];
}
RealMatrix Y = new Array2DRowRealMatrix(y);
RealMatrix B = filterCoeff(n,k);
RealMatrix S = B.multiply(Y);//B和Y相乘
testlist.get(j)[i]=S.getEntry((n-1)/2,0);
for (int a = 0; a < (n-1)/2; a++){
testlist.get(a)[i]=S.getEntry(a,0);
}
for (int a = testlist.size()-1-((n-1)/2); a < testlist.size() ; a++){
for (int b = (n-1)/2; b < n;b++){
testlist.get(testlist.size()-1-((n-1)/2))[i]=S.getEntry(b,0);
}
}
}
}
}
resultlist = testlist;
return new Smooth(resultlist);
}
}
数据平滑处理(自用)
于 2022-02-22 15:45:40 首次发布