From 581c39a072928895729e1dfa463bf82a22f2c6e4 Mon Sep 17 00:00:00 2001 From: Kartikeya Kashiva Date: Sat, 27 May 2023 15:37:54 +0530 Subject: [PATCH 1/2] added method signature and implementation comments for reference method in DataFilter.java --- .../src/main/java/brainflow/DataFilter.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/java_package/brainflow/src/main/java/brainflow/DataFilter.java b/java_package/brainflow/src/main/java/brainflow/DataFilter.java index e337dda18..d0352614b 100644 --- a/java_package/brainflow/src/main/java/brainflow/DataFilter.java +++ b/java_package/brainflow/src/main/java/brainflow/DataFilter.java @@ -1040,6 +1040,27 @@ 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 use as reference + * @param reference_channels channels to reference + * @return void + */ + public static void reference (double[][] data, ArrayList channels_to_reference, ArrayList reference_channels) + throws BrainFlowError + { + // ToDo: first of all you need to average data from reference_channels and after that subtract the averaged data from channels_to_reference + throw new BrainFlowError ("Not implemented yet", BrainFlowExitCode.NOT_IMPLEMENTED_ERROR.get_code ()); + } + /** * calculate nearest power of two */ From 4953b7710617a3f06bc350baa640e803d66c8bba Mon Sep 17 00:00:00 2001 From: Kartikeya Kashiva Date: Sat, 27 May 2023 16:23:22 +0530 Subject: [PATCH 2/2] implemented the method reference in DataFilter.java --- .../src/main/java/brainflow/DataFilter.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/java_package/brainflow/src/main/java/brainflow/DataFilter.java b/java_package/brainflow/src/main/java/brainflow/DataFilter.java index d0352614b..1a580b40c 100644 --- a/java_package/brainflow/src/main/java/brainflow/DataFilter.java +++ b/java_package/brainflow/src/main/java/brainflow/DataFilter.java @@ -1050,15 +1050,29 @@ public static double get_band_power (Pair psd, double freq_s * 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 use as reference - * @param reference_channels channels to reference + * @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 { - // ToDo: first of all you need to average data from reference_channels and after that subtract the averaged data from channels_to_reference - throw new BrainFlowError ("Not implemented yet", BrainFlowExitCode.NOT_IMPLEMENTED_ERROR.get_code ()); + // 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]; + } + } } /**