1 | function varargout = measbpmsigma(varargin) |
---|
2 | %MEASBPMSIGMA - Measures the standard deviation of the BPMs |
---|
3 | % [BPMxSTD, BPMzSTD, BPMx, BPMz, tout, DCCT, FileName] = measbpmsigma(t, BPMxFamily, BPMxList, BPMzFamily, BPMzList, FileName) |
---|
4 | % [BPMxSTD, BPMzSTD, FileName] = measbpmsigma(... , 'Struct') |
---|
5 | % |
---|
6 | % INPUTS |
---|
7 | % 1. t = time vector [seconds], or |
---|
8 | % length of time in seconds to measure data (scalar) |
---|
9 | % {default: 3 minute at a sample rate of 2 Hz} |
---|
10 | % 2 and 4. BPMxFamily and BPMzFamily are the family names of the BPM's, {default or []: the entire list} |
---|
11 | % 3 and 5. BPMxList and BPMzList are the device list of BPM's, {default or []: the entire list} |
---|
12 | % 6. 'Struct' will return data structures instead of vectors |
---|
13 | % 'Numeric' will return vector outputs {default} |
---|
14 | % 7. FileName = Filename (including directory) where the data was saved (if applicable) |
---|
15 | % 8. 'Archive' - save a data array structure to \<BPMData Directory>\<BPMSigmaFile><Date><Time>.mat {Default} |
---|
16 | % 'NoArchive' - no data will be saved to file |
---|
17 | % |
---|
18 | % OUTPUTS |
---|
19 | % For numeric output: |
---|
20 | % 1-2. BPMxSTD and BPMzSTD are standard deviation of the difference orbits |
---|
21 | % 3-4. BPMx and BPMz are the raw orbit data matrices |
---|
22 | % 5. DCCT is a row vector containing the beam current |
---|
23 | % 6. tout is a row vector of times as returned by getam |
---|
24 | % 7. FileName = Filename (including directory) where the data was saved (if applicable) |
---|
25 | % |
---|
26 | % For structures: |
---|
27 | % 1-2. BPMxSTD and BPMzSTD - the standard deviations are put the Data field and the |
---|
28 | % BPMx and BPMz data are put in the RawData field. |
---|
29 | % 3. FileName = Filename (including directory) where the data was saved (if applicable) |
---|
30 | % |
---|
31 | % NOTE |
---|
32 | % 1. All inputs are optional. All of the following have the same output: |
---|
33 | % measbpmsigma(0:.5:180); |
---|
34 | % measbpmsigma(0:.5:180, 'BPMx'); |
---|
35 | % measbpmsigma('BPMx'); |
---|
36 | % measbpmsigma('BPMx', 'BPMz'); |
---|
37 | % measbpmsigma('BPMx', [], 'BPMz'); |
---|
38 | % 2. To make the measured sigma the default: |
---|
39 | % First divide by the sqrt(BPMxSigma.NumberOfAverages) |
---|
40 | % BPMxSigma.Data = BPMxSigma.Data / BPMxSigma.NumberOfAverages; |
---|
41 | % BPMzSigma.Data = BPMzSigma.Data / BPMzSigma.NumberOfAverages; |
---|
42 | % [BPMxSigma, BPMzSigma] = measbpmsigma('Struct'); |
---|
43 | % setphysdata(BPMxSigma,'Sigma'); |
---|
44 | % setphysdata(BPMzSigma,'Sigma'); |
---|
45 | % 3. Use getsigma to get the default values |
---|
46 | |
---|
47 | % |
---|
48 | % Written by Gregory J. Portmann |
---|
49 | % Modified by Laurent S. Nadolski |
---|
50 | |
---|
51 | % Defaults |
---|
52 | BPMxFamily = 'BPMx'; |
---|
53 | BPMzFamily = 'BPMz'; |
---|
54 | BPMxList = []; |
---|
55 | BPMzList = []; |
---|
56 | T = 3*60; %total monitoring time |
---|
57 | Tsample = .5; % 2Hz by default |
---|
58 | t = []; % t= 0:Tsample:T |
---|
59 | FileName = []; %Filename for archiving |
---|
60 | ArchiveFlag = 1; %Archive by default |
---|
61 | StructOutputFlag = 0; |
---|
62 | OutputFileName = ''; |
---|
63 | Navg = getbpmaverages; % number of averages |
---|
64 | |
---|
65 | % Look if 'struct' or 'numeric' in on the input line |
---|
66 | for i = length(varargin):-1:1 |
---|
67 | if strcmpi(varargin{i},'Struct') |
---|
68 | StructOutputFlag = 1; |
---|
69 | varargin(i) = []; |
---|
70 | elseif strcmpi(varargin{i},'Numeric') |
---|
71 | StructOutputFlag = 0; |
---|
72 | varargin(i) = []; |
---|
73 | elseif strcmpi(varargin{i},'Archive') |
---|
74 | ArchiveFlag = 1; |
---|
75 | varargin(i) = []; |
---|
76 | elseif strcmpi(varargin{i},'NoArchive') |
---|
77 | ArchiveFlag = 0; |
---|
78 | varargin(i) = []; |
---|
79 | end |
---|
80 | end |
---|
81 | |
---|
82 | % t input |
---|
83 | if length(varargin) >= 1 |
---|
84 | if isnumeric(varargin{1}) |
---|
85 | t = varargin{1}; |
---|
86 | varargin(1) = []; |
---|
87 | end |
---|
88 | end |
---|
89 | |
---|
90 | % Look for BPMx family info |
---|
91 | if length(varargin) >= 1 |
---|
92 | if ischar(varargin{1}) |
---|
93 | BPMxFamily = varargin{1}; |
---|
94 | varargin(1) = []; |
---|
95 | if length(varargin) >= 1 |
---|
96 | if isnumeric(varargin{1}) |
---|
97 | BPMxList = varargin{1}; |
---|
98 | varargin(1) = []; |
---|
99 | end |
---|
100 | end |
---|
101 | else |
---|
102 | if isnumeric(varargin{1}) |
---|
103 | BPMxList = varargin{1}; |
---|
104 | varargin(1) = []; |
---|
105 | end |
---|
106 | end |
---|
107 | end |
---|
108 | |
---|
109 | % Look for BPMz family info |
---|
110 | if length(varargin) >= 1 |
---|
111 | if ischar(varargin{1}) |
---|
112 | BPMzFamily = varargin{1}; |
---|
113 | varargin(1) = []; |
---|
114 | if length(varargin) >= 1 |
---|
115 | if isnumeric(varargin{1}) |
---|
116 | BPMzList = varargin{1}; |
---|
117 | varargin(1) = []; |
---|
118 | end |
---|
119 | end |
---|
120 | else |
---|
121 | if isnumeric(varargin{1}) |
---|
122 | BPMzList = varargin{1}; |
---|
123 | varargin(1) = []; |
---|
124 | end |
---|
125 | end |
---|
126 | end |
---|
127 | |
---|
128 | % Look for FileName info |
---|
129 | if length(varargin) >= 1 |
---|
130 | if ischar(varargin{1}) |
---|
131 | FileName = varargin{1}; |
---|
132 | end |
---|
133 | end |
---|
134 | |
---|
135 | if isempty(t) |
---|
136 | t = 0:Tsample:T; |
---|
137 | end |
---|
138 | |
---|
139 | % Make a row vector |
---|
140 | t = t(:)'; |
---|
141 | |
---|
142 | % If scalar, create a vector |
---|
143 | if length(t) == 1 |
---|
144 | t = 0:Tsample:t; |
---|
145 | end |
---|
146 | |
---|
147 | %---------------------------Start of process |
---|
148 | fprintf(' Monitoring orbit and current for %.1f seconds\n', t(end)); drawnow; |
---|
149 | |
---|
150 | if isfamily('DCCT') |
---|
151 | AM = getam({BPMxFamily, BPMzFamily, 'DCCT'}, {BPMxList, BPMzList,[]}, t, 'struct'); |
---|
152 | DCCT = AM{3}; |
---|
153 | else |
---|
154 | AM = getam({BPMxFamily, BPMzFamily}, {BPMxList, BPMzList}, t, 'struct'); |
---|
155 | DCCT = []; |
---|
156 | end |
---|
157 | BPM(1) = AM{1}; |
---|
158 | BPM(2) = AM{2}; |
---|
159 | |
---|
160 | BPM(1).NumberOfAverages = Navg; |
---|
161 | BPM(1).RawData = BPM(1).Data; |
---|
162 | BPM(1).DCCT = DCCT; |
---|
163 | BPM(1).CreatedBy = 'measbpmsigma'; |
---|
164 | BPM(1).DataDescriptor = 'Standard Deviation'; |
---|
165 | |
---|
166 | BPM(2).NumberOfAverages = Navg; |
---|
167 | BPM(2).RawData = BPM(2).Data; |
---|
168 | BPM(2).DCCT = DCCT; |
---|
169 | BPM(2).CreatedBy = 'measbpmsigma'; |
---|
170 | BPM(2).DataDescriptor = 'Standard Deviation'; |
---|
171 | |
---|
172 | |
---|
173 | tout = BPM(1).tout; |
---|
174 | Mx = BPM(1).Data; |
---|
175 | % retrieve first value |
---|
176 | for i = 1:size(Mx,2) |
---|
177 | Mx(:,i) = Mx(:,i) - BPM(1).Data(:,1); |
---|
178 | end |
---|
179 | |
---|
180 | clf reset |
---|
181 | subplot(2,2,1); |
---|
182 | plot(tout, Mx); |
---|
183 | grid on; |
---|
184 | %title(sprintf('BPM Data (%s)', datestr(BPM(1).TimeStamp))) |
---|
185 | xlabel('Time [Seconds]'); |
---|
186 | ylabel('Horizontal Position [mm]'); |
---|
187 | |
---|
188 | tout = BPM(2).tout; |
---|
189 | My = BPM(2).Data; |
---|
190 | for i = 1:size(My,2) |
---|
191 | My(:,i) = My(:,i) - BPM(2).Data(:,1); |
---|
192 | end |
---|
193 | |
---|
194 | subplot(2,2,3); |
---|
195 | plot(tout, My); |
---|
196 | grid on; |
---|
197 | xlabel('Time [Seconds]'); |
---|
198 | ylabel('Vertical Position [mm]'); |
---|
199 | |
---|
200 | % Warn if the measurement did not keep in time step with t |
---|
201 | tmeas = tout-t; |
---|
202 | if any(tmeas(1:end-1) > 1.05*diff(t)) |
---|
203 | fprintf(' WARNING: The time allotted for getting data is too small\n'); |
---|
204 | end |
---|
205 | |
---|
206 | % Change the definition of .data to the standard deviation |
---|
207 | BPM(1).t = BPM(1).t(1); |
---|
208 | BPM(1).tout = BPM(1).tout(1); |
---|
209 | BPMx = BPM(1).Data; |
---|
210 | BPMz = BPM(2).Data; |
---|
211 | |
---|
212 | % Compute the standard deviation |
---|
213 | if 1 |
---|
214 | % Definition of standard deviations |
---|
215 | BPM(1).Data = std(BPM(1).Data,0,2); |
---|
216 | BPM(2).Data = std(BPM(2).Data,0,2); |
---|
217 | else |
---|
218 | % Low frequency drifting increases the STD. For many purposes, like LOCO, |
---|
219 | % this is not desireable. Using difference orbits mitigates the drift problem. |
---|
220 | Mx = BPMx; |
---|
221 | for i = 1:size(Mx,2)-1 |
---|
222 | Mx(:,i) = Mx(:,i+1) - Mx(:,i); |
---|
223 | end |
---|
224 | Mx(:,end) = []; |
---|
225 | |
---|
226 | My = BPMz; |
---|
227 | for i = 1:size(My,2)-1 |
---|
228 | My(:,i) = My(:,i+1) - My(:,i); |
---|
229 | end |
---|
230 | My(:,end) = []; |
---|
231 | |
---|
232 | BPM(1).Data = std(Mx,0,2) / sqrt(2); % sqrt(2) comes from substracting 2 random variables |
---|
233 | BPM(2).Data = std(My,0,2) / sqrt(2); |
---|
234 | |
---|
235 | end |
---|
236 | |
---|
237 | subplot(2,2,2); |
---|
238 | plot(BPM(1).Data); |
---|
239 | grid on; |
---|
240 | xlabel('BPM Number'); |
---|
241 | ylabel('Horizontal STD [mm]'); |
---|
242 | |
---|
243 | subplot(2,2,4); |
---|
244 | plot(BPM(2).Data); |
---|
245 | grid on; |
---|
246 | xlabel('BPM Number'); |
---|
247 | ylabel('Vertical STD [mm]'); |
---|
248 | addlabel(.5,1,sprintf('BPM Data (%s)', datestr(BPM(1).TimeStamp)), 10); |
---|
249 | orient landscape |
---|
250 | |
---|
251 | if ArchiveFlag | ~isempty(FileName) |
---|
252 | if ~isempty(FileName) |
---|
253 | FileNameArchive = FileName; |
---|
254 | else |
---|
255 | FileNameArchive = appendtimestamp('BPMSigma', BPM(1).TimeStamp); |
---|
256 | DirectoryName = getfamilydata('Directory','BPMData'); |
---|
257 | FileNameArchive = [DirectoryName FileNameArchive]; |
---|
258 | end |
---|
259 | fprintf(' BPM sigma data structure saved to %s.mat\n', FileNameArchive); |
---|
260 | BPMxSigma = BPM(1); |
---|
261 | BPMzSigma = BPM(2); |
---|
262 | save(FileNameArchive, 'BPMxSigma', 'BPMzSigma'); |
---|
263 | %save(FileNameArchive, 'BPM'); |
---|
264 | else |
---|
265 | FileNameArchive = []; |
---|
266 | end |
---|
267 | |
---|
268 | if StructOutputFlag |
---|
269 | % Output variables |
---|
270 | varargout{1} = BPM(1); |
---|
271 | varargout{2} = BPM(2); |
---|
272 | varargout{3} = FileNameArchive; |
---|
273 | else |
---|
274 | % Output variables |
---|
275 | varargout{1} = BPM(1).Data; |
---|
276 | varargout{2} = BPM(2).Data; |
---|
277 | varargout{3} = BPMx; |
---|
278 | varargout{4} = BPMz; |
---|
279 | varargout{5} = tout; |
---|
280 | varargout{6} = DCCT; |
---|
281 | varargout{7} = FileNameArchive; |
---|
282 | end |
---|