Beispiel #1
0
def create_all_psd():

    f = pylab.linspace(0, 1, 4096)

    pylab.figure(figsize=(12,8))

    # MA model
    p = spectrum.pma(xx, 64,128); p(); p.plot()
    """
    #ARMA 15 order
    a, b, rho = spectrum.arma_estimate(data, 15,15, 30)
    psd = spectrum.arma2psd(A=a,B=b, rho=rho)
    newpsd = tools.cshift(psd, len(psd)//2) # switch positive and negative freq
    pylab.plot(f, 10 * pylab.log10(newpsd/max(newpsd)), label='ARMA 15,15')
    """
    # YULE WALKER
    p = spectrum.pyule(xx, 7 , NFFT=4096, scale_by_freq=False); p.plot()
    # equivalent to
    # plot([x for x in p.frequencies()] , 10*log10(p.psd)); grid(True)

    #burg method
    p = spectrum.pburg(xx, 7, scale_by_freq=False);  p.plot()

    #pcovar
    p = spectrum.pcovar(xx, 7, scale_by_freq=False);  p.plot()

    #pmodcovar
    p = spectrum.pmodcovar(xx, 7, scale_by_freq=False); p.plot()

    # correlogram
    p = spectrum.pcorrelogram(xx, lag=60, NFFT=512, scale_by_freq=False); p.plot()

    # minvar
    p = spectrum.pminvar(xx, 7, NFFT=256, scale_by_freq=False); p.plot()

    # pmusic
    p = spectrum.pmusic(xx, 10,4, scale_by_freq=False); p.plot()

    # pmusic
    p = spectrum.pev(xx, 10, 4, scale_by_freq=False); p.plot()

    # periodogram
    p = spectrum.Periodogram(xx, scale_by_freq=False); p.plot()

    #
    legend( ["MA 32", "pyule 7", "pburg 7", "pcovar", "pmodcovar", "correlogram",
                "minvar", "pmusic", "pev", "periodgram"])


    pylab.ylim([-80,80])
Beispiel #2
0
def create_all_psd():

    f = pylab.linspace(0, 1, 4096)

    pylab.figure(figsize=(12, 8))

    # MA model
    p = spectrum.pma(xx, 64, 128)
    p()
    p.plot()
    """
    #ARMA 15 order
    a, b, rho = spectrum.arma_estimate(data, 15,15, 30)
    psd = spectrum.arma2psd(A=a,B=b, rho=rho)
    newpsd = tools.cshift(psd, len(psd)//2) # switch positive and negative freq
    pylab.plot(f, 10 * pylab.log10(newpsd/max(newpsd)), label='ARMA 15,15')
    """
    # YULE WALKER
    p = spectrum.pyule(xx, 7, NFFT=4096, scale_by_freq=False)
    p.plot()
    # equivalent to
    # plot([x for x in p.frequencies()] , 10*log10(p.psd)); grid(True)

    #burg method
    p = spectrum.pburg(xx, 7, scale_by_freq=False)
    p.plot()

    #pcovar
    p = spectrum.pcovar(xx, 7, scale_by_freq=False)
    p.plot()

    #pmodcovar
    p = spectrum.pmodcovar(xx, 7, scale_by_freq=False)
    p.plot()

    # correlogram
    p = spectrum.pcorrelogram(xx, lag=60, NFFT=512, scale_by_freq=False)
    p.plot()

    # minvar
    p = spectrum.pminvar(xx, 7, NFFT=256, scale_by_freq=False)
    p.plot()

    # pmusic
    p = spectrum.pmusic(xx, 10, 4, scale_by_freq=False)
    p.plot()

    # pmusic
    p = spectrum.pev(xx, 10, 4, scale_by_freq=False)
    p.plot()

    # periodogram
    p = spectrum.Periodogram(xx, scale_by_freq=False)
    p.plot()

    #
    legend([
        "MA 32", "pyule 7", "pburg 7", "pcovar", "pmodcovar", "correlogram",
        "minvar", "pmusic", "pev", "periodgram"
    ])

    pylab.ylim([-80, 80])
Beispiel #3
0
from scipy.io import loadmat
import spectrum

from matplotlib import pylab as pl
import numpy as np

import sys

if __name__ == "__main__":
    d = loadmat(sys.argv[1])
    o1 = signal.detrend(d['raw'][0])
    o2 = signal.detrend(d['raw'][1])

    diff = o1 - o2
    avg = (o1 + o2) / 2.0

    data_labels = ["O1", "O2", "O1-O2", "(O1+O2)/2"]
    datas = [o1, o2, diff, avg]

    fig, axs = pl.subplots(nrows=1, ncols=4, sharey=True)
    time = d['data']['time'][0][0][0][0]

    for i in range(4):
        psd = spectrum.pburg(datas[i], order=8, NFFT=256, sampling=128.0)
        psd.run()
        p = 10 * np.log10(psd.get_converted_psd('onesided'))
        axs[i].set_title(data_labels[i])
        axs[i].plot(psd.frequencies(), p)

    pl.show()
Beispiel #4
0
def _calc_pburg_psd(rri, fs, order=16, nfft=None):
    burg = pburg(data=rri, order=order, NFFT=nfft, sampling=fs)
    burg.scale_by_freq = False
    burg()
    return np.array(burg.frequencies()), burg.psd
Beispiel #5
0
def feature_band_selection(data,
                           labels,
                           sfreq,
                           step=1,
                           band_range=(0, 0),
                           band_size=(0, 0, 0),
                           channel=('EEG:C3', 'EEG:Cz', 'EEG:C4'),
                           features_type=0):
    # AR model parameters
    ar_order = 12
    nfft = 1000

    subject_optimal_frequency_bands = dict()

    # mu band selection
    for subject in data:
        if features_type == 1:  # compute AR Model PSD based on burg algorithm
            ar_psd = dict()
            freq_flag = True
            for channel_name in channel:
                ar_psd[channel_name] = list()
            for idx in range(len(labels[subject])):
                for channel_name in channel:
                    x = data[subject][channel_name][idx]
                    p = pburg(x,
                              order=ar_order,
                              NFFT=nfft,
                              sampling=sfreq,
                              scale_by_freq=True)
                    if freq_flag:
                        ar_psd['frequency'] = np.array(p.frequencies())
                        freq_flag = False
                    ar_psd[channel_name].append(p.psd)

        f_score = list()
        optimal_band = list()
        for band in band_size:
            for num_windows in range(
                    int((band_range[1] - band_range[0] - band) / step)):
                lowcut = band_range[0] + num_windows * step
                highcut = lowcut + band
                optimal_band.append((lowcut, highcut))
                if features_type == 1:
                    ar_freq = ar_psd['frequency']
                    psd_idx_start = np.where(ar_freq >= lowcut)[0][0]
                    psd_idx_end = np.where(ar_freq >= highcut)[0][0]
                left_features = list()
                right_features = list()
                for idx in range(len(labels[subject])):
                    features = list()
                    for channel_name in channel:
                        # BP features
                        if features_type == 0:  # 5th butterworth filter
                            filtered_data = butter_bandpass_filter(
                                data[subject][channel_name][idx],
                                lowcut,
                                highcut,
                                sfreq,
                                order=5)
                        elif features_type == 1:  # AR model PSD
                            filtered_data = ar_psd[channel_name][idx][
                                psd_idx_start:psd_idx_end]
                        else:
                            raise Exception(
                                "feature type wrong!\n band pass features: features_type=0\n "
                                "AR PSD features: features_type=1")
                        features.append(math.log10(np.var(filtered_data)))
                    if labels[subject][idx] == 1:
                        left_features.append(features)
                    elif labels[subject][idx] == 2:
                        right_features.append(features)
                left_mean_val = np.mean(left_features, axis=0)
                right_mean_val = np.mean(right_features, axis=0)
                left_var = np.var(left_features, axis=0)
                right_var = np.var(right_features, axis=0)
                f_score.append(
                    sum(np.square(left_mean_val - right_mean_val)) /
                    sum(left_var + right_var))
        # get optimal frequency corresponding to max F-score
        subject_optimal_frequency_bands[subject] = optimal_band[f_score.index(
            max(f_score))]
        # pause = input("pause")
    return subject_optimal_frequency_bands