import numpy as np
from sys import argv
from chirp_2019 import analyze_waves
from matplotlib import pyplot

dt = 2 * 8 * 33 * 14 / 1320e6
for f in argv[1:]:
    d = np.loadtxt(f)
    cav = d[:, 0] + 1j*d[:, 1]
    fwd = d[:, 2] + 1j*d[:, 3]
    t = np.arange(len(cav)) * dt
    fitc, tf, dy_dt, fit_wave, rms = analyze_waves(fwd, cav, t, dt)
    print("file %s  coeff  %9.2f %9.2f j,  %.6g %.6g j  rms %.3f" %
          (f, fitc[0].real, fitc[0].imag, fitc[1].real, fitc[1].imag, rms))
    if True:
        pyplot.plot(t, dy_dt.real, label="dv/dt")
        pyplot.plot(t, fit_wave.real, label="fit")
        pyplot.legend(frameon=False)
        pyplot.show()
