Extract & average time series over subintervals, and save to a single output

Hello Brainstorm experts,

I would like to get some help as I am new to Brainstorm.

I have a time series (MEG) and want to
i) extract every 50ms, as follows:
0~50, 25~75, 50~100, 125~175, ..... and then
ii) average each (across time, so essentially getting a 50ms average throughout the epoch).
Below is what I have now.

start = 0.0;
en = 0.05;
for ii=1:30
% Process: Extract time
sFiles = bst_process('CallProcess', 'process_extract_time', sFiles, [],...
'timewindow', [start, en], ...
'overwrite', 0);

% Process: Average time
sFiles = bst_process('CallProcess', 'process_average_time', sFiles, [],...
    'timewindow', [start, en], ...
    'isstd',      0, ...
    'overwrite',  1, ...
    'source_abs', 0);
start = start+0.025;
en = en+0.025;

end

This code seems to work except that it generates one file for every 50ms (i.e., 30 files). Instead, I want one single file that contains 30 values. (How) can I do that? Hope my question was clear. Thanks!

Every time you call process_average_time, it will create a new file, indeed.
It would be easier for you to write some Matlab code that computes directly what you need, either by editing an existing .mat file or creating a new one.

An intermediate solution would be to call the process "Pre-process > Run Matlab command" to execute your Matlab code:
https://neuroimage.usc.edu/brainstorm/Tutorials/Scripting#Custom_processing

Example for the first three 50ms blocks using the example tutorials dataset (you should write a for loop to generate all the lists of indices instead...):
image image

The rest of the Scripting tutorial can help you manipulating files and data structures without the interface.

FYI: the process "Frequency > Group in time or frequency bands" does what you want but only for time-frequency results.

1 Like

Thank you, Francois! Just a quick clarification. In the code, 1:30, 16:45, 21:60 <-- are these referring to the time range? I wonder they should instead be 1:51, 26:75, 51:100, given that I'm interested in 50ms average not 30ms? Or am I misunderstanding something?

These are data samples = columns in the data matrix.
The sampling rate is 600Hz for these files => 1 sample = 1/600 seconds ~ 0.0017s = 1.7ms
[1:30] samples = [1:30] / 600 => time selected = from 1.6667ms to 50ms
[16:45] samples = [16:45] / 600 => time selected = from 26.7ms to 75ms

1 Like