satdump/src-core/common/utils.h

205 lines
5.1 KiB
C
Raw Normal View History

2021-03-31 12:16:38 +02:00
#pragma once
#include <cstdint>
2021-05-23 17:14:46 +02:00
#include <algorithm>
#include <map>
#include <string>
#include <vector>
2021-08-15 16:47:44 +02:00
#include <sstream>
#include <climits>
2022-02-24 19:33:58 +01:00
#include <fstream>
2022-04-16 08:10:25 +02:00
#include <optional>
2023-12-10 13:03:59 +01:00
#include <chrono>
2021-05-23 17:14:46 +02:00
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);
2021-05-23 17:14:46 +02:00
template <class InputIt, class T = typename std::iterator_traits<InputIt>::value_type>
T most_common(InputIt begin, InputIt end)
{
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);
}
2021-08-15 16:47:44 +02:00
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;
}
2021-11-12 19:50:05 +01:00
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]);
}
2022-04-16 08:10:25 +02:00
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;
}
2022-05-22 15:25:15 +02:00
template <typename T>
double avg_overflowless_timestamps(std::vector<T> const &v)
{
T n = 0;
double mean = 0.0;
for (auto x : v)
{
if (x != -1)
{
double delta = x - mean;
mean += delta / ++n;
}
}
return mean;
}
2022-02-21 23:32:11 +01:00
std::vector<std::string> splitString(std::string input, char del);
template <typename T>
inline bool getBit(T &data, int &bit)
{
return (data >> bit) & 1;
2022-02-24 19:33:58 +01:00
}
2022-03-10 19:56:37 +01:00
bool isStringPresent(std::string searched, std::string keyword);
2022-02-24 19:33:58 +01:00
// Return filesize
uint64_t getFilesize(std::string filepath);
2022-04-16 08:10:25 +02:00
// 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);
2022-04-21 16:10:00 +02:00
std::string timestamp_to_string(double timestamp);
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;
2022-05-23 01:11:11 +02:00
}
double get_median(std::vector<double> values);
extern "C" time_t mktime_utc(const struct tm *timeinfo_utc); // Already in libpredict!
2022-12-22 13:08:09 +01:00
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;
2022-12-27 21:02:36 +01:00
}
2023-02-15 02:47:32 +01:00
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;
}
2023-09-09 22:35:30 +02:00
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]);
}
}
2023-12-10 13:03:59 +01:00
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;
}
2023-01-13 23:15:55 +01:00
std::string loadFileToString(std::string path);
2023-01-12 16:36:27 +01:00
std::string ws2s(const std::wstring &wstr);
2023-01-13 23:15:55 +01:00
std::wstring s2ws(const std::string &str);