static void Main(string[] args) { Console.WriteLine("EEG Data Reader Example"); EEG_Logger p = new EEG_Logger(); for (int i = 0; i < 1000; i++) { p.Run(); Thread.Sleep(10); } }
static void Main(string[] args) { Console.WriteLine("EEG Data Reader Example"); EEG_Logger p = new EEG_Logger(); for (int i = 0; i < 5000; i++) { //Example for set marker to data stream //if (i % 37 == 0) //{ // p.engine.DataSetMarker((uint)p.userID, i); //} p.Run(); Thread.Sleep(10); } }
static void Main(string[] args) { Console.WriteLine("EEG Data Reader Example"); EEG_Logger p = new EEG_Logger(); while (true) { if (Console.KeyAvailable) { break; } //Example for set marker to data stream and set sychronization signal //if (i % 37 == 0) //{ // p.engine.DataSetMarker((uint)p.userID, i); // p.engine.DataSetSychronizationSignal((uint)p.userID, i); //} p.Run(); Thread.Sleep(10); } }
static void Main(string[] args) { Console.WriteLine("EEG Data Reader Example"); EEG_Logger p = new EEG_Logger(); EEG_Data_Analyzer f3_analyzer = new EEG_Data_Analyzer(); EEG_Data_Analyzer f4_analyzer = new EEG_Data_Analyzer(); List <double> p300_List = new List <double>(); int money = 0; int aaaa = 0; string[] whatPartS = { null }; string[] whatImgS = { null }; string[] gra = new String[61]; string[] s = new string[] { "0", "0" }; //FileStream fs = new FileStream(@"C:\Users\sohyeon\Desktop\불러오기.txt",fi) int stimuli_img_count = 0; int stimuli_video_count = 0; int i = 1; while (true) { p.Run(); if (File.Exists(@"C:\Users\sohyeon\Desktop\불러오기.txt")) //텍스트값받아오기(읽어오는것) { try { s = File.ReadAllLines(@"C:\Users\sohyeon\Desktop\불러오기.txt"); } catch (Exception e) { //s = File.ReadAllLines(@"C:\Users\sohyeon\Desktop\불러오기.txt"); } } if (s[0].Equals("1") && aaaa == 0) { p.is_Reference = true; p.f3_Raw.Clear(); //list empty p.f4_Raw.Clear(); aaaa++; } // 1초경과, 데이터 128, 분석, 데이터 모으기 시작 // 이미지 프로세스 if (s[0].Equals("2") && !p.is_Reference && stimuli_img_count < 16) //p300 측정+이미지 { if (p.f3_Raw.Count >= 128 && p.f4_Raw.Count >= 128) { //count++; var f3_p300 = f3_analyzer.GetP300(p.f3_Raw); var f4_p300 = f4_analyzer.GetP300(p.f4_Raw); var avg_stimuli = (f3_p300 + f4_p300) / 2; p.picture_peak_data[stimuli_img_count] = avg_stimuli; p.f3_Raw.Clear(); p.f4_Raw.Clear(); // 결과배열 => p300 저장 Console.WriteLine($"ref_avg: {p.avg_reference}"); Console.WriteLine($"stimuli_avg: {avg_stimuli}"); stimuli_img_count++; if (stimuli_img_count == 16) { double max_val = p.picture_peak_data[0]; var max_idx = 0; for (int a = 1; a < 16; a++) { if (p.picture_peak_data[a] > max_val) { max_val = p.picture_peak_data[a]; max_idx = a; } } Console.WriteLine($"Catagory: {max_idx}"); whatImgS[0] = max_idx.ToString(); File.AppendAllLines(@"C:\Users\sohyeon\Desktop\내보내기.txt", whatImgS); } } } else if (s[0].Equals("3") && !p.is_Reference) //p300 측정+동영상재생 { if (p.f3_Raw.Count >= 128 && p.f4_Raw.Count >= 128 && stimuli_video_count < 60) { if (stimuli_video_count == 0) { if (File.Exists(@"C:\Users\sohyeon\Desktop\내보내기.txt")) { File.Delete(@"C:\Users\sohyeon\Desktop\내보내기.txt"); } } var f3_p300 = f3_analyzer.GetP300(p.f3_Raw); var f4_p300 = f4_analyzer.GetP300(p.f4_Raw); var avg_stimuli = (f3_p300 + f4_p300) / 2; gra[0] = p.avg_reference.ToString(); if (p.avg_reference < avg_stimuli) { money++; p.video_peak_data[stimuli_video_count] = 1; } gra[i] = avg_stimuli.ToString(); Console.WriteLine("\n레퍼런스는" + gra[0]); Console.WriteLine("자극" + gra[i]); i++; p.f3_Raw.Clear(); p.f4_Raw.Clear(); // 결과배열 => p300 저장 Console.WriteLine($"ref_avg: {p.avg_reference}"); Console.WriteLine($"stimuli_avg: {avg_stimuli}"); Console.WriteLine($"Money: {money}"); Console.WriteLine($"video_count: {stimuli_video_count}"); stimuli_video_count++; if (stimuli_video_count == 60) { int whatPart = 0; if (s[1].Equals("4") || s[1].Equals("5") || s[1].Equals("6") || s[1].Equals("7") || s[1].Equals("8") || s[1].Equals("9")) { //whatPart = SelectM(p.video_peak_data, s[1]); whatPart = SelectM1(p.video_peak_data, s[1]); } whatPartS[0] = whatPart.ToString(); File.AppendAllLines(@"C:\Users\sohyeon\Desktop\내보내기.txt", whatPartS); whatImgS[0] = money.ToString(); File.AppendAllLines(@"C:\Users\sohyeon\Desktop\내보내기.txt", whatImgS); File.AppendAllLines(@"C:\Users\sohyeon\Desktop\그래프.txt", gra); //위치바꾸고 sss에 일단 레퍼런스넣었고 이제 p300 60개 넣어야해 } } } Thread.Sleep(10); } }
static void Main(string[] args) { //Console.WriteLine("EEG Data Reader Example"); double p300st = 0.11485094; //p300 기준 double npst1 = 0.436268; //np 기준(긍) double npst2 = 0.434627; //np기중 (부) double p300res = 0; //p300 퍼센트 결과 double npres = 0; //np 퍼센트 결과 double end; //전체 퍼센트 string source = DateTime.Now.ToString("dd HH-mm-ss"); string ch = "AF3, F7, F3, FC5, T7, P7, O1, O2, P8,T8, FC6, F4, F8, AF4"; string leftch = "AF3, F7, F3, FC5, T7, P7, O1"; string rightch = "O2, P8, T8, FC6, F4, F8, AF4"; //p300 변화량 파일 만들기 string pfilename = "p300 " + source + ".csv"; TextWriter p300file = new StreamWriter(pfilename, false); p300file.WriteLine(ch); //긍부정 파일만들기 //string Lefilename = "Left " + source + ".csv"; //TextWriter Lefile = new StreamWriter(Lefilename, false); //Lefile.WriteLine(leftch); // // //string Rifilename = " Right " + source + ".csv"; //TextWriter Rifile = new StreamWriter(Rifilename, false); //Rifile.WriteLine(rightch); //p300 확인 로우 데이터 string p300rawname = " rawP300 " + source + ".csv"; TextWriter p300raw = new StreamWriter(p300rawname, false); p300raw.WriteLine(ch); //알파 확인 string alpha = " alpha " + source + ".csv"; TextWriter alphaa = new StreamWriter(alpha, false); alphaa.WriteLine(ch); //베타 확인 string beta = " beta " + source + ".csv"; TextWriter betaa = new StreamWriter(beta, false); betaa.WriteLine(ch); string fftle = " fftleft " + source + ".csv"; TextWriter fftleft = new StreamWriter(fftle, false); string fftri = " fftright " + source + ".csv"; TextWriter fftright = new StreamWriter(fftri, false); EEG_Logger p = new EEG_Logger(); EEG_Data_Analysis ana = new EEG_Data_Analysis(); double[,] rawEEGdata; double[,] pp300 = new double[14, 60]; int cnt_pp = 0; double[] sum_pp = new double[14] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; double[] avg_pp = new double[14]; double[,] diff_p = new double[14, 30]; double[] sum_diff = new double[14] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; double[] p300 = new double[14]; double[] ch_eeg; //3초 동안의 rawData double[,] left_ratio = new double[28, 7]; //좌반구 활성화 double[,] right_ratio = new double[28, 7]; //우반구 활성화 int fftTime = 0; //활성화도 30초 int npnum = 0; //긍부정도 텍스트파일 double sumRatio = 0; double avgRatio; double maxP300 = -100; int maxP300time = 0; double maxNp = -100; int maxNptime = 0; FFT fft = new FFT(); while (true) { if (Console.KeyAvailable) { break; } p.Run(); //p300 분석 if (p.rawDataIdx == 128) { rawEEGdata = new double[14, 128]; for (int i = 0; i < 14; i++) { for (int j = 0; j < 128; j++) { rawEEGdata[i, j] = p.ds.series[i + 3, j]; if (cnt_pp > 29) { p.rawDict[i].Add(rawEEGdata[i, j]); } } } //p300 rawdata 엑셀로 뽑기 for (int i = 0; i < 128; i++) { for (int j = 0; j < 14; j++) { p300raw.Write(rawEEGdata[j, i] + ","); } p300raw.WriteLine(""); } if (cnt_pp > 29) //광고 시작후 eeg 사이즈 { p.wsize++; } double[] arr = new double[128]; for (int i = 0; i < 14; i++) { for (int j = 0; j < 128; j++) { arr[j] = rawEEGdata[i, j]; } pp300[i, cnt_pp] = ana.GetP300(arr); } if (cnt_pp == 29) //중립뇌파 각 채널별 평균 { for (int i = 0; i < 14; i++) { for (int j = 0; j < 30; j++) { sum_pp[i] += pp300[i, j]; } avg_pp[i] = sum_pp[i] / 30; } } else if (cnt_pp > 29) //광고 뇌파 { int num; for (int i = 0; i < 14; i++) { //변화량 diff_p배열 diff_p[i, cnt_pp - 30] = ((avg_pp[i] - pp300[i, cnt_pp]) / avg_pp[i]) * 100; sum_diff[i] += diff_p[i, cnt_pp - 30]; p300file.Write(diff_p[i, cnt_pp - 30] + ","); num = cnt_pp - 30; // p300 Graph if (i == 3)//특정 채널만 txt파일로 1초에 하나씩 생성 { p.wr = new StreamWriter(@"C:\Users\sohyeon\Desktop\p300\" + num + ".txt"); p.wr.WriteLine(diff_p[i, cnt_pp - 30]); p.wr.Close(); //p300이 최고인 지점 if (diff_p[i, cnt_pp - 30] > maxP300) { maxP300 = diff_p[i, cnt_pp - 30]; maxP300time = cnt_pp - 30; } if (diff_p[i, cnt_pp - 30] >= p300st) { p300res += 4; } } } p300file.WriteLine(""); } if (cnt_pp == 59)//광고 종료 { p300file.WriteLine(""); for (int i = 0; i < 14; i++) { p300[i] = sum_diff[i] / 30; p300file.Write(p300[i] + ","); } p300file.Close(); } cnt_pp++; p.rawDataIdx = 0; } //긍부정 분석 if (p.wsize == 5) { ch_eeg = new double[128 * 4]; for (int i = 0; i < 14; i++) { double asum = 0, bsum = 0; double a, b; //여기에 알파, 베타 Array.Copy(p.rawDict[i].ToArray(), 0, ch_eeg, 0, 128 * 3); p.rawDict[i].RemoveRange(0, 128); //1초 data삭제 var fftArray = fft.Excute(ch_eeg); //fft를 한 값을 받는 배열이 필요 // Console.WriteLine(fftArray.Length); var df = fft.GetDF(5); //fftArray 배열에서 알파, 베타 뽑->비율값 for (int j = Convert.ToInt32(8 / df); j < Convert.ToInt32(12 / df); j++) { asum += fftArray[j]; } for (int j = Convert.ToInt32(21 / df); j < Convert.ToInt32(30 / df); j++) { bsum += fftArray[j]; } a = asum / (Convert.ToInt32(12 / df) - Convert.ToInt32(8 / df)); b = bsum / (Convert.ToInt32(30 / df) - Convert.ToInt32(21 / df)); alphaa.Write(a + ","); betaa.Write(b + ","); double fftRatio = b / a; //fftraw data 값 받기 for (int j = 0; j < 512; j++) { if (i >= 0 && i < 7) { fftleft.Write(fftArray[j] + ","); } if (i >= 7 && i < 14) { fftright.Write(fftArray[j] + ","); } } fftleft.WriteLine(""); fftright.WriteLine(""); if (i == 1 || i == 4 || i == 9) { sumRatio += fftRatio; } //if (i >= 0 && i < 7) //{ // left_ratio[fftTime, i] = fftRatio; //} //else if (i >= 7 && i < 14) //{ // right_ratio[fftTime, i - 7] = fftRatio; //} } avgRatio = sumRatio / 3; sumRatio = 0; p.wr2 = new StreamWriter(@"C:\Users\sohyeon\Desktop\ne_po\" + npnum + ".txt"); p.wr2.WriteLine(avgRatio); p.wr2.Close(); if (avgRatio > maxNp) { maxNp = avgRatio; maxNptime = npnum; } npnum++; if (avgRatio >= npst1) { npres += 3.8; } else if (avgRatio < npst1 && avgRatio > 2) { npres += 2; } else { npres += 1; } alphaa.WriteLine(""); betaa.WriteLine(""); // for (int i = 0; i < 7; i++) // { // Lefile.Write(left_ratio[fftTime, i] + ","); // Rifile.Write(right_ratio[fftTime, i] + ","); // // if (i == 2) //긍부정 텍스트 파일 // { // p.wr2 = new StreamWriter(@"C:\Users\user\Desktop\ne_po\" + npnum + ".txt"); // p.wr2.WriteLine(left_ratio[fftTime, i]); // p.wr2.Close(); // npnum++; // } // } // Lefile.WriteLine(""); // Rifile.WriteLine(""); fftTime++; if (cnt_pp == 60) { // Lefile.Close(); // Rifile.Close(); fftleft.Close(); fftright.Close(); p.wr = new StreamWriter(@"C:\Users\sohyeon\Desktop\p300\p300res.txt"); p.wr.WriteLine(p300res); p.wr.Close(); //최대 p300 시간 저장 p.wr = new StreamWriter(@"C:\Users\sohyeon\Desktop\p300\maxP300time.txt"); p.wr.WriteLine(maxP300time + "~" + (maxP300time + 1)); p.wr.Close(); p.wr2 = new StreamWriter(@"C:\Users\sohyeon\Desktop\ne_po\npres.txt"); p.wr2.WriteLine(npres); p.wr2.Close(); //최대 긍부정 시간 저장 p.wr2 = new StreamWriter(@"C:\Users\sohyeon\Desktop\ne_po\maxNptime.txt"); p.wr2.WriteLine(maxNptime + "~" + (maxNptime + 3)); p.wr2.Close(); p.wr2 = new StreamWriter(@"C:\Users\sohyeon\Desktop\ne_po\end.txt"); p.wr2.WriteLine((p300res * 2 + npres * 3) / 5); p.wr2.Close(); break; } p.wsize = 4; } Thread.Sleep(10); } StreamWriter srtWriter = new StreamWriter("C:\\Users\\sohyeon\\Desktop\\start.txt"); srtWriter.Write(""); srtWriter.Close(); }