Mercurial > hg > fxanalyse
annotate Allan.c @ 136:7b9cf3d4346e
Code cleanup
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Wed, 22 Jan 2014 12:29:58 +0100 |
parents | 77539f2597b1 |
children | 792ac7151f0f |
rev | line source |
---|---|
0 | 1 #include <ansi_c.h> |
2 #include <userint.h> | |
3 | |
4 #include "YLCStuff.h" | |
135 | 5 #include "FXAllan.h" |
6 #include "Allan.h" | |
0 | 7 |
135 | 8 #define DATAPOINT_COLOR VAL_RED |
9 #define ERRORBAR_COLOR VAL_RED | |
0 | 10 |
11 | |
135 | 12 static void Allan_Reset(Allan_Data * Instance); |
13 static void Allan_Display(Allan_Data * Instance); | |
14 | |
0 | 15 |
135 | 16 void Allan_InitPanel(Allan_Data * Instance, const char *title, double normalization, int parent, int control) |
17 { | |
18 if ((Instance->AllanPanel = LoadPanel (0, "FXAllan.uir", ALLANPANEL)) < 0) | |
19 return; | |
0 | 20 |
135 | 21 Allan_Reset(Instance); |
22 Instance->normalization = normalization; | |
23 Instance->autoscale = FALSE; | |
134
bd28161e5ac2
Major code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
103
diff
changeset
|
24 Instance->active = TRUE; |
bd28161e5ac2
Major code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
103
diff
changeset
|
25 Instance->parent = parent; |
bd28161e5ac2
Major code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
103
diff
changeset
|
26 Instance->control = control; |
135 | 27 |
28 SetPanelAttribute(Instance->AllanPanel, ATTR_TITLE, title); | |
29 SetPanelAttribute (Instance->AllanPanel, ATTR_CALLBACK_DATA, (void *)Instance); | |
0 | 30 DisplayPanel (Instance->AllanPanel); |
135 | 31 SetCtrlVal(Instance->AllanPanel, ALLANPANEL_NORMALIZER, normalization); |
32 } | |
33 | |
34 | |
35 void Allan_ClosePanel(Allan_Data * Instance) | |
36 { | |
37 Instance->active = FALSE; | |
38 SetCtrlVal(Instance->parent, Instance->control, FALSE); | |
39 DiscardPanel (Instance->AllanPanel); | |
40 } | |
0 | 41 |
42 | |
135 | 43 void Allan_AddFrequency(Allan_Data * Instance, double Freq) |
44 { | |
45 /* total number of points used. used to calculate the drift rate */ | |
46 int N = 1 + Instance->BlocksNumber[0]; | |
47 double Mean = Instance->Mean; | |
48 double Drift = Instance->Drift; | |
49 | |
50 /* compute drift rate */ | |
136 | 51 if (N > 1) |
135 | 52 Instance->Drift = (Drift * (N - 2) + 6 * (Freq - Mean) / N) / (N + 1); |
53 Instance->Mean = (Mean * (N - 1) + Freq) / N; | |
54 | |
55 /* compute allan deviation */ | |
136 | 56 for (int i = 0; i < ALLAN_NUM_DATAPOINTS; i++) { |
135 | 57 Instance->CurrentAverage[i] = (Instance->CurrentAverage[i]*Instance->NbCurrentAverage[i] + Freq) |
58 /(Instance->NbCurrentAverage[i]+1); | |
59 Instance->NbCurrentAverage[i] +=1; | |
60 | |
61 if (Instance->NbCurrentAverage[i] >= pow(2,i) ) { | |
62 double CurrentMean = Instance->CurrentAverage[i]; | |
63 double LastMean = Instance->LastMean[i]; | |
64 N = Instance->BlocksNumber[i]; | |
65 N++; | |
66 Instance->CurrentAverage[i] = 0; | |
67 Instance->NbCurrentAverage[i] = 0; | |
68 if (N > 1) { | |
69 Instance->AllanVar[i] = (Instance->AllanVar[i]*(N-2) | |
70 +0.5*pow(CurrentMean-LastMean,2))/(N-1) ; | |
71 } else { // ie if N=1, which is realized for the first completed block | |
72 Instance->FirstMean[i] = CurrentMean; | |
73 } | |
74 Instance->LastMean[i] = CurrentMean; | |
75 Instance->BlocksNumber[i] = N; | |
76 } | |
77 } | |
78 | |
79 Allan_Display(Instance); | |
80 } | |
0 | 81 |
135 | 82 |
83 /* private */ | |
84 | |
85 static void Allan_Reset(Allan_Data *Instance) | |
86 { | |
87 memset(Instance->AllanVar, 0, sizeof(Instance->AllanVar)); | |
88 memset(Instance->FirstMean, 0, sizeof(Instance->FirstMean)); | |
89 memset(Instance->LastMean, 0, sizeof(Instance->LastMean)); | |
90 memset(Instance->BlocksNumber, 0, sizeof(Instance->BlocksNumber)); | |
91 memset(Instance->CurrentAverage, 0, sizeof(Instance->CurrentAverage)); | |
92 memset(Instance->NbCurrentAverage, 0, sizeof(Instance->NbCurrentAverage)); | |
93 Instance->Drift = 0; | |
94 Instance->Mean = 0; | |
95 } | |
0 | 96 |
97 | |
135 | 98 static void Allan_Display(Allan_Data *Instance) |
99 { | |
100 int i, N; | |
136 | 101 double X[ALLAN_NUM_DATAPOINTS]; |
102 double Y[ALLAN_NUM_DATAPOINTS]; | |
135 | 103 double Normalizer = Instance->normalization; |
104 double DriftTau, FirstFreq, LastFreq, Error; | |
105 int dedrift; | |
106 | |
107 GetCtrlVal(Instance->AllanPanel, ALLANPANEL_DEDRIFT, &dedrift); | |
136 | 108 SetCtrlVal(Instance->AllanPanel, ALLANPANEL_DRIFT, Instance->Drift); |
135 | 109 |
136 | 110 for (i = 0; i < ALLAN_NUM_DATAPOINTS; i++) { |
135 | 111 X[i] = pow(2, i); |
112 if (dedrift) { | |
113 DriftTau = Instance->Drift * X[i]; | |
114 N = Instance->BlocksNumber[i]; | |
136 | 115 if (N > 1) { // if enough data to calculate AVAR for tau = 2^i*tau0, and therefore, AllanVar[i] is meaningful |
135 | 116 FirstFreq = Instance->FirstMean[i]; |
117 LastFreq = Instance->LastMean[i]; | |
118 Y[i] = sqrt(Instance->AllanVar[i]+DriftTau*(DriftTau/2-(LastFreq-FirstFreq)/(N-1)))/Normalizer; | |
119 // Not totaly sure it works... to be checked thoroughly | |
120 } | |
121 else { | |
122 Y[i] = 0; | |
123 } | |
124 } else { | |
125 Y[i] = sqrt(Instance->AllanVar[i])/Normalizer; | |
126 } | |
127 } | |
128 | |
129 DeleteGraphPlot(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, -1, VAL_IMMEDIATE_DRAW); | |
136 | 130 PlotXY(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, X, Y, ALLAN_NUM_DATAPOINTS, |
135 | 131 VAL_DOUBLE, VAL_DOUBLE, VAL_SCATTER, VAL_SOLID_SQUARE, VAL_SOLID, 1, DATAPOINT_COLOR); |
132 | |
136 | 133 for (i = 0; i < ALLAN_NUM_DATAPOINTS; i++) { |
135 | 134 Error = 1/sqrt(Instance->BlocksNumber[i]); |
135 PlotLine(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, X[i], Y[i]*(1-Error), X[i], Y[i]*(1+Error), ERRORBAR_COLOR); | |
136 } | |
137 } | |
0 | 138 |
139 | |
135 | 140 /* callbacks */ |
0 | 141 |
142 int CVICALLBACK Allan_CB_Reset(int panel, int control, int event, | |
143 void *callbackData, int eventData1, int eventData2) | |
144 { | |
135 | 145 switch(event) |
146 { | |
0 | 147 case EVENT_COMMIT: |
135 | 148 Allan_Data *data; |
149 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
150 Allan_Reset(data); | |
0 | 151 break; |
135 | 152 } |
0 | 153 return 0; |
154 } | |
155 | |
135 | 156 |
157 int CVICALLBACK Allan_CB_ChangeYLim (int panel, int control, int event, | |
0 | 158 void *callbackData, int eventData1, int eventData2) |
135 | 159 { |
0 | 160 switch (event) |
135 | 161 { |
0 | 162 case EVENT_COMMIT: |
136 | 163 int pmin, pmax; |
164 GetCtrlVal(panel, ALLANPANEL_MIN, &pmin); | |
165 GetCtrlVal(panel, ALLANPANEL_MAX, &pmax); | |
166 if (pmin < pmax) { | |
167 SetAxisScalingMode(panel, ALLANPANEL_ALLANPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, pow(10, pmin), pow(10, pmax)); | |
168 SetCtrlVal(panel, ALLANPANEL_CHECKBOX_AUTOSCALE, FALSE); | |
135 | 169 } |
0 | 170 break; |
135 | 171 } |
0 | 172 return 0; |
173 } | |
174 | |
175 | |
176 int CVICALLBACK Allan_CB_ChangeAutoScale (int panel, int control, int event, | |
177 void *callbackData, int eventData1, int eventData2) | |
178 { | |
179 switch (event) | |
135 | 180 { |
0 | 181 case EVENT_COMMIT: |
135 | 182 Allan_Data *data; |
183 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
136 | 184 GetCtrlVal(panel, control, &(data->autoscale)); |
135 | 185 Allan_Display(data); |
0 | 186 break; |
135 | 187 } |
0 | 188 return 0; |
189 } | |
190 | |
191 | |
135 | 192 int CVICALLBACK Allan_CB_ChangeNormalization (int panel, int control, int event, |
193 void *callbackData, int eventData1, int eventData2) | |
194 { | |
195 switch (event) | |
196 { | |
197 case EVENT_COMMIT: | |
198 Allan_Data *data; | |
199 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
200 GetCtrlVal(panel, control, &(data->normalization)); | |
201 Allan_Display(data); | |
202 break; | |
0 | 203 } |
135 | 204 return 0; |
205 } | |
0 | 206 |
207 | |
208 int CVICALLBACK CB_GeneralAllanPanel (int panel, int event, void *callbackData, | |
209 int eventData1, int eventData2) | |
210 { | |
211 switch (event) | |
135 | 212 { |
213 case EVENT_CLOSE: | |
214 Allan_Data *data; | |
215 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
216 Allan_ClosePanel(data); | |
0 | 217 break; |
135 | 218 } |
0 | 219 return 0; |
220 } |