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