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')
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With