Take the 2-minute tour ×

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:

enter image description here

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 :)!


enter image description here

Is this correct now?

share|improve this question
Honestly, this really is not a programming question is it? If you are asking for an algorithm that will give you the answer, that is fine, but just asking for mathematical expression in this case has nothing really to do with programming and there does not really belong on stackoverflow –  phil13131 Nov 22 '12 at 0:14
add comment

1 Answer

up vote 2 down vote accepted

You are doing the right thing here but your equation is wrong simply because you only want to count the sign of the product of adjacent elements when it is negative. Dont sum the sign of products since positive sign products should be neglected. For this reason, an explicit mathematical formula is tricky as positive products between adjacent elements should be ignored. What you want is a function that takes 2 arguments and evaluates to 1 when their product is negative and zero when non-negative

f(x,y) = 1 if xy < 0
       = 0 otherwise

then your number of crossing points is simply given by

sum(f(v1[i],v1[i+1])) for i = 0 to i = n-1 

where n is the length of your vector/array v1 (using C style array access notation based on zero indexing). You also have to consider edge conditions such as 4 consecutive points {-1,0,0,1} - do you want to consider this as simply one zero crossing or 2??? Only you can answer this based on the specifics of your problem, but whatever your answer adjust your algorithm accordingly.

share|improve this answer
are you saying the function can be represented as an equation? I'm confused.. I know the algorithm is correct, I just don't know whether the equation that I've written to represent it is correct? –  Phorce Nov 22 '12 at 0:19
I would say the equation is wrong since you are negating valid zero crossings with non-zero crossings (which have sign +1). See my updated answer –  mathematician1975 Nov 22 '12 at 0:26
which part of the equation is wrong? confused –  Phorce Nov 22 '12 at 0:33
@Phorce you are summing SIGN of product of adjacent elements. Thus for a vector [-1,1,2,3] your formula results in the number of crossing points as 2 (-1 + 1 + 1) which is wrong. It should be -1. Your formula should consider products that have positive sign as having a zero weight –  mathematician1975 Nov 22 '12 at 0:38
I get what you mean now (Sorry, this has been frustrating me for ages) So what I could simply do is, calculate the SIGN of the product first, once I have that, check to see if the value is -1 and if this is true, sum up the elements that are -1? –  Phorce Nov 22 '12 at 0:42
show 11 more comments

Your Answer


