Skip to content
Snippets Groups Projects
Commit 61f77762 authored by Köllőd Csaba's avatar Köllőd Csaba
Browse files

Add legacy.py

parent c220884d
No related branches found
No related tags found
No related merge requests found
import argparse
from struct import *
import matplotlib.pyplot as plt
import numpy as np
# import soundfile as sf
from scipy import signal
########################################
########################################
def notch(data):
# Create/view notch filter
samp_freq = 250 # Sample frequency (Hz)
notch_freq = 50.0 # Frequency to be removed from signal (Hz)
quality_factor = 10.0 # Quality factor
b_notch, a_notch = signal.iirnotch(notch_freq, quality_factor, samp_freq)
# freq, h = signal.freqz(b_notch, a_notch, fs=samp_freq)
# apply notch filter to signal
return signal.filtfilt(b_notch, a_notch, data)
######################################
######################################
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input_file',
required=False,
type=str,
default="test.dat",
metavar="<string>",
help="Input file to be visualized")
# filename = "test.dat"
args = parser.parse_args()
filename = args.input_file
file = open(filename, "rb")
# bin_arr = file.read()
no_packets = unpack('>i', file.read(4))[0]
great_buffer = []
sound = []
print(f"Number of packets in file: {no_packets}")
packet_struct_header = ">IIBBBBBBBBBBBHHH"
packet_struct_header_size = 4 + 4 + 10 + 1 + 2 + 2 + 2
packet_struct_eeg = "i" * 4 * 250 + 3 * "h" * 50 + 3 * "h" * 50
packet_struct_eeg_size = 4 * 4 * 250 + 3 * 2 * 50 + 3 * 2 * 50
packet_struct_emg = "i" * 6 * 250 + 3 * "h" * 50 + 3 * "h" * 50 + "h" * 6000 + "h" * 10
packet_struct_emg_size = 4 * 6 * 250 + 3 * 2 * 50 + 3 * 2 * 50 + 2 * 6000 + 2 * 10
packet_struct_header_s = 2 + 10 + 1 + 1 + 1 + 1
packet_struct_eeg_s = 250 * 4 + 3 * 50 + 3 * 50
packet_struct_emg_s = 250 * 6 + 3 * 50 + 3 * 50
packet_struct_ss_s = 6000 + 10
packet_bin = packet_struct_header + packet_struct_eeg + packet_struct_emg
packet_bin_size = packet_struct_header_size + packet_struct_eeg_size + packet_struct_emg_size
# no_packets = 60
for i in range(no_packets):
try:
packet = [*unpack(packet_bin, file.read(packet_bin_size))]
except Exception as e:
print(e)
print("Got : " + str(len(file.read(packet_bin_size))))
great_buffer.append(packet)
print(f"Packet len: {packet_bin_size}")
print(f" -- header len: {packet_struct_header_size}")
print(f" -- arc len: {4 * 4 * 250}")
print(f" -- imu len: {2 * 50}")
print(f"Remaining: {len(file.read())}")
great_buffer = np.array(great_buffer)
print(f"Great buffer shape: {great_buffer.shape}")
sound = great_buffer[:,
packet_struct_header_s + packet_struct_eeg_s + packet_struct_emg_s:packet_struct_header_s + packet_struct_eeg_s + packet_struct_emg_s + 6000]
arb_gyro = great_buffer[:,
packet_struct_header_s + packet_struct_eeg_s + packet_struct_emg_s - 300:packet_struct_header_s + packet_struct_eeg_s + packet_struct_emg_s - 150]
arb_accel = great_buffer[:,
packet_struct_header_s + packet_struct_eeg_s + packet_struct_emg_s - 150:packet_struct_header_s + packet_struct_eeg_s + packet_struct_emg_s]
arc_gyro = great_buffer[:,
packet_struct_header_s + packet_struct_eeg_s - 300:packet_struct_header_s + packet_struct_eeg_s - 150]
arc_accel = great_buffer[:,
packet_struct_header_s + packet_struct_eeg_s - 150:packet_struct_header_s + packet_struct_eeg_s]
eeg = great_buffer[:, packet_struct_header_s:packet_struct_header_s + 250 * 4]
emg = great_buffer[:,
packet_struct_header_s + packet_struct_eeg_s:packet_struct_header_s + packet_struct_eeg_s + 250 * 6]
gsr = great_buffer[:, -10:]
###############
## Plottable
##############
sound_data = np.reshape(sound, (-1,))
arc_accel = np.reshape(arc_accel, (-1, 3))
arb_accel = np.reshape(arb_accel, (-1, 3))
arc_gyro = np.reshape(arc_gyro, (-1, 3))
arb_gyro = np.reshape(arb_gyro, (-1, 3))
gsr = np.reshape(gsr, (-1,))
eeg = np.reshape(eeg, (-1, 4))
eeg = np.array([notch(eeg[:, i]) for i in range(4)])
emg = np.reshape(emg, (-1, 6))
emg = np.array([notch(emg[:, i]) for i in range(6)])
# print(f"Plot shape: {sound_data.shape}")
plt.title("GSR")
plt.plot(gsr)
plt.show()
plt.title("EMG")
plt.plot(emg[5])
plt.show()
plt.title("EEG")
plt.plot(eeg[0])
plt.plot(eeg[1])
plt.plot(eeg[2])
plt.plot(eeg[3])
plt.show()
plt.title("ARB Accel")
plt.plot(arb_accel)
plt.show()
plt.title("ARB Gyro")
plt.plot(arb_gyro)
plt.show()
plt.title("Sound data")
plt.plot(sound_data)
plt.show()
# sound_data = (sound_data - np.mean(sound_data)) / (np.max(sound_data) - np.min(sound_data))
# sf.write("audio_recording.wav", sound_data, 6000)
"""
if __name__ == "__main__":
main(sys.argv[1:])
"""
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment