Matlab 寻找正向过零和负向过零

Matlab 寻找正向过零和负向过零,matlab,signal-processing,octave,Matlab,Signal Processing,Octave,我有一个信号,当它: 1)从零交叉开始变为正 2) 复制一组点数(如8000) 3)复制8000点后,继续添加点,直到找到向下的过零部分。 我可以找到零交叉点,但我在知道如何判断零交叉点何时为正和/或零交叉点何时为负时遇到了一些问题。我在最后的8000分之后添加下一部分分数时也遇到了问题(因此问题#1和问题#3粗体我有问题) 注意:请记住我使用的信号是一个音频信号,所以它不会像一个简单的等式那么好 我已经附上了测试代码和图片。我正在使用matlab/倍频程 clear all, clc, tic

我有一个信号,当它:

1)从零交叉开始变为正

2) 复制一组点数(如8000)

3)复制8000点后,继续添加点,直到找到向下的过零部分。

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

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

我已经附上了测试代码和图片。我正在使用matlab/倍频程

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')
全部清除、clc、tic、clf;
n=16000
t=linspace(0,2*pi,n);
y=cos(6*t)+sin(4*t);
%找到零交叉点
t1=y(1:n-1);
t2=y(2:n);
tt=t1.*t2;

indx=find(tt您可以这样做来查找“上升”或“下降”过零点:

%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) ) 
%查找零交叉点
t1=y(1:n-1);
t2=y(2:n);
tt=t1.*t2;
indx=find(tt试试这个:

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

indx_down=find(x这里是测试代码,以防其他人有类似的问题

%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')
%过零测试(向上找零,复制fs 4000,向上找下一个零)。
清除所有、clc、tic、clf;
n=16000
t=linspace(0,2*pi,n);
y=cos(6*t)+sin(4*t);
求零=diff(符号(y));
indx_up=find(find_zero>0);%查找所有向上的零
indx_down=find(find_zero
function[t,s]=zerocross(x,m)
如果nargin0;
k=s(2:end)-s(1:end-1)
如果有(m='p')
f=查找(k>0);
elseif(m=='n')

f=find(kYou可能还希望在进行此测试之前过滤信号以去除高频噪声,以避免通过零点时出现多次交叉。感谢Craigim的帮助,但“测试x当前点和最后一点”是什么意思。我应该针对什么进行测试?具体如何进行测试将取决于如何读取数据的详细信息,但如果循环索引为
n
,则可能会执行类似于
x=sign(t(n-1))-sign(t(n));如果x>0;添加8000个点;elseif x==0;添加一个点;elseif xThanks的操作,这非常有帮助:-)这种检测零交叉的方法唯一的问题是向量中是否有一个精确的零。例如
diff(符号([-2 0 2])
将建议两个零交叉,而不是一个零交叉。清除这些零交叉的一种方法是逐元素
使用原始向量-
diff(符号(t))&t(2:end)
或保留交叉口的方向,
(diff(sign(t))&t(2:end)).*sign(t(2:end))
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