Module audian.bufferedfilter

Filter data on the fly.

Classes

class BufferedFilter (name='filtered', source='data', panel='trace', color='#00ee00', lw_thin=1.1, lw_thick=2)

Random access to time-series data of which only a part is held in memory.

This is a base class for accessing large audio recordings either from a file (class AudioLoader) or by computing its contents on the fly (e.g. filtered data, envelopes or spectrograms). The BufferedArray behaves like a single big ndarray with first dimension indexing the frames and second dimension indexing the channels of the data. Higher dimensions are also supported. For example, a third dimension for frequencies needed for spectrograms. Internally the class holds only a part of the data in memory. The size of this buffer is set to bufferframes frames. If more data are requested, the buffer is enlarged accordingly.

Classes inheriting BufferedArray just need to implement

self.load_buffer(offset, nsamples, pbuffer)

This function needs to load the supplied pbuffer with nframes frames of data starting at frame offset.

In the constructor or some kind of opening function, you need to set the following member variables, followed by a call to init_buffer():

self.rate            # number of frames per second
self.channels        # number of channels per frame
self.frames          # total number of frames
self.shape = (self.frames, self.channels, ...)        
self.bufferframes    # number of frames the buffer should hold
self.backframes      # number of frames kept for moving back
self.init_buffer()

or provide all this information via the constructor:

Parameters

rate : float
The sampling rate of the data in seconds.
channels : int
The number of channels.
frames : int
The number of frames.
bufferframes : int
Number of frames the curent data buffer holds.
backframes : int
Number of frames the curent data buffer should keep before requested data ranges.
verbose : int
If larger than zero show detailed error/warning messages.

Attributes

rate : float
The sampling rate of the data in seconds.
channels : int
The number of channels.
frames : int
The number of frames. Same as len().
shape : tuple
Frames and channels of the data. Optional higher dimensions.
ndim : int
Number of dimensions: 2 (frames and channels) or higher.
size : int
Total number of samples: frames times channels.
offset : int
Index of first frame in the current buffer.
buffer : ndarray of floats
The curently available data. First dimension is time, second channels. Optional higher dimensions according to ndim and shape.
bufferframes : int
Number of samples the curent data buffer holds.
backframes : int
Number of samples the curent data buffer should keep before requested data ranges.
follow : int
If zero (default), move buffer position only for requests outside the current buffer. If larger than zero then buffer position follows requested data ranges if buffer can be moved by more than followframes. This results in more frequent but smaller buffer updates. Set it after calling the constructor or init_buffer().
buffer_changed : ndarray of bool
For each channel a flag, whether the buffer content has been changed. Set to True, whenever load_buffer() was called.

Methods

  • len(): Number of frames.
  • __getitem__: Access data.
  • blocks(): Generator for blockwise processing of AudioLoader data.
  • update_buffer(): make sure that the buffer contains data of a range of indices.
  • update_time(): make sure that the buffer contains data of a given time range.
  • reload_buffer(): reload the current buffer.
  • load_buffer(): load a range of samples into a buffer.
  • move_buffer(): move and resize buffer.
  • buffer_position(): compute position and size of buffer.
  • recycle_buffer(): move buffer to new position and recycle content if possible.

Notes

Access via __getitem__ or __next__ is slow! Even worse, using numpy functions on this class first converts it to a numpy array - that is something we actually do not want! We should subclass directly from numpy.ndarray . For details see http://docs.scipy.org/doc/numpy/user/basics.subclassing.html When subclassing, there is an offset argument, that might help to speed up __getitem__ .

Construtor for initializing 2D arrays (times x channels).

Expand source code
class BufferedFilter(BufferedData):

    def __init__(self, name='filtered', source='data', panel='trace',
                 color='#00ee00', lw_thin=1.1, lw_thick=2):
        super().__init__(name, source, tbefore=10, panel=panel,
                         panel_type='trace', color=color,
                         lw_thin=lw_thin, lw_thick=lw_thick)
        self.highpass_cutoff = 0
        self.lowpass_cutoff = 1
        self.filter_order = 2
        self.sos = None

        
    def open(self, source):
        super().open(source)
        self.highpass_cutoff = 0
        self.lowpass_cutoff = self.rate/2
        self.filter_order = 2
        self.sos = None
        self.update()


    def process(self, source, dest, nbefore):
        if self.sos is None:
            dest[:, :] = source[nbefore:, :]
        else:
            for c in range(self.channels):
                dest[:, c] = sosfilt(self.sos, source[:, c],)[nbefore:]

            
    def update(self):
        if self.highpass_cutoff < 0.001*self.rate/2 and \
           self.lowpass_cutoff >= self.rate/2 - 1e-8:
            self.sos = None
        elif self.highpass_cutoff < 0.001*self.rate/2:
            self.sos = butter(self.filter_order, self.lowpass_cutoff,
                              'lowpass', fs=self.rate, output='sos')
        elif self.lowpass_cutoff >= self.rate/2 - 1e-8:
            self.sos = butter(self.filter_order, self.highpass_cutoff,
                              'highpass', fs=self.rate, output='sos')
        else:
            self.sos = butter(self.filter_order,
                              (self.highpass_cutoff, self.lowpass_cutoff),
                              'bandpass', fs=self.rate, output='sos')
        self.recompute_all()

Ancestors

Methods

def open(self, source)
def process(self, source, dest, nbefore)
def update(self)