import numpy as np
import scipy.fft as fft
from scipy.interpolate import pchip_interpolate, splrep, BSpline
from scipy.signal import hilbert
import scipy.signal as signal
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import deerlab as dl
from matplotlib.ticker import AutoMinorLocator,AutoLocator
from pyepr.dataset import create_dataset_from_axes
import xarray as xr
from pyepr.colors import primary_colors
__all__ = [
'phase_correct_respro', 'ResonatorProfileAnalysis', 'ceil', 'floor',
'calc_overlap', 'BSpline_extra', 'optimise_spectra_position',
'AmplifierLinearityAnalysis',
]
# def phase_correct_respro(data_array):
# Vre, Vim, ph = dl.correctphase(data_array.values,full_output=True)
# index = np.argmax(np.abs(Vre),axis=0)
# index = 0 # normalise to first point
# max_val = Vre[index,np.arange(Vre.shape[1])]
# scale = (max_val >= 0)*2-1
# Vre = Vre*scale
# Vim = Vim*scale
# data_array.values = Vre + 1j*Vim
# return data_array
[docs]
def phase_correct_respro(dataset):
""" Phase corrects a resonator profile dataset, by making sure the first point of each frequnecy is real"""
data = dataset.values
Vre, Vim, ph = dl.correctphase(data, full_output=True)
new_data = Vre + 1j * Vim
first_points = Vre[0, np.arange(Vre.shape[1])]
first_angles = np.angle(first_points)
new_data *= np.exp(-1j * first_angles)
dataset.values = new_data
return dataset
[docs]
class ResonatorProfileAnalysis:
def __init__(
self, dataset,f_lims=(32,36),attentuator=0,R_limit=0.2,bounds=None,p0=None,**kwargs) -> None:
"""Analysis and calculation of resonator profiles.
Fitting fuction:
.. math:: \\nu(t) = a \\cos(2\\pi f (t - x_0)) e^{-(t - x_0)/\\tau} + k
where :math:`a` is the amplitude, :math:`f` is the nutation frequency,
:math:`\\tau` is the decay time, :math:`x_0` is the offset and :math:`k` is a constant offset.
The fit is performed for each frequency in the dataset. The fit is
performed using the `curve_fit` function from `scipy.optimize`.
Parameters
----------
dataset : xr.xarray
The dataset containing the nutations. It must have both a 'LO' axis
and a 'pulse0_tp' axis.
f_lims : tuple, optional
The frequency limits of the resonator profile, by default (33,35)
attenuator: int
The value of the main attentuator in dB, by default 0
R_limit: float, optional
The R^2 limit for extracting fits, by default 0.2
bounds : tuple, optional
The bounds for the fit in the form ([lower bounds],[upper bounds]), by default None. If not given the bounds are set to ([5e-3,10,0,-1,-5],[0.3,2000,2,1,5])
p0 : list, optional
The initial guess for the fit, by default None. If not given the guess is set to [50e-3,150,1,0]
"""
# Rotate the dataset so that first axis is 'pulse0_tp' and second is 'LO'
tp_dim = dataset.coords['pulse0_tp'].dims[0]
B_dim = dataset.coords['B'].dims[0]
if tp_dim != dataset.dims[0]:
dataset = dataset.transpose(tp_dim,B_dim, transpose_coords=True)
if np.iscomplexobj(dataset) and not kwargs.get('skip_phase_correction',False):
self.dataset = phase_correct_respro(dataset)
else:
self.dataset = dataset
if "LO" in self.dataset.coords:
self.freqs = self.dataset.LO.values
self.freq_c = np.mean(self.dataset.LO)
elif "freq" in self.dataset.coords:
self.freqs = self.dataset.freq.values
self.freq_c = np.mean(self.dataset.freq)
elif "freq_axis" in self.dataset.coords:
self.freqs = self.dataset.freq_axis.values
self.freq_c = self.dataset.freq
if isinstance(self.freq_c,xr.DataArray):
self.freq_c = self.freq_c.values
[docs]
self.n_files = self.freqs.shape[0]
[docs]
self.t = self.dataset.pulse0_tp
[docs]
self.attenuator = attentuator
if bounds is not None:
self.bounds = bounds
else:
self.bounds = ([5e-3,10,0,-1,-5],[0.3,2000,2,1,5])
if p0 is not None:
self.p0 = p0
else:
self.p0 = [50e-3,150,1,0,0]
self._process_fit(R_limit,**kwargs)
pass
[docs]
def process_nutations(
self, noisedensity: float = None, threshold:int = 2,
nfft: int = 1000,**kwargs):
""" Uses a power series to extract the resonator profile.
Parameters
----------
noisedensity : tuple, optional
If not given the first trace is assumed to be so far off resonance
that it is just noise.
nfft: int, optional
The length of the fft to be used, zero padded if requred, default
is 1000.
threshold: int, optional
The multiples above the noise a single must be to not be excluded,
default is 2.
Returns
-------
prof_data: np.ndarray
The resonator profile, give in nutation frequency (GHz)
prof_frqs: np.ndarray
The frequency axis in GHz
"""
if noisedensity is None:
f, Pxx_den = signal.welch(self.nutations[0, :], self.fs, nfft=nfft)
noisedensity = np.mean(Pxx_den)
threshold_val = threshold * noisedensity
res_prof = np.zeros(self.n_files)
for i in range(0, self.n_files):
nuts = self.nutations[i, :]
f, Pxx_den_sig = signal.welch(nuts, self.fs, nfft=nfft)
nuoff = np.argwhere(Pxx_den_sig > threshold_val)
if nuoff.shape[0] > 0:
id = np.argmax(Pxx_den_sig[nuoff])
res_prof[i] = np.abs(f[nuoff][id][0])
else:
res_prof[i] = 0
nx_id = np.argwhere(res_prof > 0)
self.prof_data = res_prof[nx_id]
self.prof_frqs = np.real(self.freqs[nx_id])
return self.prof_data, self.prof_frqs
def _process_fit(self,R_limit=0.5,mask=None,debug=False,**kwargs):
"""Processes the nutation data by fitting a cosine function to each
frequency in the dataset.
Function used for fitting:
.. math:: \\nu(t) = a \\cos(2\\pi f (t - x_0)) e^{-(t - x_0)/\\tau} + k
where :math:`a` is the amplitude, :math:`f` is the nutation frequency,
:math:`\\tau` is the decay time, :math:`x_0` is the offset and :math:`k` is a constant offset.
Parameters
----------
R_limit : float, optional
The R^2 limit for extracting fits, by default 0.5
mask : np.ndarray, optional
A mask to apply to the dataset, by default None. If not given the whole dataset is used.
The mask is applied along the 'pulse0_tp' axis, so it should be a 1D array
debug : bool, optional
If True all fits that are below the R_limit are plotted, by default False
"""
self.n_LO = self.freqs.shape[0]
self.profile = np.zeros(self.n_LO)
self.profile_ci = np.zeros(self.n_LO)
fun = lambda x, f, tau,a,k,x0: a*np.cos(2*np.pi*f*(x-x0))*np.exp(-(x-x0)/tau)+ k
R2 = lambda y, yhat: 1 - np.sum((y - yhat)**2) / np.sum((y - np.mean(y))**2)
excluded = {}
for i in range(self.n_LO):
if mask is not None:
nutation = self.dataset[mask,i]
x = self.t[mask]
else:
nutation = self.dataset[:,i]
x = self.t
if np.iscomplexobj(nutation):
nutation = nutation.real
nutation = nutation/np.max(nutation)
if np.any(np.isnan(nutation)):
if debug:
print(f"Skipping frequency {self.freqs[i].values} GHz due to NaN values")
excluded[i] = None
# If the nutation is NaN, set the profile and profile_ci to nan
self.profile[i] = np.nan
self.profile_ci[i] = np.nan
continue
try:
results = curve_fit(fun, x, nutation, bounds=self.bounds,xtol=1e-4,ftol=1e-4,p0=self.p0)
if R2(nutation,fun(x,*results[0])) > R_limit:
self.profile[i] = results[0][0]
self.profile_ci[i] = np.sqrt(np.diag(results[1]))[0]*1.96
else:
excluded[i] = results[0]
self.profile[i] = np.nan
self.profile_ci[i] = np.nan
except RuntimeError as e:
if debug:
print(f"Fit failed for frequency {self.freqs[i].values} GHz \n {e}")
excluded[i] = None
# If the fit fails, set the profile and profile_ci to nan
self.profile[i] = np.nan
self.profile_ci[i] = np.nan
# Remove nan
old_freqs=self.freqs
self.freqs = self.freqs[~np.isnan(self.profile)]
self.profile = self.profile[~np.isnan(self.profile)]
self.profile_ci = self.profile_ci[~np.isnan(self.profile_ci)]
# Adjust for attenuator
self.profile = self.profile * 10**(self.attenuator/20)
self.profile_ci = self.profile_ci * 10**(self.attenuator/20)
if debug:
n_excluded = len(excluded)
if n_excluded == 0:
print("All fits were successful")
return
print(f"{n_excluded} fits were excluded due to low R^2 value")
n_columns = 2
n_rows = int(np.ceil(n_excluded/n_columns))
fig, axs = plt.subplots(n_rows, n_columns, figsize=(10, 5*n_rows), constrained_layout=True,sharex=True,sharey=True)
axs = axs.flatten() if n_rows > 1 else [axs]
for i in range(n_excluded):
key = int(list(excluded.keys())[i])
nutation = self.dataset[:,key]
freq = old_freqs[key].values
if np.iscomplexobj(nutation):
nutation = nutation.real
nutation = nutation/np.max(nutation)
x = self.t
axs[i].set_title(f'Excluded fit for {freq} GHz')
axs[i].plot(x,nutation,label=f'data')
if excluded[key] is not None:
axs[i].plot(x,fun(x,*excluded[key]),label=f'Excluded fit')
axs[i].set_xlabel('Time / ns')
return fig
[docs]
def fit(self,model='lorenz_sqrt',f_diff_threshold=2,cores=1,fc_guess=None,**kwargs):
"""Fit the resonator profile with a sum of lorentzians.
Parameters
----------
f_diff_threshold : float, optional
The difference between two peaks at which they will be merged into one, by default 0.03
"""
frq_limits = {'lb': self.f_lims[0]-1, 'ub': self.f_lims[1]+1}
if fc_guess is None:
fc_guess = np.mean([self.f_lims[0],self.f_lims[1]])
def fit_gauss1():
gauss_model = dl.dd_gauss
gauss_model.mean.set(par0=fc_guess, **frq_limits)
gauss_model.std.set(par0=0.2, lb=0.01, ub=1)
result_gauss1 = dl.fit(gauss_model, self.profile, self.freqs,reg=False)
return result_gauss1
def lorenz_fcn(x, fc, q):
y = (0.5*fc)/(q*(x-fc)**2 + q*(0.5*(fc/q))**2)
return y
def lorenz_fcn_sqrt(x, fc, q):
y = np.sqrt((0.5*fc)/(q*(x-fc)**2 + q*(0.5*(fc/q))**2))
return y
def fit_lorenz1(uni_means,uni_stds,fit_kwargs):
lorenz = dl.Model(lorenz_fcn, constants='x')
lorenz.fc.par0 = uni_means[0]
lorenz.fc.set(**frq_limits)
lorenz.q.set(lb=30, ub=np.inf)
lorenz.q.par0 = uni_means[0]/uni_stds[0]
lorenz.fc.set(par0=uni_means[0],lb=uni_means[0]-0.1,ub=uni_means[0]+0.1)
result_lorenz1 = dl.fit(lorenz, self.profile, self.freqs,reg=False, **fit_kwargs)
return result_lorenz1, lorenz_fcn, lorenz
def fit_lorenz1_sqrt(uni_means,uni_stds,fit_kwargs):
lorenz_sqrt = dl.Model(lorenz_fcn_sqrt, constants='x')
lorenz_sqrt.fc.par0 = uni_means[0]
lorenz_sqrt.fc.set(**frq_limits)
lorenz_sqrt.q.set(lb=30, ub=np.inf)
lorenz_sqrt.q.par0 = uni_means[0]/uni_stds[0]
result_lorenz1_sqrt = dl.fit(lorenz_sqrt, self.profile, self.freqs,reg=False, **fit_kwargs)
return result_lorenz1_sqrt, lorenz_fcn_sqrt, lorenz_sqrt
# First fit with a gaussian to get starting values
gs = fit_gauss1()
uni_means = [gs.mean]
uni_stds = [gs.std]
n_modes = 1
self.model_x = np.linspace(self.f_lims[0], self.f_lims[1], 200)
fit_kwargs = {'bootstrap': 30, 'bootcores':cores}
fit_kwargs.update(kwargs)
# fit_kwargs={}
if model=='lorenz':
lorenz_fit,fun,mod = fit_lorenz1(uni_means,uni_stds,fit_kwargs)
self.model_func = lambda x: lorenz_fit.scale * lorenz_fcn(x, lorenz_fit.fc, lorenz_fit.q)
self.fc = lorenz_fit.fc
self.q = lorenz_fit.q
elif model=='lorenz_sqrt':
lorenz_fit,fun,mod = fit_lorenz1_sqrt(uni_means,uni_stds,fit_kwargs)
self.model_func = lambda x: lorenz_fit.scale * lorenz_fcn_sqrt(x, lorenz_fit.fc, lorenz_fit.q)
self.fc = lorenz_fit.fc
self.q = lorenz_fit.q
self.model = self.model_func(self.model_x)
self.lorenz_model = mod
self.n_modes = n_modes
self.pha = np.imag(hilbert(-np.log(self.model)))
self.results = lorenz_fit
return lorenz_fit
[docs]
def plot(self, axs= None, fig=None, **kwargs):
"""plot.
Parameters
----------
axs : matplotlib.Axes, optional
Axes to plot on, by default None. Needs to be a flat array with two axes
fig : matplotlib.Figure, optional
Figure to plot on, by default None
Returns
-------
Matplotlib.Figure
matplotlib figure object
"""
if axs is None and fig is None:
figsize = kwargs.get('figsize', (6,4))
fig, axs = plt.subplots(2,1,figsize=figsize,sharex=True)
fig.subplots_adjust(hspace=0.05)
elif fig is None and isinstance(axs,plt.Axes):
fig = axs.figure
self.dataset.real.plot(y='pulse0_tp',x='freq', ax=axs[0],add_colorbar=False,cmap='PuOr')
axs[0].set_xlabel('',fontsize=15)
axs[1].plot(self.freqs,self.profile *1e3,ls='',ms=10,marker='.',alpha=0.8,color=primary_colors[0])
if hasattr(self,'results'):
model_x = np.linspace(self.freqs.min(), self.freqs.max(), 200)
model_y = self.model_func(model_x)
axs[1].plot(model_x,model_y *1e3,color=primary_colors[1],lw=3)
axs[1].set_ylabel('Nutation Frequency / MHz',fontsize=11)
axs[1].set_xlabel('Freq / GHz',fontsize=11)
axs[1].yaxis.set_visible(True)
axs[0].tick_params(axis='both', which='major', labelsize=10)
axs[0].set_ylabel('tp / ns',fontsize=11)
axs[1].grid()
axs[1].tick_params(axis='both', which='major', labelsize=10)
def Hz2length(x):
return 1 / ((x/1000)*2)
def length2Hz(x):
return 1 / ((x*1000)*2)
secax = axs[1].secondary_yaxis('right', functions=(Hz2length, length2Hz))
secax.set_ylabel(r'$\pi$ pulse length / ns',fontsize=11)
secax.set_ticks([8,10,12,14,16,24,32,64])
secax.tick_params(axis='both', which='major', labelsize=10)
if hasattr(self,'results'):
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
textstr = f"Q = {self.q:.2f}\n$f_c$ = {self.fc:.2f} GHz\n$\\nu_{{max}}$ = {self.model.max()*1e3:.2f} MHz"
axs[1].text(0.02, 0.95, textstr, transform=axs[1].transAxes, fontsize=11,
verticalalignment='top', bbox=props)
axs[0].set_ylim(*axs[0].get_ylim())
axs[0].vlines(self.fc,*axs[0].get_ylim(),ls='--',color='0.7',lw=2)
axs[1].set_ylim(*axs[1].get_ylim())
axs[1].vlines(self.fc,*axs[1].get_ylim(),ls='--',color='0.7',lw=2)
secax.tick_params(axis='both', which='major', labelsize=10)
secax2 = axs[1].secondary_xaxis(-0.22, functions=(lambda x: x - self.fc, lambda x: x + self.fc))
secax2.set_xlabel('Relative Freq / GHz',fontsize=11)
return fig
@classmethod
def _dummy_create(cls,freq_axis:np.ndarray,nu_max:float,Q:float,fc:float):
"""Creates a dummy resonator profile for testing purposes.
Parameters
----------
freq_axis : np.ndarray
The frequency axis of the resonator profile.
nu_max : float
The maximum nutation frequency.
Q : float
The quality factor of the resonator.
fc : float
The center frequency of the resonator.
"""
tp_axis = np.linspace(0,128,128)
nutations = np.zeros((freq_axis.shape[0],tp_axis.shape[0]))
for i,freq in enumerate(freq_axis):
f_offset = np.abs(freq - fc)
nu_local = nu_max *(0.5*fc)/(Q*(f_offset)**2 + Q*(0.5*(fc/Q))**2) * (fc/(2*Q))
nutations[i,:] = np.cos(2*np.pi*nu_local*tp_axis)*np.exp(-tp_axis/50)
extra_params = {'nAvgs':1,'nShots':1,'nReps':1,'B':12220}
dataset = create_dataset_from_axes(nutations.T, [tp_axis,freq_axis],params=extra_params, axes_labels=['pulse0_tp','LO'])
return cls(dataset,f_lims=(freq_axis.min(),freq_axis.max()))
# =============================================================================
# Spectral Position optimisation
[docs]
def ceil(number, decimals=0):
order = decimals*-1
return np.ceil(number / 10**order)*10**order
[docs]
def floor(number, decimals=0):
order = decimals*-1
return np.floor(number / 10**order)*10**order
[docs]
def calc_overlap(x, func1, func2):
"""Calcuates the overlap between two functions.
Parameters
----------
x : np.ndarray
The x axis of the functions
func1 : function
The first function
func2 : function
The second function
Returns
-------
float
The overlap between the two functions.
"""
y1 = func1(x)
y2 = func2(x)
area_1 = np.trapezoid(y1, x)
area_2 = np.trapezoid(y2, x)
y1 /= area_1
y2 /= area_2
area_12 = np.trapezoid(y1*y2, x)
return area_12
[docs]
def optimise_spectra_position(resonator_profile, fieldsweep, verbosity=0):
# band_width = resonator_profile.fc / resonator_profile.q
band_width=0.5
if band_width > 0.4:
n_points = 500
else:
n_points = 250
upper_field_lim = ceil((resonator_profile.fc + band_width), 1)
lower_field_lim = floor((resonator_profile.fc - band_width) , 1)
shift_range = (upper_field_lim - lower_field_lim)/2
x = np.linspace(lower_field_lim,upper_field_lim,n_points)
# smooth_condition = (dl.der_snr(fieldsweep.data)**2)*fieldsweep.data.shape[-1]
smooth_condition = 0.1
edfs_x = np.flip(fieldsweep.fs_x + fieldsweep.freq)
edfs_y = np.flip(fieldsweep.data)
tck_s = splrep(edfs_x, edfs_y, s=smooth_condition)
xc = signal.correlate(resonator_profile.model_func(x),BSpline_extra(tck_s)(x), mode='same')
x_shift = np.linspace(-1*shift_range,shift_range,n_points)
new_LO = x_shift[np.abs(xc).argmax()]
return new_LO
[docs]
class AmplifierLinearityAnalysis:
def __init__(self, dataset,attenuator=0) -> None:
[docs]
self.attenuator = attenuator
if "pulse0_scale" in self.dataset.coords:
self.scales= self.dataset.pulse0_scale
if "pulse0_tp" in self.dataset.coords:
self.tp = self.dataset.pulse0_tp
self._process_fit()
def _process_fit(self,R_limit=0.5,mask=None):
self.n_scales = self.scales.shape[0]
self.profile = np.zeros(self.n_scales)
self.profile_ci = np.zeros(self.n_scales)
fun = lambda x, f, tau,a,k: a*np.cos(2*np.pi*f*x)*np.exp(-x/tau)+ k
bounds = ([5e-3,10,0,-1],[0.3,2000,2,1])
p0 = [50e-3,150,1,0]
R2 = lambda y, yhat: 1 - np.sum((y - yhat)**2) / np.sum((y - np.mean(y))**2)
for i in range(self.n_scales):
if mask is not None:
nutation = self.dataset[mask,i]
x = self.tp[mask]
else:
nutation = self.dataset[:,i]
x = self.tp
if np.iscomplexobj(nutation):
nutation = nutation.epr.correctphase
nutation = nutation/np.max(np.abs(nutation))
if np.abs(np.min(nutation)) > np.max(nutation):
nutation *= -1
try:
results = curve_fit(fun, x, nutation, bounds=bounds,xtol=1e-4,ftol=1e-4,p0=p0)
if R2(nutation,fun(x,*results[0])) > R_limit:
self.profile[i] = results[0][0]
self.profile_ci[i] = np.sqrt(np.diag(results[1]))[0]*1.96
else:
self.profile[i] = np.nan
self.profile_ci[i] = np.nan
except RuntimeError:
self.profile[i] = np.nan
self.profile_ci[i] = np.nan
# Remove nan
self.scales = self.scales[~np.isnan(self.profile)]
self.profile = self.profile[~np.isnan(self.profile)]
self.profile_ci = self.profile_ci[~np.isnan(self.profile_ci)]
# Adjust for attenuator
self.profile = self.profile * 10**(self.attenuator/20)
self.profile_ci = self.profile_ci * 10**(self.attenuator/20)
[docs]
def fit(self,order=5):
""" 5th order polynomial fit to the profile """
if hasattr(self, 'profile'):
self.coefficients = np.polyfit(self.scales, self.profile/self.profile.max(), order)
self.model = np.polyval(self.coefficients, self.scales)
else:
raise ValueError("Profile not calculated. Run _process_fit() first.")
[docs]
def plot(self, axs= None, fig=None):
if axs is None and fig is None:
fig, axs = plt.subplots(1,1,constrained_layout=True,figsize=(8,8))
axs.plot(self.scales, self.profile*1e3, label="Profile", color=primary_colors[0], marker='o', markersize=5, linewidth=0,alpha=0.7)
if hasattr(self, 'model'):
axs.plot(self.scales, self.model*1e3*self.profile.max(), label="Model", color=primary_colors[0], linewidth=2)
axs.text(0.05, 0.95, f"Fit Coeff: {[f'{i:.3g}' for i in self.coefficients]}", transform=axs.transAxes, fontsize=10, verticalalignment='top',bbox=dict(facecolor='white', alpha=0.5, edgecolor='black'))
axs.legend()
axs.set_xlabel("Pulse amplitude / A.U.")
axs.set_ylabel("Nutation frequency / MHz")