Mercurial > hg > fxanalyse
annotate Allan.c @ 135:77539f2597b1
Code cleanup
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Wed, 22 Jan 2014 12:29:38 +0100 |
parents | bd28161e5ac2 |
children | 7b9cf3d4346e |
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 double Normalizer = Instance->normalization; | |
50 | |
51 /* compute drift rate */ | |
52 if (N > 1) { | |
53 Instance->Drift = (Drift * (N - 2) + 6 * (Freq - Mean) / N) / (N + 1); | |
54 SetCtrlVal(Instance->AllanPanel, ALLANPANEL_DRIFT, Instance->Drift/Normalizer); | |
55 } | |
56 Instance->Mean = (Mean * (N - 1) + Freq) / N; | |
57 | |
58 /* compute allan deviation */ | |
59 for (int i = 0; i < ALLAN_MAXPOINTSNUMBER; i++) { | |
60 Instance->CurrentAverage[i] = (Instance->CurrentAverage[i]*Instance->NbCurrentAverage[i] + Freq) | |
61 /(Instance->NbCurrentAverage[i]+1); | |
62 Instance->NbCurrentAverage[i] +=1; | |
63 | |
64 if (Instance->NbCurrentAverage[i] >= pow(2,i) ) { | |
65 double CurrentMean = Instance->CurrentAverage[i]; | |
66 double LastMean = Instance->LastMean[i]; | |
67 N = Instance->BlocksNumber[i]; | |
68 N++; | |
69 Instance->CurrentAverage[i] = 0; | |
70 Instance->NbCurrentAverage[i] = 0; | |
71 if (N > 1) { | |
72 Instance->AllanVar[i] = (Instance->AllanVar[i]*(N-2) | |
73 +0.5*pow(CurrentMean-LastMean,2))/(N-1) ; | |
74 } else { // ie if N=1, which is realized for the first completed block | |
75 Instance->FirstMean[i] = CurrentMean; | |
76 } | |
77 Instance->LastMean[i] = CurrentMean; | |
78 Instance->BlocksNumber[i] = N; | |
79 } | |
80 } | |
81 | |
82 Allan_Display(Instance); | |
83 } | |
0 | 84 |
135 | 85 |
86 /* private */ | |
87 | |
88 static void Allan_Reset(Allan_Data *Instance) | |
89 { | |
90 memset(Instance->AllanVar, 0, sizeof(Instance->AllanVar)); | |
91 memset(Instance->FirstMean, 0, sizeof(Instance->FirstMean)); | |
92 memset(Instance->LastMean, 0, sizeof(Instance->LastMean)); | |
93 memset(Instance->BlocksNumber, 0, sizeof(Instance->BlocksNumber)); | |
94 memset(Instance->CurrentAverage, 0, sizeof(Instance->CurrentAverage)); | |
95 memset(Instance->NbCurrentAverage, 0, sizeof(Instance->NbCurrentAverage)); | |
96 Instance->Drift = 0; | |
97 Instance->Mean = 0; | |
98 } | |
0 | 99 |
100 | |
135 | 101 static void Allan_Display(Allan_Data *Instance) |
102 { | |
103 int i, N; | |
104 double X[ALLAN_MAXPOINTSNUMBER]; | |
105 double Y[ALLAN_MAXPOINTSNUMBER]; | |
106 double Normalizer = Instance->normalization; | |
107 double DriftTau, FirstFreq, LastFreq, Error; | |
108 int dedrift; | |
109 | |
110 GetCtrlVal(Instance->AllanPanel, ALLANPANEL_DEDRIFT, &dedrift); | |
111 | |
112 for (i = 0; i < ALLAN_MAXPOINTSNUMBER; i++) { | |
113 X[i] = pow(2, i); | |
114 if (dedrift) { | |
115 DriftTau = Instance->Drift * X[i]; | |
116 N = Instance->BlocksNumber[i]; | |
117 if (N>=2) { // if enough data to calculate AVAR for tau = 2^i*tau0, and therefore, AllanVar[i] is meaningful | |
118 FirstFreq = Instance->FirstMean[i]; | |
119 LastFreq = Instance->LastMean[i]; | |
120 Y[i] = sqrt(Instance->AllanVar[i]+DriftTau*(DriftTau/2-(LastFreq-FirstFreq)/(N-1)))/Normalizer; | |
121 // Not totaly sure it works... to be checked thoroughly | |
122 } | |
123 else { | |
124 Y[i] = 0; | |
125 } | |
126 } else { | |
127 Y[i] = sqrt(Instance->AllanVar[i])/Normalizer; | |
128 } | |
129 } | |
130 | |
131 DeleteGraphPlot(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, -1, VAL_IMMEDIATE_DRAW); | |
132 PlotXY(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, X, Y, ALLAN_MAXPOINTSNUMBER, | |
133 VAL_DOUBLE, VAL_DOUBLE, VAL_SCATTER, VAL_SOLID_SQUARE, VAL_SOLID, 1, DATAPOINT_COLOR); | |
134 | |
135 for (i = 0; i < ALLAN_MAXPOINTSNUMBER; i++) { | |
136 Error = 1/sqrt(Instance->BlocksNumber[i]); | |
137 PlotLine(Instance->AllanPanel, ALLANPANEL_ALLANPLOT, X[i], Y[i]*(1-Error), X[i], Y[i]*(1+Error), ERRORBAR_COLOR); | |
138 } | |
139 } | |
0 | 140 |
141 | |
135 | 142 /* callbacks */ |
0 | 143 |
144 int CVICALLBACK Allan_CB_Reset(int panel, int control, int event, | |
145 void *callbackData, int eventData1, int eventData2) | |
146 { | |
135 | 147 switch(event) |
148 { | |
0 | 149 case EVENT_COMMIT: |
135 | 150 Allan_Data *data; |
151 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
152 Allan_Reset(data); | |
0 | 153 break; |
135 | 154 } |
0 | 155 return 0; |
156 } | |
157 | |
135 | 158 |
159 int CVICALLBACK Allan_CB_ChangeYLim (int panel, int control, int event, | |
0 | 160 void *callbackData, int eventData1, int eventData2) |
135 | 161 { |
0 | 162 switch (event) |
135 | 163 { |
0 | 164 case EVENT_COMMIT: |
135 | 165 int YMin, YMax; |
166 GetCtrlVal(panel, ALLANPANEL_MIN, &YMin); | |
167 GetCtrlVal(panel, ALLANPANEL_MAX, &YMax); | |
168 if (YMin < YMax) { | |
169 SetAxisScalingMode(panel, ALLANPANEL_ALLANPLOT, VAL_LEFT_YAXIS, VAL_MANUAL, pow(10,(double)YMin), pow(10,(double)YMax) ); | |
170 SetCtrlVal(panel, ALLANPANEL_CHECKBOX_AUTOSCALE, FALSE); | |
171 } | |
0 | 172 break; |
135 | 173 } |
0 | 174 return 0; |
175 } | |
176 | |
177 | |
178 int CVICALLBACK Allan_CB_ChangeAutoScale (int panel, int control, int event, | |
179 void *callbackData, int eventData1, int eventData2) | |
180 { | |
181 switch (event) | |
135 | 182 { |
0 | 183 case EVENT_COMMIT: |
135 | 184 Allan_Data *data; |
185 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
186 GetCtrlVal(panel, control, &(data->normalization)); | |
187 Allan_Display(data); | |
0 | 188 break; |
135 | 189 } |
0 | 190 return 0; |
191 } | |
192 | |
193 | |
135 | 194 int CVICALLBACK Allan_CB_ChangeNormalization (int panel, int control, int event, |
195 void *callbackData, int eventData1, int eventData2) | |
196 { | |
197 switch (event) | |
198 { | |
199 case EVENT_COMMIT: | |
200 Allan_Data *data; | |
201 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
202 GetCtrlVal(panel, control, &(data->normalization)); | |
203 Allan_Display(data); | |
204 break; | |
0 | 205 } |
135 | 206 return 0; |
207 } | |
0 | 208 |
209 | |
210 int CVICALLBACK CB_GeneralAllanPanel (int panel, int event, void *callbackData, | |
211 int eventData1, int eventData2) | |
212 { | |
213 switch (event) | |
135 | 214 { |
215 case EVENT_CLOSE: | |
216 Allan_Data *data; | |
217 GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &data); | |
218 Allan_ClosePanel(data); | |
0 | 219 break; |
135 | 220 } |
0 | 221 return 0; |
222 } |