def test_group_delay_two_channel(impulse_group_delay_two_channel): """Test the function returning the group delay of a signal, two channels.""" signal = impulse_group_delay_two_channel[0] grp = dsp.group_delay(signal, method='scipy') assert grp.shape == (signal.cshape + (signal.n_bins, )) npt.assert_allclose(grp, impulse_group_delay_two_channel[1], rtol=1e-10) grp = dsp.group_delay(signal, method='fft') assert grp.shape == (signal.cshape + (signal.n_bins, )) npt.assert_allclose(grp, impulse_group_delay_two_channel[1], rtol=1e-10)
def test_linear_phase_multichannel(): # test signal N = 64 fs = 44100 x = pf.signals.impulse(N, [0, 0], sampling_rate=fs) # test with scalar group delay y = dsp.linear_phase(x, N / 2) npt.assert_allclose(dsp.group_delay(y[0]), N / 2 * np.ones(y.n_bins)) npt.assert_allclose(dsp.group_delay(y[1]), N / 2 * np.ones(y.n_bins)) # test with array like group delay y = dsp.linear_phase(x, [N / 2, N / 4]) npt.assert_allclose(dsp.group_delay(y[0]), N / 2 * np.ones(y.n_bins)) npt.assert_allclose(dsp.group_delay(y[1]), N / 4 * np.ones(y.n_bins))
def test_group_delay_custom_frequencies(impulse_group_delay): """Test the function returning the group delay of a signal, called for specific frequencies.""" signal = impulse_group_delay[0] # Single frequency, of type int frequency = 1000 frequency_idx = np.abs(signal.frequencies - frequency).argmin() grp = dsp.group_delay(signal, frequency, method='scipy') assert grp.shape == () npt.assert_allclose(grp, impulse_group_delay[1][0, frequency_idx]) # Multiple frequencies frequency = np.array([1000, 2000]) frequency_idx = np.abs(signal.frequencies - frequency[..., np.newaxis]).argmin(axis=-1) grp = dsp.group_delay(signal, frequency, method='scipy') assert grp.shape == (2, ) npt.assert_allclose(grp, impulse_group_delay[1][0, frequency_idx])
def test_linear_phase(): # test signal N = 64 fs = 44100 x = pf.signals.impulse(N, sampling_rate=fs) # test default parameters y = dsp.linear_phase(x, N / 2) # test output assert isinstance(y, pf.Signal) npt.assert_allclose(dsp.group_delay(y), N / 2 * np.ones(y.n_bins)) # test if input did not change npt.assert_allclose(x.time, pf.signals.impulse(N).time) # test group delay in seconds y = dsp.linear_phase(x, N / 2 / fs, unit="s") npt.assert_allclose(dsp.group_delay(y), N / 2 * np.ones(y.n_bins)) # test assertion with pytest.raises(TypeError, match="signal must be a pyfar Signal"): dsp.linear_phase(1, 0) with pytest.raises(ValueError, match="unit is km"): dsp.linear_phase(x, N / 2 / fs, unit="km")
def _group_delay(signal, unit=None, xscale='log', ax=None, **kwargs): """Plot the group delay on the positive frequency axis.""" # check input if not isinstance(signal, Signal): raise TypeError('Input data has to be of type: Signal.') _check_time_unit(unit) _check_axis_scale(xscale) # prepare input kwargs = _return_default_colors_rgb(**kwargs) data = dsp.group_delay(signal) # auto detect the unit if unit is None: unit = _time_auto_unit(np.nanmax(np.abs(data) / signal.sampling_rate)) # set the unit if unit != "samples": factor, unit = _deal_time_units(unit) data = data / signal.sampling_rate * factor # prepare figure _, ax = _prepare_plot(ax) ax.set_xlabel("Frequency in Hz") ax.set_ylabel(f"Group delay in {unit}") ax.grid(True, 'both') _set_axlim(ax, ax.set_xlim, _lower_frequency_limit(signal), signal.frequencies[-1], ax.get_xlim()) _set_axlim(ax, ax.set_ylim, .5 * np.nanmin(data), 1.5 * np.nanmax(data), ax.get_ylim()) # plot data if xscale == 'log': ax.semilogx(signal.frequencies, data.T, **kwargs) else: ax.plot(signal.frequencies, data.T, **kwargs) plt.tight_layout() # set and format ticks if xscale == 'log': ax.xaxis.set_major_locator(LogLocatorITAToolbox()) ax.xaxis.set_major_formatter(LogFormatterITAToolbox()) return ax
def test_group_delay_single_channel(impulse_group_delay): """Test the function returning the group delay of a signal, single channel.""" signal = impulse_group_delay[0] with pytest.raises(ValueError, match="Invalid method"): dsp.group_delay(signal, method='invalid') with pytest.raises(ValueError, match="not supported"): dsp.group_delay(signal, method='fft', frequencies=[1, 2, 3]) grp = dsp.group_delay(signal, method='scipy') assert grp.shape == (signal.n_bins, ) npt.assert_allclose(grp, impulse_group_delay[1].flatten(), rtol=1e-10) grp = dsp.group_delay(signal, method='fft') assert grp.shape == (signal.n_bins, ) npt.assert_allclose(grp, impulse_group_delay[1].flatten(), rtol=1e-10) grp = dsp.group_delay(signal, method='fft') assert grp.shape == (signal.n_bins, ) npt.assert_allclose(grp, impulse_group_delay[1].flatten(), rtol=1e-10)