Continue to Site

# Matlab solution for finding specific position when a sign change occurs inside vector

Status
Not open for further replies.

#### Andrew8611

##### Member level 1
Does anybody knows a command in Matlab that lets me find the specific position when a sign change occurs inside a vector??
for example, I wrote t=0:0.01:10000 and then I wrote y=sin(t), which command lets me see the position on the vector y where it changes from positive to negative or from negative to positive?????

Help with matlab

I don't know whether there is a command to find the position that sign changes.
But we can write that function ourselves. I think that is not complex. we can check each y in a loop, if y>0 && y(n+1)<=0 or y<0 && y(n+1)>=0, then the sign change occurs at n. and then continue to check the remainder of y. At the end of loop, all the position of sign change are finded.

Ryan

Help with matlab

Maybe this small example will help you. It generates a sinewave frequency sweep, and then plots and tabulates the waveform's positive-slope zero-crossing points.
Code:
% Frequency sweep
F0 = 10;        % start frequency, Hertz
F1 = 100;       % stop frequency, Hertz
T  = 0.5;       % duration, seconds
FS = 1000;      % sample rate, Hertz
N = round(T * FS);
t = T * (0:N-1)' / (N-1);
y = sin(2 * pi * (F0 + (F1 - F0) / 2 .* t / T) .* t);
subplot(2,1,1); plot(t, y); xlabel('seconds');
%
% Positive-slope zero-crossing detector
z = and((y > 0), not(circshift((y > 0), 1)));  z(1) = 0;
subplot(2,1,2); plot(t, z); xlabel('seconds');
%
% Find the locations of the zero-crossing points
crossing_points = find(z);
To detect both positive and negative crossings, change the logic line to this:
z = xor((y > 0), circshift((y > 0), 1)); z(1) = 0;

I prefer using array methods like that because MATLAB executes them much faster than a "for" loop.

annabond

Points: 2

### annabond

Points: 2
require matlab code for zerocrossing detector

hi i'm in need to simulate the zero crossing detector with sinewave as input.. and then convolute the result with sine wave .... can any one provide me the code for this....

Status
Not open for further replies.