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
so the output is array of positions/indexes of the array where there's transition between positive and negative values: 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: #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: (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: (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: 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:
#6
so the output is array of positions/indexes of the array where there's transition between positive and negative values:
(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 isn't possible but it could be like this 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:
#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!