I have developed an algorithm that counts the number of times a particular block (within a 2D block) crosses zero. Here's an example:

B1 = {  1, 4, 
        -1, -3, 
        0.12, -2.3

The answer would there for be 3. To do this, what I do is:

  1. Find the signum value of each element within the block (will return "1", "-1", "0") respectively.

  2. If the signum value returns -1 then count increments by 1.

This will repeat until the there is no blocks, however, will only produce 1 value per block.

I am looking for a way to put all this process into an equation so I can demonstrate this rather than having to explain the processes in written text everytime. Is this possible?


2 Answers 2


Your algorithm, as described, is not correct. You are simply counting the number of negative numbers, not the number of zero crossings. For the particular data set that you use, B1, the two numbers are the same, but in general they won't be.

The way to do it is to multiply each number by the number after it, and if that number is negative, then you have a zero crossing.

zeroCrossings = sum((data(1:end-1) .* data(2:end)) < 0);

You also need to add some logic to handle samples that are exactly zero, but hopefully you get the idea at this point.

  • $\begingroup$ Hey, but everywhere that I have read says that, if you have a graph, and, the value is negative (it crosses "0") then this is a zero-crossing.. I am doing speech analysis $\endgroup$
    – Phorce
    Nov 21, 2012 at 20:33
  • $\begingroup$ What if your data is all negative numbers? Then it doesn't have any zero crossings. What if it is all positive numbers except for one negative number in the middle? Then it has two zero crossings. $\endgroup$
    – Jim Clay
    Nov 21, 2012 at 20:51
  • 1
    $\begingroup$ Zero crossing isn't a synonym for "less than zero", it refers to a transition from positive to negative or from negative to positive. $\endgroup$
    – Jim Clay
    Nov 21, 2012 at 20:52
  • $\begingroup$ @user1582478 A zero-crossing means exactly that - you cross the zero line. That means either going from positive to negative, or from negative to positive. $\endgroup$
    – Spacey
    Nov 21, 2012 at 20:54
  • 2
    $\begingroup$ @user1582478 Yes, that is correct. But think about why it is true. Dont just memorize the formula. A "zero-crossing" is not some exotic transform - it is simply, counting how many times your signal changes its sign. If you went from + to -, you have a zero crossing. If you went from - to +, you have a zero crossing. Look at this vector: [-3 2 4 13.2 -0.4 -9 -0.1 1 9]. How many zero crossings do you have? (How many times do you change sign?) Answer is 3. $\endgroup$
    – Spacey
    Nov 21, 2012 at 21:53

If you typically want to 'convert' a random set of 'digital' points into a curve, then interpolation techniques or Curve fitting might be a good approach to consider.

Otherwise, Jim Clay's answers would be sufficient.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.