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

📄 algorithmpf.java

📁 包含了模式识别中常用的一些分类器设计算法
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		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 * pforder       //       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 ;    }         /**      * Draws Gaussian points around each data point     *     * @param  mean_x_a double x value of mean point     * @param  mean_y_a double y value of mean point     * @param  set_index_a which data set of points to draw gaussian     */    public void drawGaussian(double mean_x_a, double mean_y_a, int set_index_a) {	BiNormal bn = new BiNormal();		switch (set_index_a) {	case 1:	bn.gaussian(20, mean_x_a, mean_y_a, gset_1_x_d, 		                      gset_1_y_d,		                      0.05, 0.0, 0.0, 0.05);	break;	case 2:	bn.gaussian(20, mean_x_a, mean_y_a, gset_2_x_d, 		                      gset_2_y_d,		                      0.05, 0.0, 0.0, 0.05);	break;	case 3:	bn.gaussian(20, mean_x_a, mean_y_a, gset_3_x_d, 		                      gset_3_y_d,		                      0.05, 0.0, 0.0, 0.05);	break;	case 4:	bn.gaussian(20, mean_x_a, mean_y_a, gset_4_x_d, 		                      gset_4_y_d,		                      0.05, 0.0, 0.0, 0.05);	break;	}           }        /**     * Transforms a given set of points to a new space     * using the class independent principal component analysis algorithm     *     * @param point_index_a the index in the data vector to use     * @param set_index_a   the set to use     * @return a vector of points in a circle     */    public Vector<MyPoint> transformPCA(int point_index_a, int set_index_a) {	// declare local variables	//	int size = 0;	int xsize = 0;	int ysize = 0;	Vector<MyPoint> circle_vector = new Vector<MyPoint>();		// declare variables to compute the global mean	//	double xval = 0.0;	double yval = 0.0;	double xmean = 0.0;	double ymean = 0.0;			// declare the covariance object	//	Covariance cov = new Covariance();	// declare the covariance matrix	//	Matrix covariance = new Matrix();	covariance.row = covariance.col = 2;	covariance.Elem = new double[2][2];	// declare arrays for the eigenvalues	//	double eigVal[] = null;	// declare an array to store the eigen vectors	//	double eigVec[] = new double[2];	// declare matrix objects	//	Matrix T = new Matrix();	Matrix M = new Matrix();	Matrix W = new Matrix();	// allocate memory for the matrix elements	//	T.Elem = new double[2][2];	M.Elem = new double[2][2];	W.Elem = new double[2][2];	// declare arrays to store the samples	//	double x[] = null;	double y[] = null;	// declare the maximum size of all data sets together	//	int maxsize = 20;	// initialize arrays to store the samples	//	x = new double[maxsize];	y = new double[maxsize];	// get the samples from the first data set	//	size = 20;	// set up the initial random vectors i.e., the vectors of	// X and Y coordinate points form the display	//	switch (set_index_a) {	case 1:	    for (int i = 0; i < size; i++) {				MyPoint p = gset_1_d.elementAt(point_index_a).elementAt(i);		xval += p.x;		yval += p.y;		x[xsize++] = p.x;		y[ysize++] = p.y;	    }	    break;	case 2:	    for (int i = 0; i < size; i++) {				MyPoint p = gset_2_d.elementAt(point_index_a).elementAt(i);		xval += p.x;		yval += p.y;		x[xsize++] = p.x;		y[ysize++] = p.y;	    }	    break;	case 3:	    for (int i = 0; i < size; i++) {				MyPoint p = gset_3_d.elementAt(point_index_a).elementAt(i);		xval += p.x;		yval += p.y;		x[xsize++] = p.x;		y[ysize++] = p.y;	    }	    break;	case 4:	    for (int i = 0; i < size; i++) {				MyPoint p = gset_4_d.elementAt(point_index_a).elementAt(i);		xval += p.x;		yval += p.y;		x[xsize++] = p.x;		y[ysize++] = p.y;	    }	    break;		}	if (maxsize > 0)	{	    // initialize the transformation matrix dimensions	    //	    W.row = 2;	    W.col = 2;	    // reset the matrices	    //	    W.resetMatrix();	    // compute the covariance matrix of the first data set	    //	    covariance.Elem = cov.computeCovariance(x, y);	    cov_matrix_d = covariance;	    // initialize the matrix needed to compute the eigenvalues	    //	    T.initMatrix(covariance.Elem, 2, 2);	    // make a copy of the original matrix	    //	    M.copyMatrix(T);	    // compute the eigen values	    eigVal = Eigen.compEigenVal(T);	    // compute the eigen vectors	    //	    for (int i = 0; i < 2; i++) {		Eigen.calcEigVec(M, eigVal[i], eigVec);		for (int j = 0; j < 2; j++) {		    W.Elem[j][i] = eigVec[j] / Math.sqrt(eigVal[i]);		}	    }	    	    // save the transformation matrix 	    //	    trans_matrix_d = W;	}	// compute the global mean of the data sets	//	xmean = xval / xsize;	ymean = yval / ysize;	// determine points for the support regions	//	double val[][] = new double[2][1];	Matrix invT = new Matrix();	Matrix supp = new Matrix();	Matrix temp = new Matrix();	// set up the angle with which to rotate the axis	//	double theta = 0.0;	double alpha = cov_matrix_d.Elem[0][0] - cov_matrix_d.Elem[1][1];	double beta = -2 * cov_matrix_d.Elem[0][1];	if (eigVal[0] > eigVal[1]) {	    theta = Math.atan2((alpha - Math.sqrt((alpha * alpha) 						  + (beta * beta))), beta);	}	else {	    theta = Math.atan2((alpha + Math.sqrt((alpha * alpha) 						  + (beta * beta))), beta);	}	// compute the inverse of the transformation matrix	//	trans_matrix_d.invertMatrix(invT);	// loop through all points on the circumference of the 	// gaussian sphere in the transformed space	//	for (int i = 0; i < 360; i++) {	    // get the x and y co-ordinates	    //	    val[0][0] = 1.5 * Math.cos(i);	    val[1][0] = 1.5 * Math.sin(i);	    // set up the points as a matrix in order for multiplication	    //	    temp.initMatrix(val, 2, 1);	    // transform the points from the feature space back to the	    // original space to create the support region for the data set	    //	    invT.multMatrix(temp, supp);	    // rotate the points after transforming them to the new space	    //	    xval = (supp.Elem[0][0] * Math.cos(theta)) 		 - (supp.Elem[1][0] * Math.sin(theta));	    yval = (supp.Elem[0][0] * Math.sin(theta)) 		 + (supp.Elem[1][0] * Math.cos(theta));	    // time shift the co-ordinates to the global mean	    //	    xval = xval + xmean;	    yval = yval + ymean;	    // add the point to the support region vector	    //	    MyPoint pt = new MyPoint(xval, yval);	    circle_vector.addElement(pt);	}	return circle_vector;    }    }

⌨️ 快捷键说明

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