spaudiopy.process

Collection of audio processing tools.

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['axes.grid'] = True

import spaudiopy as spa

Memory cached functions

spaudiopy.process.resample_hrirs(hrir_l, hrir_r, fs_hrir, fs_target, jobs_count=None)[source]

Memoized version of resample_hrirs(hrir_l, hrir_r, fs_hrir, fs_target, jobs_count=None)

Resample HRIRs to new SamplingRate(t), using multiprocessing.

Parameters:
  • hrir_l ((g, h) numpy.ndarray) – h(t) for grid position g.

  • hrir_r ((g, h) numpy.ndarray) – h(t) for grid position g.

  • fs_hrir (int) – Current fs(t) of hrirs.

  • fs_target (int) – Target fs(t) of hrirs.

  • jobs_count (int or None, optional) – Number of parallel jobs, ‘None’ employs ‘cpu_count’.

Returns:

  • hrir_l_resampled ((g, h_n) numpy.ndarray) – h_n(t) resampled for grid position g.

  • hrir_r_resampled ((g, h_n) numpy.ndarray) – h_n(t) resampled for grid position g.

  • fs_hrir (int) – New fs(t) of hrirs.

Functions

ambeo_a2b(Ambi_A[, filter_coeffs])

Convert A 'MultiSignal' (type I: FLU, FRD, BLD, BRU) to B AmbiBSignal.

b_to_stereo(Ambi_B)

Downmix B format first order Ambisonics to Stereo.

energy_decay(p)

Energy decay curve (EDC) in dB by Schroeder backwards integration.

frac_octave_filterbank(n, N_out, fs, f_low)

Fractional octave band filterbank.

frac_octave_smoothing(a, smoothing_n[, WEIGHTED])

Fractional octave (weighted) smoothing.

gain_clipping(gain, threshold)

Limit gain factor by soft clipping function.

half_sided_Hann(N)

Design half-sided Hann tapering window of order N (>=3).

hrirs_ctf(hrirs[, MIN_PHASE, freq_lims, ...])

Get common transfer function (CTF) EQ for HRIRs.

ilds_from_hrirs(hrirs[, f_cut, TODB])

Calculate ILDs from HRIRs by high/band-passed broad-band RMS.

itds_from_hrirs(hrirs[, f_cut, upsample])

Calculate ITDs from HRIRs inter-aural cross-correlation (IACC).

lagrange_delay(N, delay)

Return fractional delay filter using lagrange interpolation.

match_loudness(sig_in, sig_target)

Match loundess of input to target, based on RMS and avoid clipping.

pulsed_noise(t_noise, t_pause, fs[, reps, ...])

Pulsed noise train, pink or white.

resample_signal(s_time, fs_current, fs_target)

Resample time signal.

resample_spectrum(single_spec, fs_current, ...)

Resample single sided spectrum, as e.g.

subband_levels(x, width, fs[, power, axis])

Compute the level/power in each subband of subband signals.

spaudiopy.process.resample_signal(s_time, fs_current, fs_target, axis=-1)[source]

Resample time signal.

Parameters:
  • s_time (numpy.ndarray) – Time signal, or signals stacked.

  • fs_current (int)

  • fs_target (int)

  • axis (int, optional) – Axis along which to resample. The default is -1.

Returns:

single_spec_resamp (numpy.ndarray.)

spaudiopy.process.resample_spectrum(single_spec, fs_current, fs_target, axis=-1)[source]

Resample single sided spectrum, as e.g. from np.fft.rfft().

Parameters:
  • single_spec (numpy.ndarray) – Single sided spectrum, or spectra stacked.

  • fs_current (int)

  • fs_target (int)

  • axis (int, optional) – Axis along which to resample. The default is -1.

Returns:

single_spec_resamp (numpy.ndarray.)

spaudiopy.process.hrirs_ctf(hrirs, MIN_PHASE=True, freq_lims=(125, 10000.0), grid_weights=None)[source]

Get common transfer function (CTF) EQ for HRIRs.

Often used to equalize the direction independent coloration of a measurement. Can be used to replace headphone EQ.

Parameters:
  • hrirs (sig.HRIRs)

  • MIN_PHASE (bool, optional) – Minimum phase EQ. The default is True.

  • freq_lims (tuple, optional) – Frequency limits of inversion. The default is (125, 10e3).

  • grid_weights (array_like, optional) – Grid weights of hrirs, None will calculate them. The default is None.

Returns:

eq_taps (np.ndarray) – EQ filter taps, same length as HRIRs.

spaudiopy.process.ilds_from_hrirs(hrirs, f_cut=(1000.0, 20000.0), TODB=True)[source]

Calculate ILDs from HRIRs by high/band-passed broad-band RMS.

Parameters:
  • hrirs (sig.HRIRs)

  • f_cut (float (2,), optional) – Band-pass cutoff frequencies. The default is (1000, 20000).

  • TODB (bool, optional) – ILD in dB RMS ratio, otherwise as RMS difference. The default is TRUE.

Returns:

ild (array_like) – ILD per grid point, positive value indicates left ear louder.

spaudiopy.process.itds_from_hrirs(hrirs, f_cut=(100, 1500.0), upsample=4)[source]

Calculate ITDs from HRIRs inter-aural cross-correlation (IACC).

The method calculates IACC on energy of upsampled, filtered HRIRs.

Parameters:
  • hrirs (sig.HRIRs)

  • f_cut (float (2,), optional) – Band-pass cutoff frequencies. The default is (100, 1500).

  • upsample (int, optional) – Upsampling factor. The default is 4.

Returns:

itd (array_like) – ITD in seconds per grid point, positive value indicates left ear first.

References

Andreopoulou, A., & Katz, B. F. G. (2017). Identification of perceptually relevant methods of inter-aural time difference estimation. JASA.

spaudiopy.process.match_loudness(sig_in, sig_target)[source]

Match loundess of input to target, based on RMS and avoid clipping.

Parameters:
  • sig_in ((n, c) array_like) – Input(t) samples n, channel c.

  • sig_target ((n, c) array_like) – Target(t) samples n, channel c.

Returns:

sig_out ((n, c) array_like) – Output(t) samples n, channel c.

spaudiopy.process.ambeo_a2b(Ambi_A, filter_coeffs=None)[source]

Convert A ‘MultiSignal’ (type I: FLU, FRD, BLD, BRU) to B AmbiBSignal.

Parameters:
  • Ambi_A (sig.MultiSignal) – Input signal.

  • filter_coeffs (string) – Picklable file that contains b0_d, a0_d, b1_d, a1_d.

Returns:

Ambi_B (sig.AmbiBSignal) – B-format output signal.

spaudiopy.process.b_to_stereo(Ambi_B)[source]

Downmix B format first order Ambisonics to Stereo.

Parameters:

Ambi_B (sig.AmbiBSignal) – B-format output signal.

Returns:

L, R (array_like)

spaudiopy.process.lagrange_delay(N, delay)[source]

Return fractional delay filter using lagrange interpolation.

For best results, delay should be near N/2 +/- 1.

Parameters:
  • N (int) – Filter order.

  • delay (float) – Delay in samples.

Returns:

h ((N+1,) array_like) – FIR Filter.

spaudiopy.process.frac_octave_smoothing(a, smoothing_n, WEIGHTED=True)[source]

Fractional octave (weighted) smoothing.

Parameters:
  • a ((n,) array_like) – Input spectrum.

  • smoothing_n (int) – 1 / smoothing_n octave band.

  • WEIGHTED (bool, optional) – Use (hamming) weighting on mean around center. The default is True.

Returns:

smoothed_a ((n,) np.array)

spaudiopy.process.frac_octave_filterbank(n, N_out, fs, f_low, f_high=None, mode='energy', overlap=0.5, slope_l=3)[source]

Fractional octave band filterbank.

Design of digital fractional-octave-band filters with energy conservation and perfect reconstruction.

Parameters:
  • n (int) – Octave fraction, e.g. n=3 third-octave bands.

  • N_out (int) – Number of non-negative frequency bins [0, fs/2].

  • fs (int) – Sampling frequency in Hz.

  • f_low (int) – Center frequency of first full band in Hz.

  • f_high (int) – Cutoff frequency in Hz, above which no further bands are generated.

  • mode (‘energy’ or ‘amplitude’) – ‘energy’ produces -3dB at crossover, ‘amplitude’ -6dB.

  • overlap (float) – Band overlap, should be between [0, 0.5].

  • slope_l (int) – Band transition slope, implemented as recursion order l.

Returns:

  • g ((b, N) np.ndarray) – Band gains for non-negative frequency bins.

  • ff ((b, 3) np.ndarray) – Filter frequencies as [f_lo, f_c, f_hi].

Notes

This filterbank is originally designed such that the sum of gains squared sums to unity. The alternative ‘amplitude’ mode ensures that the gains sum directly to unity.

References

Antoni, J. (2010). Orthogonal-like fractional-octave-band filters. The Journal of the Acoustical Society of America, 127(2), 884–895.

Examples

fs = 44100
N = 2**16
gs, ff = spa.process.frac_octave_filterbank(n=1, N_out=N, fs=fs,
                                            f_low=100, f_high=8000)
f = np.linspace(0, fs//2, N)
fig, ax = plt.subplots(2, 1, constrained_layout=True)
ax[0].semilogx(f, gs.T)
ax[0].set_title('Band gains')
ax[1].semilogx(f, np.sum(np.abs(gs)**2, axis=0))
ax[1].set_title(r"$\sum |g| ^ 2$")
for a_idx in ax:
    a_idx.grid(True)
    a_idx.set_xlim([20, fs//2])
    a_idx.set_xlabel('f in Hz')
    a_idx.set_ylabel('Amplitude')

(png, hires.png, pdf)

_images/spaudiopy-process-2.png
spaudiopy.process.subband_levels(x, width, fs, power=False, axis=-1)[source]

Compute the level/power in each subband of subband signals.

spaudiopy.process.energy_decay(p)[source]

Energy decay curve (EDC) in dB by Schroeder backwards integration.

Parameters:

p (array_like)

Returns:

rd (array_like)

spaudiopy.process.half_sided_Hann(N)[source]

Design half-sided Hann tapering window of order N (>=3).

spaudiopy.process.gain_clipping(gain, threshold)[source]

Limit gain factor by soft clipping function. Limits gain factor to +6dB beyond threshold point. (Pass values as factors/ratios, not dB!)

Parameters:
  • gain (array_like)

  • threshold (float)

Returns:

gain_clipped (array_like)

Examples

x = np.linspace(-10, 10, 1000)
lim_threshold = 2.5
y = spa.process.gain_clipping(x, lim_threshold)
plt.figure()
plt.plot(x, x, '--', label='In')
plt.plot(x, y, label='Out')
plt.legend()
plt.xlabel('In')
plt.ylabel('Out')
plt.grid(True)

(png, hires.png, pdf)

_images/spaudiopy-process-3.png
spaudiopy.process.pulsed_noise(t_noise, t_pause, fs, reps=10, t_fade=0.02, pink_noise=True, normalize=True)[source]

Pulsed noise train, pink or white.

Parameters:
  • t_noise (float) – t in s for pulse.

  • t_pause (float) – t in s between pulses.

  • fs (int) – Sampling frequency.

  • reps (int, optional) – Repetitions (independent). The default is 10.

  • t_fade (float, optional) – t in s for fade in and out. The default is 0.02.

  • pink_noise (bool, optional) – Use ‘pink’ (1/f) noise. The default is True

  • normalize (bool, optional) – Normalize output. The default is True.

Returns:

s_out (array_like) – output signal.