diff FXAnalyse.c @ 17:9cadd12e7722

Implement N3 measurement. Large code cleanup.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Wed, 18 Jul 2012 18:48:36 +0200
parents 783407954cdf
children 3a7f088d4960
line wrap: on
line diff
--- a/FXAnalyse.c	Wed Jul 18 18:47:44 2012 +0200
+++ b/FXAnalyse.c	Wed Jul 18 18:48:36 2012 +0200
@@ -4,6 +4,7 @@
 #include <userint.h>
 #include <formatio.h>
 #include <string.h>
+#include <future.h>
 
 #include "YLCStuff.h"
 #include "FXAnalyse.h" 
@@ -41,6 +42,7 @@
 static PanelHandle CalcN1Panel ;
 static PanelHandle CalcN2Panel ;
 static PanelHandle CalcN3Panel ;
+static PanelHandle EstimateN3Panel;
 
 //==============================================================================
 // Static functions
@@ -58,7 +60,7 @@
 
 double Ch1, Ch2, Ch3, Ch4 ; // le type long double est identique au double (8 octets) sous CVI !!! 
 double Math1, Math2, Math3, Math4, Math5; // N est en fait un entier en pratique...
-double N1, N2, N3,N4 ;
+double N1, N2, N3, N4 ;
 
 Plot_Data PlotCh1, PlotCh2, PlotCh3, PlotCh4, PlotMath1, PlotMath2, PlotMath3, PlotMath4, PlotMath5 ;  
 Allan_Data AllanCh1, AllanCh2, AllanCh3, AllanCh4, AllanMath1, AllanMath2, AllanMath3, AllanMath4, AllanMath5 ; 
@@ -78,18 +80,28 @@
 ///////////////////
 //Pour les calculs des N
 
+enum {
+	N_MEASUREMENT_STEP_0,
+	N_MEASUREMENT_STEP_1,
+	N_MEASUREMENT_STEP_2,
+	N_MEASUREMENT_STEP_3,
+	N_MEASUREMENT_STEP_4,
+};
+
+int settling = 0;
+
 volatile bool Measuring_1 = FALSE,Step1_1 = FALSE,Step2_1 = FALSE,Step3_1 = FALSE, Measuring_2 = FALSE,Step1_2 = FALSE,Step2_2 = FALSE,Step3_2 = FALSE;
 volatile bool Measuring_3 = FALSE,Step1_3 = FALSE,Step2_3 = FALSE,Step3_3 = FALSE;
 volatile bool TimetoBegin=FALSE;
 
-double FrequDDS1=110000000.0,FrequDDS2=15300000.0,FrequDDS3=150400000.000000;
+double FrequDDS1=110000000.0, FrequDDS2=15300000.0, FrequDDS3=150400000.0, FrequDDS4=110000000.0;
 double Slope_1=0.0,Slope_2=0.0,Slope_3=0.0,Beatslope_2=0.0; 
-double SlopeTime1=60.0,SlopeTime2=60.0;
+double SlopeTime1=40.0, SlopeTime2=40.0; SlopeTime3=40.0;
 
-double N_1=0.0,N_2=0.0,N_3=0.0;
-double DeltaT_1=8.0,DeltakHz_1=1800.0,t1_1=0.0,t2_1=0.0,t3_1=0.0, Frepplus_1=0.0, Frepminus_1=0.0;
-double DeltaT_2=8.0,DeltakHz_2=1500.0,t1_2=0.0,t2_2=0.0,t3_2=0.0, Frepplus_2=0.0, Frepminus_2=0.0;
-double DeltaT_3=20.0,DeltakHz_3=100.0,t1_3=0.0,t2_3=0.0,t3_3=0.0, Frepplus_3=0.0, Frepminus_3=0.0;
+double N_1=0.0, N_2=0.0, N_3=0.0;
+double DeltaT_1=20.0, DeltakHz_1=500.0, t1_1=0.0, t2_1=0.0, t3_1=0.0, Frepplus_1=0.0, Frepminus_1=0.0;
+double DeltaT_2=20.0, DeltakHz_2=500.0, t1_2=0.0, t2_2=0.0, t3_2=0.0, Frepplus_2=0.0, Frepminus_2=0.0;
+double DeltaT_3=20.0, DeltakHz_3=500.0, t1_3=0.0, t2_3=0.0, t3_3=0.0, Frepplus_3=0.0, Frepminus_3=0.0;
 
 
 int DeltaTMoy_1=3,n_1=0,DeltaTMoy_2=3,n_2=0,DeltaTMoy_3=3,n_3=0;
@@ -111,12 +123,15 @@
 
 double Ch4Plus=0.0,Ch4Minus=0.0;
 
-int NDEJAPASSE=0, nDDSChange=0, nstabilization=0  ;
+int NDEJAPASSE=0, nDDSChange=0, nstabilization=0;
 
 //////
 //Pour les signes de fb
 
-double Frequencystep1=10000.0,tbegin1=0.0,Frepbefore1=0.0,Frequency1=0.0,Frequencystep2=10.0,tbegin2=0.0,Frepbefore2=0.0,Ch2before=0.0,Frequency2=0.0,Frequencystep3=100000.0,tbegin3=0.0,Frepbefore3=0.0,Frequency3=0.0;
+double Frequencystep1=10000.0, tbegin1=0.0, Frepbefore1=0.0, Frequency1=0.0;
+double Frequencystep2=10.0, tbegin2=0.0, Frepbefore2=0.0, Ch2before=0.0, Frequency2=0.0;
+double Frequencystep3=100000.0, tbegin3=0.0, Frepbefore3=0.0, Frequency3=0.0;
+
 volatile bool Getsign1=FALSE,Getsign2=FALSE,Getsign3=FALSE;
 double Signe1=1.0,Signe2=1.0,Signe3=0.0;
 
@@ -248,6 +263,8 @@
 		return -1;
 	if ((CalcN3Panel = LoadPanel (0, "FXAnalyse.uir", CALCN3)) < 0)
 		return -1;
+	if ((EstimateN3Panel = LoadPanel (MainPanel, "FXAnalyse.uir", ESTIMATEN3)) < 0)
+		return -1;
 	
 	DisplayPanel (MainPanel);
 	
@@ -1179,116 +1196,174 @@
 									}
 							}
 						
+						switch (Measuring_3) {
 
+							case N_MEASUREMENT_STEP_0:
+								// not measuring N3
+								break;
 							
-						/*	if (Measuring_3==TRUE)
-							{
-								if (Step1_3==FALSE) {
-									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequDDS3);
-									t1_3=utc;
-									Frequ_slope_3=Math1;
-									Moy_slope_3= Frequ_slope_3;
-									N_slope_3=1;
-									Step1_3=TRUE ;
-									}
-								else {
-									if(Step2_3==FALSE){
-										N_slope_3=N_slope_3+1; 
-										Frequ_slope_3=Math1;
-										Moy_slope_3=((N_slope_3-1)*Moy_slope_3 + Frequ_slope_3)/N_slope_3;
-										Slope_slope_3 = (Slope_slope_3*(N_slope_3-2) + 6*(Frequ_slope_3-Moy_slope_3)/N_slope_3)/(N_slope_3+1) ;
-										if (utc-t1_3>DeltaT_3) {
-											Slope_3 = Slope_slope_3;
-											GetCtrlVal(MainPanel, PANEL_DDSFREPOUT, &FrequencyDDSBes) ; 
-											DDSBes_SetFrequency(&DDSBes, FrequencyDDSBes+(FACTORDDSBES*DeltakHz_3*1000));
-											DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequDDS3+DeltakHz_3*1000);
-											t2_3=utc;
-											Step2_3=TRUE ;
-											N_slope_3=0;
-											Frequ_slope_3=0.0;
-											Moy_slope_3=0.0;
-											Slope_slope_3 =0.0;
-											}
-										}
-									else {
-										if(Step2_3==FALSE){
-											if (utc-t2_3<DeltaT_3) {
-													Frepplus_3=Frepplus_3 +Math1+250000000-Slope_3*(utc-t2_3);
-													n_3=n_3+1;
-													}
-											else{
-												Frepplus_3=Frepplus_3/n_3;
-												DDSBes_SetFrequency(&DDSBes, FrequencyDDSBes-(FACTORDDSBES*DeltakHz_3*1000)); 
-												DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequDDS3-DeltakHz_3*1000);
-												n_3=0;
-												t3_3=utc;
-												Step3_3=TRUE ;
-												}
-											}
-										else {
-											if (utc-t3_3<DeltaT_3) {
-													Frepminus_3=Frepminus_3 +Math1+250000000-Slope_3*(utc-t3_3);
-													n_3=n_3+1;
-													}
-											else{
-												Frepminus_3=Frepminus_3/(n_3);
-												N_3 = (16*DeltakHz_3*1000 -Slope_3*(t3_3-t2_3))/(Frepplus_3-Frepminus_3);
-												n_3=0;
-												Frepminus_3=0.0;
-												Frepplus_3=0.0;
-												DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequDDS3);  
-												Measuring_2=FALSE ;
-												Step1_3=FALSE ;
-												Step2_3=FALSE ;
-												Step3_3=FALSE ;
-												t1_3=0.0;
-												t2_3=0.0;
-												t3_3=0.0;
-												}
-											}
-										}
+							case N_MEASUREMENT_STEP_1: // init
+								
+								SetCtrlVal(MainPanel, PANEL_DDS4, FrequDDS4);
+								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, FrequDDS4);
+								settling = 3;
+								
+								t1_3 = utc;
+								N_slope_3 = 0;
+								// record current DDS3 frequency
+								GetCtrlVal(MainPanel, PANEL_DDS3, &FrequencyDDS3Init);
+
+								 // step 1 done
+								Measuring_3 = N_MEASUREMENT_STEP_2;
+								break;
+								
+							case N_MEASUREMENT_STEP_2: // slope measurement
+								
+								if (settling > 0) {
+									settling--;
+									break;
+								}
+								
+								N_slope_3++;
+								Frequ_slope_3 = Ch2;
+								Moy_slope_3 = ((N_slope_3-1)*Moy_slope_3 + Frequ_slope_3)/N_slope_3;
+								Slope_slope_3 = (Slope_slope_3*(N_slope_3-2) + 6*(Frequ_slope_3-Moy_slope_3)/N_slope_3)/(N_slope_3+1) ;
+								
+								if (utc - t1_3 > SlopeTime3) {
+									// slope measurement
+									Slope_3 = Slope_slope_3;
+									
+									t2_3 = utc;
+									N_slope_3 = 0;
+									Frequ_slope_3 = 0.0;
+									Moy_slope_3 = 0.0;
+									Slope_slope_3 = 0.0;
+									
+									// step 2 done
+									Measuring_3 = N_MEASUREMENT_STEP_3;
+									
+									// frep positive step
+									SetCtrlVal(MainPanel, PANEL_DDS4, FrequDDS4 + DeltakHz_3 * 1000);
+									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, FrequDDS4 + DeltakHz_3 * 1000);
+									// compensate with DDS3 to keep measured beatnote in counter box range
+									double fDDS3 = FrequencyDDS3Init + N3/N1 * DeltakHz_3;
+									SetCtrlVal(MainPanel, PANEL_DDS3, fDDS3);
+									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3);
+									// allow counter to settle
+									settling = 3;
+								}
+								break;
+								
+							case N_MEASUREMENT_STEP_3: // frep positive step
+								
+								if (settling > 0) {
+									settling--;
+									break;
 								}
-							}		*/
-						
-					
-						
-						
+								
+								n_3++;
+								Frepplus_3 += Ch2 - Slope_3 * (utc - t2_3);
+									
+								if (utc - t2_3 > DeltaT_3) {
+									// positive step measurement
+									Frepplus_3 = Frepplus_3 / n_3;
+									
+									n_3 = 0;
+									t3_3 = utc;
+									
+									// step 3 done
+									Measuring_3 = N_MEASUREMENT_STEP_4;
+									
+									// frep negative step
+									SetCtrlVal(MainPanel, PANEL_DDS4, FrequDDS4 - DeltakHz_3 * 1000);
+									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, FrequDDS4 - DeltakHz_3 * 1000);
+									// compensate with DDS3 to keep measured beatnote in counter box range
+									double fDDS3 = FrequencyDDS3Init - N3/N1 * DeltakHz_3;
+									SetCtrlVal(MainPanel, PANEL_DDS3, fDDS3);
+									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3);
+									// allow counter to settle
+									settling = 3;
+								}
+								break;
+								
+								
+							case N_MEASUREMENT_STEP_4: // frep negative step
+								
+								if (settling > 0) {
+									settling--;
+									break;
+								}
+								
+								n_3++;
+								Frepminus_3 += Ch2 - Slope_3 * (utc - t3_3);
+												
+								if (utc - t3_3 > DeltaT_3) {
+									// positive step measurement
+									Frepminus_3 = Frepminus_3 / n_3;
+									
+									// compute N3
+									N_3 = 1000.0 * DeltakHz_3 / (Frepplus_3 - Frepminus_3);
+									SetCtrlVal(CalcN3Panel, CALCN3_N_3, N_3);
+									
+									t1_3=0.0;
+									t2_3=0.0;
+									t3_3=0.0;
+									n_3 = 0;
+									Frepminus_3 = 0.0;
+									Frepplus_3 = 0.0;
+										
+									// step 4 done
+									Measuring_3 = N_MEASUREMENT_STEP_0;
+
+									// back to nominal frep
+									SetCtrlVal(MainPanel, PANEL_DDS4, FrequDDS4);
+									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, FrequDDS4);
+								}
+								break;
+						}
 						
 						// Calcul du signe de fb
 						
-						if (Getsign1 ==TRUE) {
-							if (utc> tbegin1+2){
-								if (Math1> Frepbefore1) { Signe1=-1.0;}
-								else {Signe1=1.0;}
+						if (Getsign1 == TRUE) {
+							if (utc > tbegin1+2) {
+								if (Math1 > Frepbefore1)
+									Signe1 = -1.0;
+								else
+									Signe1 = +1.0;
 								SetCtrlVal(MainPanel, PANEL_DDS1, Frequency1) ; 
 								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency1);
-								Getsign1=FALSE;
-								}
+								Getsign1 = FALSE;
 							}
-						if (Getsign2 ==TRUE) {
-							if (utc> tbegin2+2){
-								if (Math1> Frepbefore2) { 
-									if (Ch2>Ch2before) {Signe2=+1.0;}
-									else {Signe2=-1.0;} 
-								}
-								else { 
-									if (Ch2>Ch2before) {Signe2=-1.0;}
-									else {Signe2=+1.0;}
+						}
+						if (Getsign2 == TRUE) {
+							if (utc > tbegin2+2){
+								if (Math1 > Frepbefore2) { 
+									if (Ch2 > Ch2before)
+										Signe2 = +1.0;
+									else
+										Signe2 = -1.0;
+								} else {
+									if (Ch2 > Ch2before)
+										Signe2 = -1.0;
+									else 
+										Signe2 = +1.0;
 								}
 								SetCtrlVal(MainPanel, PANEL_DDS1, Frequency2) ; 
 								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency2);
-								Getsign2=FALSE;
-								}
+								Getsign2 = FALSE;
 							}
-						if (Getsign3 ==TRUE) {
-							if (utc> tbegin3+2){
-								if (Ch1> Frepbefore3) { Signe3=-1.0;}
-								else {Signe3=1.0;}
+						}
+						if (Getsign3 == TRUE) {
+							if (utc > tbegin3+2){
+								if (Ch3 > Frepbefore3)
+									Signe3 = -1.0;
+								else
+									Signe3 = +1.0;
 								SetCtrlVal(MainPanel, PANEL_DDS3, Frequency3) ; 
-								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, Frequency3);
-								Getsign3=FALSE;
-								}
+								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, Frequency3);
+								Getsign3 = FALSE;
 							}
+						}
 								
 						
 						
@@ -2085,47 +2160,26 @@
 }
 
 
-int CVICALLBACK OnAcceptN (int panel, int control, int event,
+int CVICALLBACK CB_OnAcceptN (int panel, int control, int event,
 		void *callbackData, int eventData1, int eventData2)
 {
-	double N;
-	
 	switch (event)
-		{
+	{
 		case EVENT_COMMIT:
 			if (panel == CalcN1Panel) {
-				N = floor(N_1);
-					if (N_1-N<0.5)
-					{
-						N1=N;
-					}
-					else
-					{
-						N1=N+1;
-					}
-				SetCtrlVal(MainPanel, PANEL_N1CHOICE, N1) ;
-				} 
-			
+				N1 = round(N_1);
+				SetCtrlVal(MainPanel, PANEL_N1CHOICE, N1);
+			}
 			if (panel == CalcN2Panel) {
-				N = floor(N_2);
-					if (N_2-N<0.5)
-					{
-						N2=N;
-					}
-					else
-					{
-						N2=N+1;
-					}
-				SetCtrlVal(MainPanel, PANEL_N2CHOICE, N2) ;
-				} 
-
+				N2 = round(N_2);
+				SetCtrlVal(MainPanel, PANEL_N2CHOICE, N2);
+			}
 			if (panel == CalcN3Panel) {
-				GetCtrlVal(CalcN3Panel, CALCN3_N_3, &N) ;
-				SetCtrlVal(MainPanel, PANEL_N3CHOICE, N) ;
-				N3=N;
-				} 
+				N3 = round(N_3);
+				SetCtrlVal(MainPanel, PANEL_N3CHOICE, N3);
+			} 
 			break;
-		}
+	}
 	return 0;
 }
 
@@ -2287,36 +2341,20 @@
 {
 	
 	switch (event)
-		{
+	{
 		case EVENT_COMMIT:
-			if (control==CALCN1_COMMANDBUTTON)
-			{
-					Measuring_1=TRUE;
-			}
-			if (control==CALCN2_COMMANDBUTTON) 
-			{
-					Measuring_2=TRUE;
-			}
+			if (panel == CalcN1Panel)
+				Measuring_1 = TRUE;
+			if (panel == CalcN2Panel)
+				Measuring_2 = TRUE;
+			if (panel == CalcN3Panel)
+				Measuring_3 = TRUE;
 		break;
-		}
-			 
-
+	}
 	
 	return 0;
 }
 
-int CVICALLBACK CB_OnStartN3Calculus (int panel, int control, int event,
-		void *callbackData, int eventData1, int eventData2)
-{
-	
-	switch (event)
-	{
-		case EVENT_COMMIT:
-			Measuring_3=TRUE;
-			break;
-	}
-	return 0;
-}
 
 
 int CVICALLBACK CB_OnNStop (int panel, int control, int event,
@@ -2325,116 +2363,93 @@
 	int PanelIsVisible;
 	
 	switch (event)
-		{
+	{
 		case EVENT_COMMIT:
-			//switch (control)
-			if (control==CALCN1_COMMANDBUTTON_2)
-			{
-			GetPanelAttribute (CalcN1Panel, ATTR_VISIBLE , &PanelIsVisible);
-					if (PanelIsVisible) {
-						HidePanel(CalcN1Panel) ;
-						}
-			
-						Measuring_1=FALSE;
-						Step1_1=FALSE ;
-						Step2_1=FALSE ;
-						Step3_1=FALSE ;
-						Frepminus_1=0.0;
-						Frepplus_1=0.0;
-			  		   t1_1=0.0;
-			 		   t2_1=0.0;
-			 		   t3_1=0.0; 
-						N_slope_1=0;
-						Frequ_slope_1=0.0;
-						Moy_slope_1=0.0;
-						Slope_slope_1 =0.0;
-						Ch4_slope=0.0;
-						Moy_Ch4slope_1=0.0;
-						Slope_Ch4slope_1=0.0;
-						DDSBesChanged1=FALSE;
-						DDSBesChanged2=FALSE; 
-						nDDSChange=0.0;
-						
-						SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
-						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
-						SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit) ;
-						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyDDSBesInit); 
-						nstabilization=0;	
+			if (panel == CalcN1Panel) {
+				GetPanelAttribute (CalcN1Panel, ATTR_VISIBLE, &PanelIsVisible);
+				if (PanelIsVisible)
+					HidePanel(CalcN1Panel);
+				
+				Measuring_1=FALSE;
+				Step1_1=FALSE ;
+				Step2_1=FALSE ;
+				Step3_1=FALSE ;
+				Frepminus_1=0.0;
+				Frepplus_1=0.0;
+			  	t1_1=0.0;
+			 	t2_1=0.0;
+			 	t3_1=0.0; 
+				N_slope_1=0;
+				Frequ_slope_1=0.0;
+				Moy_slope_1=0.0;
+				Slope_slope_1 =0.0;
+				Ch4_slope=0.0;
+				Moy_Ch4slope_1=0.0;
+				Slope_Ch4slope_1=0.0;
+				DDSBesChanged1=FALSE;
+				DDSBesChanged2=FALSE; 
+				nDDSChange=0.0;
+					
+				SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
+				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
+				SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit) ;
+				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyDDSBesInit); 
+				nstabilization=0;
+			}	
+			if (panel == CalcN2Panel) {
+				GetPanelAttribute (CalcN2Panel, ATTR_VISIBLE, &PanelIsVisible);
+				if (PanelIsVisible)
+					HidePanel(CalcN2Panel);
+				
+				Measuring_2=FALSE;
+				Step1_2=FALSE ;
+				Step2_2=FALSE ;
+				Step3_2=FALSE ;
+				Frepminus_2=0.0;
+				Delta10K_Minus=0.0;
+				Frepplus_2=0.0;
+				Delta10K_Plus=0.0; 
+				DeltaDDS3=0.0; 
+			 	t1_2=0.0;
+			  	t2_2=0.0;
+			  	t3_2=0.0; 
+				N_slope_2=0;
+				Frequ_slope_2=0.0;
+				Moy_slope_2=0.0;
+				Beat_slope_2=0.0;
+				Moy_Beatslope_2=0.0;
+				Slope_Beatslope_2 =0.0;
+				Slope_slope_2 =0.0;
+				DDSBesChanged1=FALSE;
+				DDSBesChanged2=FALSE; 
+				nDDSChange=0.0;
+				
+				SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
+				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
+				SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit) ;
+				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyDDSBesInit);
+				SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init) ;
+				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequencyDDS3Init); 
+				nstabilization=0;
 			}
-			
-			
-			if (control==CALCN2_STOP2)
-			{  
-			GetPanelAttribute (CalcN2Panel, ATTR_VISIBLE , &PanelIsVisible);
-					if (PanelIsVisible) {
-						HidePanel(CalcN2Panel) ;
-						}
-			
-						Measuring_2=FALSE;
-						Step1_2=FALSE ;
-						Step2_2=FALSE ;
-						Step3_2=FALSE ;
-						Frepminus_2=0.0;
-						Delta10K_Minus=0.0;
-						Frepplus_2=0.0;
-						Delta10K_Plus=0.0; 
-						DeltaDDS3=0.0; 
-			 		    t1_2=0.0;
-			  	  	    t2_2=0.0;
-			  		    t3_2=0.0; 
-						N_slope_2=0;
-						Frequ_slope_2=0.0;
-						Moy_slope_2=0.0;
-						Beat_slope_2=0.0;
-						Moy_Beatslope_2=0.0;
-						Slope_Beatslope_2 =0.0;
-						Slope_slope_2 =0.0;
-						DDSBesChanged1=FALSE;
-						DDSBesChanged2=FALSE; 
-						nDDSChange=0.0;
-						SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
-						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
-						SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit) ;
-						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyDDSBesInit);
-						SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init) ;
-						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequencyDDS3Init); 
-						nstabilization=0;
+			if (panel == CalcN3Panel) {		
+				GetPanelAttribute (CalcN3Panel, ATTR_VISIBLE, &PanelIsVisible);
+				if (PanelIsVisible)										  
+					HidePanel(CalcN3Panel);
+				
+				Measuring_3 = FALSE;
+				Frepminus_3 = 0.0;
+				Frepplus_3 = 0.0;
+			   	t1_3 = 0.0;
+			  	t2_3 = 0.0;
+			   	t3_3 = 0.0; 
+				N_slope_3 = 0;
+				Frequ_slope_3 = 0.0;
+				Moy_slope_3 = 0.0;
+				Slope_slope_3 = 0.0;
 			}
-				
-			
 		break;
-		}
-	return 0;
-}
-
-int CVICALLBACK CB_OnN3Stop (int panel, int control, int event,
-		void *callbackData, int eventData1, int eventData2)
-{
-	int PanelIsVisible;
-	
-	switch (event)
-		{
-		case EVENT_COMMIT:
-					GetPanelAttribute (CalcN3Panel, ATTR_VISIBLE , &PanelIsVisible);
-					if (PanelIsVisible) {										  
-						HidePanel(CalcN3Panel) ;
-						}
-			
-						Measuring_3=FALSE;
-						Step1_3=FALSE ;
-						Step2_3=FALSE ;
-						Step3_3=FALSE ;
-						Frepminus_3=0.0;
-						Frepplus_3=0.0;
-			   		 t1_3=0.0;
-			  		  t2_3=0.0;
-			   		 t3_3=0.0; 
-						N_slope_3=0;
-						Frequ_slope_3=0.0;
-						Moy_slope_3=0.0;
-						Slope_slope_3 =0.0;
-			break;
-		}
+	}
 	return 0;
 }
 
@@ -2444,25 +2459,22 @@
 {
 	double Time ;
 	
-	switch (event)
-		{
+	switch (event) {
 		case EVENT_COMMIT:
-			if (control==CALCN1_INTEGRATIONTIME){
+			if (panel == CalcN1Panel) {
 				GetCtrlVal(CalcN1Panel, CALCN1_INTEGRATIONTIME, &Time) ;
 				DeltaT_1=Time;
 			}
-			if (control==CALCN2_INTEGRATIONTIME){
+			if (panel == CalcN2Panel) {
 				GetCtrlVal(CalcN2Panel, CALCN2_INTEGRATIONTIME, &Time) ;
 				DeltaT_2=Time;
 			}
-			if (control==CALCN3_INTEGRATIONTIME){
+			if (panel == CalcN3Panel) {
 				GetCtrlVal(CalcN3Panel, CALCN3_INTEGRATIONTIME, &Time) ;
 				DeltaT_3=Time;
 			}
-
 			break;
-			
-		}
+	}
 	return 0;
 }
 
@@ -2473,68 +2485,64 @@
 	double Frequ ;
 	
 	switch (event)
-		{
+	{
 		case EVENT_COMMIT:
-			if (control==CALCN1_DELTAFREQ){
-				GetCtrlVal(CalcN1Panel, CALCN1_DELTAFREQ, &Frequ) ;
-				DeltakHz_1=Frequ;
+			if (panel == CalcN1Panel) {
+				GetCtrlVal(CalcN1Panel, CALCN1_DELTAFREQ, &Frequ);
+				DeltakHz_1 = Frequ;
 			}
-			if (control==CALCN2_DELTAFREQ){
+			if (panel == CalcN2Panel) {
 				GetCtrlVal(CalcN2Panel, CALCN2_DELTAFREQ, &Frequ) ;
 				DeltakHz_2=Frequ;
 			}
-			if (control==CALCN3_DELTAFREQ){
+			if (panel == CalcN3Panel) {
 				GetCtrlVal(CalcN3Panel, CALCN3_DELTAFREQ, &Frequ) ;
 				DeltakHz_3=Frequ;
 			}
-
 			break;
-			
-		}
+	}
 	return 0;
 }
 
 
 int  CVICALLBACK CB_OnFindSign (int panel, int control, int event, 
-		 void *callbackData, int eventData1, int eventData2)
+		void *callbackData, int eventData1, int eventData2)
 {
 	
-	switch (event)
-		{
+	switch (event) {
 		case EVENT_COMMIT:
-			if (control==PANEL_FINDSIGN1){
-				tbegin1=utc;
-				Frepbefore1=Math1;
-				GetCtrlVal(MainPanel, PANEL_DDS1, &Frequency1) ;
-				SetCtrlVal(MainPanel, PANEL_DDS1, Frequency1+Frequencystep1) ;  
-				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency1+Frequencystep1);
-				Getsign1=TRUE;
+			switch (control)
+			{
+				case PANEL_FINDSIGN1:
+					tbegin1 = utc;
+					Frepbefore1 = Math1;
+					GetCtrlVal(MainPanel, PANEL_DDS1, &Frequency1) ;
+					SetCtrlVal(MainPanel, PANEL_DDS1, Frequency1+Frequencystep1) ;  
+					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency1+Frequencystep1);
+					Getsign1 = TRUE;
+					break;
+				case PANEL_FINDSIGN2:
+					tbegin2 = utc;
+					Frepbefore2 = Math1;
+					Ch2before = Ch2;
+					GetCtrlVal(MainPanel, PANEL_DDS1, &Frequency2) ;
+					SetCtrlVal(MainPanel, PANEL_DDS1, Frequency2+Frequencystep2) ;
+					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency2+Frequencystep2);
+					Getsign2 = TRUE;
+					break;
+				case PANEL_FINDSIGN3:
+					tbegin3 = utc;
+					Frepbefore3 = Math1;
+					GetCtrlVal(MainPanel, PANEL_DDS4, &Frequency3) ;
+					SetCtrlVal(MainPanel, PANEL_DDS4, Frequency3+Frequencystep3) ; 
+					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, Frequency3+Frequencystep3);
+					Getsign3 = TRUE;
+					break;
 			}
-			if (control==PANEL_FINDSIGN2){
-				tbegin2=utc;
-				Frepbefore2=Math1;
-				Ch2before=Ch2;
-				GetCtrlVal(MainPanel, PANEL_DDS1, &Frequency2) ;
-				SetCtrlVal(MainPanel, PANEL_DDS1, Frequency2+Frequencystep2) ;
-				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency2+Frequencystep2);
-				Getsign2=TRUE;
-			}
-			if (control==PANEL_FINDSIGN3){
-				tbegin3=utc;
-				Frepbefore3=Ch1;
-				GetCtrlVal(MainPanel, PANEL_DDS3, &Frequency3) ;
-				SetCtrlVal(MainPanel, PANEL_DDS3, Frequency3+Frequencystep3) ; 
-				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, Frequency3+Frequencystep3);
-				Getsign3=TRUE;
-			}
-
-
 			break;
-			
-		}
+	}
 	return 0;
-} 
-
+}
 
 
 int  CVICALLBACK CB_OnFind275K (int panel, int control, int event, 
@@ -2577,21 +2585,22 @@
 	double Time ;
 	
 	switch (event)
-		{
+	{
 		case EVENT_COMMIT:
-			if (control==CALCN1_SLOPETIME){
-				GetCtrlVal(CalcN1Panel, CALCN1_SLOPETIME, &Time) ;
+			if (panel == CalcN1Panel) {
+				GetCtrlVal(CalcN1Panel, CALCN1_SLOPETIME, &Time);
 				SlopeTime1=Time;
 			}
-
-			if (control==CALCN2_SLOPETIME){
-				GetCtrlVal(CalcN2Panel, CALCN2_SLOPETIME, &Time) ;
+			if (panel == CalcN2Panel) {
+				GetCtrlVal(CalcN2Panel, CALCN2_SLOPETIME, &Time);
 				SlopeTime2=Time;
 			}
-
+			if (panel == CalcN3Panel) {
+				GetCtrlVal(CalcN3Panel, CALCN3_SLOPETIME, &Time);
+				SlopeTime3=Time;
+			}
 			break;
-			
-		}
+	}
 	return 0;
 }
 
@@ -2876,7 +2885,7 @@
 	return 0;
 }
 
-int CVICALLBACK SlopeReference_cb (int panel, int control, int event,
+int CVICALLBACK CB_OnSlopeReference (int panel, int control, int event,
 		void *callbackData, int eventData1, int eventData2)
 {
 	switch (event)
@@ -2887,3 +2896,84 @@
 	}
 	return 0;
 }
+
+
+int CVICALLBACK CB_OnEstimateN (int panel, int control, int event,
+		void *callbackData, int eventData1, int eventData2)
+{
+	int visible;
+	double wl;
+	
+	switch (event)
+	{
+		case EVENT_COMMIT:
+			/* be prepaid to support more N estimates for different beat notes */
+			switch (control)
+			{
+				case PANEL_ESTIMATE_N3:
+					GetPanelAttribute(EstimateN3Panel, ATTR_VISIBLE , &visible);
+					if (! visible) {
+						DisplayPanel(EstimateN3Panel);
+					}
+					/* set current frep */
+					SetCtrlVal(EstimateN3Panel, ESTIMATEN3_FREP, 250e6 + Math1);
+					/* default wavelenght for Sr cavity */
+					GetCtrlVal(EstimateN3Panel, ESTIMATEN3_WAVELENGTH, &wl);
+					if (wl == 0.0)
+						SetCtrlVal(EstimateN3Panel, ESTIMATEN3_WAVELENGTH, 698.50);
+					/* reset N3 estimate */
+					SetCtrlVal(EstimateN3Panel, ESTIMATEN3_N, 0.0);
+					break;
+			}
+			break;
+	}
+	
+	return 0;
+}			
+
+int CVICALLBACK CB_OnNEstimateCancel (int panel, int control, int event,
+		void *callbackData, int eventData1, int eventData2)
+{
+	int visible;
+	
+	switch (event)
+	{
+		case EVENT_COMMIT:
+			GetPanelAttribute(panel, ATTR_VISIBLE, &visible);
+			if (visible)
+				HidePanel(panel);
+			break;
+	}
+	return 0;
+}
+
+int CVICALLBACK CB_OnNEstimateSet (int panel, int control, int event,
+		void *callbackData, int eventData1, int eventData2)
+{
+	switch (event)
+	{
+		case EVENT_COMMIT:
+			GetCtrlVal(panel, ESTIMATEN3_N, &N3);
+			SetCtrlVal(MainPanel, PANEL_N3CHOICE, N3);
+			HidePanel(panel);
+			break;
+	}
+	return 0;
+}
+
+int CVICALLBACK CB_OnNEstimate (int panel, int control, int event,
+		void *callbackData, int eventData1, int eventData2)
+{
+	double frep, wl, N;
+	
+	switch (event)
+	{
+		case EVENT_COMMIT:
+			GetCtrlVal(panel, ESTIMATEN3_FREP, &frep);
+			GetCtrlVal(panel, ESTIMATEN3_WAVELENGTH, &wl);
+			N = round(299792458.0 / wl / 1e-9 / frep);
+			SetCtrlVal(panel, ESTIMATEN3_N, N);
+			break;
+	}
+	return 0;
+}