psiaudio.util module

analyze_mic_sens(ref_waveforms, exp_waveforms, vrms, ref_mic_gain, exp_mic_gain, output_gain, ref_mic_sens, **kwargs)
analyze_tone(waveforms, frequency, fs, mic_gain, trim=0, thd_harmonics=3)
as_numeric(x)
band_to_spectrum_level(band_db, flb, fub)

Convert overall band level to spectrum level

check_interleaved_octaves(freqs, min_octaves=1)

Ensure that frequencies are spaced at least an octave apart

Parameters
  • freqs (ordered sequence) – Sequence of frequencies in the desired ordering.

  • min_octaves (float) – Minimum octave spacing to enforce (this is multiplied by 0.95 to allow for some fudge factor if frequencies were rounded).

Notes

  • If you are rounding frequencies to the nearest Hz, the actual octaves spacing may be slightly less or more than the desired spacing due to the rounding. We multiply min_octaves by 0.99 to allow for this small error in octave spacing.

  • This also checks that the last frequency is an octave from the first frequency.

csd(s, window=None, waveform_averages=None, detrend='linear')
csd_df(s, fs, *args, **kw)
csd_to_signal(csd)
db(target, reference=1)
dbi(db, reference=1)
dbtopa(db)

Convert dB SPL to Pascal

\[10^{dB/20.0}/(20\cdot10^{-6})\]
>>> round(dbtopa(94), 4)
1.0024
>>> dbtopa(100)
2.0
>>> dbtopa(120)
20.0
>>> patodb(dbtopa(94.0))
94.0

Will also take sequences:

>>> print(dbtopa([80, 100, 120]))
[ 0.2  2.  20. ]
diff_matrix(n_chan, reference, labels=None)
freq_smooth(frequency, power, bandwidth=20)

Uses Konno & Ohmachi (1998) algorithm

golay_ir(n, a, b, a_signal, b_signal)

Estimate system impulse response from Golay sequence

Implements algorithm described in Zhou et al. 1992

golay_pair(n=15)

Generate pair of Golay sequences

golay_tf(a, b, a_signal, b_signal, fs)

Estimate system transfer function from Golay sequence

Implements algorithm as described in Zhou et al. 1992.

iir(psd, phase, frequency, cutoff=None, phase_correction=None, truncate=None, truncate_spectrum=False, reference='mean')

Given the impulse response, compute the inverse impulse response.

Parameters

TODO (#) –

Note

Specifying the cutoff range is highly recommended to get a well-behaved function.

interleave_octaves(freqs, min_octaves=1)

Return correct ordering for frequencies in interleaved paradigm as per. Buran et al.

This function works with both kHz and Hz.

>>> interleave_octaves([2, 2.8, 4, 5.6, 8])
[8, 4, 2, 5.6, 2.8]
>>> interleave_octaves([2000, 2800, 4000, 5600, 8000])
[8000, 4000, 2000, 5600, 2800]

If a set of frequencies cannot appropriately be ordered, a ValueError is raised. In this example, the first and last frequences are within one octave.

>>> interleave_octaves([2000, 2800, 4000])
Traceback (most recent call last):
  ...
ValueError: Unable to interleave 4000, 2000, 2800 appropriately.

You can use a different octave spacing.

>>> interleave_octaves([2000, 2800, 4000], 0.5)
[4000, 2800, 2000]
ir_iir(impulse_response, fs, smooth=None, *args, **kwargs)
normalize_rms(waveform, out=None)

Normalize RMS power to 1 (typically used when generating a noise waveform that will be scaled by a calibration factor)

waveformarray_like

Input array.

outarray_like

An array to store the output. Must be the same shape as waveform.

octave_space(lb, ub, step, mode='nearest')
>>> print(octave_space(4, 32, 1.0))
[ 4.  8. 16. 32.]
>>> freq = octave_space(0.5, 50.0, 0.25, 'nearest')
>>> print(round(min(freq), 2))
0.5
>>> print(round(max(freq), 2))
53.82
>>> freq = octave_space(0.5, 50.0, 0.25, 'bounded')
>>> print(round(min(freq), 2))
0.5
>>> print(round(max(freq), 2))
45.25
patodb(pa)

Convert Pascal to dB SPL

\[20*log10(pa/20e-6)\]
>>> round(patodb(1))
94
>>> patodb(2)
100.0
>>> patodb(0.2)
80.0

Will also take sequences: >>> print(patodb([0.2, 2.0, 20.0])) [ 80. 100. 120.]

phase(s, fs, window=None, waveform_averages=None, unwrap=True)
process_tone(fs, signal, frequency, min_snr=None, max_thd=None, thd_harmonics=3, silence=None)

Compute the RMS at the specified frequency. Check for distortion.

Parameters
  • fs (float) – Sampling frequency of signal

  • signal (ndarray) – Last dimension must be time. If more than one dimension, first dimension must be repetition.

  • frequency (float) – Frequency (Hz) to analyze

  • min_snr ({None, float}) – If specified, must provide a noise floor measure (silence). The ratio, in dB, of signal RMS to silence RMS must be greater than min_snr. If not, a CalibrationNFError is raised.

  • max_thd ({None, float}) – If specified, ensures that the total harmonic distortion, as a percentage, is less than max_thd. If not, a CalibrationTHDError is raised.

  • thd_harmonics (int) – Number of harmonics to compute. If you pick too many, some harmonics may be above the Nyquist frequency and you’ll get an exception.

  • thd_harmonics – Number of harmonics to compute. If you pick too many, some harmonics may be above the Nyquist frequency and you’ll get an exception.

  • silence ({None, ndarray}) – Noise floor measurement. Required for min_snr. Shape must match signal in all dimensions except the first and last.

Returns

result – Series will be indexed with RMS, SNR, THD and frequency. DataFrame will contain columns for RMS, SNR, THD and frequency. The return type will depend on the dimensionality of the input array.

Return type

pandas Series or DataFrame

psd(s, fs, window=None, waveform_averages=None, trim_samples=True)
psd_bootstrap(x, fs, n_draw=400, n_bootstrap=100, rng=None, window=None)
psd_df(s, fs, *args, waveform_averages=None, **kw)
psd_freq(s, fs)
resample_fft(waveform, fs, target_fs)
resample_poly(waveform, fs, target_fs)
rms(s, detrend=False)
rms_rfft(x)
spectrum_to_band_level(spectrum_db, flb, fub)

Convert overall band level to spectrum level

summarize_golay(fs, a, b, a_response, b_response, waveform_averages=None)
thd(s, fs, frequency, harmonics=3, window=None)
tone_conv(s, fs, frequency, window=None)
tone_phase_conv(s, fs, frequency, window=None)
tone_phase_fft(s, fs, frequency, window=None)
tone_power_conv(s, fs, frequency, window=None)
tone_power_conv_nf(s, fs, frequency, window=None)
tone_power_fft(s, fs, frequency, window=None)
transfer_function(stimulus, response, fs)
truncated_ifft(spectrum, original_fs, truncated_fs)