Generic mapping multiple numerical ranges

Asked

Viewed 28 times

-1

I’m writing a code, for making decisions on an 8bit microcontroller, an AVR.

I get 8 range of double values, limited by Vf_max and Vf_min, I don’t know which will be the biggest in each track. As in the example below, it is perceived that coordinates are distributed in their quadrants:

const double LIMIT_NORTE[]    = {  -1,   1};   // 0x14
const double LIMIT_NORDESTE[] = {   1,  89};   // 0x11
const double LIMIT_LESTE[]    = {  89,  91};   // 0x12
const double LIMIT_SUDESTE[]  = {  91, 179};   // 0x22
const double LIMIT_SUL[]      = { 179, 180};   // 0x23
const double LIMIT_SUDOESTE[] = {-180, -91};   // 0x33
const double LIMIT_OESTE[]    = { -91, -89};   // 0x34
const double LIMIT_NOROESTE[] = { -89,  -1};   // 0x44

So, if I receive a value for example 45, I know that this value is in the first quadrant and is equivalent to the Northeast range, 1 to 89, if I receive a value for example -130, I know it is in the southwest, and so on.

But I need to convert that received value into a percentage of the track that it belongs to, and I’m not able to find a formula that makes such a conversion possible in general, since in the program, such tracks can change when parameterized, in other words, the limits of what is considered the South strip, can be expanded, reducing the limits of the Southeast and Southwest.

For example receiving 45 know intuitively that will be 50% of the Northeast range.

Can anyone help me with a generic formula for such a calculation? Remembering the map() function widely publicized in Arduino does not work, and I’ve tried what is proposed in https://stackoverflow.com/a/5732390/2766598

  • https://pt.wikipedia.org/wiki/Regra_de_tr%C3%AAs_simples

1 answer

1


Assuming the ranges are semi-open [min, max), you can solve this problem with the following generic algorithm:

//pseudo-código

intervalo encontra_intervalo(valor) {
  para cada possível intervalo i {
    se valor >= i.min ou valor < i.max
      retorne i
  }
}

double calcula_porcentagem(valor, min, max) {
  tamanho_intervalo = abs(max + (min * -1))
  valor_truncado = abs(valor + (min * -1))
  valor_normalizado = valor_truncado / tamanho_intervalo
  retorne valor_normalizado
}

For each value first search for which range it is in, with the range information calculate the normalized value for the selected range.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.