⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 algorithmlp.java

📁 完整的模式识别库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	   p.x =  ((MyPoint)v.elementAt(i)).x; 	   mv.addElement(new MyPoint(p));       }               return average ;    }         /**    * Computes the Auto Correlation and  Linear Coefficients and     * displays the coefficients.    *    * @return    True    *    */    boolean step2()    {	autoCorrelation();	lpcCoefficient();	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;    }        /**    *    * Computes the autocorrelation coeffient from the data sets       *    */    public void autoCorrelation()    {	if (iset1.size() > 0 )	{	    auto_co_1 = new double[lporder + 1];	    autocorrelate(iset1, auto_co_1);	}		if (iset2.size() > 0)        {	    auto_co_2 = new double[lporder + 1];	    autocorrelate(iset2, auto_co_2);	}		if (iset3.size() > 0)        {	    auto_co_3 = new double[lporder + 1];	    autocorrelate(iset3, auto_co_3);	}		if (iset4.size() > 0)        {	    auto_co_4 = new double[lporder + 1];	    autocorrelate(iset4, auto_co_4);	}	    }    /**     *     * Actaully computes the autocorrelation coefficients     *     * @param  v Vector of datapoints     * @param  autoCoeff_co array of autocorrelation coefficients     *     */    public void autocorrelate ( Vector v, double[] autoCoeff_co)     {	int size;	size                 = v.size();      	double[] amplitude_y = new double[size];      	// to store the amplitude for calculation autocoefficient      	//    	for (int i = 0; i < size; i++)	    amplitude_y[i] = ((MyPoint)v.elementAt(i)).y;      	for (int j = 0; j <= lporder; j++)      	{      	    autoCoeff_co[j] = 0;      	    if ( j < size)      		for (int k = 0; k < size - j; k++)		    autoCoeff_co[j] = autoCoeff_co[j] 			            + amplitude_y[k] * amplitude_y[k + j];	}   	// normalize the autocorrelation cofficient      	//     	for (int i = lporder; i >= 0; i--)       	    autoCoeff_co[i] = autoCoeff_co[i] / autoCoeff_co[0];	      }    /**     *     * Computes the Linear Prediction coefficient from the data sets        *     */    public void lpcCoefficient()    {		// Rabiner, Schafer, "Digital Processing of Speech signals", 	// Bell Laboratories, Inc. 1978, 	// section 8.3.2 Durbin's recursive solution for 	// the autocorrelation equations pp 411-412		if (set1_d.size() > 0)	{	    final_lpc_1 = new double [ lporder + 1];	    ref_coeff_1 = new double [ lporder + 1];	    estimate_err_1 = calculate_lpc( auto_co_1, 					    final_lpc_1, ref_coeff_1);	}	if (set2_d.size() > 0)	{	    final_lpc_2 = new double [ lporder + 1];	    ref_coeff_2 = new double [ lporder + 1];	    estimate_err_2 = calculate_lpc( auto_co_2, 					    final_lpc_2, ref_coeff_2);	}	if (set3_d.size() > 0)	{	    final_lpc_3 = new double [ lporder + 1];	    ref_coeff_3 = new double [ lporder + 1];	    estimate_err_3 = calculate_lpc( auto_co_3, 					    final_lpc_3, ref_coeff_3);	}	if (set4_d.size() > 0)	{	    final_lpc_4 = new double [ lporder + 1];	    ref_coeff_4 = new double [ lporder + 1];	    estimate_err_4 = calculate_lpc( auto_co_4, 					    final_lpc_4, ref_coeff_4);	}    }    /**     *     * Actually calculate the LP coefficient and the Residual Error      * Energy, and Reflection Coefficients     *     * @param  auto_coeff array of auto correlation coefficients     * @param  lpc        array of linear prediction coefficients     * @param  rc_reg     array of reflection coefficients     *      * @return residual error energy in a double     *     */    public double calculate_lpc (double[] auto_coeff, 				 double[] lpc, double[] rc_reg)    {	int j_bckwd       = 0;	long middle_index = 0;	// Reference:	// J.D. Markel and A.H. Gray, "Linear Prediction of Speech,"	// Springer-Verlag Berlin Heidelberg, New York, USA, pp. 219,	// 1980. 	// initialization	//	lpc[0] = 1;	double err_energy = auto_coeff[0];	// if the energy of the signal is zero we set all the predictor	// coefficients to zero and exit	//	if (err_energy == (double)0)	{	    for (int i = 0; i < lporder + 1 ; i++)		lpc[i] = 0;	}	else        {	    // do the first step manually	    //	    double sum = 0;	    double tmp = 0;	    	    sum         = -auto_coeff[1] / err_energy;	    rc_reg[1]   = sum;	    lpc[1]      = rc_reg[1];	    tmp         = 1 - rc_reg[1] * rc_reg[1];	    err_energy *= tmp;	    // recursion	    //	    for (int i = 2; i <= lporder; i++)	    {		sum = 0;		for (int j = 1; j < i; j++)		    sum += lpc[j] * auto_coeff[i - j]; 		rc_reg[i]    = -(auto_coeff[i] + sum) / err_energy;	    		lpc[i]       = rc_reg[i];		j_bckwd      = i - 1;		middle_index = i / 2;		for (int j = 1; j <= middle_index; j++)		{		    sum          = lpc[j_bckwd] + rc_reg[i] * lpc[j];		    lpc[j]       = lpc[j] + rc_reg[i] * lpc[i - j];		    lpc[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(iset1.size() > 0)	    estimate (iset1, y_estimate1, average1, final_lpc_1);		if(iset2.size() > 0)	    	    estimate (iset2, y_estimate2, average2, final_lpc_2);		if(iset3.size() > 0)	    estimate (iset3, y_estimate3, average3, final_lpc_3);		if(iset4.size() > 0)	    estimate (iset4, y_estimate4, average4, final_lpc_4);    }         /**     *     * Estimates the amplitude based on the LP coeficients.     *     * @param    iset         interpolated data points     * @param    y_estimate   predicted final signal data points     * @param    avg          mean of the original datapoints given     * @param    final_lpc    array of final linear prediction coefficients     *      */    public void estimate( Vector<MyPoint> iset, Vector<MyPoint> y_estimate, 			  double avg, double[] final_lpc)    {	   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 <= lporder) && (z > 0); j++, z-- )		   sum = sum - amplitude[z - 1] * final_lpc[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 y_estimate, Vector 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 LP order, Error Energy and Reflection Coefficients      *     */    public void step2_display()    {	// display the LP order	//	pro_box_d.appendMessages("         LP order =  " + lporder + "\n");	if (set1_d.size() > 0 )        {	    int num_pts =  iset1.size();	    int n = 0;	    display_result(auto_co_1, ref_coeff_1, final_lpc_1, 			   actual_err_1, estimate_err_1, n, num_pts);       	}	if (set2_d.size() > 0 )        {	    int num_pts =  iset2.size();	        	    int n = 1;	    display_result(auto_co_2, ref_coeff_2, final_lpc_2, 			   actual_err_2, estimate_err_2, n, num_pts);	}	if (set3_d.size() > 0 )        {	    int num_pts =  iset3.size();	    int n = 2;	    display_result(auto_co_3, ref_coeff_3, final_lpc_3, 			   actual_err_3, estimate_err_3, n, num_pts);	}	if (set4_d.size() > 0 )        {	    int num_pts =  iset4.size();	    int n = 3;	    display_result(auto_co_4, ref_coeff_4, final_lpc_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_lpc Linear Prediction 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_lpc, 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 <= lporder; i++)	    if (i == lporder)		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 <= lporder; i++)	    if (i == lporder)		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" + "         Prediction Coefficients:" 				 + "\n");	pro_box_d.appendMessages("              [ ");	for (int i = 0 ; i <= lporder; i++)	    if (i == lporder )	    pro_box_d.appendMessages(MathUtil.SetDecimal(final_lpc[i], 3) 				     + " ");	    else		pro_box_d.appendMessages(MathUtil.SetDecimal(final_lpc[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");     }    /**     *     * 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");       	return true;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -