lockin

This module contains classes and functions for performing digital lock-in amplifier data analysis.

class kpfm.lockin.LockIn(t, x, fs=None)[source]

A basic digital lock-in amplifier.

Run an input signal x through a digital lock-in amplifier. A finite impulse response (FIR) lock-in filter can be provided by lock or lock2, or a custom FIR filter can be used by directly calling run. After generating the complex lock-in output, the lock-in can be phased by running phase, or autophase. After phasing, the lock-in output channels are X, the in-phase channel and Y, the out-of-phase channel.

Parameters:
  • t (array_like) – Time array
  • x (array_like) – Input signal array
  • fs (float) – Sampling rate

Example

>>> fs = 1000.0
>>> t = np.arange(1000)/fs
>>> A = 1 - 0.1 * t
>>> f = 80 + 0.1 * t
>>> x = A * np.sin(np.cumsum(f)*2*np.pi/fs)
>>> li = LockIn(t, x, fs)

We process the data with a 20 Hz bandwidth lock-in amplifier filter.

>>> li.lock(bw=20.0)
Response:
f   mag       dB
 0.000 1.000    0.000
10.000 0.996   -0.035
20.000 0.500   -6.022
40.025 0.000  -91.020
80.051 0.000 -113.516
500.000 0.000 -204.987

The lock-in amplifier automatically infers the reference frequency. The printed response shows the lock-in amplifier gain at different frequencies. For the output to be valid the gain at the reference frequency must be very small (-60 dB or smaller).

We phase the lock-in amplifier output, and then have the lock-in variables available for use.

>>> li.phase()
>>> li('t') # Shortcut for accessing masked version of the signal.
__init__(t, x, fs=None)[source]
classmethod from_x(Cls, x, fs, t0=0)[source]

Generate the time array internally.

__repr__()[source]
run(f0=None, fir=None)[source]

Run the lock-in amplifier at reference frequency f0, using the finite impulse response filter fir.

lock(bw=None, f0=None, bw_ratio=0.5, coeff_ratio=9.0, coeffs=None, window='blackman')[source]

Standard, windowed finite impulse response filter.

lock_butter(N, f3dB, t_exclude=0, f0=None, print_response=True)[source]

Butterworth filter the lock-in amplifier output

manual_phase(phi0, f0corr=None)[source]

Manually phase the lock-in output with phase phi0 (in radians).

absolute_phase(mask, guess=0.0)[source]

Perform a curve fit

class kpfm.lockin.FIRStateLock(fir, dec, f0, phi0, t0=0, fs=1.0)[source]

Lock-in amplifier object which uses an FIR filter, decimates data, and processes data in batches.

Pass data in with the filt function. Lock-in amplifier output stored in z_out. Time array accessible with the get_t function.

Parameters:
  • fir (array_like) – finite-impulse-response (FIR) filter coefficients
  • dec (int) – Decimation factor (output sampling rate = input sampling rate /dec)
  • f0 (scalar) – Lock-in amplifier reference frequency.
  • phi0 (scalar) – Initial lock-in amplifier phase.
  • t0 (scalar, optional) – Inital time associated with the first incoming data point. Defaults to 0.
  • fs (scalar, optional) – Input sampling rate. Defaults to 1.
__init__(fir, dec, f0, phi0, t0=0, fs=1.0)[source]
class kpfm.lockin.FIRStateLockVarF(fir, dec, f0, phi0, t0=0, fs=1.0)[source]

Variable frequency lock-in amplifier object which uses an FIR filter, decimates data, and processes data in batches.

Pass data in with the filt function. Lock-in amplifier output stored in z_out. Time array corresponding to the data in z_out accessible with the get_t function.

Parameters:
  • fir (array_like) – finite-impulse-response (FIR) filter coefficients
  • dec (int) – Decimation factor (output sampling rate = input sampling rate /dec)
  • f0 (function) – Lock-in amplifier reference frequency as a function of time
  • phi0 (scalar) – Initial lock-in amplifier phase.
  • t0 (scalar, optional) – Inital time associated with the first incoming data point. Defaults to 0.
  • fs (scalar, optional) – Input sampling rate. Defaults to 1.
__init__(fir, dec, f0, phi0, t0=0, fs=1.0)[source]
kpfm.lockin.freq_from_fft(sig, fs)[source]

Estimate frequency from peak of FFT

kpfm.lockin.parabolic(f, x)[source]

Quadratic interpolation for estimating the true position of an inter-sample maximum when nearby samples are known.

f is a vector and x is an index for that vector.

Returns (vx, vy), the coordinates of the vertex of a parabola that goes through point x and its two neighbors.

Example: Defining a vector f with a local maximum at index 3 (= 6), find local maximum if points 2, 3, and 4 actually defined a parabola.

In [3]: f = [2, 3, 1, 6, 4, 2, 3, 1]

In [4]: parabolic(f, argmax(f)) Out[4]: (3.2142857142857144, 6.1607142857142856)

kpfm.lockin.fir_weighted_lsq(weight_func, N)[source]

Return intercept, slope filter coefficients for a linear least squares fit with weight function weight_func, using N most recent points.

kpfm.lockin.lock2(f0, fp, fc, fs, coeff_ratio=8.0, coeffs=None, window='blackman', print_response=True)[source]

Create a gentle fir filter. Pass frequencies below fp, cutoff frequencies above fc, and gradually taper to 0 in between.

These filters have a smoother time domain response than filters created with lock.