]> sigrok.org Git - pulseview.git/blob - pv/util.hpp
AnalogSegment: Calculate min/max also for small sample sizes
[pulseview.git] / pv / util.hpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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 2 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
20 #ifndef PULSEVIEW_UTIL_HPP
21 #define PULSEVIEW_UTIL_HPP
22
23 #include <cmath>
24
25 #ifndef Q_MOC_RUN
26 #include <boost/multiprecision/cpp_dec_float.hpp>
27 #endif
28
29 #include <QMetaType>
30 #include <QString>
31
32 namespace pv {
33 namespace util {
34
35 enum class TimeUnit {
36         Time = 1,
37         Samples = 2
38 };
39
40 enum class SIPrefix {
41         unspecified = -1,
42         yocto, zepto,
43         atto, femto, pico,
44         nano, micro, milli,
45         none,
46         kilo, mega, giga,
47         tera, peta, exa,
48         zetta, yotta
49 };
50
51 /// Returns the exponent that corresponds to a given prefix.
52 int exponent(SIPrefix prefix);
53
54 /// Timestamp type providing yoctosecond resolution.
55 typedef boost::multiprecision::number<
56         boost::multiprecision::cpp_dec_float<24>,
57         boost::multiprecision::et_off> Timestamp;
58
59 /**
60  * Formats a given timestamp with the specified SI prefix.
61  *
62  * If 'prefix' is left 'unspecified', the function chooses a prefix so that
63  * the value in front of the decimal point is between 1 and 999.
64  *
65  * The default value "s" for the unit argument makes the most sense when
66  * formatting time values, but a different value can be given if the function
67  * is reused to format a value of another quantity.
68  *
69  * @param t The value to format.
70  * @param prefix The SI prefix to use.
71  * @param precision The number of digits after the decimal separator.
72  * @param unit The unit of quantity.
73  * @param sign Whether or not to add a sign also for positive numbers.
74  *
75  * @return The formatted value.
76  */
77 QString format_time_si(
78         const Timestamp& v,
79         SIPrefix prefix = SIPrefix::unspecified,
80         unsigned precision = 0,
81         QString unit = "s",
82         bool sign = true);
83
84 /**
85  * Wrapper around 'format_time_si()' that interprets the given 'precision'
86  * value as the number of decimal places if the timestamp would be formatted
87  * without a SI prefix (using 'SIPrefix::none') and adjusts the precision to
88  * match the given 'prefix'
89  *
90  * @param t The value to format.
91  * @param prefix The SI prefix to use.
92  * @param precision The number of digits after the decimal separator if the
93  *        'prefix' would be 'SIPrefix::none', see above for more information.
94  * @param unit The unit of quantity.
95  * @param sign Whether or not to add a sign also for positive numbers.
96  *
97  * @return The formatted value.
98  */
99 QString format_time_si_adjusted(
100         const Timestamp& t,
101         SIPrefix prefix,
102         unsigned precision = 0,
103         QString unit = "s",
104         bool sign = true);
105
106 /**
107  * Formats the given timestamp using "[+-]DD:HH:MM:SS.mmm uuu nnn ppp..." format.
108  *
109  * "DD" and "HH" are left out if they would be "00" (but if "DD" is generated,
110  * "HH" is also always generated. The "MM:SS" part is always produced, the
111  * number of subsecond digits can be influenced using the 'precision' parameter.
112  *
113  * @param t The value to format.
114  * @param precision The number of digits after the decimal separator.
115  * @param sign Whether or not to add a sign also for positive numbers.
116  *
117  * @return The formatted value.
118  */
119 QString format_time_minutes(
120         const Timestamp& t,
121         signed precision = 0,
122         bool sign = true);
123
124 } // namespace util
125 } // namespace pv
126
127 Q_DECLARE_METATYPE(pv::util::Timestamp)
128
129 #endif // PULSEVIEW_UTIL_HPP