Last active
August 29, 2015 14:18
-
-
Save garrafote/24efd01c75ffe988c159 to your computer and use it in GitHub Desktop.
C++ function to normalize 8bit input into [-1 1] range with a deadzone in the middle.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Normalizes a 8 bit integer value into [-1 1] range. | |
// The deadzone is the middle portion of the 8 bit range | |
// to be considered as 0 output. For a deadzone [dz] value | |
// of n, the deadzone range will be [127-n 128+n]. | |
// | |
// in = 0 (127 - dz) (128 + dz) 255 | |
// out = -1 0 0 1 | |
// <---x-----------x--------------x----------x---> | |
// |______________| | |
// 2 * dz | |
// | |
float normalize(uint8_t value, uint8_t deadzone) { | |
// lower and upper zone boundaries | |
uint8_t lz = 127 - deadzone; | |
uint8_t uz = 128 + deadzone; | |
// the [lz uz] range is the deadzone range | |
if (value >= lz && value <= uz) { | |
return 0; | |
} | |
// value is in [0 lz) range | |
if (value < lz) { | |
short sValue = (short)value - lz; | |
float nValue = (float)sValue / (lz); | |
return nValue; | |
} | |
else /* value is in (uz 255] range */ { | |
short sValue = (short)value - uz; | |
float nValue = (float)sValue / (lz); | |
return nValue; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment