Commit b15f9feb authored by Andrey Filippov's avatar Andrey Filippov
Browse files

Merge remote-tracking branch 'origin/dct'

parents c7e1de46 7c36f258
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -12003,7 +12003,11 @@ if (MORE_BUTTONS) {
			int       loopDebugLevel){
		boolean noMove=false;
		if (newMotorPos==null) {
			try {
				newMotorPos=focusingMotors.readElphel10364Motors().clone();
			} catch (Exception e){
				System.out.println("moveAndMaybeProbe(): Failed to read motors");
			}
			justMove=true;
			noMove=true;
		}
@@ -12098,7 +12102,11 @@ if (MORE_BUTTONS) {
//		System.out.println(">"+focusingMotors.historySize()+": "+focusingMotors.curpos[0]+", "+focusingMotors.curpos[1]+", "+focusingMotors.curpos[2]);
		boolean noMove=false;
		if (newMotorPos==null) {
			try {
				newMotorPos=focusingMotors.readElphel10364Motors().clone();
			} catch (Exception e){
				System.out.println("moveMeasureAndSave(): motors unreachable.");
			}
			noMove=true;
		}
		if (!noMove)focusingMotors.moveElphel10364Motors(
+8 −3
Original line number Diff line number Diff line
@@ -2671,7 +2671,7 @@ public class CalibrationHardwareInterface {
    		}
    		
    		boolean result=commandToDevice(command);
    		updateCurrents();
    		if (result)	updateCurrents();
    		return result;

    	}
@@ -2718,7 +2718,8 @@ public class CalibrationHardwareInterface {
    				String msg = e1.getMessage();
    				if (msg==null || msg.equals(""))  msg = ""+e1;
					IJ.showMessage("Error",msg); 
					throw new IllegalArgumentException (msg);
					return false;
//					throw new IllegalArgumentException (msg);
    			}catch(ParserConfigurationException pce) {
    				pce.printStackTrace();
    				return false;
@@ -3471,7 +3472,9 @@ public class CalibrationHardwareInterface {
    		IJ.showStatus("");
    		String error = e1.getMessage();
    		if (error==null || error.equals(""))  error = ""+e1;
    		IJ.showMessage("commandElphel10364Motors ERRROR", ""+error);
    		System.out.println("commandElphel10364Motors ERRROR: "+error);
    		// *********** Temporary removed message box (usually "HOST UNREACHABLE") *************
//    		IJ.showMessage("commandElphel10364Motors ERRROR", ""+error);
    		return null;
    	}catch(ParserConfigurationException pce) {
    		pce.printStackTrace();
@@ -6529,7 +6532,9 @@ if (debugLevel>=debugThreshold) System.out.println(i+" "+diff[0]+" "+diff[1]+" "
    	}
    	public double distFromProbed(int [] position){
    		double minDist=Double.NaN;
    		if (this.history.size() <1) return Double.NaN;
    		if (position==null) position= this.history.get(this.history.size()-1).motorsPos;
    		
    		if (this.history.size()>0){
    			for (int i=0;i<this.history.size();i++) if (this.history.get(i).isProbed) {
    				int [] probedPosition=this.history.get(i).motorsPos;
+18 −8
Original line number Diff line number Diff line
@@ -1691,15 +1691,25 @@ import org.apache.commons.configuration.XMLConfiguration;
			if (gridImages!=null) {
//				this.pathName="";  // modified, keep the path anyway
// overwrite saved paths with the provided images, number of images{ should match
				if (this.gIP.length!=gridImages.length){
				if (this.gIP.length == gridImages.length){
					for (int i=0;i<this.gIP.length;i++){
						this.gIP[i].gridImage=gridImages[i];
						this.gIP[i].path=null; // not needed, just in case
						this.gIP[i].enabled=true;// enable all (actually just one) acquired images
					}
				} else {
					String msg="Number of provided images ("+gridImages.length+") does not match parameters restored from the "+pathname+" ("+this.gIP.length+")";
		    		IJ.showMessage("Error",msg);
		    		throw new IllegalArgumentException (msg);
				}
//		    		throw new IllegalArgumentException (msg);
					for (int i=0; i<this.gIP.length ; i++){
					this.gIP[i].gridImage=gridImages[i];
						this.gIP[i].path=null; // not needed, just in case
						this.gIP[i].enabled=true;// enable all (actually just one) acquired images
						if (i < gridImages.length) {
							this.gIP[i].gridImage=gridImages[i];
						} else {
							this.gIP[i].gridImage=null;
						}
					}
				}
//				setGridImages(gridImages);
			}
@@ -2080,7 +2090,7 @@ import org.apache.commons.configuration.XMLConfiguration;
        			imp_grid=this.gIP[numGridImg].gridImage;
        		} else {
        			if (this.updateStatus) IJ.showStatus("Reading grid file "+(fileNumber+1)+" (of "+(numImages)+"): "+this.gIP[fileNumber].path);
        			if (this.debugLevel>1) System.out.print(fileNumber+" ("+this.gIP[fileNumber].getStationNumber()+"): "+this.gIP[fileNumber].path);
        			if (this.debugLevel>-1) System.out.print(fileNumber+" ("+this.gIP[fileNumber].getStationNumber()+"): "+this.gIP[fileNumber].path);
        			imp_grid=opener.openImage("", this.gIP[fileNumber].path);  // or (path+filenames[nFile])
        			if (imp_grid==null) {
        				String msg="Failed to read grid file "+this.gIP[fileNumber].path;
+182 −30
Original line number Diff line number Diff line
@@ -44,10 +44,10 @@ public class DttRad2 {
	double [] hwindow = null; // half window
	int    [][] fold_index = null; // index of the source item in 2nx2n array input to mdct_2d.
	                               // First index (0..n^2-1) index in the folded array (dct-iV input) 
	                               // Second index(0..3) - item to add (2 vertiacl, 2 - horizontal)  
	double [][] fold_k = null; // Matching fold_index items. Each is a product of 2 window coefficients and sign  
	                               // Second index(0..3) - item to add (2 vertical, 2 - horizontal)  
	double [][][] fold_k = null; // First index - mode: 0 - CC 1: SC, 2: CS, 3: SS. Other indices matching fold_index items. Each is a product of 2 window coefficients and sign  
	int    [] unfold_index = null;  // index  for each element of idct(2nx2n)
	double [] unfold_k = null; // Matching unfold_index items. Each is a product of 2 window coefficients and sign  
	double [][] unfold_k = null; // First index - mode: 0 - CC 1: SC, 2: CS, 3: SS. Other indices matching unfold_index items. Each is a product of 2 window coefficients and sign  
	
	public DttRad2 (int maxN){ // n - maximal
		setup_arrays(maxN); // always setup arrays for fast calculations
@@ -87,25 +87,31 @@ public class DttRad2 {
	// For index in dct-iv input (0..n-1) get 2 variants of index in mdct input array (0..2*n-1)
	// second index : 0 - index in X array 2*n long
	//                1 - window index (0..n-1), [0] - minimal, [n-1] - max
	//                2 - sign of the term
	//                2 - sign of the C term  (-~c - d, +a -~b)
	//                3 - sign of the S term  (+~c - d, +a +~b)
	private int [][] get_fold_indices(int x, int n){
		int n1 = n>>1;
		int [][] ind = new int[2][3];
		int [][] ind = new int[2][4];
		if (x <n1) {
			ind[0][0] = n + n1 - x - 1; // -cR
			ind[0][0] = n + n1 - x - 1; // C: -cR, S: +cR
			ind[0][1] = n1     + x;
			ind[0][2] = -1;
			ind[1][0] = n + n1 + x;     // -d
			ind[0][3] =  1;
			ind[1][0] = n + n1 + x;     // C: -d,  S: -d
			ind[1][1] = n1     - x - 1;
			ind[1][2] = -1;
			ind[1][3] = -1;
		} else {
			x-=n1;
			ind[0][0] =          x;     // +a
			ind[0][0] =          x;     // C: +a, S: +a
			ind[0][1] =          x;
			ind[0][2] = 1;
			ind[1][0] = n      - x - 1; // -bR
			ind[0][3] = 1;
			
			ind[1][0] = n      - x - 1; // C: -bR, S: +bR
			ind[1][1] = n      - x - 1;
			ind[1][2] = -1;
			ind[1][3] =  1;
		}
		
		return ind;
@@ -114,45 +120,120 @@ public class DttRad2 {
	private void set_fold_2d(int n){ // n - DCT and window size
		if ((fold_index != null) && (fold_index.length == n*n)) return;
		fold_index = new int[n*n][4];
		fold_k =     new double[n*n][4];
		fold_k =     new double[4][n*n][4];
		int []    vert_ind = new int[2];
		double [] vert_k = new double[2];
		double [][] vert_k = new double[2][2];
		int    [] hor_ind = new int[2];
		double [] hor_k = new double[2];
		double [][] hor_k = new double[2][2];
		int [][] fi;
		int n2 = 2*n;
		for (int i = 0; i < n; i++ ){
			fi = get_fold_indices(i,n);
			vert_ind[0] = fi[0][0];
			vert_ind[1] = fi[1][0];
			vert_k[0] =   fi[0][2] * hwindow[fi[0][1]];
			vert_k[1] =   fi[1][2] * hwindow[fi[1][1]];
			vert_k[0][0] =   fi[0][2] * hwindow[fi[0][1]]; // use cosine sign
			vert_k[0][1] =   fi[1][2] * hwindow[fi[1][1]]; // use cosine sign
			vert_k[1][0] =   fi[0][3] * hwindow[fi[0][1]]; // use sine sign
			vert_k[1][1] =   fi[1][3] * hwindow[fi[1][1]]; // use sine sign
			for (int j = 0; j < n; j++ ){
				fi = get_fold_indices(j,n);
				hor_ind[0] = fi[0][0];
				hor_ind[1] = fi[1][0];
				hor_k[0] =   fi[0][2] * hwindow[fi[0][1]];
				hor_k[1] =   fi[1][2] * hwindow[fi[1][1]];
				hor_k[0][0] =   fi[0][2] * hwindow[fi[0][1]]; // use cosine sign
				hor_k[0][1] =   fi[1][2] * hwindow[fi[1][1]]; // use cosine sign
				hor_k[1][0] =   fi[0][3] * hwindow[fi[0][1]]; // use sine sign
				hor_k[1][1] =   fi[1][3] * hwindow[fi[1][1]]; // use sine sign
				int indx = n*i + j;
				for (int k = 0; k<4;k++) {
					fold_index[indx][k] = n2 * vert_ind[(k>>1) & 1] + hor_ind[k & 1];
					fold_k[indx][k] =     vert_k[(k>>1) & 1] * hor_k[k & 1]; 
				}
				for (int mode = 0; mode<4; mode++){
					for (int k = 0; k<4;k++) {
						fold_k[mode][indx][k] =     vert_k[(mode>>1) &1][(k>>1) & 1] * hor_k[mode &1][k & 1]; 
					}
				}
			}
		}
		if (n < 8) {
			for (int i = 0; i < n; i++ ){
				fi = get_fold_indices(i,n);
				System.out.println(i+"->"+String.format("[%2d % 2d % 2d] [%2d %2d %2d] %f %f",
						fi[0][0],fi[0][1],fi[0][2],
						fi[1][0],fi[1][1],fi[1][2], hwindow[fi[0][1]], hwindow[fi[1][1]]));
				System.out.println(i+"->"+String.format("?[%2d %2d %2d %2d] [%2d %2d %2d %2d] %f %f",
						fi[0][0],fi[0][1],fi[0][2],fi[0][3],
						fi[1][0],fi[1][1],fi[1][2],fi[1][3], hwindow[fi[0][1]], hwindow[fi[1][1]]));
			}
		}
	}
	
	public double [][][] get_fold_2d(
			double scale_hor,
			double scale_vert)
	{
		return get_fold_2d( this.N, scale_hor, scale_vert);
	}
	public double [][][] get_fold_2d(
			int n,
			double scale_hor,
			double scale_vert
			){ // n - DCT and window size
//		if ((fold_index != null) && (fold_index.length == n*n)) return;
//		fold_index = new int[n*n][4];
		
			double [] hwindow_h = new double[n];
			double [] hwindow_v = new double[n];
			double f = Math.PI/(2.0*n);
			for (int i = 0; i < n; i++ ) {
				double ah = f*scale_hor * (n-i-0.5);
				double av = f*scale_vert * (n-i-0.5);
				hwindow_h[i] = (ah > (Math.PI/2))? 0.0: Math.cos(ah);
				hwindow_v[i] = (av > (Math.PI/2))? 0.0: Math.cos(av);
			}
		double [][][] fold_sk = new double[2][n*n][4];
		int []    vert_ind =    new int[2];
		double [][] vert_k =    new double[2][2];
		int    [] hor_ind =     new int[2];
		double [][] hor_k =     new double[2][2];
		int [][] fi;
		for (int i = 0; i < n; i++ ){
			fi = get_fold_indices(i,n);
			vert_ind[0] = fi[0][0];
			vert_ind[1] = fi[1][0];
//			vert_k[0] =   fi[0][2] * hwindow_v[fi[0][1]];
//			vert_k[1] =   fi[1][2] * hwindow_v[fi[1][1]];
			vert_k[0][0] =   fi[0][2] * hwindow_v[fi[0][1]]; // use cosine sign
			vert_k[0][1] =   fi[1][2] * hwindow_v[fi[1][1]]; // use cosine sign
			vert_k[1][0] =   fi[0][3] * hwindow_v[fi[0][1]]; // use sine sign
			vert_k[1][1] =   fi[1][3] * hwindow_v[fi[1][1]]; // use sine sign
			
			for (int j = 0; j < n; j++ ){
				fi = get_fold_indices(j,n);
				hor_ind[0] = fi[0][0];
				hor_ind[1] = fi[1][0];
//				hor_k[0] =   fi[0][2] * hwindow_h[fi[0][1]];
//				hor_k[1] =   fi[1][2] * hwindow_h[fi[1][1]];
				hor_k[0][0] =   fi[0][2] * hwindow_h[fi[0][1]]; // use cosine sign
				hor_k[0][1] =   fi[1][2] * hwindow_h[fi[1][1]]; // use cosine sign
				hor_k[1][0] =   fi[0][3] * hwindow_h[fi[0][1]]; // use sine sign
				hor_k[1][1] =   fi[1][3] * hwindow_h[fi[1][1]]; // use sine sign
				
				int indx = n*i + j;
//				for (int k = 0; k<4;k++) {
//					fold_sk[indx][k] =     vert_k[(k>>1) & 1] * hor_k[k & 1]; 
//				}
				
				for (int mode = 0; mode<4; mode++){
					for (int k = 0; k<4;k++) {
						fold_sk[mode][indx][k] =     vert_k[(mode>>1) &1][(k>>1) & 1] * hor_k[mode &1][k & 1]; 
					}
				}
			}
		}
		return fold_sk;
	}
	
/*	
	// return index+1 and sign for 1-d imdct. x is index (0..2*n-1) of the imdct array, value is sign * (idct_index+1),
	// where idct_index (0..n-1) is index in the dct-iv array
	private int get_unfold_index(int x, int n){
	private int get_unfold_index_signes(int x, int n){
		int n1 = n>>1;
		int segm = x / n1;
		x = x % n1;
@@ -167,7 +248,7 @@ public class DttRad2 {
	private void set_unfold_2d(int n){ // n - DCT size
		if ((unfold_index != null) && (unfold_index.length == 4*n*n)) return;
		unfold_index = new int[4*n*n];
		unfold_k = new double[4*n*n];
		unfold_k = new double[4][4*n*n];
		int n2 = 2*n;
		for (int i = 0; i < 2*n; i++ ){
			int index_vert = get_unfold_index(i,n);
@@ -202,6 +283,52 @@ public class DttRad2 {
			}
		}
	}	
*/
	// return index and two signs (c,s) for 1-d imdct. x is index (0..2*n-1) of the imdct array, value is sign * (idct_index+1),
	// where idct_index (0..n-1) is index in the dct-iv array
	private int [] get_unfold_index_signs(int x, int n){
		int n1 = n>>1;
		int segm = x / n1;
		x = x % n1;
		int [] is2  = new int[3]; 
		switch (segm){
		case 0: is2[0] =  x + n1;     is2[1]=  1; is2[2] =  1; return is2; // return 1+ (x + n1);
		case 1: is2[0] =  n -  x - 1; is2[1]= -1; is2[2] =  1; return is2; // return -(n - x);
		case 2: is2[0] =  n1 - x - 1; is2[1]= -1; is2[2] =  1; return is2; // return -(n1 - x);
		case 3: is2[0] =  x;          is2[1]= -1; is2[2] = -1; return is2; // return -(1 + x);
		}
		return null; //should never happen
	}
	private void set_unfold_2d(int n){ // n - DCT size
		if ((unfold_index != null) && (unfold_index.length == 4*n*n)) return;
		unfold_index = new int[4*n*n];
		unfold_k = new double[4][4*n*n];
		int n2 = 2*n;
		for (int i = 0; i < 2*n; i++ ){
			int [] is2_vert = get_unfold_index_signs(i,n);
			double [] k_vert = {is2_vert[1]*hwindow[(i < n)?i:n2 -i -1], is2_vert[2]*hwindow[(i < n)?i:n2 -i -1]};
			int index_vert = is2_vert[0] * n;
			
			for (int j = 0; j < 2*n; j++ ){
				int [] is2_hor = get_unfold_index_signs(j,n);
				int index_hor = is2_hor[0];
				double [] k_hor = {is2_hor[1] * hwindow[(j < n)?j:n2 -j -1], is2_hor[2] * hwindow[(j < n)?j:n2 -j -1]};
				unfold_index[n2*i+j]=(index_vert+index_hor);
				for (int mode = 0; mode < 4; mode++){
					unfold_k[mode][n2*i+j] = k_vert[(mode>>1) &1]*k_hor[mode &1]; 
				}
				
				if (n < 8) System.out.print(String.format("%4d", unfold_index[n2*i+j]));
				
			}
			if (n < 8) System.out.println();
		}
		if (n < 8) {
			for (int i = 0; i < 2*n; i++ ){
				System.out.println(i+"=>"+get_unfold_index_signs(i,n)[0]+", "+get_unfold_index_signs(i,n)[1]+", "+get_unfold_index_signs(i,n)[2]);
			}
		}
	}	
	
	public double [] dttt_iv(double [] x){
		return dttt_iv(x, 0, 1 << (ilog2(x.length)/2)); 
@@ -347,28 +474,53 @@ public class DttRad2 {
	}
	
	// Convert 2nx2n overlapping tile to n*n for dct-iv
	public double [] fold_tile(double [] x) { // x should be 2n*2n
	public double [] fold_tile(double [] x, int mode) { // x should be 2n*2n
		return fold_tile(x, 1 << (ilog2(x.length/4)/2));
	}
	public double [] fold_tile(double [] x, int n) { // x should be 2n*2n
	public double [] fold_tile(double [] x, int n, int mode) { // x should be 2n*2n
		return fold_tile(x,n, mode,this.fold_k);
//		double [] y = new double [n*n];
//		for (int i = 0; i<y.length;i++) {
//			y[i] = 0;
//			for (int k = 0; k < 4; k++){
//				y[i] += x[fold_index[i][k]] * fold_k[i][k];
//			}
//		}
//		return y;
	}

	public double [] fold_tile(
			double [] x,
			int n,
			int mode, //////
			double [][][] fold_k
			) { // x should be 2n*2n
		double [] y = new double [n*n];
		for (int i = 0; i<y.length;i++) {
			y[i] = 0;
			for (int k = 0; k < 4; k++){
				y[i] += x[fold_index[i][k]] * fold_k[i][k];
				y[i] += x[fold_index[i][k]] * fold_k[mode][i][k];
			}
		}
		return y;
	}
	
	public double [] unfold_tile(double [] x) { // x should be n*n
		return unfold_tile(x, 1 << (ilog2(x.length)/2));
	
	public double [] unfold_tile(
			double [] x,  // x should be n*n
			int mode)
	{
		return unfold_tile(x, 1 << (ilog2(x.length)/2), mode);
		
	}
	public double [] unfold_tile(double [] x, int n) { // x should be 2n*2n
	public double [] unfold_tile(
			double [] x,  // x should be 2n*2n
			int n,
			int mode) 
	{
		double [] y = new double [4*n*n];
		for (int i = 0; i<y.length;i++) {
			y[i] = unfold_k[i]* x[unfold_index[i]];
			y[i] = unfold_k[mode][i]* x[unfold_index[i]];
		}
		return y;
	}
+139 −29

File changed.

Preview size limit exceeded, changes collapsed.

Loading