nafihahmd
Created December 8, 2020 © MIT

Relax :A Fitness Band for Your Mind

Finally a device that can understand your emotions

AdvancedShowcase (no instructions)4 days35
Relax :A Fitness Band for Your Mind

Things used in this project

Hardware components

Ultra96-V2
Avnet Ultra96-V2
×1

Software apps and online services

Vivado Design Suite HLx Editions
AMD Vivado Design Suite HLx Editions

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Single Channel EEG Amplifier

Code

Realtime EEG Plotting -Terminal Software

Python
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.integrate import simps
from scipy import signal
import pandas as pd
import argparse
import cv2

#%matplotlib tk




// Command Line arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--path", required=True, help="path to eeg recording")
ap.add_argument("-c", "--channel",help="Channel over which eeg data is received")
ap.add_argument("-m", "--mode", required=True, help="eg. plot raw data, delta training etc")
args = vars(ap.parse_args())


fs_Hz = 173.61
data = np.genfromtxt(args["path"])
time = np.arange(data.size) / fs_Hz

t = time[:1023]
d = data[:1023]
i = 1024
line1 = []

if (args["mode"]=="mode1"):
// Function to plot Raw EEG signal in real-time
	def live_plotter(x_vec,y1_data,line1,identifier='',pause_time=0.1):
	    if line1==[]:
	        plt.ion()
	        fig, ax = plt.subplots(1, 1, figsize=(12, 5))
	        line1, = ax.plot(x_vec,y1_data,'',alpha=0.8) 
	        plt.xlabel('Time (seconds)')
	        plt.ylabel('Voltage(uV)')
	        plt.title('Channel 1')
	        plt.show()
	    
	    line1.set_ydata(y1_data)
	    if np.min(y1_data)<=line1.axes.get_ylim()[0] or np.max(y1_data)>=line1.axes.get_ylim()[1]:
	        plt.ylim([np.min(y1_data)-np.std(y1_data),np.max(y1_data)+np.std(y1_data)])
	    plt.pause(pause_time)
	    return line1

	try:
	    while i<4096:
	        d[-1] = data[i]
	        line1 = live_plotter(t,d,line1)
	        d = np.append(d[1:],0.0)
	        i += 1
	except KeyboardInterrupt:
	    print("Program Interrupted")
	    pass


	cap.release()
	cv2.destroyAllWindows()

Calculating Relative Band power with Python

Python
# Import Libraries

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.integrate import simps
from scipy import signal
import pandas as pd

# Load Data
fs_Hz = 173.61

data = np.genfromtxt("Dataset/sleep.txt") # EEG is saved as text file
time = np.arange(data.size) / fs_Hz

win = 4 * fs_Hz
freqs, psd = signal.welch(data, fs_Hz, nperseg=win)

# Fuction to calculate Relative Band Power
def relpower(a):
    (low, high) = a
    idx_delta = np.logical_and(freqs >= low, freqs <= high)
    freq_res = freqs[1] - freqs[0]  # = 1 / 4 = 0.25
    
    # Relative delta power (expressed as a percentage of total power)
    delta_power = simps(psd[idx_delta], dx=freq_res)
    total_power = simps(psd, dx=freq_res)
    delta_rel_power = delta_power / total_power
    return(delta_rel_power)
    
# Define frequency interval of each band
band = {
    'delta':(.5,4),
    'theta':(4,7),
    'alpha':(8,12),
    'beta':(12,30),
    'gamma':(30,100)
}
d_ = []

# Finally calculation
for x in band:
    d_.append(relpower(band.get(x)))
df = pd.DataFrame(list(band.keys()), index=list(range(5)), columns=['Band'])
df['Power'] = pd.DataFrame(d_)

# Now plot the power as a histogram
sns.catplot(x='Band', y = 'Power',  kind="bar", data=df)

Credits

nafihahmd

nafihahmd

4 projects • 2 followers

Comments