Search code examples
ioscfloating-pointaccelerate-framework

Accelerate framework "sign" function


I'm trying to find a super fast way of getting the sign of each value in a vector. I was hoping to find a function in the accelerate framework to do this, but couldn't find one. Here's what it would do:

float *inputVector = .... // some audio vector
int length = ...// length of input vector.
float *outputVector = ....// result

for( int i = 0; i<length; i++ )
{
  if( inputVector[i] >= 0 ) outputVector[i] = 1;
  else outputVector[i] = -1;
}

Solution

  • Ok, I think I've found a way...

    vvcopysignf() "Copies an array, setting the sign of each value based on a second array."

    So, one method would be to make an array of 1s, then use this function to change the sign of the 1s based on an input array.

    float *ones = ... // a vector filled with 1's
    float *input = .... // an input vector
    float *output = ... // an output vector
    int bufferSize = ... // size of the vectors;
    
    vvcopysignf(output, ones, input, &bufferSize);
    
    //output now is an array of -1s and 1s based the sign of the input.