1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#pragma once
#include <cstdint>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <sstream>
#include <climits>
#include <fstream>
#include <optional>
#include <chrono>
#ifdef _MSC_VER
#define timegm _mkgmtime
#endif
void char_array_to_uchar(int8_t *in, uint8_t *out, int nsamples);
void signed_soft_to_unsigned(int8_t *in, uint8_t *out, int nsamples);
template <class InputIt, class T = typename std::iterator_traits<InputIt>::value_type>
T most_common(InputIt begin, InputIt end, T def)
{
if (begin == end)
return def;
std::map<T, int> counts;
for (InputIt it = begin; it != end; ++it)
{
if (counts.find(*it) != counts.end())
++counts[*it];
else
counts[*it] = 1;
}
return std::max_element(counts.begin(), counts.end(), [](const std::pair<T, int> &pair1, const std::pair<T, int> &pair2)
{ return pair1.second < pair2.second; })
->first;
}
template <class InputIt, class T = typename std::iterator_traits<InputIt>::value_type>
T average_common(InputIt begin, InputIt end)
{
T avg = 0;
size_t count = 0;
for (InputIt it = begin; it != end; ++it)
{
avg += *it;
count++;
}
return avg / T(count);
}
template <typename T>
std::string to_string_with_precision(const T a_value, const int n = 6)
{
std::ostringstream out;
out.precision(n);
out << std::fixed << a_value;
return out.str();
}
template <typename T>
T swap_endian(T u)
{
union
{
T u;
unsigned char u8[sizeof(T)];
} source, dest;
source.u = u;
for (size_t k = 0; k < sizeof(T); k++)
dest.u8[k] = source.u8[sizeof(T) - k - 1];
return dest.u;
}
template <typename T>
int percentile(T *array, int size, float percentile)
{
float number_percent = (size + 1) * percentile / 100.0f;
if (number_percent == 1)
return array[0];
else if (number_percent == size)
return array[size - 1];
else
return array[(int)number_percent - 1] + (number_percent - (int)number_percent) * (array[(int)number_percent] - array[(int)number_percent - 1]);
}
template <typename T>
double avg_overflowless(std::vector<T> const &v)
{
T n = 0;
double mean = 0.0;
for (auto x : v)
{
double delta = x - mean;
mean += delta / ++n;
}
return mean;
}
std::vector<std::string> splitString(std::string input, char del);
template <typename T>
inline bool getBit(T &data, int &bit)
{
return (data >> bit) & 1;
}
bool isStringPresent(std::string searched, std::string keyword);
// Return filesize
uint64_t getFilesize(std::string filepath);
// cURL helper function
size_t curl_write_std_string(void* contents, size_t size, size_t nmemb, std::string* s);
// Perform a HTTP Request on the provided URL and return the result as a string
int perform_http_request(std::string url, std::string &result, std::string added_header = "", float *progress = nullptr);
// Perform a HTTP Request on the provided URL and return the result as a string, with POST data
int perform_http_request_post(std::string url_str, std::string &result, std::string post_req, std::string added_header = "");
std::string timestamp_to_string(double timestamp, bool local = false);
inline std::vector<float> double_buffer_to_float(double *ptr, int size)
{
std::vector<float> ret;
for (int i = 0; i < size; i++)
ret.push_back(ptr[i]);
return ret;
}
double get_median(std::vector<double> values);
template <typename T>
std::vector<uint8_t> unsigned_to_bitvec(T v)
{
std::vector<uint8_t> c;
for (int s = (sizeof(T) * 8) - 1; s >= 0; s--)
c.push_back((v >> s) & 1);
return c;
}
template <typename T>
std::vector<T> oversample_vector(std::vector<T> data, int oversampling)
{
std::vector<T> r;
for (T &v : data)
for (int i = 0; i < oversampling; i++)
r.push_back(v);
return r;
}
template <typename... T>
std::string svformat(const char *fmt, T &&...args)
{
// Allocate a buffer on the stack that's big enough for us almost
// all the time.
size_t size = 1024;
std::vector<char> buf;
buf.resize(size);
// Try to vsnprintf into our buffer.
size_t needed = snprintf((char *)&buf[0], size, fmt, args...);
// NB. On Windows, vsnprintf returns -1 if the string didn't fit the
// buffer. On Linux & OSX, it returns the length it would have needed.
if (needed <= size)
{
// It fit fine the first time, we're done.
return std::string(&buf[0]);
}
else
{
// vsnprintf reported that it wanted to write more characters
// than we allotted. So do a malloc of the right size and try again.
// This doesn't happen very often if we chose our initial size
// well.
size = needed;
buf.resize(size);
needed = snprintf((char *)&buf[0], size, fmt, args...);
return std::string(&buf[0]);
}
}
inline double getTime()
{
auto time = std::chrono::system_clock::now();
auto since_epoch = time.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(since_epoch);
return millis.count() / 1e3;
}
std::string loadFileToString(std::string path);
std::string ws2s(const std::wstring &wstr);
std::wstring s2ws(const std::string &str);
std::string prepareAutomatedPipelineFolder(time_t timevalue, double frequency, std::string pipeline_name, std::string folder = "");
std::string prepareBasebandFileName(double timeValue_precise, uint64_t samplerate, uint64_t frequency);
void hsv_to_rgb(float h, float s, float v, uint8_t *rgb);
inline int ensureIs2Multiple(int v)
{
if (v % 2 == 1)
v++;
return v;
}
|