查找序列中的零岛

编程入门 行业动态 更新时间:2024-10-16 16:04:17
本文介绍了查找序列中的零岛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

想象一下你有一个很长的序列.找到序列全为零(或更准确地说,序列下降到接近零值abs(X)

Imagine you have a very long sequence. What is the most efficient way of finding the intervals where the sequence is all zeros (or more precisely the sequence drops to near-zero values abs(X)<eps):

为简单起见,我们假设以下顺序:

For simplicity, lets assume the following sequence:

sig = [1 1 0 0 0 0 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 0];

我正在尝试获取以下信息:

I'm trying to get the following information:

startIndex EndIndex Duration 3 6 4 12 12 1 14 16 3 25 26 2 30 30 1

然后使用这些信息,我们找到持续时间 >= 到某个指定值(比如 3)的区间,并返回所有这些区间组合中的值的索引:

then using this information, we find the intervals with duration >= to some specified value (say 3), and returning the indices of the values in all these intervals combined:

indices = [3 4 5 6 14 15 16];

最后一部分与上一个问题有关:

That last part is related to a previous question:

MATLAB:矢量化数组创建从开始/结束索引列表

这是我目前所拥有的:

sig = [1 1 0 0 0 0 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 0]; len = length(sig); thresh = 3; %# align the signal with itself successively shifted by one %# v will thus contain 1 in the starting locations of the zero interval v = true(1,len-thresh+1); for i=1:thresh v = v & ( sig(i:len-thresh+i) == 0 ); end %# extend the 1's till the end of the intervals for i=1:thresh-1 v(find(v)+1) = true; end %# get the final indices v = find(v);

我希望对代码进行矢量化/优化,但我愿意接受其他解决方案.我必须强调,空间和时间效率非常重要,因为我正在处理大量长生物信号.

I'm looking to vectorize/optimize the code, but I'm open to other solutions. I have to stress that space and time efficiencies are very important, since I'm processing a large number of long bio-signals.

推荐答案

这些是我以矢量化方式解决您的问题所采取的步骤,从给定的矢量 sig 开始:

These are the steps I would take to solve your problem in a vectorized way, starting with a given vector sig:

  • 首先,对向量设置阈值以获得零和一的向量tsig(信号绝对值下降到足够接近零的零,其他地方的零):

  • First, threshold the vector to get a vector tsig of zeros and ones (zeroes where the absolute value of the signal drops close enough to zero, ones elsewhere):

tsig = (abs(sig) >= eps); %# Using eps as the threshold

  • 接下来,使用函数DIFF 和 查找:

    dsig = diff([1 tsig 1]); startIndex = find(dsig < 0); endIndex = find(dsig > 0)-1; duration = endIndex-startIndex+1;

  • 然后,找到持续时间大于或等于某个值(例如 3,来自您的示例)的零字符串:

  • Then, find the strings of zeroes with a duration greater than or equal to some value (such as 3, from your example):

    stringIndex = (duration >= 3); startIndex = startIndex(stringIndex); endIndex = endIndex(stringIndex);

  • 最后,使用 我对链接问题的回答中的方法 用于生成您的最终索引集:

  • Finally, use the method from my answer to the linked question to generate your final set of indices:

    indices = zeros(1,max(endIndex)+1); indices(startIndex) = 1; indices(endIndex+1) = indices(endIndex+1)-1; indices = find(cumsum(indices));

  • 更多推荐

    查找序列中的零岛

    本文发布于:2023-11-30 09:20:53,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1649485.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:序列

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!