]> sigrok.org Git - libsigrok.git/blame - src/analog.c
analog: use correct unit for dBm/dBV
[libsigrok.git] / src / analog.c
CommitLineData
fb019a0e
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
6ec6c43b 20#include <config.h>
c2a25ebb
BV
21#include <stdio.h>
22#include <stdint.h>
fb019a0e 23#include <string.h>
c2a25ebb 24#include <ctype.h>
962172e4 25#include <math.h>
c1aae900 26#include <libsigrok/libsigrok.h>
fb019a0e
BV
27#include "libsigrok-internal.h"
28
e00b3f58 29/** @cond PRIVATE */
fb019a0e 30#define LOG_PREFIX "analog"
e00b3f58
UH
31/** @endcond */
32
33/**
34 * @file
35 *
36 * Handling and converting analog data.
37 */
38
39/**
40 * @defgroup grp_analog Analog data handling
41 *
42 * Handling and converting analog data.
43 *
44 * @{
45 */
fb019a0e 46
a5892391
BV
47struct unit_mq_string {
48 uint64_t value;
2c240774 49 const char *str;
a5892391
BV
50};
51
ca7dbb56 52/* Please use the same order as in enum sr_unit (libsigrok.h). */
a5892391
BV
53static struct unit_mq_string unit_strings[] = {
54 { SR_UNIT_VOLT, "V" },
55 { SR_UNIT_AMPERE, "A" },
56 { SR_UNIT_OHM, "\xe2\x84\xa6" },
57 { SR_UNIT_FARAD, "F" },
a5892391
BV
58 { SR_UNIT_KELVIN, "K" },
59 { SR_UNIT_CELSIUS, "\xc2\xb0""C" },
60 { SR_UNIT_FAHRENHEIT, "\xc2\xb0""F" },
61 { SR_UNIT_HERTZ, "Hz" },
62 { SR_UNIT_PERCENTAGE, "%" },
f7bcc686 63 { SR_UNIT_BOOLEAN, "" },
a5892391
BV
64 { SR_UNIT_SECOND, "s" },
65 { SR_UNIT_SIEMENS, "S" },
cdc31195
AJ
66 { SR_UNIT_DECIBEL_MW, "dBm" },
67 { SR_UNIT_DECIBEL_VOLT, "dBV" },
f7bcc686 68 { SR_UNIT_UNITLESS, "" },
a5892391
BV
69 { SR_UNIT_DECIBEL_SPL, "dB" },
70 { SR_UNIT_CONCENTRATION, "ppm" },
71 { SR_UNIT_REVOLUTIONS_PER_MINUTE, "RPM" },
72 { SR_UNIT_VOLT_AMPERE, "VA" },
73 { SR_UNIT_WATT, "W" },
74 { SR_UNIT_WATT_HOUR, "Wh" },
75 { SR_UNIT_METER_SECOND, "m/s" },
76 { SR_UNIT_HECTOPASCAL, "hPa" },
77 { SR_UNIT_HUMIDITY_293K, "%rF" },
78 { SR_UNIT_DEGREE, "\xc2\xb0" },
f7bcc686
UH
79 { SR_UNIT_HENRY, "H" },
80 { SR_UNIT_GRAM, "g" },
81 { SR_UNIT_CARAT, "ct" },
82 { SR_UNIT_OUNCE, "oz" },
83 { SR_UNIT_TROY_OUNCE, "oz t" },
84 { SR_UNIT_POUND, "lb" },
85 { SR_UNIT_PENNYWEIGHT, "dwt" },
86 { SR_UNIT_GRAIN, "gr" },
87 { SR_UNIT_TAEL, "tael" },
88 { SR_UNIT_MOMME, "momme" },
89 { SR_UNIT_TOLA, "tola" },
90 { SR_UNIT_PIECE, "pcs" },
a5892391
BV
91 ALL_ZERO
92};
93
ca7dbb56 94/* Please use the same order as in enum sr_mqflag (libsigrok.h). */
a5892391 95static struct unit_mq_string mq_strings[] = {
a5892391
BV
96 { SR_MQFLAG_AC, " AC" },
97 { SR_MQFLAG_DC, " DC" },
98 { SR_MQFLAG_RMS, " RMS" },
99 { SR_MQFLAG_DIODE, " DIODE" },
100 { SR_MQFLAG_HOLD, " HOLD" },
101 { SR_MQFLAG_MAX, " MAX" },
102 { SR_MQFLAG_MIN, " MIN" },
103 { SR_MQFLAG_AUTORANGE, " AUTO" },
104 { SR_MQFLAG_RELATIVE, " REL" },
f7bcc686
UH
105 { SR_MQFLAG_SPL_FREQ_WEIGHT_A, "(A)" },
106 { SR_MQFLAG_SPL_FREQ_WEIGHT_C, "(C)" },
107 { SR_MQFLAG_SPL_FREQ_WEIGHT_Z, "(Z)" },
108 { SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT, "(SPL)" },
109 { SR_MQFLAG_SPL_TIME_WEIGHT_S, " S" },
110 { SR_MQFLAG_SPL_TIME_WEIGHT_F, " F" },
111 { SR_MQFLAG_SPL_LAT, " LAT" },
112 /* Not a standard function for SLMs, so this is a made-up notation. */
113 { SR_MQFLAG_SPL_PCT_OVER_ALARM, "%oA" },
114 { SR_MQFLAG_DURATION, " DURATION" },
a5892391
BV
115 { SR_MQFLAG_AVG, " AVG" },
116 { SR_MQFLAG_REFERENCE, " REF" },
f7bcc686 117 { SR_MQFLAG_UNSTABLE, " UNSTABLE" },
6d5cd3bd 118 { SR_MQFLAG_FOUR_WIRE, " 4-WIRE" },
a5892391
BV
119 ALL_ZERO
120};
121
edb691fc 122SR_PRIV int sr_analog_init(struct sr_datafeed_analog *analog,
41caa319
AJ
123 struct sr_analog_encoding *encoding,
124 struct sr_analog_meaning *meaning,
125 struct sr_analog_spec *spec,
126 int digits)
127{
128 memset(analog, 0, sizeof(*analog));
129 memset(encoding, 0, sizeof(*encoding));
130 memset(meaning, 0, sizeof(*meaning));
131 memset(spec, 0, sizeof(*spec));
132
133 analog->encoding = encoding;
134 analog->meaning = meaning;
135 analog->spec = spec;
136
137 encoding->unitsize = sizeof(float);
138 encoding->is_float = TRUE;
139#ifdef WORDS_BIGENDIAN
140 encoding->is_bigendian = TRUE;
141#else
142 encoding->is_bigendian = FALSE;
143#endif
144 encoding->digits = digits;
145 encoding->is_digits_decimal = TRUE;
146 encoding->scale.p = 1;
147 encoding->scale.q = 1;
148 encoding->offset.p = 0;
149 encoding->offset.q = 1;
150
151 spec->spec_digits = digits;
152
153 return SR_OK;
154}
155
22fb1bff
UH
156/**
157 * Convert an analog datafeed payload to an array of floats.
158 *
8dc423b0
UH
159 * Sufficient memory for outbuf must have been pre-allocated by the caller,
160 * who is also responsible for freeing it when no longer needed.
161 *
22fb1bff
UH
162 * @param[in] analog The analog payload to convert. Must not be NULL.
163 * analog->data, analog->meaning, and analog->encoding
164 * must not be NULL.
165 * @param[out] outbuf Memory where to store the result. Must not be NULL.
166 *
22fb1bff
UH
167 * @retval SR_OK Success.
168 * @retval SR_ERR Unsupported encoding.
169 * @retval SR_ERR_ARG Invalid argument.
170 *
171 * @since 0.4.0
172 */
edb691fc 173SR_API int sr_analog_to_float(const struct sr_datafeed_analog *analog,
4b4fdeea 174 float *outbuf)
fb019a0e
BV
175{
176 float offset;
5cee3d08 177 unsigned int b, i, count;
fb019a0e 178 gboolean bigendian;
5cee3d08
UH
179
180 if (!analog || !(analog->data) || !(analog->meaning)
181 || !(analog->encoding) || !outbuf)
182 return SR_ERR_ARG;
183
184 count = analog->num_samples * g_slist_length(analog->meaning->channels);
fb019a0e
BV
185
186#ifdef WORDS_BIGENDIAN
187 bigendian = TRUE;
188#else
189 bigendian = FALSE;
190#endif
8dc423b0 191
fb019a0e 192 if (!analog->encoding->is_float) {
4d376e08
SB
193 float offset = analog->encoding->offset.p / (float)analog->encoding->offset.q;
194 float scale = analog->encoding->scale.p / (float)analog->encoding->scale.q;
195 gboolean is_signed = analog->encoding->is_signed;
196 gboolean is_bigendian = analog->encoding->is_bigendian;
197 int8_t *data8 = (int8_t *)(analog->data);
198 int16_t *data16 = (int16_t *)(analog->data);
199 int32_t *data32 = (int32_t *)(analog->data);
200
201 switch (analog->encoding->unitsize) {
202 case 1:
203 if (is_signed) {
204 for (unsigned int i = 0; i < count; i++) {
205 outbuf[i] = scale * data8[i];
206 outbuf[i] += offset;
207 }
208 } else {
209 for (unsigned int i = 0; i < count; i++) {
210 outbuf[i] = scale * R8(data8 + i);
211 outbuf[i] += offset;
212 }
213 }
214 break;
215 case 2:
216 if (is_signed && is_bigendian) {
217 for (unsigned int i = 0; i < count; i++) {
218 outbuf[i] = scale * RB16S(&data16[i]);
219 outbuf[i] += offset;
220 }
221 } else if (is_bigendian) {
222 for (unsigned int i = 0; i < count; i++) {
223 outbuf[i] = scale * RB16(&data16[i]);
224 outbuf[i] += offset;
225 }
226 } else if (is_signed) {
227 for (unsigned int i = 0; i < count; i++) {
228 outbuf[i] = scale * RL16S(&data16[i]);
229 outbuf[i] += offset;
230 }
231 } else {
232 for (unsigned int i = 0; i < count; i++) {
233 outbuf[i] = scale * RL16(&data16[i]);
234 outbuf[i] += offset;
235 }
236 }
237 break;
238 case 4:
239 if (is_signed && is_bigendian) {
240 for (unsigned int i = 0; i < count; i++) {
241 outbuf[i] = scale * RB32S(&data32[i]);
242 outbuf[i] += offset;
243 }
244 } else if (is_bigendian) {
245 for (unsigned int i = 0; i < count; i++) {
246 outbuf[i] = scale * RB32(&data32[i]);
247 outbuf[i] += offset;
248 }
249 } else if (is_signed) {
250 for (unsigned int i = 0; i < count; i++) {
251 outbuf[i] = scale * RL32S(&data32[i]);
252 outbuf[i] += offset;
253 }
254 } else {
255 for (unsigned int i = 0; i < count; i++) {
256 outbuf[i] = scale * RL32(&data32[i]);
257 outbuf[i] += offset;
258 }
259 }
260 break;
261 default:
8dc423b0
UH
262 sr_err("Unsupported unit size '%d' for analog-to-float"
263 " conversion.", analog->encoding->unitsize);
4d376e08
SB
264 return SR_ERR;
265 }
266 return SR_OK;
fb019a0e
BV
267 }
268
269 if (analog->encoding->unitsize == sizeof(float)
270 && analog->encoding->is_bigendian == bigendian
b07a1b04
ML
271 && analog->encoding->scale.p == 1
272 && analog->encoding->scale.q == 1
4b4fdeea 273 && analog->encoding->offset.p / (float)analog->encoding->offset.q == 0) {
fb019a0e 274 /* The data is already in the right format. */
7d65dd3a 275 memcpy(outbuf, analog->data, count * sizeof(float));
fb019a0e 276 } else {
7d65dd3a 277 for (i = 0; i < count; i += analog->encoding->unitsize) {
fb019a0e
BV
278 for (b = 0; b < analog->encoding->unitsize; b++) {
279 if (analog->encoding->is_bigendian == bigendian)
3e277549
ML
280 ((uint8_t *)outbuf)[i + b] =
281 ((uint8_t *)analog->data)[i * analog->encoding->unitsize + b];
fb019a0e 282 else
3e277549
ML
283 ((uint8_t *)outbuf)[i + (analog->encoding->unitsize - b)] =
284 ((uint8_t *)analog->data)[i * analog->encoding->unitsize + b];
fb019a0e 285 }
b07a1b04
ML
286 if (analog->encoding->scale.p != 1
287 || analog->encoding->scale.q != 1)
4b4fdeea
BV
288 outbuf[i] = (outbuf[i] * analog->encoding->scale.p) / analog->encoding->scale.q;
289 offset = ((float)analog->encoding->offset.p / (float)analog->encoding->offset.q);
290 outbuf[i] += offset;
fb019a0e
BV
291 }
292 }
293
294 return SR_OK;
295}
c2a25ebb 296
962172e4
AJ
297/**
298 * Scale a float value to the appropriate SI prefix.
299 *
300 * @param[in,out] value The float value to convert to appropriate SI prefix.
301 * @param[in,out] digits The number of significant decimal digits in value.
302 *
303 * @return The SI prefix to which value was scaled, as a printable string.
304 *
305 * @since 0.5.0
306 */
307SR_API const char *sr_analog_si_prefix(float *value, int *digits)
308{
309#define NEG_PREFIX_COUNT 5 /* number of prefixes below unity */
310#define POS_PREFIX_COUNT (int)(ARRAY_SIZE(prefixes) - NEG_PREFIX_COUNT - 1)
8dc423b0 311 static const char *prefixes[] = { "f", "p", "n", "µ", "m", "", "k", "M", "G", "T" };
962172e4 312
8dc423b0 313 if (!value || !digits || isnan(*value))
962172e4
AJ
314 return prefixes[NEG_PREFIX_COUNT];
315
316 float logval = log10f(fabsf(*value));
317 int prefix = (logval / 3) - (logval < 1);
318
8dc423b0
UH
319 if (prefix < -NEG_PREFIX_COUNT)
320 prefix = -NEG_PREFIX_COUNT;
321 if (3 * prefix < -*digits)
322 prefix = (-*digits + 2 * (*digits < 0)) / 3;
323 if (prefix > POS_PREFIX_COUNT)
324 prefix = POS_PREFIX_COUNT;
962172e4
AJ
325
326 *value *= powf(10, -3 * prefix);
327 *digits += 3 * prefix;
8dc423b0 328
962172e4
AJ
329 return prefixes[prefix + NEG_PREFIX_COUNT];
330}
331
22fb1bff 332/**
a5892391
BV
333 * Convert the unit/MQ/MQ flags in the analog struct to a string.
334 *
8dc423b0
UH
335 * The string is allocated by the function and must be freed by the caller
336 * after use by calling g_free().
337 *
22fb1bff
UH
338 * @param[in] analog Struct containing the unit, MQ and MQ flags.
339 * Must not be NULL. analog->meaning must not be NULL.
340 * @param[out] result Pointer to store result. Must not be NULL.
a24da9a8 341 *
22fb1bff
UH
342 * @retval SR_OK Success.
343 * @retval SR_ERR_ARG Invalid argument.
a5892391
BV
344 *
345 * @since 0.4.0
346 */
edb691fc 347SR_API int sr_analog_unit_to_string(const struct sr_datafeed_analog *analog,
a24da9a8 348 char **result)
a5892391 349{
a24da9a8 350 int i;
5cee3d08
UH
351 GString *buf;
352
353 if (!analog || !(analog->meaning) || !result)
354 return SR_ERR_ARG;
355
356 buf = g_string_new(NULL);
a5892391 357
a5892391
BV
358 for (i = 0; unit_strings[i].value; i++) {
359 if (analog->meaning->unit == unit_strings[i].value) {
a24da9a8 360 g_string_assign(buf, unit_strings[i].str);
a5892391
BV
361 break;
362 }
363 }
364
365 /* More than one MQ flag may apply. */
a24da9a8
ML
366 for (i = 0; mq_strings[i].value; i++)
367 if (analog->meaning->mqflags & mq_strings[i].value)
368 g_string_append(buf, mq_strings[i].str);
369
370 *result = buf->str;
371 g_string_free(buf, FALSE);
a5892391
BV
372
373 return SR_OK;
374}
375
22fb1bff 376/**
90cefe0c
BV
377 * Set sr_rational r to the given value.
378 *
22fb1bff
UH
379 * @param[out] r Rational number struct to set. Must not be NULL.
380 * @param[in] p Numerator.
381 * @param[in] q Denominator.
382 *
383 * @since 0.4.0
90cefe0c 384 */
53e5d3d1 385SR_API void sr_rational_set(struct sr_rational *r, int64_t p, uint64_t q)
90cefe0c 386{
5cee3d08
UH
387 if (!r)
388 return;
389
90cefe0c
BV
390 r->p = p;
391 r->q = q;
392}
393
bdba3626
SB
394#ifndef HAVE___INT128_T
395struct sr_int128_t {
396 int64_t high;
397 uint64_t low;
398};
399
400struct sr_uint128_t {
401 uint64_t high;
402 uint64_t low;
403};
404
405static void mult_int64(struct sr_int128_t *res, const int64_t a,
406 const int64_t b)
407{
408 uint64_t t1, t2, t3, t4;
409
410 t1 = (UINT32_MAX & a) * (UINT32_MAX & b);
411 t2 = (UINT32_MAX & a) * (b >> 32);
412 t3 = (a >> 32) * (UINT32_MAX & b);
413 t4 = (a >> 32) * (b >> 32);
414
415 res->low = t1 + (t2 << 32) + (t3 << 32);
416 res->high = (t1 >> 32) + (uint64_t)((uint32_t)(t2)) + (uint64_t)((uint32_t)(t3));
417 res->high >>= 32;
418 res->high += ((int64_t)t2 >> 32) + ((int64_t)t3 >> 32) + t4;
419}
420
421static void mult_uint64(struct sr_uint128_t *res, const uint64_t a,
422 const uint64_t b)
423{
424 uint64_t t1, t2, t3, t4;
425
426 // (x1 + x2) * (y1 + y2) = x1*y1 + x1*y2 + x2*y1 + x2*y2
427 t1 = (UINT32_MAX & a) * (UINT32_MAX & b);
428 t2 = (UINT32_MAX & a) * (b >> 32);
429 t3 = (a >> 32) * (UINT32_MAX & b);
430 t4 = (a >> 32) * (b >> 32);
431
432 res->low = t1 + (t2 << 32) + (t3 << 32);
433 res->high = (t1 >> 32) + (uint64_t)((uint32_t)(t2)) + (uint64_t)((uint32_t)(t3));
434 res->high >>= 32;
435 res->high += ((int64_t)t2 >> 32) + ((int64_t)t3 >> 32) + t4;
436}
437#endif
438
439/**
8dc423b0 440 * Compare two sr_rational for equality.
bdba3626 441 *
8dc423b0 442 * The values are compared for numerical equality, i.e. 2/10 == 1/5.
bdba3626 443 *
8dc423b0
UH
444 * @param[in] a First value.
445 * @param[in] b Second value.
bdba3626 446 *
8dc423b0
UH
447 * @retval 1 if both values are equal.
448 * @retval 0 Otherwise.
bdba3626
SB
449 *
450 * @since 0.5.0
451 */
452SR_API int sr_rational_eq(const struct sr_rational *a, const struct sr_rational *b)
453{
454#ifdef HAVE___INT128_T
455 __int128_t m1, m2;
456
457 /* p1/q1 = p2/q2 <=> p1*q2 = p2*q1 */
458 m1 = ((__int128_t)(b->p)) * ((__uint128_t)a->q);
459 m2 = ((__int128_t)(a->p)) * ((__uint128_t)b->q);
460
461 return (m1 == m2);
462
463#else
464 struct sr_int128_t m1, m2;
465
466 mult_int64(&m1, a->q, b->p);
467 mult_int64(&m2, a->p, b->q);
468
469 return (m1.high == m2.high) && (m1.low == m2.low);
470#endif
471}
472
ee1b6054 473/**
8dc423b0 474 * Multiply two sr_rational.
ee1b6054
SB
475 *
476 * The resulting nominator/denominator are reduced if the result would not fit
477 * otherwise. If the resulting nominator/denominator are relatively prime,
478 * this may not be possible.
479 *
8dc423b0
UH
480 * It is safe to use the same variable for result and input values.
481 *
482 * @param[in] a First value.
483 * @param[in] b Second value.
484 * @param[out] res Result.
17d5a11c 485 *
ee1b6054 486 * @retval SR_OK Success.
8dc423b0 487 * @retval SR_ERR_ARG Resulting value too large.
ee1b6054
SB
488 *
489 * @since 0.5.0
490 */
491SR_API int sr_rational_mult(struct sr_rational *res, const struct sr_rational *a,
492 const struct sr_rational *b)
493{
494#ifdef HAVE___INT128_T
495 __int128_t p;
496 __uint128_t q;
497
498 p = (__int128_t)(a->p) * (__int128_t)(b->p);
499 q = (__uint128_t)(a->q) * (__uint128_t)(b->q);
500
501 if ((p > INT64_MAX) || (p < INT64_MIN) || (q > UINT64_MAX)) {
502 while (!((p & 1) || (q & 1))) {
503 p /= 2;
504 q /= 2;
505 }
506 }
507
508 if ((p > INT64_MAX) || (p < INT64_MIN) || (q > UINT64_MAX)) {
509 // TODO: determine gcd to do further reduction
510 return SR_ERR_ARG;
511 }
512
513 res->p = (int64_t)(p);
514 res->q = (uint64_t)(q);
515
516 return SR_OK;
517
518#else
519 struct sr_int128_t p;
520 struct sr_uint128_t q;
521
522 mult_int64(&p, a->p, b->p);
523 mult_uint64(&q, a->q, b->q);
524
525 while (!(p.low & 1) && !(q.low & 1)) {
526 p.low /= 2;
8dc423b0
UH
527 if (p.high & 1)
528 p.low |= (1ll << 63);
ee1b6054
SB
529 p.high >>= 1;
530 q.low /= 2;
8dc423b0
UH
531 if (q.high & 1)
532 q.low |= (1ll << 63);
ee1b6054
SB
533 q.high >>= 1;
534 }
535
536 if (q.high)
537 return SR_ERR_ARG;
538 if ((p.high >= 0) && (p.low > INT64_MAX))
539 return SR_ERR_ARG;
540 if (p.high < -1)
541 return SR_ERR_ARG;
542
543 res->p = (int64_t)p.low;
544 res->q = q.low;
545
546 return SR_OK;
547#endif
548}
549
17d5a11c 550/**
8dc423b0 551 * Divide rational a by rational b.
17d5a11c
SB
552 *
553 * The resulting nominator/denominator are reduced if the result would not fit
554 * otherwise. If the resulting nominator/denominator are relatively prime,
555 * this may not be possible.
556 *
8dc423b0
UH
557 * It is safe to use the same variable for result and input values.
558 *
559 * @param[in] num Numerator.
560 * @param[in] div Divisor.
561 * @param[out] res Result.
17d5a11c
SB
562 *
563 * @retval SR_OK Success.
8dc423b0
UH
564 * @retval SR_ERR_ARG Division by zero.
565 * @retval SR_ERR_ARG Denominator of divisor too large.
566 * @retval SR_ERR_ARG Resulting value too large.
17d5a11c
SB
567 *
568 * @since 0.5.0
569 */
570SR_API int sr_rational_div(struct sr_rational *res, const struct sr_rational *num,
571 const struct sr_rational *div)
572{
573 struct sr_rational t;
574
575 if (div->q > INT64_MAX)
576 return SR_ERR_ARG;
577 if (div->p == 0)
578 return SR_ERR_ARG;
579
580 if (div->p > 0) {
581 t.p = div->q;
582 t.q = div->p;
583 } else {
584 t.p = -div->q;
585 t.q = -div->p;
586 }
587
588 return sr_rational_mult(res, num, &t);
589}
590
e00b3f58 591/** @} */