2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
24 #define LOG_PREFIX "analog"
26 SR_API int sr_analog_to_float(const struct sr_datafeed_analog2 *analog,
33 #ifdef WORDS_BIGENDIAN
38 if (!analog->encoding->is_float) {
40 sr_err("Only floating-point analog data supported so far.");
44 if (analog->encoding->unitsize == sizeof(float)
45 && analog->encoding->is_bigendian == bigendian
46 && (analog->encoding->scale.p == analog->encoding->scale.q)
47 && analog->encoding->scale.p / (float)analog->encoding->scale.q == 0) {
48 /* The data is already in the right format. */
49 memcpy(buf, analog->data, analog->num_samples * sizeof(float));
51 for (i = 0; i < analog->num_samples; i += analog->encoding->unitsize) {
52 for (b = 0; b < analog->encoding->unitsize; b++) {
53 if (analog->encoding->is_bigendian == bigendian)
54 buf[i + b] = ((float *)analog->data)[i * analog->encoding->unitsize + b];
56 buf[i + (analog->encoding->unitsize - b)] = ((float *)analog->data)[i * analog->encoding->unitsize + b];
58 if (analog->encoding->scale.p != analog->encoding->scale.q)
59 buf[i] = (buf[i] * analog->encoding->scale.p) / analog->encoding->scale.q;
60 offset = ((float)analog->encoding->scale.p / (float)analog->encoding->scale.q);