diff --git a/java_package/brainflow/src/main/java/brainflow/DataFilter.java b/java_package/brainflow/src/main/java/brainflow/DataFilter.java index e337dda18..1a580b40c 100644 --- a/java_package/brainflow/src/main/java/brainflow/DataFilter.java +++ b/java_package/brainflow/src/main/java/brainflow/DataFilter.java @@ -1040,6 +1040,41 @@ public static double get_band_power (Pair psd, double freq_s return res[0]; } + /** + * (re)reference channels + * + * To eliminate ground-related noise, an online reference is chosen out of the recorded EEG channels. + * This reference is used as “baseline” for all remaining EEG channels. + * This means the signal at the other EEG channels is expressed as the difference from this reference. + * + * Thie method averages data from reference_channels and then subtract the averaged data from channels_to_reference + * + * @param data data to process + * @param channels_to_reference channels to be referenced or expressed as difference from reference_channels averaged data + * @param reference_channels channels to use as reference or baseline + * @return void + */ + public static void reference (double[][] data, ArrayList channels_to_reference, ArrayList reference_channels) + throws BrainFlowError + { + // Calculate the average data points of reference_channels + double[] avg_reference = new double[data[0].length]; + for (int channel : reference_channels) { + for (int i = 0; i < data[channel].length; i++) { + avg_reference[i] += data[channel][i]; + } + } + for (int i = 0; i < avg_reference.length; i++) { + avg_reference[i] /= reference_channels.size(); + } + // Subtract the average of reference_channels from each channel in channels_to_reference + for (int channel : channels_to_reference) { + for (int i = 0; i < data[channel].length; i++) { + data[channel][i] -= avg_reference[i]; + } + } + } + /** * calculate nearest power of two */