在 Matlab 中给定信号的索引点,从左到右提取一定数量的样本

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

存在一个矩阵Idx = [1xM],待提取信号的索引号为M维。对于每个索引号,从左到右提取一定数量的样本,形成原始信号的新子信号。 例如,索引号左侧 3 个样本和右侧 4 个样本,如下所示:

[idx-3:idx+4]

[New_Sig] 成为单行矩阵,而不是与索引矩阵 [Idx]

的索引号维度相同
Fs = 500; %Frequency:500
StartIdx = 0.150 * Fs;
EndIdx = 0.500 * Fs;

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

下面是来自 Idx 矩阵的 两个索引点 的示例:

[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]
.....

这是我解决这个问题的建议:

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)

作为输出,我们得到:

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

警告:目前,您的 old_sig 包含唯一值。因此,find 将找到正确的(唯一的)索引。如果信号中的值确实重复,则需要指定应找到哪个值。

另一个警告:根据信号 old_sig 的大小以及 Idx 中的索引数量,此方法可能会占用大量内存。