-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added framework for filtering data
- Implemented a Bessel low pass filter
- Loading branch information
1 parent
2afafc2
commit d97a31d
Showing
8 changed files
with
284 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
Implementation of an 'N' order Bessel filter | ||
Author: Arvind Balijepalli | ||
Created: 7/1/2013 | ||
ChangeLog: | ||
7/1/13 AB Initial version | ||
""" | ||
import numpy as np | ||
import scipy.signal as sig | ||
|
||
import metaIOFilter | ||
|
||
class besselLowpassFilter(metaIOFilter.metaIOFilter): | ||
""" | ||
""" | ||
def __init__(self, **kwargs): | ||
""" | ||
Keyword Args: | ||
In addition to metaIOFilter.__init__ args, | ||
filterOrder the filter order | ||
filterCutoff filter cutoff frequency in Hz | ||
""" | ||
# base class processing last | ||
super(besselLowpassFilter, self).__init__(**kwargs) | ||
|
||
try: | ||
self.filterOrder=float(kwargs['filterOrder']) | ||
self.filterCutoff=float(kwargs['filterCutoff']) | ||
except KeyError: | ||
print "Missing mandatory arguments 'filterOrder' or 'filterCutoff'" | ||
|
||
|
||
def filterData(self, icurr, Fs): | ||
""" | ||
Filter self.eventData with the filter model setup in __init__ | ||
""" | ||
self.eventData=icurr | ||
self.Fs=Fs | ||
|
||
self.filterModel=sig.filter_design.bessel( | ||
N=self.filterOrder, | ||
Wn=(self.filterCutoff/(self.Fs/2)), | ||
btype='lowpass', | ||
analog=False, | ||
output='ba' | ||
) | ||
|
||
# calculate the initial state of the filter and scale it with the first data point | ||
# so there is no sharp transient at the start of the data | ||
zi=sig.lfilter_zi(b=self.filterModel[0], a=self.filterModel[1])*self.eventData[0] | ||
|
||
[self.eventData, zf]=sig.lfilter( | ||
b=self.filterModel[0], | ||
a=self.filterModel[1], | ||
x=self.eventData, | ||
zi=zi | ||
) | ||
|
||
def formatsettings(self): | ||
""" | ||
Return a formatted string of filter settings | ||
""" | ||
fmtstr="" | ||
|
||
fmtstr+='\tFilter settings: \n' | ||
fmtstr+='\t\tFilter type = {0}\n'.format(self.__class__.__name__) | ||
fmtstr+='\t\tFilter order = {0}\n'.format(self.filterOrder) | ||
fmtstr+='\t\tFilter cutoff = {0} kHz\n'.format(self.filterCutoff*1e-3) | ||
fmtstr+='\t\tDecimation = {0}\n'.format(self.decimate) | ||
|
||
return fmtstr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
""" | ||
A meta class that defines the interface for filtering data that is read in by any | ||
implementation of metaTrajIO | ||
Author: Arvind Balijepalli | ||
Created: 7/1/2013 | ||
ChangeLog: | ||
7/1/13 AB Initial version | ||
""" | ||
from abc import ABCMeta, abstractmethod | ||
import util | ||
|
||
class metaIOFilter(object): | ||
""" | ||
Defines the interface for specific filter implementations. Each filtering | ||
algorithm must sub-class metaIOFilter and implement the following abstract | ||
function: | ||
filterData: apply a filter to self.eventData | ||
""" | ||
__metaclass__=ABCMeta | ||
|
||
def __init__(self, **kwargs): | ||
""" | ||
Keyword Args: | ||
decimate sets the downsampling ratio of the filtered data (default:1, no decimation). | ||
Properties: | ||
filteredData list of filtered and decimated data | ||
filterFs sampling frequency after filtering and decimation | ||
""" | ||
self.decimate=int(kwargs.pop('decimate', 1)) | ||
|
||
@abstractmethod | ||
def filterData(self, icurr, Fs): | ||
""" | ||
This is the equivalent of a pure virtual function in C++. Specific filtering | ||
algorithms must implement this method and then call this base function using super for | ||
additional processing. | ||
Implementations of this method MUST store (1) a ref to the raw event data in self.eventData AND | ||
(2) the sampling frequence in self.Fs. | ||
Args: | ||
icurr ionic current in pA | ||
Fs original sampling frequency in Hz | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def formatsettings(self): | ||
""" | ||
Return a formatted string of filter settings | ||
""" | ||
pass | ||
|
||
@property | ||
def filteredData(self): | ||
""" | ||
Decimate the data when it is accessed | ||
""" | ||
# return util.decimate(self.eventData, self.decimate) | ||
return self.eventData[::self.decimate] | ||
|
||
@property | ||
def filterFs(self): | ||
return self.Fs/self.decimate | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.