Mercurial > hg > fxanalyse
annotate Allan.c @ 189:e6cb16365d12
New build
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Mon, 31 Mar 2014 17:03:31 +0200 |
parents | be87c8e78266 |
children | 920ff733b43b |
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); | |
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); | |
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++) { |
137
792ac7151f0f
Fix Allan deviation linear drift contribution removal
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
136
diff
changeset
|
107 if (Instance->AllanVar[i] == 0.0) |
792ac7151f0f
Fix Allan deviation linear drift contribution removal
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
136
diff
changeset
|
108 break; |
138 | 109 x[i] = pow(2, i); |
137
792ac7151f0f
Fix Allan deviation linear drift contribution removal
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
136
diff
changeset
|
110 /* remove linear drift estimated contribution to the allan variance */ |
135 | 111 if (dedrift) { |
138 | 112 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
|
113 /* 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
|
114 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
|
115 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
|
116 the best estimator of the allan variance */ |
138 | 117 y[i] = sqrt(fabs(Instance->AllanVar[i] - corr)) / Instance->normalization; |
135 | 118 } else { |
138 | 119 y[i] = sqrt(Instance->AllanVar[i]) / Instance->normalization; |
135 | 120 } |
121 } | |
122 | |
123 DeleteGraphPlot(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, -1, VAL_IMMEDIATE_DRAW); | |
138 | 124 PlotXY(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, x, y, ALLAN_NUM_DATAPOINTS, |
135 | 125 VAL_DOUBLE, VAL_DOUBLE, VAL_SCATTER, VAL_SOLID_SQUARE, VAL_SOLID, 1, DATAPOINT_COLOR); |
126 | |
136 | 127 for (i = 0; i < ALLAN_NUM_DATAPOINTS; i++) { |
138 | 128 error = 1 / sqrt(Instance->BlocksNumber[i]); |
129 PlotLine(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, | |
130 x[i], y[i] * (1 - error), x[i], y[i] * (1 + error), ERRORBAR_COLOR); | |
135 | 131 } |
132 } | |
0 | 133 |
134 | |
135 | 135 /* callbacks */ |
0 | 136 |
137 int CVICALLBACK Allan_CB_Reset(int panel, int control, int event, | |
138 void *callbackData, int eventData1, int eventData2) | |
139 { | |
135 | 140 switch(event) |
141 { | |
0 | 142 case EVENT_COMMIT: |
135 | 143 Allan_Data *data; |
144 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
145 Allan_Reset(data); | |
0 | 146 break; |
135 | 147 } |
0 | 148 return 0; |
149 } | |
150 | |
135 | 151 |
152 int CVICALLBACK Allan_CB_ChangeYLim (int panel, int control, int event, | |
0 | 153 void *callbackData, int eventData1, int eventData2) |
135 | 154 { |
0 | 155 switch (event) |
135 | 156 { |
0 | 157 case EVENT_COMMIT: |
136 | 158 int pmin, pmax; |
159 GetCtrlVal(panel, ALLANPANEL_MIN, &pmin); | |
160 GetCtrlVal(panel, ALLANPANEL_MAX, &pmax); | |
161 if (pmin < pmax) { | |
162 SetAxisScalingMode(panel, ALLANPANEL_ALLANPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, pow(10, pmin), pow(10, pmax)); | |
163 SetCtrlVal(panel, ALLANPANEL_CHECKBOX_AUTOSCALE, FALSE); | |
135 | 164 } |
0 | 165 break; |
135 | 166 } |
0 | 167 return 0; |
168 } | |
169 | |
170 | |
171 int CVICALLBACK Allan_CB_ChangeAutoScale (int panel, int control, int event, | |
172 void *callbackData, int eventData1, int eventData2) | |
173 { | |
174 switch (event) | |
135 | 175 { |
0 | 176 case EVENT_COMMIT: |
135 | 177 Allan_Data *data; |
178 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
136 | 179 GetCtrlVal(panel, control, &(data->autoscale)); |
135 | 180 Allan_Display(data); |
0 | 181 break; |
135 | 182 } |
0 | 183 return 0; |
184 } | |
185 | |
186 | |
135 | 187 int CVICALLBACK Allan_CB_ChangeNormalization (int panel, int control, int event, |
188 void *callbackData, int eventData1, int eventData2) | |
189 { | |
190 switch (event) | |
191 { | |
192 case EVENT_COMMIT: | |
193 Allan_Data *data; | |
194 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
195 GetCtrlVal(panel, control, &(data->normalization)); | |
196 Allan_Display(data); | |
197 break; | |
0 | 198 } |
135 | 199 return 0; |
200 } | |
0 | 201 |
202 | |
203 int CVICALLBACK CB_GeneralAllanPanel (int panel, int event, void *callbackData, | |
204 int eventData1, int eventData2) | |
205 { | |
206 switch (event) | |
135 | 207 { |
208 case EVENT_CLOSE: | |
209 Allan_Data *data; | |
210 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
211 Allan_ClosePanel(data); | |
0 | 212 break; |
135 | 213 } |
0 | 214 return 0; |
215 } |