From a94b699bbdcfd139386039fb70ce5d8d58c27c14 Mon Sep 17 00:00:00 2001 From: NateSchoolfield Date: Wed, 10 Feb 2021 14:58:37 -0800 Subject: [PATCH] First commit --- auth_key_miband4.txt | 1 + bluesleep.py | 2 +- crymonitor.py | 63 ++++++++++++++++++++++++++++++++++++ datafilter.py | 76 ++++++++++++++++++++++++++++++++++++++++++++ mac_miband4.txt | 1 + test.py | 19 +++++++++++ 6 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 auth_key_miband4.txt create mode 100644 crymonitor.py create mode 100644 datafilter.py create mode 100644 mac_miband4.txt create mode 100644 test.py diff --git a/auth_key_miband4.txt b/auth_key_miband4.txt new file mode 100644 index 0000000..9aba109 --- /dev/null +++ b/auth_key_miband4.txt @@ -0,0 +1 @@ +c04bc1eb2efaea8efc20882624b9a0f6 diff --git a/bluesleep.py b/bluesleep.py index e52ccd0..e196d8c 100755 --- a/bluesleep.py +++ b/bluesleep.py @@ -15,7 +15,7 @@ mac_filename = 'mac.txt' maximize_graph = False vibration_settings = { - 'interval_minutes': 0.1, + 'interval_minutes': 45, 'duration_seconds': 5, 'type': 'random' } diff --git a/crymonitor.py b/crymonitor.py new file mode 100644 index 0000000..5dcc979 --- /dev/null +++ b/crymonitor.py @@ -0,0 +1,63 @@ +import pyaudio, time, threading +import numpy as np +from matplotlib import pyplot as plt +import matplotlib.animation as animation + + +CHUNKSIZE = 1024 # fixed chunk size + +# initialize portaudio +p = pyaudio.PyAudio() + +info = p.get_host_api_info_by_index(0) +numdevices = info.get('deviceCount') +for i in range(0, numdevices): + if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0: + print("Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0, i).get('name')) + +stream = p.open( + format=pyaudio.paInt16, + channels=1, + rate=44100, + input=True, + frames_per_buffer=CHUNKSIZE, + input_device_index=18 + ) + + + + +plt.style.use('dark_background') +graph_figure = plt.figure() +graph_figure.canvas.set_window_title('blesleep') + +graph_axes = graph_figure.add_subplot(1, 1, 1) +graph_data = {} + + + +def graph_animation(i): + graph_axes.clear() + + graph_axes.plot(numpydata) + +ani = animation.FuncAnimation(graph_figure, graph_animation, interval=1000) + +def get_audio_data(): + global numpydata + while True: + data = stream.read(CHUNKSIZE) + numpydata = np.frombuffer(data, dtype=np.int16) + time.sleep(1) + + +threading.Thread(target=get_audio_data).start() + + +plt.show() + + +# close stream +stream.stop_stream() +stream.close() +p.terminate() \ No newline at end of file diff --git a/datafilter.py b/datafilter.py new file mode 100644 index 0000000..c642415 --- /dev/null +++ b/datafilter.py @@ -0,0 +1,76 @@ +import time, os, csv +from datetime import datetime +from matplotlib import pyplot as plt + +datapath = '/home/daddy/Projects/miband/data/2021_02_07/' +window_min = 30 +figure_height = 9 +figure_width = 18 +fullscreen = False + + +files = os.listdir(datapath) +wavs = [] +csvs = [] +for file in files: + if 'wav' in file: + wavs.append(file) + elif 'csv' in file and 'raw' in file: + csvs.append(file) + + +event_data_list = [] +for wav in wavs: + event_dict = { + 'name': wav, + 'data': { + 'mov_x': [], + 'mov_y': [], + 'bpm_x': [], + 'bpm_y': [] + } + } + + wavtime = datetime.strptime(wav, '%Y_%m_%d__%H_%M_%S.wav') + wavestamp = wavtime.timestamp() + + plotdata_mov_x = [] + plotdata_mov_y = [] + plotdata_bpm_x = [] + plotdata_bpm_y = [] + for mycsv in csvs: + with open((datapath + mycsv), newline='') as csvfile: + csvreader = csv.DictReader(csvfile, delimiter=',') + for row in csvreader: + if abs((float(wavestamp) - float(row['time']))) <= (window_min * 60): + if 'bpm' in row: + event_dict['data']['bpm_x'].append( int(row['bpm']) ) + event_dict['data']['bpm_y'].append( datetime.fromtimestamp(float(row['time'])) ) + elif 'movement' in row: + event_dict['data']['mov_x'].append( int(row['movement']) ) + event_dict['data']['mov_y'].append( datetime.fromtimestamp(float(row['time'])) ) + + event_data_list.append(event_dict) + + +for event_data in event_data_list: + event_name = event_data['name'].rsplit('.', 1)[0] + output_png_filename = '{}{}'.format(event_name, ".png") + data = event_data['data'] + plt.close("all") + + fig, ax = plt.subplots() + ax2 = ax.twinx() + ax.plot(data['bpm_y'], data['bpm_x'], color="red") + ax2.plot(data['mov_y'], data['mov_x'], color="blue") + + fig.set_figheight(figure_height) + fig.set_figwidth(figure_width) + fig.autofmt_xdate() + + if fullscreen: + plt.get_current_fig_manager().full_screen_toggle() + plt.title(event_name) + if data['bpm_y']: + plt.savefig(datapath + output_png_filename) + plt.show() \ No newline at end of file diff --git a/mac_miband4.txt b/mac_miband4.txt new file mode 100644 index 0000000..9aa6ded --- /dev/null +++ b/mac_miband4.txt @@ -0,0 +1 @@ +EB:DD:13:97:9C:0D diff --git a/test.py b/test.py new file mode 100644 index 0000000..7313b83 --- /dev/null +++ b/test.py @@ -0,0 +1,19 @@ +import numpy as np +import matplotlib.pyplot as plt +x = np.arange(0, 10, 0.1) +y1 = 0.05 * x**2 +y2 = -1 *y1 + +fig, ax1 = plt.subplots() + + + +ax2 = ax1.twinx() +figures=[manager.canvas.figure + for manager in plt._pylab_helpers.Gcf.get_all_fig_managers()] +print(figures) + +ax1.plot(x, y1, 'g-') +ax2.plot(x, y2, 'b-') + +plt.show() \ No newline at end of file