找到零交叉,正的和零相交都是负的。

时间:2023-02-01 16:18:10

I have a signal that I would like to copy when it:

我有一个信号,我想在它:

1) starts at zero crossing going positive

1)从零交叉开始,走向正。

2) copy a set number of points (like 8000)

2)复制设置点数(如8000)

3) and after the 8000 points are copied continue appending points until a zero crossing going down section is found.

在8000点被复制后,继续追加点,直到找到一个零相交的下降段。

I can find the zero crossing but I'm having some issues with knowing how to tell when there is a zero crossing going positive and/or a zero crossing going negative. I'm also having trouble with adding the next section of points after the 8000 points at the end (So question #1 and question #3 in bold I'm have issues with)

我可以找到零交叉,但我有一些问题,知道如何分辨什么时候有一个零交叉,正的和/或一个零交叉,变成负的。我也有问题,在最后的8000点之后添加下一部分的分数(所以问题#1和第3个问题,我有问题)

Note: please keep in mind the signal I'm using is an audio signal so it won't be as nice as a simple equation.

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

I've attached the test code along with an image. I'm using matlab / octave

我已经附加了测试代码和图像。我用的是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')

找到零交叉,正的和零相交都是负的。

4 个解决方案

#1


14  

Try this:

试试这个:

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

That will give you the crossing points and their direction. In your loop where you add samples, just test x for the current point and the last point. If it's zero, keep going. If it's positive, add on your 8000 points and go back to testing. If it's negative, stop.

这会给你一个交叉点和他们的方向。在您的循环中添加示例,只测试当前点和最后一点的x。如果是零,继续。如果它是正的,那就增加8000点,回到测试中。如果是负数,停止。

Edit: Corrected typo in first code line.

编辑:在第一行代码中纠正错误。

#2


1  

Here's the test code in-case someone else has a similar question

这里是测试代码,以防别人也有类似的问题。

%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


0  

You can do like this to find "going-up" or "going-down" zero crossings:

你可以这样做,找到“goup”或“going-down”零交叉:

%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


0  

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

#1


14  

Try this:

试试这个:

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

That will give you the crossing points and their direction. In your loop where you add samples, just test x for the current point and the last point. If it's zero, keep going. If it's positive, add on your 8000 points and go back to testing. If it's negative, stop.

这会给你一个交叉点和他们的方向。在您的循环中添加示例,只测试当前点和最后一点的x。如果是零,继续。如果它是正的,那就增加8000点,回到测试中。如果是负数,停止。

Edit: Corrected typo in first code line.

编辑:在第一行代码中纠正错误。

#2


1  

Here's the test code in-case someone else has a similar question

这里是测试代码,以防别人也有类似的问题。

%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


0  

You can do like this to find "going-up" or "going-down" zero crossings:

你可以这样做,找到“goup”或“going-down”零交叉:

%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


0  

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