0

I've been searching the answer for an exercise. This exercise gives us an array with numbers between -1 and 1. We are asked to count how many times the signal array changes sign. For example : you have this array : [0.7,0.5,0.6,-0.1,0.9,0.6,0.4,-0.2,-0.3,0.2,0.3,-0.3,-0.9,-0.7,0.1,0.2] It changes signs 7 times This is the code I have made that should get the "count" of how many times it changes signs. For some reason, it doesn't work, the count at the end is equal to 0 and it should be 7. Can you help me please ? Thank you very much.

--THE CODE--

import numpy as np

def zeroCrossing(signal):

    j=0
    ls=len(signal)
    print(ls)
    count=0
    x=0
    y=0
    for j in range(0,ls):
        if signal[j]>0 and x==1:
            j=j+1
            y=0
        elif signal[j]>0 and x==0:
            count==count+1
            x=1
            j=j+1
        elif signal[j]<0 and y==1:
            j=j+1
            x=0
        elif signal[j]<0 and y==0:
            count==count+1
            y=1
            j=j+1
    
    return count

print(zeroCrossing(np.array([0.7,0.5,0.6,-0.1,0.9,0.6,0.4,-0.2,-0.3,0.2,0.3,-0.3,-0.9,-0.7,0.1,0.2])))
roganjosh
  • 12,594
  • 4
  • 29
  • 46
Matt Winther
  • 33
  • 1
  • 3

3 Answers3

1

You can achieve what you want simply by iterating through the array as follows:

sign_changes = 0
last_number = None
for n in my_array:
    if n < 0: # checks if n is negative
        if last_number != "negative": # checks if the last number was not negative
            sign_changes += 1
        last_number = "negative"
    else: # takes n as positive (including 0)
        if last_number != "positive": # checks if the last number was not positive
            sign_changes += 1
        last_number = "positive"

Edit:

This code will count 7 in the example array given by the OP, as the OP expects, which counts the first number as a "sign change". To avoid this (and instead count 6 sign changes), small tweaks to the code can be made. The most simple is to change the last_number != "positive" and last_number != "negative" conditions to last_number == "negative" and last_number == "positive" respectively.

Alexander Kalian
  • 1,059
  • 2
  • 9
  • 17
  • If they are going to use arrays then this is hopelessly slow – roganjosh Dec 06 '20 at 19:33
  • 1
    Whether or not it is "hopeless" depends on the exact aims of the script/program. This is a simple solution that works well and answers the question. The OP has not specified any high performance requirements. – Alexander Kalian Dec 06 '20 at 19:40
0

You are not modifying count variable, because you use comparison operator instead of assignment. Replace count==count+1 by count=count+1 to fix it.

fdermishin
  • 3,519
  • 3
  • 24
  • 45
0

Since you always count the first number as 1 time, you may try this numpy code:

arr = np.array([0.7,0.5,0.6,-0.1,0.9,0.6,0.4,-0.2,-0.3,0.2,0.3,-0.3,-0.9,-0.7,0.1,0.2])
change_count = np.count_nonzero(np.diff(np.sign(arr))) + 1

Out[122]: 7
Andy L.
  • 24,909
  • 4
  • 17
  • 29