source: MML/trunk/machine/SOLEIL/StorageRing/bpm/anabetaturnbyturn.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: 6.2 KB
Line 
1function [bxmeas bzmeas nux nuz] = anabetaturnbyturn(varargin)
2% ANABETATURNBYTURN - Compute beta function at BPM location using turn by turn data
3%
4%  INPUTS
5%  1. Number of turn - Default and maximum value is depth read on BPM
6%  Optional input arguments
7%  1. Optional display
8%     'Display'   - Plot the beta function {Default if no outputs exist}
9%     'NoDisplay' - Bpmdata will not be plotted {Default if outputs exist}
10%  2. 'File'      - Get from File (interactive)
11%                 - if Filename given as a second argument, open the specified file
12%  3. 'Ref'       - Comparison with a Reference File
13%
14%  OUTPUTS
15%  1. bxmeas - Horizontal beta funtion at BPM location
16%  2. bzmeas - Vertical beta funtion at BPM location
17%  3. nux - H-Tune determined by FFT
18%  4. nuz - V-Tune determined by FFT
19%
20%  See Also getbpmrawdata, findfreq, convertBPMData2CERNformat
21
22%
23% Written by Laurent S. Nadolski
24
25FileFlag = 0;   % Get data from a file
26ReferenceFlag = 1;
27DisplayFlag = 1;
28FileName = '';
29imax = 1026; % Default number of turn
30
31
32% if no output variable, display results
33if nargout > 0
34    DisplayFlag = 0;
35end
36
37for i = length(varargin):-1:1
38    if strcmpi(varargin{i},'Display')
39        DisplayFlag = 1;
40        varargin(i) = [];
41    elseif strcmpi(varargin{i},'NoDisplay')
42        DisplayFlag = 0;
43        varargin(i) = [];
44    elseif strcmpi(varargin{i},'Ref')
45        ReferenceFlag = 1;
46        varargin(i) = [];
47    elseif strcmpi(varargin{i},'NoRef')
48        ReferenceFlag = 0;
49        varargin(i) = [];
50    elseif strcmpi(varargin{i},'File')
51        FileFlag = 1;
52        if length(varargin) > i
53            % Look for a filename as the next input
54            if ischar(varargin{i+1})
55                FileName = varargin{i+1};
56                varargin(i+1) = [];
57                [pathstr,name,ext] = fileparts(FileName);
58                if isempty(ext)
59                    FileName = [FileName, '.mat'];
60                end
61            end
62        end
63        varargin(i) = [];
64    elseif strcmpi(varargin{i},'NoArchive')
65        ArchiveFlag = 0;
66        varargin(i) = [];
67    elseif strcmpi(varargin{i},'Struct')
68        StructureFlag = 1;
69        varargin(i) = [];
70    end
71end
72
73% Check if data depth (number of turns) is provided by user
74if ~isempty(varargin)
75    imax = varargin{1};
76end
77% Get File by asking users
78   
79if FileFlag
80    if ~isempty(FileName) && exist(FileName, 'file')
81        a = load(FileName);
82        AM = getfield(a, 'AM');
83    else
84        DirectoryName = getfamilydata('Directory','BPMData');
85        pwd_old = pwd;
86        cd(DirectoryName);
87        [Filename PathName] = uigetfile('BPMTurnByTurn*');
88        cd(pwd_old);
89        if  isequal(FileName,0)
90            disp('User pressed cancel')
91            exit(0);
92        else
93            a = load(fullfile(PathName, Filename));
94            AM = getfield(a, 'AM');
95        end
96    end
97else % Online data
98    AM = getbpmrawdata('Struct');
99end
100
101istart = 15;
102iend = min(istart + imax - 1, size(AM.Data.X, 2));
103Xpos = AM.Data.X(:,istart:iend)';
104Zpos = AM.Data.Z(:,istart:iend)';
105
106if DisplayFlag
107    figure
108    subplot(2,1,1)
109    plot(Xpos-repmat(mean(Xpos), size(Xpos,1),1))
110    ylabel('X [mm]')
111    subplot(2,1,2)
112    plot(Zpos-repmat(mean(Zpos), size(Zpos,1),1))
113    ylabel('Z [mm]')
114    xlabel('Turn number');
115    suptitle('Turn by turn data')
116end
117%%
118%refreshthering;
119%steptune([0.005 0.0125],'Model');
120[bxm bzm] = modelbeta('BPMx');
121%%
122ind = (1:120); % All BPMs
123[nux nuz ampx ampz] = findfreq(Xpos(:,ind),Zpos(:,ind));
124
125% fit average beta values to model
126factx  = mean(bxm)/mean(ampx.*ampx);
127factz  = mean(bzm)/mean(ampz.*ampz);
128bxmeas = (ampx.*ampx*factx)';
129bzmeas = (ampz.*ampz*factz)';
130
131if DisplayFlag   
132    figure
133    av = getspos('BPMx');
134    %av = [av(1:30); av(1:30); av(1:30); av(1:30)];
135    h1 = subplot(2,1,1);
136    plot(av,bxmeas,'b*-',av,bxm,'k.-'); hold on;
137    ylabel('betax [m]'); grid on;
138    h2 = subplot(2,1,2);
139    plot(av,bzmeas,'b*-',av,bzm,'k.-')
140    linkaxes([h1 h2], 'x');
141    grid on; ylabel('betaz [m]'); xlabel('s [m]');
142    suptitle('Beta function fitted at BPM position from turn by turn data (blue stars) and theory (black)')
143    %tmp = bxmeas(74);  bxmeas(74) = bxmeas(75) ; bxmeas(75) = tmp;
144    addlabel(sprintf('H-betabeat %3.2f %% rms V-betabeat %3.2f %% rms               %s', ...
145        std((bxmeas - bxm)./bxm)*100,std((bzmeas - bzm)./bzm)*100,AM.TimeStamp));
146
147    figure
148    av = getspos('BPMx');
149    h1 = subplot(5,1,[1 2]);
150    plot(av,nux,'b*-'); hold on;
151    ylabel('nux'); grid on;
152    yaxis([.205 .215])
153    h2 = subplot(5,1,3);
154    drawlattice;
155    h3 = subplot(5,1,[4 5]);
156    plot(av,nuz,'b*-')
157    linkaxes([h1 h2 h3], 'x');
158    grid on; ylabel('nuz'); xlabel('s [m]');
159    yaxis([.315 .325])
160    suptitle(sprintf('Tunes at BPM positions from turn by turn data over %d turns', imax));
161    %tmp = bxmeas(74);  bxmeas(74) = bxmeas(75) ; bxmeas(75) = tmp;
162    addlabel(sprintf('H-betabeat %3.2f %% rms V-betabeat %3.2f %% rms               %s', ...
163        std((bxmeas - bxm)./bxm)*100,std((bzmeas - bzm)./bzm)*100,AM.TimeStamp));
164
165   
166    figure
167    h1 = subplot(5,1,[1 2]);
168    plot(av,(bxmeas-bxm)./bxm*100,'k.-'); hold on;
169    ylabel('betax beating [%]'); grid on;
170    yaxis([-20 20])
171    h2 = subplot(5,1,3);
172    drawlattice;
173    h3 = subplot(5,1,[4 5]);
174    plot(av,(bzmeas-bzm)./bzm*100,'k.-'); hold on;
175    linkaxes([h1 h2 h3], 'x');
176    set([h1 h2 h3],'XGrid','On','YGrid','On');
177    yaxis([-20 20])
178    grid on; ylabel('betaz beating [%]'); xlabel('s [m]');
179    suptitle('Beta beatingfitted at BPM position from turn by turn data (black stars) and reference (red)')
180   
181    addlabel(sprintf('H-betabeat %3.2f %% rms V-betabeat %3.2f %% rms               %s', ...
182        std((bxmeas - bxm)./bxm)*100,std((bzmeas - bzm)./bzm)*100,AM.TimeStamp));
183
184    if ReferenceFlag
185        RefDirName = getfamilydata('Directory', 'BPMData');
186        RefFileName = 'BPMTurnByTurn_2009-03-28_09-32-07_betabeatref.mat';
187        [Refbxmeas Refbzmeas Refnux Refnuz] = anabetaturnbyturn(imax, 'File', fullfile(RefDirName,RefFileName));
188        plot(h1, av, (Refbxmeas-bxm)./bxm*100, 'r.-')
189        plot(h3, av, (Refbzmeas-bzm)./bzm*100, 'r.-')
190    end
191    %IDList = getidState;
192    %fprintf('Measurement: I= %4.1f A nux=%5.4f nuz=%5.4f\n',     IDList.current, IDList.tunes)
193end
194
Note: See TracBrowser for help on using the repository browser.