diff FXAnalyse.c @ 44:2e37910b28bc

Simplify logic and reindent
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Tue, 09 Oct 2012 14:36:10 +0200
parents 39a0f396d778
children 6503feae1809
line wrap: on
line diff
--- a/FXAnalyse.c	Tue Oct 09 13:32:51 2012 +0200
+++ b/FXAnalyse.c	Tue Oct 09 14:36:10 2012 +0200
@@ -532,923 +532,914 @@
 	switch (event)
 	{
 		case EVENT_TIMER_TICK:
-			switch(Acquiring) 
-				{
-				case TRUE:
-					
-					GetFileInfo(LogFileName, &LogFileSize) ;
+			
+			if (! Acquiring)
+				break;
+			
+			GetFileInfo(LogFileName, &LogFileSize);
 					
-					if (LogFileSize > OldLogFilePtr+2*FXLINELENGTH-2) {  // if a complete newline has been written
-						
-						SuspendTimerCallbacks() ; 
-						
-						// Open Log file and get to the beginning of newly completed line
-						LogFile = OpenFile(LogFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII) ;
-						OldLogFilePtr += FXLINELENGTH;
-						SetFilePtr(LogFile, OldLogFilePtr, 0) ;
-					
-						// return the last complete string from the log file and scan it for date and time information 
-						
-						// first, the time tag, and store it in various formats
-						ReadFile(LogFile, TimeTag, 17);
-						
-						CopyBytes(Date,0,TimeTag,4,2);
-						CopyBytes(Date,3,TimeTag,2,2);   
-						CopyBytes(Date,8,TimeTag,0,2);
-						CopyBytes(Time,0,TimeTag,7,2);
-						CopyBytes(Time,3,TimeTag,9,2);
-						CopyBytes(Time,6,TimeTag,11,6);
-						SetCtrlVal(MainPanel, PANEL_DATE, Date) ;
-						SetCtrlVal(MainPanel, PANEL_TIME, Time) ; 
+			if (LogFileSize > OldLogFilePtr+2*FXLINELENGTH-2) {  // if a complete newline has been written
 						
-						CopyBytes(Year,2,TimeTag,0,2); // first 2 bytes of year string remains "20"
-						CopyBytes(ShortYear,0,TimeTag,0,2);
-						CopyBytes(Month,0,TimeTag,2,2);
-						CopyBytes(Day,0,TimeTag,4,2);
-						CopyBytes(Hour,0,TimeTag,7,2);
-						CopyBytes(Min,0,TimeTag,9,2);
-						CopyBytes(Sec,0,TimeTag,11,6);
-						Fmt(&LocalTime.tm_year, "%d<%s", Year);
-						Fmt(&LocalTime.tm_mon,  "%d<%s", Month);
-						Fmt(&LocalTime.tm_mday, "%d<%s", Day);
-						Fmt(&LocalTime.tm_hour, "%d<%s", Hour);
-						Fmt(&LocalTime.tm_min,  "%d<%s", Min);
-						Fmt(&LocalTime.tm_sec,  "%d<%s", "00");    // special case to handle non integer number of UTC seconds
-						LocalTime.tm_hour += 0;
-						LocalTime.tm_min  -= 0;  
-						LocalTime.tm_sec  -= 0;  
-						LocalTime.tm_mon  -= 1 ;  	// january is month 0 for tm struct
-						LocalTime.tm_year -= 1900 ; // year is number of years since 1900 for tm struct
-						LocalTime.tm_isdst = -1;  // daylight saving flag MUST be set to -1 (unallocated is bugging and +1 is making 1 hour error in summer)
-						utcTime = mktime (&LocalTime);
-						utc = (double) utcTime + strtod(Sec,NULL) ;
-						mjd=utc/86400.;		//nb de jours depuis l'origine d'UTC (01/01/1900 à 00h00 GMT)
-						mjd+=15020;			//date MJD de la date origine d'UTC
-						SetCtrlVal(MainPanel, PANEL_UTC, utc);
-						SetCtrlVal(MainPanel, PANEL_MJD, mjd);
+				SuspendTimerCallbacks();
 				
-						// scan the line for counters's channels information
-						
-						ReadLine(LogFile, LineBuffer, FXLINELENGTH+9) ;
-						CloseFile(LogFile);
-						
-						Scan(LineBuffer, "%f%f%f%f", &Ch1, &Ch2, &Ch3, &Ch4) ;
-						Ch1 = 1000*Ch1;
-						Ch2 = 1000*Ch2;
-						Ch3 = 1000*Ch3;
-						Ch4 = 1000*Ch4;
-						
-						SetCtrlVal(MainPanel, PANEL_FREQ1, Ch1) ;
-						SetCtrlVal(MainPanel, PANEL_FREQ2, Ch2) ;
-						SetCtrlVal(MainPanel, PANEL_FREQ3, Ch3) ;  
-						SetCtrlVal(MainPanel, PANEL_FREQ4, Ch4) ;
-						SetCtrlVal(MainPanel, PANEL_SIGN1, Signe1);
-						SetCtrlVal(MainPanel, PANEL_SIGN2, Signe2); 
-						SetCtrlVal(MainPanel, PANEL_SIGN3, Signe3); 
-						SetCtrlVal(CalcN2Panel, CALCN2_N, N_2);
-						SetCtrlVal(CalcN2Panel, CALCN2_SLOPE, Beatslope_2);
-						SetCtrlVal(MainPanel, PANEL_CENTERFREQUENCY, CenterFrequencyCh2);  
-						SetCtrlVal(MainPanel, PANEL_LED1, SlopeMeasuring); 	   
-						SetCtrlVal(MainPanel, PANEL_LED2, OnSlopeCancelling);
-						// Treat data
+				// Open Log file and get to the beginning of newly completed line
+				LogFile = OpenFile(LogFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
+				OldLogFilePtr += FXLINELENGTH;
+				SetFilePtr(LogFile, OldLogFilePtr, 0);
+				
+				// return the last complete string from the log file and scan it for date and time information 
+				
+				// first, the time tag, and store it in various formats
+				ReadFile(LogFile, TimeTag, 17);
 						
-						Math1 = mupEval(MathParser1) ;
-						SetCtrlVal(MainPanel,PANEL_MATH1, Math1) ;
-
-						Math2 = mupEval(MathParser2) ;
-						SetCtrlVal(MainPanel,PANEL_MATH2, Math2) ;
-						
-						Math3 = mupEval(MathParser3) ;
-						SetCtrlVal(MainPanel,PANEL_MATH3, Math3) ;
-						
-						Math4 = mupEval(MathParser4) ;
-						SetCtrlVal(MainPanel,PANEL_MATH4, Math4) ;
-						
-						Math5 = mupEval(MathParser5) ;
-						SetCtrlVal(MainPanel,PANEL_MATH5, Math5) ;
-						
-						// Plot Data and calculus if required
-						
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ1PLOT, &BoxChecked) ;
-						if (BoxChecked) {
-							Plot_AddFrequency(&PlotCh1, Ch1) ;
-							}
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ1ALLAN, &BoxChecked) ;
-						if (BoxChecked) {
-							Allan_AddFrequency(&AllanCh1, Ch1) ;
-							}
-
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ2PLOT, &BoxChecked) ;
-						if (BoxChecked) {
-							Plot_AddFrequency(&PlotCh2, Ch2) ;
-							}
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ2ALLAN, &BoxChecked) ;
-						if (BoxChecked) {
-							Allan_AddFrequency(&AllanCh2, Ch2) ;
-							}
-							
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ3PLOT, &BoxChecked) ;
-						if (BoxChecked) {
-							Plot_AddFrequency(&PlotCh3, Ch3) ;
-							}
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ3ALLAN, &BoxChecked) ;
-						if (BoxChecked) {
-							Allan_AddFrequency(&AllanCh3, Ch3) ;
-							}
-							
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ4PLOT, &BoxChecked) ;
-						if (BoxChecked) {
-							Plot_AddFrequency(&PlotCh4, Ch4) ;
-							}
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ4ALLAN, &BoxChecked) ;
-						if (BoxChecked) {
-							Allan_AddFrequency(&AllanCh4, Ch4) ;
-							}
-						
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH1PLOT, &BoxChecked) ;
-						if (BoxChecked) {
-							Plot_AddFrequency(&PlotMath1, Math1) ;
-							}
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH1ALLAN, &BoxChecked) ;
-						if (BoxChecked) {
-							Allan_AddFrequency(&AllanMath1, Math1) ;
-							}
-	
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2PLOT, &BoxChecked) ;
-						if (BoxChecked) {
-							Plot_AddFrequency(&PlotMath2, Math2) ;
-							}
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2ALLAN, &BoxChecked) ;
-						if (BoxChecked) {
-							Allan_AddFrequency(&AllanMath2, Math2) ;
-							}
-						
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3PLOT, &BoxChecked) ;
-						if (BoxChecked) {
-							Plot_AddFrequency(&PlotMath3, Math3) ;
-							}
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3ALLAN, &BoxChecked) ;
-						if (BoxChecked) {
-							Allan_AddFrequency(&AllanMath3, Math3) ;
-							}
-						
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH4PLOT, &BoxChecked) ;
-						if (BoxChecked) {
-							Plot_AddFrequency(&PlotMath4, Math4) ;
-							}
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH4ALLAN, &BoxChecked) ;
-						if (BoxChecked) {
-							Allan_AddFrequency(&AllanMath4, Math4) ;
-							}
+				CopyBytes(Date,0,TimeTag,4,2);
+				CopyBytes(Date,3,TimeTag,2,2);   
+				CopyBytes(Date,8,TimeTag,0,2);
+				CopyBytes(Time,0,TimeTag,7,2);
+				CopyBytes(Time,3,TimeTag,9,2);
+				CopyBytes(Time,6,TimeTag,11,6);
+				SetCtrlVal(MainPanel, PANEL_DATE, Date);
+				SetCtrlVal(MainPanel, PANEL_TIME, Time); 
+				
+				CopyBytes(Year,2,TimeTag,0,2); // first 2 bytes of year string remains "20"
+				CopyBytes(ShortYear,0,TimeTag,0,2);
+				CopyBytes(Month,0,TimeTag,2,2);
+				CopyBytes(Day,0,TimeTag,4,2);
+				CopyBytes(Hour,0,TimeTag,7,2);
+				CopyBytes(Min,0,TimeTag,9,2);
+				CopyBytes(Sec,0,TimeTag,11,6);
+				Fmt(&LocalTime.tm_year, "%d<%s", Year);
+				Fmt(&LocalTime.tm_mon,  "%d<%s", Month);
+				Fmt(&LocalTime.tm_mday, "%d<%s", Day);
+				Fmt(&LocalTime.tm_hour, "%d<%s", Hour);
+				Fmt(&LocalTime.tm_min,  "%d<%s", Min);
+				Fmt(&LocalTime.tm_sec,  "%d<%s", "00");    // special case to handle non integer number of UTC seconds
+				LocalTime.tm_hour += 0;
+				LocalTime.tm_min  -= 0;  
+				LocalTime.tm_sec  -= 0;  
+				LocalTime.tm_mon  -= 1;  	// january is month 0 for tm struct
+				LocalTime.tm_year -= 1900; // year is number of years since 1900 for tm struct
+				LocalTime.tm_isdst = -1;  // daylight saving flag MUST be set to -1 (unallocated is bugging and +1 is making 1 hour error in summer)
+				utcTime = mktime (&LocalTime);
+				utc = (double) utcTime + strtod(Sec,NULL);
+				mjd=utc/86400.;		//nb de jours depuis l'origine d'UTC (01/01/1900 à 00h00 GMT)
+				mjd+=15020;			//date MJD de la date origine d'UTC
+				SetCtrlVal(MainPanel, PANEL_UTC, utc);
+				SetCtrlVal(MainPanel, PANEL_MJD, mjd);
+				
+				// scan the line for counters's channels information
+				
+				ReadLine(LogFile, LineBuffer, FXLINELENGTH+9);
+				CloseFile(LogFile);
+				
+				Scan(LineBuffer, "%f%f%f%f", &Ch1, &Ch2, &Ch3, &Ch4);
+				Ch1 = 1000*Ch1;
+				Ch2 = 1000*Ch2;
+				Ch3 = 1000*Ch3;
+				Ch4 = 1000*Ch4;
 						
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5PLOT, &BoxChecked) ;
-						if (BoxChecked) {
-							Plot_AddFrequency(&PlotMath5, Math5) ;
-							}
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5ALLAN, &BoxChecked) ;
-						if (BoxChecked) {
-							Allan_AddFrequency(&AllanMath5, Math5) ;
-							}
-						
-						// Calcul de N
-						
-						switch (Measuring_1) {
-
-							case N_MEASUREMENT_NONE:
-								// not measuring
-								break;
-							
-							case N_MEASUREMENT_INIT:
-								// initialization step
-								
-								// set DDS1 to nominal frequency
-								SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1);
-								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
-									
-								GetCtrlVal(MainPanel, PANEL_DDS2, &FrequencyDDSBesInit);
-								t2_1 = t3_1 = 0.0;
-								t1_1 = utc;
-								Frequ_slope_1 = Math1;
-								Moy_slope_1 = Frequ_slope_1;
-								Ch4_slope = Ch4;
-								Moy_Ch4slope_1 = Ch4_slope;
-								N_slope_1 = 1;
-								
-								// next step
-								Measuring_1 += 1;
-								break;
-								
-							case N_MEASUREMENT_SLOPE:
-								// slope measurement
-								
-								N_slope_1 = N_slope_1 + 1; 
-								Frequ_slope_1 = Math1;
-								Ch4_slope = Ch4; 
-								Moy_slope_1 = ((N_slope_1-1)*Moy_slope_1 + Frequ_slope_1)/N_slope_1;
-								Moy_Ch4slope_1 = ((N_slope_1-1)*Moy_Ch4slope_1 + Ch4_slope)/N_slope_1;
-								Slope_slope_1 = (Slope_slope_1*(N_slope_1-2) + 6*(Frequ_slope_1-Moy_slope_1)/N_slope_1)/(N_slope_1+1);
-								Slope_Ch4slope_1 = (Slope_Ch4slope_1*(N_slope_1-2) + 6*(Ch4_slope-Moy_Ch4slope_1)/N_slope_1)/(N_slope_1+1);
-								
-								if ((utc - t1_1) > SlopeTime1) {
-									Slope_1 = Slope_slope_1;
-									Ch4Slope =  Slope_Ch4slope_1;
-									SetCtrlVal(CalcN1Panel, CALCN1_SLOPE, Slope_1);
-									
-									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;
-
-									// frep positive step
-									DDS4xAD9912_FrequencyRampe(&DDS4xAD9912,1, FrequDDS1,(FrequDDS1+DeltakHz_1*1000), Step1/Ndiv);
-									SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1+DeltakHz_1*1000));
-									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1+DeltakHz_1*1000));
-								
-									// allow counter to settle
-									settling = 3;
-								
-									// next step
-									Measuring_1 += 1;
-								}
-								break;
-								
-							case N_MEASUREMENT_ADJUST_FREQ_PLUS:
-							case N_MEASUREMENT_ADJUST_FREQ_MINUS:
-								// adjust DDS frequency to keep beatnote within the bandpass filter
+				SetCtrlVal(MainPanel, PANEL_FREQ1, Ch1);
+				SetCtrlVal(MainPanel, PANEL_FREQ2, Ch2);
+				SetCtrlVal(MainPanel, PANEL_FREQ3, Ch3);  
+				SetCtrlVal(MainPanel, PANEL_FREQ4, Ch4);
+				SetCtrlVal(MainPanel, PANEL_SIGN1, Signe1);
+				SetCtrlVal(MainPanel, PANEL_SIGN2, Signe2); 
+				SetCtrlVal(MainPanel, PANEL_SIGN3, Signe3); 
+				SetCtrlVal(CalcN2Panel, CALCN2_N, N_2);
+				SetCtrlVal(CalcN2Panel, CALCN2_SLOPE, Beatslope_2);
+				SetCtrlVal(MainPanel, PANEL_CENTERFREQUENCY, CenterFrequencyCh2);  
+				SetCtrlVal(MainPanel, PANEL_LED1, SlopeMeasuring); 	   
+				SetCtrlVal(MainPanel, PANEL_LED2, OnSlopeCancelling);
+				// Treat data
+				
+				Math1 = mupEval(MathParser1);
+				SetCtrlVal(MainPanel,PANEL_MATH1, Math1);
+				
+				Math2 = mupEval(MathParser2);
+				SetCtrlVal(MainPanel,PANEL_MATH2, Math2);
+				
+				Math3 = mupEval(MathParser3);
+				SetCtrlVal(MainPanel,PANEL_MATH3, Math3);
+				
+				Math4 = mupEval(MathParser4);
+				SetCtrlVal(MainPanel,PANEL_MATH4, Math4);
+				
+				Math5 = mupEval(MathParser5);
+				SetCtrlVal(MainPanel,PANEL_MATH5, Math5);
+				
+				// Plot Data and calculus if required
+				
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ1PLOT, &BoxChecked);
+				if (BoxChecked) {
+					Plot_AddFrequency(&PlotCh1, Ch1);
+				}
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ1ALLAN, &BoxChecked);
+				if (BoxChecked) {
+					Allan_AddFrequency(&AllanCh1, Ch1);
+				}
 
-								if (settling > 0) {
-									settling--;
-									break;
-								}
-								
-								double fDDS2;
-								GetCtrlVal(MainPanel, PANEL_DDS2, &fDDS2);
-								fDDS2 += 275000 - Ch4;
-								SetCtrlVal(MainPanel, PANEL_DDS2, fDDS2);
-								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, fDDS2);
-								
-								// allow counter to settle
-								settling = 3;
-								
-								// next step
-								Measuring_1 += 1;
-								break;								
-								
-							case N_MEASUREMENT_FREP_PLUS:
-								// frep positive step
-								
-								if (settling > 0) {
-									settling--;
-									break;
-								}
-								
-								if (t2_1 == 0.0)
-									t2_1 = utc;
-								
-								Frepplus_1 = Frepplus_1 + Math1 - Slope_1 * (utc - t2_1);
-								Ch4Plus = Ch4Plus + Ch4 - Ch4Slope * (utc - t2_1);
-								n_1 += 1;
-									
-								if ((utc - t2_1) > DeltaT_1) {
-									Frepplus_1 = Frepplus_1 / n_1;
-									Ch4Plus = Ch4Plus / n_1; 
-									n_1 = 0;
-									
-									// frep negative step
-									DDS4xAD9912_FrequencyRampe(&DDS4xAD9912,1, (FrequDDS1+DeltakHz_1*1000),(FrequDDS1-DeltakHz_1*1000), Step1/Ndiv);
-									SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1-DeltakHz_1*1000));  
-									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1-DeltakHz_1*1000));
-									
-									// allow counter to settle
-									settling = 3;
-								
-									// next step
-									Measuring_1 += 1;
-								}
-								break;
-												
-							case N_MEASUREMENT_FREP_MINUS:
-								// frep negative step
-								
-								if (settling > 0) {
-									settling--;
-									break;
-								}
-
-								if (t3_1 == 0.0)
-									t3_1 = utc;
-								
-								Frepminus_1 = Frepminus_1 + Math1 - Slope_1 * (utc - t3_1);
-								Ch4Minus = Ch4Minus + Ch4 - Ch4Slope * (utc - t3_1);
-								n_1 += 1;
-								
-								if ((utc - t3_1) > DeltaT_1) {
-									Frepminus_1 = Frepminus_1 / n_1;
-									Ch4Minus = Ch4Minus / n_1;
-									n_1 = 0;
-									
-									// compute N1
-									N_1 = Signe1 * (2*Ndiv * DeltakHz_1 * 1000)/(Frepminus_1 - Frepplus_1 - Slope_1 * (t3_1 - t2_1));
-									SetCtrlVal(CalcN1Panel, CALCN1_N, N_1);
-									
-									t1_1 = 0.0;
-									t2_1 = 0.0;
-									t3_1 = 0.0;
-									Frepminus_1 = 0.0;
-									Frepplus_1 = 0.0;
-									
-									// back to nominal frep
-									DDS4xAD9912_FrequencyRampe(&DDS4xAD9912, 1, FrequDDS1-DeltakHz_1*1000,FrequDDS1, Step1/Ndiv );
-									SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1);
-									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
-									SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit);
-									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyDDSBesInit);
-									
-									// done
-									Measuring_1 = N_MEASUREMENT_NONE;
-								}
-								break;
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ2PLOT, &BoxChecked);
+				if (BoxChecked) {
+					Plot_AddFrequency(&PlotCh2, Ch2);
+				}
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ2ALLAN, &BoxChecked);
+				if (BoxChecked) {
+					Allan_AddFrequency(&AllanCh2, Ch2);
+				}
+				
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ3PLOT, &BoxChecked);
+				if (BoxChecked) {
+					Plot_AddFrequency(&PlotCh3, Ch3);
+				}
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ3ALLAN, &BoxChecked);
+				if (BoxChecked) {
+					Allan_AddFrequency(&AllanCh3, Ch3);
+				}
+				
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ4PLOT, &BoxChecked);
+				if (BoxChecked) {
+					Plot_AddFrequency(&PlotCh4, Ch4);
+				}
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ4ALLAN, &BoxChecked);
+				if (BoxChecked) {
+					Allan_AddFrequency(&AllanCh4, Ch4);
+				}
+				
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH1PLOT, &BoxChecked);
+				if (BoxChecked) {
+					Plot_AddFrequency(&PlotMath1, Math1);
+				}
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH1ALLAN, &BoxChecked);
+				if (BoxChecked) {
+					Allan_AddFrequency(&AllanMath1, Math1);
+				}
+				
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2PLOT, &BoxChecked);
+				if (BoxChecked) {
+					Plot_AddFrequency(&PlotMath2, Math2);
+				}
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2ALLAN, &BoxChecked);
+				if (BoxChecked) {
+					Allan_AddFrequency(&AllanMath2, Math2);
+				}
+				
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3PLOT, &BoxChecked);
+				if (BoxChecked) {
+					Plot_AddFrequency(&PlotMath3, Math3);
+				}
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3ALLAN, &BoxChecked);
+				if (BoxChecked) {
+					Allan_AddFrequency(&AllanMath3, Math3);
+				}
+				
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH4PLOT, &BoxChecked);
+				if (BoxChecked) {
+					Plot_AddFrequency(&PlotMath4, Math4);
+				}
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH4ALLAN, &BoxChecked);
+				if (BoxChecked) {
+					Allan_AddFrequency(&AllanMath4, Math4);
+				}
+				
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5PLOT, &BoxChecked);
+				if (BoxChecked) {
+					Plot_AddFrequency(&PlotMath5, Math5);
+				}
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5ALLAN, &BoxChecked);
+				if (BoxChecked) {
+					Allan_AddFrequency(&AllanMath5, Math5);
+				}
+				
+				// Calcul de N
+				
+				switch (Measuring_1) {
+					
+					case N_MEASUREMENT_NONE:
+						// not measuring
+						break;
+					
+					case N_MEASUREMENT_INIT:
+						// initialization step
+						
+						// set DDS1 to nominal frequency
+						SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1);
+						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
+						
+						GetCtrlVal(MainPanel, PANEL_DDS2, &FrequencyDDSBesInit);
+						t2_1 = t3_1 = 0.0;
+						t1_1 = utc;
+						Frequ_slope_1 = Math1;
+						Moy_slope_1 = Frequ_slope_1;
+						Ch4_slope = Ch4;
+						Moy_Ch4slope_1 = Ch4_slope;
+						N_slope_1 = 1;
+						
+						// next step
+						Measuring_1 += 1;
+						break;
+						
+					case N_MEASUREMENT_SLOPE:
+						// slope measurement
+						
+						N_slope_1 = N_slope_1 + 1; 
+						Frequ_slope_1 = Math1;
+						Ch4_slope = Ch4; 
+						Moy_slope_1 = ((N_slope_1-1)*Moy_slope_1 + Frequ_slope_1)/N_slope_1;
+						Moy_Ch4slope_1 = ((N_slope_1-1)*Moy_Ch4slope_1 + Ch4_slope)/N_slope_1;
+						Slope_slope_1 = (Slope_slope_1*(N_slope_1-2) + 6*(Frequ_slope_1-Moy_slope_1)/N_slope_1)/(N_slope_1+1);
+						Slope_Ch4slope_1 = (Slope_Ch4slope_1*(N_slope_1-2) + 6*(Ch4_slope-Moy_Ch4slope_1)/N_slope_1)/(N_slope_1+1);
+						
+						if ((utc - t1_1) > SlopeTime1) {
+							Slope_1 = Slope_slope_1;
+							Ch4Slope =  Slope_Ch4slope_1;
+							SetCtrlVal(CalcN1Panel, CALCN1_SLOPE, Slope_1);
+							
+							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;
+							
+							// frep positive step
+							DDS4xAD9912_FrequencyRampe(&DDS4xAD9912,1, FrequDDS1,(FrequDDS1+DeltakHz_1*1000), Step1/Ndiv);
+							SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1+DeltakHz_1*1000));
+							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1+DeltakHz_1*1000));
+							
+							// allow counter to settle
+							settling = 3;
+							
+							// next step
+							Measuring_1 += 1;
+							}
+						break;
+						
+					case N_MEASUREMENT_ADJUST_FREQ_PLUS:
+					case N_MEASUREMENT_ADJUST_FREQ_MINUS:
+						// adjust DDS frequency to keep beatnote within the bandpass filter
+						
+						if (settling > 0) {
+							settling--;
+							break;
 						}
 						
-						if (Measuring_2==TRUE)
-							{
-								if (Step1_2==FALSE) {
-									SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
-									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
-									
-								    GetCtrlVal(MainPanel, PANEL_DDS2, &FrequencyDDSBesInit) ;
-									GetCtrlVal(MainPanel, PANEL_DDS3, &FrequencyDDS3Init) ;
-									t1_2=utc;
-									Frequ_slope_2=Math1;
-									Beat_slope_2=Ch2;
-									Moy_slope_2= Frequ_slope_2;
-									Moy_Beatslope_2= Beat_slope_2;
-									N_slope_2=1;
-									Step1_2=TRUE ;
-									Nu1=(250000000+Math1)*N1;
-									}
-								else {
-									if(Step2_2==FALSE){
-										N_slope_2=N_slope_2+1; 
-										Frequ_slope_2=Math1;
-										Beat_slope_2=Ch2;  
-										Moy_slope_2=((N_slope_2-1)*Moy_slope_2 + Frequ_slope_2)/N_slope_2;
-										Moy_Beatslope_2=((N_slope_2-1)*Moy_Beatslope_2 + Beat_slope_2)/N_slope_2;
-										Slope_slope_2 = (Slope_slope_2*(N_slope_2-2) + 6*(Frequ_slope_2-Moy_slope_2)/N_slope_2)/(N_slope_2+1) ;
-										Slope_Beatslope_2 = (Slope_Beatslope_2*(N_slope_2-2) + 6*(Beat_slope_2-Moy_Beatslope_2)/N_slope_2)/(N_slope_2+1) ;
-										if (utc-t1_2>SlopeTime2) {
-											Slope_2 = Slope_slope_2;
-											Beatslope_2 = Slope_Beatslope_2;
-											Step2_2=TRUE ;
-											N_slope_2=0;
-											Frequ_slope_2=0.0;
-											Moy_slope_2=0.0;
-											Slope_slope_2 =0.0;
-											Moy_Beatslope_2=0.0;
-											Slope_Beatslope_2 =0.0;
-											Beat_slope_2=0.0;
-											
-											DDS4xAD9912_FrequencyRampe (&DDS4xAD9912,1, FrequDDS1,(FrequDDS1+DeltakHz_2*1000), Step2/Ndiv ) ;
-											SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1+DeltakHz_2*1000)) ;   
-											DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1+DeltakHz_2*1000));
-											Delay(0.1); 
-											DeltaDDS3 = -DeltakHz_2*1000*(-Signe1/Signe2)*Ndiv*(Nu2)/(Nu1) - Beatslope_2*(utc-t1_2);
-											SetCtrlVal(MainPanel, PANEL_DDS3, (FrequencyDDS3Init+DeltaDDS3)) ; 
-											DDS4xAD9912_SetFrequency (&DDS4xAD9912,3,(FrequencyDDS3Init+DeltaDDS3)) ;
-											
-											}
-										}
-									else {
-									  if (DDSBesChanged1==FALSE){
-										  
-										  		if (nDDSChange<3)
-												{ nDDSChange=nDDSChange+1;}
-												
-												else
-												{
-												nDDSChange=0;
-													
-												double DeltaFrep275 = 275000-Ch4;
-												double DeltaFrep10 = 10000-Ch2;
-												DeltaDDS3 = DeltaDDS3 + DeltaFrep10;
-												FrequencyDDSBes = FrequencyDDSBesInit + DeltaFrep275;
-												FrequencyDDS3 = FrequencyDDS3Init + DeltaDDS3;
-												
-												SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBes) ;
-												DDS4xAD9912_SetFrequency(&DDS4xAD9912,2,FrequencyDDSBes);
-												
-												Delay(0.1); 
-												SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3) ;    
-												DDS4xAD9912_SetFrequency(&DDS4xAD9912,3,FrequencyDDS3);
-												 
-												DDSBesChanged1=TRUE;
-												t2_2=utc;
+						double fDDS2;
+						GetCtrlVal(MainPanel, PANEL_DDS2, &fDDS2);
+						fDDS2 += 275000 - Ch4;
+						SetCtrlVal(MainPanel, PANEL_DDS2, fDDS2);
+						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, fDDS2);
+						
+						// allow counter to settle
+						settling = 3;
+						
+						// next step
+						Measuring_1 += 1;
+						break;								
+						
+					case N_MEASUREMENT_FREP_PLUS:
+						// frep positive step
+						
+						if (settling > 0) {
+							settling--;
+							break;
+						}
+						
+						if (t2_1 == 0.0)
+							t2_1 = utc;
+						
+						Frepplus_1 = Frepplus_1 + Math1 - Slope_1 * (utc - t2_1);
+						Ch4Plus = Ch4Plus + Ch4 - Ch4Slope * (utc - t2_1);
+						n_1 += 1;
+						
+						if ((utc - t2_1) > DeltaT_1) {
+							Frepplus_1 = Frepplus_1 / n_1;
+							Ch4Plus = Ch4Plus / n_1; 
+							n_1 = 0;
+							
+							// frep negative step
+							DDS4xAD9912_FrequencyRampe(&DDS4xAD9912,1, (FrequDDS1+DeltakHz_1*1000),(FrequDDS1-DeltakHz_1*1000), Step1/Ndiv);
+							SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1-DeltakHz_1*1000));  
+							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1-DeltakHz_1*1000));
+							
+							// allow counter to settle
+							settling = 3;
+							
+							// next step
+							Measuring_1 += 1;
+						}
+						break;
+						
+					case N_MEASUREMENT_FREP_MINUS:
+						// frep negative step
+						
+						if (settling > 0) {
+							settling--;
+							break;
+						}
+						
+						if (t3_1 == 0.0)
+							t3_1 = utc;
+						
+						Frepminus_1 = Frepminus_1 + Math1 - Slope_1 * (utc - t3_1);
+						Ch4Minus = Ch4Minus + Ch4 - Ch4Slope * (utc - t3_1);
+						n_1 += 1;
+						
+						if ((utc - t3_1) > DeltaT_1) {
+							Frepminus_1 = Frepminus_1 / n_1;
+							Ch4Minus = Ch4Minus / n_1;
+							n_1 = 0;
+							
+							// compute N1
+							N_1 = Signe1 * (2*Ndiv * DeltakHz_1 * 1000)/(Frepminus_1 - Frepplus_1 - Slope_1 * (t3_1 - t2_1));
+							SetCtrlVal(CalcN1Panel, CALCN1_N, N_1);
+						
+							t1_1 = 0.0;
+							t2_1 = 0.0;
+							t3_1 = 0.0;
+							Frepminus_1 = 0.0;
+							Frepplus_1 = 0.0;
 							
-												}
-										  	
-									  }
-									   else{
-										  
-										if(Step3_2==FALSE){
-										  if (nstabilization<3)
-										  {nstabilization= nstabilization+1;}
-										  else
-										  {
-											if (utc-t2_2<DeltaT_2) {
-													Frepplus_2=Frepplus_2 +Math1+250000000-Slope_2*(utc-t2_2);
-													Delta10K_Plus= Delta10K_Plus + 10000 - (Ch2 -Beatslope_2*(utc-t2_2));
-													n_2=n_2+1;
-													}
-											else{
-												Frepplus_2=Frepplus_2/n_2;
-												Delta10K_Plus=Delta10K_Plus/n_2;
-												n_2=0;
-												Step3_2=TRUE ;
-												nstabilization=0;
-												DDS4xAD9912_FrequencyRampe ( &DDS4xAD9912,1, (FrequDDS1+DeltakHz_2*1000),(FrequDDS1-DeltakHz_2*1000), Step2/Ndiv ) ;
-												SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1-DeltakHz_2*1000)) ; 
-												DDS4xAD9912_SetFrequency (&DDS4xAD9912,1, (FrequDDS1-DeltakHz_2*1000) ) ;
-												
-												Delay(0.1); 
-												DeltaDDS3 = (FrequencyDDS3Init+DeltakHz_2*1000*(-Signe1/Signe2)*Ndiv*(Nu2)/(Nu1)) - FrequencyDDS3;
-												SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3+DeltaDDS3) ; 
-												DDS4xAD9912_SetFrequency (&DDS4xAD9912,3, FrequencyDDS3+DeltaDDS3 ) ; 
-												
-												}
-										  }
-										}
-										
-										else {
-											if (DDSBesChanged2==FALSE){
-												
-												if (nDDSChange<3)
-												{ nDDSChange=nDDSChange+1;}
-												
-												else
-												{
-												nDDSChange=0;
-										
-												
-												double DeltaFrep275=275000-Ch4;
-												double DeltaFrep10=10000-Ch2;
-												DeltaDDS3 = DeltaDDS3+(DeltaFrep10) ;
-												FrequencyDDSBes=FrequencyDDSBes+(DeltaFrep275) ;
-												FrequencyDDS3=FrequencyDDS3+DeltaDDS3 ; 
-												DDS4xAD9912_SetFrequency(&DDS4xAD9912,2,FrequencyDDSBes);
-												SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBes);
-												
-												Delay(0.1); 
-												SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3) ; 
-												DDS4xAD9912_SetFrequency(&DDS4xAD9912,3,FrequencyDDS3);
-												
-												DDSBesChanged2=TRUE;
-												t3_2=utc;
-
+							// back to nominal frep
+							DDS4xAD9912_FrequencyRampe(&DDS4xAD9912, 1, FrequDDS1-DeltakHz_1*1000,FrequDDS1, Step1/Ndiv );
+							SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1);
+							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
+							SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit);
+							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyDDSBesInit);
+							
+							// done
+							Measuring_1 = N_MEASUREMENT_NONE;
+						}
+						break;
+				}
+				
+				if (Measuring_2==TRUE)
+				{
+					if (Step1_2==FALSE) {
+						SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1);
+						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
 						
-												}
-											}
-											
-											else{
-											  if (nstabilization<3)
-											  { nstabilization=nstabilization+1;}
-											  else
-											  {
-												if (utc-t3_2<DeltaT_2) {
-													Frepminus_2=Frepminus_2 +Math1+250000000-Slope_2*(utc-t3_2);
-													Delta10K_Minus=  Delta10K_Minus +10000 - ( Ch2 -Beatslope_2*(utc-t3_2));
-													n_2=n_2+1;
-													}
-												else{
-													Frepminus_2=Frepminus_2/(n_2);
-													Delta10K_Minus= Delta10K_Minus/n_2;
-													N_2 = (Signe2)*(-DeltaDDS3+Delta10K_Plus-Delta10K_Minus-Beatslope_2*(t3_2-t2_2) )/(Frepminus_2-Frepplus_2-Slope_2*(t3_2-t2_2));
-													n_2=0;
-													Frepminus_2=0.0;
-													Frepplus_2=0.0;
-													Delta10K_Minus=0.0;
-													Delta10K_Plus=0.0;  
-													DDS4xAD9912_FrequencyRampe (&DDS4xAD9912, 1, FrequDDS1-DeltakHz_2*1000,FrequDDS1, Step2/Ndiv ) ;
-													SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
-													DDS4xAD9912_SetFrequency(&DDS4xAD9912,1,FrequDDS1);
-													
-													Delay(0.1); 
-													
-													SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit) ;  
-													DDS4xAD9912_SetFrequency (&DDS4xAD9912, 2, FrequencyDDSBesInit ) ;
-													
-													Delay(0.1); 
-													
-													SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init-Beatslope_2*(utc-t1_2)) ;
-													DDS4xAD9912_SetFrequency (&DDS4xAD9912, 3, FrequencyDDS3Init-Beatslope_2*(utc-t1_2) ) ;
-													
-													Measuring_2=FALSE ;
-													Step1_2=FALSE ;
-													Step2_2=FALSE ;
-													Step3_2=FALSE ;
-													t1_2=0.0;
-													t2_2=0.0;
-													t3_2=0.0;
-													DDSBesChanged1=FALSE;
-													DDSBesChanged2=FALSE;
-													FrequencyDDSBes=0.0;
-													nstabilization=0;
-													
-													}
-											  }
-											}
-											}
-									  }
-										}
-									}
+						GetCtrlVal(MainPanel, PANEL_DDS2, &FrequencyDDSBesInit);
+						GetCtrlVal(MainPanel, PANEL_DDS3, &FrequencyDDS3Init);
+						t1_2=utc;
+						Frequ_slope_2=Math1;
+						Beat_slope_2=Ch2;
+						Moy_slope_2= Frequ_slope_2;
+						Moy_Beatslope_2= Beat_slope_2;
+						N_slope_2=1;
+						Step1_2=TRUE;
+						Nu1=(250000000+Math1)*N1;
+					}
+					else {
+						if(Step2_2==FALSE){
+							N_slope_2=N_slope_2+1; 
+							Frequ_slope_2=Math1;
+							Beat_slope_2=Ch2;  
+							Moy_slope_2=((N_slope_2-1)*Moy_slope_2 + Frequ_slope_2)/N_slope_2;
+							Moy_Beatslope_2=((N_slope_2-1)*Moy_Beatslope_2 + Beat_slope_2)/N_slope_2;
+							Slope_slope_2 = (Slope_slope_2*(N_slope_2-2) + 6*(Frequ_slope_2-Moy_slope_2)/N_slope_2)/(N_slope_2+1);
+							Slope_Beatslope_2 = (Slope_Beatslope_2*(N_slope_2-2) + 6*(Beat_slope_2-Moy_Beatslope_2)/N_slope_2)/(N_slope_2+1);
+							if (utc-t1_2>SlopeTime2) {
+								Slope_2 = Slope_slope_2;
+								Beatslope_2 = Slope_Beatslope_2;
+								Step2_2=TRUE;
+								N_slope_2=0;
+								Frequ_slope_2=0.0;
+								Moy_slope_2=0.0;
+								Slope_slope_2 =0.0;
+								Moy_Beatslope_2=0.0;
+								Slope_Beatslope_2 =0.0;
+								Beat_slope_2=0.0;
+								
+								DDS4xAD9912_FrequencyRampe (&DDS4xAD9912,1, FrequDDS1,(FrequDDS1+DeltakHz_2*1000), Step2/Ndiv );
+								SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1+DeltakHz_2*1000));   
+								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1+DeltakHz_2*1000));
+								Delay(0.1); 
+								DeltaDDS3 = -DeltakHz_2*1000*(-Signe1/Signe2)*Ndiv*(Nu2)/(Nu1) - Beatslope_2*(utc-t1_2);
+								SetCtrlVal(MainPanel, PANEL_DDS3, (FrequencyDDS3Init+DeltaDDS3)); 
+								DDS4xAD9912_SetFrequency (&DDS4xAD9912,3,(FrequencyDDS3Init+DeltaDDS3));
+								
 							}
-						
-						switch (Measuring_3) {
-
-							case N_MEASUREMENT_STEP_0:
-								// not measuring N3
-								break;
-							
-							case N_MEASUREMENT_STEP_1: // init
+						}
+						else {
+							if (DDSBesChanged1==FALSE){
 								
-								SetCtrlVal(MainPanel, PANEL_DDS4, FrequDDS4);
-								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, FrequDDS4);
-								settling = 3;
+								if (nDDSChange<3)
+								{ nDDSChange=nDDSChange+1;}
 								
-								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;
+								else
+								{
+									nDDSChange=0;
+									
+									double DeltaFrep275 = 275000-Ch4;
+									double DeltaFrep10 = 10000-Ch2;
+									DeltaDDS3 = DeltaDDS3 + DeltaFrep10;
+									FrequencyDDSBes = FrequencyDDSBesInit + DeltaFrep275;
+									FrequencyDDS3 = FrequencyDDS3Init + DeltaDDS3;
+									
+									SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBes);
+									DDS4xAD9912_SetFrequency(&DDS4xAD9912,2,FrequencyDDSBes);
+									
+									Delay(0.1); 
+									SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3);    
+									DDS4xAD9912_SetFrequency(&DDS4xAD9912,3,FrequencyDDS3);
+									
+									DDSBesChanged1=TRUE;
+									t2_2=utc;
+									
 								}
 								
-								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 * Ndiv * DeltakHz_3 * 1000;
-									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
+							}
+							else{
 								
-								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 * Ndiv * DeltakHz_3 * 1000;
-									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;
+								if(Step3_2==FALSE){
+									if (nstabilization<3)
+									{nstabilization= nstabilization+1;}
+									else
+									{
+										if (utc-t2_2<DeltaT_2) {
+											Frepplus_2=Frepplus_2 +Math1+250000000-Slope_2*(utc-t2_2);
+											Delta10K_Plus= Delta10K_Plus + 10000 - (Ch2 -Beatslope_2*(utc-t2_2));
+											n_2=n_2+1;
+										}
+										else
+										{
+											Frepplus_2=Frepplus_2/n_2;
+											Delta10K_Plus=Delta10K_Plus/n_2;
+											n_2=0;
+											Step3_2=TRUE;
+											nstabilization=0;
+											DDS4xAD9912_FrequencyRampe ( &DDS4xAD9912,1, (FrequDDS1+DeltakHz_2*1000),(FrequDDS1-DeltakHz_2*1000), Step2/Ndiv );
+											SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1-DeltakHz_2*1000)); 
+											DDS4xAD9912_SetFrequency (&DDS4xAD9912,1, (FrequDDS1-DeltakHz_2*1000) );
+											
+											Delay(0.1); 
+											DeltaDDS3 = (FrequencyDDS3Init+DeltakHz_2*1000*(-Signe1/Signe2)*Ndiv*(Nu2)/(Nu1)) - FrequencyDDS3;
+											SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3+DeltaDDS3); 
+											DDS4xAD9912_SetFrequency (&DDS4xAD9912,3, FrequencyDDS3+DeltaDDS3 ); 
+											
+										}
+									}
 								}
 								
-								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 + (2 * N3/N1 * Ndiv * DeltakHz_3 * 1000));
-									SetCtrlVal(CalcN3Panel, CALCN3_N, 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;
+								else {
+									if (DDSBesChanged2==FALSE){
 										
-									// step 4 done
-									Measuring_3 = N_MEASUREMENT_STEP_0;
+										if (nDDSChange<3) {
+											nDDSChange=nDDSChange+1;
+										}
+										else
+										{
+											nDDSChange=0;
 
-									// back to nominal frep
-									SetCtrlVal(MainPanel, PANEL_DDS4, FrequDDS4);
-									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, FrequDDS4);
-									// back to initial DDS3 frequency
-									SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init);
-									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequencyDDS3Init);
-								}
-								break;
-						}
-						
-						// Calcul du signe de fb
-						
-						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;
-							}
-						}
-						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;
-							}
-						}
-						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, 4, Frequency3);
-								Getsign3 = FALSE;
-							}
-						}
-						
-						// slope cancelling 
-						if (SlopeMeasuring == TRUE)
-						{
-							double currentFreq = 0.0;
-							
-							// select reference
-							switch (slopeReference) {
-								case SLOPE_REFERENCE_MICROWAVE:
-									currentFreq = Math2;
-									break;
-								case SLOPE_REFERENCE_HG_CAVITY:
-									currentFreq = Ch2 * 1542.2 / 1062.5;
-									break;
-							}
-							
-							if  (utc-SlopeMeasuringTimeBegin > TimetoSlope)
-							{
-								Nratio = Nratio + 1;
-								
-								if (Nratio >= 1) {
-									MoyMath2 = MoyMath2 + Moy_Math2slope;
-								}
-								
-								if (invertSlopeSign) {
-									SlopeMath2 = SlopeMath2 - Slope_Math2slope;
-								} else {
-									SlopeMath2 = SlopeMath2 + Slope_Math2slope;
-								}
-								
-								N_Math2slope = 0.0;
-								Math2_slope = 0.0;
-								Moy_Math2slope = 0.0;
-								Slope_Math2slope = 0.0;
-								
-								if (Nratio == 1 && CenterFrequencyCh2ToDetermine == TRUE)
-								{
-									CenterFrequencyCh2 = MoyMath2;
-									CenterFrequencyCh2ToDetermine = FALSE;
-								}
-
-								OnSlopeCancelling = TRUE;
-								
-								if (Nratio == ratio)
-								{
-									if (FrequCorrec == TRUE)
-									{
-										SlopeCorrection = (MoyMath2/ratio-CenterFrequencyCh2)/TimetoSlope;
-										SlopeMath2 = SlopeMath2 + SlopeCorrection;
-									}
-									Nratio = 0;
-									MoyMath2 = 0.0;
-								}
-								
-								FoxFrequ = DDSFox_ReadFreq(&DDS1xAD9956);
-								SetCtrlVal(MainPanel, PANEL_SLOPETOCANCEL, SlopeMath2);
-								DDSFox_Set(&DDS1xAD9956, FoxFrequ, SlopeMath2);
-								
-								nstabilisationSlopeMeasuring = 0;
-								SlopeMeasuringTimeBegin = utc;
-								
-							}
-							else
-							{
-								if (nstabilisationSlopeMeasuring < 5)
-								{
-									nstabilisationSlopeMeasuring = nstabilisationSlopeMeasuring + 1;
-									Math2_slope = currentFreq; 
-								}
-								else
-								{
-									if ((currentFreq-Math2_slope) < limitotakoff && (currentFreq-Math2_slope) > -limitotakoff)
-									{
-										N_Math2slope = N_Math2slope + 1;
-										Math2_slope = currentFreq;
-										Moy_Math2slope = ((N_Math2slope-1)*Moy_Math2slope + Math2_slope)/N_Math2slope;
-										Slope_Math2slope = (Slope_Math2slope*(N_Math2slope-2) + 6*(Math2_slope-Moy_Math2slope)/N_Math2slope)/(N_Math2slope+1) ;
+											double DeltaFrep275=275000-Ch4;
+											double DeltaFrep10=10000-Ch2;
+											DeltaDDS3 = DeltaDDS3+(DeltaFrep10);
+											FrequencyDDSBes=FrequencyDDSBes+(DeltaFrep275);
+											FrequencyDDS3=FrequencyDDS3+DeltaDDS3; 
+											DDS4xAD9912_SetFrequency(&DDS4xAD9912,2,FrequencyDDSBes);
+											SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBes);
+											
+											Delay(0.1); 
+											SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3); 
+											DDS4xAD9912_SetFrequency(&DDS4xAD9912,3,FrequencyDDS3);
+											
+											DDSBesChanged2=TRUE;
+											t3_2=utc;
+										}
 									}
 									else
 									{
-										if (AutoStopSlopeCancellingIfDelock) {
-											// stop slope cancelling if the comb is not locked
-											 
-											double frequency = DEDRIFT_DDS_FREQUENCY;
-											if (KeepFrequ)
-												frequency = DDSFox_ReadFreq(&DDS1xAD9956);
-												
-											if (! KeepSlope) {
-												SlopeMath2 = 0.0;
-												OnSlopeCancelling = FALSE;
+										if (nstabilization<3) {
+											nstabilization=nstabilization+1;
+										}
+										else
+										{
+											if (utc-t3_2<DeltaT_2) {
+												Frepminus_2=Frepminus_2 +Math1+250000000-Slope_2*(utc-t3_2);
+												Delta10K_Minus=  Delta10K_Minus +10000 - ( Ch2 -Beatslope_2*(utc-t3_2));
+												n_2=n_2+1;
 											}
-											
-											SetCtrlVal(MainPanel, PANEL_SLOPETOCANCEL, SlopeMath2);
-											DDSFox_Set(&DDS1xAD9956, frequency, SlopeMath2);
-											
-											SlopeMeasuring = FALSE;
-											N_Math2slope = 0.0;
-											Math2_slope = 0.0;
-											MoyMath2 = 0.0; 
-											Moy_Math2slope = 0.0;
-											Slope_Math2slope = 0.0;
-											CenterFrequencyCh2 = 0.0;
-											CenterFrequencyCh2ToDetermine = TRUE;
-											Nratio = -1;
-											nstabilisationSlopeMeasuring = 0;
-											
-											SetCtrlVal(MainPanel, PANEL_STARTCANCEL, 0);
+											else
+											{
+												Frepminus_2=Frepminus_2/(n_2);
+												Delta10K_Minus= Delta10K_Minus/n_2;
+												N_2 = (Signe2)*(-DeltaDDS3+Delta10K_Plus-Delta10K_Minus-Beatslope_2*(t3_2-t2_2) )/(Frepminus_2-Frepplus_2-Slope_2*(t3_2-t2_2));
+												n_2=0;
+												Frepminus_2=0.0;
+												Frepplus_2=0.0;
+												Delta10K_Minus=0.0;
+												Delta10K_Plus=0.0;  
+												DDS4xAD9912_FrequencyRampe (&DDS4xAD9912, 1, FrequDDS1-DeltakHz_2*1000,FrequDDS1, Step2/Ndiv );
+												SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1);
+												DDS4xAD9912_SetFrequency(&DDS4xAD9912,1,FrequDDS1);
+												
+												Delay(0.1); 
+												
+												SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit);  
+												DDS4xAD9912_SetFrequency (&DDS4xAD9912, 2, FrequencyDDSBesInit );
+												
+												Delay(0.1); 
+												
+												SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init-Beatslope_2*(utc-t1_2));
+												DDS4xAD9912_SetFrequency (&DDS4xAD9912, 3, FrequencyDDS3Init-Beatslope_2*(utc-t1_2) );
+												
+												Measuring_2=FALSE;
+												Step1_2=FALSE;
+												Step2_2=FALSE;
+												Step3_2=FALSE;
+												t1_2=0.0;
+												t2_2=0.0;
+												t3_2=0.0;
+												DDSBesChanged1=FALSE;
+												DDSBesChanged2=FALSE;
+												FrequencyDDSBes=0.0;
+												nstabilization=0;
+												
+											}
 										}
 									}
 								}
 							}
 						}
+					}
+				}
+				
+				switch (Measuring_3) {
+					
+					case N_MEASUREMENT_STEP_0:
+						// not measuring N3
+						break;
 						
-						// re-centering
-						if (KeepCentering) 
-						{
-							
-							DeltaCh4=275000-Ch4;
-							DeltaCh2=10000-Ch2;
-							
-							if (utc- CenteringTimeBegin275K > Timetorecenter275K && CenteringTimeBegin275K>10)
-							{
-								 GetCtrlVal(MainPanel, PANEL_DDS2, &FrequencyToChange) ;
-								 SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyToChange+DeltaCh4) ;
-					 		   	 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyToChange+DeltaCh4);
-								 CenteringTimeBegin275K=utc;
-								
-							
-							}
-							
-							if (utc- CenteringTimeBegin10K > Timetorecenter10K && CenteringTimeBegin10K>10)
-							{
-								  GetCtrlVal(MainPanel, PANEL_DDS3, &FrequencyToChange) ;
-								  SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyToChange+DeltaCh2) ; 
-					 			  DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequencyToChange+DeltaCh2);
-								  CenteringTimeBegin10K=utc; 
-								
-								
-							}
-							
-							
-						}
-							
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2AUTOSAV, &BoxChecked) ;		   // AutoSave OL
-						if (BoxChecked) {
-							SetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2SAVE, TRUE) ;   // so that it will try to write it (at next block) if it seems reasonnable, even though it was off before
+					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;
 						}
 						
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2SAVE, &BoxChecked) ;			   // Save OL (Math2)
-						if (BoxChecked) {
-							FileOpt = OpenFile("z:\\MeasuresFifi1\\OptCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII) ;
-							Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math2);
-							WriteLine(FileOpt, ReportString, -1) ;
-							CloseFile(FileOpt) ;
-							FileOpt = OpenFile("C:\\Femto\\Results\\OptCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII) ;   // a local backup for debugging
-							Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math2);
-							WriteLine(FileOpt, ReportString, -1) ;
-							CloseFile(FileOpt) ;
+						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 * Ndiv * DeltakHz_3 * 1000;
+							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 * Ndiv * DeltakHz_3 * 1000;
+							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;
 						}
 						
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3AUTOSAV, &BoxChecked) ;		   // AutoSave Hg (Math3)
-						if (BoxChecked) {
-							SetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3SAVE, TRUE) ;   // so that it will try to write it (at next block) if it seems reasonnable, even though it was off before
+						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 + (2 * N3/N1 * Ndiv * DeltakHz_3 * 1000));
+							SetCtrlVal(CalcN3Panel, CALCN3_N, 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);
+							// back to initial DDS3 frequency
+							SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init);
+							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequencyDDS3Init);
+						}
+						break;
+				}
+				
+				// Calcul du signe de fb
+				
+				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;
+					}
+				}
+				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;
+					}
+				}
+				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, 4, Frequency3);
+						Getsign3 = FALSE;
+					}
+				}
+				
+				// slope cancelling 
+				if (SlopeMeasuring == TRUE)
+				{
+					double currentFreq = 0.0;
+					
+					// select reference
+					switch (slopeReference) {
+					case SLOPE_REFERENCE_MICROWAVE:
+						currentFreq = Math2;
+						break;
+					case SLOPE_REFERENCE_HG_CAVITY:
+						currentFreq = Ch2 * 1542.2 / 1062.5;
+						break;
+					}
+					
+					if  (utc-SlopeMeasuringTimeBegin > TimetoSlope)
+					{
+						Nratio = Nratio + 1;
+						
+						if (Nratio >= 1) {
+							MoyMath2 = MoyMath2 + Moy_Math2slope;
 						}
 						
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3SAVE, &BoxChecked) ;			   // Save Hg
-						
-						if (BoxChecked) {
-							FileHg = OpenFile("z:\\MeasuresFifi1\\HgCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII) ;
-							Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math3);
-							WriteLine(FileHg, ReportString, -1) ;
-							CloseFile(FileHg) ;
-							FileHg = OpenFile("C:\\Femto\\Results\\HgCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII) ;
-							Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math3);
-							WriteLine(FileHg, ReportString, -1) ;
-							CloseFile(FileHg) ;
+						if (invertSlopeSign) {
+							SlopeMath2 = SlopeMath2 - Slope_Math2slope;
+						} else {
+							SlopeMath2 = SlopeMath2 + Slope_Math2slope;
 						}
 						
-						GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5SAVE, &BoxChecked) ;				// Save ExtraMath (Math5)
-						if (BoxChecked) {
-							FileExtraMath = OpenFile(ExtraMathFileName, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII) ;
-							Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p7]", Date, Time, utc, Math5);
-							WriteLine(FileExtraMath, ReportString, -1) ;
-							CloseFile(FileExtraMath) ;
+						N_Math2slope = 0.0;
+						Math2_slope = 0.0;
+						Moy_Math2slope = 0.0;
+						Slope_Math2slope = 0.0;
+						
+						if (Nratio == 1 && CenterFrequencyCh2ToDetermine == TRUE)
+						{
+							CenterFrequencyCh2 = MoyMath2;
+							CenterFrequencyCh2ToDetermine = FALSE;
+						}
+						
+						OnSlopeCancelling = TRUE;
+						
+						if (Nratio == ratio)
+						{
+							if (FrequCorrec == TRUE)
+							{
+								SlopeCorrection = (MoyMath2/ratio-CenterFrequencyCh2)/TimetoSlope;
+								SlopeMath2 = SlopeMath2 + SlopeCorrection;
+							}
+							Nratio = 0;
+							MoyMath2 = 0.0;
 						}
 						
-						// Special case to handle change of day at next second
-						if ( LocalTime.tm_hour==23 && LocalTime.tm_min==59 && strtod(Sec,NULL)>=58 ) { 
-							Acquiring = FALSE ;
-							do {
-								Delay(5.1);
-								CurrentFileName(LogFileName) ;
-							} while (!GetFileInfo(LogFileName, &OldLogFilePtr));
-							Acquiring = TRUE ;
-							OldLogFilePtr = 2;
-						}
+						FoxFrequ = DDSFox_ReadFreq(&DDS1xAD9956);
+						SetCtrlVal(MainPanel, PANEL_SLOPETOCANCEL, SlopeMath2);
+						DDSFox_Set(&DDS1xAD9956, FoxFrequ, SlopeMath2);
+						
+						nstabilisationSlopeMeasuring = 0;
+						SlopeMeasuringTimeBegin = utc;
 						
-						ResumeTimerCallbacks() ; 
-						
-						} ;
+					}
+					else
+					{
+						if (nstabilisationSlopeMeasuring < 5)
+						{
+							nstabilisationSlopeMeasuring = nstabilisationSlopeMeasuring + 1;
+							Math2_slope = currentFreq; 
+						}
+						else
+						{
+							if ((currentFreq-Math2_slope) < limitotakoff && (currentFreq-Math2_slope) > -limitotakoff)
+							{
+								N_Math2slope = N_Math2slope + 1;
+								Math2_slope = currentFreq;
+								Moy_Math2slope = ((N_Math2slope-1)*Moy_Math2slope + Math2_slope)/N_Math2slope;
+								Slope_Math2slope = (Slope_Math2slope*(N_Math2slope-2) + 6*(Math2_slope-Moy_Math2slope)/N_Math2slope)/(N_Math2slope+1);
+							}
+							else
+							{
+								if (AutoStopSlopeCancellingIfDelock) {
+									// stop slope cancelling if the comb is not locked
+									
+									double frequency = DEDRIFT_DDS_FREQUENCY;
+									if (KeepFrequ)
+										frequency = DDSFox_ReadFreq(&DDS1xAD9956);
+									
+									if (! KeepSlope) {
+										SlopeMath2 = 0.0;
+										OnSlopeCancelling = FALSE;
+									}
+									
+									SetCtrlVal(MainPanel, PANEL_SLOPETOCANCEL, SlopeMath2);
+									DDSFox_Set(&DDS1xAD9956, frequency, SlopeMath2);
+									
+									SlopeMeasuring = FALSE;
+									N_Math2slope = 0.0;
+									Math2_slope = 0.0;
+									MoyMath2 = 0.0; 
+									Moy_Math2slope = 0.0;
+									Slope_Math2slope = 0.0;
+									CenterFrequencyCh2 = 0.0;
+									CenterFrequencyCh2ToDetermine = TRUE;
+									Nratio = -1;
+									nstabilisationSlopeMeasuring = 0;
+									
+									SetCtrlVal(MainPanel, PANEL_STARTCANCEL, 0);
+								}
+							}
+						}
+					}
+				}
+				
+				// re-centering
+				if (KeepCentering) 
+				{
+					
+					DeltaCh4=275000-Ch4;
+					DeltaCh2=10000-Ch2;
+					
+					if (utc- CenteringTimeBegin275K > Timetorecenter275K && CenteringTimeBegin275K>10)
+					{
+						GetCtrlVal(MainPanel, PANEL_DDS2, &FrequencyToChange);
+						SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyToChange+DeltaCh4);
+						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyToChange+DeltaCh4);
+						CenteringTimeBegin275K=utc;
+					}
 					
-					break;
-				case FALSE:
-					break;
+					if (utc- CenteringTimeBegin10K > Timetorecenter10K && CenteringTimeBegin10K>10)
+							{
+								GetCtrlVal(MainPanel, PANEL_DDS3, &FrequencyToChange);
+								SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyToChange+DeltaCh2); 
+								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequencyToChange+DeltaCh2);
+								CenteringTimeBegin10K=utc; 
+							}
+				}
+				
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2AUTOSAV, &BoxChecked);		   // AutoSave OL
+				if (BoxChecked) {
+					SetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2SAVE, TRUE);   // so that it will try to write it (at next block) if it seems reasonnable, even though it was off before
+				}
+				
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2SAVE, &BoxChecked);			   // Save OL (Math2)
+				if (BoxChecked) {
+					FileOpt = OpenFile("z:\\MeasuresFifi1\\OptCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
+					Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math2);
+					WriteLine(FileOpt, ReportString, -1);
+					CloseFile(FileOpt);
+					FileOpt = OpenFile("C:\\Femto\\Results\\OptCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);   // a local backup for debugging
+					Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math2);
+					WriteLine(FileOpt, ReportString, -1);
+					CloseFile(FileOpt);
+				}
+				
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3AUTOSAV, &BoxChecked);		   // AutoSave Hg (Math3)
+				if (BoxChecked) {
+					SetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3SAVE, TRUE);   // so that it will try to write it (at next block) if it seems reasonnable, even though it was off before
 				}
+				
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3SAVE, &BoxChecked);			   // Save Hg
+				
+				if (BoxChecked) {
+					FileHg = OpenFile("z:\\MeasuresFifi1\\HgCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
+					Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math3);
+					WriteLine(FileHg, ReportString, -1);
+					CloseFile(FileHg);
+					FileHg = OpenFile("C:\\Femto\\Results\\HgCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
+					Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math3);
+					WriteLine(FileHg, ReportString, -1);
+					CloseFile(FileHg);
+				}
+				
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5SAVE, &BoxChecked);				// Save ExtraMath (Math5)
+				if (BoxChecked) {
+					FileExtraMath = OpenFile(ExtraMathFileName, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
+					Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p7]", Date, Time, utc, Math5);
+					WriteLine(FileExtraMath, ReportString, -1);
+					CloseFile(FileExtraMath);
+				}
+				
+				// Special case to handle change of day at next second
+				if ( LocalTime.tm_hour==23 && LocalTime.tm_min==59 && strtod(Sec,NULL)>=58 ) { 
+					Acquiring = FALSE;
+					do {
+						Delay(5.1);
+						CurrentFileName(LogFileName);
+					} while (!GetFileInfo(LogFileName, &OldLogFilePtr));
+					Acquiring = TRUE;
+					OldLogFilePtr = 2;
+				}
+				
+				ResumeTimerCallbacks(); 
+				
+			}		
 			break;
-		}
+	}
 	return 0;
 }
 
 
-
-
 int CVICALLBACK CB_OnFreqPlot (int panel, int control, int event,
 		void *callbackData, int eventData1, int eventData2)
 {