pyfar.dsp

Functions:

group_delay(signal[, frequencies, method])

Returns the group delay of a signal in samples.

phase(signal[, deg, unwrap])

Returns the phase for a given signal object.

regularized_spectrum_inversion(signal, …)

Invert the spectrum of a signal applying frequency dependent regularization.

spectrogram(signal[, dB, log_prefix, …])

Compute the magnitude spectrum versus time.

wrap_to_2pi(x)

Wraps phase to 2 pi.

pyfar.dsp.group_delay(signal, frequencies=None, method='fft')[source]

Returns the group delay of a signal in samples.

Parameters
  • signal (Signal object) – An audio signal object from the pyfar signal class

  • frequencies (number array like) – Frequency or frequencies in Hz at which the group delay is calculated. The default is None, in which case signal.frequencies is used.

  • method ('scipy', 'fft', optional) – Method to calculate the group delay of a Signal. Both methods calculate the group delay using the method presented in 1 avoiding issues due to discontinuities in the unwrapped phase. Note that the scipy version additionally allows to specify frequencies for which the group delay is evaluated. The default is ‘fft’, which is faster.

Returns

group_delay – Frequency dependent group delay in samples. The array is flattened if a single channel signal was passed to the function.

Return type

numpy array

References

1

https://www.dsprelated.com/showarticle/69.php

pyfar.dsp.phase(signal, deg=False, unwrap=False)[source]

Returns the phase for a given signal object.

Parameters
  • signal (Signal, FrequencyData) – pyfar Signal or FrequencyData object.

  • deg (Boolean) – Specifies, whether the phase is returned in degrees or radians.

  • unwrap (Boolean) – Specifies, whether the phase is unwrapped or not. If set to “360”, the phase is wrapped to 2 pi.

Returns

phase – Phase.

Return type

np.array()

pyfar.dsp.regularized_spectrum_inversion(signal, freq_range, regu_outside=1.0, regu_inside=1e-10, regu_final=None)[source]

Invert the spectrum of a signal applying frequency dependent regularization. Regularization can either be specified within a given frequency range using two different regularization factors, or for each frequency individually using the parameter regu_final. In the first case the regularization factors for the frequency regions are cross-faded using a raised cosine window function with a width of math:f*sqrt(2) above and below the given frequency range. Note that the resulting regularization function is adjusted to the quadratic maximum of the given signal. In case the regu_final parameter is used, all remaining options are ignored and an array matching the number of frequency bins of the signal needs to be given. In this case, no normalization of the regularization function is applied. Finally, the inverse spectrum is calculated as 2, 3,

S^{-1}(f) = \frac{S^*(f)}{S^*(f)S(f) + \epsilon(f)}

Parameters
  • signal (pyfar.Signal) – The signals which spectra are to be inverted.

  • freq_range (tuple, array_like, double) – The upper and lower frequency limits outside of which the regularization factor is to be applied.

  • regu_outside (float, optional) – The normalized regularization factor outside the frequency range. The default is 1.

  • regu_inside (float, optional) – The normalized regularization factor inside the frequency range. The default is 10**(-200/20).

  • regu_final (float, array_like, optional) – The final regularization factor for each frequency, by default None. If this parameter is set, the remaining regularization factors are ignored.

Returns

The resulting signal after inversion.

Return type

pyfar.Signal

References

2

O. Kirkeby and P. A. Nelson, “Digital Filter Designfor Inversion Problems in Sound Reproduction,” J. Audio Eng. Soc., vol. 47, no. 7, p. 13, 1999.

3

P. C. Hansen, Rank-deficient and discrete ill-posed problems: numerical aspects of linear inversion. Philadelphia: SIAM, 1998.

pyfar.dsp.spectrogram(signal, dB=True, log_prefix=20, log_reference=1, window='hann', window_length=1024, window_overlap_fct=0.5)[source]

Compute the magnitude spectrum versus time.

This is a wrapper for scipy.signal.spectogram with two differences. First, the returned times refer to the start of the FFT blocks, i.e., the first time is always 0 whereas it is window_length/2 in scipy. Second, the returned spectrogram is normalized accroding to signal.signal_type and signal.fft_norm.

Parameters
  • signal (Signal) – pyfar Signal object.

  • db (Boolean) – False to plot the logarithmic magnitude spectrum. The default is True.

  • log_prefix (integer, float) – Prefix for calculating the logarithmic time data. The default is 20.

  • log_reference (integer) – Reference for calculating the logarithmic time data. The default is 1.

  • window (str) – Specifies the window (See scipy.signal.get_window). The default is ‘hann’.

  • window_length (integer) – Specifies the window length in samples. The default ist 1024.

  • window_overlap_fct (double) – Ratio of points to overlap between fft segments [0…1]. The default is 0.5

Returns

  • frequencies (numpy array) – Frequencies in Hz at which the magnitude spectrum was computed

  • times (numpy array) – Times in seconds at which the magnitude spectrum was computed

  • spectrogram (numpy array)

pyfar.dsp.wrap_to_2pi(x)[source]

Wraps phase to 2 pi.

Parameters

x (double) – Input phase to be wrapped to 2 pi.

Returns

x – Phase wrapped to 2 pi.

Return type

double