Mercurial > hg > fxanalyse
annotate Allan.c @ 228:5e69d9abbbf2
Update Hg expected frequency with value of last measurement
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Thu, 09 Oct 2014 15:58:32 +0200 |
parents | ddc8c47db3df |
children |
rev | line source |
---|---|
0 | 1 #include <ansi_c.h> |
2 #include <userint.h> | |
3 | |
144 | 4 #include "utils.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); | |
219
ddc8c47db3df
Fix allan deviation plot
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
214
diff
changeset
|
29 SetPanelAttribute(Instance->AllanPanel, ATTR_CALLBACK_DATA, (void *)Instance); |
ddc8c47db3df
Fix allan deviation plot
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
214
diff
changeset
|
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); | |
138 | 39 DiscardPanel(Instance->AllanPanel); |
135 | 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 } | |
72 Instance->LastMean[i] = CurrentMean; | |
73 Instance->BlocksNumber[i] = N; | |
74 } | |
75 } | |
76 | |
77 Allan_Display(Instance); | |
78 } | |
0 | 79 |
135 | 80 |
81 /* private */ | |
82 | |
83 static void Allan_Reset(Allan_Data *Instance) | |
84 { | |
85 memset(Instance->AllanVar, 0, sizeof(Instance->AllanVar)); | |
86 memset(Instance->LastMean, 0, sizeof(Instance->LastMean)); | |
87 memset(Instance->BlocksNumber, 0, sizeof(Instance->BlocksNumber)); | |
88 memset(Instance->CurrentAverage, 0, sizeof(Instance->CurrentAverage)); | |
89 memset(Instance->NbCurrentAverage, 0, sizeof(Instance->NbCurrentAverage)); | |
90 Instance->Drift = 0; | |
91 Instance->Mean = 0; | |
92 } | |
0 | 93 |
94 | |
135 | 95 static void Allan_Display(Allan_Data *Instance) |
96 { | |
138 | 97 double x[ALLAN_NUM_DATAPOINTS]; |
98 double y[ALLAN_NUM_DATAPOINTS]; | |
99 double error; | |
100 int dedrift; | |
137
792ac7151f0f
Fix Allan deviation linear drift contribution removal
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
136
diff
changeset
|
101 int i; |
135 | 102 |
103 GetCtrlVal(Instance->AllanPanel, ALLANPANEL_DEDRIFT, &dedrift); | |
136 | 104 SetCtrlVal(Instance->AllanPanel, ALLANPANEL_DRIFT, Instance->Drift); |
135 | 105 |
136 | 106 for (i = 0; i < ALLAN_NUM_DATAPOINTS; i++) { |
138 | 107 x[i] = pow(2, i); |
219
ddc8c47db3df
Fix allan deviation plot
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
214
diff
changeset
|
108 |
ddc8c47db3df
Fix allan deviation plot
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
214
diff
changeset
|
109 if (Instance->AllanVar[i] == 0.0) { |
ddc8c47db3df
Fix allan deviation plot
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
214
diff
changeset
|
110 y[i] = 0.0; |
ddc8c47db3df
Fix allan deviation plot
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
214
diff
changeset
|
111 continue; |
ddc8c47db3df
Fix allan deviation plot
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
214
diff
changeset
|
112 } |
ddc8c47db3df
Fix allan deviation plot
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
214
diff
changeset
|
113 |
137
792ac7151f0f
Fix Allan deviation linear drift contribution removal
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
136
diff
changeset
|
114 /* remove linear drift estimated contribution to the allan variance */ |
135 | 115 if (dedrift) { |
138 | 116 double corr = 0.5 * (Instance->Drift * x[i]) * (Instance->Drift * x[i]); |
137
792ac7151f0f
Fix Allan deviation linear drift contribution removal
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
136
diff
changeset
|
117 /* uncertainty in the estimate of the drift rate may over correct |
792ac7151f0f
Fix Allan deviation linear drift contribution removal
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
136
diff
changeset
|
118 the estimated allan variance and result in a negative value. in |
792ac7151f0f
Fix Allan deviation linear drift contribution removal
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
136
diff
changeset
|
119 this case we take the absolute value of the corrected value as |
792ac7151f0f
Fix Allan deviation linear drift contribution removal
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
136
diff
changeset
|
120 the best estimator of the allan variance */ |
138 | 121 y[i] = sqrt(fabs(Instance->AllanVar[i] - corr)) / Instance->normalization; |
135 | 122 } else { |
138 | 123 y[i] = sqrt(Instance->AllanVar[i]) / Instance->normalization; |
135 | 124 } |
125 } | |
126 | |
127 DeleteGraphPlot(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, -1, VAL_IMMEDIATE_DRAW); | |
138 | 128 PlotXY(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, x, y, ALLAN_NUM_DATAPOINTS, |
135 | 129 VAL_DOUBLE, VAL_DOUBLE, VAL_SCATTER, VAL_SOLID_SQUARE, VAL_SOLID, 1, DATAPOINT_COLOR); |
130 | |
214
920ff733b43b
Fix Allan deviation plot
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
144
diff
changeset
|
131 for (i = 0; (i < ALLAN_NUM_DATAPOINTS) & (Instance->BlocksNumber[i] > 0); i++) { |
138 | 132 error = 1 / sqrt(Instance->BlocksNumber[i]); |
133 PlotLine(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, | |
134 x[i], y[i] * (1 - error), x[i], y[i] * (1 + error), ERRORBAR_COLOR); | |
135 | 135 } |
136 } | |
0 | 137 |
138 | |
135 | 139 /* callbacks */ |
0 | 140 |
141 int CVICALLBACK Allan_CB_Reset(int panel, int control, int event, | |
142 void *callbackData, int eventData1, int eventData2) | |
143 { | |
135 | 144 switch(event) |
145 { | |
0 | 146 case EVENT_COMMIT: |
135 | 147 Allan_Data *data; |
148 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
149 Allan_Reset(data); | |
0 | 150 break; |
135 | 151 } |
0 | 152 return 0; |
153 } | |
154 | |
135 | 155 |
156 int CVICALLBACK Allan_CB_ChangeYLim (int panel, int control, int event, | |
0 | 157 void *callbackData, int eventData1, int eventData2) |
135 | 158 { |
0 | 159 switch (event) |
135 | 160 { |
0 | 161 case EVENT_COMMIT: |
136 | 162 int pmin, pmax; |
163 GetCtrlVal(panel, ALLANPANEL_MIN, &pmin); | |
164 GetCtrlVal(panel, ALLANPANEL_MAX, &pmax); | |
165 if (pmin < pmax) { | |
166 SetAxisScalingMode(panel, ALLANPANEL_ALLANPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, pow(10, pmin), pow(10, pmax)); | |
167 SetCtrlVal(panel, ALLANPANEL_CHECKBOX_AUTOSCALE, FALSE); | |
135 | 168 } |
0 | 169 break; |
135 | 170 } |
0 | 171 return 0; |
172 } | |
173 | |
174 | |
175 int CVICALLBACK Allan_CB_ChangeAutoScale (int panel, int control, int event, | |
176 void *callbackData, int eventData1, int eventData2) | |
177 { | |
178 switch (event) | |
135 | 179 { |
0 | 180 case EVENT_COMMIT: |
135 | 181 Allan_Data *data; |
182 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
136 | 183 GetCtrlVal(panel, control, &(data->autoscale)); |
135 | 184 Allan_Display(data); |
0 | 185 break; |
135 | 186 } |
0 | 187 return 0; |
188 } | |
189 | |
190 | |
135 | 191 int CVICALLBACK Allan_CB_ChangeNormalization (int panel, int control, int event, |
192 void *callbackData, int eventData1, int eventData2) | |
193 { | |
194 switch (event) | |
195 { | |
196 case EVENT_COMMIT: | |
197 Allan_Data *data; | |
198 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
199 GetCtrlVal(panel, control, &(data->normalization)); | |
200 Allan_Display(data); | |
201 break; | |
0 | 202 } |
135 | 203 return 0; |
204 } | |
0 | 205 |
206 | |
207 int CVICALLBACK CB_GeneralAllanPanel (int panel, int event, void *callbackData, | |
208 int eventData1, int eventData2) | |
209 { | |
210 switch (event) | |
135 | 211 { |
212 case EVENT_CLOSE: | |
213 Allan_Data *data; | |
214 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
215 Allan_ClosePanel(data); | |
0 | 216 break; |
135 | 217 } |
0 | 218 return 0; |
219 } |