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

时间:2019-05-21 07:51:35

标签: matlab signals

有一个矩阵Idx = [1xM],其维数为要提取的信号的索引号。对于每个索引号,应从左至右提取一定数量的样本,以形成原始信号的新子信号。 例如,索引编号从左侧的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]
.....

1 个答案:

答案 0 :(得分:2)

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

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中的索引数量,这种方法可能会占用大量内存。