PowerSpectrum

lumicks.pylake.force_calibration.power_spectrum.PowerSpectrum

class PowerSpectrum(frequency, power, sample_rate, total_duration, unit='V', *, num_points_per_block=1, total_samples_used=None, variance=None)

Power spectrum data for a time series.

frequency

Frequency values for the power spectrum. [Hz]

Type:

numpy.ndarray

power

Power values for the power spectrum (typically in V^2/Hz).

Type:

numpy.ndarray

sample_rate

The sampling rate for the original data. [Hz]

Type:

float

total_duration

The total duration of the original data. [seconds]

Type:

float

compress()

Returns a compressed power spectrum by downsampling the internal data

By default, when calling PowerSpectrum.downsampled_by(100), the raw data is kept at the full frequency resolution internally. When calling compress, this data is discarded and only the downsampled variant is kept. This saves memory, but doesn’t allow calling methods which depend on the data not being downsampled (such as identify_peaks) from being called.

downsampled_by(factor, reduce=<function mean>) PowerSpectrum

Returns a spectrum downsampled by a given factor.

Parameters:
  • factor (int) – Factor to down-sample the spectrum by.

  • reduce (callable) – (Deprecated) Function to use for down-sampling the data. Only np.mean will be supported going forward.

static from_data(data, sample_rate, unit='V', window_seconds=None) PowerSpectrum

Power spectrum

Parameters:
  • data (numpy.ndarray) – Data from which to calculate a power spectrum.

  • sample_rate (float) – Sampling rate at which this data was acquired [Hz].

  • unit (str) – Units the data is in (default: V).

  • window_seconds (float) – Window duration [seconds]. When specified the data is divided into blocks of length window_seconds. Power spectra are computed for each block after which they are averaged. If omitted, no windowing is used.

identify_peaks(model_fun: callable, *, peak_cutoff: float = 20.0, baseline: float = 1.0) List[Tuple[float, float]]

Identify peaks in the power spectrum, based on an exponential probability model with rate parameter (lambda) = 1. This means that the power spectrum cannot be blocked or windowed. This is beta functionality. While usable, this has not yet been tested in a large number of different scenarios. The API can still be subject to change without any prior deprecation notice! If you use this functionality keep a close eye on the changelog for any changes that may affect your analysis.

Parameters:
  • model_fun (callable) – A function of one argument, frequency, that gives the theoretical power spectrum. The function is used to normalize the experimental power spectrum. Note that you can pass an instance of CalibrationResults.

  • peak_cutoff (float) – Indicates what value of the normalized spectrum is deemed abnormally high. Default is 20.0, which corresponds to a chance of about 2 in 1E9 that a peak of that magnitude occurs naturally in an exponential distribution with rate parameter = 1.0. The minimum is baseline (see below).

  • baseline (float) – The baseline level a peak needs to drop down to. Lower means that exponentially more data is considered to be part of the peak. The default is 1.0, and the range of baseline is [0.0, peak_cutoff]. No fit or data smoothing is performed, the peak starts or ends at the first data point that satisfies the criterion.

Returns:

frequency_ranges – f_start is the frequency where a peak starts, and f_stop is the frequency where a peak ends (exclusive).

Return type:

list of tuples (f_start, f_stop)

Raises:
  • ValueError – Raises a ValueError if the function is called on a PowerSpectrum object with blocking applied. This function only works for PowerSpectrum objects without blocking or windowing.

  • ValueError – Raises a ValueError when the peak_cutoff is smaller than baseline and when baseline is less than zero

in_range(frequency_min, frequency_max) PowerSpectrum

Returns part of the power spectrum within a given frequency range.

plot(*, show_excluded=False, **kwargs)

Plot power spectrum

Parameters:
with_spectrum(power, num_points_per_block=1, variance=None) PowerSpectrum

Return a copy with a different spectrum

Parameters:
  • power (numpy.ndarray) – Vector of power spectral values

  • num_points_per_block (int) – Number of points per block used to obtain power spectral values.

  • variance (numpy.ndarray, optional) – Variance of the power spectrum.

Returns:

power_spectrum – Power spectrum with new spectral density values.

Return type:

PowerSpectrum

Raises:

ValueError – If the power spectrum provided has a different length from the current one.

property frequency_bin_width: float

Returns the frequency bin width of the spectrum

property unfiltered_frequency: ndarray

Returns full frequency axis of the power spectrum without exclusion ranges applied.

property unfiltered_power: ndarray

Returns full power spectral density without exclusion ranges applied.