##// END OF EJS Templates
Creates method to compute resolutions of a dataset...
Creates method to compute resolutions of a dataset This method will be used to compute y-axis resolution of a spectrogram

File last commit:

r1032:01201e853d86
r1032:01201e853d86
Show More
DataSeriesUtils.h
139 lines | 6.2 KiB | text/x-c | CLexer
#ifndef SCIQLOP_DATASERIESUTILS_H
#define SCIQLOP_DATASERIESUTILS_H
#include "CoreGlobal.h"
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(LOG_DataSeriesUtils)
/**
* Utility class with methods for data series
*/
struct SCIQLOP_CORE_EXPORT DataSeriesUtils {
/**
* Represents a resolution used to generate the data of a mesh on the x-axis or in Y.
*
* A resolution is represented by a value and flag indicating if it's in the logarithmic scale
* @sa Mesh
*/
struct Resolution {
double m_Val{std::numeric_limits<double>::quiet_NaN()};
bool m_Logarithmic{false};
};
/**
* Processes data from a data series to complete the data holes with a fill value.
*
* A data hole is determined by the resolution passed in parameter: if, between two continuous
* data on the x-axis, the difference between these data is greater than the resolution, then
* there is one or more holes between them. The holes are filled by adding:
* - for the x-axis, new data corresponding to the 'step resolution' starting from the first
* data;
* - for values, a default value (fill value) for each new data added on the x-axis.
*
* For example, with :
* - xAxisData = [0, 1, 5, 7, 14 ]
* - valuesData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (two components per x-axis data)
* - fillValue = NaN
* - and resolution = 2;
*
* For the x axis, we calculate as data holes: [3, 9, 11, 13]. These holes are added to the
* x-axis data, and NaNs (two per x-axis data) are added to the values:
* => xAxisData = [0, 1, 3, 5, 7, 9, 11, 13, 14 ]
* => valuesData = [0, 1, 2, 3, NaN, NaN, 4, 5, 6, 7, NaN, NaN, NaN, NaN, NaN, NaN, 8, 9]
*
* It is also possible to set bounds for the data series. If these bounds are defined and exceed
* the limits of the data series, data holes are added to the series at the beginning and/or the
* end.
*
* The generation of data holes at the beginning/end of the data series is performed starting
* from the x-axis series limit and adding data holes at each 'resolution step' as long as the
* new bound is not reached.
*
* For example, with :
* - xAxisData = [3, 4, 5, 6, 7 ]
* - valuesData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
* - fillValue = NaN
* - minBound = 0
* - maxBound = 12
* - and resolution = 2;
*
* => Starting from 3 and decreasing 2 by 2 until reaching 0 : a data hole at value 1 will be
* added to the beginning of the series
* => Starting from 7 and increasing 2 by 2 until reaching 12 : data holes at values 9 and 11
* will be added to the end of the series
*
* So :
* => xAxisData = [1, 3, 4, 5, 6, 7, 9, 11 ]
* => valuesData = [NaN, NaN, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, NaN, NaN, NaN, NaN]
*
* @param xAxisData the x-axis data of the data series
* @param valuesData the values data of the data series
* @param resolution the resoultion (on x-axis) used to determinate data holes
* @param fillValue the fill value used for data holes in the values data
* @param minBound the limit at which to start filling data holes for the series. If set to NaN,
* the limit is not used
* @param maxBound the limit at which to end filling data holes for the series. If set to NaN,
* the limit is not used
*
* @remarks There is no control over the consistency between x-axis data and values data. The
* method considers that the data is well formed (the total number of values data is a multiple
* of the number of x-axis data)
*/
static void fillDataHoles(std::vector<double> &xAxisData, std::vector<double> &valuesData,
double resolution,
double fillValue = std::numeric_limits<double>::quiet_NaN(),
double minBound = std::numeric_limits<double>::quiet_NaN(),
double maxBound = std::numeric_limits<double>::quiet_NaN());
/**
* Computes the resolution of a dataset passed as a parameter.
*
* The resolution of a dataset is the minimum difference between two values that follow in the
* set.
* For example:
* - for the set [0, 2, 4, 8, 10, 11, 13] => the resolution is 1 (difference between 10 and 11).
*
* A resolution can be calculated on the logarithmic scale (base of 10). In this case, the
* dataset is first converted to logarithmic values.
* For example:
* - for the set [10, 100, 10000, 1000000], the values are converted to [1, 2, 4, 6] => the
* logarithmic resolution is 1 (difference between 1 and 2).
*
* @param begin the iterator pointing to the beginning of the dataset
* @param end the iterator pointing to the end of the dataset
* @param logarithmic computes a logarithmic resolution or not
* @return the resolution computed
* @warning the method considers the dataset as sorted and doesn't control it.
*/
template <typename Iterator>
static Resolution resolution(Iterator begin, Iterator end, bool logarithmic = false);
};
template <typename Iterator>
DataSeriesUtils::Resolution DataSeriesUtils::resolution(Iterator begin, Iterator end,
bool logarithmic)
{
// Retrieves data into a work dataset
using ValueType = typename Iterator::value_type;
std::vector<ValueType> values{};
std::copy(begin, end, std::back_inserter(values));
// Converts data if logarithmic flag is activated
if (logarithmic) {
std::for_each(values.begin(), values.end(),
[logarithmic](auto &val) { val = std::log10(val); });
}
// Computes the differences between the values in the dataset
std::adjacent_difference(values.begin(), values.end(), values.begin());
// Retrieves the smallest difference
auto resolutionIt = std::min_element(values.begin(), values.end());
auto resolution
= resolutionIt != values.end() ? *resolutionIt : std::numeric_limits<double>::quiet_NaN();
return Resolution{resolution, logarithmic};
}
#endif // SCIQLOP_DATASERIESUTILS_H