【问题标题】:Finding zero crossing that are going positive and zero crossing that are going negative找到正向的零交叉和负向的零交叉
【发布时间】:2013-05-03 19:43:28
【问题描述】:

我有一个信号想要复制:

1) 从零交叉开始变为正数

2) 复制一定数量的点(比如 8000)

3) 并在复制完 8000 个点后继续追加点,直到找到向下的零交叉部分。

我可以找到零交叉,但我在知道如何判断零交叉何时为正和/或零交叉何时为负时遇到了一些问题。我也无法在最后的 8000 点之后添加下一部分点(所以问题 #1 和问题 #3 粗体强>我有问题)

注意:请记住,我使用的信号是音频信号,所以它不如简单的方程式那么好。

我附上了测试代码和一张图片。我正在使用 matlab / octave

clear all, clc, tic, clf;
n=16000
t=linspace(0,2*pi,n);
y=cos(6*t)+sin(4*t);

%find zero crossings
t1=y(1:n-1);
t2=y(2:n);
tt=t1.*t2;
indx=find(tt<0)

%1) start at first zero crossing going positive 
%2) get 8000 pts 
%3) and after the 8000 points continue appending points until a zero crossing going down section is found
new_y=y(indx(1,1):8000); %start at zero section found get 8000 pts
subplot(2,1,1);plot(y);title('Original Signal')
subplot(2,1,2);plot(new_y);title('New signal')

【问题讨论】:

    标签: matlab signal-processing octave


    【解决方案1】:

    试试这个:

    x = diff(sign(y));
    indx_up = find(x>0);
    indx_down = find(x<0);
    

    这将为您提供交叉点及其方向。在您添加样本的循环中,只需测试 x 的当前点和最后一点。如果为零,请继续。如果是肯定的,加上你的 8000 分并返回测试。如果它是负面的,停止。

    编辑:更正了第一行代码中的错字。

    【讨论】:

    • 在进行此测试之前,您可能还需要过滤信号以去除高频噪声,以避免在通过零时出现多次交叉。
    • 感谢 Craigim 的帮助,但您的意思是“测试 x 的当前点和最后一点”。我应该用什么来测试它们?
    • 具体如何操作将取决于您如何读取数据的详细信息,但如果您的循环索引为n,那么您可能会执行类似x = sign(t(n-1)) - sign(t(n)); if x&gt;0; add 8000 pts; elseif x==0; append a point; elseif x&lt;0; stop adding points;end 的操作。
    • 非常感谢 :-)
    • 这种检测过零的方法的唯一问题是向量中是否存在精确的零。例如diff(sign([-2 0 2])) 会建议两个过零而不是一个。清除这些的一种方法是逐个元素 and 与原始向量 - diff(sign(t)) &amp; t(2:end) 或保留交叉点的方向 (diff(sign(t)) &amp; t(2:end)) .* sign(t(2:end))
    【解决方案2】:

    这是测试代码以防其他人有类似问题

    %zero crossing testing  (find zero upward, copy fs 4000, find next zero upward.
    clear all, clc, tic, clf;
    n=16000
    t=linspace(0,2*pi,n);
    y=cos (6*t)+sin(4*t);
    
    find_zero = diff(sign(y));
    indx_up = find(find_zero>0); %find all upward going zeros
    indx_down = find(find_zero<0); %find all downward going zeros
    new_y=[];
    
    fs_range_wanted=indx_up(1,1)+4000; %starts from first zero adds sample size wanted
    new_y=[y(indx_up(1,1):fs_range_wanted)]; %may have to minus 1
    ii=0;
    while (find_zero(1,fs_range_wanted+ii)  ~= 2);  %do while not going dwn and append 
        ii=ii+1
        y_pt_loc=fs_range_wanted+ii %what is the location of the point
        new_y = [new_y, y(1,fs_range_wanted+ii)]; %append points
    end
    
    
    subplot(3,1,1);plot(y);title('Original Signal')
    subplot(3,1,2);plot(new_y);title('New signal')
    subplot(3,1,3);plot(find_zero);title('Zeros-Pos-Neg')
    

    【讨论】:

      【解决方案3】:

      您可以这样做来查找“上升”或“下降”过零:

      %find zero crossings
      t1=y(1:n-1);
      t2=y(2:n);
      tt=t1.*t2;
      indx=find(tt<0)
      
      dt        = t2-t1;
      indx_up   = find( (tt<0) & (dt>0) ) 
      indx_down = find( (tt<0) & (dt<0) ) 
      

      【讨论】:

        【解决方案4】:
        function[t,s]=zerocorss(x,m)
            if nargin<2
              m='b';
            end
        
            s=x>0;
        
            k=s(2:end)-s(1:end-1)
        
          if any(m=='p')
              f=find(k>0);
          elseif (m=='n')
              f=find(k<0);
          else
              f=find(k~=0);
          end
        
          s=x(f+1)-x(f);
          f=f-x(f)./s;
        
          if ~nargout
              n=length(x);
              subplot(2,1,1),plot(1:n,x,'x',t,zerocorss(length(x)/1),'o');
              subplot(2,1,2),stem(t,s);
          end
        end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-06-10
          • 1970-01-01
          • 2019-10-09
          • 1970-01-01
          • 1970-01-01
          • 2019-12-21
          • 1970-01-01
          相关资源
          最近更新 更多