54 for (
size_t i = 0; i < params.
size; ++i) {
63 for (
size_t i = 0; i < params.
size; ++i) {
65 if (std::abs(norm_dist) <= 1.0) {
66 const real_t norm_dist_sq = norm_dist * norm_dist;
68 FACTOR * std::pow(
static_cast<real_t>(1.0) - norm_dist_sq,
static_cast<real_t>(3.0));
77 for (
size_t i = 0; i < params.
size; ++i) {
79 if (std::abs(norm_dist) < 1.0) {
80 const real_t norm_dist_sq = norm_dist * norm_dist;
81 kernel[i] = std::exp(-
static_cast<real_t>(1.0) / (
static_cast<real_t>(1.0) - norm_dist_sq));
91 for (
size_t i = 0; i < params.
size; ++i) {
93 if (std::abs(norm_dist) <= 1.0) {
94 kernel[i] = factor * (
static_cast<real_t>(1.0) - norm_dist * norm_dist);
104 for (
size_t i = 0; i < params.
size; ++i) {
106 if (std::abs(norm_dist) <= 1.0) {
108 static_cast<real_t>(cos_factor * std::cos(
PI * norm_dist /
static_cast<real_t>(2.0)));
118 for (
size_t i = 0; i < params.
size; ++i) {
120 if (std::abs(norm_dist) <= 1.0) {
121 const real_t norm_dist_sq = norm_dist * norm_dist;
122 kernel[i] = factor * (
static_cast<real_t>(1.0) - norm_dist_sq) *
123 (
static_cast<real_t>(1.0) - norm_dist_sq);
138 const size_t size = params.
size;
139 std::vector<real_t> kernel(size);
142 switch (params.
type) {
162 throw std::invalid_argument(
"Unsupported kernel type for smoothing.");
166 const real_t sum = std::accumulate(kernel.begin(), kernel.end(),
static_cast<real_t>(0.0));
168 for (
real_t &val : kernel) {
191 if (y_values.empty()) {
194 if (bin_width <= 0) {
195 throw std::invalid_argument(
"'bin_width' must be a positive bin width for smoothing.");
198 throw std::invalid_argument(
"Smoothing sigma must be positive.");
203 const size_t num_points = y_values.size();
204 const auto kernel_radius_bins =
static_cast<size_t>(4.0 * sigma / bin_width);
207 const size_t max_kernel_radius = num_points / 2;
208 size_t kernel_radius = std::min(kernel_radius_bins, max_kernel_radius);
211 if (kernel_radius == 0) {
215 const size_t kernel_size = 2 * kernel_radius + 1;
220 generateKernel({.size = kernel_size, .bin_width = bin_width, .sigma = sigma, .type = type});
221 std::vector<real_t> smoothed(num_points, 0.0);
224 for (
size_t i = 0; i < std::min(kernel_radius, num_points); ++i) {
225 for (
size_t j = 0; j < kernel_size; ++j) {
226 const long long idx =
static_cast<long long>(i) +
static_cast<long long>(j) -
227 static_cast<long long>(kernel_radius);
228 smoothed[i] += y_values[
static_cast<size_t>(std::max(0LL, idx))] * kernel[j];
236 if (num_points > 2 * kernel_radius) {
237 for (
size_t i = kernel_radius; i < num_points - kernel_radius; ++i) {
238 const real_t *src = y_values.data() + (i - kernel_radius);
240 for (
size_t j = 0; j < kernel_size; ++j) {
241 sum += src[j] * kernel[j];
248 const size_t right_start = (num_points > kernel_radius) ? num_points - kernel_radius : num_points;
249 for (
size_t i = right_start; i < num_points; ++i) {
250 for (
size_t j = 0; j < kernel_size; ++j) {
251 const long long idx =
static_cast<long long>(i) +
static_cast<long long>(j) -
252 static_cast<long long>(kernel_radius);
253 const long long clamped = std::min(idx,
static_cast<long long>(num_points) - 1);
254 smoothed[i] += y_values[
static_cast<size_t>(clamped)] * kernel[j];
270 const std::vector<real_t> &y_values,
272 if (r_values.size() != y_values.size() || r_values.size() < 2) {
275 params.
bin_width = r_values[1] - r_values[0];
Mathematical and physical constants for the Correlation tool.
Floating point precision type definitions for Correlation.
Definition AVX2Kernels.hpp:19
void fillCosine(std::vector< real_t > &kernel, const KernelGenerationParams ¶ms, real_t center)
Definition Smoothing.hpp:101
void fillGaussian(std::vector< real_t > &kernel, const KernelGenerationParams ¶ms, real_t center)
Definition Smoothing.hpp:48
void fillBiweight(std::vector< real_t > &kernel, const KernelGenerationParams ¶ms, real_t center)
Definition Smoothing.hpp:115
void fillTriweight(std::vector< real_t > &kernel, const KernelGenerationParams ¶ms, real_t center)
Definition Smoothing.hpp:60
void fillEpanechnikov(std::vector< real_t > &kernel, const KernelGenerationParams ¶ms, real_t center)
Definition Smoothing.hpp:88
void fillBump(std::vector< real_t > &kernel, const KernelGenerationParams ¶ms, real_t center)
Definition Smoothing.hpp:75
Defines universal mathematical and physical constants.
KernelType
Selection of kernel types for smoothing.
Definition Smoothing.hpp:26
@ Triweight
Definition Smoothing.hpp:26
@ Biweight
Definition Smoothing.hpp:26
@ Cosine
Definition Smoothing.hpp:26
@ Epanechnikov
Definition Smoothing.hpp:26
@ Bump
Definition Smoothing.hpp:26
@ Gaussian
Definition Smoothing.hpp:26
constexpr real_t PI
High-precision mathematical pi constant.
Definition Constants.hpp:27
std::vector< real_t > generateKernel(const KernelGenerationParams ¶ms)
Generates a smoothing kernel.
Definition Smoothing.hpp:137
constexpr real_t TWO_PI
2 * PI
Definition Constants.hpp:41
constexpr T distance(const Vector3< T > &vec_a, const Vector3< T > &vec_b) noexcept
Computes the distance between two vectors.
Definition LinearAlgebra.hpp:533
std::vector< real_t > kernelSmoothing(const std::vector< real_t > &y_values, KernelSmoothingParams params)
Applies kernel smoothing to a data series.
Definition Smoothing.hpp:185
double real_t
Definition Precision.hpp:18
Parameters for kernel generation.
Definition Smoothing.hpp:31
real_t bin_width
Bin width of the underlying data.
Definition Smoothing.hpp:33
KernelType type
Mathematical kernel function type.
Definition Smoothing.hpp:35
size_t size
Discrete kernel array length (odd integer).
Definition Smoothing.hpp:32
real_t sigma
Physical kernel standard deviation / bandwidth.
Definition Smoothing.hpp:34
Options bundling for data series kernel smoothing.
Definition Smoothing.hpp:41
KernelType type
Kernel function selection.
Definition Smoothing.hpp:44
real_t sigma
Kernel bandwidth standard deviation.
Definition Smoothing.hpp:43
real_t bin_width
Uniform bin width of the data (e.g., Δr in Å).
Definition Smoothing.hpp:42