summaryrefslogtreecommitdiff
path: root/nitime/analysis/spectral.py
blob: 02b54103ed42b3b465a8c51e08b4eaaa1d619f17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
import numpy as np
from nitime.lazy import scipy
from nitime.lazy import scipy_signal as signal
from nitime.lazy import scipy_fftpack as fftpack

from nitime import descriptors as desc
from nitime import utils as tsu
from nitime import algorithms as tsa
from nitime import timeseries as ts

from .base import BaseAnalyzer


class SpectralAnalyzer(BaseAnalyzer):
    """ Analyzer object for spectral analysis"""
    def __init__(self, input=None, method=None, BW=None, adaptive=False,
                 low_bias=False):
        """
        The initialization of the

        Parameters
        ----------
        input: time-series objects

        method: dict (optional),
           The method spec used in calculating 'psd' see
           :func:`algorithms.get_spectra` for details.

        BW: float (optional),
            In 'spectrum_multi_taper' The bandwidth of the windowing function
            will determine the number tapers to use. This parameters represents
            trade-off between frequency resolution (lower main lobe BW for the
            taper) and variance reduction (higher BW and number of averaged
            estimates).

        adaptive : {True/False}
           In 'spectrum_multi_taper', use an adaptive weighting routine to
           combine the PSD estimates of different tapers.

        low_bias: {True/False}
           In spectrum_multi_taper, use bias correction


        Examples
        --------
        >>> np.set_printoptions(precision=4)  # for doctesting
        >>> t1 = ts.TimeSeries(data = np.arange(0,1024,1).reshape(2,512),
        ... sampling_rate=np.pi)
        >>> s1 = SpectralAnalyzer(t1)
        >>> s1.method['this_method']
        'welch'
        >>> s1.method['Fs'] # doctest: +ELLIPSIS
        3.1415926535... Hz
        >>> f,s = s1.psd
        >>> f
        array([ 0.    ,  0.0491,  0.0982,  0.1473,  0.1963,  0.2454,  0.2945,
                0.3436,  0.3927,  0.4418,  0.4909,  0.54  ,  0.589 ,  0.6381,
                0.6872,  0.7363,  0.7854,  0.8345,  0.8836,  0.9327,  0.9817,
                1.0308,  1.0799,  1.129 ,  1.1781,  1.2272,  1.2763,  1.3254,
                1.3744,  1.4235,  1.4726,  1.5217,  1.5708])
        >>> s[0,0]   # doctest: +ELLIPSIS
        1128276.92538360...
        """
        BaseAnalyzer.__init__(self, input)

        self.method = method

        if self.method is None:
            self.method = {'this_method': 'welch',
                           'Fs': self.input.sampling_rate}

        self.BW = BW
        self.adaptive = adaptive
        self.low_bias = low_bias

    @desc.setattr_on_read
    def psd(self):
        """
        The standard output for this analyzer is a tuple f,s, where: f is the
        frequency bands associated with the discrete spectral components
        and s is the PSD calculated using :func:`mlab.psd`.

        """
        NFFT = self.method.get('NFFT', 64)
        Fs = self.input.sampling_rate
        detrend = self.method.get('detrend', tsa.mlab.detrend_none)
        window = self.method.get('window', tsa.mlab.window_hanning)
        n_overlap = self.method.get('n_overlap', int(np.ceil(NFFT / 2.0)))

        if np.iscomplexobj(self.input.data):
            psd_len = NFFT
            dt = complex
        else:
            psd_len = NFFT / 2.0 + 1
            dt = float
        
        #If multi-channel data:
        if len(self.input.data.shape) > 1:
            psd_shape = (self.input.shape[:-1] + (psd_len,))
            flat_data = np.reshape(self.input.data, (-1,
                                                     self.input.data.shape[-1]))
            flat_psd = np.empty((flat_data.shape[0], psd_len), dtype=dt)
            for i in range(flat_data.shape[0]):
                #'f' are the center frequencies of the frequency bands
                #represented in the psd. These are identical in each iteration
                #of the loop, so they get reassigned into the same variable in
                #each iteration:
                temp, f = tsa.mlab.psd(flat_data[i],
                            NFFT=NFFT,
                            Fs=Fs,
                            detrend=detrend,
                            window=window,
                            noverlap=n_overlap)
                flat_psd[i] = temp.squeeze()
            psd = np.reshape(flat_psd, psd_shape).squeeze()

        else:
            psd, f = tsa.mlab.psd(self.input.data,
                            NFFT=NFFT,
                            Fs=Fs,
                            detrend=detrend,
                            window=window,
                            noverlap=n_overlap)

        return f, psd

    @desc.setattr_on_read
    def cpsd(self):
        """
        This outputs both the PSD and the CSD calculated using
        :func:`algorithms.get_spectra`.

        Returns
        -------

        (f,s): tuple
           f: Frequency bands over which the psd/csd are calculated and
           s: the n by n by len(f) matrix of PSD (on the main diagonal) and CSD
           (off diagonal)
        """
        self.welch_method = self.method
        self.welch_method['this_method'] = 'welch'
        self.welch_method['Fs'] = self.input.sampling_rate
        f, spectrum_welch = tsa.get_spectra(self.input.data,
                                           method=self.welch_method)

        return f, spectrum_welch

    @desc.setattr_on_read
    def periodogram(self):
        """

        This is the spectrum estimated as the FFT of the time-series

        Returns
        -------
        (f,spectrum): f is an array with the frequencies and spectrum is the
        complex-valued FFT.
        """
        return tsa.periodogram(self.input.data,
                               Fs=self.input.sampling_rate)

    @desc.setattr_on_read
    def spectrum_fourier(self):
        """

        This is the spectrum estimated as the FFT of the time-series

        Returns
        -------
        (f,spectrum): f is an array with the frequencies and spectrum is the
        complex-valued FFT.

        """

        data = self.input.data
        sampling_rate = self.input.sampling_rate

        fft = fftpack.fft
        if np.any(np.iscomplex(data)):
            # Get negative frequencies, as well as positive:
            f = np.linspace(-sampling_rate/2., sampling_rate/2., data.shape[-1])
            spectrum_fourier = np.fft.fftshift(fft(data))
        else:
            f = tsu.get_freqs(sampling_rate, data.shape[-1])
            spectrum_fourier = fft(data)[..., :f.shape[0]]
            
        return f, spectrum_fourier

    @desc.setattr_on_read
    def spectrum_multi_taper(self):
        """

        The spectrum and cross-spectra, computed using
        :func:`multi_taper_csd'

        """
        if np.iscomplexobj(self.input.data):
            psd_len = self.input.shape[-1] 
            dt = complex
        else:
            psd_len = self.input.shape[-1] / 2 + 1
            dt = float

        #Initialize the output
        spectrum_multi_taper = np.empty((self.input.shape[:-1] + (psd_len,)),
                                        dtype=dt)

        #If multi-channel data:
        if len(self.input.data.shape) > 1:
            for i in range(self.input.data.shape[0]):
                # 'f' are the center frequencies of the frequency bands
                # represented in the MT psd. These are identical in each
                # iteration of the loop, so they get reassigned into the same
                # variable in each iteration:
                f, spectrum_multi_taper[i], _ = tsa.multi_taper_psd(
                    self.input.data[i],
                    Fs=self.input.sampling_rate,
                    BW=self.BW,
                    adaptive=self.adaptive,
                    low_bias=self.low_bias)
        else:
            f, spectrum_multi_taper, _ = tsa.multi_taper_psd(self.input.data,
                                                  Fs=self.input.sampling_rate,
                                                  BW=self.BW,
                                                  adaptive=self.adaptive,
                                                  low_bias=self.low_bias)

        return f, spectrum_multi_taper


class FilterAnalyzer(desc.ResetMixin):
    """ A class for performing filtering operations on time-series and
    producing the filtered versions of the time-series

    Parameters
    ----------

    time_series: A nitime TimeSeries object.

    lb,ub: float (optional)
       Lower and upper band of a pass-band into which the data will be
       filtered. Default: 0, Nyquist

    boxcar_iterations: int (optional)
       For box-car filtering, how many times to iterate over the data while
       convolving with a box-car function. Default: 2

    gpass: float (optional)
       For iir filtering, the pass-band maximal ripple loss (default: 1)

    gstop: float (optional)
       For iir filtering, the stop-band minimal attenuation (default: 60).

    filt_order: int (optional)
        For iir/fir filtering, the order of the filter. Note for fir filtering,
        this needs to be an even number. Default: 64

    iir_ftype: str (optional)
        The type of filter to be used in iir filtering (see
        scipy.signal.iirdesign for details). Default 'ellip'

    fir_win: str
        The window to be used in fir filtering (see scipy.signal.firwin for
        details). Default: 'hamming'

    Note
    ----
    All filtering methods used here keep the original DC component of the
    signal.

    """
    def __init__(self, time_series, lb=0, ub=None, boxcar_iterations=2,
                 filt_order=64, gpass=1, gstop=60, iir_ftype='ellip',
                 fir_win='hamming'):

        #Initialize all the local variables you will need for all the different
        #filtering methods:
        self.data = time_series.data
        self.sampling_rate = time_series.sampling_rate
        self.ub = ub
        self.lb = lb
        self.time_unit = time_series.time_unit
        self._boxcar_iterations = boxcar_iterations
        self._gstop = gstop
        self._gpass = gpass
        self._filt_order = filt_order
        self._ftype = iir_ftype
        self._win = fir_win

    def filtfilt(self, b, a, in_ts=None):

        """
        Zero-phase delay filtering (either iir or fir).

        Parameters
        ----------

        a,b: filter coefficients

        in_ts: time-series object.
           This allows to replace the input. Instead of analyzing this
           analyzers input data, analyze some other time-series object

        Note
        ----

        This is a wrapper around scipy.signal.filtfilt

        """
        # Switch in the new in_ts:
        if in_ts is not None:
            data = in_ts.data
            Fs = in_ts.sampling_rate
        else:
            data = self.data
            Fs = self.sampling_rate

        #filtfilt only operates channel-by-channel, so we need to loop over the
        #channels, if the data is multi-channel data:
        if len(data.shape) > 1:
            out_data = np.empty(data.shape, dtype=data.dtype)
            for i in range(data.shape[0]):
                out_data[i] = signal.filtfilt(b, a, data[i])
                #Make sure to preserve the DC:
                dc = np.mean(data[i])
                out_data[i] -= np.mean(out_data[i])
                out_data[i] += dc
        else:
            out_data = signal.filtfilt(b, a, data)
            #Make sure to preserve the DC:
            dc = np.mean(data)
            out_data -= np.mean(out_data)
            out_data += dc

        return ts.TimeSeries(out_data,
                             sampling_rate=Fs,
                             time_unit=self.time_unit)

    @desc.setattr_on_read
    def fir(self):
        """
        Filter the time-series using an FIR digital filter. Filtering is done
        back and forth (using scipy.signal.filtfilt) to achieve zero phase
        delay
        """
        #Passband and stop-band are expressed as fraction of the Nyquist
        #frequency:
        if self.ub is not None:
            ub_frac = self.ub / (self.sampling_rate / 2.)
        else:
            ub_frac = 1.0

        lb_frac = self.lb / (self.sampling_rate / 2.)

        if lb_frac < 0 or ub_frac > 1:
            e_s = "The lower-bound or upper bound used to filter"
            e_s += " are beyond the range 0-Nyquist. You asked for"
            e_s += " a filter between"
            e_s += "%s and %s percent of" % (lb_frac * 100, ub_frac * 100)
            e_s += "the Nyquist frequency"
            raise ValueError(e_s)

        n_taps = self._filt_order + 1

        #This means the filter order you chose was too large (needs to be
        #shorter than a 1/3 of your time-series )
        if n_taps > self.data.shape[-1] * 3:
            e_s = "The filter order chosen is too large for this time-series"
            raise ValueError(e_s)

        # a is always 1:
        a = [1]

        sig = ts.TimeSeries(data=self.data, sampling_rate=self.sampling_rate)

        #Lowpass:
        if ub_frac < 1:
            b = signal.firwin(n_taps, ub_frac, window=self._win)
            sig = self.filtfilt(b, a, sig)

        #High-pass
        if lb_frac > 0:
            #Includes a spectral inversion:
            b = -1 * signal.firwin(n_taps, lb_frac, window=self._win)
            b[n_taps / 2] = b[n_taps / 2] + 1
            sig = self.filtfilt(b, a, sig)

        return sig

    @desc.setattr_on_read
    def iir(self):
        """
        Filter the time-series using an IIR filter. Filtering is done back and
        forth (using scipy.signal.filtfilt) to achieve zero phase delay

        """

        #Passband and stop-band are expressed as fraction of the Nyquist
        #frequency:
        if self.ub is not None:
            ub_frac = self.ub / (self.sampling_rate / 2.)
        else:
            ub_frac = 1.0

        lb_frac = self.lb / (self.sampling_rate / 2.)

        # For the band-pass:
        if lb_frac > 0 and ub_frac < 1:

            wp = [lb_frac, ub_frac]

            ws = [np.max([lb_frac - 0.1, 0]),
                  np.min([ub_frac + 0.1, 1.0])]

        # For the lowpass:
        elif lb_frac == 0:
            wp = ub_frac
            ws = np.min([ub_frac + 0.1, 0.9])

        # For the highpass:
        elif ub_frac == 1:
            wp = lb_frac
            ws = np.max([lb_frac - 0.1, 0.1])

        b, a = signal.iirdesign(wp, ws, self._gpass, self._gstop,
                                ftype=self._ftype)

        return self.filtfilt(b, a)

    @desc.setattr_on_read
    def filtered_fourier(self):
        """

        Filter the time-series by passing it to the Fourier domain and null
        out the frequency bands outside of the range [lb,ub]

        """

        freqs = tsu.get_freqs(self.sampling_rate, self.data.shape[-1])

        if self.ub is None:
            self.ub = freqs[-1]

        power = fftpack.fft(self.data)
        idx_0 = np.hstack([np.where(freqs < self.lb)[0],
                           np.where(freqs > self.ub)[0]])

        #Make sure that you keep the DC component:
        keep_dc = np.copy(power[..., 0])
        power[..., idx_0] = 0
        power[..., -1 * idx_0] = 0  # Take care of the negative frequencies
        power[..., 0] = keep_dc  # And put the DC back in when you're done:

        data_out = fftpack.ifft(power)

        data_out = np.real(data_out)  # In order to make sure that you are not
                                      # left with float-precision residual
                                      # complex parts

        return ts.TimeSeries(data=data_out,
                             sampling_rate=self.sampling_rate,
                             time_unit=self.time_unit)

    @desc.setattr_on_read
    def filtered_boxcar(self):
        """
        Filter the time-series by a boxcar filter.

        The low pass filter is implemented by convolving with a boxcar function
        of the right length and amplitude and the high-pass filter is
        implemented by subtracting a low-pass version (as above) from the
        signal
        """

        if self.ub is not None:
            ub = self.ub / self.sampling_rate
        else:
            ub = 1.0

        lb = self.lb / self.sampling_rate

        data_out = tsa.boxcar_filter(np.copy(self.data),
                                     lb=lb, ub=ub,
                                     n_iterations=self._boxcar_iterations)

        return ts.TimeSeries(data=data_out,
                                 sampling_rate=self.sampling_rate,
                                 time_unit=self.time_unit)


class HilbertAnalyzer(BaseAnalyzer):

    """Analyzer class for extracting the Hilbert transform """

    def __init__(self, input=None):
        """Constructor function for the Hilbert analyzer class.

        Parameters
        ----------

        input: TimeSeries

        """
        BaseAnalyzer.__init__(self, input)

    @desc.setattr_on_read
    def analytic(self):
        """The natural output for this analyzer is the analytic signal """
        data = self.input.data
        sampling_rate = self.input.sampling_rate
        #If you have scipy with the fixed scipy.signal.hilbert (r6205 and
        #later)
        if scipy.__version__ >= '0.9':
            hilbert = signal.hilbert
        else:
            hilbert = tsu.hilbert_from_new_scipy

        return ts.TimeSeries(data=hilbert(data),
                             sampling_rate=sampling_rate)

    @desc.setattr_on_read
    def amplitude(self):
        return ts.TimeSeries(data=np.abs(self.analytic.data),
                             sampling_rate=self.analytic.sampling_rate)

    @desc.setattr_on_read
    def phase(self):
        return ts.TimeSeries(data=np.angle(self.analytic.data),
                             sampling_rate=self.analytic.sampling_rate)

    @desc.setattr_on_read
    def real(self):
        return ts.TimeSeries(data=self.analytic.data.real,
                             sampling_rate=self.analytic.sampling_rate)

    @desc.setattr_on_read
    def imag(self):
        return ts.TimeSeries(data=self.analytic.data.imag,
                             sampling_rate=self.analytic.sampling_rate)


class MorletWaveletAnalyzer(BaseAnalyzer):

    """Analyzer class for extracting the (complex) Morlet wavelet transform """

    def __init__(self, input=None, freqs=None, sd_rel=.2, sd=None, f_min=None,
                 f_max=None, nfreqs=None, log_spacing=False, log_morlet=False):
        """Constructor function for the Wavelet analyzer class.

        Parameters
        ----------

        freqs: list or float
          List of center frequencies for the wavelet transform, or a scalar
          for a single band-passed signal.

        sd: list or float
          List of filter bandwidths, given as standard-deviation of center
          frequencies. Alternatively sd_rel can be specified.

        sd_rel: float
          Filter bandwidth, given as a fraction of the center frequencies.

        f_min: float
          Minimal frequency.

        f_max: float
          Maximal frequency.

        nfreqs: int
          Number of frequencies.

        log_spacing: bool
          If true, frequencies will be evenly spaced on a log-scale.
          Default: False

        log_morlet: bool
          If True, a log-Morlet wavelet is used, if False, a regular Morlet
          wavelet is used. Default: False
        """
        BaseAnalyzer.__init__(self, input)
        self.freqs = freqs
        self.sd_rel = sd_rel
        self.sd = sd
        self.f_min = f_min
        self.f_max = f_max
        self.nfreqs = nfreqs
        self.log_spacing = log_spacing
        self.log_morlet = log_morlet

        if log_morlet:
            self.wavelet = tsa.wlogmorlet
        else:
            self.wavelet = tsa.wmorlet

        if freqs is not None:
            self.freqs = np.array(freqs)
        elif f_min is not None and f_max is not None and nfreqs is not None:
            if log_spacing:
                self.freqs = np.logspace(np.log10(f_min), np.log10(f_max),
                                         num=nfreqs, endpoint=True)
            else:
                self.freqs = np.linspace(f_min, f_max, num=nfreqs,
                                         endpoint=True)
        else:
            raise NotImplementedError

        if sd is None:
            self.sd = self.freqs * self.sd_rel

    @desc.setattr_on_read
    def analytic(self):
        """The natural output for this analyzer is the analytic signal"""
        data = self.input.data
        sampling_rate = self.input.sampling_rate

        a_signal =\
    ts.TimeSeries(data=np.zeros(self.freqs.shape + data.shape,
                                dtype='D'), sampling_rate=sampling_rate)
        if self.freqs.ndim == 0:
            w = self.wavelet(self.freqs, self.sd,
                             sampling_rate=sampling_rate, ns=5,
                             normed='area')

            # nd = (w.shape[0] - 1) / 2
            a_signal.data[...] = (np.convolve(data, np.real(w), mode='same') +
                            1j * np.convolve(data, np.imag(w), mode='same'))
        else:
            for i, (f, sd) in enumerate(zip(self.freqs, self.sd)):
                w = self.wavelet(f, sd, sampling_rate=sampling_rate,
                                 ns=5, normed='area')

                # nd = (w.shape[0] - 1) / 2
                a_signal.data[i, ...] = (
                    np.convolve(data, np.real(w), mode='same') +
                    1j * np.convolve(data, np.imag(w), mode='same'))

        return a_signal

    @desc.setattr_on_read
    def amplitude(self):
        return ts.TimeSeries(data=np.abs(self.analytic.data),
                             sampling_rate=self.analytic.sampling_rate)

    @desc.setattr_on_read
    def phase(self):
        return ts.TimeSeries(data=np.angle(self.analytic.data),
                             sampling_rate=self.analytic.sampling_rate)

    @desc.setattr_on_read
    def real(self):
        return ts.TimeSeries(data=self.analytic.data.real,
                             sampling_rate=self.analytic.sampling_rate)

    @desc.setattr_on_read
    def imag(self):
        return ts.TimeSeries(data=self.analytic.data.imag,
                             sampling_rate=self.analytic.sampling_rate)