music.fir

music.fir(samples, sonic_vector, freq=True, max_freq=True)[source]

Apply a FIR filter to a sonic_array.

Parameters:
samplesarray_like

A sequence of absolute values for the frequencies (if freq=True) or samples of an impulse response.

sonic_vectorarray_like

An one-dimensional array with the PCM samples of the signal (e.g. sound) for the FIR filter to be applied to.

freqbool

Set to True if samples holds frequency amplitude absolute values or False if samples is an impulse response. If max_freq=True, the separations between the frequencies are: fs / (2 * N - 2). If max_freq=False, the separation between the frequencies are fs / (2 * N - 1). Where N is the length of the provided samples.

max_freqbool

Set to True if the last item in the samples is related to the Nyquist frequency fs / 2. Ignored if freq=False.

Returns:
ndarray

The filtered signal, of length len(sonic_vector) + len(kernel) - 1. The kernel is symmetric, so the filter is linear phase and the output is delayed by half the kernel.

Raises:
ValueError

If either argument is not one-dimensional, or if either is empty. numpy raises for these anyway, but with messages such as “object too deep for desired array”, which name neither the argument nor the problem.

Notes

If freq=True, the samples are the absolute values of the frequency components. The phases are set to zero to maintain the phases of the components of the original signal.

A magnitude response is applied by convolving with its inverse transform, not with the magnitudes themselves. Convolving with them directly – which this did – makes a flat response a boxcar average rather than the identity it should be.

Examples

>>> signal = np.arange(5.)
>>> flat = np.ones(5)  # pass every frequency unchanged
>>> filtered = fir(flat, signal)
>>> np.allclose(filtered[4:9], signal)
True