File size: 12,750 Bytes
7df9186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import numpy as np
from numpy import sqrt, arctan2, exp, mean, std, pi, zeros, hstack, vstack, arange, diff, linalg
from scipy.interpolate import BSpline
from matplotlib import pyplot


class rf_waveforms:
    # Default waveform dump format; can be overridden at class construction time
    DAT_FMT = ["FWD_I", "FWD_Q",
               "REV_I", "REV_Q",
               "CAV_I", "CAV_Q",
               "LOOPB_I", "LOOPB_Q", ]  # Drive
    MIN_PTS = 256

    def __init__(self, data_file, skip_pt=0, data_format=DAT_FMT):
        self.waves = np.loadtxt(data_file).transpose()

        self.skip_pt = skip_pt
        self.DAT_FMT = data_format

        if self.waves.shape[0] != len(self.DAT_FMT) or\
           self.waves.shape[1] < self.MIN_PTS:
            print("ERROR: Bad data shape: {0}".format(self.waves.data.shape))
            exit(1)

    def get_ch(self, ch_key):
        if ch_key not in self.DAT_FMT:
            print("ERROR: No channel {} in waveform data".format(ch_key))

        return self.waves[self.DAT_FMT.index(ch_key)][self.skip_pt:]


class digaree_coeff:
    CONFIG_PARAMS = ["bandwidth", "wvform_dt",
                     "freq_quantum", "digaree_dt",
                     "out_shift"]

    def __init__(self, c_dict, fir_gain=80, dig_data_w=20, dig_extra_w=4):
        for p in self.CONFIG_PARAMS:
            if p not in c_dict:
                print("ERROR: Parameter {} missing from config dict".format(p))
                exit(1)
        self.c_dict = c_dict
        self.dig_dw = dig_data_w   # Width of Digaree inputs/parameters
        self.dig_ew = dig_extra_w  # Extra guard bits
        self.fir_g = fir_gain      # pre-scaling for dV/dt

    def compute(self, wvf, plot=False, verbose=False):
        print("ERROR: This method must be overridden by a coefficient calculation method")
        exit(1)

    """
    Scale Beta and 1/T so output detune frequency is in units of scaled Hz.

    # Input Beta is in Hz.
    # conf.dict["freq_quantum"] determines the resolution of detune output.
    # conf.dict["out_shift"] compensates for resizing of detune output. E.g:
      if Digaree's data width is 20-bit but transmitted detune is 18-bit,
      this scaling factor must be set to 2**(20-18) = 4.
    # conf.dict["digaree_dt"] is pre-scaled by a constant FIR gain for dV/dt
      (see comments in cgen_srf.py)
    """
    def get_coeffs(self, beta):
        invT = 1.0/(self.c_dict["digaree_dt"]*self.fir_g)  # inverse seconds

        # Divide by 2pi to get Hz
        invT = invT / (2*pi)
        beta = beta  # beta is already in Hz

        # Scale by freq quantum and (optional) output shift
        invT = (invT * self.c_dict["out_shift"]) / self.c_dict["freq_quantum"]
        beta = (beta * self.c_dict["out_shift"]) / self.c_dict["freq_quantum"]

        detune_coeffs = [beta.real, beta.imag, invT, 32768]

        # Round to int and check for overflow wrt Digaree signed pipeline width
        sff = [int(round(x)) for x in detune_coeffs]
        ok = all([abs(x) < 2**(self.dig_dw+self.dig_ew-1) for x in sff])

        if not ok:
            print("ERROR: Computed coefficients exceed Digaree pipeline width")
            for idx, kx in enumerate(sff):
                print("sf_consts[%d] = %d" % (idx, kx))
            sff = None
        else:
            for idx, kx in enumerate(sff):
                print("sf_consts[%d] = %d" % (idx, kx))
            print("fq = %f" % (self.c_dict["freq_quantum"]))

        return sff


"""
Analyzes waveform data to get the setup parameters for detune computations.
Data is assumed to be in GDR mode, which is both good and bad:
simpler math, but need an externally-provided bandwidth.
Only needs/uses forward and cavity signals.

Depends on microphonics to make large variations in the imaginary part
of the complex state parameter, while the real part (based on Q_L) is
relatively constant on these time scales.  With GDR mode holding the
cavity voltage fixed, the forward wave can be rotated and scaled to give
that state parameter.

Has been tested when this system is parasiting off another controller.
It will be useful for in-situ correction of cable drift during long
CW GDR runs, which would otherwise introduce tune angle drift.
"""


class detune_gdr(digaree_coeff):

    def fwd_coeff(self, fwd, verbose=False):
        # This covariance and eigenvalue computation finds out how to
        # rotate the forward wave to give large imaginary fluctuations
        # but small real fluctuations.
        mmm1 = np.cov(fwd.real, fwd.imag)
        e_val, e_vec = np.linalg.eig(mmm1)

        # we need e_val sorted from larger to smaller
        ev_list = list(zip(e_val, e_vec))
        ev_list.sort(key=lambda tup: tup[0], reverse=True)
        e_val, e_vec = zip(*ev_list)
        fp0 = arctan2(e_vec[0][0], e_vec[0][1])
        coeff = exp(-1j*fp0)
        fwdx = fwd * coeff

        # eigenvectors have arbitrary sign
        if mean(fwdx).real < 0:
            fwdx = -fwdx
            coeff = -coeff
            fp0 = (fp0 + 2*pi) % (2*pi) - pi
        drv = fwdx.imag/mean(fwdx.real)
        coeff = coeff/mean(fwdx.real)
        if verbose:
            print("Orthogonal fwd rms (%.1f, %.1f)" % tuple(sqrt(e_val)))
            print("Forward phase zero %.3f radians" % fp0)
            print("Drive normalized variation %.3f rms" % std(drv))
        return coeff

    def compute(self, wvf, plot=False, verbose=False):
        # loopback and reverse not used
        fwd = wvf.get_ch("FWD_I") + 1j*wvf.get_ch("FWD_Q")
        cav = wvf.get_ch("CAV_I") + 1j*wvf.get_ch("CAV_Q")
        fwd_c = self.fwd_coeff(fwd, verbose=verbose)
        cav_c = 1/mean(cav)
        beta = self.c_dict["bandwidth"] * fwd_c / cav_c

        # Plot FWD, CAV and beta*FWD/CAV
        fwdx = fwd * fwd_c
        cavx = cav * cav_c
        a = beta * fwd / cav
        t = np.arange(len(cav)) * self.c_dict["wvform_dt"]
        f, (ax1, ax2) = pyplot.subplots(1, 2, figsize=(16, 8))
        ax1.plot(fwdx.real, fwdx.imag, label="Forward")
        ax1.plot(cavx.real, cavx.imag, label="Cavity")
        ax1.set_xlim(0.98, 1.02)
        ax1.set_ylim(-0.8, 0.8)
        ax1.set_xlabel('Real')
        ax1.set_ylabel('Imag')
        ax1.legend(frameon=False)
        ax1.set_title("Input waveforms, normalized and rotated")
        ax2.plot(t, a.real, label='real')
        ax2.plot(t, a.imag, label='imag')
        ax2.set_xlim(0, max(t))
        ax2.set_ylim(-12.0, 18.0)
        ax2.set_xlabel("Time (s)")
        ax2.set_ylabel("Frequency (Hz)")
        ax2.set_title("Resulting state-space coefficient")
        ax2.legend(frameon=False)
        # Note that the mean value of the coefficient's real part is,
        # by construction, equal to the bandwidth supplied to us in
        # c_dict["bandwidth"].
        # All the results are normalized based on that single number.

        fig_name = "cw_fit.png"
        pyplot.savefig(fig_name)
        print("Plot saved to {}".format(fig_name))
        if plot:
            pyplot.show()

        if verbose:
            print("SI beta %.3f%+.3fj Hz, magnitude %.3f Hz" % (beta.real, beta.imag, abs(beta)))
        return self.get_coeffs(beta)


"""
Analyzes waveform data to get the setup parameters for detune computations.
Data is assumed to be in pulsed mode.
Only needs/uses forward and cavity signals.
"""


class detune_pulse(digaree_coeff):

    """
    Cardinal B-spline  https://en.wikipedia.org/wiki/B-spline
    Same as the output of a second-order CIC interpolator
    Bespoke wrapper as drop-in for the old scipy.signal.bspline,
    which was removed in SciPy 1.13.
    """
    def bspline(self, x, n):
        knots = np.arange(-(n+1)/2, (n+3)/2)
        out = BSpline.basis_element(knots)(x)
        out[(x < knots[0]) | (x > knots[-1])] = 0.0
        return out

    def create_basist(self, block=15, n=10):
        npt = (n-1)*block+1
        basist = zeros([npt, n])
        x = arange(npt)/float(block)
        for jx in range(n):
            basist[:, jx] = self.bspline(x-jx, 2)
        # end-effects, it's important that the sum is flat
        basist[:, 0] += self.bspline(x+1, 2)
        basist[:, n-1] += self.bspline(x-n, 2)
        return basist

    # basist is n*m, where n=len(cav)-1, m is number of time-dependent bases
    def compute(self, wvf, plot=False, verbose=False):
        b_blk, b_n = self.c_dict["basist_block"], self.c_dict["basist_n"]
        basist = self.create_basist(block=b_blk, n=b_n)
        cav = wvf.get_ch("CAV_I") + 1j*wvf.get_ch("CAV_Q")
        fwd = wvf.get_ch("FWD_I") + 1j*wvf.get_ch("FWD_Q")

        ny = basist.shape[0]+1
        cav = cav[:ny]
        fwd = fwd[:ny]

        acav = 0.5 * (cav[1:] + cav[:-1])
        afwd = 0.5 * (fwd[1:] + fwd[:-1])
        dcav = diff(cav)/self.c_dict["wvform_dt"]
        cave = acav * basist.T
        # Fit dcav to a*acav + b*afwd
        # Treat real and imaginary parts separately, since we want the imaginary
        # part of b to be sum_i g_i*f_i(t) while the real part is constant.
        goal = hstack([dcav.real, dcav.imag])
        basis1 = hstack([afwd.real, afwd.imag])   # b.real
        basis2 = hstack([-afwd.imag, afwd.real])  # b.imag
        basis3 = hstack([acav.real, acav.imag])   # a.real
        basisn = hstack([-cave.imag, cave.real])  # a.imag repeated
        basis = vstack([basis1, basis2, basis3, basisn])

        fitc, resid, rank, sing = linalg.lstsq(basis.T, goal, rcond=-1)

        beta = fitc[0]+1j*fitc[1]  # in rad/s
        beta_hz = beta / (2*pi)
        bw_hz = fitc[2]/(2*pi)  # Hz
        det_hz = fitc[3:]/(2*pi)  # Hz
        if verbose:
            print("Bandwidth %.3f Hz" % -bw_hz)
            print("Detune Hz", det_hz)
            print("SI beta %.3f%+.3fj Hz, magnitude %.3f Hz" % (beta.real, beta.imag, abs(beta)))

        if False:
            fitv = fitc.dot(basis)
            # print(goal.shape, basis.shape, fitv.shape)
            pyplot.plot(goal, label='goal')
            pyplot.plot(fitv, label='fit')
            for jx in range(len(fitc)):
                pyplot.plot(fitc[jx]*basis[jx, :], label="%d" % jx)
            pyplot.legend(frameon=False)
            pyplot.show()

        # Useful for plotting
        detune_hz = basist.dot(fitc[3:])/(2*pi)

        return self.get_coeffs(beta_hz), beta, detune_hz, bw_hz


if __name__ == "__main__":
    from argparse import ArgumentParser

    parser = ArgumentParser(description="Detune coefficient computation")
    parser.add_argument("-m", "--mode", dest="mode", default="cw", const="cw", nargs="?",
                        choices=["cw", "pulse"], help="Coeff calculation mode")
    parser.add_argument("-f", "--datafile", dest="datafile", default=None, required=True,
                        help="IQ data input file")
    parser.add_argument("-v", "--verbose", action="store_true", dest="verbose", help="Verbose mode")
    parser.add_argument("-p", "--plot", action="store_true", dest="plot", help="Plot")

    args = parser.parse_args()

    # Read in IQ waveforms
    rf_wvf = rf_waveforms(args.datafile, data_format=["UN_I", "UN_Q",
                                                      "FWD_I", "FWD_Q",
                                                      "REV_I", "REV_Q",
                                                      "CAV_I", "CAV_Q"])

    adc_clk = 1320.0e6 / 14.0  # Hz

    # Waveform acquisition timestep
    wvform_dt = (255*2*33) / adc_clk

    # Digaree data stream timestep
    digaree_dt = (32*2*33) / adc_clk

    # Setup configuration dict for detune coefficient calculation
    detune_dict = {"bandwidth": 15.0,  # Ignored in pulse mode
                   "wvform_dt": wvform_dt,
                   "freq_quantum": 0.0355256,
                   "digaree_dt": digaree_dt,
                   "basist_block": 10,
                   "basist_n": 24,
                   "out_shift": 4}
    # I should write a long essay about the meaning of basist_block and
    # basist_n, which are only used in pulse-mode.  These describe the
    # degrees of freedom available to fit variations in cavity frequency
    # during a pulse due to Lorentz Force Detuning (LFD).  basist_block is
    # the number of sample points per degree of freedom, and basist_n is
    # the number of such degrees of freedom.  The number of waveform points
    # used in the analysis is basist_block * (basist_n - 1) + 2.  That should
    # be less than the number of points in the "on" time of the pulse.
    # I hope that helps.  :-)

    if args.mode == "cw":
        print("Calculating detune coefficients from CW data")
        detune_coeff = detune_gdr(detune_dict)
    else:
        detune_coeff = detune_pulse(detune_dict)

    detune_coeff.compute(rf_wvf, args.plot, args.verbose)