在MATLAB中,在给定数据向量的情况下,在阈值内找到零交叉 [英] In MATLAB, find zero crossings within a threshold, given a vector of data
问题描述
我已经在MATLAB中编写了一个函数来计算给定信号数据矢量的过零次数.如果找到零交叉点,我还将检查所涉及的两个矢量索引之间的绝对差是否大于阈值-这是为了减少信号噪声的影响.
I have written a function in MATLAB to count the number of zero crossings given a vector of signal data. If I find a zero crossing, I also check whether the absolute difference between the two vector indices involved is greater than a threshold value - this is to try to reduce the influence of signal noise.
zc = [];
thresh = 2;
for i = 1:length(v)-1
if ( (v(i)>0 && v(i+1)<0) || (v(i)<0 && v(i+1)>0) ) && abs(v(i)-v(i+1)) >= thresh
zc = [zc; i+1];
end
end
zcCount = length(zc);
我在这里使用过零函数中的向量对其进行了测试: http://hips.seas.harvard.edu/content/count-zero-crossings-matlab
I used the vector from the zero crossings function here to test it: http://hips.seas.harvard.edu/content/count-zero-crossings-matlab
A = [-0.49840598306643,
1.04975509964655,
-1.67055867973620,
-2.01437026154355,
0.98661592496732,
-0.06048256273708,
1.19294080740269,
2.68558025885591,
0.85373360483580,
1.00554850567375];
这看起来不错,但是有没有更有效的方法来达到相同的结果呢?例如.在上述网页上,他们只需使用以下行来计算零交叉点:
It seems to work fine but is there a more efficient way of achieving the same result? E.g. on the above webpage, they simply use the following line to calculate zero crossings:
z=find(diff(v>0)~=0)+1;
是否可以将阈值检查合并到类似的有效方法中?
Is there a way to incorporate the threshold check into something similarly efficient?
推荐答案
如何
zeroCrossIndex=diff(v>0)~=0
threshholdIndex = diff(v) >= thresh;
zcCount = sum(zeroCrossIndex & threshholdIndex)
这篇关于在MATLAB中,在给定数据向量的情况下,在阈值内找到零交叉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!