Entry 3199

classFilter

   

Submitted by anonymous on Feb. 13, 2010 at 8:39 p.m.
Language: Python. Code size: 3.7 KB.

################################################################################
# classFilter.py                                                               #
#                                                                              #
#   This is a class that filters QRS data. There is a plot member included for #
#   debugging.                                                                 #
#                                                                              #
#   Stephen Brophy                                                             #
#   13 Feb 2010                                                                #
#                                                                              #
################################################################################

from numpy import *
from pylab import *
from scipy import *

class qrs_filter:
    def __init__(self, data, fs, lb, ub):
        # data = path to the data file
        # lb = lower bound
        # ub = upper bound
        self.data = data
        self.fs = fs
        self.lb = lb
        self.ub = ub
    
    def lowpass(self, filename):
        # Filename is the variable we want to save too
        # Low pass filtering
        #
        # |---|xxxxxxxxx|---|
        #
        
        x = fft(loadtxt(self.data))
        
        index = round(self.ub * size(x)/ self.fs)
        counter = index
        while counter <= (size(x)-index):
            x[counter] = 100
            counter = counter + 1
            
        x = ifft(x)
        savetxt(filename, x)       
            
    def highpass(self, filename):
        # Filename is the variable we want to save too
        
        # High pass filtering
        #
        # xxx|-----------|xxx
        #
        
        x = fft(loadtxt(self.data))
        
        index = round(self.lb * size(x)/self.fs)
        counter = 0
        
        while counter < index:
            x[counter] = 0
            counter = counter + 1
            
        counter = size(x) - index
        
        while counter < size(x):
            x[counter] = 100
            counter = counter + 1
            
        x = ifft(x)
        savetxt(filename, x)

    def bandpass(self, filename):
        # Filename is the variable we want to save too
    
        # Bandpass filtering
        #
        # --|xxx|-----|xxx|--
        #
    
        temp = self.data        # Save the self.data
        self.lowpass(filename)  # Lowpass filter
        self.data = filename
        self.highpass(filename) # Highpass filter
        self.data = temp
        
    def bandstop(self, filename):
        # Filename is the variable we want to save too
    
        # Bandstop filtering
        #
        # xx|---|xxxxx|---|xx
        #
        
        x = fft(loadtxt(self.data))
        
        counter = round(self.lb * size(x)/self.fs)
        index = round(self.ub * size(x)/self.fs)
        
        while counter < index:
            x[counter] = 0
            counter = counter + 1
            
        counter = size(x) - round(self.lb * size(x)/self.fs)
        index = size(x) - round(self.ub * size(x)/self.fs)    
        while counter < index:
            x[counter] = 0
            counter = counter + 1
        
        x = ifft(x)
        savetxt(filename, x)

    def plot(self, filename):
        # This is here for debugging
        x = loadtxt(filename)
        plot(x)
        
        # Setting plot parameters
        grid(True)
        xlabel('Samples [n], fs = ' + str(self.fs))
        ylabel('Magnitude')
        title('Filtered Data - ' + filename)
        #Showing the plot
        show()

    def __str__(self):
        pass

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).