FINDRF1 - Finds the RF frequency that minimizes the horizonal dispersion [DeltaRF, RFnew, frf] = findrf1(DeltaRFvec, BPMFamily, BPMList, FileName) INPUTS 1. DeltaRFvec - Vector of RF changes {Default or empty: [-.2% -.1% 0% .1% .2%] energy change} 2. BPMFamily - Family name {Default or empty: 'BPMx'} 3. BPMList - Device or element list of BPMs {Default or empty: all} 4. FileName to save data (data in structure 'frf') {Default or empty: don't save data} OUTPUTS 1. DeltaRF - Change in RF 2. RFnew - Zero crossing of the fit 3. frf - RF structure containing all the measurement data NOTES 1. The RF frequency that minimized the dispersion maybe not be the optimal RF frequency. The ALS found that choosing an RF frequency which minimizes the energy shift due to the horizontal corrector magnets to be a more robust and repeatable solution. 2. This function measures the dispersion, hence changes the RF frequency! rmdisp will basically do the same thing as findrf1 without changing the RF frequency. However, findrf1 gives more control over the fit range for the RF change and allows for more noise reduction in the fit. That said, rmdisp is usually just fine to use. 3. Beware of the magnitude of DeltaRFvec for nonlinear accelerators. See also findrf, rmdisp, plotcm
0001 function [DeltaRF, RFnew, frf] = findrf1(DeltaRF, BPMFamily, BPMList, FileName) 0002 %FINDRF1 - Finds the RF frequency that minimizes the horizonal dispersion 0003 % [DeltaRF, RFnew, frf] = findrf1(DeltaRFvec, BPMFamily, BPMList, FileName) 0004 % 0005 % INPUTS 0006 % 1. DeltaRFvec - Vector of RF changes {Default or empty: [-.2% -.1% 0% .1% .2%] energy change} 0007 % 2. BPMFamily - Family name {Default or empty: 'BPMx'} 0008 % 3. BPMList - Device or element list of BPMs {Default or empty: all} 0009 % 4. FileName to save data (data in structure 'frf') {Default or empty: don't save data} 0010 % 0011 % OUTPUTS 0012 % 1. DeltaRF - Change in RF 0013 % 2. RFnew - Zero crossing of the fit 0014 % 3. frf - RF structure containing all the measurement data 0015 % 0016 % NOTES 0017 % 1. The RF frequency that minimized the dispersion maybe not be the 0018 % optimal RF frequency. The ALS found that choosing an RF frequency which 0019 % minimizes the energy shift due to the horizontal corrector magnets 0020 % to be a more robust and repeatable solution. 0021 % 2. This function measures the dispersion, hence changes the RF frequency! 0022 % rmdisp will basically do the same thing as findrf1 without changing the 0023 % RF frequency. However, findrf1 gives more control over the fit range for 0024 % the RF change and allows for more noise reduction in the fit. That said, 0025 % rmdisp is usually just fine to use. 0026 % 3. Beware of the magnitude of DeltaRFvec for nonlinear accelerators. 0027 % 0028 % See also findrf, rmdisp, plotcm 0029 0030 % Written by Greg Portmann 0031 0032 0033 ChangeRFFlag = 1; 0034 DisplayFlag = 1; 0035 0036 0037 % Starting Point 0038 RF0 = getrf('Struct'); 0039 0040 0041 if nargin < 1 0042 DeltaRF = []; 0043 end 0044 if isempty(DeltaRF) 0045 %DeltaRF = getfamilydata('DeltaRFChro'); 0046 if isempty(DeltaRF) 0047 DeltaRF = getrf * getmcf * [-.002 -.001 0 .001 .002]; % .2% energy change 0048 %DeltaRF = getrf * getmcf * [-.006 -.003 0 .003 .006]; % .6% energy change 0049 %DeltaRF = [-400 -200 0 200 400]; % Hz 0050 %if strcmpi(RF0.UnitsString, 'Hz') 0051 %elseif strcmpi(RF0.UnitsString, 'MHz') 0052 % DeltaRF = DeltaRF * 1e-6; % MHz 0053 %else 0054 % error('RF units unknown, hence default input frequency cannot be choosen.'); 0055 %end 0056 end 0057 end 0058 if nargin < 2 0059 BPMFamily = ''; 0060 end 0061 if isempty(BPMFamily) 0062 BPMFamily = 'BPMx'; 0063 end 0064 if nargin < 3 0065 BPMList = family2dev(BPMFamily); 0066 end 0067 if isempty(BPMList) 0068 BPMList = family2dev(BPMFamily); 0069 end 0070 if nargin < 4 0071 FileName = []; 0072 end 0073 0074 0075 BPMDelay = getfamilydata('BPMDelay'); 0076 if isempty(BPMDelay) 0077 BPMDelay = 0; 0078 end 0079 0080 0081 Xoffset = getoffset(BPMFamily, BPMList); 0082 Xgolden = getgolden(BPMFamily, BPMList); 0083 0084 0085 % Get Dispersion 0086 Dx = measdisp([], BPMFamily, BPMList); 0087 0088 0089 for i = 1:length(DeltaRF) 0090 fprintf(' %d. Setting RF to %f [%s] \n', i, RF0.Data + DeltaRF(i), RF0.UnitsString); 0091 setrf(RF0.Data + DeltaRF(i)); 0092 sleep(BPMDelay); 0093 x(:,i) = getam(BPMFamily, BPMList) - Xoffset; 0094 rf(1,i) = getrf; 0095 end 0096 0097 0098 % Set RF back to starting point 0099 setrf(RF0); 0100 0101 0102 % Find LS fit to the line 0103 y = x' * Dx; % Dot product of Dx and the X orbit 0104 X = [ones(max(size(rf)),1) rf']; 0105 %b = inv(X'*X)*X'*y; 0106 b = X \ y; 0107 RFnew = -b(1) / b(2); 0108 0109 0110 rf1 = linspace(rf(1),rf(max(size(rf))),100); 0111 yfit = b(1) + b(2)*rf1; 0112 0113 DeltaRF = RFnew - RF0.Data; 0114 0115 0116 %figure 0117 clf reset 0118 plot(rf1,yfit, 'b', rf,y,'og', RFnew,0,'xr'); 0119 grid on 0120 xlabel('RF Frequency [MHz]'); 0121 ylabel('Dot product of Dx and Hor. Orbit'); 0122 title(sprintf('FIT: %g + %g * RF, \\DeltaRF = %g', b(1), b(2), DeltaRF)); 0123 0124 0125 0126 % Set the RF frequency 0127 if ChangeRFFlag 0128 % fprintf('\n Starting RF = %f [%s]\n', RF0.Data, RF0.UnitsString); 0129 % fprintf(' Zero crossing of the RF = %f Delta RF = %g [%s]\n', RFnew, DeltaRF, RF0.UnitsString); 0130 % answer = input(' Do you want to set the RF frequence now (n/y)? ','s'); 0131 % if strcmp(answer, 'y') == 1 0132 % setrf(RFnew); 0133 % fprintf(' New RF frequency = %f [%s]\n', getrf, RF0.UnitsString); 0134 % fprintf(' Measurement complete.\n'); 0135 % else 0136 % fprintf(' Measurement complete. No change to the RF frequency.\n'); 0137 % end 0138 if ~isempty(DeltaRF) 0139 if DisplayFlag 0140 answer = inputdlg({strvcat(strvcat(strvcat(sprintf('Recommend new RF Freqenecy is %g %s', RFnew, RF0.UnitsString), sprintf('Delta RF is %g %s', DeltaRF, RF0.UnitsString)), ' '),'Change the RF frequency?')},'FINDFR1',1,{sprintf('%g',DeltaRF)}); 0141 if isempty(answer) 0142 fprintf(' No change was made to the RF frequency.\n'); 0143 return 0144 end 0145 DeltaRF = str2num(answer{1}); 0146 end 0147 steprf(DeltaRF); 0148 if DisplayFlag 0149 fprintf(' RF frequency change by %f %s.\n', DeltaRF, RFUnitsString); 0150 end 0151 else 0152 error('RF frequency not changed because of a calculation problem.'); 0153 end 0154 end 0155 0156 0157 if ~isempty(FileName) %save file 0158 frf.TimeStamp = clock; 0159 frf.CreatedBy = 'findrf'; 0160 frf.FileName = FileName; 0161 frf.x = x; 0162 frf.rf = rf; 0163 frf.Dx = Dx; 0164 frf.RF0 = RF0; 0165 frf.RFnew = RFnew; 0166 frf.DeltaRF = DeltaRF; 0167 frf.BPMFamily = BPMFamily; 0168 frf.BPMList = BPMList; 0169 frf.Xgolden = Xgolden; 0170 frf.Xoffset = Xoffset; 0171 save(FileName, 'frf'); 0172 fprintf(' Data saved to %s.mat in directory %s.\n', FileName, pwd); 0173 end 0174 0175 0176