Mercurial > hg > fxanalyse
diff Plot.c @ 135:77539f2597b1
Code cleanup
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Wed, 22 Jan 2014 12:29:38 +0100 |
parents | bd28161e5ac2 |
children | be87c8e78266 |
line wrap: on
line diff
--- 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 <ansi_c.h> #include <userint.h> #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 (YMin<YMax) { - SetAxisScalingMode(panel, PLOTPANEL_FREQPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, YMin, YMax) ; - SetCtrlVal(panel, PLOTPANEL_CHECKBOX_AUTOSCALE, FALSE) ; - } ; - break; - case EVENT_RIGHT_CLICK: - - break; - } - return 0; -} - -int CVICALLBACK Plot_CB_ChangeMin (int panel, int control, int event, - void *callbackData, int eventData1, int eventData2) -{ - double YMin, YMax ; + double ymin, ymax; switch (event) - { + { case EVENT_COMMIT: - GetCtrlVal(panel, PLOTPANEL_MIN, &YMin) ; - GetCtrlVal(panel, PLOTPANEL_MAX, &YMax) ; - if (YMin<YMax ) { - SetAxisScalingMode(panel, PLOTPANEL_FREQPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, YMin, YMax) ; - SetCtrlVal(panel, PLOTPANEL_CHECKBOX_AUTOSCALE, FALSE) ; - } ; + GetCtrlVal(panel, PLOTPANEL_MIN, &ymin); + GetCtrlVal(panel, PLOTPANEL_MAX, &ymax); + /* adjust control limits */ + SetCtrlAttribute(panel, PLOTPANEL_MIN, ATTR_MAX_VALUE, ymax); + SetCtrlAttribute(panel, PLOTPANEL_MAX, ATTR_MIN_VALUE, ymin); + /* disable autoscaling */ + SetCtrlVal(panel, PLOTPANEL_CHECKBOX_AUTOSCALE, FALSE); + SetAxisScalingMode(panel, PLOTPANEL_FREQPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, ymin, ymax); break; - case EVENT_RIGHT_CLICK: - - break; - } + } return 0; } @@ -136,126 +177,63 @@ int CVICALLBACK Plot_CB_ChangeAutoScale (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { - bool AutoScale = FALSE; - int NotCare = 20000000 ; - double YMin = 10000000 , YMax = 64000000 ; + int autoscale = FALSE; + double ymin, ymax; switch (event) - { + { case EVENT_COMMIT: - GetCtrlVal(panel, PLOTPANEL_CHECKBOX_AUTOSCALE, &AutoScale) ; - if (AutoScale) { - SetAxisScalingMode(panel, PLOTPANEL_FREQPLOT, VAL_LEFT_YAXIS, VAL_AUTOSCALE, YMin, YMax) ; - GetAxisScalingMode(panel, PLOTPANEL_FREQPLOT,VAL_LEFT_YAXIS, &NotCare, &YMin, &YMax ) ; - SetCtrlVal(panel, PLOTPANEL_MIN, YMin); - SetCtrlVal(panel, PLOTPANEL_MAX, YMax); - } - else { - GetCtrlVal(panel, PLOTPANEL_MIN, &YMin); - GetCtrlVal(panel, PLOTPANEL_MAX, &YMax); - SetAxisScalingMode(panel, PLOTPANEL_FREQPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, YMin, YMax) ; - } ; + GetCtrlVal(panel, PLOTPANEL_CHECKBOX_AUTOSCALE, &autoscale); + if (autoscale) { + SetAxisScalingMode(panel, PLOTPANEL_FREQPLOT, VAL_LEFT_YAXIS, VAL_AUTOSCALE, 0, 0); + GetAxisScalingMode(panel, PLOTPANEL_FREQPLOT,VAL_LEFT_YAXIS, NULL, &ymin, &ymax); + SetCtrlVal(panel, PLOTPANEL_MIN, ymin); + SetCtrlVal(panel, PLOTPANEL_MAX, ymax); + /* adjust control limits */ + SetCtrlAttribute(panel, PLOTPANEL_MIN, ATTR_MAX_VALUE, ymax); + SetCtrlAttribute(panel, PLOTPANEL_MAX, ATTR_MIN_VALUE, ymin); + } else { + GetCtrlVal(panel, PLOTPANEL_MIN, &ymin); + GetCtrlVal(panel, PLOTPANEL_MAX, &ymax); + SetAxisScalingMode(panel, PLOTPANEL_FREQPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, ymin, ymax); + } break; - case EVENT_RIGHT_CLICK: - - break; - } + } return 0; } + int CVICALLBACK Plot_CB_ChangeScalingStep(int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { - double ScalingStep ; + double step; switch (event) - { + { case EVENT_COMMIT: - GetCtrlVal(panel, PLOTPANEL_SCALINGSTEP, &ScalingStep) ; - SetCtrlAttribute(panel, PLOTPANEL_MIN, ATTR_INCR_VALUE, ScalingStep) ; - SetCtrlAttribute(panel, PLOTPANEL_MAX, ATTR_INCR_VALUE, ScalingStep) ; + GetCtrlVal(panel, PLOTPANEL_SCALINGSTEP, &step); + SetCtrlAttribute(panel, PLOTPANEL_MIN, ATTR_INCR_VALUE, step); + SetCtrlAttribute(panel, PLOTPANEL_MAX, ATTR_INCR_VALUE, step); break; - case EVENT_RIGHT_CLICK: - break; - } + } return 0; } -// ******************** Member functions, public ******************************************* - -int Plot_AddFrequency(Plot_Data * Instance, double Freq) { - - double YMin = 10000000, YMax = 64000000, Mean = 0, ADev = 0 , Slope = 0 , Drift = 0 ; - bool AutoScale, DeDrift ; - int N = 0, NotCare ; - - // Retrieve previous values for mean, adev and slope - Mean = Instance->Mean ; - 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; }