Пример #1
0
def prepostfilter(reader):

    scaled_csi = reader.csi_trace

    no_frames = len(scaled_csi)
    no_subcarriers = scaled_csi[0]["csi"].shape[0]
    finalEntry = getCSI(scaled_csi)[15]

    stdev = running_stdev(finalEntry, 2)
    hampelData = hampel(finalEntry, 15)
    smoothedData = running_mean(hampelData.copy(), 15)

    y = finalEntry
    y2 = hampelData
    y3 = smoothedData

    x = list([x["timestamp"] for x in scaled_csi])

    plt.plot(x, y, label="Raw")
    plt.plot(x, y2, label="Hampel")
    # plt.plot(x, stdev, label="Standard Deviation")
    plt.plot(x, y3, "r", label="Hampel + Running Mean")

    plt.xlabel("Time (s)")
    plt.ylabel("Amplitude (dBm)")
    plt.legend(loc="upper right")

    plt.show()
Пример #2
0
def heatmap(scaled_csi):

    xax = getTimestamps(scaled_csi)
    csi, no_frames, no_subcarriers = getCSI(scaled_csi)

    Fs = 1 / np.mean(np.diff(xax))
    print("Sampling Rate: " + str(Fs))

    limits = [0, xax[-1], 1, no_subcarriers]

    #x = subcarrier index
    #y = time (s)
    #z = amplitude (dBm)

    for x in range(no_subcarriers):
        hampelData = hampel(csi[x].flatten(), 5)
        smoothedData = running_mean(hampelData, 5)

        # b, a = signal.butter(9, [1/int(Fs/2), 2/int(Fs/2)], "bandpass")
        # csi[x] = signal.lfilter(b, a, csi[x].flatten())
        csi[x] = smoothedData

    fig, ax = plt.subplots()
    im = ax.imshow(csi, cmap="jet", extent=limits, aspect="auto")

    cbar = ax.figure.colorbar(im, ax=ax)
    cbar.ax.set_ylabel("Amplitude (dBm)")

    plt.xlabel("Time (s)")
    plt.ylabel("Subcarrier Index")

    plt.show()
Пример #3
0
def beatsfilter(scaled_csi):
    xax = getTimestamps(scaled_csi)
    csi, no_frames, no_subcarriers = getCSI(scaled_csi)

    Fs = 1 / np.mean(np.diff(xax))

    stabilities = []

    for x in range(no_subcarriers):
        finalEntry = csi[x].flatten()
        variation = stats.variation(finalEntry)

        if not np.isnan(variation) and variation < 0.5:
            hampelData = hampel(finalEntry, 5)
            smoothedData = running_mean(hampelData, 5)
            filtData = bandpass(5, 1, 1.3, Fs, smoothedData)

            wLength = 1 * Fs
            N = np.mod(len(filtData), wLength)
            windows = np.array_split(filtData, N)

            psds = []
            for window in windows:
                f, Pxx_den = signal.welch(window, Fs)
                fMax = f[np.argmax(Pxx_den)]
                psds.append(fMax)

            specStab = 1 / np.var(psds)
            print("Sub: {} has spectral stability: {}".format(x, specStab))

            # plt.plot(xax, filtData, alpha=0.5)
            stabilities.append({
                "sub": x,
                "stability": specStab,
                "data": filtData
            })

    stabilities.sort(key=lambda x: x["stability"], reverse=True)

    bestStab = stabilities[0]["stability"]
    news = []
    for stab in stabilities:
        if stab["stability"] >= (bestStab / 100) * 20:
            news.append(stab)

    print("Using {} subcarriers.".format(len(news)))
    pxxs = []
    for sub in news:
        filtData = sub["data"]
        f, Pxx_den = signal.welch(filtData, Fs)
        pxxs.append(Pxx_den)

    meanPsd = np.mean(pxxs, axis=0)

    plt.plot(f * 60, meanPsd, alpha=0.5)

    plt.xlabel("Estimated Heart Rate [bpm]")
    plt.ylabel("PSD [V**2/Hz]")
    plt.legend(loc="upper right")
    plt.show()
Пример #4
0
def heatmap(scaled_csi):

    timestamps = getTimestamps(scaled_csi)
    csi, no_frames, no_subcarriers = getCSI(scaled_csi)

    Fs = 1 / np.mean(np.diff(timestamps))
    print("Sampling Rate: " + str(Fs))

    limits = [0, timestamps[-1], 1, no_subcarriers]

    for x in range(no_subcarriers):
        hampelData = hampel(csi[x].flatten(), 5)
        smoothedData = running_mean(hampelData, 5)
        butteredData = bandpass(7, 0.2, 0.3, Fs, smoothedData)
        csi[x] = butteredData

    fig, ax = plt.subplots()
    im = ax.imshow(csi, cmap="jet", extent=limits, aspect="auto")

    cbar = ax.figure.colorbar(im, ax=ax)
    cbar.ax.set_ylabel("Amplitude (dBm)")

    plt.xlabel("Time (s)")
    plt.ylabel("Subcarrier Index")

    plt.show()
Пример #5
0
def fft(reader):

    scaled_csi = reader.csi_trace

    no_frames = len(scaled_csi)
    no_subcarriers = scaled_csi[0]["csi"].shape[0]

    finalEntry = getCSI(scaled_csi)[15].flatten()

    hampelData = hampel(finalEntry, 20)
    smoothedData = running_mean(hampelData, 30)
    y = smoothedData.flatten()
    y -= np.mean(y)

    b, a = signal.butter(5, [1 / 10, 2 / 10], "bandpass")
    y = signal.lfilter(b, a, y)

    x = [i / 20 for i in range(no_frames)]
    tdelta = (x[-1] - x[0]) / len(x)

    Fs = 1 / tdelta
    n = no_frames

    #rfft is a real Fast Fourier Transform, as we aren't interested in the imaginary parts.
    #as half of the points lie in the imaginary/negative domain, we get an n/2 point FFT.

    #rfftfreq produces a list of frequency bins, which requires calculation to produce
    #interpretable frequencies for BPM tracking. by plotting calculated bpm values,
    #the graph can be more easily read.

    #despite rfft producing an n/2 point fft, the calculation still uses n,
    #because the point spacing is still based on n points.

    # ((binNumber*samplingRate)/n)*60 produces a per minute rate.

    ffty = np.fft.rfft(y, len(y))
    freq = np.fft.rfftfreq(len(y), tdelta)
    freqX = [((i * Fs) / n) * 60 for i in range(len(freq))]

    plt.subplot(211)
    plt.xlabel("Time (s)")
    plt.ylabel("Amplitude (Hz)")

    plt.plot(x, y)

    plt.subplot(212)
    plt.xlabel("Breaths Per Minute (BPM)")
    plt.ylabel("Amplitude (dB/Hz)")

    axes = plt.gca()
    axes.set_xlim([0, 30])

    plt.plot(freqX, np.abs(ffty))

    plt.show()
Пример #6
0
    def updateButterworth(self, data):
        self.all_data.append(data)
        self.updateTimestamps()

        if not self.updateTimestamps():
            self.all_data = self.all_data[:-1]
            return None
        scaled_csi = self.all_data

        no_frames = len(scaled_csi)
        no_subcarriers = scaled_csi[0]["csi"].shape[0]

        if no_frames < 50:
            return None

        #Replace complex CSI with amplitude.
        finalEntry = [
            db(abs(scaled_csi[x]["csi"][15][0][0])) for x in range(no_frames)
        ]

        hampelData = hampel(finalEntry, 10)
        smoothedData = running_mean(hampelData, 30)
        y = smoothedData

        x = list([x["timestamp"] for x in scaled_csi])
        tdelta = (x[-1] - x[0]) / len(x)

        Fs = 1 / tdelta
        n = no_frames

        y = bandpass(5, 1.0, 1.3, Fs, y)

        ffty = np.fft.rfft(y, len(y))
        freq = np.fft.rfftfreq(len(y), tdelta)
        freqX = [((i * Fs) / n) * 60 for i in range(len(freq))]

        self.plotButt.set_xdata(freqX)
        self.plotButt.set_ydata(np.abs(ffty))

        self.ax.relim()
        self.ax.autoscale_view()
        self.fig.canvas.draw()
        self.fig.canvas.flush_events()
Пример #7
0
def varianceGraph(reader):

    scaled_csi = reader.csi_trace

    no_frames = len(scaled_csi)
    no_subcarriers = scaled_csi[1]["csi"].shape[0]

    y = []

    finalEntry = getCSI(scaled_csi)

    for x in range(no_subcarriers):
        hampelData = hampel(finalEntry[x], 10)
        smoothedData = running_mean(hampelData, 25)
        y.append(variance(smoothedData))

    plt.xlabel("Subcarrier Index")
    plt.ylabel("Variance")

    plt.plot(y)
    plt.show()
Пример #8
0
def breathingfilter(scaled_csi):
    xax = getTimestamps(scaled_csi)
    csi, no_frames, no_subcarriers = getCSI(scaled_csi)

    # Zeroing out invalid subcarriers.
    # for i in []:
    #     for x in range(no_frames):
    #         csi[i][x] = 0

    # for x in range(no_subcarriers):
    for x in range(80, 100):
        finalEntry = csi[x].flatten()

        hampelData = hampel(finalEntry, 10)
        smoothedData = running_mean(hampelData, 10)
        # csi[x] = hampelData
        csi[x] = smoothedData
        plt.plot(xax, csi[x], alpha=0.5, label=x)

    plt.xlabel("Time (s)")
    plt.ylabel("Amplitude")
    plt.legend(loc="upper right")
    plt.show()
Пример #9
0
def shorttime(reader, subcarrier):

    scaled_csi = reader.csi_trace

    no_frames = len(scaled_csi)
    no_subcarriers = scaled_csi[0]["csi"].shape[0]

    finalEntry = np.zeros((no_frames, 1))
    hampelData = np.zeros((no_frames, 1))
    smoothedData = np.zeros((no_frames, 1))

    #Replace complex CSI with amplitude.

    finalEntry = [
        db(
            abs(scaled_csi[x]["csi"][subcarrier][reader.x_antenna][
                reader.y_antenna])) for x in range(no_frames)
    ]

    hampelData = hampel(finalEntry, 30)
    smoothedData = running_mean(hampelData, 20)
    y = smoothedData

    tdelta = 0.01
    x = list([x * tdelta for x in range(0, no_frames)])

    Fs = 1 / tdelta
    n = no_frames

    fmin = 0.7
    fmax = 2.3

    b, a = signal.butter(3, [fmin / (Fs / 2), fmax / (Fs / 2)], "bandpass")
    y = signal.lfilter(b, a, y)

    f, t, Zxx = signal.stft(y, Fs, nperseg=2000, noverlap=1750)

    #bin indices for the observed amplitude peak in each segment.
    Zxx = np.abs(Zxx)
    maxAx = np.argmax(Zxx, axis=0)

    freqs = []
    for i in maxAx:
        arc = f[i]
        if arc >= fmin and arc <= fmax:
            arc *= 60
            freqs.append(arc)
        else:
            freqs.append(None)

    bpms = []
    for freq in freqs:
        if freq != None:
            bpms.append(freq)

    print(bpms)
    print("Average HR: " + str(np.average(bpms)))

    #We're only interested in frequencies between 0.5 and 2.
    freq_slice = np.where((f >= fmin) & (f <= fmax))

    f = f[freq_slice]
    Zxx = Zxx[freq_slice, :][0]

    plt.pcolormesh(t, f * 60, np.abs(Zxx))
    plt.plot(t, freqs, color="red", marker="x")

    plt.title('STFT Magnitude')
    # plt.ylabel('Frequency [Hz]')
    plt.ylabel('Heart Rate [BPM]')
    plt.xlabel('Time [sec]')
    plt.show()