source: MML/trunk/mml/measlifetime.m @ 4

Last change on this file since 4 was 4, checked in by zhangj, 11 years ago

Initial import--MML version from SOLEIL@2013

File size: 7.6 KB
Line 
1function [Tau, I0, t, DCCT, chi2n] = measlifetime(varargin)
2%MEASLIFETIME - Measures the lifetime using an exponential or linear least squares fit to beam current
3%  [Tau, I0, t, DCCT] = measlifetime(t, DCCT)
4%  [Tau, I0, t, DCCT] = measlifetime(t)
5%  [Tau, I0, t, DCCT] = measlifetime
6%
7%  INPUTS #1 - t is a vector or positive scalar
8%  1. t    = a.  If vector, time [seconds] (vector input)
9%            b.  If scalar and t > 0, length of time in seconds to measure current
10%                Default sample period is .5 seconds.
11%  2. DCCT = current vector [mAmps]
12%            if the DCCT vector is empty then this function will
13%            get the current using getdcct at the times defined in t
14%  3. Method - 'Exponential' for exponential least square fit
15%            - 'Linear' for linear least square fit
16%
17%     or
18%
19%  [Tau, I0, t, DCCT] = measlifetime(DCCT_Drop, Tmax, Tmin, Nmin)
20%
21%  INPUTS #2 - "t" is negative
22%  1. DCCT_Drop - If DCCT_Drop is scalar and DCCT_Drop <= 0, then the beam current will be
23%                 monitored until the current is DCCT_Drop.  Default sample period is .5 seconds.
24%                 Default:  Monitor the beam current until current drops 60 uA
25%                           (At Spear sigma(DCCT) = 0.001 mA)
26%  2. Tmax - Maximum time to measure DCCT {Default: inf}
27%  3. Tmin - Minimum time to measure DCCT {Default: 0}
28%  4. Nmin - Minimum number of unique data points when monitoring DCCT drop {Default: 6}
29
30%     The goal is to measure the current until a current drop of DCCT_Drop is achived.  However, the
31%     time that takes will never goes above Tmax.  And if DCCT_Drop is achived then the measurement will
32%     continue until Tmin or Nmin points is achieved (but not exceeding Tmax).
33%
34%
35%  OUTPUTS
36%  DCCTfit = I0 * exp(-t/Tau); Exponential
37%  DCCTfit = I0 * (1-t/Tau);   Linear
38%  1. Tau  - Computed lifetime   [hours]
39%  2. I0   - Computed            [mAmps]
40%  3. DCCT - Beam current vector [mAmps]
41%  4. t    - Actual time         [Seconds]
42%  5. chi2n - normalized chi2
43%
44%
45%  NOTES
46%  1. If no output exists, the beam current and fit will be plotted to the screen
47%     as well as the residual of the DCCT.
48%  2. DCCT is assumed to be in mAmps
49
50%
51%  Written by Gregory S. Portmann
52%  Modified by Amor Nadji, May 2005
53%  Adapted by Laurent S. Nadolski, 23/01/06
54%  Added covariance calculation
55
56% Default method if not user given
57MethodFlag = 'Exponential';
58sigma = 0.2e-6; % Error in dcct measure in Amps for 7 read per second config
59
60% Parser for method
61for i = length(varargin):-1:1
62    if strcmpi(varargin{i},'Linear')
63        MethodFlag = 'Linear';
64        varargin(i) = [];
65    elseif strcmpi(varargin{i},'Exponential')
66        MethodFlag = 'Exponential';
67        varargin(i) = [];
68    end
69end
70
71T_Seconds = .5;     % Default sample period [Seconds]
72TmaxDefault = inf;  % Maximum time
73TminDefault = 0;    % Minimum time
74NminDefault = 6;    % Minimum number of data points
75
76
77% Input parsing
78Tmax = [];
79if length(varargin) == 0
80    MonitorFlag = 2;
81    deltaDCCT = 60 * 0.001;
82    Tmin = TminDefault;
83    Tmax = TmaxDefault;
84    Nmin = NminDefault;
85   
86elseif length(varargin) >= 1
87    t = varargin{1};
88    if all(size(t)==[1 1])
89        if t > 0
90            MonitorFlag = 1;
91            t = 0:T_Seconds:t;
92            Tmax = TmaxDefault;
93        else
94            MonitorFlag = 2;
95            deltaDCCT = abs(t);
96            if length(varargin) >= 2
97                Tmax = DCCT;
98            else
99                Tmax = [];
100            end
101        end
102        if length(varargin) < 3
103            Tmin = [];
104        end
105        if length(varargin) < 4
106            Nmin = [];
107        end
108        if isempty(Tmax)
109            Tmax = TmaxDefault;
110        end
111        if isempty(Tmin)
112            Tmin = TminDefault;
113        end
114        if isempty(Nmin)
115            Nmin = NminDefault;
116        end
117    else
118        % Time vector input
119        if length(varargin) < 2
120            MonitorFlag = 1;
121        else
122            MonitorFlag = 0;
123        end
124    end
125end
126
127
128if MonitorFlag == 1
129   
130    % Get DCCT data at a fix interval determined by the input vector t
131    %disp(['   Monitoring beam current for ', num2str(t(length(t))), ' seconds.']);   
132    t0 = gettime;
133    for j = 1:length(t)
134        T = t(j) - (gettime-t0);
135        if T > 0
136            pause(T);
137        end
138        tout(j,1) = gettime - t0;   
139        DCCT(j,1) = getdcct;
140    end
141   
142elseif MonitorFlag == 2
143   
144    % Monitor for a fixed DCCT drop
145    %disp(['   Monitoring beam current until current drops by more than ', num2str(deltaDCCT), ' mA.']);   
146    j = 1;
147    n = 1;
148    tout(n,1) = 0;
149    DCCT(n,1) = getdcct;
150    t0 = gettime;
151    t0_Display = 0;
152    while ((abs(DCCT(end,1)-DCCT(1,1)) < deltaDCCT) & (DCCT(end,1) > 0.1)) | ...
153            n < Nmin | (gettime-t0) < Tmin
154        j = j+1;
155        T = (j-1)*T_Seconds - (gettime-t0);
156        if T > 0
157            pause(T);
158        end
159        DCCTnew = getdcct;
160        if DCCTnew ~= DCCT(n)
161            n = n + 1;
162            tout(n,1) = gettime - t0;
163            DCCT(n,1) = DCCTnew;
164        end
165        if gettime-t0 > Tmax
166            break;
167        end
168        if gettime-t0_Display > 10   
169            fprintf('   Monitoring DCCT for lifetime measurement (%s)\n', ...
170                datestr(clock,0));
171            t0_Display = gettime;
172        end
173    end
174    t = tout;
175end
176
177% Column vectors
178DCCT = DCCT(:);
179t = t(:);
180
181
182% Lookfor identical data in DCCT.  Some machine don't update at T_Sample and
183% having the same reading twice is probably not so good for the LS fit.
184iExtra = find(diff(DCCT)==0);
185DCCT(iExtra) = [];
186t(iExtra) = [];
187
188if length(DCCT) < 2
189    Tau = NaN;
190    I0 = NaN;
191    fprintf('   Only 1 unique DCCT reading, hence Tau is set to NaN.\n');
192    %error('There must be at least 2 unique point to fit a lifetime.');
193    return
194end
195   
196   
197% LS fit
198if strcmpi(MethodFlag,'Exponential')
199    y = log(DCCT);
200else
201    y = DCCT;
202end
203
204X = [ones(size(t)) t];
205
206% Linear Least square fit
207invXX = inv(X'*X);
208B = invXX*X'*y  ;   
209
210if strcmpi(MethodFlag,'Exponential')
211    % yfit = exp(B(1))*exp(B(2)*tfit);
212    I0 = exp(B(1));
213    Tau = -1/B(2)/3600;    % In hours
214    yfit = exp(B(1))*exp(B(2)*t);
215    stitle = 'Least Squares Fit : I0*exp(-t/tau)';
216else
217    % yfit = B(1) + B(2)*tfit;
218    I0 = B(1) ;
219    Tau = -B(1)/B(2)/60/60;    % In hours
220    yfit = B(1) + B(2)*t;
221    stitle = 'Least Squares Fit : I0*(1 - t/tau)';
222end
223
224%% Erreur
225%chi2 = 1/(sigma*sigma)*sum(power(DCCT - yfit,2));
226chi2 = 1/(sigma*sigma)*sum(power(y - B(1)-B(2)*t,2));
227% Normalized chi2
228chi2n = chi2/(length(t)-length(B));
229% Covariance matrix
230Mcovariance = chi2n*invXX;
231
232if strcmpi(MethodFlag,'Exponential')
233    dtau = sqrt(Mcovariance(2,2)/B(2)/B(2))/3600;
234else
235    dtau = sqrt(Mcovariance(1,1)/B(2)/B(2) + ...
236        power(B(1)/B(2)/B(2),2)*Mcovariance(2,2) - ...
237        2*B(1)/power(B(2),3)*Mcovariance(2,1));
238end
239
240if isnan(Tau) || chi2n > 5
241    fprintf('   Life time measurement is inaccurate!\n');
242end
243
244if nargout == 0   
245       
246    clf reset
247    subplot(2,1,1)
248    plot(t,DCCT,'o-b', t,yfit,'--r'); hold on;
249    % errorbar(t,DCCT,sigma*ones(size(DCCT)),'.b'); hold off
250    title(sprintf('Beam Current vs Time: Lifetime= %2.2f +/- %2.2f (h) with chi2n = %2.2g', Tau, dtau, chi2n))
251%    title(sprintf('Beam Current vs Time: Lifetime= %2.2f (h) with chi2n = %2.2g', Tau, chi2n))
252    xlabel('Time [seconds]');
253    ylabel('Beam Current [mA]');
254    legend('Measured Beam Current',stitle,0);
255    grid on;
256    xlim([t(1) t(end)]);
257
258    subplot(2,1,2)
259    plot(t,DCCT-yfit);
260    title(['Residual Error (RMS = ' num2str(std(DCCT-yfit),'%.2g') ' mA)']);
261    xlabel('Time [seconds]');
262    ylabel('Lifetime Corrected Beam Current Variation');
263    grid on;
264   
265    addlabel(1,0, datestr(clock,0));
266end
Note: See TracBrowser for help on using the repository browser.