brainflow-dev / brainflow

BrainFlow is a library intended to obtain, parse and analyze EEG, EMG, ECG and other kinds of data from biosensors
https://brainflow.org/
MIT License
1.27k stars 327 forks source link

restore support for pieeg #729

Closed Andrey1994 closed 2 months ago

Andrey1994 commented 2 months ago

Recreate #723 from a local branch

Hi @nickgamb! Great work on this, I wanted to push some minor changes in code style fixes and also wanted to remove some extra stuff which remained from Android part. And to dont affect your master branch(also think I have no permissions for pushing there) recreated your PR frrom a local branch with some extra changes made by me

Andrey1994 commented 2 months ago

We need a final testing round on it, cannot be merged wo confirmation from somebody with device and I dont have one

nickgamb commented 2 months ago

eeg_data_brainflow_3.csv

Hi @Andrey1994 I have the board and can try but I notice the amplitude on the data when using ConsciousLabs ThinkPulse electrodes is low. I typically need change the gain when using these to 8 or more.

    uint8_t reg_address = 0x04 + channel;
    uint8_t reg_value = 0x04 | gain;

Other than that things seem to be working. I compiled this branch on a pi4 with a pieeg hat connected and used brainflow to acquire the data. I did not apply any filtering. When I have time this weekend I will increase the gain and recompile to see if that can improve the result with my setup.

Hope that helps some - if not perfect.

Andrey1994 commented 2 months ago

can you store all the data as in brainflow? without filtering for eeg channels. One of tests to run is to run it for 10 seconds and see how many rows there are in the file

Andrey1994 commented 2 months ago

I am not sure about gain, as far as I understand somehow its used here

            for (size_t i = 0; i < eeg_channels.size (); i++)
            {
                int offset = 3 * i + 3;
                uint32_t voltage = (buf[offset] << 8) | buf[offset + 1];
                voltage = (voltage << 8) | buf[offset + 2];
                uint32_t voltage_test = voltage | data_test;
                if (voltage_test == data_check)
                {
                    voltage = 16777214 - voltage;
                }

                package[eeg_channels[i]] = 0.27 * voltage;
            }

And assuming that everything is correct, if you change the gain you need to update these equations. In the end resulting voltage will be the same I think

Andrey1994 commented 2 months ago

Maybe @Ildaron knows how to update equations for a new gain, to be honest I stll dont get why this conversion is not the same as for openbci devices

nickgamb commented 2 months ago

It may not be required. @Ildaron has already looked and said it probably is not important. I do set the gain via register in pieeg (which is supported) when I switch acquisition mode in my app to not use BrainFlow. But we can ignore gain for now. Since your request seems to just be related to export format - let me pull some data again today for you that just exports the raw BrainFlow data. That will allow you to hopefully finish validation. I can fork/PR with some suggestions to make things like gain more configurable and you can consider if it is a fit. Might be more specific to my setup than others though. Give me a few hours to be a dad and then I will get to it. ✌️

Andrey1994 commented 2 months ago

we can add support for setting gain via config_board method, it may look like this:

board.config_board("{"channel_number": 1, "gain": 8}")

Other formats for it are also ok. But equations to calculate resulting voltage should be updated. BrainFlow returns lets call it "body" voltage and apparently this value should not be increased by multiple times when you change the gain. As I understand it gain is more like sensitivity setting

Andrey1994 commented 2 months ago

and config_board stuff can be done in a separated PR

nickgamb commented 2 months ago

Yep, agree. And yes it is a sensitivity thing. I have read that ThinkPulse electrodes benefit from a gain of 8 or more but I have noticed it adds more noise than anything else. We should definitely take it to a new PR. 👍. 

nickgamb commented 2 months ago

pieeg_brainflow_data.csv

Figure_1

Hi @Andrey1994 . This data was taken using brainflow_get_data.py using my headset sitting in a forest far away from most interference.

I think it is looking promising.

Andrey1994 commented 2 months ago

thanks! looks awesome, I will create new release with pieeg support