Beispiel #1
0
    def test_highpass(self):
        filtered = bandPassFilter(self.signal,
                                  sampleRate=self.sampleRate,
                                  highpass=self.highpass)

        self.assertGreater(np.corrcoef(filtered, self.signalHigh)[0, 1], 0.9)
        self.assertAlmostEqual(energy(filtered) / energy(self.signalHigh),
                               1,
                               delta=0.1)
Beispiel #2
0
    def getSignalAtBands(self, i=None, bands=defaultBands):
        """
        Rebuilds the signal from a component i but only in the specified
        frequency bands.

        Parameters
        ----------
        i: Variable type, optional
            * int:  the index of the channel.
            * str:  the name of the channel.
            * list of strings and integers:  a list of channels that will be
              used.
            * slice: a slice selecting the range of channels that will be
              used.
            * None: all the channels will be used.

        bands: dict, optional
            This parameter is used to indicate the bands that are going to be
            used. It is a dict with the name of each band as key and a tuple
            with the lower and upper bounds as value.

        Returns
        -------
        dict of numpy.ndarray (1D or 2D)
            The keys are the same keys the bands dictionary is using. The
            values are the signal filtered in every band at the given index of
            the window. If more than one channel is selected the return object
            will be a dict containing 2D arrays in which each row is a signal
            filtered at the corresponding channel.
        """

        bandsSignals = {}
        for key, band in bands.items():
            bounds = self.getBoundsForBand(band)
            bandsSignals[key] = self._applyFunctionTo(
                lambda x: bandPassFilter(x, self.sampleRate, bounds[0], bounds[
                    1]), i)

        return bandsSignals
Beispiel #3
0
 def test_nopass(self):
     filtered = bandPassFilter(self.signal)
     np.testing.assert_array_equal(self.signal, filtered)
Beispiel #4
0
    def __init__(self, data, sampleRate=None, windowSize=None, names=None,
                 highpass=None, lowpass=None, normalize=False, ICA=False,
                 selectedSignals=None):
        """
        Parameters
        ----------
        data: 2D matrix
            The signals data in the shape (nChannels, nSamples).
        sampleRate: numeric, optional
            The frequency at which the data was recorded. By default its value
            is the lenght of the data.
        windowSize: int, optional
            The size of the window in which the calculations will be done. By
            default its value is the lenght of one second of the data.
        names: list of strings
            A list containing the names of each channel in the same positions
            than data channels.
        highpass: numeric, optional
            The signal will be filtered above this value.
        lowpass: numeric, optional
            The signal will be filtered bellow this value.
        normalize: boolean, optional
            If True, the data will be normalizing using z-scores. Default =
            False.
        ICA: boolean, optional
            If True, Independent Component Analysis will be applied to the data
            . It is applied always after normalization if normalize = True.
            Default: False.
        selectedSignals: list of strings or ints
            If the data file has names asociated to each columns, those columns
            can be selected through the name or the index of the column. If the
            data file hasn't names in the columns, they can be selected just by
            the index.
        """

        self.data = data

        #Names check
        if names:
            self.names = names
        else:
            self.names = [str(i) for i in range(data.shape[0])]

        #Check selected signals
        if selectedSignals:
            self.selectSignals(selectedSignals)

        #SampleRate check
        if not sampleRate:
            self.sampleRate=len(self.data[0])
        else:
            self.sampleRate=sampleRate

        #windowSize check
        if not windowSize:
            self.windowSize = self.sampleRate
        else:
            self.windowSize = windowSize

        #Attributes inicialization
        self.nChannels = len(self.data)
        self.nSamples = len(self.data[0])
        self.startPoint = 0
        self.endPoint = self.nSamples
        self.step=None
        self.iterator = None
        self.duration = self.nSamples/self.sampleRate

        #Preparing inner EEG ovject
        self.prepareEEG(self.windowSize)

        #Lowpass and highpass check
        if lowpass or highpass:
            for i,channel in enumerate(self.data):
                self.data[i]=bandPassFilter(channel, self.sampleRate, highpass,
                                            lowpass)

        #ICA check
        if ICA:
            ica=FastICA()
            self.data=ica.fit_transform(self.data.transpose()).transpose()
            self.names = [str(i) for i in range(self.nChannels)]

        #normilze check
        if normalize:
            self.data=zscore(self.data,axis=1)

        #Setting an initial value for the EEG window
        self.moveEEGWindow(self.startPoint)