Пример #1
0
                                      phase=np.pi / 3)
fg = reflection.LinearReflection().func(frequency=frequency,
                                        resonance_frequency=f_r,
                                        internal_loss=1e-5,
                                        coupling_loss=5e-5)
data = bg * fg + 0.0002 * \
    (np.random.randn(frequency.size) + 1j * np.random.randn(frequency.size))

# ## Use initial params to improve on the guessing function
# The default algorithm used by `lmfit` is Levenberg-Marquardt, which is fast but finds only the local minimum of the residual function given by the initial values. The most common reason for a fit to fail is that the `guess` function provides initial values that are in a local minimum that is not the global minimum. If a data set that looks "reasonable" fails to converge to the correct values, a quick fix is to try different initial values. If best-fit parameters from a previous successful fit are available and the data to be fit is similar, this same technique can be used to accelerate the fit or achieve convergence.

params = lmfit.Parameters()
params.add(name='resonance_frequency', value=1e9)
r = reflection.LinearReflectionFitter(frequency=frequency,
                                      data=data,
                                      params=params)
fig, ax = see.magnitude_vs_frequency(resonator=r)
print(r.result.fit_report())

# ## Use initial parameters to control whether or not to vary a parameter in the fit
# For example, the value of the coupling can be fixed to a value from a simulation.

params = lmfit.Parameters()
params.add(name='coupling_loss', value=5e-5, vary=False)
r = reflection.LinearReflectionFitter(frequency=frequency,
                                      data=data,
                                      params=params)
fig, ax = see.magnitude_vs_frequency(resonator=r)
print(r.result.fit_report())  # Note that coupling_loss is now fixed
plt.show()
see.real_and_imaginary(resonator=resonator, axes=ax_raw, normalize=False)
see.real_and_imaginary(resonator=resonator, axes=ax_norm, normalize=True)
ax_raw.legend(handles=ax_raw.lines,
              labels=('data', 'fit', 'resonance'),
              fontsize='xx-small')

#
# Adjust these values for your plot settings
fig, axes = plt.subplots(2, 2, sharex='all', figsize=(6, 6), dpi=300)
ax_raw_mag, ax_norm_mag, ax_raw_phase, ax_norm_phase = axes.flatten()
ax_raw_mag.set_title('measurement plane')
ax_norm_mag.set_title('resonator plane')
ax_raw_phase.set_title('measurement plane')
ax_norm_phase.set_title('resonator plane')
see.magnitude_vs_frequency(resonator=resonator,
                           axes=ax_raw_mag,
                           normalize=False,
                           frequency_scale=1e-9)
see.magnitude_vs_frequency(resonator=resonator,
                           axes=ax_norm_mag,
                           normalize=True,
                           frequency_scale=1e-9)
see.phase_vs_frequency(resonator=resonator,
                       axes=ax_raw_phase,
                       normalize=False,
                       frequency_scale=1e-9)
see.phase_vs_frequency(resonator=resonator,
                       axes=ax_norm_phase,
                       normalize=True,
                       frequency_scale=1e-9)

#
Пример #3
0
print("resonance_frequency: {:.2f}".format(
    abs(r.resonance_frequency - resonance_frequency) /
    r.resonance_frequency_error))
print("coupling_loss: {:.2f}".format(
    abs(r.coupling_loss - coupling_loss) / r.coupling_loss_error))
print("internal_loss: {:.2f}".format(
    abs(r.internal_loss - internal_loss) / r.internal_loss_error))
print("magnitude: {:.2f}".format(
    abs(r.magnitude - magnitude) / r.magnitude_error))
print("phase: {:.2f}".format(abs(r.phase - phase) / r.phase_error))

#  [markdown]
# The `see.py` module contains functions that quickly create nice plots of the data. The line is the model evaluated at lots of points, and the larger dot in the same color is the model at the resonance frequency.

#
fig, ax = see.magnitude_vs_frequency(r)

#
fig, ax = see.phase_vs_frequency(r)

#  [markdown]
# If the `normalize` keyword is true, the data and model values are divided by the background model in order to normalize them to the resonator plane. Here, the data and model approach -1 + 0j far from resonance because of the reflection. The data points are fairly equally distributed around the model, which forms a circle in the complex plane, and this is generally a sign that the fit is good and that the background model sufficiently describes the background.

#
fig, ax = see.real_and_imaginary(r, normalize=True)

#  [markdown]
# The `triptych` function used here plots the data and fits three different ways. The initial fit is not plotted by default, but it can be useful for debugging if a fit fails to converge to reasonable values. Often, the problem is simply that the initial values are too far from the true values. See the notebook `advanced.ipynb` for some methods for succesfully fitting in this case.

#
fig, (ax_magnitude, ax_phase, ax_complex) = see.triptych(resonator=r,