Finding zero crossing that are going positive and

2019-02-13 21:08发布

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

1) starts at zero crossing going positive

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

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

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)

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

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')

enter image description here

4条回答
Viruses.
2楼-- · 2019-02-13 21:44

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

%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) ) 
查看更多
爷、活的狠高调
3楼-- · 2019-02-13 21:46
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
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-02-13 21:49

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.

Edit: Corrected typo in first code line.

查看更多
闹够了就滚
5楼-- · 2019-02-13 21:49

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')

enter image description here

查看更多
登录 后发表回答