{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-02-14T16:59:52.224327Z", "start_time": "2020-02-14T16:59:51.683530Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import os\n", "import pandas as pd \n", "import sys\n", "import matplotlib.pyplot as plt\n", "import eventBased as eb\n", "\n", "dataFolder = '../data'\n", "experiment = 4\n", "sub = 72\n", "dataRwLb = os.path.join(dataFolder, \"dataLabeld\")\n", "dataEBLb = os.path.join(dataFolder, \"dataEB/sub\"+str(sub))\n", "baseFreq = 50\n", "dT = 1/baseFreq\n", "\n", "start = 10000\n", "stop = int(start+baseFreq*2.56)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-02-14T16:59:52.231439Z", "start_time": "2020-02-14T16:59:52.225375Z" } }, "outputs": [], "source": [ "fileHigh = './highPass.fir'\n", "tapsHigh = list(pd.read_csv(fileHigh,header = None)[0])\n", "fileLow = './lowPass.fir'\n", "tapsLow = list(pd.read_csv(fileLow,header = None)[0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-02-14T16:59:52.236961Z", "start_time": "2020-02-14T16:59:52.233237Z" } }, "outputs": [], "source": [ "plt.close('all')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.679Z" } }, "outputs": [], "source": [ "%matplotlib auto\n", "np.set_printoptions(precision=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## load data " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.680Z" } }, "outputs": [], "source": [ "def getExpNo(s):\n", " idx = 0\n", " for i in range(len(s)):\n", " idx = i\n", " if s[i].isalpha():\n", " break\n", " return int(s[0:idx])\n", "def pik(ls, s):\n", " for e in ls:\n", " if s in e:\n", " return e\n", "def generateFreqScale(dt,N, correctCoeff = None):\n", " if correctCoeff:\n", " dt *= correctCoeff\n", " fs = np.arange(-1/(2*dt),1/(2*dt)-dT/10,1/(dt*N))\n", " return fs \n", "\n", "pathToRwFiles = sorted([os.path.join(dataRwLb,x) for x in os.listdir(dataRwLb) if getExpNo(x) == experiment])\n", "pathToEBFiles = sorted([os.path.join(dataEBLb,x) for x in os.listdir(dataEBLb) if getExpNo(x) == experiment])\n", "\n", "#Original data ---------------------------------------\n", "dataAccRw = pd.read_csv(pik(pathToRwFiles, \"acc\"))\n", "dataGyroRw = pd.read_csv(pik(pathToRwFiles, \"gyro\"))\n", "\n", "#EB data ---------------------------------------\n", "dataAccX = pd.read_csv(pik(pathToEBFiles, \"accx\"), header = None)\n", "dataAccY = pd.read_csv(pik(pathToEBFiles, \"accy\"), header = None)\n", "dataAccZ = pd.read_csv(pik(pathToEBFiles, \"accz\"), header = None)\n", "dataGyroX = pd.read_csv(pik(pathToEBFiles, \"gyrox\"), header = None)\n", "dataGyroY = pd.read_csv(pik(pathToEBFiles, \"gyroy\"), header = None)\n", "dataGyroZ = pd.read_csv(pik(pathToEBFiles, \"gyroz\"), header = None)\n", "\n", "#Some infos:\n", "lengthT = len(dataAccRw)/baseFreq\n", "print(\"Length of the signal, in seconds: {}s\".format(lengthT))\n", "print(\"--------------------------------------------------------------------\")\n", "avgFreqAccX = len(dataAccX)/lengthT\n", "print(\"Average frequency for the x accelerometer: {}Hz\".format(avgFreqAccX))\n", "avgFreqAccY = len(dataAccY)/lengthT\n", "print(\"Average frequency for the y accelerometer: {}Hz\".format(avgFreqAccY))\n", "avgFreqAccZ = len(dataAccZ)/lengthT\n", "print(\"Average frequency for the z accelerometer: {}Hz\".format(avgFreqAccZ))\n", "print(\"--------------------------------------------------------------------\")\n", "avgFreqGyroX = len(dataGyroX)/lengthT\n", "print(\"Average frequency for the x gyroscope: {}Hz\".format(avgFreqGyroX))\n", "avgFreqGyroY = len(dataGyroY)/lengthT\n", "print(\"Average frequency for the y gyroscope: {}Hz\".format(avgFreqGyroY))\n", "avgFreqGyroZ = len(dataGyroZ)/lengthT\n", "print(\"Average frequency for the z gyroscope: {}Hz\".format(avgFreqGyroZ))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.682Z" } }, "outputs": [], "source": [ "delta = []\n", "for lbl in ['x','y','z'] :\n", " ls = list(dataAccRw[lbl])\n", " for i in range(1,len(ls)):\n", " dif = abs(ls[i]-ls[i-1])\n", " if dif != 0:\n", " delta.append(dif)\n", "print(\"Acc Min delta: \", min(delta))\n", "print(\"Acc Max delta: \", max(delta))\n", "print(\"Acc Avg delta: \", np.average(delta))\n", "print()\n", "minDvAcc = [min(delta),min(delta),min(delta)]\n", "\n", "delta = []\n", "for lbl in ['x','y','z'] :\n", " ls = list(dataGyroRw['z'])\n", " for i in range(1,len(ls)):\n", " dif = abs(ls[i]-ls[i-1])\n", " if dif != 0:\n", " delta.append(dif)\n", "print(\"Gyro Min delta: \", min(delta))\n", "print(\"Gyro Max delta: \", max(delta))\n", "print(\"Gyro Avg delta: \", np.average(delta))\n", "print()\n", "minDvGyro = [min(delta),min(delta),min(delta)]\n", "\n", "minDvTot = minDvAcc\n", "minDvTot.extend(minDvGyro)\n", "print(minDvTot)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.683Z" } }, "outputs": [], "source": [ "accx = [list(dataAccX[0]),list(dataAccX[1]),list(dataAccX[2])]\n", "accy = [list(dataAccY[0]),list(dataAccY[1]),list(dataAccY[2])]\n", "accz = [list(dataAccZ[0]),list(dataAccZ[1]),list(dataAccZ[2])]\n", "\n", "gyrox = [list(dataGyroX[0]),list(dataGyroX[1]),list(dataGyroX[2])]\n", "gyroy = [list(dataGyroY[0]),list(dataGyroY[1]),list(dataGyroY[2])]\n", "gyroz = [list(dataGyroZ[0]),list(dataGyroZ[1]),list(dataGyroZ[2])]\n", "\n", "dictSigNames = {'accx': 0,'accy': 1,'accz': 2,'gyrox': 3,'gyroy': 4,'gyroz': 5}\n", "\n", "stream = eb.VectorStreamer()\n", "stream.load(dictSigNames.keys(),accx,accy,accz,gyrox,gyroy,gyroz)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.685Z" } }, "outputs": [], "source": [ "a = eb.inertial.inertial(filtersTaps=[tapsLow,tapsHigh], dv = minDvTot)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.687Z" }, "scrolled": true }, "outputs": [], "source": [ "numWindow = 0\n", "batchWindow = 50\n", "state = 'train'\n", "windowPass = False\n", "\n", "for elem in stream.get():\n", " t,v,lbl,dim = elem[0],elem[1],elem[2],dictSigNames[elem[3]]\n", " if state == 'train':\n", " windowPass = a.buildTrain(t,v,lbl,dim)\n", " else:\n", " res,orig,lblsTot = a.test(t,v,lbl,dim)\n", " if type(orig) != type(None):\n", " print()\n", " print(\"/*\"*35)\n", " print(\"Results: {}({} --> {}) vs original: {}\".format(res,lblsTot,lblsTot[np.argmax(res)],orig))\n", " print(\"/*\"*35)\n", " print()\n", " state = 'train'\n", " if windowPass:\n", " windowPass = False\n", " numWindow +=1\n", " print(\"Window Number: \",numWindow)\n", " if numWindow % batchWindow == 0:\n", " success = a.fit()\n", "# print(\"/*\"*35)\n", "# print(success)\n", "# print(\"/*\"*35)\n", " if success:\n", " state = 'test'\n", " else:\n", " print(\"Error in fitting !\")\n", " print(\"----------------------------------------------------\")\n", " \n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.687Z" } }, "outputs": [], "source": [ "from sklearn.svm import SVC\n", "svm = SVC(kernel='linear', probability = True, decision_function_shape = \"ovr\")\n", "vec = [[1,2,3,4],[7,6,8,9],[1,2,90,5],[4,6,78,0]]\n", "lbl = [4,4,3,3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.689Z" } }, "outputs": [], "source": [ "a = svm.fit(vec,lbl)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.691Z" } }, "outputs": [], "source": [ "# vec.append([1,4,88,2])\n", "# lbl.append(0)\n", "svm.predict_proba(vec)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.692Z" } }, "outputs": [], "source": [ "eb = signalDescriptor(dataAccX[0],dataAccX[1])\n", "rw = signalDescriptor(np.arange(len(dataAccRw['x'])),dataAccRw['x'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.694Z" } }, "outputs": [], "source": [ "fEB = eb.nudft(startT = start, endT = stop)\n", "fRW = rw.nudft(startT = start, endT = stop-1)\n", "print(\"Finished nuDft\")\n", "fEBres = eb.resampledDFT(startT = start, endT = stop)\n", "fRWres = rw.resampledDFT(startT = start, endT = stop)\n", "print(\"Finished reDft\")\n", "f = np.fft.fft(dataAccRw['x'][start:stop])\n", "print(\"Finished numpy fft\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.695Z" } }, "outputs": [], "source": [ "fRWScale = generateFreqScale(dT,len(f))\n", "fEBScale = generateFreqScale(dT,len(fEB),len(f)/len(fEB))\n", "#--------------------------------------\n", "plt.figure()\n", "plt.title(\"data Raw\")\n", "plt.plot(dataAccRw['x'][start:stop])\n", "\n", "plt.figure()\n", "plt.title(\"data EB sampled\")\n", "t,v = eb.findInterval(start,stop)\n", "plt.plot(t,v)\n", "#--------------------------------------\n", "plt.figure()\n", "plt.title(\"Non uniform DFT on Event Based\")\n", "plt.plot(fEBScale,np.abs(eb.shift(fEB)))\n", "\n", "plt.figure()\n", "plt.title(\"Non uniform DFT on Raw\")\n", "plt.plot(fRWScale,np.abs(rw.shift(fRW)))\n", "#---------------------------------------\n", "plt.figure()\n", "plt.title(\"uniform DFT on re-sampled Event Based\")\n", "plt.plot(fRWScale,np.abs(eb.shift(fEBres)))\n", "\n", "plt.figure()\n", "plt.title(\"uniform DFT on raw signal\")\n", "plt.plot(fRWScale,np.abs(rw.shift(fRWres)))\n", "#---------------------------------------\n", "plt.figure()\n", "plt.title(\"Numpy FFT on the original signal\")\n", "plt.plot(fRWScale,np.abs(rw.shift(f)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.710Z" } }, "outputs": [], "source": [ "import scipy.stats as stats" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.711Z" } }, "outputs": [], "source": [ "stats.entropy([1/2, 1/2], base=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.711Z" } }, "outputs": [], "source": [ "a = [7,7,7,7,7,8,9,9,9,9,9,3,3,2,1,1,-4,-3,-9]\n", "sum(np.abs(a))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.713Z" } }, "outputs": [], "source": [ "a = [7,7,7,7,7,8,9,9,9,9,9,3,3,2,1,1]\n", "np.median(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.714Z" } }, "outputs": [], "source": [ "len(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.715Z" } }, "outputs": [], "source": [ "sorted(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.717Z" } }, "outputs": [], "source": [ "b = a[0:11]\n", "c = a[11:len(a)]\n", "\n", "mb = np.median(b)\n", "mc = np.median(c)\n", "q = " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.718Z" } }, "outputs": [], "source": [ "a = [2,3,5,6,8,4,345,567,3]\n", "sum([x**2 for x in a])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2020-02-14T16:59:51.723Z" } }, "outputs": [], "source": [ "taps = tapsLow\n", "f = 19.5\n", "t = np.arange(0.1,100,0.02)\n", "y = np.sin(2*np.pi*f*t)\n", "delay = (len(taps)-1)//2\n", "filt = []\n", "tFilt =[]\n", "gain = []\n", "\n", "for i in range(delay,len(t)-delay):\n", " res = np.dot(y[i-delay:i+delay+1],taps)\n", " filt.append(res)\n", " tFilt.append(i*0.02)\n", "\n", "filt = np.array(filt)\n", "\n", "plt.figure()\n", "plt.title('filtered')\n", "plt.plot(tFilt,filt)\n", "#plt.figure()\n", "#plt.title('Original')\n", "plt.plot(t,y)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.1" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }