# Number of Zero-crossing

I have written an algorithm that calculates the number of zero-crossings within a signal. By this, I mean the number of times a value changes from + to - and vise versa.

The algorithm is explained like this:

If there are the following elements:

v1 = {90, -4, -3, 1, 3}


Then you multiply the value by the value next to it. (i * i+1)

Then taking the sign value sign(val) determine if this is positive or negative. Example:

e1 = {90 * -4} = -360 -> sigum(e1) = -1
e2 = {-4 * -3} =  12  -> signum(e2) = 1
e3 = {-3 *  1} =  -3  -> signum(e3) = -1
e4 = {1 *   3} =  3   -> signum(e4) = 1


Therefore the total number of values changed from negative to positive is = 2 ..

Now I want to put this forumular, algorithm into an equation so that I can present it.

I have asked a simular question, but got really confused so went away and thought about it and came up with (what I think the equation should look like).. It's probably wrong, well, laughably wrong. But here it is:

Now the logic behind it:

I pass in a V (val)

I get the absolute value of the summation of the signum from calculating (Vi * Vi+1) .. The signum(Vi * Vi+1) should produce -1, 1, ..., values

If and only if the value is -1 (Because I'm only interested in the number of times zero is crossed, therefore, the zero values.

Does this look correct, if not, can anyone suggest improvements?

Thank you :)!

-

I think you mean $(1-\text{sgn}(V_i V_{i+1}))/2$. That is $1$ if the signum is $-1$ and $0$ if the signum is $1$. The absolute value, on the other hand, is $1$ whether the signum is $+1$ or $-1$.
But what are you going to do about cases where $V_i = 0$?
The fix is to take $\sum_i (1 - \text{sgn}(V_i V_{i+1}))/2$. Thus you are adding $1$ when the signum is $-1$ and you are adding $0$ when the signum is $1$. The result is the number of times that the signum is $-1$. – Robert Israel Nov 22 '12 at 0:51
@mjqxxxx: That's why I asked "But what are you going to do about cases where $V_i = 0$?" – Robert Israel Nov 22 '12 at 8:44