11using System ;
2+ using System . Collections . Generic ;
23using System . Threading ;
34using System . Windows . Forms ;
45using brainflow ;
@@ -12,13 +13,24 @@ public static class Device
1213 static bool initError ;
1314 static bool disposing ;
1415
16+ static Thread boardThread ;
17+
1518 static BoardShim boardShim ;
1619 static int [ ] eegChannels ;
17- static Thread boardThread ;
20+ static int samplingRate ;
1821
1922 static double [ ] eegValues ;
2023 static double [ ] eegTicks ;
2124 static bool [ ] clearValues ;
25+ static bool readMetrics = false ;
26+ static bool hasMetrics = false ;
27+ static double [ ] featureVector ;
28+
29+ static MLModel mindfulnessMlModel ;
30+ static BrainFlowModelParams mindfulnessModelParams ;
31+
32+ static MLModel restfulnessMlModel ;
33+ static BrainFlowModelParams restfulnessModelParams ;
2234
2335 public static EventHandler < EventArgs > Channel1Changed ;
2436 public static EventHandler < EventArgs > Channel2Changed ;
@@ -81,24 +93,35 @@ public static Boolean Initialize()
8193 return true ;
8294 }
8395
84- static void GetBoardData ( int board_id , BrainFlowInputParams input_params )
96+ static void GetBoardData ( int boardId , BrainFlowInputParams inputParams )
8597 {
86- boardShim = new BoardShim ( board_id , input_params ) ;
98+ boardShim = new BoardShim ( boardId , inputParams ) ;
8799
88100 boardShim . prepare_session ( ) ;
89101 boardShim . start_stream ( ) ;
90102
91- eegChannels = BoardShim . get_eeg_channels ( board_id ) ;
103+ samplingRate = BoardShim . get_sampling_rate ( boardId ) ;
104+ eegChannels = BoardShim . get_eeg_channels ( boardId ) ;
92105 eegValues = new double [ eegChannels . Length ] ;
93106 eegTicks = new double [ eegChannels . Length ] ;
94107 clearValues = new bool [ eegChannels . Length ] ;
95108
109+ mindfulnessModelParams = new BrainFlowModelParams ( ( int ) BrainFlowMetrics . MINDFULNESS , ( int ) BrainFlowClassifiers . DEFAULT_CLASSIFIER ) ;
110+ mindfulnessMlModel = new MLModel ( mindfulnessModelParams ) ;
111+ mindfulnessMlModel . prepare ( ) ;
112+
113+ restfulnessModelParams = new BrainFlowModelParams ( ( int ) BrainFlowMetrics . RESTFULNESS , ( int ) BrainFlowClassifiers . DEFAULT_CLASSIFIER ) ;
114+ restfulnessMlModel = new MLModel ( mindfulnessModelParams ) ;
115+ restfulnessMlModel . prepare ( ) ;
116+
117+ var bandData = new List < double [ , ] > ( ) ;
118+
96119 do
97120 {
98- double [ , ] unprocessed_data = boardShim . get_board_data ( 20 ) ;
121+ double [ , ] boardData = boardShim . get_board_data ( ) ;
99122 foreach ( var index in eegChannels )
100123 {
101- double [ ] rows = unprocessed_data . GetRow ( index ) ;
124+ double [ ] rows = boardData . GetRow ( index ) ;
102125
103126 foreach ( var row in rows )
104127 {
@@ -130,6 +153,31 @@ static void GetBoardData(int board_id, BrainFlowInputParams input_params)
130153 if ( index == 15 && Channel15Changed != null ) Channel15Changed ( string . Join ( "," , rows ) , null ) ;
131154 if ( index == 16 && Channel16Changed != null ) Channel16Changed ( string . Join ( "," , rows ) , null ) ;
132155 }
156+
157+ if ( readMetrics )
158+ {
159+ bandData . Add ( boardData ) ;
160+
161+ if ( bandData . Count > 512 )
162+ {
163+ bandData . RemoveAt ( 0 ) ;
164+
165+ double [ , ] data = new double [ 0 , 0 ] ;
166+
167+ foreach ( var d in bandData )
168+ {
169+ data = mergeArrays ( data , d , eegChannels . Length * 2 ) ;
170+ }
171+
172+ if ( data . Length > 16384 )
173+ {
174+ Tuple < double [ ] , double [ ] > bands = DataFilter . get_avg_band_powers ( data , eegChannels , samplingRate , true ) ;
175+ featureVector = bands . Item1 ;
176+ hasMetrics = true ;
177+ }
178+
179+ }
180+ }
133181 }
134182 while ( ! disposing ) ;
135183 }
@@ -154,5 +202,54 @@ public static double GetEEG(int index)
154202 clearValues [ index - 1 ] = true ;
155203 return average ;
156204 }
205+
206+ public static double GetMetric ( BrainFlowMetrics metric )
207+ {
208+ readMetrics = true ;
209+
210+ if ( ! initialized ) return 0 ;
211+ if ( ! hasMetrics ) return 0 ;
212+
213+ double prediction = 0 ;
214+
215+ if ( metric == BrainFlowMetrics . MINDFULNESS )
216+ {
217+ prediction = mindfulnessMlModel . predict ( featureVector ) [ 0 ] ;
218+ }
219+ else if ( metric == BrainFlowMetrics . RESTFULNESS )
220+ {
221+ prediction = restfulnessMlModel . predict ( featureVector ) [ 0 ] ;
222+ }
223+
224+ return prediction ;
225+ }
226+
227+ static double [ , ] mergeArrays ( double [ , ] data1 , double [ , ] data2 , int depth )
228+ {
229+ var data1Length = data1 . Length / depth ;
230+ var data2Length = data2 . Length / depth ;
231+
232+ double [ , ] data = new double [ depth , data1Length + data2Length ] ; ;
233+
234+ int i = 0 ;
235+ for ( ; i < depth ; i ++ )
236+ {
237+ for ( int j = 0 ; j < data1Length ; j ++ )
238+ {
239+ data [ i , j ] = data1 [ i , j ] ;
240+ }
241+ }
242+ i = 0 ;
243+ for ( ; i < depth ; i ++ )
244+ {
245+ for ( int j = 0 ; j < data2Length ; j ++ )
246+ {
247+ data [ i , data1Length + j ] = data2 [ i , j ] ;
248+ }
249+
250+ }
251+
252+ return data ;
253+ }
157254 }
158255}
0 commit comments