Reputation: 3145
I have streaming data, as float values that range between -2 and +4. I need to write a function that normalizes these values between -1 and +1.
I have:
float normalize(float input)
{
int min = -1;
int max = 1;
float normalized_x = (input - min) / (max - min);
return normalized_x;
}
But this gives me values that are incorrect, and range from -0.4 to +2.3, roughly. What do I need to adjust in my function?
Thank you.
Upvotes: 2
Views: 11500
Reputation: 1537
You want to first center the range around 0, then divide to make it go from -1 to 1.
float normalize(float input)
{
float normalized_x = (input - 1) / 3;
return normalized_x;
}
More generalized:
const float min = -2;
const float max = 4;
float normalize(float input)
{
float average = (min + max) / 2;
float range = (max - min) / 2;
float normalized_x = (input - average) / range;
return normalized_x;
}
Upvotes: 10