# HG changeset patch # User Daniele Nicolodi # Date 1390390178 -3600 # Node ID 77539f2597b1e6d6102a3983c735535b332a8b28 # Parent bd28161e5ac2d0f28d79381d7f0883ef11c78d31 Code cleanup diff -r bd28161e5ac2 -r 77539f2597b1 Allan.c --- a/Allan.c Wed Jan 22 12:29:28 2014 +0100 +++ b/Allan.c Wed Jan 22 12:29:38 2014 +0100 @@ -2,115 +2,175 @@ #include #include "YLCStuff.h" +#include "FXAllan.h" +#include "Allan.h" -#include "FXAllan.h" // Auto generated panel definitions and protypes -#include "Allan.h" // My own .h file, containing prototypes and definitions for the class Allan... - +#define DATAPOINT_COLOR VAL_RED +#define ERRORBAR_COLOR VAL_RED -// ******************* Member functions : constructor and destructor ************************* +static void Allan_Reset(Allan_Data * Instance); +static void Allan_Display(Allan_Data * Instance); + -int Allan_InitPanel(Allan_Data * Instance, const char * title, double Normalizer, int parent, int control) { +void Allan_InitPanel(Allan_Data * Instance, const char *title, double normalization, int parent, int control) +{ + if ((Instance->AllanPanel = LoadPanel (0, "FXAllan.uir", ALLANPANEL)) < 0) + return; - int i ; - - if ((Instance->AllanPanel = LoadPanel (0, "FXAllan.uir", ALLANPANEL)) < 0) - return -1; - SetPanelAttribute(Instance->AllanPanel, ATTR_TITLE, title) ; - SetPanelAttribute (Instance->AllanPanel, ATTR_CALLBACK_DATA, (void *)Instance); // the panel callback therefore knows which data structure it is associated to + Allan_Reset(Instance); + Instance->normalization = normalization; + Instance->autoscale = FALSE; Instance->active = TRUE; Instance->parent = parent; Instance->control = control; - for (i=0 ; iAllanVar[i] = 0 ; - Instance->LastMean[i] = 0 ; - Instance->BlocksNumber[i] = 0 ; - Instance->CurrentAverage[i] = 0 ; - Instance->NbCurrentAverage[i] = 0 ; - } ; - Instance->Drift = 0 ; + + SetPanelAttribute(Instance->AllanPanel, ATTR_TITLE, title); + SetPanelAttribute (Instance->AllanPanel, ATTR_CALLBACK_DATA, (void *)Instance); DisplayPanel (Instance->AllanPanel); - SetCtrlVal(Instance->AllanPanel, ALLANPANEL_NORMALIZER, Normalizer) ; - return 0 ; - } + SetCtrlVal(Instance->AllanPanel, ALLANPANEL_NORMALIZER, normalization); +} + + +void Allan_ClosePanel(Allan_Data * Instance) +{ + Instance->active = FALSE; + SetCtrlVal(Instance->parent, Instance->control, FALSE); + DiscardPanel (Instance->AllanPanel); +} +void Allan_AddFrequency(Allan_Data * Instance, double Freq) +{ + /* total number of points used. used to calculate the drift rate */ + int N = 1 + Instance->BlocksNumber[0]; + double Mean = Instance->Mean; + double Drift = Instance->Drift; + double Normalizer = Instance->normalization; + + /* compute drift rate */ + if (N > 1) { + Instance->Drift = (Drift * (N - 2) + 6 * (Freq - Mean) / N) / (N + 1); + SetCtrlVal(Instance->AllanPanel, ALLANPANEL_DRIFT, Instance->Drift/Normalizer); + } + Instance->Mean = (Mean * (N - 1) + Freq) / N; + + /* compute allan deviation */ + for (int i = 0; i < ALLAN_MAXPOINTSNUMBER; i++) { + Instance->CurrentAverage[i] = (Instance->CurrentAverage[i]*Instance->NbCurrentAverage[i] + Freq) + /(Instance->NbCurrentAverage[i]+1); + Instance->NbCurrentAverage[i] +=1; + + if (Instance->NbCurrentAverage[i] >= pow(2,i) ) { + double CurrentMean = Instance->CurrentAverage[i]; + double LastMean = Instance->LastMean[i]; + N = Instance->BlocksNumber[i]; + N++; + Instance->CurrentAverage[i] = 0; + Instance->NbCurrentAverage[i] = 0; + if (N > 1) { + Instance->AllanVar[i] = (Instance->AllanVar[i]*(N-2) + +0.5*pow(CurrentMean-LastMean,2))/(N-1) ; + } else { // ie if N=1, which is realized for the first completed block + Instance->FirstMean[i] = CurrentMean; + } + Instance->LastMean[i] = CurrentMean; + Instance->BlocksNumber[i] = N; + } + } + + Allan_Display(Instance); +} -int Allan_ClosePanel(Allan_Data * Instance) { - Instance->active = FALSE; - SetCtrlVal(Instance->parent, Instance->control, FALSE); - DiscardPanel (Instance->AllanPanel); - return 0; - } + +/* private */ + +static void Allan_Reset(Allan_Data *Instance) +{ + memset(Instance->AllanVar, 0, sizeof(Instance->AllanVar)); + memset(Instance->FirstMean, 0, sizeof(Instance->FirstMean)); + memset(Instance->LastMean, 0, sizeof(Instance->LastMean)); + memset(Instance->BlocksNumber, 0, sizeof(Instance->BlocksNumber)); + memset(Instance->CurrentAverage, 0, sizeof(Instance->CurrentAverage)); + memset(Instance->NbCurrentAverage, 0, sizeof(Instance->NbCurrentAverage)); + Instance->Drift = 0; + Instance->Mean = 0; +} +static void Allan_Display(Allan_Data *Instance) +{ + int i, N; + double X[ALLAN_MAXPOINTSNUMBER]; + double Y[ALLAN_MAXPOINTSNUMBER]; + double Normalizer = Instance->normalization; + double DriftTau, FirstFreq, LastFreq, Error; + int dedrift; + + GetCtrlVal(Instance->AllanPanel, ALLANPANEL_DEDRIFT, &dedrift); + + for (i = 0; i < ALLAN_MAXPOINTSNUMBER; i++) { + X[i] = pow(2, i); + if (dedrift) { + DriftTau = Instance->Drift * X[i]; + N = Instance->BlocksNumber[i]; + if (N>=2) { // if enough data to calculate AVAR for tau = 2^i*tau0, and therefore, AllanVar[i] is meaningful + FirstFreq = Instance->FirstMean[i]; + LastFreq = Instance->LastMean[i]; + Y[i] = sqrt(Instance->AllanVar[i]+DriftTau*(DriftTau/2-(LastFreq-FirstFreq)/(N-1)))/Normalizer; + // Not totaly sure it works... to be checked thoroughly + } + else { + Y[i] = 0; + } + } else { + Y[i] = sqrt(Instance->AllanVar[i])/Normalizer; + } + } + + DeleteGraphPlot(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, -1, VAL_IMMEDIATE_DRAW); + PlotXY(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, X, Y, ALLAN_MAXPOINTSNUMBER, + VAL_DOUBLE, VAL_DOUBLE, VAL_SCATTER, VAL_SOLID_SQUARE, VAL_SOLID, 1, DATAPOINT_COLOR); + + for (i = 0; i < ALLAN_MAXPOINTSNUMBER; i++) { + Error = 1/sqrt(Instance->BlocksNumber[i]); + PlotLine(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, X[i], Y[i]*(1-Error), X[i], Y[i]*(1+Error), ERRORBAR_COLOR); + } +} -// ******************** Member functions, callbacks (private) ******************************* +/* callbacks */ int CVICALLBACK Allan_CB_Reset(int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { - Allan_Data * Instance = NULL; - - switch(event) { - + switch(event) + { case EVENT_COMMIT: - GetPanelAttribute (panel, ATTR_CALLBACK_DATA, &Instance); - Allan_Reset(Instance) ; + Allan_Data *data; + GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); + Allan_Reset(data); break; - - case EVENT_RIGHT_CLICK: - - break; - } - + } return 0; } -int CVICALLBACK Allan_CB_ChangeMax (int panel, int control, int event, + +int CVICALLBACK Allan_CB_ChangeYLim (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) -{ - int YMin, YMax ; - +{ switch (event) - { + { case EVENT_COMMIT: - GetCtrlVal(panel, ALLANPANEL_MIN, &YMin) ; - GetCtrlVal(panel, ALLANPANEL_MAX, &YMax) ; - if (YMinnormalization)); + Allan_Display(data); break; - case EVENT_RIGHT_CLICK: - - break; - } */ + } return 0; } - -// ******************** Member functions, public ***************************************** - -int Allan_Reset(Allan_Data * Instance) { - - int i ; - - for (i=0 ; iAllanVar[i] = 0 ; - Instance->LastMean[i] = 0 ; - Instance->BlocksNumber[i] = 0 ; - Instance->CurrentAverage[i] = 0 ; - Instance->NbCurrentAverage[i] = 0 ; - } ; - - return 0 ; - -} - - -int Allan_AddFrequency(Allan_Data * Instance, double Freq) { - - int i, N; - double CurrentMean, LastMean, Drift, Mean, Normalizer ; - - N = Instance->BlocksNumber[0]+1 ; // the total number of points used, usefull to calculate drift rate - Mean = Instance->Mean ; - Drift = Instance->Drift ; - - GetCtrlVal(Instance->AllanPanel, ALLANPANEL_NORMALIZER, &Normalizer); - - if (N > 1) { // Drift measure needs at least 2 values ! - Instance->Drift = (Drift*(N-2) + 6*(Freq-Mean)/N)/(N+1) ; - SetCtrlVal(Instance->AllanPanel, ALLANPANEL_DRIFT, Instance->Drift/Normalizer ) ; +int CVICALLBACK Allan_CB_ChangeNormalization (int panel, int control, int event, + void *callbackData, int eventData1, int eventData2) +{ + switch (event) + { + case EVENT_COMMIT: + Allan_Data *data; + GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); + GetCtrlVal(panel, control, &(data->normalization)); + Allan_Display(data); + break; } - - // update mean value (usefull to calculate drift rate with linear regression incremental algorithm...) - Instance->Mean = (Mean*(N-1)+Freq)/N ; - - - for (i=0 ; iCurrentAverage[i] = (Instance->CurrentAverage[i]*Instance->NbCurrentAverage[i] + Freq) - /(Instance->NbCurrentAverage[i]+1) ; - Instance->NbCurrentAverage[i] +=1 ; - - if (Instance->NbCurrentAverage[i] >= pow(2,i) ) { + return 0; +} - CurrentMean = Instance->CurrentAverage[i]; - LastMean = Instance->LastMean[i] ; - N = Instance->BlocksNumber[i] ; - N++ ; - Instance->CurrentAverage[i] = 0 ; - Instance->NbCurrentAverage[i] = 0 ; - if (N>1) { - Instance->AllanVar[i] = (Instance->AllanVar[i]*(N-2) - +0.5*pow(CurrentMean-LastMean,2))/(N-1) ; - } - else { // ie if N=1, which is realized for the first completed block - Instance->FirstMean[i] = CurrentMean ; - } ; - Instance->LastMean[i] = CurrentMean ; - Instance->BlocksNumber[i] = N ; - } ; - } ; - - Allan_Display(Instance, VAL_WHITE, VAL_BLUE) ; - - return 0 ; -} - -int Allan_Display (Allan_Data * Instance, int ColorPt, int ColorErrorBar){ - - int PointsNumber = ALLAN_MAXPOINTSNUMBER ; - int i, N ; - double X[ALLAN_MAXPOINTSNUMBER] ; - double Y[ALLAN_MAXPOINTSNUMBER] ; - double Normalizer, DriftTau, FirstFreq, LastFreq, Error ; - bool DeDrift ; - - GetCtrlVal(Instance->AllanPanel, ALLANPANEL_NORMALIZER, &Normalizer) ; - GetCtrlVal(Instance->AllanPanel, ALLANPANEL_DEDRIFT, &DeDrift) ; - - for (i=0 ; iAllanVar[i])/Normalizer ;} - else { - DriftTau = Instance->Drift*X[i] ; - N = Instance->BlocksNumber[i] ; - if (N>=2) { // if enough data to calculate AVAR for tau = 2^i*tau0, and therefore, AllanVar[i] is meaningful - FirstFreq = Instance->FirstMean[i] ; - LastFreq = Instance->LastMean[i] ; - Y[i] = sqrt(Instance->AllanVar[i]+DriftTau*(DriftTau/2-(LastFreq-FirstFreq)/(N-1)))/Normalizer ; - // Not totaly sure it works... to be checked thoroughly - } - else { Y[i] = 0 ; } ; - } ; - } ; - - DeleteGraphPlot(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, -1, VAL_IMMEDIATE_DRAW) ; - PlotXY (Instance->AllanPanel, ALLANPANEL_ALLANPLOT, X, Y, PointsNumber, - VAL_DOUBLE, VAL_DOUBLE, VAL_SCATTER, VAL_SOLID_SQUARE, VAL_SOLID, 1, ColorPt); - - for (i=0 ; iBlocksNumber[i]) ; - PlotLine (Instance->AllanPanel, ALLANPANEL_ALLANPLOT, X[i], Y[i]*(1-Error), X[i], Y[i]*(1+Error), ColorErrorBar); - } ; - - - return 0 ; -} - -int Allan_DisplayErrorBars (Allan_Data * Instance, int color){ - - int i; - double X[ALLAN_MAXPOINTSNUMBER] ; - double Val, Error, Normalizer; - - GetCtrlVal(Instance->AllanPanel, ALLANPANEL_NORMALIZER, &Normalizer) ; - for (i=0 ; iAllanVar[i])/Normalizer ; - Error = 1/sqrt(Instance->BlocksNumber[i]) ; - PlotLine (Instance->AllanPanel, ALLANPANEL_ALLANPLOT, X[i], Val*(1-Error), X[i], Val*(1+Error), color); - } ; - - return 0 ; -} int CVICALLBACK CB_GeneralAllanPanel (int panel, int event, void *callbackData, int eventData1, int eventData2) { - Allan_Data * Instance = NULL; - - switch (event) - { - case EVENT_GOT_FOCUS: - + { + case EVENT_CLOSE: + Allan_Data *data; + GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); + Allan_ClosePanel(data); break; - case EVENT_LOST_FOCUS: - - break; - case EVENT_CLOSE: - GetPanelAttribute (panel, ATTR_CALLBACK_DATA, &Instance); - Allan_ClosePanel(Instance) ; - - break; - } + } return 0; } diff -r bd28161e5ac2 -r 77539f2597b1 Allan.h --- a/Allan.h Wed Jan 22 12:29:28 2014 +0100 +++ b/Allan.h Wed Jan 22 12:29:38 2014 +0100 @@ -14,6 +14,8 @@ double CurrentAverage[ALLAN_MAXPOINTSNUMBER] ; // the sum of the last p data points retrieved (with p<2^2n). used for averaging as soon as p=2^n int NbCurrentAverage[ALLAN_MAXPOINTSNUMBER] ; // number of data point being summed in CurrentAverage (from 0 up to 2^n) double Drift, Mean ; // the drift rate of the data (in Hz/s), and the mean value (in Hz) : only usefull for dedrifting... + double normalization; + int autoscale; int active; int parent; int control; @@ -22,11 +24,7 @@ // The functions of crypto class Allan -int Allan_InitPanel(Allan_Data * Instance, const char * title, double Normalizer, int parent, int control) ; -int Allan_ClosePanel(Allan_Data * Instance) ; -int Allan_AddFrequency(Allan_Data * Instance, double Freq) ; +void Allan_InitPanel(Allan_Data * Instance, const char * title, double Normalizer, int parent, int control) ; +void Allan_ClosePanel(Allan_Data * Instance) ; +void Allan_AddFrequency(Allan_Data * Instance, double Freq) ; -int Allan_Reset(Allan_Data * Instance) ; -int Allan_AddFrequency(Allan_Data * Instance, double Freq); -int Allan_Display(Allan_Data * Instance, int ColorPt, int ColorErrorBar); -int Allan_DisplayErrorBars(Allan_Data * Instance, int color); diff -r bd28161e5ac2 -r 77539f2597b1 FXAllan.h --- a/FXAllan.h Wed Jan 22 12:29:28 2014 +0100 +++ b/FXAllan.h Wed Jan 22 12:29:38 2014 +0100 @@ -1,6 +1,6 @@ /**************************************************************************/ /* LabWindows/CVI User Interface Resource (UIR) Include File */ -/* Copyright (c) National Instruments 2010. All Rights Reserved. */ +/* Copyright (c) National Instruments 2014. All Rights Reserved. */ /* */ /* WARNING: Do not add to, delete from, or otherwise modify the contents */ /* of this include file. */ @@ -15,14 +15,19 @@ /* Panels and Controls: */ #define ALLANPANEL 1 /* callback function: CB_GeneralAllanPanel */ -#define ALLANPANEL_NORMALIZER 2 /* callback function: Allan_CB_ChangeNormalizer */ -#define ALLANPANEL_ALLANPLOT 3 -#define ALLANPANEL_MAX 4 /* callback function: Allan_CB_ChangeMax */ -#define ALLANPANEL_MIN 5 /* callback function: Allan_CB_ChangeMin */ -#define ALLANPANEL_CHECKBOX_AUTOSCALE 6 /* callback function: Allan_CB_ChangeAutoScale */ -#define ALLANPANEL_RESETBUTTON 7 /* callback function: Allan_CB_Reset */ -#define ALLANPANEL_DRIFT 8 -#define ALLANPANEL_DEDRIFT 9 +#define ALLANPANEL_NORMALIZER 2 /* control type: numeric, callback function: Allan_CB_ChangeNormalization */ +#define ALLANPANEL_ALLANPLOT 3 /* control type: graph, callback function: (none) */ +#define ALLANPANEL_MAX 4 /* control type: numeric, callback function: Allan_CB_ChangeYLim */ +#define ALLANPANEL_MIN 5 /* control type: numeric, callback function: Allan_CB_ChangeYLim */ +#define ALLANPANEL_CHECKBOX_AUTOSCALE 6 /* control type: radioButton, callback function: Allan_CB_ChangeAutoScale */ +#define ALLANPANEL_RESETBUTTON 7 /* control type: command, callback function: Allan_CB_Reset */ +#define ALLANPANEL_DRIFT 8 /* control type: numeric, callback function: (none) */ +#define ALLANPANEL_DEDRIFT 9 /* control type: radioButton, callback function: (none) */ + + + /* Control Arrays: */ + + /* (no control arrays in the resource file) */ /* Menu Bars, Menus, and Menu Items: */ @@ -30,12 +35,11 @@ /* (no menu bars in the resource file) */ - /* Callback Prototypes: */ + /* Callback Prototypes: */ int CVICALLBACK Allan_CB_ChangeAutoScale(int panel, int control, int event, void *callbackData, int eventData1, int eventData2); -int CVICALLBACK Allan_CB_ChangeMax(int panel, int control, int event, void *callbackData, int eventData1, int eventData2); -int CVICALLBACK Allan_CB_ChangeMin(int panel, int control, int event, void *callbackData, int eventData1, int eventData2); -int CVICALLBACK Allan_CB_ChangeNormalizer(int panel, int control, int event, void *callbackData, int eventData1, int eventData2); +int CVICALLBACK Allan_CB_ChangeNormalization(int panel, int control, int event, void *callbackData, int eventData1, int eventData2); +int CVICALLBACK Allan_CB_ChangeYLim(int panel, int control, int event, void *callbackData, int eventData1, int eventData2); int CVICALLBACK Allan_CB_Reset(int panel, int control, int event, void *callbackData, int eventData1, int eventData2); int CVICALLBACK CB_GeneralAllanPanel(int panel, int event, void *callbackData, int eventData1, int eventData2); diff -r bd28161e5ac2 -r 77539f2597b1 FXAllan.uir Binary file FXAllan.uir has changed diff -r bd28161e5ac2 -r 77539f2597b1 FXAnalyse.c --- a/FXAnalyse.c Wed Jan 22 12:29:28 2014 +0100 +++ b/FXAnalyse.c Wed Jan 22 12:29:38 2014 +0100 @@ -49,7 +49,7 @@ double utc; double Ch1, Ch2, Ch3, Ch4; double Math1, Math2, Math3, Math4, Math5; -double N1, N2, N3, N4; +double N1, N2, N3; // panels @@ -538,11 +538,6 @@ { case EVENT_COMMIT: QuitUserInterface(0); - mupRelease(MathParser1); - mupRelease(MathParser2); - mupRelease(MathParser3); - mupRelease(MathParser4); - mupRelease(MathParser5); break; } return 0; diff -r bd28161e5ac2 -r 77539f2597b1 FXAnalyse.uir Binary file FXAnalyse.uir has changed diff -r bd28161e5ac2 -r 77539f2597b1 FXPlot.h --- a/FXPlot.h Wed Jan 22 12:29:28 2014 +0100 +++ b/FXPlot.h Wed Jan 22 12:29:38 2014 +0100 @@ -1,6 +1,6 @@ /**************************************************************************/ /* LabWindows/CVI User Interface Resource (UIR) Include File */ -/* Copyright (c) National Instruments 2011. All Rights Reserved. */ +/* Copyright (c) National Instruments 2014. All Rights Reserved. */ /* */ /* WARNING: Do not add to, delete from, or otherwise modify the contents */ /* of this include file. */ @@ -15,23 +15,23 @@ /* Panels and Controls: */ #define PLOTPANEL 1 /* callback function: CB_PlotEvent */ -#define PLOTPANEL_MAX 2 /* callback function: Plot_CB_ChangeMax */ -#define PLOTPANEL_MIN 3 /* callback function: Plot_CB_ChangeMin */ -#define PLOTPANEL_TEXTMSG_3 4 -#define PLOTPANEL_TEXTMSG_4 5 -#define PLOTPANEL_CHECKBOX_AUTOSCALE 6 /* callback function: Plot_CB_ChangeAutoScale */ -#define PLOTPANEL_RESETBUTTON 7 /* callback function: Plot_CB_Reset */ -#define PLOTPANEL_TEXTMSG_6 8 -#define PLOTPANEL_FREQPLOT 9 -#define PLOTPANEL_ADEV 10 -#define PLOTPANEL_SLOPE 11 -#define PLOTPANEL_MEAN 12 -#define PLOTPANEL_TEXTMSG 13 -#define PLOTPANEL_SCALINGSTEP 14 /* callback function: Plot_CB_ChangeScalingStep */ -#define PLOTPANEL_TEXTMSG_8 15 -#define PLOTPANEL_DEDRIFT 16 /* callback function: Plot_CB_ChangeDrift */ -#define PLOTPANEL_GETSLOPEBUTTON 17 /* callback function: Plot_CB_GetDrift */ -#define PLOTPANEL_CHECKBOX_DEDRIFT 18 /* callback function: Plot_CB_Reset */ +#define PLOTPANEL_MAX 2 /* control type: numeric, callback function: Plot_CB_ChangeYLim */ +#define PLOTPANEL_MIN 3 /* control type: numeric, callback function: Plot_CB_ChangeYLim */ +#define PLOTPANEL_CHECKBOX_AUTOSCALE 4 /* control type: radioButton, callback function: Plot_CB_ChangeAutoScale */ +#define PLOTPANEL_RESETBUTTON 5 /* control type: command, callback function: Plot_CB_Reset */ +#define PLOTPANEL_FREQPLOT 6 /* control type: graph, callback function: (none) */ +#define PLOTPANEL_ADEV 7 /* control type: numeric, callback function: (none) */ +#define PLOTPANEL_SLOPE 8 /* control type: numeric, callback function: (none) */ +#define PLOTPANEL_MEAN 9 /* control type: numeric, callback function: (none) */ +#define PLOTPANEL_SCALINGSTEP 10 /* control type: ring, callback function: Plot_CB_ChangeScalingStep */ +#define PLOTPANEL_DEDRIFT 11 /* control type: numeric, callback function: Plot_CB_ChangeDrift */ +#define PLOTPANEL_GETSLOPEBUTTON 12 /* control type: command, callback function: Plot_CB_GetDrift */ +#define PLOTPANEL_CHECKBOX_DEDRIFT 13 /* control type: radioButton, callback function: Plot_CB_Reset */ + + + /* Control Arrays: */ + + /* (no control arrays in the resource file) */ /* Menu Bars, Menus, and Menu Items: */ @@ -39,14 +39,13 @@ /* (no menu bars in the resource file) */ - /* Callback Prototypes: */ + /* Callback Prototypes: */ int CVICALLBACK CB_PlotEvent(int panel, int event, void *callbackData, int eventData1, int eventData2); int CVICALLBACK Plot_CB_ChangeAutoScale(int panel, int control, int event, void *callbackData, int eventData1, int eventData2); int CVICALLBACK Plot_CB_ChangeDrift(int panel, int control, int event, void *callbackData, int eventData1, int eventData2); -int CVICALLBACK Plot_CB_ChangeMax(int panel, int control, int event, void *callbackData, int eventData1, int eventData2); -int CVICALLBACK Plot_CB_ChangeMin(int panel, int control, int event, void *callbackData, int eventData1, int eventData2); int CVICALLBACK Plot_CB_ChangeScalingStep(int panel, int control, int event, void *callbackData, int eventData1, int eventData2); +int CVICALLBACK Plot_CB_ChangeYLim(int panel, int control, int event, void *callbackData, int eventData1, int eventData2); int CVICALLBACK Plot_CB_GetDrift(int panel, int control, int event, void *callbackData, int eventData1, int eventData2); int CVICALLBACK Plot_CB_Reset(int panel, int control, int event, void *callbackData, int eventData1, int eventData2); diff -r bd28161e5ac2 -r 77539f2597b1 FXPlot.uir Binary file FXPlot.uir has changed diff -r bd28161e5ac2 -r 77539f2597b1 Plot.c --- a/Plot.c Wed Jan 22 12:29:28 2014 +0100 +++ b/Plot.c Wed Jan 22 12:29:38 2014 +0100 @@ -1,134 +1,175 @@ - #include #include #include "YLCStuff.h" -#include "FXAnalyse.h" - -#include "FXPlot.h" // Auto generated panel definitions and protypes -#include "Plot.h" // My own .h file, containing prototypes and definitions for the class Plot... - -// ******************* Member functions : constructor and destructor ********************** +#include "FXPlot.h" +#include "Plot.h" -int Plot_InitPanel(Plot_Data * Instance, const char * title, double PlotMin, double PlotMax, int parent, int control) { - +void Plot_InitPanel(Plot_Data *Instance, const char *title, double ymin, double ymax, int parent, int control) +{ if ((Instance->PlotPanel = LoadPanel (0, "FXPlot.uir", PLOTPANEL)) < 0) - return -1; - SetPanelAttribute(Instance->PlotPanel, ATTR_TITLE, title) ; - SetPanelAttribute (Instance->PlotPanel, ATTR_CALLBACK_DATA, (void *)Instance); // the panel callback therefore knows which data structure it is associated to + return; + + SetPanelAttribute(Instance->PlotPanel, ATTR_TITLE, title); + SetPanelAttribute(Instance->PlotPanel, ATTR_CALLBACK_DATA, (void *)Instance); Instance->active = TRUE; Instance->parent = parent; Instance->control = control; - Instance->IndexPoint = 0 ; - Instance->Mean = 0 ; + Instance->IndexPoint = 0; + Instance->Mean = 0; Instance->Slope = 0; - Instance->ADev = 0 ; - Instance->Frequencies = calloc(MAXPOINTSNUMBER, sizeof(double)) ; - DisplayPanel (Instance->PlotPanel); - SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MIN, PlotMin) ; - SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MAX, PlotMax) ; - SetAxisScalingMode(Instance->PlotPanel, PLOTPANEL_FREQPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, PlotMin, PlotMax) ; - return 0 ; + Instance->ADev = 0; + Instance->Frequencies = calloc(MAXPOINTSNUMBER, sizeof(double)); + DisplayPanel(Instance->PlotPanel); + + SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MIN, ymin); + SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MAX, ymax); + + if ((ymin != 0.0) && (ymax != 0.0)) { + /* manual scaling */ + SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MIN, ymin); + SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MAX, ymax); + SetAxisScalingMode(Instance->PlotPanel, PLOTPANEL_FREQPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, ymin, ymax); + } else { + /* auto scaling */ + SetCtrlVal(Instance->PlotPanel, PLOTPANEL_CHECKBOX_AUTOSCALE, TRUE); + SetAxisScalingMode(Instance->PlotPanel, PLOTPANEL_FREQPLOT, VAL_LEFT_YAXIS, VAL_AUTOSCALE, 0, 0); } +} - -int Plot_ClosePanel(Plot_Data * Instance) { +void Plot_ClosePanel(Plot_Data * Instance) +{ Instance->active = FALSE; free(Instance->Frequencies); SetCtrlVal(Instance->parent, Instance->control, FALSE); - DiscardPanel (Instance->PlotPanel); - return 0; - } - - + DiscardPanel(Instance->PlotPanel); +} + -// ******************** Member functions, callbacks (private) ******************************* +void Plot_AddFrequency(Plot_Data * Instance, double Freq) +{ + double Drift = 0; + int DeDrift; + int N = 0; -int CVICALLBACK CB_PlotEvent(int panel, int event, void *callbackData, int eventData1, int eventData2) { + double Mean = Instance->Mean; + double Slope = Instance->Slope; + double ADev = Instance->ADev; + + /* Correct Freq with drift (feed forward) if dedrift is on */ + GetCtrlVal(Instance->PlotPanel, PLOTPANEL_CHECKBOX_DEDRIFT, &DeDrift); + if (DeDrift) { + GetCtrlVal(Instance->PlotPanel, PLOTPANEL_DEDRIFT, &Drift); + Freq -= ((double) Instance->IndexPoint)*Drift; + } + + /* Add Freq to graph plot */ + Instance->Frequencies[Instance->IndexPoint++] = Freq; + N = Instance->IndexPoint; + + if (N > 1) { + /* adev and slope computation need at least 2 data points */ + Instance->Slope = (Slope*(N-2) + 6*(Freq-Mean)/N)/(N+1); + SetCtrlVal(Instance->PlotPanel, PLOTPANEL_SLOPE, Instance->Slope); + Instance->ADev = sqrt( ( ADev*ADev*(N-2) + 0.5*pow(Freq-Instance->Frequencies[N-2],2) ) / (N-1)); + SetCtrlVal(Instance->PlotPanel, PLOTPANEL_ADEV, Instance->ADev); + } + Instance->Mean = (Mean*(N-1)+Freq)/N; + SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MEAN, Instance->Mean); - int VirtualKeyCode; - int StepIndex; - double Step; + /* if too many points recorded restart */ + if (N > MAXPOINTSNUMBER - 2) { + Instance->IndexPoint = 0; + Instance->Mean = 0; + Instance->Slope = 0; + Instance->ADev = 0; + SetCtrlVal(Instance->PlotPanel,PLOTPANEL_MEAN, 0.0); + SetCtrlVal(Instance->PlotPanel,PLOTPANEL_SLOPE, 0.0); + SetCtrlVal(Instance->PlotPanel,PLOTPANEL_ADEV, 0.0); + } + + DeleteGraphPlot(Instance->PlotPanel, PLOTPANEL_FREQPLOT, -1, VAL_IMMEDIATE_DRAW); + PlotY(Instance->PlotPanel, PLOTPANEL_FREQPLOT, Instance->Frequencies, Instance->IndexPoint, + VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_BLUE); - Plot_Data * Instance = NULL; - + int autoscale; + GetCtrlVal(Instance->PlotPanel, PLOTPANEL_CHECKBOX_AUTOSCALE, &autoscale); + if (autoscale) { + /* update plot limits */ + double ymin, ymax; + GetAxisScalingMode(Instance->PlotPanel, PLOTPANEL_FREQPLOT,VAL_LEFT_YAXIS, NULL, &ymin, &ymax); + SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MIN, ymin); + SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MAX, ymax); + /* adjust control limits */ + SetCtrlAttribute(Instance->PlotPanel, PLOTPANEL_MIN, ATTR_MAX_VALUE, ymax); + SetCtrlAttribute(Instance->PlotPanel, PLOTPANEL_MAX, ATTR_MIN_VALUE, ymin); + } +} + + +/* callbacks */ + +int CVICALLBACK CB_PlotEvent(int panel, int event, + void *callbackData, int eventData1, int eventData2) +{ switch (event) { - case EVENT_CLOSE: - GetPanelAttribute (panel, ATTR_CALLBACK_DATA, &Instance); - Plot_ClosePanel(Instance) ; - break; + case EVENT_CLOSE: + Plot_Data *data; + GetPanelAttribute (panel, ATTR_CALLBACK_DATA, &data); + Plot_ClosePanel(data); + break; - case EVENT_KEYPRESS: - VirtualKeyCode = GetKeyPressEventVirtualKey(eventData2); - switch (VirtualKeyCode) - { - case 2304: //ie right arrow - GetCtrlIndex(panel, PLOTPANEL_SCALINGSTEP, &StepIndex); - if (StepIndex<10){ - SetCtrlIndex(panel, PLOTPANEL_SCALINGSTEP, ++StepIndex) ; - GetCtrlVal(panel, PLOTPANEL_SCALINGSTEP, &Step); - SetCtrlAttribute(panel, PLOTPANEL_MIN, ATTR_INCR_VALUE, Step) ; - SetCtrlAttribute(panel, PLOTPANEL_MAX, ATTR_INCR_VALUE, Step) ; - }; + case EVENT_KEYPRESS: + int keycode = GetKeyPressEventVirtualKey(eventData2); + int index; + double step; + switch (keycode) + { + case 2304: /* right arrow */ + GetCtrlIndex(panel, PLOTPANEL_SCALINGSTEP, &index); + if (index < 10) { + SetCtrlIndex(panel, PLOTPANEL_SCALINGSTEP, ++index); + GetCtrlVal(panel, PLOTPANEL_SCALINGSTEP, &step); + SetCtrlAttribute(panel, PLOTPANEL_MIN, ATTR_INCR_VALUE, step); + SetCtrlAttribute(panel, PLOTPANEL_MAX, ATTR_INCR_VALUE, step); + } + break; + case 2048: /* left arrow */ + GetCtrlIndex(panel, PLOTPANEL_SCALINGSTEP, &index); + if (index > 0) { + SetCtrlIndex(panel, PLOTPANEL_SCALINGSTEP, --index); + GetCtrlVal(panel, PLOTPANEL_SCALINGSTEP, &step); + SetCtrlAttribute(panel, PLOTPANEL_MIN, ATTR_INCR_VALUE, step); + SetCtrlAttribute(panel, PLOTPANEL_MAX, ATTR_INCR_VALUE, step); + } + break; + } break; - case 2048: //ie left arrow - GetCtrlIndex(panel, PLOTPANEL_SCALINGSTEP, &StepIndex); - if (StepIndex>0){ - SetCtrlIndex(panel, PLOTPANEL_SCALINGSTEP, --StepIndex) ; - GetCtrlVal(panel, PLOTPANEL_SCALINGSTEP, &Step); - SetCtrlAttribute(panel, PLOTPANEL_MIN, ATTR_INCR_VALUE, Step) ; - SetCtrlAttribute(panel, PLOTPANEL_MAX, ATTR_INCR_VALUE, Step) ; - }; - break; - }; - break; } return 0; } -int CVICALLBACK Plot_CB_ChangeMax (int panel, int control, int event, + +int CVICALLBACK Plot_CB_ChangeYLim (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { - double YMin, YMax ; - - switch (event) - { - case EVENT_COMMIT: - GetCtrlVal(panel, PLOTPANEL_MIN, &YMin) ; - GetCtrlVal(panel, PLOTPANEL_MAX, &YMax) ; - if (YMinMean ; - Slope = Instance->Slope; - ADev = Instance->ADev; - - // Correct Freq with drift (feed forward) if dedrift is on - GetCtrlVal(Instance->PlotPanel, PLOTPANEL_CHECKBOX_DEDRIFT, &DeDrift) ; - if(DeDrift) { - GetCtrlVal(Instance->PlotPanel, PLOTPANEL_DEDRIFT, &Drift) ; - Freq -= ((double) Instance->IndexPoint)*Drift ; - } ; - - // Add Freq to graph plot - Instance->Frequencies[Instance->IndexPoint++] = Freq ; - N = Instance->IndexPoint ; // N is now the new number of points in the graph - - - if (N > 1) { // ADEV and SLOPE need at least 2 values ! - Instance->Slope = (Slope*(N-2) + 6*(Freq-Mean)/N)/(N+1) ; - SetCtrlVal(Instance->PlotPanel, PLOTPANEL_SLOPE, Instance->Slope ) ; - Instance->ADev = sqrt( ( ADev*ADev*(N-2) + 0.5*pow(Freq-Instance->Frequencies[N-2],2) ) / (N-1) ) ; - SetCtrlVal(Instance->PlotPanel, PLOTPANEL_ADEV, Instance->ADev ) ; - } ; - Instance->Mean = (Mean*(N-1)+Freq)/N ; - SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MEAN, Instance->Mean); - - if (N > MAXPOINTSNUMBER - 2) { // If too many points recorded, restart new plot with new values of Mean, Slope and Adev - Instance->IndexPoint = 0; - Instance->Mean = 0; - Instance->Slope = 0; - Instance->ADev = 0; - SetCtrlVal(Instance->PlotPanel,PLOTPANEL_MEAN, 0.) ; - SetCtrlVal(Instance->PlotPanel,PLOTPANEL_SLOPE, 0.) ; - SetCtrlVal(Instance->PlotPanel,PLOTPANEL_ADEV, 0.) ; - } ; - - DeleteGraphPlot(Instance->PlotPanel, PLOTPANEL_FREQPLOT, -1,VAL_IMMEDIATE_DRAW) ; - PlotY(Instance->PlotPanel, PLOTPANEL_FREQPLOT, Instance->Frequencies, Instance->IndexPoint, VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_BLUE) ; - GetCtrlVal(Instance->PlotPanel, PLOTPANEL_CHECKBOX_AUTOSCALE, &AutoScale) ; - if (AutoScale) { - GetAxisScalingMode(Instance->PlotPanel, PLOTPANEL_FREQPLOT,VAL_LEFT_YAXIS, &NotCare, &YMin, &YMax ) ; - SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MIN, YMin); - SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MAX, YMax); - } ; - return 0 ; - } - - - - int CVICALLBACK Plot_CB_Reset (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { - Plot_Data * Instance = NULL; - + Plot_Data *data; switch (event) - { + { case EVENT_COMMIT: - GetPanelAttribute (panel, ATTR_CALLBACK_DATA, &Instance); - Instance->IndexPoint = 0 ; - Instance->Mean = 0 ; - Instance->Slope = 0; - Instance->ADev = 0 ; - + GetPanelAttribute (panel, ATTR_CALLBACK_DATA, &data); + data->IndexPoint = 0; + data->Mean = 0; + data->Slope = 0; + data->ADev = 0; break; - } + } return 0; } @@ -263,63 +241,50 @@ int CVICALLBACK Plot_CB_GetDrift (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { - Plot_Data * Instance = NULL; - bool DeDrift ; - double Drift ; + Plot_Data *data; + int dedrift; + double drift; switch (event) - { + { case EVENT_COMMIT: - - //Instance = ThisPlot(panel) ; - GetPanelAttribute (panel, ATTR_CALLBACK_DATA, &Instance); - GetCtrlVal(panel, PLOTPANEL_CHECKBOX_DEDRIFT, &DeDrift) ; - - if (!DeDrift) { - SetCtrlVal(panel,PLOTPANEL_DEDRIFT,Instance->Slope) ; - } - else { - GetCtrlVal(panel, PLOTPANEL_DEDRIFT, &Drift) ; - Drift += Instance->Slope ; - SetCtrlVal(panel, PLOTPANEL_DEDRIFT, Drift) ; - - Instance->IndexPoint = 0 ; - Instance->Mean = 0 ; - Instance->Slope = 0 ; - Instance->ADev = 0 ; - - } ; - - + GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); + GetCtrlVal(panel, PLOTPANEL_CHECKBOX_DEDRIFT, &dedrift); + if (dedrift) { + GetCtrlVal(panel, PLOTPANEL_DEDRIFT, &drift); + drift += data->Slope; + SetCtrlVal(panel, PLOTPANEL_DEDRIFT, drift); + data->IndexPoint = 0; + data->Mean = 0; + data->Slope = 0; + data->ADev = 0; + } else + SetCtrlVal(panel,PLOTPANEL_DEDRIFT, data->Slope); break; - } + } return 0; } + int CVICALLBACK Plot_CB_ChangeDrift (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { - bool DeDrift ; - Plot_Data * Instance = NULL; + int dedrift; + Plot_Data *data; switch (event) - { + { case EVENT_COMMIT: - GetCtrlVal(panel, PLOTPANEL_CHECKBOX_DEDRIFT, &DeDrift) ; - if(DeDrift) { - - //Instance = ThisPlot(panel) ; - GetPanelAttribute (panel, ATTR_CALLBACK_DATA, &Instance); - - Instance->IndexPoint = 0 ; - Instance->Mean = 0 ; - Instance->Slope = 0 ; - Instance->ADev = 0 ; - - } ; - + GetCtrlVal(panel, PLOTPANEL_CHECKBOX_DEDRIFT, &dedrift); + if (dedrift) { + GetPanelAttribute (panel, ATTR_CALLBACK_DATA, &data); + data->IndexPoint = 0; + data->Mean = 0; + data->Slope = 0; + data->ADev = 0; + } break; - } + } return 0; } diff -r bd28161e5ac2 -r 77539f2597b1 Plot.h --- a/Plot.h Wed Jan 22 12:29:28 2014 +0100 +++ b/Plot.h Wed Jan 22 12:29:38 2014 +0100 @@ -1,20 +1,22 @@ -#include "YLCStuff.h" +#ifndef __FXANALIZE_PLOT_H__ +#define __FXANALIZE_PLOT_H__ #define MAXPOINTSNUMBER 10000 typedef struct { - PanelHandle PlotPanel ; // le handle sur le graphe - int IndexPoint ; // le nombre de points à plotter - double * Frequencies ; // les valeurs à plotter (alloué dynamiquement par le constructeur...) - double Mean, Slope, ADev ; // les valeurs de moyenne, pente et deviation d'Allan calculés au fur et à mesure - int active; - int parent; - int control; - } Plot_Data ; + int PlotPanel; /* display panel handle */ + int IndexPoint; /* data points number */ + double *Frequencies; /* data points */ + double Mean; /* data computed at each value insertion */ + double Slope; + double ADev; + int active; /* active */ + int parent; /* panel containig the activation control */ + int control; /* activation control handle */ +} Plot_Data; -int Plot_InitPanel(Plot_Data * Instance, const char * title, double PlotMin, double PlotMax, int parent, int control); -int Plot_ClosePanel(Plot_Data * Instance) ; +void Plot_InitPanel(Plot_Data * Instance, const char *title, double ymin, double ymax, int parent, int control); +void Plot_ClosePanel(Plot_Data * Instance); +void Plot_AddFrequency(Plot_Data * Instance, double Freq); -int Plot_AddFrequency(Plot_Data * Instance, double Freq) ; - - +#endif