📄 algorithmpf.java,v
字号:
{ // do the first step manually // double sum = 0; double tmp = 0; sum = -auto_coeff[1] / err_energy; rc_reg[1] = sum; pfc[1] = rc_reg[1]; tmp = 1 - rc_reg[1] * rc_reg[1]; err_energy *= tmp; // recursion // for (int i = 2; i <= pforder; i++) { sum = 0; for (int j = 1; j < i; j++) sum += pfc[j] * auto_coeff[i - j]; rc_reg[i] = -(auto_coeff[i] + sum) / err_energy; pfc[i] = rc_reg[i]; j_bckwd = i - 1; middle_index = i / 2; for (int j = 1; j <= middle_index; j++) { sum = pfc[j_bckwd] + rc_reg[i] * pfc[j]; pfc[j] = pfc[j] + rc_reg[i] * pfc[i - j]; pfc[j_bckwd] = sum; j_bckwd--; } // compute new error // tmp = 1.0 - rc_reg[i] * rc_reg[i]; err_energy *= tmp; } } return err_energy; } /** * * Calculates the estimated points for the data inputs * */ public void final_estimate() { if(iset_1_d.size() > 0) estimate (iset_1_d, y_estimate1, average1, final_pfc_1); if(iset_2_d.size() > 0) estimate (iset_2_d, y_estimate2, average2, final_pfc_2); if(iset_3_d.size() > 0) estimate (iset_3_d, y_estimate3, average3, final_pfc_3); if(iset_4_d.size() > 0) estimate (iset_4_d, y_estimate4, average4, final_pfc_4); } /** * * Estimates the amplitude based on the PF coeficients. * * @@param iset interpolated data points * @@param y_estimate predicted final signal data points * @@param avg mean of the original datapoints given * @@param final_pfc array of final particle filtering coefficients * */ public void estimate( Vector<MyPoint> iset, Vector<MyPoint> y_estimate, double avg, double[] final_pfc) { y_estimate.removeAllElements(); double amplitude[]; for(int i = 0; i < iset.size(); i++) y_estimate.addElement(new MyPoint((MyPoint)iset.elementAt(i))); amplitude = new double[y_estimate.size()]; for (int i = 0; i < y_estimate.size(); i++) amplitude[i] = ((MyPoint)y_estimate.elementAt(i)).y; ((MyPoint)y_estimate.firstElement()).y = 0 ; for ( int i = 1; i < y_estimate.size(); i++ ) { int z = i; double sum = 0; for( int j = 1; (j <= pforder) && (z > 0); j++, z-- ) sum = sum - amplitude[z - 1] * final_pfc[j]; ((MyPoint)y_estimate.elementAt(i)).y = sum + avg; } } /** * * Compute the actual error from the given data points and the estimated * values. * * @@param y_estimate datapoints of the estimated datapoints * @@param iset original datapoints * * @@return actual error energy in a double */ public double actual_error (Vector<MyPoint> y_estimate, Vector<MyPoint> iset) { double error_value; double act_error = (double) 0; int j = 0; int i = 0; // first point and the last point have their x-coordinates same // So the difference in y-values is the error value // error_value = ((MyPoint)y_estimate.firstElement()).y - ((MyPoint)iset.firstElement()).y; act_error = act_error + error_value * error_value; error_value = ((MyPoint)y_estimate.lastElement()).y - ((MyPoint)iset.lastElement()).y; act_error = act_error + error_value * error_value; // for next values // need to continue till all the user defined points are exhausted // j++; for (i = 1; ( (i < y_estimate.size()) && (j < iset.size()) ); i++) { while ( (((MyPoint)y_estimate.elementAt(i)).x < ((MyPoint)iset.elementAt(j)).x) && ((i < y_estimate.size() - 1) && (j < iset.size())) ) { i++; } if ( j < iset.size()) { if ( ((MyPoint)y_estimate.elementAt(i)).x == ((MyPoint)iset.elementAt(j)).x ) { error_value = ((MyPoint)y_estimate.elementAt(i)).y - ((MyPoint)iset.elementAt(j)).y; act_error = act_error + error_value * error_value; } if ( ((MyPoint)y_estimate.elementAt(i)).x > ((MyPoint)iset.elementAt(j)).x ) { double y1 = ((MyPoint)y_estimate.elementAt(i)).y; double y2 = ((MyPoint)y_estimate.elementAt(i - 1)).y; double x1 = ((MyPoint)y_estimate.elementAt(i)).x; double x2 = ((MyPoint)y_estimate.elementAt(i - 1)).x; double x_unknown = ((MyPoint)iset.elementAt(j)).x; error_value = y2 - ( (x2 - x_unknown) * ( y2 - y1) / (x2 - x1)); act_error = act_error + error_value * error_value ; } } j++; } return act_error; } /** * * Displays PF order, Error Energy and Reflection Coefficients * */ public void step2_display() { // display the PF order // pro_box_d.appendMessages(" PF order = " + pforder + "\n"); if (set_1_d.size() > 0 ) { int num_pts = iset_1_d.size(); int n = 0; display_result(auto_co_1, ref_coeff_1, final_pfc_1, actual_err_1, estimate_err_1, n, num_pts); } if (set_2_d.size() > 0 ) { int num_pts = iset_2_d.size(); int n = 1; display_result(auto_co_2, ref_coeff_2, final_pfc_2, actual_err_2, estimate_err_2, n, num_pts); } if (set_3_d.size() > 0 ) { int num_pts = iset_3_d.size(); int n = 2; display_result(auto_co_3, ref_coeff_3, final_pfc_3, actual_err_3, estimate_err_3, n, num_pts); } if (set_4_d.size() > 0 ) { int num_pts = iset_4_d.size(); int n = 3; display_result(auto_co_4, ref_coeff_4, final_pfc_4, actual_err_4, estimate_err_4, n, num_pts); } } /** * Display the results in the process box * * @@param auto_coeff Auto Correlation Coefficients * @@param refCoef Refelction Coefficient * @@param final_pfc Particle Filering Coefficients * @@param est_err Estimated Error * @@param act_err Actual Error * @@param length Length of the data points * */ public void display_result( double[] auto_coeff, double[] refCoef, double[] final_pfc, double est_err, double act_err, int index, int length) { pro_box_d.appendMessages("\n" + " Class " + index + " : \n"); pro_box_d.appendMessages(" Number of Points = " + length + "\n"); pro_box_d.appendMessages( " AutoCorrelation Coefficients:" + "\n"); pro_box_d.appendMessages(" [ "); for (int i = 0 ; i <= pforder; i++) if (i == pforder) pro_box_d.appendMessages(MathUtil.SetDecimal(auto_coeff[i], 3) + " "); else pro_box_d.appendMessages(MathUtil.SetDecimal(auto_coeff[i], 3) + ", "); pro_box_d.appendMessages(" ]"); pro_box_d.appendMessages("\n" + " Reflection Coefficients:" + "\n"); pro_box_d.appendMessages(" [ "); for (int i = 1 ; i <= pforder; i++) if (i == pforder) pro_box_d.appendMessages(MathUtil.SetDecimal(refCoef[i], 3) + " "); else pro_box_d.appendMessages(MathUtil.SetDecimal(refCoef[i], 3) + ", "); pro_box_d.appendMessages(" ]"); pro_box_d.appendMessages("\n" + " Filtering Coefficients:" + "\n"); pro_box_d.appendMessages(" [ "); for (int i = 0 ; i <= pforder; i++) if (i == pforder ) pro_box_d.appendMessages(MathUtil.SetDecimal(final_pfc[i], 3) + " "); else pro_box_d.appendMessages(MathUtil.SetDecimal(final_pfc[i], 3) + ", "); pro_box_d.appendMessages(" ]"); pro_box_d.appendMessages("\n" + " Estimated Error Energy = " + MathUtil.SetDecimal(act_err, 3)); pro_box_d.appendMessages("\n" + " Actual Error Energy = " + MathUtil.SetDecimal(est_err, 3) + "\n"); }}@1.3log@changed display of initialization.@text@d362 3d371 3@1.2log@implemented Kalman filter.@text@a12 1 *d294 10a303 1 String str = new String("**** Under Construction **** \n Kalman Filter implementation 0. \n Checking for the Data Validity. ");@1.1log@Initial revision@text@d7 7d16 33d89 4a92 4 Vector<MyPoint> set1_d = new Vector<MyPoint>(40, 20); Vector<MyPoint> set2_d = new Vector<MyPoint>(40, 20); Vector<MyPoint> set3_d = new Vector<MyPoint>(40, 20); Vector<MyPoint> set4_d = new Vector<MyPoint>(40, 20);d96 4a99 4 Vector<MyPoint> iset1 = new Vector<MyPoint>(40, 20); Vector<MyPoint> iset2 = new Vector<MyPoint>(40, 20); Vector<MyPoint> iset3 = new Vector<MyPoint>(40, 20); Vector<MyPoint> iset4 = new Vector<MyPoint>(40, 20); d103 4a106 4 Vector<MyPoint> mset1 = new Vector<MyPoint>(40, 20); Vector<MyPoint> mset2 = new Vector<MyPoint>(40, 20); Vector<MyPoint> mset3 = new Vector<MyPoint>(40, 20); Vector<MyPoint> mset4 = new Vector<MyPoint>(40, 20); d110 4a113 4 Vector<MyPoint> display_set1 = new Vector<MyPoint>(40, 20); Vector<MyPoint> display_set2 = new Vector<MyPoint>(40, 20); Vector<MyPoint> display_set3 = new Vector<MyPoint>(40, 20); Vector<MyPoint> display_set4 = new Vector<MyPoint>(40, 20); d116 1a116 1 //d158 74a231 1 // to store the final resultsd262 1a262 1 step_count = 3;d265 6d273 11d288 8a295 1 String str = new String(" 0. Checking for the Data Validity.");d298 1a298 1 str = new String(" 1. Displaying the Input Data Points.");d301 1a301 1 str = new String(" 2. Computing PF Parameters.");d304 7a310 1 str = new String(" 3. Displaying the Particle Filtered Signal");d313 4d326 4a329 4 set1_d = data_points_d.dset1; set2_d = data_points_d.dset2; set3_d = data_points_d.dset3; set4_d = data_points_d.dset4;d332 1a332 1 if (set1_d.size() > set2_d.size())d336 2a337 2 if (set3_d.size() > set4_d.size()) max2 = set3_d.size();d339 1a339 1 max2 = set4_d.size();d349 2a350 2 if((checkdata_PF(set1_d) == true) && (checkdata_PF(set2_d) == true) && (checkdata_PF(set3_d) == true) && (checkdata_PF(set4_d) == true))d376 2a377 1 if ( pf.size() == 1) d379 7a385 3 else for(int i = 0; i <= pf.size() - 1 ; i++ ) for(int j = i + 1; j <= pf.size() - 1 ; j++ )d391 2a392 1 ((MyPoint)pf.elementAt(j)).x) )d394 3a396 1d398 1d426 23a448 2 } d479 1a479 1 if(set1_d.size() > 0 )d481 7a487 7 display_set1.removeAllElements(); iset1.removeAllElements(); mset1.removeAllElements(); interpol(set1_d, display_set1); average1 = mean(display_set1, mset1); interpol(mset1, iset1);d489 1a489 1 output_panel_d.addOutput(set1_d, (Classify.PTYPE_INPUT * 4), d493 1a493 1 if(set2_d.size() > 0 )d495 7a501 7 display_set2.removeAllElements(); iset2.removeAllElements(); mset2.removeAllElements(); interpol(set2_d, display_set2); average2 = mean(display_set2, mset2); interpol(mset2, iset2); d503 1a503 1 output_panel_d.addOutput(set2_d, (Classify.PTYPE_INPUT * 4), d507 1a507 1 if(set3_d.size() > 0 )d509 7a515 7 display_set3.removeAllElements(); iset3.removeAllElements(); mset3.removeAllElements(); interpol(set3_d, display_set3); average3 = mean(display_set3, mset3); interpol(mset3, iset3);d517 1a517 1 output_panel_d.addOutput(set3_d, (Classify.PTYPE_INPUT * 4), d521 1a521 1 if(set4_d.size() > 0 )d523 7a529 7 display_set4.removeAllElements(); iset4.removeAllElements(); mset4.removeAllElements(); interpol(set4_d, display_set4); average4 = mean(display_set4, mset4); interpol(mset4, iset4);d531 1a531 1 output_panel_d.addOutput(set4_d, (Classify.PTYPE_INPUT * 4), d539 330d880 2d883 3d907 1d1043 1a1043 56 /** * Computes the Auto Correlation and Linear Coefficients and * displays the coefficients. * * @@return true * */ boolean step2() { autoCorrelation(); pfcCoefficient(); final_estimate(); // need a subroutine here to calculate the actual error: // 1. Get first two points on the estimated waveform. // 2. Check the point given by the user for the x-coordinate, // if the x-coordinate of the user point is same as // either of two points [from one], // 2a. YES. note the difference in the y-coordinate, this is the error. // 2b. Error Energy is square of this term. // 2c. NO. check if the user point is outside these two points // 2c.1. Yes, pick the next point in the estimated waveform, // and repeat from step 2 again. // 2c.2. No, note the x-coordinate of the user point, // find the y-coordinate lying on the straight line between // the two estimated points. // 2c.2a. Find the difference in the y-coordinates calculate in // step 2c.2 and the user given. This is the error. // 2c.3 The Error Energy is the square of this term. // 3. Continue with step 2 // till you have covered all the user defined points. if(set1_d.size() > 0) { actual_err_1 = (double) 0; actual_err_1 = actual_error(y_estimate1, set1_d); } if(set2_d.size() > 0) { actual_err_2 = (double) 0; actual_err_2 = actual_error(y_estimate2, set2_d); } if(set3_d.size() > 0) { actual_err_3 = (double) 0; actual_err_3 = actual_error(y_estimate3, set3_d); } if(set4_d.size() > 0) { actual_err_4 = (double) 0; actual_err_4 = actual_error(y_estimate4, set4_d); } step2_display(); return true; } d1052 1a1052 1 if (iset1.size() > 0 )d1055 1a1055 1 autocorrelate(iset1, auto_co_1);d1058 1a1058 1 if (iset2.size() > 0)d1061 1a1061 1 autocorrelate(iset2, auto_co_2);d1064 1a1064 1 if (iset3.size() > 0)d1067 1a1067 1 autocorrelate(iset3, auto_co_3);d1070 1a1070 1 if (iset4.size() > 0)d1073 1a1073 1 autocorrelate(iset4, auto_co_4);d1240 2a1241 2 if(iset1.size() > 0) estimate (iset1, y_estimate1, average1, final_pfc_1);d1243 2a1244 2 if(iset2.size() > 0) estimate (iset2, y_estimate2, average2, final_pfc_2);d1246 2a1247 2 if(iset3.size() > 0) estimate (iset3, y_estimate3, average3, final_pfc_3);d1249 2a1250 2 if(iset4.size() > 0) estimate (iset4, y_estimate4, average4, final_pfc_4);d1371 1a1371 1 if (set1_d.size() > 0 )d1373 1a1373 1 int num_pts = iset1.size();d1379 1a1379 1 if (set2_d.size() > 0 )d1381 1a1381 1 int num_pts = iset2.size(); d1387 1a1387 1 if (set3_d.size() > 0 )d1389 1a1389 1 int num_pts = iset3.size();d1395 1a1395 1 if (set4_d.size() > 0 )d1397 1a1397 1 int num_pts = iset4.size();a1466 36 /** * * displays the predicted signal * * @@return true * */ boolean step3() { // 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(set1_d.size() > 0) output_panel_d.addOutput( y_estimate1, Classify.PTYPE_LINE, Color.black); if(set2_d.size() > 0) output_panel_d.addOutput( y_estimate2, Classify.PTYPE_LINE, Color.pink); if(set3_d.size() > 0) output_panel_d.addOutput( y_estimate3, Classify.PTYPE_LINE, Color.cyan); if(set4_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");a1467 2 return true; }@
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -