Collecting Continuous Alpha/Beta Data.

CartevCartev America
edited January 4 in Ganglion

I am only asking about this here because the folks on the Brainflow forum could not assist me in this problem. I'm trying to use Alpha/Beta ratio to control a musical algorithm real-time. I need to receive continuous ratio values, so I tried looping the one of the Band Power samples on the Brainflow guide.

import time
from brainflow.board_shim import BoardShim, BrainFlowInputParams, LogLevels, BoardIds
from brainflow.data_filter import DataFilter, WindowOperations, DetrendOperations
def main():

    params = BrainFlowInputParams()
    board_id = BoardIds.GANGLION_BOARD.value
    board_descr = BoardShim.get_board_descr(board_id)
    params.serial_port = "COM7"
    sampling_rate = int(board_descr['sampling_rate'])

        board = BoardShim(board_id, params)

        while True:
            BoardShim.log_message(LogLevels.LEVEL_INFO.value, 'start sleeping in the main thread')
            # Fetch data from the board
            data = board.get_current_board_data(num_samples=512) 

            eeg_channels = board.get_eeg_channels(board_id)
            eeg_channel = eeg_channels[1]

            nfft = min(DataFilter.get_nearest_power_of_two(len(data[eeg_channel])), len(data[eeg_channel]))

            DataFilter.detrend(data[eeg_channel], DetrendOperations.LINEAR.value)
            psd = DataFilter.get_psd_welch(data[eeg_channel], nfft, nfft // 2, sampling_rate,

            # Calculate the band power for alpha (7.0-13.0 Hz) and beta (14.0-30.0 Hz) frequency ranges
            freq_start_alpha, freq_end_alpha = 7.0, 13.0
            freq_start_beta, freq_end_beta = 14.0, 30.0

            band_power_alpha = DataFilter.get_band_power(psd, freq_start_alpha, freq_end_alpha)
            band_power_beta = DataFilter.get_band_power(psd, freq_start_beta, freq_end_beta)

            # Print or log the ratio
            ratio = band_power_alpha / band_power_beta
            print("alpha/beta ratio:", ratio)

    except Exception as e:
        print(f"Error: {e}")
        # Stop the stream and release the session when done
        if 'board' in locals() and board.is_prepared():

if __name__ == "__main__":

Here is the error I am getting. There is no other info in the log:

[2024-01-03 12:42:36.177] [board_logger] [debug] start streaming
[2024-01-03 12:42:36.178] [board_logger] [info] start sleeping in the main thread
[2024-01-03 12:42:38.181] [data_logger] [error] Please review your arguments.
Error: INVALID_ARGUMENTS_ERROR:13 unable to calc psd welch

My thought is that the sleep value (2) is not long enough for the program to calculate the needed values. It works when I make the sleep value 5 seconds. Is there any way I could get it to spit out values quicker?


  • retiututretiutut Louisiana, USA

    I would continue working with Andrey and exploring how to make this work on BrainFlow Slack. This is a very specific question about BrainFlow, and he is the creator. Patience, resilience, and hard work will always end in a good result!

Sign In or Register to comment.