Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

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

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]
.....
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...