@@ -581,14 +581,7 @@ public static void write_file (double[,] data, string file_name, string file_mod
581
581
throw new BrainFlowError ( res ) ;
582
582
}
583
583
584
- double [ , ] result = new double [ num_rows [ 0 ] , num_cols [ 0 ] ] ;
585
- for ( int i = 0 ; i < num_rows [ 0 ] ; i ++ )
586
- {
587
- for ( int j = 0 ; j < num_cols [ 0 ] ; j ++ )
588
- {
589
- result [ i , j ] = data_arr [ i * num_cols [ 0 ] + j ] ;
590
- }
591
- }
584
+ double [ , ] result = data_arr . Reshape ( num_rows [ 0 ] , num_cols [ 0 ] ) ;
592
585
return result ;
593
586
}
594
587
@@ -643,6 +636,62 @@ public static Tuple<double[], double[]> get_custom_band_powers (double[,] data,
643
636
return return_data ;
644
637
}
645
638
639
+ /// <summary>
640
+ /// Calculate ICA
641
+ /// </summary>
642
+ /// <param name="data"></param>
643
+ /// <param name="num_components"></param>
644
+ /// <returns></returns>
645
+ public static Tuple < double [ , ] , double [ , ] , double [ , ] , double [ , ] > perform_ica ( double [ , ] data , int num_components )
646
+ {
647
+ if ( data == null )
648
+ {
649
+ throw new BrainFlowError ( ( int ) BrainFlowExitCodes . INVALID_ARGUMENTS_ERROR ) ;
650
+ }
651
+ int [ ] channels = new int [ data . GetLength ( 0 ) ] ;
652
+ for ( int i = 0 ; i < channels . Length ; i ++ )
653
+ channels [ i ] = i ;
654
+ return perform_ica ( data , num_components , channels ) ;
655
+ }
656
+
657
+ /// <summary>
658
+ /// Calculate ICA
659
+ /// </summary>
660
+ /// <param name="data"></param>
661
+ /// <param name="num_components"></param>
662
+ /// <param name="channels"></param>
663
+ /// <returns></returns>
664
+ public static Tuple < double [ , ] , double [ , ] , double [ , ] , double [ , ] > perform_ica ( double [ , ] data , int num_components , int [ ] channels )
665
+ {
666
+ if ( ( num_components < 1 ) || ( data == null ) || ( channels == null ) )
667
+ {
668
+ throw new BrainFlowError ( ( int ) BrainFlowExitCodes . INVALID_ARGUMENTS_ERROR ) ;
669
+ }
670
+ int cols = data . GetLength ( 1 ) ;
671
+ double [ ] data_1d = new double [ cols * channels . Length ] ;
672
+ for ( int i = 0 ; i < channels . Length ; i ++ )
673
+ {
674
+ Array . Copy ( data . GetRow ( channels [ i ] ) , 0 , data_1d , i * data . GetRow ( channels [ i ] ) . Length , data . GetRow ( channels [ i ] ) . Length ) ;
675
+ }
676
+ int channels_len = channels . Length ;
677
+ double [ ] w = new double [ num_components * num_components ] ;
678
+ double [ ] k = new double [ channels_len * num_components ] ;
679
+ double [ ] a = new double [ channels_len * num_components ] ;
680
+ double [ ] s = new double [ cols * num_components ] ;
681
+
682
+ int res = DataHandlerLibrary . perform_ica ( data_1d , channels_len , cols , num_components , w , k , a , s ) ;
683
+ if ( res != ( int ) BrainFlowExitCodes . STATUS_OK )
684
+ {
685
+ throw new BrainFlowError ( res ) ;
686
+ }
687
+ double [ , ] w_mat = w . Reshape ( num_components , num_components ) ;
688
+ double [ , ] k_mat = k . Reshape ( num_components , channels_len ) ;
689
+ double [ , ] a_mat = a . Reshape ( channels_len , num_components ) ;
690
+ double [ , ] s_mat = s . Reshape ( num_components , cols ) ;
691
+ Tuple < double [ , ] , double [ , ] , double [ , ] , double [ , ] > return_data = new Tuple < double [ , ] , double [ , ] , double [ , ] , double [ , ] > ( w_mat , k_mat , a_mat , s_mat ) ;
692
+ return return_data ;
693
+ }
694
+
646
695
/// <summary>
647
696
/// calculate avg and stddev bandpowers across channels
648
697
/// </summary>
0 commit comments