Extract certain numbers of samples from left to ri

2019-09-25 06:48发布

There is a matrix Idx = [1xM] with M dimension of index numbers of a signal to be extracted. For each index number, from left to right certain numbers of samples should be extracted to form a new sub-signal of original signal. For example, 3 samples from left and 4 samples from right of an index number see below:

[idx-3:idx+4]

[New_Sig] becomes an one-row matrix instead of being the same dimension of index number from index matrix [Idx]

Fs = 500; %Frequency:500
StartIdx = 0.150 * Fs;
EndIdx = 0.500 * Fs;

[Idx] = [.....];
[New_Sig] = [Idx-StartIdx : Idx+EndIdx];

Here is an example for two index points from Idx matrix below:

[Idx] = [2 19 23 43 48 52 62 74 78 79 81]
old_sig = [-2 0 1 2 5 6 7 8 10 19 20 21 22 23 24 25 ...]

if # of sample from left = 3, # of sample from right = 4:

a_new_sub = [-2 0 1 **2** 5 6 7 8]
b_new_sub = [7 8 10 **19** 20 21 22 23]
.....

1条回答
\"骚年 ilove
2楼-- · 2019-09-25 06:53

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.

查看更多
登录 后发表回答