How to create sliding window

5 vues (au cours des 30 derniers jours)
ocunc
ocunc le 26 Avr 2018
Commenté : Jan le 19 Juin 2018

I know this question was asked before, but I could not find a solution that suits my problem. The code should do the following:

Data is recorded and saved in a variable "data", which is a 3D array (e.g.: data = 27 x 15 x 74635). Here, I only need column 3 (c = 74635) from data. Each number (e.g. 1, 2, 3,...) in column 3 contains a 2D array.

The code should save each number (from column 3) in a single variable “package”.

It should continue saving each following number as a "package", until a predefined number of packages (which are set by variable “x”) is reached.

I was thinking to apply a sliding window approach, if it makes sense, but I have no idea how to do so. Since I am dealing with many data, I was thinking to preallocate the array. So here is what I got until now:

Data = zeros(size(Recording.data,3)); % preallocating the array to the size of my amount of data
for i = 1:size(Recording.data,3)
  Data(:,:,i = [Data,squeeze(Recording.data(:,:,i))];

How can I apply a sliding window "algorithm"/approach to my code?

  5 commentaires
ocunc
ocunc le 26 Avr 2018
Imagine you are doing EEG and recording brain activity with 32 electrodes that are distributed over your head. Each electrode measures a current (data) over some time. The measured data of the EEG are stored in a matrix called "data" with the following dimensions: data = 32 x 20 x 74635. Here, 32 are the electrodes (electrode 1, 2, 3,...32), 20 is the number of data frames per trial (matrix page), and 74635 is the number of trials (so the measured EEG data are marked/split/segmented in 74635 segments, like little time windows). The following names for the variables are random.
Let's take the first trial (first segment of 74635), which is data(:,:,1) and is a 32x20-matrix (so, data(:,:,1) shows the data values for each of the 32 channels and 20 frames per trial for the first trial). Data(:,:,1) is exactly the matrix that I want to save in a variable "w". Now there are 74634 more trials and thus, I want to save each trial in a separate (temporary) variable. If 5 (should actually be a predefined variable "x") trials are reached (so, data(:,:,1), data(:,:,2), data(:,:,3), data(:,:,4), data(:,:,5), then all five trials should be saved in another variable "y0". Then the code continues with the next 5 trials and saves them in a variable "y1", next five trials in "y2",... At the end, the code should collect five y's and save them in a variable "z". And "z" is then used for further analysis.
Now I was wondering how to do that? I think it is not efficient to save every 5 trials of the entire data set in variables, which is why I was thinking of a time window that always just saves the required amount of data that is necessary to do analysis with. So I would need a time window that saves each time 5 trials of the entire data set in a variable (from beginning to end) until a predefined number of required variables (e.g. 5) are reached, then these 5 variables should be saved in one variable to do further analysis with.
I hope that makes it more clear. I am thankful for every hint that I cant get!
Jan
Jan le 19 Juin 2018
@Norman Sinnigen: It is impolite to delete a question after answers have been given.

Connectez-vous pour commenter.

Réponse acceptée

Guillaume
Guillaume le 27 Avr 2018
Ok, now the inputs are very clear. And the answer to your question is: don't do it!
Here's a very simple rule: if you start numbering variables, you've gone wrong. This will always lead to complex code that is hard to debug and slow to execute. In your particular case, I don't evn understand why you want to create y0. You've already got a variable for it, it's data(:, :, 1:5) . So why not use that in subsequent code?
You could split data into a cell array
y = mat2cell(data, size(data, 1), size(data, 2), 5*ones(1, size(data, 3)/5)); %only works if the number of page is a multiple of 5
%hence yx would be y{x}
or reshape it into a 4D array
y = reshape(data, size(data, 1), size(data, 2), 5, []);
%hence yx would be y(:, :, :, x)
But you may as well keep the data as it is.
I'm not clear at all what the subsequent analysis is but whatever it is you don't need to create additional variables for it, and it will be simpler if you don't.
  2 commentaires
ocunc
ocunc le 28 Avr 2018
Modifié(e) : ocunc le 28 Avr 2018
The solution is way more simple than I thought. Thank you!
Regarding the time window I still think about an idea, like a framework on how to implement that in Matlab (sorry for being such a newbie!). I am struggling a bit with the implementation of a code that works during real-time recordings. So I try to explain it as well (with real numbers instead of variables):
Imagine there is an incoming data package from a real-time measurement (Data(:, :, 1) every 20ms. So every 20 ms an 63x20x1 array (data package) arrives.
The code should collect the first 25 data packages (Data(:, :, 1:25) and perform two parallel computations with these 25 data packages. Would it be wise to use a parfor-loop? Then it uses these results to compare them with the next incoming data packages (Data(:, :, 26), (Data(:, :, 27),...until 25 data packages (Data(:, :, 26:50) arrived. Then the code should perform two parallel computations with these new 25 data packages, and so on.
Any suggestions on how to set that up? I hope the required information are enough.
Guillaume
Guillaume le 30 Avr 2018
Note: I don't have the parallel toolbox nor have I ever used matlab for real time acquisition. I use other software for that.
So every 20 ms an 63x20x1 array (data package) arrives. Assuming it's double data (8 bytes/sample), that's about 504 kB/s. That's not a particularly high throughput but you can't still take too long to process it. Avoid data copies as much as possible.
perform two parallel computations with these 25 data packages. Would it be wise to use a parfor-loop If it's two parallel but independent computations, I wouldn't think so. I would just have two background workers that you call each time with whatever method the parallel toolbox gives you for that.
If your computations takes longer than 20 ms, then you won't be able to process your data real time. In that case, you'll have to implement a producer/consumer system where the real time portion of the code puts the received data in a queue, and the background workers fetch the data from the queue as they're ready.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur EEG/MEG/ECoG dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by