1 | function [X, f] = multiturnfft(x, LineType); |
---|
2 | %MULTITURNFFT - Takes the FFT of multiple turn data (plots results if no output exists) |
---|
3 | % |
---|
4 | % [X, f] = multiturnfft(x, LineType); |
---|
5 | % |
---|
6 | % INPUTS |
---|
7 | % 1. x - Multiple turn data ((#BPM x N turns) or (BPM x N turns x 6)) |
---|
8 | % 2. LineType - Line type if plotting FFT data {Default: 'b'} |
---|
9 | % |
---|
10 | % OUTPUTS |
---|
11 | % 1. X - FFT data |
---|
12 | % 2. f - Frequency vector |
---|
13 | % |
---|
14 | % NOTES |
---|
15 | % 1. If x is (NumberOfBPMs x N x 6), then x(:,:,1) will automatically squeeze to a 2-dimensional matrix. |
---|
16 | % For example, x = x(1,:,1) would be a 1 x N vector. |
---|
17 | % |
---|
18 | % EXAMPLES |
---|
19 | % 1. Get 1024 turns at BPM(1,2) and compute the FFT |
---|
20 | % [x, ATIndex, LostBeam] = getturns([.001 0, 0.001, 0, 0, 0]', 1024, 'BPMx', [1 2;6 3]); |
---|
21 | % [X, fx] = multiturnfft(x(1,:,:)); |
---|
22 | % X(:,1) is FFT(x) |
---|
23 | % X(:,2) is FFT(Px) |
---|
24 | % X(:,3) is FFT(y) |
---|
25 | % X(:,4) is FFT(Py) |
---|
26 | % 2. To plot the FFT(x) data for the BPM(1,2): |
---|
27 | % multiturnfft(x(1,:,1)); |
---|
28 | % To plot the FFT(y) data for the BPM(6,3) |
---|
29 | % multiturnfft(x(2,:,3)); |
---|
30 | % 3. Multiple BPM can be used but some case should be taken to keep the phase change |
---|
31 | % between BPMs roughly equal. |
---|
32 | % For instace, to plot the FFT(x) data of all BPMs for the first 10 turns: |
---|
33 | % [x, ATIndex, LostBeam] = getturns([.001 0, 0.001, 0, 0, 0]', 10, 'BPMx', []); |
---|
34 | % multiturnfft(x(:,:,1)); |
---|
35 | % |
---|
36 | % See also getturns getpvmodel |
---|
37 | % |
---|
38 | % Written by Greg Portmann |
---|
39 | |
---|
40 | |
---|
41 | if nargin == 0 |
---|
42 | error('1 input required.'); |
---|
43 | end |
---|
44 | if nargin < 2 |
---|
45 | LineType = 'b'; |
---|
46 | end |
---|
47 | |
---|
48 | |
---|
49 | % How many BPMs in the FFT |
---|
50 | nBPM = size(x,1); |
---|
51 | |
---|
52 | |
---|
53 | % How many turns |
---|
54 | N = size(x,2); |
---|
55 | |
---|
56 | |
---|
57 | % x, Px, y, Py case |
---|
58 | if size(x,3) ~= 1 |
---|
59 | for i = 1:min(size(x,3),4) |
---|
60 | if nargout == 0 |
---|
61 | if i == 1 |
---|
62 | clf reset |
---|
63 | multiturnfft(squeeze(x(:,:,i)), N, LineType); |
---|
64 | subplot(2,1,1); |
---|
65 | if nBPM > 1 |
---|
66 | 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))); |
---|
67 | else |
---|
68 | 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))); |
---|
69 | end |
---|
70 | ylabel('Horizontal Orbit [mm]'); |
---|
71 | elseif i == 2 |
---|
72 | elseif i == 3 |
---|
73 | figure(gcf+1); |
---|
74 | multiturnfft(squeeze(x(:,:,i)), N, LineType); |
---|
75 | subplot(2,1,1); |
---|
76 | if nBPM > 1 |
---|
77 | 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))); |
---|
78 | else |
---|
79 | 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))); |
---|
80 | end |
---|
81 | ylabel('Vertical Orbit [mm]'); |
---|
82 | elseif i == 4 |
---|
83 | end |
---|
84 | else |
---|
85 | X(:,i) = multiturnfft(squeeze(x(:,:,i)), N, LineType); |
---|
86 | end |
---|
87 | end |
---|
88 | return |
---|
89 | |
---|
90 | %if size(x,3) ~= 1 |
---|
91 | % fprintf(' x-input has too many dimensions, squeezing to 2-dim -- squeeze(x(1,:,:)).\n'); |
---|
92 | % x = squeeze(x(1,:,:)); |
---|
93 | %end |
---|
94 | end |
---|
95 | |
---|
96 | |
---|
97 | |
---|
98 | % FFT |
---|
99 | X = fft(x(:), nBPM*N); % FFT |
---|
100 | X(1) = []; % Remove the DC term |
---|
101 | |
---|
102 | |
---|
103 | f = (1:floor(nBPM*N/2)) / N; % Frequency vector |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | if nargout == 0 |
---|
108 | % Plot the results |
---|
109 | Xmax = N; %min([100 N]); |
---|
110 | |
---|
111 | subplot(2,1,1); |
---|
112 | plot(1000*x(:)); |
---|
113 | %plot(1:size(x,2), 1000*x); |
---|
114 | %plot(1000*x'); |
---|
115 | %xaxis([0 Xmax]); |
---|
116 | axis tight |
---|
117 | if nBPM > 1 |
---|
118 | title(sprintf('%d BPMs %d Turns', nBPM, N)); |
---|
119 | else |
---|
120 | title(sprintf('%d Turns', N)); |
---|
121 | end |
---|
122 | ylabel('Data'); |
---|
123 | if N == 1 |
---|
124 | xlabel('First Turn'); |
---|
125 | elseif nBPM == 1 |
---|
126 | xlabel('Turn Number'); |
---|
127 | else |
---|
128 | xlabel(''); |
---|
129 | end |
---|
130 | |
---|
131 | % if size(x,1) >= 4 |
---|
132 | % plot(1000*x, LineType); |
---|
133 | % xaxis([0 Xmax]); |
---|
134 | % title(sprintf('%d Total Turns: x(0)=%.3g [mm] xp(0)=%.3g [mrad]', N, 1000*x(1,1), 1000*x(1,2))); |
---|
135 | % ylabel('Horizontal [mm]'); |
---|
136 | % else |
---|
137 | % plot(x, LineType); |
---|
138 | % xaxis([0 Xmax]); |
---|
139 | % title(sprintf('FFT of Single Turn Data (%d turns, Initial Conition = %.3g)', N, x(1,1))); |
---|
140 | % ylabel('Data'); |
---|
141 | % end |
---|
142 | |
---|
143 | subplot(2,1,2); |
---|
144 | semilogy(f, abs(X(1:floor(nBPM*N/2),1)), LineType); |
---|
145 | xaxis([0 .5*nBPM]); |
---|
146 | grid on; |
---|
147 | |
---|
148 | if nBPM > 1 |
---|
149 | xlabel('Tune'); |
---|
150 | else |
---|
151 | xlabel('Fractional Tune'); |
---|
152 | end |
---|
153 | |
---|
154 | ylabel('abs(FFT)'); |
---|
155 | end |
---|
156 | |
---|
157 | |
---|