MULTITURNFFT - Takes the FFT of multiple turn data (plots results if no output exists) [X, f] = multiturnfft(x, LineType); INPUTS 1. x - Multiple turn data ((#BPM x N turns) or (BPM x N turns x 6)) 2. LineType - Line type if plotting FFT data {Default: 'b'} OUTPUTS 1. X - FFT data 2. f - Frequency vector NOTES 1. If x is (NumberOfBPMs x N x 6), then x(:,:,1) will automatically squeeze to a 2-dimensional matrix. For example, x = x(1,:,1) would be a 1 x N vector. EXAMPLES 1. Get 1024 turns at BPM(1,2) and compute the FFT [x, ATIndex, LostBeam] = getturns([.001 0, 0.001, 0, 0, 0]', 1024, 'BPMx', [1 2;6 3]); [X, fx] = multiturnfft(x(1,:,:)); X(:,1) is FFT(x) X(:,2) is FFT(Px) X(:,3) is FFT(y) X(:,4) is FFT(Py) 2. To plot the FFT(x) data for the BPM(1,2): multiturnfft(x(1,:,1)); To plot the FFT(y) data for the BPM(6,3) multiturnfft(x(2,:,3)); 3. Multiple BPM can be used but some case should be taken to keep the phase change between BPMs roughly equal. For instace, to plot the FFT(x) data of all BPMs for the first 10 turns: [x, ATIndex, LostBeam] = getturns([.001 0, 0.001, 0, 0, 0]', 10, 'BPMx', []); multiturnfft(x(:,:,1)); See also getturns, getpvmodel
0001 function [X, f] = multiturnfft(x, LineType); 0002 %MULTITURNFFT - Takes the FFT of multiple turn data (plots results if no output exists) 0003 % 0004 % [X, f] = multiturnfft(x, LineType); 0005 % 0006 % INPUTS 0007 % 1. x - Multiple turn data ((#BPM x N turns) or (BPM x N turns x 6)) 0008 % 2. LineType - Line type if plotting FFT data {Default: 'b'} 0009 % 0010 % OUTPUTS 0011 % 1. X - FFT data 0012 % 2. f - Frequency vector 0013 % 0014 % NOTES 0015 % 1. If x is (NumberOfBPMs x N x 6), then x(:,:,1) will automatically squeeze to a 2-dimensional matrix. 0016 % For example, x = x(1,:,1) would be a 1 x N vector. 0017 % 0018 % EXAMPLES 0019 % 1. Get 1024 turns at BPM(1,2) and compute the FFT 0020 % [x, ATIndex, LostBeam] = getturns([.001 0, 0.001, 0, 0, 0]', 1024, 'BPMx', [1 2;6 3]); 0021 % [X, fx] = multiturnfft(x(1,:,:)); 0022 % X(:,1) is FFT(x) 0023 % X(:,2) is FFT(Px) 0024 % X(:,3) is FFT(y) 0025 % X(:,4) is FFT(Py) 0026 % 2. To plot the FFT(x) data for the BPM(1,2): 0027 % multiturnfft(x(1,:,1)); 0028 % To plot the FFT(y) data for the BPM(6,3) 0029 % multiturnfft(x(2,:,3)); 0030 % 3. Multiple BPM can be used but some case should be taken to keep the phase change 0031 % between BPMs roughly equal. 0032 % For instace, to plot the FFT(x) data of all BPMs for the first 10 turns: 0033 % [x, ATIndex, LostBeam] = getturns([.001 0, 0.001, 0, 0, 0]', 10, 'BPMx', []); 0034 % multiturnfft(x(:,:,1)); 0035 % 0036 % See also getturns, getpvmodel 0037 0038 % Written by Greg Portmann 0039 0040 0041 if nargin == 0 0042 error('1 input required.'); 0043 end 0044 if nargin < 2 0045 LineType = 'b'; 0046 end 0047 0048 0049 % How many BPMs in the FFT 0050 nBPM = size(x,1); 0051 0052 0053 % How many turns 0054 N = size(x,2); 0055 0056 0057 % x, Px, y, Py case 0058 if size(x,3) ~= 1 0059 for i = 1:min(size(x,3),4) 0060 if nargout == 0 0061 if i == 1 0062 clf reset 0063 multiturnfft(squeeze(x(:,:,i)), N, LineType); 0064 subplot(2,1,1); 0065 if nBPM > 1 0066 title(sprintf('%d BPMs %d Turns: x(0) = %.3g [mm] Px(0) = %.3g [mrad] y(0) = %.3g [mm] Py(0) = %.3g [mrad]', nBPM, N, 1000*x(1,1,1), 1000*x(1,1,2), 1000*x(1,1,3), 1000*x(1,1,4))); 0067 else 0068 title(sprintf('%d Turns: x(0) = %.3g [mm] Px(0) = %.3g [mrad] y(0) = %.3g [mm] Py(0) = %.3g [mrad]', N, 1000*x(1,1,1), 1000*x(1,1,2), 1000*x(1,1,3), 1000*x(1,1,4))); 0069 end 0070 ylabel('Horizontal Orbit [mm]'); 0071 elseif i == 2 0072 elseif i == 3 0073 figure(gcf+1); 0074 multiturnfft(squeeze(x(:,:,i)), N, LineType); 0075 subplot(2,1,1); 0076 if nBPM > 1 0077 title(sprintf('%d BPMs %d Turns: x(0) = %.3g [mm] Px(0) = %.3g [mrad] y(0) = %.3g [mm] Py(0) = %.3g [mrad]', nBPM, N, 1000*x(1,1,1), 1000*x(1,1,2), 1000*x(1,1,3), 1000*x(1,1,4))); 0078 else 0079 title(sprintf('%d Turns: x(0) = %.3g [mm] Px(0) = %.3g [mrad] y(0) = %.3g [mm] Py(0) = %.3g [mrad]', N, 1000*x(1,1,1), 1000*x(1,1,2), 1000*x(1,1,3), 1000*x(1,1,4))); 0080 end 0081 ylabel('Vertical Orbit [mm]'); 0082 elseif i == 4 0083 end 0084 else 0085 X(:,i) = multiturnfft(squeeze(x(:,:,i)), N, LineType); 0086 end 0087 end 0088 return 0089 0090 %if size(x,3) ~= 1 0091 % fprintf(' x-input has too many dimensions, squeezing to 2-dim -- squeeze(x(1,:,:)).\n'); 0092 % x = squeeze(x(1,:,:)); 0093 %end 0094 end 0095 0096 0097 0098 % FFT 0099 X = fft(x(:), nBPM*N); % FFT 0100 X(1) = []; % Remove the DC term 0101 0102 0103 f = (1:floor(nBPM*N/2)) / N; % Frequency vector 0104 0105 0106 0107 if nargout == 0 0108 % Plot the results 0109 Xmax = N; %min([100 N]); 0110 0111 subplot(2,1,1); 0112 plot(1000*x(:)); 0113 %plot(1:size(x,2), 1000*x); 0114 %plot(1000*x'); 0115 %xaxis([0 Xmax]); 0116 axis tight 0117 if nBPM > 1 0118 title(sprintf('%d BPMs %d Turns', nBPM, N)); 0119 else 0120 title(sprintf('%d Turns', N)); 0121 end 0122 ylabel('Data'); 0123 if N == 1 0124 xlabel('First Turn'); 0125 elseif nBPM == 1 0126 xlabel('Turn Number'); 0127 else 0128 xlabel(''); 0129 end 0130 0131 % if size(x,1) >= 4 0132 % plot(1000*x, LineType); 0133 % xaxis([0 Xmax]); 0134 % title(sprintf('%d Total Turns: x(0)=%.3g [mm] xp(0)=%.3g [mrad]', N, 1000*x(1,1), 1000*x(1,2))); 0135 % ylabel('Horizontal [mm]'); 0136 % else 0137 % plot(x, LineType); 0138 % xaxis([0 Xmax]); 0139 % title(sprintf('FFT of Single Turn Data (%d turns, Initial Conition = %.3g)', N, x(1,1))); 0140 % ylabel('Data'); 0141 % end 0142 0143 subplot(2,1,2); 0144 semilogy(f, abs(X(1:floor(nBPM*N/2),1)), LineType); 0145 xaxis([0 .5*nBPM]); 0146 grid on; 0147 0148 if nBPM > 1 0149 xlabel('Tune'); 0150 else 0151 xlabel('Fractional Tune'); 0152 end 0153 0154 ylabel('abs(FFT)'); 0155 end 0156 0157