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)-
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()
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). TheBufferedArray
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 tobufferframes
frames. If more data are requested, the buffer is enlarged accordingly.Classes inheriting
BufferedArray
just need to implementself.load_buffer(offset, nsamples, pbuffer)
This function needs to load the supplied
pbuffer
withnframes
frames of data starting at frameoffset
.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
offloats
- The curently available data. First dimension is time, second channels.
Optional higher dimensions according to
ndim
andshape
. 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.
buffer_changed
:ndarray
ofbool
- For each channel a flag, whether the buffer content has been changed.
Set to
True
, wheneverload_buffer()
was called.
Methods
len()
: Number of frames.__getitem__
: Access data.blocks()
: Generator for blockwise processing of the 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.move_buffer()
: move and resize buffer (called by update_buffer()).load_buffer()
: load a range of samples into a buffer (called by reload_buffer() and move_buffer())._buffer_position()
: compute position and size of buffer (used by update_buffer())._recycle_buffer()
: move buffer to new position and recycle content if possible (called by move_buffer()).allocate_buffer()
: reallocate the buffer to have the right size (called by _recycle_buffer()).
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).
Ancestors
- BufferedData
- audioio.bufferedarray.BufferedArray
Methods
def open(self, source)
-
Expand source code
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)
-
Expand source code
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)
-
Expand source code
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()