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

↘锁芯ラ 提交于 2019-12-13 11:19:08

问题


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:


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.



来源:https://stackoverflow.com/questions/56233611/extract-certain-numbers-of-samples-from-left-to-right-given-index-point-of-a-sig

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!