Mercurial > hg > fxanalyse
comparison Plot.c @ 0:d9aae7d7f2c6
Import
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Tue, 03 Jul 2012 10:38:59 +0200 |
parents | |
children | 381da7b34cd8 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d9aae7d7f2c6 |
---|---|
1 | |
2 #include <ansi_c.h> | |
3 #include <userint.h> | |
4 | |
5 #include "YLCStuff.h" | |
6 #include "FXAnalyse.h" | |
7 | |
8 #include "FXPlot.h" // Auto generated panel definitions and protypes | |
9 #include "Plot.h" // My own .h file, containing prototypes and definitions for the class Plot... | |
10 | |
11 | |
12 | |
13 // ******************* Member functions : constructor and destructor ********************** | |
14 | |
15 int Plot_InitPanel(Plot_Data * Instance, char * title, double PlotMin, double PlotMax, void (*OnCloseFunc)(int) ) { | |
16 | |
17 if ((Instance->PlotPanel = LoadPanel (0, "FXPlot.uir", PLOTPANEL)) < 0) | |
18 return -1; | |
19 SetPanelAttribute(Instance->PlotPanel, ATTR_TITLE, title) ; | |
20 SetPanelAttribute (Instance->PlotPanel, ATTR_CALLBACK_DATA, (void *)Instance); // the panel callback therefore knows which data structure it is associated to | |
21 Instance->OnClosePanel = OnCloseFunc ; | |
22 Instance->IndexPoint = 0 ; | |
23 Instance->Mean = 0 ; | |
24 Instance->Slope = 0; | |
25 Instance->ADev = 0 ; | |
26 Instance->Frequencies = calloc(MAXPOINTSNUMBER, sizeof(double)) ; | |
27 DisplayPanel (Instance->PlotPanel); | |
28 SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MIN, PlotMin) ; | |
29 SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MAX, PlotMax) ; | |
30 SetAxisScalingMode(Instance->PlotPanel, PLOTPANEL_FREQPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, PlotMin, PlotMax) ; | |
31 return 0 ; | |
32 } | |
33 | |
34 | |
35 | |
36 int Plot_ClosePanel(Plot_Data * Instance) { | |
37 free(Instance->Frequencies) ; | |
38 Instance->OnClosePanel(Instance->PlotPanel) ; | |
39 DiscardPanel (Instance->PlotPanel); | |
40 return 0; | |
41 } | |
42 | |
43 | |
44 | |
45 // ******************** Member functions, callbacks (private) ******************************* | |
46 | |
47 int CVICALLBACK CB_PlotEvent(int panel, int event, void *callbackData, int eventData1, int eventData2) { | |
48 | |
49 int VirtualKeyCode ; | |
50 int StepIndex ; | |
51 double Step ; | |
52 | |
53 Plot_Data * Instance = NULL; | |
54 void (*truc)(void); | |
55 | |
56 switch (event) | |
57 { | |
58 case EVENT_CLOSE: | |
59 GetPanelAttribute (panel, ATTR_CALLBACK_DATA, &Instance); | |
60 Plot_ClosePanel(Instance) ; | |
61 break; | |
62 | |
63 case EVENT_KEYPRESS: | |
64 VirtualKeyCode = GetKeyPressEventVirtualKey(eventData2); | |
65 switch (VirtualKeyCode) | |
66 { | |
67 case 2304: //ie right arrow | |
68 GetCtrlIndex(panel, PLOTPANEL_SCALINGSTEP, &StepIndex); | |
69 if (StepIndex<10){ | |
70 SetCtrlIndex(panel, PLOTPANEL_SCALINGSTEP, ++StepIndex) ; | |
71 GetCtrlVal(panel, PLOTPANEL_SCALINGSTEP, &Step); | |
72 SetCtrlAttribute(panel, PLOTPANEL_MIN, ATTR_INCR_VALUE, Step) ; | |
73 SetCtrlAttribute(panel, PLOTPANEL_MAX, ATTR_INCR_VALUE, Step) ; | |
74 }; | |
75 break; | |
76 case 2048: //ie left arrow | |
77 GetCtrlIndex(panel, PLOTPANEL_SCALINGSTEP, &StepIndex); | |
78 if (StepIndex>0){ | |
79 SetCtrlIndex(panel, PLOTPANEL_SCALINGSTEP, --StepIndex) ; | |
80 GetCtrlVal(panel, PLOTPANEL_SCALINGSTEP, &Step); | |
81 SetCtrlAttribute(panel, PLOTPANEL_MIN, ATTR_INCR_VALUE, Step) ; | |
82 SetCtrlAttribute(panel, PLOTPANEL_MAX, ATTR_INCR_VALUE, Step) ; | |
83 }; | |
84 break; | |
85 }; | |
86 break; | |
87 } | |
88 return 0; | |
89 } | |
90 | |
91 int CVICALLBACK Plot_CB_ChangeMax (int panel, int control, int event, | |
92 void *callbackData, int eventData1, int eventData2) | |
93 { | |
94 double YMin, YMax ; | |
95 | |
96 switch (event) | |
97 { | |
98 case EVENT_COMMIT: | |
99 GetCtrlVal(panel, PLOTPANEL_MIN, &YMin) ; | |
100 GetCtrlVal(panel, PLOTPANEL_MAX, &YMax) ; | |
101 if (YMin<YMax) { | |
102 SetAxisScalingMode(panel, PLOTPANEL_FREQPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, YMin, YMax) ; | |
103 SetCtrlVal(panel, PLOTPANEL_CHECKBOX_AUTOSCALE, FALSE) ; | |
104 } ; | |
105 break; | |
106 case EVENT_RIGHT_CLICK: | |
107 | |
108 break; | |
109 } | |
110 return 0; | |
111 } | |
112 | |
113 int CVICALLBACK Plot_CB_ChangeMin (int panel, int control, int event, | |
114 void *callbackData, int eventData1, int eventData2) | |
115 { | |
116 double YMin, YMax ; | |
117 | |
118 switch (event) | |
119 { | |
120 case EVENT_COMMIT: | |
121 GetCtrlVal(panel, PLOTPANEL_MIN, &YMin) ; | |
122 GetCtrlVal(panel, PLOTPANEL_MAX, &YMax) ; | |
123 if (YMin<YMax ) { | |
124 SetAxisScalingMode(panel, PLOTPANEL_FREQPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, YMin, YMax) ; | |
125 SetCtrlVal(panel, PLOTPANEL_CHECKBOX_AUTOSCALE, FALSE) ; | |
126 } ; | |
127 break; | |
128 case EVENT_RIGHT_CLICK: | |
129 | |
130 break; | |
131 } | |
132 return 0; | |
133 } | |
134 | |
135 | |
136 int CVICALLBACK Plot_CB_ChangeAutoScale (int panel, int control, int event, | |
137 void *callbackData, int eventData1, int eventData2) | |
138 { | |
139 bool AutoScale = FALSE; | |
140 int NotCare = 20000000 ; | |
141 double YMin = 10000000 , YMax = 64000000 ; | |
142 | |
143 switch (event) | |
144 { | |
145 case EVENT_COMMIT: | |
146 GetCtrlVal(panel, PLOTPANEL_CHECKBOX_AUTOSCALE, &AutoScale) ; | |
147 if (AutoScale) { | |
148 SetAxisScalingMode(panel, PLOTPANEL_FREQPLOT, VAL_LEFT_YAXIS, VAL_AUTOSCALE, YMin, YMax) ; | |
149 GetAxisScalingMode(panel, PLOTPANEL_FREQPLOT,VAL_LEFT_YAXIS, &NotCare, &YMin, &YMax ) ; | |
150 SetCtrlVal(panel, PLOTPANEL_MIN, YMin); | |
151 SetCtrlVal(panel, PLOTPANEL_MAX, YMax); | |
152 } | |
153 else { | |
154 GetCtrlVal(panel, PLOTPANEL_MIN, &YMin); | |
155 GetCtrlVal(panel, PLOTPANEL_MAX, &YMax); | |
156 SetAxisScalingMode(panel, PLOTPANEL_FREQPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, YMin, YMax) ; | |
157 } ; | |
158 break; | |
159 case EVENT_RIGHT_CLICK: | |
160 | |
161 break; | |
162 } | |
163 return 0; | |
164 } | |
165 | |
166 int CVICALLBACK Plot_CB_ChangeScalingStep(int panel, int control, int event, | |
167 void *callbackData, int eventData1, int eventData2) | |
168 { | |
169 double ScalingStep ; | |
170 | |
171 switch (event) | |
172 { | |
173 case EVENT_COMMIT: | |
174 GetCtrlVal(panel, PLOTPANEL_SCALINGSTEP, &ScalingStep) ; | |
175 SetCtrlAttribute(panel, PLOTPANEL_MIN, ATTR_INCR_VALUE, ScalingStep) ; | |
176 SetCtrlAttribute(panel, PLOTPANEL_MAX, ATTR_INCR_VALUE, ScalingStep) ; | |
177 break; | |
178 case EVENT_RIGHT_CLICK: | |
179 break; | |
180 } | |
181 return 0; | |
182 } | |
183 | |
184 | |
185 // ******************** Member functions, public ******************************************* | |
186 | |
187 int Plot_AddFrequency(Plot_Data * Instance, double Freq) { | |
188 | |
189 double YMin = 10000000, YMax = 64000000, Mean = 0, ADev = 0 , Slope = 0 , Drift = 0 ; | |
190 bool AutoScale, DeDrift ; | |
191 int N = 0, NotCare ; | |
192 | |
193 // Retrieve previous values for mean, adev and slope | |
194 Mean = Instance->Mean ; | |
195 Slope = Instance->Slope; | |
196 ADev = Instance->ADev; | |
197 | |
198 // Correct Freq with drift (feed forward) if dedrift is on | |
199 GetCtrlVal(Instance->PlotPanel, PLOTPANEL_CHECKBOX_DEDRIFT, &DeDrift) ; | |
200 if(DeDrift) { | |
201 GetCtrlVal(Instance->PlotPanel, PLOTPANEL_DEDRIFT, &Drift) ; | |
202 Freq -= ((double) Instance->IndexPoint)*Drift ; | |
203 } ; | |
204 | |
205 // Add Freq to graph plot | |
206 Instance->Frequencies[Instance->IndexPoint++] = Freq ; | |
207 N = Instance->IndexPoint ; // N is now the new number of points in the graph | |
208 | |
209 | |
210 if (N > 1) { // ADEV and SLOPE need at least 2 values ! | |
211 Instance->Slope = (Slope*(N-2) + 6*(Freq-Mean)/N)/(N+1) ; | |
212 SetCtrlVal(Instance->PlotPanel, PLOTPANEL_SLOPE, Instance->Slope ) ; | |
213 Instance->ADev = sqrt( ( ADev*ADev*(N-2) + 0.5*pow(Freq-Instance->Frequencies[N-2],2) ) / (N-1) ) ; | |
214 SetCtrlVal(Instance->PlotPanel, PLOTPANEL_ADEV, Instance->ADev ) ; | |
215 } ; | |
216 Instance->Mean = (Mean*(N-1)+Freq)/N ; | |
217 SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MEAN, Instance->Mean); | |
218 | |
219 if (N > MAXPOINTSNUMBER - 2) { // If too many points recorded, restart new plot with new values of Mean, Slope and Adev | |
220 Instance->IndexPoint = 0; | |
221 Instance->Mean = 0; | |
222 Instance->Slope = 0; | |
223 Instance->ADev = 0; | |
224 SetCtrlVal(Instance->PlotPanel,PLOTPANEL_MEAN, 0.) ; | |
225 SetCtrlVal(Instance->PlotPanel,PLOTPANEL_SLOPE, 0.) ; | |
226 SetCtrlVal(Instance->PlotPanel,PLOTPANEL_ADEV, 0.) ; | |
227 } ; | |
228 | |
229 DeleteGraphPlot(Instance->PlotPanel, PLOTPANEL_FREQPLOT, -1,VAL_IMMEDIATE_DRAW) ; | |
230 PlotY(Instance->PlotPanel, PLOTPANEL_FREQPLOT, Instance->Frequencies, Instance->IndexPoint, VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_BLUE) ; | |
231 GetCtrlVal(Instance->PlotPanel, PLOTPANEL_CHECKBOX_AUTOSCALE, &AutoScale) ; | |
232 if (AutoScale) { | |
233 GetAxisScalingMode(Instance->PlotPanel, PLOTPANEL_FREQPLOT,VAL_LEFT_YAXIS, &NotCare, &YMin, &YMax ) ; | |
234 SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MIN, YMin); | |
235 SetCtrlVal(Instance->PlotPanel, PLOTPANEL_MAX, YMax); | |
236 } ; | |
237 return 0 ; | |
238 } | |
239 | |
240 | |
241 | |
242 | |
243 int CVICALLBACK Plot_CB_Reset (int panel, int control, int event, | |
244 void *callbackData, int eventData1, int eventData2) | |
245 { | |
246 Plot_Data * Instance = NULL; | |
247 | |
248 switch (event) | |
249 { | |
250 case EVENT_COMMIT: | |
251 GetPanelAttribute (panel, ATTR_CALLBACK_DATA, &Instance); | |
252 Instance->IndexPoint = 0 ; | |
253 Instance->Mean = 0 ; | |
254 Instance->Slope = 0; | |
255 Instance->ADev = 0 ; | |
256 | |
257 break; | |
258 } | |
259 return 0; | |
260 } | |
261 | |
262 | |
263 int CVICALLBACK Plot_CB_GetDrift (int panel, int control, int event, | |
264 void *callbackData, int eventData1, int eventData2) | |
265 { | |
266 Plot_Data * Instance = NULL; | |
267 bool DeDrift ; | |
268 double Drift ; | |
269 | |
270 switch (event) | |
271 { | |
272 case EVENT_COMMIT: | |
273 | |
274 //Instance = ThisPlot(panel) ; | |
275 GetPanelAttribute (panel, ATTR_CALLBACK_DATA, &Instance); | |
276 GetCtrlVal(panel, PLOTPANEL_CHECKBOX_DEDRIFT, &DeDrift) ; | |
277 | |
278 if (!DeDrift) { | |
279 SetCtrlVal(panel,PLOTPANEL_DEDRIFT,Instance->Slope) ; | |
280 } | |
281 else { | |
282 GetCtrlVal(panel, PLOTPANEL_DEDRIFT, &Drift) ; | |
283 Drift += Instance->Slope ; | |
284 SetCtrlVal(panel, PLOTPANEL_DEDRIFT, Drift) ; | |
285 | |
286 Instance->IndexPoint = 0 ; | |
287 Instance->Mean = 0 ; | |
288 Instance->Slope = 0 ; | |
289 Instance->ADev = 0 ; | |
290 | |
291 } ; | |
292 | |
293 | |
294 break; | |
295 } | |
296 return 0; | |
297 } | |
298 | |
299 int CVICALLBACK Plot_CB_ChangeDrift (int panel, int control, int event, | |
300 void *callbackData, int eventData1, int eventData2) | |
301 { | |
302 bool DeDrift ; | |
303 Plot_Data * Instance = NULL; | |
304 | |
305 switch (event) | |
306 { | |
307 case EVENT_COMMIT: | |
308 GetCtrlVal(panel, PLOTPANEL_CHECKBOX_DEDRIFT, &DeDrift) ; | |
309 if(DeDrift) { | |
310 | |
311 //Instance = ThisPlot(panel) ; | |
312 GetPanelAttribute (panel, ATTR_CALLBACK_DATA, &Instance); | |
313 | |
314 Instance->IndexPoint = 0 ; | |
315 Instance->Mean = 0 ; | |
316 Instance->Slope = 0 ; | |
317 Instance->ADev = 0 ; | |
318 | |
319 } ; | |
320 | |
321 break; | |
322 } | |
323 return 0; | |
324 } | |
325 |