【英语学习系统】神念科技脑电头带适配、脑电信号读取

2019-04-13 14:55发布

使用神念科技的脑电头带进行脑电信号的获取,用于记录学习者学习过程中的专注度放松度等指标
脑电头带:

购买头带后带有的一系列开发用的资料:


脑电头带使用蓝牙进行信号传输,首先安装千月蓝牙助手并与脑电头带建立连接

购买头戴时带有一系列的开发工具,比如一个已经集成好的图形化脑电信号的程序,下图是佩戴脑电头带后测试得到的数据图


开发包中包含一个HelloEEG程序,为一个与脑电头带进行信号传输的demo,自动进行串口查询与信号检测

源代码:
using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Reflection; using System.IO; using System.IO.Ports; using NeuroSky.ThinkGear; using NeuroSky.ThinkGear.Algorithms; using System.Net.Sockets; using System.Net; namespace testprogram { class Program { static Connector connector; static bool golfZoneDemo = false; static double task_famil_baseline, task_famil_cur, task_famil_change; static bool task_famil_first; static double mental_eff_baseline, mental_eff_cur, mental_eff_change; static bool mental_eff_first; //******************* //socket通信,客户端 //******************* byte[] data = new byte[1024]; static Socket newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); static IPEndPoint ie = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000);//服务器的IP和端口 public static void Main(string[] args) { while (true) { try { //因为客户端只是用来向特定的服务器发送信息,所以不需要绑定本机的IP和端口。不需要监听。 newclient.Connect(ie); break; } catch (SocketException e) { Console.WriteLine("*******************unable to connect to server*****************"); Console.WriteLine(e.ToString()); return; } } //接收消息 //int recv = newclient.Receive(data); //string stringdata = Encoding.ASCII.GetString(data, 0, recv); //Console.WriteLine(stringdata); //while (true) //{ // string input =""; // newclient.Send(Encoding.ASCII.GetBytes(input)); //接收消息 //recv = newclient.Receive(data); //stringdata = Encoding.ASCII.GetString(data, 0, recv); //Console.WriteLine(stringdata); //} //Console.WriteLine("disconnect from sercer"); //newclient.Shutdown(SocketShutdown.Both); //newclient.Close(); Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); if (assembly != null) { object[] customAttribute1 = assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false); if ((customAttribute1 != null) && (customAttribute1.Length > 0)) Console.WriteLine(((AssemblyTitleAttribute)customAttribute1[0]).Title); object[] customAttribute2 = assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false); if ((customAttribute2 != null) && (customAttribute2.Length > 0)) Console.WriteLine(((AssemblyCompanyAttribute)customAttribute2[0]).Company); Console.WriteLine(assembly.GetName().Version.ToString()); } AppDomain MyDomain = AppDomain.CurrentDomain; Assembly[] AssembliesLoaded = MyDomain.GetAssemblies(); foreach (Assembly MyAssembly in AssembliesLoaded) { if (MyAssembly.FullName.Contains("ThinkGear")) Console.WriteLine(MyAssembly.FullName); } Console.WriteLine("----------"); if (golfZoneDemo) Console.WriteLine("Hello Golfer!"); else Console.WriteLine("Hello EEG!"); Console.WriteLine("----------"); // Initialize a new Connector and add event handlers connector = new Connector(); connector.DeviceConnected += new EventHandler(OnDeviceConnected); connector.DeviceConnectFail += new EventHandler(OnDeviceFail); connector.DeviceValidating += new EventHandler(OnDeviceValidating); // Scan for devices // add this one to scan 1st //connector.ConnectScan("COM40"); connector.ConnectScan("COM17"); //start the mental effort and task familiarity calculations if (golfZoneDemo) { connector.setMentalEffortEnable(false); connector.setTaskFamiliarityEnable(false); connector.setBlinkDetectionEnabled(false); } else { connector.enableTaskDifficulty(); //depricated connector.enableTaskFamiliarity(); //depricated connector.setMentalEffortRunContinuous(true); connector.setMentalEffortEnable(true); connector.setTaskFamiliarityRunContinuous(true); connector.setTaskFamiliarityEnable(true); connector.setBlinkDetectionEnabled(true); } task_famil_baseline = task_famil_cur = task_famil_change = 0.0; task_famil_first = true; mental_eff_baseline = mental_eff_cur = mental_eff_change = 0.0; mental_eff_first = true; Thread.Sleep(8 * 60 * 1000); // time to live for this program (8 min * 60 sec * 1000 ms) Console.WriteLine("----------"); if (golfZoneDemo) Console.WriteLine("Time is up. Goodbye from Golf Zone sample program!"); else Console.WriteLine("Time is up. Goodbye from EEG sample program!"); Console.WriteLine("----------"); // Close all open connections connector.Close(); Thread.Sleep(10 * 1000); // delay long enough for a human to see the message (10 sec * 1000 ms) Environment.Exit(0); } /** * Called when a device is connected */ static void OnDeviceConnected(object sender, EventArgs e) { Connector.DeviceEventArgs de = (Connector.DeviceEventArgs)e; Console.WriteLine("Device found on: " + de.Device.PortName); de.Device.DataReceived += new EventHandler(OnDataReceived); } /** * Called when scanning fails */ static void OnDeviceFail(object sender, EventArgs e) { Console.WriteLine("No devices found! :("); } /** * Called when each port is being validated */ static void OnDeviceValidating(object sender, EventArgs e) { Console.WriteLine("Validating: "); } static byte rcv_poorSignal_last = 255; // start with impossible value static byte rcv_poorSignal; static byte rcv_poorSig_cnt = 0; /** * Called when data is received from a device */ static void OnDataReceived(object sender, EventArgs e) { //Device d = (Device)sender; Device.DataEventArgs de = (Device.DataEventArgs)e; DataRow[] tempDataRowArray = de.DataRowArray; TGParser tgParser = new TGParser(); tgParser.Read(de.DataRowArray); /* Loop through new parsed data */ for (int i = 0; i < tgParser.ParsedData.Length; i++) { if (tgParser.ParsedData[i].ContainsKey("MSG_MODEL_IDENTIFIED")) { Console.WriteLine("Model Identified"); connector.setMentalEffortRunContinuous(true); connector.setMentalEffortEnable(true); connector.setTaskFamiliarityRunContinuous(true); connector.setTaskFamiliarityEnable(true); connector.setPositivityEnable(false); // // the following are included to demonstrate the overide messages // connector.setRespirationRateEnable(true); // not allowed with EEG connector.setPositivityEnable(true);// not allowed when famil/diff are enabled } if (tgParser.ParsedData[i].ContainsKey("MSG_ERR_CFG_OVERRIDE")) { Console.WriteLine("ErrorConfigurationOverride: " + tgParser.ParsedData[i]["MSG_ERR_CFG_OVERRIDE"]); } if (tgParser.ParsedData[i].ContainsKey("MSG_ERR_NOT_PROVISIONED")) { Console.WriteLine("ErrorModuleNotProvisioned: " + tgParser.ParsedData[i]["MSG_ERR_NOT_PROVISIONED"]); } if (tgParser.ParsedData[i].ContainsKey("TimeStamp")) { //Console.WriteLine("TimeStamp"); } if (tgParser.ParsedData[i].ContainsKey("Raw")) { //Console.WriteLine("Raw: " + tgParser.ParsedData[i]["Raw"]); } if (tgParser.ParsedData[i].ContainsKey("RespiratoryRate")) { Console.WriteLine("RespiratoryRate: " + tgParser.ParsedData[i]["RespiratoryRate"]); } if (tgParser.ParsedData[i].ContainsKey("RawCh1")) { //Console.WriteLine("RawCh1: " + tgParser.ParsedData[i]["RawCh1"]); } if (tgParser.ParsedData[i].ContainsKey("RawCh2")) { //Console.Write(", Raw Ch2:" + tgParser.ParsedData[i]["RawCh2"]); } if (tgParser.ParsedData[i].ContainsKey("PoorSignal")) { // NOTE: this doesn't work well with BMD sensors Dual Headband or CardioChip rcv_poorSignal = (byte)tgParser.ParsedData[i]["PoorSignal"]; if (rcv_poorSignal != rcv_poorSignal_last || rcv_poorSig_cnt >= 30) { // when there is a change of state OR every 30 reports rcv_poorSig_cnt = 0; // reset counter rcv_poorSignal_last = rcv_poorSignal; if (rcv_poorSignal == 0) { // signal is good, we are connected to a subject Console.WriteLine("SIGNAL: we have good contact with the subject"); } else { Console.WriteLine("SIGNAL: is POOR: " + rcv_poorSignal); } } else rcv_poorSig_cnt++; } if (tgParser.ParsedData[i].ContainsKey("Attention")) { if (tgParser.ParsedData[i]["Attention"] != 0) { // Console.WriteLine("测试数据Attention*****************" + tgParser.ParsedData[i]["Attention"]); Console.WriteLine("Attention : " + tgParser.ParsedData[i]["Attention"]); string Attention = tgParser.ParsedData[i]["Attention"].ToString(); newclient.Send(Encoding.ASCII.GetBytes(Attention)); Console.WriteLine("*****************************传输:"+Attention); } } if (tgParser.ParsedData[i].ContainsKey("Attention 1")) { if (tgParser.ParsedData[i]["Attention 1"] != 0) Console.WriteLine("Attention 1: " + tgParser.ParsedData[i]["Attention 1"]); } if (tgParser.ParsedData[i].ContainsKey("Attention 2")) { if (tgParser.ParsedData[i]["Attention 2"] != 0) Console.WriteLine("Attention 2: " + tgParser.ParsedData[i]["Attention 2"]); } if (tgParser.ParsedData[i].ContainsKey("Meditation")) { if (tgParser.ParsedData[i]["Meditation"] != 0) Console.WriteLine("Meditation: " + tgParser.ParsedData[i]["Meditation"]); } if (tgParser.ParsedData[i].ContainsKey("Meditation 1")) { if (tgParser.ParsedData[i]["Meditation 1"] != 0) Console.WriteLine("Meditation 1: " + tgParser.ParsedData[i]["Meditation 1"]); } if (tgParser.ParsedData[i].ContainsKey("Meditation 2")) { if (tgParser.ParsedData[i]["Meditation 2"] != 0) Console.WriteLine("Meditation 2: " + tgParser.ParsedData[i]["Meditation 2"]); } if (!golfZoneDemo) // turn this off for the Golf Zone Demo { if (tgParser.ParsedData[i].ContainsKey("BlinkStrength")) { Console.WriteLine(" BlinkStrength: " + tgParser.ParsedData[i]["BlinkStrength"]); } if (tgParser.ParsedData[i].ContainsKey("MentalEffort")) { mental_eff_cur = (Double)tgParser.ParsedData[i]["MentalEffort"]; if (mental_eff_first) { mental_eff_first = false; } else { /* * calculate the percentage change from the previous sample */ mental_eff_change = calcPercentChange(mental_eff_baseline, mental_eff_cur); if (mental_eff_change > 500.0 || mental_eff_change < -500.0) { Console.WriteLine(" Mental Effort: excessive range"); } else { Console.WriteLine(" Mental Effort: " + mental_eff_change + " %"); } } mental_eff_baseline = mental_eff_cur; } if (tgParser.ParsedData[i].ContainsKey("TaskFamiliarity")) { task_famil_cur = (Double)tgParser.ParsedData[i]["TaskFamiliarity"]; if (task_famil_first) { task_famil_first = false; } else { /* * calculate the percentage change from the previous sample */ task_famil_change = calcPercentChange(task_famil_baseline, task_famil_cur); if (task_famil_change > 500.0 || task_famil_change < -500.0) { Console.WriteLine(" Task Familiarity: excessive range"); } else { Console.WriteLine(" Task Familiarity: " + task_famil_change + " %"); } } task_famil_baseline = task_famil_cur; } if (tgParser.ParsedData[i].ContainsKey("Positivity")) { Console.WriteLine(" Positivity: " + tgParser.ParsedData[i]["Positivity"]); } } if (golfZoneDemo) { if (tgParser.ParsedData[i].ContainsKey("ReadyZone")) { Console.Write(" GolfZone: "); if (tgParser.ParsedData[i]["ReadyZone"] == 3) { Console.WriteLine("Elite: you are the best, putt when you are ready"); } else if (tgParser.ParsedData[i]["ReadyZone"] == 2) { Console.WriteLine("Intermediate: you are good, relax and putt smoothly"); } else if (tgParser.ParsedData[i]["ReadyZone"] == 1) { Console.WriteLine("Beginner: take a breath and don't rush"); } else { Console.WriteLine("Try to relax, focus on your target"); } } } } } static double calcPercentChange(double baseline, double current) { double change; if (baseline == 0.0) baseline = 1.0; //don't allow divide by zero /* * calculate the percentage change */ change = current - baseline; change = (change / baseline) * 1000.0 + 0.5; change = Math.Floor(change) / 10.0; return (change); } } }


下面为更改后的代码,与demo相比将专注度以及放松的的值获取后保存在文件中,方便Unity脚本读取数据并显示到系统中using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Reflection; using System.IO; using NeuroSky.ThinkGear; using System.ComponentModel; namespace testprogram { class Program { static Connector connector; static string text; static bool golfZoneDemo = false; static double task_famil_baseline, task_famil_cur, task_famil_change; static bool task_famil_first; static double mental_eff_baseline, mental_eff_cur, mental_eff_change; static bool mental_eff_first; public int count1=0, count2=0; public float sum1=0, sum2=0; public static void Main(string[] args) { Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); if (assembly != null) { object[] customAttribute1 = assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false); if ((customAttribute1 != null) && (customAttribute1.Length > 0)) Console.WriteLine(((AssemblyTitleAttribute)customAttribute1[0]).Title); object[] customAttribute2 = assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false); if ((customAttribute2 != null) && (customAttribute2.Length > 0)) Console.WriteLine(((AssemblyCompanyAttribute)customAttribute2[0]).Company); Console.WriteLine(assembly.GetName().Version.ToString()); } AppDomain MyDomain = AppDomain.CurrentDomain; Assembly[] AssembliesLoaded = MyDomain.GetAssemblies(); foreach (Assembly MyAssembly in AssembliesLoaded) { if (MyAssembly.FullName.Contains("ThinkGear")) Console.WriteLine(MyAssembly.FullName); } Console.WriteLine("----------"); if (golfZoneDemo) Console.WriteLine("Hello Golfer!"); else Console.WriteLine("Hello EEG!"); Console.WriteLine("----------"); // Initialize a new Connector and add event handlers connector = new Connector(); connector.DeviceConnected += new EventHandler(OnDeviceConnected); connector.DeviceConnectFail += new EventHandler(OnDeviceFail); connector.DeviceValidating += new EventHandler(OnDeviceValidating); // Scan for devices // add this one to scan 1st //connector.ConnectScan("COM40"); connector.ConnectScan("COM3"); //start the mental effort and task familiarity calculations if (golfZoneDemo) { connector.setMentalEffortEnable(false); connector.setTaskFamiliarityEnable(false); connector.setBlinkDetectionEnabled(false); } else { connector.enableTaskDifficulty(); //depricated connector.enableTaskFamiliarity(); //depricated connector.setMentalEffortRunContinuous(true); connector.setMentalEffortEnable(true); connector.setTaskFamiliarityRunContinuous(true); connector.setTaskFamiliarityEnable(true); connector.setBlinkDetectionEnabled(true); } task_famil_baseline = task_famil_cur = task_famil_change = 0.0; task_famil_first = true; mental_eff_baseline = mental_eff_cur = mental_eff_change = 0.0; mental_eff_first = true; Thread.Sleep(28 * 60 * 1000); // time to live for this program (8 min * 60 sec * 1000 ms) Console.WriteLine("----------"); if (golfZoneDemo) Console.WriteLine("Time is up. Goodbye from Golf Zone sample program!"); else Console.WriteLine("Time is up. Goodbye from EEG sample program!"); Console.WriteLine("----------"); // Close all open connections connector.Close(); Thread.Sleep(10 * 1000); // delay long enough for a human to see the message (10 sec * 1000 ms) Environment.Exit(0); Console.ReadLine(); } /** * Called when a device is connected */ static void OnDeviceConnected(object sender, EventArgs e) { Connector.DeviceEventArgs de = (Connector.DeviceEventArgs)e; Console.WriteLine("Device found on: " + de.Device.PortName); de.Device.DataReceived += new EventHandler(OnDataReceived); } /** * Called when scanning fails */ static void OnDeviceFail(object sender, EventArgs e) { Console.WriteLine("No devices found! :("); } /** * Called when each port is being validated */ static void OnDeviceValidating(object sender, EventArgs e) { Console.WriteLine("Validating: "); } static byte rcv_poorSignal_last = 255; // start with impossible value static byte rcv_poorSignal; static byte rcv_poorSig_cnt = 0; /** * Called when data is received from a device */ static void OnDataReceived(object sender, EventArgs e) { //Device d = (Device)sender; Device.DataEventArgs de = (Device.DataEventArgs)e; DataRow[] tempDataRowArray = de.DataRowArray; TGParser tgParser = new TGParser(); tgParser.Read(de.DataRowArray); int count1 = 0, count2 = 0; double sum1 = 0, sum2 = 0; /* Loop through new parsed data */ for (int i = 0; i < tgParser.ParsedData.Length; i++) { if (tgParser.ParsedData[i].ContainsKey("MSG_MODEL_IDENTIFIED")) { Console.WriteLine("Model Identified"); connector.setMentalEffortRunContinuous(true); connector.setMentalEffortEnable(true); connector.setTaskFamiliarityRunContinuous(true); connector.setTaskFamiliarityEnable(true); connector.setPositivityEnable(false); // // the following are included to demonstrate the overide messages // connector.setRespirationRateEnable(true); // not allowed with EEG connector.setPositivityEnable(true);// not allowed when famil/diff are enabled } if (tgParser.ParsedData[i].ContainsKey("MSG_ERR_CFG_OVERRIDE")) { Console.WriteLine("ErrorConfigurationOverride: " + tgParser.ParsedData[i]["MSG_ERR_CFG_OVERRIDE"]); } if (tgParser.ParsedData[i].ContainsKey("MSG_ERR_NOT_PROVISIONED")) { Console.WriteLine("ErrorModuleNotProvisioned: " + tgParser.ParsedData[i]["MSG_ERR_NOT_PROVISIONED"]); } if (tgParser.ParsedData[i].ContainsKey("TimeStamp")) { //Console.WriteLine("TimeStamp"); } if (tgParser.ParsedData[i].ContainsKey("Raw")) { //Console.WriteLine("Raw: " + tgParser.ParsedData[i]["Raw"]); } if (tgParser.ParsedData[i].ContainsKey("RespiratoryRate")) { Console.WriteLine("RespiratoryRate: " + tgParser.ParsedData[i]["RespiratoryRate"]); } if (tgParser.ParsedData[i].ContainsKey("RawCh1")) { //Console.WriteLine("RawCh1: " + tgParser.ParsedData[i]["RawCh1"]); } if (tgParser.ParsedData[i].ContainsKey("RawCh2")) { //Console.Write(", Raw Ch2:" + tgParser.ParsedData[i]["RawCh2"]); } if (tgParser.ParsedData[i].ContainsKey("PoorSignal")) { // NOTE: this doesn't work well with BMD sensors Dual Headband or CardioChip rcv_poorSignal = (byte)tgParser.ParsedData[i]["PoorSignal"]; if (rcv_poorSignal != rcv_poorSignal_last || rcv_poorSig_cnt >= 30) { // when there is a change of state OR every 30 reports rcv_poorSig_cnt = 0; // reset counter rcv_poorSignal_last = rcv_poorSignal; if (rcv_poorSignal == 0) { // signal is good, we are connected to a subject Console.WriteLine("SIGNAL: we have good contact with the subject"); } else { Console.WriteLine("SIGNAL: is POOR: " + rcv_poorSignal); } } else rcv_poorSig_cnt++; } if (tgParser.ParsedData[i].ContainsKey("Attention")) { if (tgParser.ParsedData[i]["Attention"] != 0) { Console.WriteLine("Attention : " + tgParser.ParsedData[i]["Attention"]); text+= tgParser.ParsedData[i]["Attention"]; Stream stream = File.Open("E:\项目文档\TestTxt.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); StreamWriter sw = new StreamWriter(stream, Encoding.Default); sw.WriteLine(tgParser.ParsedData[i]["Attention"]); sum1 += tgParser.ParsedData[i]["Attention"]; count1++; sw.Flush(); sw.Close(); } } //各种参数采集 //if (tgParser.ParsedData[i].ContainsKey("Raw")) //{ // Console.WriteLine("Raw : " + tgParser.ParsedData[i]["Raw"]); // text += tgParser.ParsedData[i]["Raw"]; // Stream stream = File.Open("E:\Raw.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); // StreamWriter sw = new StreamWriter(stream, Encoding.Default); // sw.WriteLine(tgParser.ParsedData[i]["Raw"]); // sw.Flush(); // sw.Close(); //} //if (tgParser.ParsedData[i].ContainsKey("EegPowerDelta")) //{ // Console.WriteLine("EegPowerDelta : " + tgParser.ParsedData[i]["EegPowerDelta"]); // text += tgParser.ParsedData[i]["EegPowerDelta"]; // Stream stream = File.Open("E:\EegPowerDelta.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); // StreamWriter sw = new StreamWriter(stream, Encoding.Default); // sw.WriteLine(tgParser.ParsedData[i]["EegPowerDelta"]); // sw.Flush(); // sw.Close(); //} //if (tgParser.ParsedData[i].ContainsKey("EegPowerTheta")) //{ // Console.WriteLine("EegPowerTheta : " + tgParser.ParsedData[i]["EegPowerTheta"]); // text += tgParser.ParsedData[i]["EegPowerTheta"]; // Stream stream = File.Open("E:\EegPowerTheta.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); // StreamWriter sw = new StreamWriter(stream, Encoding.Default); // sw.WriteLine(tgParser.ParsedData[i]["EegPowerTheta"]); // sw.Flush(); // sw.Close(); //} //if (tgParser.ParsedData[i].ContainsKey("EegPowerAlpha2")) //{ // Console.WriteLine("EegPowerAlpha2 : " + tgParser.ParsedData[i]["EegPowerAlpha2"]); // text += tgParser.ParsedData[i]["EegPowerAlpha2"]; // Stream stream = File.Open("E:\EegPowerAlpha2.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); // StreamWriter sw = new StreamWriter(stream, Encoding.Default); // sw.WriteLine(tgParser.ParsedData[i]["EegPowerAlpha2"]); // sw.Flush(); // sw.Close(); //} //if (tgParser.ParsedData[i].ContainsKey("EegPowerAlpha1")) //{ // Console.WriteLine("EegPowerAlpha1 : " + tgParser.ParsedData[i]["EegPowerAlpha1"]); // text += tgParser.ParsedData[i]["EegPowerAlpha1"]; // Stream stream = File.Open("E:\EegPowerAlpha1.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); // StreamWriter sw = new StreamWriter(stream, Encoding.Default); // sw.WriteLine(tgParser.ParsedData[i]["EegPowerAlpha1"]); // sw.Flush(); // sw.Close(); //} //if (tgParser.ParsedData[i].ContainsKey("EegPowerBeta1")) //{ // Console.WriteLine("EegPowerBeta1 : " + tgParser.ParsedData[i]["EegPowerBeta1"]); // text += tgParser.ParsedData[i]["EegPowerBeta1"]; // Stream stream = File.Open("E:\EegPowerBeta1.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); // StreamWriter sw = new StreamWriter(stream, Encoding.Default); // sw.WriteLine(tgParser.ParsedData[i]["EegPowerBeta1"]); // sw.Flush(); // sw.Close(); //} //if (tgParser.ParsedData[i].ContainsKey("EegPowerBeta2")) //{ // Console.WriteLine("EegPowerBeta2 : " + tgParser.ParsedData[i]["EegPowerBeta2"]); // text += tgParser.ParsedData[i]["EegPowerBeta2"]; // Stream stream = File.Open("E:\EegPowerBeta2.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); // StreamWriter sw = new StreamWriter(stream, Encoding.Default); // sw.WriteLine(tgParser.ParsedData[i]["EegPowerBeta2"]); // sw.Flush(); // sw.Close(); //} //if (tgParser.ParsedData[i].ContainsKey("EegPowerGamma1")) //{ // Console.WriteLine("EegPowerGamma1 : " + tgParser.ParsedData[i]["EegPowerGamma1"]); // text += tgParser.ParsedData[i]["EegPowerGamma1"]; // Stream stream = File.Open("E:\EegPowerGamma1.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); // StreamWriter sw = new StreamWriter(stream, Encoding.Default); // sw.WriteLine(tgParser.ParsedData[i]["EegPowerGamma1"]); // sw.Flush(); // sw.Close(); //} //if (tgParser.ParsedData[i].ContainsKey("EegPowerGamma2")) //{ // Console.WriteLine("EegPowerGamma2 : " + tgParser.ParsedData[i]["EegPowerGamma2"]); // text += tgParser.ParsedData[i]["EegPowerGamma2"]; // Stream stream = File.Open("E:\EegPowerGamma2.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); // StreamWriter sw = new StreamWriter(stream, Encoding.Default); // sw.WriteLine(tgParser.ParsedData[i]["EegPowerGamma2"]); // sw.Flush(); // sw.Close(); //} if (tgParser.ParsedData[i].ContainsKey("Attention 1")) { if (tgParser.ParsedData[i]["Attention 1"] != 0) Console.WriteLine("Attention 1: " + tgParser.ParsedData[i]["Attention 1"]); } if (tgParser.ParsedData[i].ContainsKey("Attention 2")) { if (tgParser.ParsedData[i]["Attention 2"] != 0) Console.WriteLine("Attention 2: " + tgParser.ParsedData[i]["Attention 2"]); } if (tgParser.ParsedData[i].ContainsKey("Meditation")) { if (tgParser.ParsedData[i]["Meditation"] != 0) { Console.WriteLine("Meditation: " + tgParser.ParsedData[i]["Meditation"]); text += tgParser.ParsedData[i]["Meditation"]; Stream stream = File.Open("E:\项目文档\TestTxt1.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); StreamWriter sw = new StreamWriter(stream, Encoding.Default); sw.WriteLine(tgParser.ParsedData[i]["Meditation"]); sum2 += tgParser.ParsedData[i]["Meditation"]; count2++; sw.Flush(); sw.Close(); } } if (tgParser.ParsedData[i].ContainsKey("Meditation 1")) { if (tgParser.ParsedData[i]["Meditation 1"] != 0) Console.WriteLine("Meditation 1: " + tgParser.ParsedData[i]["Meditation 1"]); } if (tgParser.ParsedData[i].ContainsKey("Meditation 2")) { if (tgParser.ParsedData[i]["Meditation 2"] != 0) Console.WriteLine("Meditation 2: " + tgParser.ParsedData[i]["Meditation 2"]); } if (!golfZoneDemo) // turn this off for the Golf Zone Demo { if (tgParser.ParsedData[i].ContainsKey("BlinkStrength")) { Console.WriteLine(" BlinkStrength: " + tgParser.ParsedData[i]["BlinkStrength"]); } if (tgParser.ParsedData[i].ContainsKey("MentalEffort")) { mental_eff_cur = (Double)tgParser.ParsedData[i]["MentalEffort"]; if (mental_eff_first) { mental_eff_first = false; } else { /* * calculate the percentage change from the previous sample */ mental_eff_change = calcPercentChange(mental_eff_baseline, mental_eff_cur); if (mental_eff_change > 500.0 || mental_eff_change < -500.0) { Console.WriteLine(" Mental Effort: excessive range"); } else { Console.WriteLine(" Mental Effort: " + mental_eff_change + " %"); } } mental_eff_baseline = mental_eff_cur; } if (tgParser.ParsedData[i].ContainsKey("TaskFamiliarity")) { task_famil_cur = (Double)tgParser.ParsedData[i]["TaskFamiliarity"]; if (task_famil_first) { task_famil_first = false; } else { /* * calculate the percentage change from the previous sample */ task_famil_change = calcPercentChange(task_famil_baseline, task_famil_cur); if (task_famil_change > 500.0 || task_famil_change < -500.0) { Console.WriteLine(" Task Familiarity: excessive range"); } else { Console.WriteLine(" Task Familiarity: " + task_famil_change + " %"); } } task_famil_baseline = task_famil_cur; } if (tgParser.ParsedData[i].ContainsKey("Positivity")) { Console.WriteLine(" Positivity: " + tgParser.ParsedData[i]["Positivity"]); } } if (golfZoneDemo) { if (tgParser.ParsedData[i].ContainsKey("ReadyZone")) { Console.Write(" GolfZone: "); if (tgParser.ParsedData[i]["ReadyZone"] == 3) { Console.WriteLine("Elite: you are the best, putt when you are ready"); } else if (tgParser.ParsedData[i]["ReadyZone"] == 2) { Console.WriteLine("Intermediate: you are good, relax and putt smoothly"); } else if (tgParser.ParsedData[i]["ReadyZone"] == 1) { Console.WriteLine("Beginner: take a breath and don't rush"); } else { Console.WriteLine("Try to relax, focus on your target"); } } } } } private void MainWindow_Closing(object sender, CancelEventArgs e) { StreamWriter sw = new StreamWriter("E:\physicalSignal.txt"); sw.WriteLine("Attention的均值"+sum1/count1+","+ "Meditation的均值" + sum2 / count2); } static double calcPercentChange(double baseline, double current) { double change; if (baseline == 0.0) baseline = 1.0; //don't allow divide by zero /* * calculate the percentage change */ change = current - baseline; change = (change / baseline) * 1000.0 + 0.5; change = Math.Floor(change) / 10.0; return (change); } } }