Usage#

This guide demonstrates how to connect to a BrainAccess device and acquire EEG data from MATLAB using the public ba_* functions.

Quickstart: Data Acquisition with MATLAB Wrappers#

The typical MATLAB workflow involves these steps:

  1. Initialize the runtime: Call ba_setup() and ba_init().

  2. Discover or select a device: Use ba_scan() and choose a Bluetooth device name.

  3. Configure EEG: Call ba_configure_eeg(...) with the desired channels and sample rate.

  4. Start acquisition: Call ba_start_stream().

  5. Retrieve data: Use ba_read_chunk(...) or ba_collect_seconds(...) to pull data into MATLAB arrays.

  6. Stop acquisition: Call ba_stop_stream(), then disconnect and close.

The stream configuration (enabled channels, gain, bias electrodes and sample rate) applies to the next stream only. The device resets it when a stream stops or the device disconnects, so configure it again before every ba_start_stream(). Every stream starts with all electrodes enabled at gain 8 and 250 Hz; ba_configure_eeg(...) disables the electrodes you leave out of channel_ids, and with ba_set_channel_enabled you disable them yourself. Chunks still unread when ba_stop_stream() returns, including the stream’s last, shorter chunk, stay readable with ba_read_chunk(...) until the next start or disconnect.

Here is the recommended minimal acquisition flow:

ba_setup();
ba_init();

devices = ba_scan();
device_name = devices(1).name;

ba_connect(device_name);
features = ba_get_device_features();
channel_ids = 1:features.electrode_count;  % electrode i is channel ID 1 + i
ba_configure_eeg(channel_ids, 250, 8, []);
ba_start_stream();

[data, meta] = ba_collect_seconds(5.0, 250, true);

ba_stop_stream();
ba_disconnect();
ba_close();

plot(data');

Continuous Data Acquisition with Annotations#

This mirrors the Python acquisition example that starts a stream, sends annotations while the stream is running, then saves and plots the collected data. In MATLAB the session is typically saved to a .mat file.

ba_setup();
ba_init();

devices = ba_scan();
device_name = devices(1).name;

ba_connect(device_name);
features = ba_get_device_features();
channel_ids = 1:features.electrode_count;  % electrode i is channel ID 1 + i
ba_configure_eeg(channel_ids, 250, 8, []);
ba_start_stream();

pause(5.0);

all_data = [];
for annotation = 1:10
    pause(1.0);
    ba_annotate(num2str(annotation));

    [chunk, meta] = ba_read_chunk(1000, true);
    if meta.available && ~isempty(chunk)
        all_data = [all_data, chunk];
    end
end

annotations = ba_get_annotations();

ba_stop_stream();
ba_disconnect();
ba_close();

save("brainaccess-session.mat", "all_data", "annotations");
plot(all_data');

Minimal Data Acquisition#

This is the MATLAB equivalent of the low-level Python core acquisition example. It keeps the flow explicit: scan, connect, configure, stream, read, stop.

ba_setup();
ba_init();

devices = ba_scan();
device_name = devices(1).name;

ba_connect(device_name);
features = ba_get_device_features();
channel_ids = 1:features.electrode_count;  % electrode i is channel ID 1 + i
ba_configure_eeg(channel_ids, 250, 8, []);
ba_start_stream();

pause(2.0);
[chunk, meta] = ba_read_chunk(1000, true);

ba_stop_stream();
ba_disconnect();
ba_close();

disp(meta);
plot(chunk');

Impedance Measurement#

Check electrode contact before a recording. In an impedance mode the electrode rows carry each electrode’s impedance in kOhm instead of voltage. Select a bias electrode for a reliable measurement:

ba_connect(device_name);
features = ba_get_device_features();
ba_configure_eeg(1:features.electrode_count, 250, 8, 1);  % electrode 0 is bias
ba_set_impedance_mode('31.2hz');
ba_start_stream();

pause(3.0);
[impedance_kohm, meta] = ba_read_chunk(0, true);
disp(impedance_kohm(:, end));  % newest value per electrode

ba_stop_stream();  % also turns the impedance mode off

Library Settings and Logging#

The library writes a log file, bacore.log, into the current folder. Turn it off, or move it, with ba_set_config; the setting stays in force for the rest of the MATLAB session:

ba_setup();
ba_set_config('enable_logs', false);
% or: ba_set_config('log_path', fullfile(tempdir, 'bacore.log'));

Full Example Files#

Runnable example files that implement these flows:

API Reference#

For detailed information on all public MATLAB entry points, refer to the API Reference reference.