Fractional Octave Filter Bank

Module name: octbank

This module implements a fractional octave filter bank. The band passes are realized with butterworth second order sections described by [Stearns2002]. For the second order section filter routines the module sosfiltering is used. With the class FractionalOctaveFilterbank you can create filtering objects that apply to the [IEC-61260].

An example filter bank is shown by the figures below.

(Source code)

References

[Stearns2002]Stearns, Samuel D., Digital Signal Processing with examples in MATLAB
[IEC-61260]Electroacoustics - Octave-band and fractional-octave-band filters

Functions

class pyfilterbank.octbank.FractionalOctaveFilterbank(sample_rate=44100, order=4, nth_oct=3.0, norm_freq=1000.0, start_band=-19, end_band=13, edge_correction_percent=0.01, filterfun='cffi')[source]

Fractional octave filter bank with second order section butterworth band passes.

Parameters:

sample_rate : int

Sampling rate of the signals to be filtered.

order : int

Filter order of the bands. As this are second order sections, it has to be even. Otherweise you’ll get an error.

nth_oct : scalar

Number of bands per octave.

norm_freq : scalar

This is the reference frequency for all fractional octaves placed around this band.

start_band : int

First Band number of fractional octaves below norm_freq.

end_band : int

Last band number of fractional octaves above norm_freq.

edge_correction_percent : scalar

Percentage of widening or narrowing the bands.

filterfun : {‘cffi’, ‘py’, ‘cprototype’}

Function used by the method filter().

Examples

>>> from pyfilterbank import FractionalOctaveFilterbank
>>> from pylab import plt, np
>>>
>>> sample_rate = 44100
>>> ofb = FractionalOctaveFilterbank(sample_rate, order=4)
>>>
>>> x = np.random.randn(4*sample_rate)
>>> y, states = ofb.filter(x)
>>> L = 10 * np.log10(np.sum(y*y,axis=0))
>>> plt.plot(L)

Attributes

center_frequencies (ndarray)
band_edges (ndarray) Frequencies at -3 dB point for all band passes. This are the cross sections of the bands if no edge correction applied.
sosmat (ndarray) Filter coefficient matrix with second order section band passes.
num_bands (int) Number of frequency bands in the filter bank.
band_widths (ndarray) The -3 dB band width of each band pass in the filter bank.

Methods

effective_filter_lengths

Returns an estimate of the effective filter length

filter(x, ffilt=False, states=None)[source]

Filters the input by the settings of the filterbank object.

Parameters:

x : ndarray

Input signal (Nx0)

ffilt : bool

Forward and backward filtering, if Ture.

states : dict

States of all filter sections in the filterbank. Initial you can states=None before block process.

Returns:

y : ndarray

Fractional octave signals of the filtered input x

states : dict

Dictionary containing all filter section states.

filter_mimo_c(x, states=None)[source]

Filters the input by the settings of the filterbank object.

It supports multi channel audio and returns a 3-dim ndarray. Only for real valued signals. No ffilt (backward forward filtering) implemented in this method.

Parameters:

x : ndarray

Signal to be filtered.

states : ndarray or None

States of the filter sections (for block processing).

Returns:

signal : ndarray

Signal array (NxBxC), with N samples, B frequency bands and C-signal channels.

states : ndarray

Filter states of all filter sections.

set_filterfun(filterfun_name)[source]

Set the function that is used for filtering with the method self.filter.

Parameters:

filterfun_name : {‘cffi’, ‘py’, ‘cprototype’}

Three different filter functions, ‘cffi’ is the fastest, ‘py’ is implemented with lfilter.

class pyfilterbank.octbank.ThirdOctFFTLevel(fmin=30, fmax=17000, nfft=16384, fs=44100, flag_mean=False)[source]

Third octave levels by fft. TODO: rename variables TODO: Write Documentation

Methods

pyfilterbank.octbank.centerfreq_to_bandnum(center_freq, norm_freq, nth_oct)[source]

Returns band number from given center frequency.

pyfilterbank.octbank.design_sosmat_band_passes(order, band_edges, sample_rate, edge_correction_percent=0.0)[source]

Return matrix containig sos coeffs of bandpasses.

Parameters:

order : int

Order of the band pass filters.

band_edges : ndarray

Band edge frequencies for the bandpasses.

sample_rate : scalar

Sample frequency.

edge_correction_percent : scalar

Percentage for the correction of the bandedges. Float between -100 % and 100 %. It can be helpfull dependent on the used filter order. p > 0 widens the band passes.

Returns:

sosmat : ndarray

Second order section coefficients. Each column is one band pass cascade of coefficients.

pyfilterbank.octbank.design_sosmat_low_pass_high_pass_bounds(order, band_edges, sample_rate)[source]

Returns matrix containing sos coeffs of low and highpass. The cutoff frequencies are placed at the first and last band edge.

Note

This funtion is not used anymore.

Parameters:

order : int

Order of the band pass filters.

band_edges : ndarray

Band edge frequencies for the low an highpass.

sample_rate : scalar

Sample rate.

Returns:

sosdict : ndarray

Second order section coefficients, the first column contains the low pass coefs and the second column contains the highpass coeffs.

pyfilterbank.octbank.example_plot()[source]

Creates a plot with freqz() of the default FractionalOctaveFilterbank.

pyfilterbank.octbank.find_nominal_freq(center_frequencies, nominal_frequencies)[source]

Find the nearest nominal frequencies to a given array.

Parameters:

center_frequencies : ndarray

Some frequencies for those the neares neighbours shall be found.

nominal_frequencies : ndarray

The nominal frequencies we want to get the best fitting values to center_frequencies from.

Returns:

nominal_frequencies : generator object

The neares neighbors nomina freqs to the given frequencies.

pyfilterbank.octbank.frequencies_fractional_octaves(start_band, end_band, norm_freq, nth_oct)[source]

Return center and band edge frequencies of fractional octaves.

Parameters:

start_band : int

The starting center frequency at norm_freq`*2^(`start_band/nth_oct).

end_band : int

The last center frequency at norm_freq`*2^(`end_band/nth_oct).

norm_freq : scalar

The center frequency of the band number 0.

nth_oct : scalar

The distance between the center frequencies. For third octaves nth_oct=3.

Returns:

center_frequencies : ndarray

Frequencies spaced in nth_oct from start_band to end_band with the norm_freq at band number 0.

band_edges : ndarray

Edge frequencies (-3 dB points) of the fractional octave bands. With constant relative Bandwidth.

pyfilterbank.octbank.freqz(ofb, length_sec=6, ffilt=False, plot=True)[source]

Computes the IR and FRF of a digital filter.

Parameters:

ofb : FractionalOctaveFilterbank object

length_sec : scalar

Length of the impulse response test signal.

ffilt : bool

Backard forward filtering. Effectiv order is doubled then.

plot : bool

Create Plots or not.

Returns:

x : ndarray

Impulse test signal.

y : ndarray

Impules responses signal of the filters.

f : ndarray

Frequency vector for the FRF.

Y : Frequency response (FRF) of the summed filters.

pyfilterbank.octbank.to_normalized_frequencies(frequencies, sample_rate, clip=True)[source]

Returns normalized frequency array.

Parameters:

frequencies : ndarray

Vector with given frequencies.

sample_rate : scalar

The sample rate. Frequencies beyond Nyquist criterion will be truncated.

Returns:

normalized_frequencies : ndarray

Normalized, truncated frequency array.