diff options
author | Keith Packard <keithp@keithp.com> | 2010-02-28 18:24:46 -0800 |
---|---|---|
committer | Keith Packard <keithp@keithp.com> | 2010-02-28 18:25:43 -0800 |
commit | 6e61170d42936c18cd6efba6f4c14af616a30745 (patch) | |
tree | ee62b3feea77f68e0ae3e185e00d93520d0c35fa | |
parent | e4e17e6d3844ae682a0e7a9469a522359bac77b6 (diff) |
Need to duplicate new altitude conversion code in aoview.
Altitude conversion is now done with a smaller table and interpolation
instead of a giant table.
Signed-off-by: Keith Packard <keithp@keithp.com>
-rw-r--r-- | ao-tools/ao-view/aoview_convert.c | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/ao-tools/ao-view/aoview_convert.c b/ao-tools/ao-view/aoview_convert.c index 02416647..8ee05e1d 100644 --- a/ao-tools/ao-view/aoview_convert.c +++ b/ao-tools/ao-view/aoview_convert.c @@ -17,26 +17,50 @@ #include "aoview.h" -static int16_t altitude_table[2048] = { +static int16_t altitude_table[] = { #include "altitude.h" }; +#define ALT_FRAC_SCALE (1 << ALT_FRAC_BITS) +#define ALT_FRAC_MASK (ALT_FRAC_SCALE - 1) + int16_t aoview_pres_to_altitude(int16_t pres) { - pres = pres >> 4; - if (pres < 0) pres = 0; - if (pres > 2047) pres = 2047; - return altitude_table[pres]; + uint8_t o; + int16_t part; + + if (pres < 0) + pres = 0; + o = pres >> ALT_FRAC_BITS; + part = pres & ALT_FRAC_MASK; + + return ((int32_t) altitude_table[o] * (ALT_FRAC_SCALE - part) + + (int32_t) altitude_table[o+1] * part + (ALT_FRAC_SCALE >> 1)) >> ALT_FRAC_BITS; } int16_t aoview_altitude_to_pres(int16_t alt) { - int16_t pres; + int16_t span, sub_span; + uint8_t l, h, m; + int32_t pres; - for (pres = 0; pres < 2047; pres++) - if (altitude_table[pres] <= alt) - break; - return pres << 4; + l = 0; + h = NALT - 1; + while ((h - l) != 1) { + m = (l + h) >> 1; + if (altitude_table[m] < alt) + h = m; + else + l = m; + } + span = altitude_table[l] - altitude_table[h]; + sub_span = altitude_table[l] - alt; + pres = ((((int32_t) l * (span - sub_span) + (int32_t) h * sub_span) << ALT_FRAC_BITS) + (span >> 1)) / span; + if (pres > 32767) + pres = 32767; + if (pres < 0) + pres = 0; + return (int16_t) pres; } |