Mercurial > hg > fxanalyse
annotate Allan.c @ 137:792ac7151f0f
Fix Allan deviation linear drift contribution removal
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Wed, 22 Jan 2014 13:41:07 +0100 |
parents | 7b9cf3d4346e |
children | 02044ad2749a |
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 } | |
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 { | |
137
792ac7151f0f
Fix Allan deviation linear drift contribution removal
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
136
diff
changeset
|
97 int i; |
136 | 98 double X[ALLAN_NUM_DATAPOINTS]; |
99 double Y[ALLAN_NUM_DATAPOINTS]; | |
137
792ac7151f0f
Fix Allan deviation linear drift contribution removal
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
136
diff
changeset
|
100 double Error; |
135 | 101 int dedrift; |
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; |
135 | 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) { |
137
792ac7151f0f
Fix Allan deviation linear drift contribution removal
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
136
diff
changeset
|
112 double corr = 0.5 * (Instance->Drift * X[i]) * (Instance->Drift * X[i]); |
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 */ |
792ac7151f0f
Fix Allan deviation linear drift contribution removal
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
136
diff
changeset
|
117 Y[i] = sqrt(fabs(Instance->AllanVar[i] - corr)) / Instance->normalization; |
135 | 118 } else { |
137
792ac7151f0f
Fix Allan deviation linear drift contribution removal
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
136
diff
changeset
|
119 Y[i] = sqrt(Instance->AllanVar[i]) / Instance->normalization; |
135 | 120 } |
121 } | |
122 | |
123 DeleteGraphPlot(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, -1, VAL_IMMEDIATE_DRAW); | |
136 | 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++) { |
135 | 128 Error = 1/sqrt(Instance->BlocksNumber[i]); |
129 PlotLine(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, X[i], Y[i]*(1-Error), X[i], Y[i]*(1+Error), ERRORBAR_COLOR); | |
130 } | |
131 } | |
0 | 132 |
133 | |
135 | 134 /* callbacks */ |
0 | 135 |
136 int CVICALLBACK Allan_CB_Reset(int panel, int control, int event, | |
137 void *callbackData, int eventData1, int eventData2) | |
138 { | |
135 | 139 switch(event) |
140 { | |
0 | 141 case EVENT_COMMIT: |
135 | 142 Allan_Data *data; |
143 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
144 Allan_Reset(data); | |
0 | 145 break; |
135 | 146 } |
0 | 147 return 0; |
148 } | |
149 | |
135 | 150 |
151 int CVICALLBACK Allan_CB_ChangeYLim (int panel, int control, int event, | |
0 | 152 void *callbackData, int eventData1, int eventData2) |
135 | 153 { |
0 | 154 switch (event) |
135 | 155 { |
0 | 156 case EVENT_COMMIT: |
136 | 157 int pmin, pmax; |
158 GetCtrlVal(panel, ALLANPANEL_MIN, &pmin); | |
159 GetCtrlVal(panel, ALLANPANEL_MAX, &pmax); | |
160 if (pmin < pmax) { | |
161 SetAxisScalingMode(panel, ALLANPANEL_ALLANPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, pow(10, pmin), pow(10, pmax)); | |
162 SetCtrlVal(panel, ALLANPANEL_CHECKBOX_AUTOSCALE, FALSE); | |
135 | 163 } |
0 | 164 break; |
135 | 165 } |
0 | 166 return 0; |
167 } | |
168 | |
169 | |
170 int CVICALLBACK Allan_CB_ChangeAutoScale (int panel, int control, int event, | |
171 void *callbackData, int eventData1, int eventData2) | |
172 { | |
173 switch (event) | |
135 | 174 { |
0 | 175 case EVENT_COMMIT: |
135 | 176 Allan_Data *data; |
177 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
136 | 178 GetCtrlVal(panel, control, &(data->autoscale)); |
135 | 179 Allan_Display(data); |
0 | 180 break; |
135 | 181 } |
0 | 182 return 0; |
183 } | |
184 | |
185 | |
135 | 186 int CVICALLBACK Allan_CB_ChangeNormalization (int panel, int control, int event, |
187 void *callbackData, int eventData1, int eventData2) | |
188 { | |
189 switch (event) | |
190 { | |
191 case EVENT_COMMIT: | |
192 Allan_Data *data; | |
193 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
194 GetCtrlVal(panel, control, &(data->normalization)); | |
195 Allan_Display(data); | |
196 break; | |
0 | 197 } |
135 | 198 return 0; |
199 } | |
0 | 200 |
201 | |
202 int CVICALLBACK CB_GeneralAllanPanel (int panel, int event, void *callbackData, | |
203 int eventData1, int eventData2) | |
204 { | |
205 switch (event) | |
135 | 206 { |
207 case EVENT_CLOSE: | |
208 Allan_Data *data; | |
209 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
210 Allan_ClosePanel(data); | |
0 | 211 break; |
135 | 212 } |
0 | 213 return 0; |
214 } |