📄 algorithmkf.java
字号:
return true; } /** * * * @return true * */ boolean step3() { return true; } /** * * * @return true * */ boolean step4() { return true; } /** * * * @return true * */ boolean step5() { return true; } /** * * first observation * * initialObsn = 1200; * estimateObsn = 0; * *1 newState = stateGain * currentState; * *2 varErrorNew = varErrorInitial * stateGain * stateGain + varStateNoise; * estimateObsn = measureGain * newState; * *3 kalmanGain = measureGain * varErrorNew ; * kalmanGain = kalmanGain / * ( measureGain * measureGain * varErrorNew + varMeasureNoise); * *4 newerState = * newState + kalmanGain * (obsnSequence[index] - estimateObsn); * *5 double a = 0; * a = ( 1 - kalmanGain * measureGain) * ( 1 - kalmanGain * measureGain); * * varErrorNewer = * varErrorNew * a + varMeasureNoise * kalmanGain * kalmanGain; * * varErrorInitial = varErrorNewer; * currentState = newerState; * * displays the predicted signal * * @return true * */ boolean step6() { double est_obsn = 0; double kalman_gain = 0; if(set_1_d.size() > 0) { y_estimate1.removeAllElements(); curr_state_1_d = set_1_d.elementAt(0).y; curr_error_1_d = 10; for (int i = 0; i < set_1_d.size(); i++) { //Equation 1 // new_state_1_d = state_gain * curr_state_1_d; //Equation 2 // new_error_1_d = curr_error_1_d * state_gain * state_gain + var_state_noise; est_obsn = meas_gain * new_state_1_d; //Equation 3 // kalman_gain = meas_gain * new_error_1_d; kalman_gain = kalman_gain / (meas_gain * meas_gain * new_error_1_d + var_meas_noise); //Equation 4 // newer_state_1_d = new_state_1_d + kalman_gain * (set_1_d.elementAt(i).y - est_obsn); //Equation 5 // double a = (1 - kalman_gain * meas_gain) * (1 - kalman_gain * meas_gain); newer_error_1_d = new_error_1_d * a + var_meas_noise * kalman_gain * kalman_gain; MyPoint pt = new MyPoint(set_1_d.elementAt(i).x, newer_state_1_d); y_estimate1.addElement(pt); curr_error_1_d = newer_error_1_d; curr_state_1_d = newer_state_1_d; } } if(set_2_d.size() > 0) { y_estimate2.removeAllElements(); curr_state_2_d = set_2_d.elementAt(0).y; curr_error_2_d = 10; for (int i = 0; i < set_2_d.size(); i++) { //Equation 1 // new_state_2_d = state_gain * curr_state_2_d; //Equation 2 // new_error_2_d = curr_error_2_d * state_gain * state_gain + var_state_noise; est_obsn = meas_gain * new_state_2_d; //Equation 3 // kalman_gain = meas_gain * new_error_2_d; kalman_gain = kalman_gain / (meas_gain * meas_gain * new_error_2_d + var_meas_noise); //Equation 4 // newer_state_2_d = new_state_2_d + kalman_gain * (set_2_d.elementAt(i).y - est_obsn); //Equation 5 // double a = (1 - kalman_gain * meas_gain) * (1 - kalman_gain * meas_gain); newer_error_2_d = new_error_2_d * a + var_meas_noise * kalman_gain * kalman_gain; MyPoint pt = new MyPoint(set_2_d.elementAt(i).x, newer_state_2_d); y_estimate2.addElement(pt); curr_error_2_d = newer_error_2_d; curr_state_2_d = newer_state_2_d; } } if(set_3_d.size() > 0) { y_estimate3.removeAllElements(); curr_state_3_d = set_3_d.elementAt(0).y; curr_error_3_d = 10; for (int i = 0; i < set_3_d.size(); i++) { //Equation 1 // new_state_3_d = state_gain * curr_state_3_d; //Equation 2 // new_error_3_d = curr_error_3_d * state_gain * state_gain + var_state_noise; est_obsn = meas_gain * new_state_3_d; //Equation 3 // kalman_gain = meas_gain * new_error_3_d; kalman_gain = kalman_gain / (meas_gain * meas_gain * new_error_3_d + var_meas_noise); //Equation 4 // newer_state_3_d = new_state_3_d + kalman_gain * (set_3_d.elementAt(i).y - est_obsn); //Equation 5 // double a = (1 - kalman_gain * meas_gain) * (1 - kalman_gain * meas_gain); newer_error_3_d = new_error_3_d * a + var_meas_noise * kalman_gain * kalman_gain; MyPoint pt = new MyPoint(set_3_d.elementAt(i).x, newer_state_3_d); y_estimate3.addElement(pt); curr_error_3_d = newer_error_3_d; curr_state_3_d = newer_state_3_d; } } if(set_4_d.size() > 0) { y_estimate4.removeAllElements(); curr_state_4_d = set_4_d.elementAt(0).y; curr_error_4_d = 10; for (int i = 0; i < set_4_d.size(); i++) { //Equation 1 // new_state_4_d = state_gain * curr_state_4_d; //Equation 2 // new_error_4_d = curr_error_4_d * state_gain * state_gain + var_state_noise; est_obsn = meas_gain * new_state_4_d; //Equation 3 // kalman_gain = meas_gain * new_error_4_d; kalman_gain = kalman_gain / (meas_gain * meas_gain * new_error_4_d + var_meas_noise); //Equation 4 // newer_state_4_d = new_state_4_d + kalman_gain * (set_4_d.elementAt(i).y - est_obsn); //Equation 5 // double a = (1 - kalman_gain * meas_gain) * (1 - kalman_gain * meas_gain); newer_error_4_d = new_error_4_d * a + var_meas_noise * kalman_gain * kalman_gain; MyPoint pt = new MyPoint(set_4_d.elementAt(i).x, newer_state_4_d); y_estimate4.addElement(pt); curr_error_4_d = newer_error_4_d; curr_state_4_d = newer_state_4_d; } } // The display needs to be changed to draw a line between // the two points on the waveform. // The method is: // 1. Get the first two estimated points. // 2. Draw a straight line between these two points. // 3. Drop the first point, now take the next point. // 4. Continue with step 2 and 3 in the same way, // till you come to the end. // if(set_1_d.size() > 0) output_panel_d.addOutput( y_estimate1, Classify.PTYPE_LINE, Color.black); if(set_2_d.size() > 0) output_panel_d.addOutput( y_estimate2, Classify.PTYPE_LINE, Color.pink); if(set_3_d.size() > 0) output_panel_d.addOutput( y_estimate3, Classify.PTYPE_LINE, Color.cyan); if(set_4_d.size() > 0) output_panel_d.addOutput( y_estimate4, Classify.PTYPE_LINE, Color.magenta); output_panel_d.repaint(); // displaying "Algorithm Complete" // pro_box_d.appendMessages("Algorithm Complete " + "\n"); return true; } /** * * Calculates the interpolated points for the data inputs * * @param v input data points * @param iset interpolated data points * */ public void interpol(Vector<MyPoint> v , Vector<MyPoint> iset) { double sample; // x distance between first and last points // double d ; MyPoint r = new MyPoint(); double[] y2 = new double[v.size()]; double[] y = new double[v.size()]; double[] x = new double[v.size()]; for (int k = 0; k < v.size(); k++) { x[k] = ((MyPoint)v.elementAt(k)).x; y[k] = ((MyPoint)v.elementAt(k)).y; } // Reference: // "Numerical Recipe in C", Cambridge University Press, pp 113-116 // total time interval of the data points calculated // d = ((MyPoint)v.lastElement()).x - ((MyPoint)v.firstElement()).x ; // time step calculation // scale = iporder * kforder // sample = d / scale; spline(x, y, y2, v.size()); // iset holds new data points - interpolated values // First value added // iset.addElement(new MyPoint((MyPoint)v.firstElement())); // Increment to next Time value // r.x = sample + ((MyPoint)v.firstElement()).x; while (r.x <= ((MyPoint)v.lastElement()).x) { for(int j = 1; j < v.size(); j++) { // the New Interpolated amplitude will be calculated // for the two points within which the time intervals // lies, Hence, the condition check // if((r.x <= ((MyPoint)v.elementAt(j)).x ) && (r.x >= ((MyPoint)v.elementAt(j - 1)).x)) { r.y = 0 ; splint ((MyPoint)v.elementAt(j - 1), (MyPoint)v.elementAt(j), r, y2, (j - 1)); // New value appended to the array // iset.addElement(new MyPoint(r)); break; } } r.x = r.x + sample; } // sometimes the last element of the user entered // values is missed out hence the condition to // cross-check and put it in // if (((MyPoint)iset.lastElement()).x != ((MyPoint)v.lastElement()).x ) { iset.addElement( new MyPoint((MyPoint)v.lastElement())); } } /** * * Actually interpolates the points * * @param x array containing the x coordinates of datapoints * @param y array containing the y coordinates of datapoints * @param y2 array containing the interpolated y coordinates * @param size the size of the array to be interpolated * */ public void spline (double[] x, double[] y, double[] y2, int size ) { double[] u = new double[size]; double p; double sig; y2[0] = 0; u[0] = 0; for( int i = 1; i < size - 1; i++ ) { sig = ( x[i] - x[i - 1] ) / ( x[i + 1] - x[i - 1] ) ; p = sig * y2[i - 1] + 2 ; y2[i] = ( sig - 1) / p ; u[i] = ( y[i + 1] - y[i] ) / ( x[i + 1] - x[i] ) ; u[i] = ( 6 * u[i] / ( x[i + 1] - x[i - 1] ) - sig * u[i - 1] ) / p ; } y2[size - 1] = 0 ; // This is the back substitution loop of the tridiagonal algorithm // for(int i = size - 2; i >= 0; i-- ) { y2[i] = y2[i] * y2[i + 1] + u[i] ; } } /** * Interpolates for a point between the two known points * using Cubic Interpolation * * @param u1 start point for the interpolation * @param u2 end point for the interpolation * @param r returning point, basically the interpolated point * @param y2 array used for reassigning of r * @param i the sample number * */ public void splint ( MyPoint u1, MyPoint u2, MyPoint r, double[] y2, int i ) { double h; double a; double b; h = u2.x - u1.x ; a = ( u2.x - r.x ) / h ; b = ( r.x - u1.x ) / h ; r.y = a * u1.y + b * u2.y + ((a * a * a - a) * y2[i] + ( b * b * b - b ) * y2[i + 1] ) * ( h * h ) / 6 ; } /** * * Calculates the mean and the zero-mean data points * * @param v orginal datapoints * @param mv zero mean datapoints * * @return The average in a double * */ public double mean(Vector<MyPoint> v, Vector<MyPoint> mv) { double average = 0; MyPoint p = new MyPoint(); for(int i = 0; i < v.size(); i++ ) { average = average + ((MyPoint)v.elementAt(i)).y; } average = average / v.size(); for(int i = 0; i < v.size(); i++ ) { p.y = ((MyPoint)v.elementAt(i)).y - average; p.x = ((MyPoint)v.elementAt(i)).x; mv.addElement(new MyPoint(p)); } return average ; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -