def test_cpedometer(): """ Test pedometer implemented in C Returns ------- status: str "OK" or "ERROR" """ from sensbiotk.io import iofox import pylab as py [timea, acc] = \ iofox.load_foxacc_csvfile("./data/walk4_acc.csv") # [timea, acc] = cut_signal(timea, acc, 12.0, 29.0) peak_detected = np.zeros(len(acc)) for k in range(0, len(acc)): tab = np.array(acc[k, 0:3], dtype='f') [step, state, peak_detected[k]] = compute_ped(k, tab) print "Step numbers=", step py.figure() py.plot(timea[:, 0], acc) py.figure() #py.plot(peak_detected, "o") py.show()
def test_cfreefall(): """ Test pedometer implemented in C Returns ------- status: str "OK" or "ERROR" """ from sensbiotk.io import iofox [timea, acc] = \ iofox.load_foxacc_csvfile(".data/freefall1_acc.csv") val_state = np.zeros(len(acc)) val_debug = np.zeros(len(acc)) for k in range(0, len(acc)): tab = np.array(acc[k, 0:3], dtype='f') [step, val_state[k], val_debug[k]] = compute_ped(k, tab) py.figure() py.title("FreeFall Accelerations") py.plot(timea[:, 0], acc) py.figure() py.title("FreeFall detection Results (C)") py.plot(val_state) return
def test_load(): """ Test loading functions """ # read imutest_acc/mag/gyr with load_foximu_csvfile [time, acc, mag, gyr] = fox.load_foximu_csvfile("data/imutest_acc.csv", "data/imutest_mag.csv", "data/imutest_gyr.csv", 0.06, 1) sizedata = 285 if ((np.size(time) == sizedata) and (np.size(acc) == sizedata * 3) and (np.size(mag) == sizedata * 3) and (np.size(gyr) == sizedata * 3)): resp = True else: resp = False yield assert_equal, resp, True # read doortest_big_acc with load_foxacc_csvfile [timea, acc] = fox.load_foxacc_csvfile("data/doortest_big_acc.csv") sizedata = 262628 if (np.size(timea[:, 0]) == sizedata) and (np.size(acc) == sizedata * 3): resp = True else: resp = False yield assert_equal, resp, True # read doortest_big_mag with load_foxmag_csvfile [timem, mag] = fox.load_foxmag_csvfile("data/doortest_big_mag.csv") sizedata = 392876 if (np.size(timem[:, 0]) == sizedata) and (np.size(mag) == sizedata * 3): resp = True else: resp = False yield assert_equal, resp, True # read doortest_big_gyr with load_foxgyr_csvfile [timeg, gyr] = fox.load_foxgyr_csvfile("data/doortest_big_gyr.csv") sizedata = 270388 if (np.size(timeg[:, 0]) == sizedata) and (np.size(gyr) == sizedata * 3): resp = True else: resp = False yield assert_equal, resp, True # read gpiotest with load_foxgpio_csvfile [timeio, gpio] = fox.load_foxgpio_csvfile("data/gpiotest_gpio.csv") sizedata = 7143 if (np.size(timeio[:, 0]) == sizedata) and (np.size(gpio) == sizedata * 5): resp = True else: resp = False yield assert_equal, resp, True
def test_save(): """ Test saving functions """ # save imutest_acc with load_foxacc_csvfile [time, acc] = fox.load_foxacc_csvfile("data/imutest_acc.csv") resp = fox.save_foxacc_csvfile("tmpdata/imutest_acc_save.csv", time, acc) yield assert_equal, resp, "OK" [time1, acc1] = fox.load_foxacc_csvfile("tmpdata/imutest_acc_save.csv") yield assert_array_almost_equal, time, time1 yield assert_array_almost_equal, acc, acc1 # save imutest_mag with load_foxmag_csvfile [time, mag] = fox.load_foxmag_csvfile("data/imutest_mag.csv") resp = fox.save_foxmag_csvfile("tmpdata/imutest_mag_save.csv", time, mag) yield assert_equal, resp, "OK" [time1, mag1] = fox.load_foxmag_csvfile("tmpdata/imutest_mag_save.csv") yield assert_array_almost_equal, time, time1 yield assert_array_almost_equal, mag, mag1 # save imutest_gyr with load_foxgyr_csvfile [time, gyr] = fox.load_foxgyr_csvfile("data/imutest_gyr.csv") resp = fox.save_foxgyr_csvfile("tmpdata/imutest_gyr_save.csv", time, gyr) yield assert_equal, resp, "OK" [time1, gyr1] = fox.load_foxgyr_csvfile("tmpdata/imutest_gyr_save.csv") yield assert_array_almost_equal, time, time1 yield assert_array_almost_equal, gyr, gyr1 # save_foxsignals_csvfile [time, acc, mag, gyr] = fox.load_foximu_csvfile("data/imutest_acc.csv", "data/imutest_mag.csv", "data/imutest_gyr.csv", 0.06, 1) resp = fox.save_foxsignals_csvfile(time, acc, mag, gyr, "tmpdata/imutest.csv") yield assert_equal, resp, "OK"
def test_pedometer(): """ Test pedometer implemented in Python Returns ------- status: str "OK" or "ERROR" """ from sensbiotk.io import iofox from sensbiotk.algorithms import basic as algo import pylab as py [timea, acc] = iofox.load_foxacc_csvfile("./data/walk1_acc.csv") [timea, acc] = algo.cut_signal(timea, acc, 12.0, 29.0) acc_norm = algo.compute_norm(acc) #acc_filt = algo.lowpass_filter(acc_norm, 2.0, 100.0) #acc_filt = algo.lowpass_filter2(acc_norm, 2.0, 200.0) acc_filt = algo.moving_average(acc_norm, 30) #acc_filt = algo.moving_average2(acc_norm, 50) index_peak = algo.search_maxpeak(acc_filt) [time_peak, acc_peak] = algo.threshold_signal(timea[index_peak], acc_filt[index_peak], 11.0) print "Step numbers=", len(acc_peak) py.figure() py.plot(timea[:, 0], acc[:, 0:3]) py.title("Walking 1 Accelerations") py.legend(('x', 'y', 'z'), bbox_to_anchor=(0, 1, 1, 0), ncol=2, loc=3, borderaxespad=0.) py.figure() py.title("Walking 1 Results") py.plot(timea[:, 0], acc_norm) py.plot(timea[:, 0], acc_filt) py.plot(time_peak, acc_peak, "o") py.figure() py.plot(np.diff(acc_filt)) py.plot(np.diff(np.sign(np.diff(acc_filt)) < 0)) py.show() return "OK"
def test_freefall(): """ Test freefall detector implemented in Python Returns ------- status: str "OK" or "ERROR" """ from sensbiotk.io import iofox from sensbiotk.algorithms import basic as algo [timea, acc] = iofox.load_foxacc_csvfile("./data/freefall1_acc.csv") acc_norm = algo.compute_norm(acc) [state_freefall] = threshold_inf(acc_norm, 1.5) [state_freefall] = threshold_time(timea[:, 0], state_freefall, 0.1) [state_inactive] = search_state(timea[:, 0], acc_norm, 10.0, 5.0) [state_inactive] = threshold_time(timea[:, 0], state_inactive, 0.1) py.figure() py.plot(timea[:, 0], acc[:, 0:3]) py.title("FreeFall Accelerations") py.legend(('x', 'y', 'z'), bbox_to_anchor=(0, 1, 1, 0), ncol=2, loc=3, borderaxespad=0.) py.figure() py.title("FreeFall detection Results (Python)") py.plot(acc_norm) py.plot(state_freefall) py.plot(state_inactive * 5) return "OK"
def main(argv): """ Main command """ options = [] filename = "" try: opts, _ = getopt.getopt(argv, "i:d:b:vt:f:pamgh", [ "input=", "dir=", "basename", "verif", "time=", "frequency=", "plot", "acc", "mag", "gyr", "help" ]) except getopt.GetoptError: usage() sys.exit(2) period = 0.005 diroutput = "." newbasename = "" filenames = "" for opt, arg in opts: if opt in ("-h", "--help"): usage() sys.exit() elif opt in ("-i", "--input"): fnames = arg if not os.path.isfile(fnames): if not os.path.isdir(fnames): print "error: input file/dir does not exist" usage() sys.exit(2) else: # directory input specified filenames = os.listdir(fnames) idx = 0 for onefile in filenames: filenames[idx] = fnames + "/" + onefile #filenames[idx] = fnames + "/" + filenames[idx] idx = idx + 1 else: # filename input specified filenames = [fnames] elif opt in ("-d", "--dir"): diroutput = arg elif opt in ("-b", "--basename"): newbasename = arg elif opt in ("-v", "--verif"): options.append("-v") elif opt in ("-t", "--time"): try: period = float(arg) except ValueError: usage() sys.exit(2) if period <= 0: usage() sys.exit(2) elif opt in ("-f", "--frequency"): try: period = float(arg) except ValueError: usage() sys.exit(2) if period <= 0: usage() sys.exit(2) else: period = 1 / period elif opt in ("-p", "--plot"): options.append("-p") elif opt in ("-a", "--acc"): options.append("-a") elif opt in ("-m", "--mag"): options.append("-m") elif opt in ("-g", "--gyr"): options.append("-g") if len(filenames) == 0: usage() sys.exit(2) for filename in filenames: print "Conversion:", filename # Get names file basefile, fileacc, filemag, filegyr, fileimu =\ getnamefiles(filename, newbasename, diroutput) # Read and convert file answer = iofox.convert_sensors_rawfile(filename, fileacc, filemag, filegyr) if answer != "OK": usage() sys.exit(2) # Load ascii files converted [myt, acc, mag, gyr] = \ iofox.load_foximu_csvfile(fileacc, filemag, filegyr, period, 1) iofox.save_foxsignals_csvfile(myt, acc, mag, gyr, *fileimu) # Plot if necessary if "-p" in options: label = basefile + " IMU" viz.plot_imu(-1, label, myt, acc, mag, gyr) if "-a" in options: label = basefile + " Acc" plotsig(myt, acc, label, "ACC (m/s^2") if "-m" in options: label = basefile + " Mag" plotsig(myt, mag, label, "MAG (gauss)") if "-g" in options: label = basefile + " Gyr" plotsig(myt, gyr, label, "GYR (rad/s)") if "-v" in options: [myt, acc] = iofox.load_foxacc_csvfile(fileacc) label = basefile + " time verif." veriftime(label, myt[:, 0]) # Clean temp files os.remove(fileacc) os.remove(filemag) os.remove(filegyr) plt.show()