[Solved] Extract certain numbers of samples from left to right given index point of a signal in Matlab


Here’s my suggestion to solve this issue:

StartIdx = 3;
EndIdx = 4;

Idx = [2 19 23];
old_sig = [-2 0 1 2 5 6 7 8 10 19 20 21 22 23 24 25 26 27 28 29];

% Get number of "indices".
nIdx = numel(Idx);

% Find actual indices.
idx = ceil(find(repmat(old_sig, nIdx, 1) == repmat(Idx', 1, numel(old_sig))) / nIdx);

% Set up correct (index) ranges to access in old_sig.
idx = repmat(-StartIdx:EndIdx, nIdx, 1) + repmat(idx, 1, (StartIdx + EndIdx + 1));

% Determine output.
new_sig = old_sig(idx)

As output, we get:

new_sig =
   -2    0    1    2    5    6    7    8
    7    8   10   19   20   21   22   23
   20   21   22   23   24   25   26   27

Caveat: At the moment, your old_sig contains unique values. Therefore, the find will find the correct (unique) index. If the values in your signal do repeat, one need to specify which value should be found.

Another caveat: Depending, on how large your signal old_sig is, and how many indices you have in Idx, this approach might get memory intensive.

11

solved Extract certain numbers of samples from left to right given index point of a signal in Matlab