daily pastebin goal
18%
SHARE
TWEET

Untitled

a guest Mar 20th, 2018 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """Small example OSC server
  2.  
  3. This program listens to several addresses, and prints some information about
  4. received packets.
  5. """
  6. import argparse
  7. import math
  8. import numpy as np
  9. import pyeeg
  10.  
  11. from pythonosc import dispatcher
  12. from pythonosc import osc_server
  13. from pythonosc import udp_client
  14.  
  15. def normalize(L,trucs):
  16.     for i in range(4):
  17.         L[i] = (L[i]-trucs[i][0])/trucs[i][1]
  18.     return L
  19.  
  20. def compute_baseline(signal, time, newData):
  21.     if signal.shape[0] <= (time*220):
  22.         signal = np.vstack((signal,newData))
  23.     if signal.shape[0] == (time*220): #une minute de eeg
  24.         print("creating baseline...")
  25.         for i in range(4):
  26.             baseline.append([np.mean(signal[:,i]),np.std(signal[:,i])])
  27.         print(baselineSet)
  28.         print(signal.shape)
  29.         signal = np.array([]).reshape(0,4)
  30.         print(signal.shape)
  31.         print('Done')
  32.     return signal
  33.  
  34. def handler(unused_addr, args, ch1,ch2,ch3,ch4):
  35.     baseline = args[1] # on recupere la baseline
  36.     baselineSet = len(baseline)==0
  37.     L= [ch1,ch2,ch3,ch4]
  38.     if baselineSet:
  39.         args[0]=compute_baseline(args[0],10 ,L)
  40.  
  41.     if not baselineSet:
  42.         args[0] = np.vstack((args[0],normalize(L, baseline))) #On ajoute les nouvelles valeurs EEG
  43.         data = args[0]
  44.         buf = np.mean(data[-3*220:], axis=1)
  45.         print(data.shape)
  46.         value = pyeeg.dfa(buf.ravel())
  47.         client.send_message("/puredata/dfa", value)
  48.  
  49. if __name__ == "__main__":
  50.  
  51.   Parser = argparse.ArgumentParser()
  52.   Parser.add_argument("--clientip", default="192.168.0.199",
  53.       help="The ip of the OSC server")
  54.   Parser.add_argument("--clientport", type=int, default=5005,
  55.       help="The port the OSC server is listening on")
  56.   Parser.add_argument("--serverip",
  57.       default="127.0.0.1", help="The ip to listen on")
  58.   Parser.add_argument("--serverport",
  59.       type=int, default=5005, help="The port to listen on")
  60.   Args = Parser.parse_args()
  61.  
  62.   client = udp_client.SimpleUDPClient(Args.clientip, Args.clientport)
  63.  
  64.   baseline = []
  65.   L = np.array([]).reshape(0,4)
  66.   dispatcher = dispatcher.Dispatcher()
  67.   #dispatcher.map("/muse/eeg", print)
  68.   dispatcher.map("/muse/eeg", handler, L, baseline)
  69.  
  70.   server = osc_server.ThreadingOSCUDPServer(
  71.       (Args.serverip, Args.serverport), dispatcher)
  72.   print("Serving on {}".format(server.server_address))
  73.   server.serve_forever()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top