qetpy.utils package

Submodules

qetpy.utils.utils module

qetpy.utils.utils.inrange(vals, bounds)

Function for returning a boolean mask that specifies which values in an array are between the specified bounds (inclusive of the bounds).

Parameters:
vals : array_like

A 1-d array of values.

bounds : array_like

The bounds for which we will check if each value in vals is in between. This should be an array of shape (2,). However, a longer array will not throw an error, as the function will just use the first two values

Returns:
mask : ndarray

A boolean array of the same shape as vals. True means that the value was between the bounds, False means that the value was not.

qetpy.utils.utils.stdcomplex(x, axis=0)

Function to return complex standard deviation (individually computed for real and imaginary components) for an array of complex values.

Parameters:
x : ndarray

An array of complex values from which we want the complex standard deviation.

axis : int, optional

Which axis to take the standard deviation of (should be used if the dimension of the array is greater than 1)

Returns:
std_complex : ndarray

The complex standard deviation of the inputted array, along the specified axis.

qetpy.utils.utils.removeoutliers(x, maxiter=20, skewtarget=0.05)

Function to return indices of inlying points, removing points by minimizing the skewness

Parameters:
x : ndarray

Array of real-valued variables from which to remove outliers.

maxiter : float, optional

Maximum number of iterations to continue to minimize skewness. Default is 20.

skewtarget : float, optional

Desired residual skewness of distribution. Default is 0.05.

Returns:
inds : ndarray

Boolean indices indicating which values to select/reject, same length as x.

qetpy.utils.utils.iterstat(data, cut=3, precision=1000.0)

Function to iteratively remove outliers based on how many standard deviations they are from the mean, where the mean and standard deviation are recalculated after each cut.

Parameters:
data : ndarray

Array of data that we want to remove outliers from

cut : float, optional

Number of standard deviations from the mean to be used for outlier rejection

precision : float, optional

Threshold for change in mean or standard deviation such that we stop iterating. The threshold is determined by np.std(data)/precision. This means that a higher number for precision means a lower threshold (i.e. more iterations).

Returns:
datamean : float

Mean of the data after outliers have been removed.

datastd : float

Standard deviation of the data after outliers have been removed

datamask : ndarray

Boolean array indicating which values to keep or reject in data, same length as data.

qetpy.utils.utils.foldpsd(psd, fs)

Return the one-sided version of the inputted two-sided psd.

Parameters:
psd : ndarray

A two-sided psd to be converted to one-sided

fs : float

The sample rate used for the psd

Returns:
f : ndarray

The frequencies corresponding to the outputted one-sided psd

psd_folded : ndarray

The one-sided (folded over) psd corresponding to the inputted two-sided psd

qetpy.utils.utils.calc_psd(x, fs=1.0, folded_over=True)

Return the PSD of an n-dimensional array, assuming that we want the PSD of the last axis.

Parameters:
x : array_like

Array to calculate PSD of.

fs : float, optional

Sample rate of the data being taken, assumed to be in units of Hz.

folded_over : bool, optional

Boolean value specifying whether or not the PSD should be folded over. If True, then the symmetric values of the PSD are multiplied by two, and we keep only the positive frequencies. If False, then the entire PSD is saved, including positive and negative frequencies. Default is to fold over the PSD.

Returns:
f : ndarray

Array of sample frequencies

psd : ndarray

Power spectral density of ‘x’

qetpy.utils.utils.calc_offset(x, fs=1.0, sgfreq=100.0, is_didv=False)

Calculates the DC offset of time series trace.

Parameters:
x : ndarray

Array to calculate offsets of.

fs : float, optional

Sample rate of the data being taken, assumed to be in units of Hz.

sgfreq : float, optional

The frequency of signal generator (if is_didv is True. If False, then this is ignored).

is_didv : bool, optional

If False, average of full trace is returned. If True, then the average of n Periods is returned (where n is the max number of full periods present in a trace).

Returns:
offset : ndarray

Array of offsets with same shape as input x minus the last dimension

std : ndarray

Array of std with same shape as offset

qetpy.utils.utils.lowpassfilter(traces, cut_off_freq=100000, fs=625000.0, order=1)

Applies a low pass filter to the inputted time series traces

Parameters:
traces : ndarray

An array of shape (# traces, # bins per trace).

cut_off_freq : float, int, optional

The cut off 3dB frequency for the low pass filter, defaults to 100kHz.

fs : float, int, optional

Digitization rate of data, defaults to 625e3 Hz.

order : int, optional

The order of the low pass filter, defaults to 1.

Returns:
filt_traces : ndarray

Array of low pass filtered traces with the same shape as inputted traces.

qetpy.utils.utils.align_traces(traces, lgcjustshifts=False, n_cut=5000, cut_off_freq=5000.0, fs=625000.0)

Function to align dIdV traces if each trace does not trigger at the same point. Uses a convolution of the traces to find the time offset.

Parameters:
traces : ndarray

Array of shape (# traces, # bins per trace).

lgcjustshifts : boolean, optional

If False, the aligned traces and the phase shifts are returned. If True, just the phase shifts are returned. Default is False.

n_cut : int, optional

The number of bins to use to do the convolution. Just need enough information to see the periodic signal. Default is 5000.

cut_off_freq : float or int, optional

3dB cut off frequency for filter. Default is 5000 Hz.

fs : float or int, optional

Sample rate of data in Hz. Default is 625e3 Hz.

Returns:
shifts : ndarray

Array of phase shifts for each trace in units of bins.

masked_aligned : masked ndarray, optional

Array of time shift corrected traces, same shape as input traces. The masked array masks the np.NaN values in the time shifted traces so that normal numpy functions will ignore the nan’s in computations.

qetpy.utils.utils.gen_noise(psd, fs=1.0, ntraces=1)

Function to generate noise traces with random phase from a given PSD. The PSD calculated from the generated noise traces should be the equivalent to the inputted PSD as the number of traces goes to infinity.

Parameters:
psd : ndarray

The two-sided power spectral density that will be used to generate the noise.

fs : float, optional

Sample rate of the data being taken, assumed to be in units of Hz.

ntraces : int, optional

The number of noise traces that should be generated. Default is 1.

Returns:
noise : ndarray

An array containing all of the generated noise traces from the inputted PSD. Has shape (ntraces, len(psd)).

Module contents