neurotools.signal.phase module

Routines for working with analytic signals.

neurotools.signal.phase.amp(x)[source]

Extracts amplitude envelope using Hilbert transform. x must be narrow-band. No padding is performed — watch out for boundary effects.

Parameters:

x (np.array) – numeric time-series data

Returns:

abs(hilbert(x))

Return type:

np.array

neurotools.signal.phase.ifreq(x, Fs=1000, mode='pad')[source]

Extract the instantaneous frequency from a narrow-band signal using the Hilbert transform.

Parameters:
  • x (np.array) – numeric time-series data

  • Fs (int) – defaults to 1000

  • mode (str) – ‘pad’ will return a signal of the original length ‘valid’ will return a signal 1 sample shorter, with derivative computed between each pair of points in the original signal.

neurotools.signal.phase.unwrapped_angle(z)[source]

Get the argument of a complex timeseries with phase unwrapping.

Parameters:

z (np.complex64)

neurotools.signal.phase.pdiff(x)[source]

Take the derivative of a sequence of phases. Times when this derivative wraps around form 0 to 2π are correctly handeled.

Parameters:

x (np.array) – Array of phase values in radians to differentiate.

Returns:

dx – Phase derivative of x, with wrapping around 2π handled automatically.

Return type:

np.array

neurotools.signal.phase.rewrap(x)[source]

Used to handle wraparound when getting phase derivatives. See pdiff.

Parameters:

dx (np.array) – Array of phase derivatives, with 2π wrap-around not yet handled.

Returns:

dx – Phase derivative of x, with wrapping around 2π handled automatically.

Return type:

np.array

neurotools.signal.phase.pghilbert(x)[source]

Extract phase derivative in time from a narrow-band real-valued signal using the hilbert transform.

Parameters:

x (np.float32) – Narrow-band real-valued signal to get the phase gradient of.

Returns:

d/dt[phase(x)] – Time derivative in radians/sample

Return type:

np.array

neurotools.signal.phase.unwrap(h)[source]

Unwraps a sequences of phase measurements so that, rather than ranging from 0 to 2π, the values increase (or decrease) continuously.

Parameters:

h (np.float32)

Returns:

Re-wrapped phases, in radians. These phases will continue counting up/down and are not wrapped to [0,2π).

Return type:

np.array

neurotools.signal.phase.ang(x)[source]

Uses the Hilbert transform to extract the phase of x, in radians. X should be narrow-band.

Parameters:

x (np.float32) – Narrow-band real-valued signal to get the phase gradient of.

Returns:

ang – Phase angle of x, in radiance.

Return type:

np.float32

neurotools.signal.phase.fix_derivative(x)[source]

Adjust discrete derivative x to pad-back the 1 sample removed by differentiation, and center the derivative on samples (rather than between them).

Applying this after using diff() is equivalent to using the differentiation kernel [-.5,0,.5] in the interior of the array and [-1,1] at its endpoints

Parameters:

x (np.array) – Derivative signal to fix.

Return type:

np.array

neurotools.signal.phase.phase_rotate(s, f, Fs=1000.0)[source]

Only the phase advancement portion of a resonator.

Parameters:
  • s (np.array) – Analytic signal

  • f (float) – Frequency, in Hz.

  • Fs (positive number; default 1000) – Sampling rate.

Return type:

np.array

neurotools.signal.phase.randband(N, fa=None, fb=None, Fs=1000)[source]

Returns Gaussian random noise band-pass filtered between fa and fb.

Parameters:

N (int) – Number of samples to draw

neurotools.signal.phase.phase_randomize(signal)[source]

Phase randomizes a signal by rotating frequency components by a random angle. Negative frequencies are rotated in the opposite direction. The nyquist frequency, if present, has it’s sign randomly flipped.

Parameters:

signal (1D np.array)

Return type:

np.array

neurotools.signal.phase.phase_randomize_from_amplitudes(amplitudes)[source]

treats input amplitudes as amplitudes of fourier components

neurotools.signal.phase.sign_preserving_amplitude_demodulate(analytic_signal, doplot=False)[source]

Extracts an amplitude-modulated component from an analytic signal, Correctly flipping the sign of the signal when it crosses zero, rather than returning a rectified result.

Sign-changes are heuristically detected basd on the following:
  • An abnormally large skip in phase between two time points, larger than np.pi/2, that is also a local extremum in phase velocity

  • local minima in the amplitude at low-voltage with high curvature

Parameters:
  • analytic_signal (np.arrau)

  • doplot (boolean; default False) – Whether to draw plot

Returns:

demodulated

Return type:

np.array