def getFreqBandOrValue(data, new_data, freq_value, global_max):
    # delete first row
    data = np.delete(data, 0, 0)

    # add new_data as a row at the end of data. columns=electrodes rows=timestep
    data = np.vstack([data, new_data])
    # transpose the data numpy array
    data = np.transpose(data)

    # compute power spectrum of data
    f, ps = sps.welch(data, fs=26)
    # print the power spectrum
    print("ps", ps)
    # print the frequency
    print("f", f)

    extract_amplitude = []
    # delta freq band
    if freq_value == -1:
        extract_amplitude = getAmplitudesByFrequencyBand(ps, 0)
    # theta freq band
    elif freq_value == -2:
        extract_amplitude = getAmplitudesByFrequencyBand(ps, 1)
    # alpha freq band
    elif freq_value == -3:
        extract_amplitude = getAmplitudesByFrequencyBand(ps, 2)
    # specific freq value wanted
    else:
        interval = [freq_value - 0.5, freq_value + 0.5]
        start_index = -1
        end_index = -1
        for i in range(len(f)):
            if interval[0] <= f[i] <= interval[1]:
                if start_index == -1:
                    start_index = i
                else:
                    end_index = i

        print("start ", start_index, f[start_index], "end ", end_index,
              f[end_index])
        extract_amplitude = ps[:, start_index:end_index]
    # create a numpy array called temp
    temp = np.asarray(extract_amplitude)

    # temp holds mean of each row in extractAmplitude
    temp = np.mean(temp, axis=1)
    # calculate the maximum of the two values - called local_max
    local_max = max(np.amax(temp), global_max)

    # traverse through elements in the temp nuumpy array
    for i in range(len(temp)):
        # normalize all amplitudes by the global max
        temp[i] = temp[i] / local_max
    # return the temp, local_max, and data numpy arrays
    return [temp, local_max, data]
def getCmapByFreqVal(data, newdata, freqValue, globalMax):
    # delete first row
    data = np.delete(data, 0, 0)

    # add newdata as a row at the end of data. columns=electrodes rows=timestep
    data = np.vstack([data, newdata])
    data = np.transpose(data)

    # compute power spectrum of data
    f, ps = sps.welch(data, fs=26)
    #print("ps", ps)
    #print("f", f)

    extractAmplitude = []
    # delta freq band
    if (freqValue == -1):
        extractAmplitude = getAmplitudesByFrequencyBand(ps, 0)
    # theta freq band
    elif freqValue == -2:
        extractAmplitude = getAmplitudesByFrequencyBand(ps, 1)
    # alpha freq band
    elif freqValue == -3:
        extractAmplitude = getAmplitudesByFrequencyBand(ps, 2)
    # specific freq value wanted
    else:
        interval = [freqValue - 0.5, freqValue + 0.5]
        startIndex = -1
        endIndex = -1
        for i in range(len(f)):
            if interval[0] <= f[i] <= interval[1]:
                if startIndex == -1:
                    startIndex = i
                else:
                    endIndex = i

        #print("start ", startIndex, f[startIndex],
        #     "end ", endIndex, f[endIndex])
        extractAmplitude = ps[:, startIndex:endIndex]

    temp = np.asarray(extractAmplitude)

    # temp holds mean of each row in extractAmplitude
    temp = np.mean(temp, axis=1)
    localMax = max(np.amax(temp), globalMax)

    for i in range(len(temp)):
        # normalize all amplitudes by the global max
        temp[i] = temp[i] / localMax
    return [temp, localMax, data]
def getCmapForZscores(data, newdata, freqValue):
    # delete first row of data
    data = np.delete(data, 0, 0)

    # add newdata as a row at the end of data. columns=electrodes rows=timestep
    data = np.vstack([data, newdata])
    data = np.transpose(data)

    # compute power spectrum of data
    f, ps = sps.welch(data, fs=26)
    #print("ps", ps)

    extractAmplitude = []
    # delta freq band
    if (freqValue == -1):
        extractAmplitude = getAmplitudesByFrequencyBand(ps, 0)
    # theta freq band
    elif freqValue == -2:
        extractAmplitude = getAmplitudesByFrequencyBand(ps, 1)
    # alpha freq band
    elif freqValue == -3:
        extractAmplitude = getAmplitudesByFrequencyBand(ps, 2)
    # specific freq value wanted
    else:
        startIndex, endIndex = getInterval(f, freqValue)
        extractAmplitude = ps[:, startIndex:endIndex]

    temp = np.asarray(extractAmplitude)
    # temp holds mean of each row in extractAmplitude
    temp = np.mean(temp, axis=1)
    #print("temp", temp)

    # square all values to make them 0 <= x <= 1
    temp = np.square(temp)
    # calculate zscores for the array
    zscoreArray = stats.zscore(temp)

    # next line creates positive and negative zscores, so if the value was between 0 to 0.5, it is
    # scaled to between -1 and 0, and if the value was between 0.5 and 1, it is scaled to between
    # 0 and 1

    zscoreArray = np.power(
        (zscoreArray / np.amax(np.absolute(zscoreArray))), 3) / 2 + 0.5
    return [zscoreArray, data]
예제 #4
0
def plotNodes(i):
    global data

    start_time = time.time()
    inlet = StreamInlet(streams[0])

    # get a new sample
    sample = inlet.pull_sample()
    newdata = np.asarray(sample[0][:n])
    # print(newdata)

    # delete first row of data
    data = np.delete(data, 0, 0)

    # add newdata as a row at the end of data. columns=electrodes rows=timestep
    data = np.vstack([data, newdata])
    data = np.transpose(data)

    # compute power spectrum of data
    f, ps = sps.welch(data, fs=26)


# get the amplitudes associated with the various bands of frequencies
    extractAmplitudeDelta = getAmplitudesByFrequencyBand(ps, 0)
    extractAmplitudeTheta = getAmplitudesByFrequencyBand(ps, 1)
    extractAmplitudeAlpha = getAmplitudesByFrequencyBand(ps, 2)
    tempDelta = np.asarray(extractAmplitudeDelta)
    tempTheta = np.asarray(extractAmplitudeTheta)
    tempAlpha = np.asarray(extractAmplitudeAlpha)

    # temp holds mean of each row in extractAmplitude
    tempDelta = np.mean(tempDelta, axis=1)
    tempTheta = np.mean(tempTheta, axis=1)
    tempAlpha = np.mean(tempAlpha, axis=1)

    # square all values to make them 0 <= x <= 1
    tempDelta = np.square(tempDelta)
    tempTheta = np.square(tempTheta)
    tempAlpha = np.square(tempAlpha)

    # calculate zscores for the array
    zscoreArrayDelta = stats.zscore(tempDelta)
    zscoreArrayTheta = stats.zscore(tempTheta)
    zscoreArrayAlpha = stats.zscore(tempAlpha)

    sumOfZscores = zscoreArrayDelta + zscoreArrayTheta + zscoreArrayAlpha

    zscoreArrayDelta = np.divide(zscoreArrayDelta, sumOfZscores)
    zscoreArrayTheta = np.divide(zscoreArrayTheta, sumOfZscores)
    zscoreArrayAlpha = np.divide(zscoreArrayAlpha, sumOfZscores)

    # next line creates positive and negative zscores, so if the value was between 0 to 0.5, it is
    # scaled to between -1 and 0, and if the value was between 0.5 and 1, it is scaled to between
    # 0 and 1
    zscoreArrayDelta = (
        (zscoreArrayDelta / np.amax(zscoreArrayDelta)) / 2) + 0.5
    zscoreArrayTheta = (
        (zscoreArrayTheta / np.amax(zscoreArrayTheta)) / 2) + 0.5
    zscoreArrayAlpha = (
        (zscoreArrayAlpha / np.amax(zscoreArrayAlpha)) / 2) + 0.5

    print(zscoreArrayAlpha)
    # define vectors for plot colors and opacity
    # altColors = freqs / 33
    colorsDelta = cmap(zscoreArrayDelta)
    colorsTheta = cmap(zscoreArrayTheta)
    colorsAlpha = cmap(zscoreArrayAlpha)

    # colors.astype(float)
    # colors[:, -1] = maxes / maxes.max()
    # print(altColors)
    # print(colors)

    ax1.set_xlim(-6, 6)
    ax1.set_ylim(-6, 6)
    ax2.set_xlim(-6, 6)
    ax2.set_ylim(-6, 6)
    ax3.set_xlim(-6, 6)
    ax3.set_ylim(-6, 6)
    # ax1.scatter(x, y, s = 100, c = altColors, cmap = plt.cm.jet_r)
    ax1.scatter(x, y, s=100, c=colorsDelta)
    ax2.scatter(x, y, s=100, c=colorsTheta)
    ax3.scatter(x, y, s=100, c=colorsAlpha)

    elapsed_time = time.time() - start_time