Thread: How to return the edge points positions-zero crossing points of array values?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    34

    How to return the edge points positions-zero crossing points of array values?

    Hello !
    I'm trying to implement in c a function that returns the positions of the zero crossing points of its array values, this means to return all the positions where there's a transition between positive values nd negative values(or vice versa -transition between negative and positive values).
    Assumption that the value zero is considered as transition and there couldn't be more than one zero continuously- can't appear two zeros following each other (I will explain in progress by an example about this).
    In addition , the value of the array is considered as transition because there's nothing after the last value of the array so we assume that implicitly the last value and NULL (there's nothing after the last value of the array) is a transition.

    the array values are float values .

    @Note: first position of the array is considered index = 1 and not index = zero (this is assumption to my problem).

    Examples:
    #1
    Code:
    Array data= {-1,3}
    so the output is array of positions/indexes of the array where there's transition between positive and negative values:
    Code:
    {1 ,2};
    Explanation:
    the value of position 1 is (-1) so after it we see positive value 3 so the position 1 is considered as transition between negative or positive (vice versa also ok).
    the position 2 (last position at the array) is also added to the output positions because there's no values after position 2 (out of the array size) so it's as assumption considered transition, so we always in my case add the last position of the array to the output positions .


    #2
    Code:
    Array data= {-1,-1,1,-3,-1,3,3}
    so the output is array of positions where there's transition between positive and negative values:
    Code:
    {2,3,5,7};
    #3
    Code:
    Array data= {-1,-1,1,-3,-1,3,3,0}
    so the output is array of positions where there's transition between positive and negative values:
    Code:
    {2,3,5,7,8};
    (see here that position 8 is added because there's zero appeared in the last position of the array, and as I said value with zero is considered as transition so we need to write its position on the output)

    #4
    Code:
    Array data= {-1,-1,1,-3,-1}
    so the output is array of positions where there's transition between positive and negative values:
    Code:
    {2,3};
    (see what I marked in the array data in this example its corresponded to the output positions where there's a transition between positive values and negative values)

    #5
    Code:
    Array data= {-1,0,1}
    so the output is array of positions/indexes of the array where there's transition between positive and negative values:
    Code:
    {1 ,2 , 3};
    Explanation:
    the value of position 1 is (-1) so after it we see positive value 0 so the position 1 is considered as transition between negative or positive (vice versa also ok)- I already said above the when there's value 0 as it's considered as position because it's transition.

    the position 2 (second value 0 ) is also added to the output positions because as I said the value 0 is considered as transition (doesn't matter if we implicitly look at it from right to left or left to right).

    the position 3 (last position at the array) is also added to the output positions because there's no values after position 3 (out of the array size) so it's considered transition as assumption, so we always in my case add the last position of the array to the output positions .

    #5
    Code:
    Array data= {-1,0,1,0}
    so the output is array of positions/indexes of the array where there's transition between positive and negative values:
    Code:
    {1 ,2 , 3, 4};
    #6
    Code:
    Array data= {0,1}
    so the output is array of positions/indexes of the array where there's transition between positive and negative values:
    Code:
    {1 ,2 };
    (as I said the value zero is implicitly considered as transition, so the position 1 is added )

    #7
    there can't be case that more than one zero following each other this means
    Code:
    {1,0,0}
    isn't possible but it could be like this
    Code:
    {1,0,1,0}
    this is possible because there's no more than one zero following each other.


    Hope my problem is understandable!


    So what I've implemented in C and I'm to get correct outputs:
    Code:
    
    
    Code:
    #include<stdio.h>
    #include<stdbool.h>
    
    zeroCrossing(float *data, float *zerCross, int nx);
    /* zero crossing function */
    /* data = input array which it's the Array data*/
    /* zerCross = output zero crossing array- output positions */
    void zeroCrossing(float *data, float *zerCross, int nx)
    {
        int i;
        bool sign1, sign2;
    
    
        memset(zerCross, 0, nx*sizeof(float));
        for(i=0; i<nx-1; i++)     /* loop over data  */
        {
            sign1 = getSign(data[i]);
            sign2 = getSign(data[i+1]);
            if(sign1!=sign2)  /* set zero crossing location */
                zerCross[i+1] = 1;
        }
    }
    
    
    /* get sign of number */
    bool getSign(float data)
    {
        if(data>0)      /* positif data */
            return (1);
        else            /* negatif data */
            return (0);
    } 
    
    
    
    int main()
    {
       %we input manually the float of our array
        float array[9] = {1.0,2.0,3.0,0.0,-1.0,-2.0,-3.0,0.0,1.0}; 
    
        float *p = array;
        float f1[9];
        float *p2 = f1;
        int bx= 2 ;
        zeroCrossing(array, f1, bx); 
    }

    I get wrong outputs and a compilations error, any help please?
    maybe my algorithm isn't good so for my problem it would be appreciated for any suggestion/help.

    thanks alot!
    Last edited by JohnnyOmari; 12-22-2020 at 01:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble printing x and y values of struct points in an array?
    By arcadedragon in forum C Programming
    Replies: 6
    Last Post: 10-03-2012, 10:15 AM
  2. C++ Plot Simple Points / Graph, X & Y array points
    By Khadafi in forum C++ Programming
    Replies: 9
    Last Post: 11-11-2011, 03:47 AM
  3. Decimal Points and Binary Points
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-07-2002, 01:06 AM
  4. multiple return points
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 05-09-2002, 03:01 AM

Tags for this Thread