1

I'm a French student engineer in signal processing field and I do my internship in a neuroscience laboratory. I've to process a lot of data from the brain activity with the help of Matlab, so one of my main subject is to optimize the code. But now I'm stuck in a situation that I can't resolve and I don't find anything about it on the web. I explain my problem :

For example the matrix a :

a = [ 1 2 3 4 5;  6 7 8 9 10;11 12 13 14 15]

Each row is the datas of a signal (so here we have 3 signals), and I want, for each signal/row, cut the vector in blcoks interlinked of the same length.

For instance for the signal 1, S1 = [1 2 3 4 5] I want to extract the bloc S1_1 = [1 2 3], S1_2 = [2 3 4], and S1_3 = [3 4 5] and compute every sub-block.

My first idea was to use nested loop like that :

[nrow ncol] = size(a);

for i = 1 : nrow
   for j = 4 : ncol
      sub_block = a(i, (j-3):j);
      result(i, j-3) = compute(sub_block);
   end
end

BUT as I said I have to process a lot of datas, so I want to avoid for-loop. I'm looking for an algorithm wich will be able to remove these for-loop but I don't know how to do...

I saw the function 'reshape' but this on give me sub-block like : S1_1 = [1 2 3], S1_2 = [4 5 6] I can't use it because here in the sub-block S1_2 I have the data from the signal 1 and the signal 2.

Then I saw the function 'blockproc' but I didn't really understand how it process and I'm not really convaince that this one can help me...

So, I hope you understand my problem and that you could help me or indicate me a way to find a solution.

DEADBEEF
  • 1,930
  • 2
  • 17
  • 32

4 Answers4

0

Blockproc seems to be doing a block operation rather than a sliding operation. A bit of digging around gives this:

http://www.mathworks.com/help/images/ref/nlfilter.html

But it seems the image processing toolbox is needed.

Also this might help:

http://dovgalecs.com/blog/matlab-sliding-window-easy-and-painless/

In general, try to search for sliding-window or convolution, and try to see if something shows up.

zw324
  • 26,764
  • 16
  • 85
  • 118
0

You could probably find another way of doing your loop with the arrayfun function, but the fact is that it might not necessarily be faster arrayfun can be significantly slower than an explicit loop in matlab. Why?

Community
  • 1
  • 1
m_power
  • 3,156
  • 5
  • 33
  • 54
0

In addition to @Ziyao Wei's suggestion you could alternatively use im2col:

>> S = im2col(a', [3 1])
S =

     1     2     3     6     7     8    11    12    13
     2     3     4     7     8     9    12    13    14
     3     4     5     8     9    10    13    14    15

Where S(:, 3*k-2:3*k) for k = 1:data_rows are the desired sub-blocks of row k of your data (a).

p8me
  • 1,840
  • 1
  • 15
  • 23
0

Thank you a lot for all your (quick) answers ! I didn't expect to get an answer so quickly !

I have the image processing toolbox and your different methods are greats ! I'll have to use the im2col because is the "slower" solution for me and I can remove one for-loop.

Thank you for your help

DEADBEEF
  • 1,930
  • 2
  • 17
  • 32