使用MATLAB查找大向量的交点

时间:2018-07-31 02:51:05

标签: matlab signals vectorization intersection

如何在MATLAB中比较两个信号以找到它们的交点?我的信号是大向量,其中可能包含重复值。

我一直在尝试使用intersect使用以下方法,这种方法对于随机生成的信号效果很好。

% Example case
sig1 = rand(100,1);
sig2 = [sig1(end-10:end); rand(90,1)]; % a signal with imposed intersection.
[c, ia, ib] = intersect(sig1, sig2);
plot(sig2)
hold on
scatter(ib, sig2(ib), 'filled')
hold off

我正在对我的真实数据使用这种方法,但是由于信号中的重复值,它无法产生正确的交点。因此,我认为向两个信号都添加一个非常小的随机噪声,然后应用intersect,但是,intersect不能添加阈值。

有人可以给我一些提示,如何可靠而有效地找到两个大信号测量的交集?还有其他方法吗?预先谢谢你。

背景:

我实际上有几条大型录音,sig1sig2sig3,...。每两个连续的录音,例如sig1sig2可能有重叠,这意味着sig1的记录结尾可以与sig2的开头完全相同。因此,我的目标是检查是否存在重叠,检测它们,然后将其删除以连接所有度量:sig1sig2sig3,...

我也知道这些录音的顺序,因此可以将交点视为sig1(end-N:end) = sig2(1:N+1)

2 个答案:

答案 0 :(得分:1)

典型的方法是互相关(the signal processing toolbox has the function xcorr)。互相关的峰值指示两个信号最相似的延迟。这是您需要了解的两个信号的重叠量。

由于您要比较一个信号的尾部和另一个信号的首部,因此我们将互相关仅应用于信号的那些部分。这确实需要知道重叠可以有多大(某个上限),这是不理想的。如果我们正在计算的互相关的裁剪部分太短(即不包含完整的重叠),则计算出的偏移将是不正确的。并且如果它太长,太长,则互相关可能无法识别正确的偏移(峰值可能隐藏在噪声中)。也许其他人可以接受这个想法,并从中建立更强大的功能...

(我没有安装信号处理工具箱,所以我改用fftifft来实现它)

% Two example signals
sig1 = rand(100,1);
sig2 = [sig1(end-10:end); rand(90,1)];

% Take the end of sig1 and the start of sig2
N = 15; % should be larger than the overlap
end1 = sig1(end-N+1:end);
start2 = sig2(1:N);

% Compute cross-correlation
xc = ifft(fft(end1).*conj(fft(start2)));

% Find peak
[~,shift] = max(xc);

% Crop signal #2
Nrep = N-shift+1
sig2_cropped = sig2(Nrep+1:end);

% Plot
clf
subplot(2,1,1)
plot(sig1)
hold on
plot(numel(sig1)-Nrep+1:numel(sig1),sig1(end-Nrep+1:end),'r.')
subplot(2,1,2)
plot(sig2)
hold on
plot(1:Nrep,sig2(1:Nrep),'r.')

一种可能更健壮但比上述方法更慢的快捷方法是在循环中实现比较:

Nrep = 0;
for N = 1:min(numel(sig2),numel(sig1))
   % Take the end of sig1 and the start of sig2
   end1 = sig1(end-N+1:end);
   start2 = sig2(1:N);
   % Compare
   if all(end1==start2) % possibly do this with a tolerance
      Nrep = N;
      break
   end
end

在这里,我们开始比较1个样本的重叠,然后将其逐个递增,直到找到匹配项。如果找不到Nrep==0,则不会重复任何采样。

答案 1 :(得分:1)

如果需要容忍,请使用Function buildColAr(ByVal v As Variant) As Variant ' Purpose: return column number array from splitted string values ' Example: a = buildColAr("A,B,C,F,G,R,S,T") returns 1|2|3|6|7|18|19|20 Dim i&, temp v = Split(v, ","): ReDim temp(LBound(v) To UBound(v)) For i = LBound(v) To UBound(v) temp(i) = Cells(1, v(i)).Column ' get column numbers Next i buildColAr = temp End Function ismember

ismembertol