Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
Smoothing.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include "math/Constants.hpp"
12#include "math/Precision.hpp"
13#include <algorithm>
14#include <cmath>
15#include <cstdint>
16#include <numeric>
17#include <stdexcept>
18#include <vector>
19
20namespace correlation::math {
21
26enum class KernelType : std::uint8_t { Gaussian, Bump, Triweight, Epanechnikov, Cosine, Biweight };
27
37
46namespace detail {
47
48inline void fillGaussian(std::vector<real_t> &kernel, const KernelGenerationParams &params,
49 real_t center) {
50 const real_t prefactor =
51 static_cast<real_t>(1.0) / static_cast<real_t>(std::sqrt(TWO_PI) * params.sigma);
52 const real_t exp_coeff =
53 static_cast<real_t>(-1.0) / (static_cast<real_t>(2.0) * params.sigma * params.sigma);
54 for (size_t i = 0; i < params.size; ++i) {
55 const real_t distance = (static_cast<real_t>(i) - center) * params.bin_width;
56 kernel[i] = prefactor * std::exp(exp_coeff * distance * distance);
57 }
58}
59
60inline void fillTriweight(std::vector<real_t> &kernel, const KernelGenerationParams &params,
61 real_t center) {
62 constexpr real_t FACTOR = static_cast<real_t>(35.0) / static_cast<real_t>(32.0);
63 for (size_t i = 0; i < params.size; ++i) {
64 const real_t norm_dist = ((static_cast<real_t>(i) - center) * params.bin_width) / params.sigma;
65 if (std::abs(norm_dist) <= 1.0) {
66 const real_t norm_dist_sq = norm_dist * norm_dist;
67 kernel[i] =
68 FACTOR * std::pow(static_cast<real_t>(1.0) - norm_dist_sq, static_cast<real_t>(3.0));
69 } else {
70 kernel[i] = 0.0;
71 }
72 }
73}
74
75inline void fillBump(std::vector<real_t> &kernel, const KernelGenerationParams &params,
76 real_t center) {
77 for (size_t i = 0; i < params.size; ++i) {
78 const real_t norm_dist = ((static_cast<real_t>(i) - center) * params.bin_width) / params.sigma;
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));
82 } else {
83 kernel[i] = 0.0;
84 }
85 }
86}
87
88inline void fillEpanechnikov(std::vector<real_t> &kernel, const KernelGenerationParams &params,
89 real_t center) {
90 const real_t factor = static_cast<real_t>(3.0) / static_cast<real_t>(4.0);
91 for (size_t i = 0; i < params.size; ++i) {
92 const real_t norm_dist = ((static_cast<real_t>(i) - center) * params.bin_width) / params.sigma;
93 if (std::abs(norm_dist) <= 1.0) {
94 kernel[i] = factor * (static_cast<real_t>(1.0) - norm_dist * norm_dist);
95 } else {
96 kernel[i] = 0.0;
97 }
98 }
99}
100
101inline void fillCosine(std::vector<real_t> &kernel, const KernelGenerationParams &params,
102 real_t center) {
103 const real_t cos_factor = PI / static_cast<real_t>(4.0);
104 for (size_t i = 0; i < params.size; ++i) {
105 const real_t norm_dist = ((static_cast<real_t>(i) - center) * params.bin_width) / params.sigma;
106 if (std::abs(norm_dist) <= 1.0) {
107 kernel[i] =
108 static_cast<real_t>(cos_factor * std::cos(PI * norm_dist / static_cast<real_t>(2.0)));
109 } else {
110 kernel[i] = 0.0;
111 }
112 }
113}
114
115inline void fillBiweight(std::vector<real_t> &kernel, const KernelGenerationParams &params,
116 real_t center) {
117 const real_t factor = static_cast<real_t>(15.0) / static_cast<real_t>(16.0);
118 for (size_t i = 0; i < params.size; ++i) {
119 const real_t norm_dist = ((static_cast<real_t>(i) - center) * params.bin_width) / params.sigma;
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);
124 } else {
125 kernel[i] = 0.0;
126 }
127 }
128}
129
130} // namespace detail
131
137[[nodiscard]] inline std::vector<real_t> generateKernel(const KernelGenerationParams &params) {
138 const size_t size = params.size;
139 std::vector<real_t> kernel(size);
140 const real_t center = static_cast<real_t>(size - 1) / static_cast<real_t>(2.0);
141
142 switch (params.type) {
144 detail::fillGaussian(kernel, params, center);
145 break;
147 detail::fillTriweight(kernel, params, center);
148 break;
149 case KernelType::Bump:
150 detail::fillBump(kernel, params, center);
151 break;
153 detail::fillEpanechnikov(kernel, params, center);
154 break;
156 detail::fillCosine(kernel, params, center);
157 break;
159 detail::fillBiweight(kernel, params, center);
160 break;
161 default:
162 throw std::invalid_argument("Unsupported kernel type for smoothing.");
163 }
164
165 // Normalize the discrete kernel to ensure the sum of its elements is 1.
166 const real_t sum = std::accumulate(kernel.begin(), kernel.end(), static_cast<real_t>(0.0));
167 if (sum > 1e-9) {
168 for (real_t &val : kernel) {
169 val /= sum;
170 }
171 }
172 return kernel;
173}
174
185inline std::vector<real_t> kernelSmoothing(const std::vector<real_t> &y_values,
186 KernelSmoothingParams params) {
187 real_t const bin_width = params.bin_width;
188 real_t const sigma = params.sigma;
189 KernelType const type = params.type;
190
191 if (y_values.empty()) {
192 return {};
193 }
194 if (bin_width <= 0) {
195 throw std::invalid_argument("'bin_width' must be a positive bin width for smoothing.");
196 }
197 if (sigma <= 0.0) {
198 throw std::invalid_argument("Smoothing sigma must be positive.");
199 }
200
201 // Calculate the kernel radius in bins by dividing the physical sigma by bin_width.
202 // We use 4.0 * sigma to cover approximately 99.99% of the Gaussian area.
203 const size_t num_points = y_values.size();
204 const auto kernel_radius_bins = static_cast<size_t>(4.0 * sigma / bin_width);
205
206 // Clamp the radius to prevent the kernel from exceeding half the data size.
207 const size_t max_kernel_radius = num_points / 2;
208 size_t kernel_radius = std::min(kernel_radius_bins, max_kernel_radius);
209
210 // Ensure a minimum kernel size (3 bins: -1, 0, +1) if sigma is very small.
211 if (kernel_radius == 0) {
212 kernel_radius = 1;
213 }
214
215 const size_t kernel_size = 2 * kernel_radius + 1;
216
217 // Pass the original physical sigma (in distance units) to the kernel
218 // generator.
219 const auto kernel =
220 generateKernel({.size = kernel_size, .bin_width = bin_width, .sigma = sigma, .type = type});
221 std::vector<real_t> smoothed(num_points, 0.0);
222
223 // Phase 1 – left boundary (bins 0 .. kernel_radius-1): needs index clamping.
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];
229 }
230 }
231
232 // Phase 2 – interior (bins kernel_radius .. num_points-kernel_radius-1):
233 // index i - kernel_radius + j is always in [0, num_points-1], no branch needed.
234 // The clean inner loop (sum += src[j] * kernel[j]) is auto-vectorised by
235 // GCC/Clang at -O2 using whatever SIMD width the TU was compiled with.
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);
239 real_t sum = 0.0;
240 for (size_t j = 0; j < kernel_size; ++j) {
241 sum += src[j] * kernel[j];
242 }
243 smoothed[i] = sum;
244 }
245 }
246
247 // Phase 3 – right boundary (bins num_points-kernel_radius .. num_points-1): needs clamping.
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];
255 }
256 }
257
258 return smoothed;
259}
260
269inline std::vector<real_t> kernelSmoothing(const std::vector<real_t> &r_values,
270 const std::vector<real_t> &y_values,
271 KernelSmoothingParams params) {
272 if (r_values.size() != y_values.size() || r_values.size() < 2) {
273 return {};
274 }
275 params.bin_width = r_values[1] - r_values[0];
276 return kernelSmoothing(y_values, params);
277}
278
279} // namespace correlation::math
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 &params, real_t center)
Definition Smoothing.hpp:101
void fillGaussian(std::vector< real_t > &kernel, const KernelGenerationParams &params, real_t center)
Definition Smoothing.hpp:48
void fillBiweight(std::vector< real_t > &kernel, const KernelGenerationParams &params, real_t center)
Definition Smoothing.hpp:115
void fillTriweight(std::vector< real_t > &kernel, const KernelGenerationParams &params, real_t center)
Definition Smoothing.hpp:60
void fillEpanechnikov(std::vector< real_t > &kernel, const KernelGenerationParams &params, real_t center)
Definition Smoothing.hpp:88
void fillBump(std::vector< real_t > &kernel, const KernelGenerationParams &params, 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 &params)
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