MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ConvolutionTransformer.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux::Yantra {
7
9
10/**
11 * @enum ConvolutionOperation
12 * @brief Specific convolution operations supported
13 */
14enum class ConvolutionOperation : uint8_t {
15 DIRECT_CONVOLUTION, ///< Standard convolution
16 CROSS_CORRELATION, ///< Cross-correlation
17 MATCHED_FILTER, ///< Matched filtering
18 DECONVOLUTION, ///< Deconvolution
19 AUTO_CORRELATION ///< Auto-correlation
20};
21
22/** @brief Construction settings for ConvolutionTransformer. */
24
25/**
26 * @class ConvolutionTransformer
27 * @brief Concrete transformer for convolution-based operations
28 *
29 * Handles various convolution operations:
30 * - Direct convolution and cross-correlation
31 * - Impulse response application
32 * - Matched filtering and signal detection
33 * - Deconvolution and restoration
34 */
35template <ComputeData InputType = std::vector<Kakshya::DataVariant>, ComputeData OutputType = InputType>
36class ConvolutionTransformer final : public UniversalTransformer<InputType, OutputType> {
37public:
40
41 /**
42 * @brief Constructs a ConvolutionTransformer with specified operation
43 * @param op The convolution operation to perform (default: DIRECT_CONVOLUTION)
44 */
50
51 /**
52 * @brief Construct and configure a convolution transformer in one step.
53 * @param config Initial transformation settings.
54 */
56 : ConvolutionTransformer(config.operation)
57 {
58 this->set_strategy(config.strategy);
59 this->set_quality(config.quality);
60 this->set_scope(config.scope);
61 this->set_intensity(config.intensity);
62
63 for (const auto& key : config.keys) {
64 this->add_transformation_key(key);
65 }
66
67 for (const auto& [name, value] : config.parameters) {
68 this->set_parameter(name, value);
69 }
70 }
71
72 /**
73 * @brief Gets the transformation type
74 * @return TransformationType::CONVOLUTION
75 */
76 [[nodiscard]] TransformationType get_transformation_type() const override
77 {
79 }
80
81 /**
82 * @brief Gets the transformer name including the operation type
83 * @return String representation of the transformer name
84 */
85 [[nodiscard]] std::string get_transformer_name() const override
86 {
87 return std::string("ConvolutionTransformer_").append(Reflect::enum_to_string(m_operation));
88 }
89
90protected:
91 /**
92 * @brief Applies the configured convolution operation
93 * @param input Input data
94 * @return Transformed output
95 *
96 * Extracts per-channel double spans, calls the corresponding
97 * Kinesis::Discrete::Convolution primitive on each channel, then
98 * reconstructs the typed output via OperationHelper.
99 */
101 {
102 switch (m_operation) {
103
105 const auto ir = get_parameter_or<std::vector<double>>(
106 "impulse_response", std::vector<double> { 1.0 });
107 return apply_per_channel(input, [&ir](std::span<const double> ch) {
108 return D::convolve(ch, ir);
109 });
110 }
111
113 const auto tmpl = get_parameter_or<std::vector<double>>(
114 "template_signal", std::vector<double> { 1.0 });
115 const auto norm = get_parameter_or<bool>("normalize", true);
116 return apply_per_channel(input, [&tmpl, norm](std::span<const double> ch) {
117 return D::cross_correlate(ch, tmpl, norm);
118 });
119 }
120
122 const auto ref = get_parameter_or<std::vector<double>>(
123 "reference_signal", std::vector<double> { 1.0 });
124 return apply_per_channel(input, [&ref](std::span<const double> ch) {
125 return D::matched_filter(ch, ref);
126 });
127 }
128
130 const auto ir = get_parameter_or<std::vector<double>>(
131 "impulse_response", std::vector<double> { 1.0 });
132 const auto reg = get_parameter_or<double>("regularization", 1e-6);
133 return apply_per_channel(input, [&ir, reg](std::span<const double> ch) {
134 return D::deconvolve(ch, ir, reg);
135 });
136 }
137
139 const auto norm = get_parameter_or<bool>("normalize", true);
140 return apply_per_channel(input, [norm](std::span<const double> ch) {
141 return D::auto_correlate(ch, norm);
142 });
143 }
144
145 default:
146 return create_output(input);
147 }
148 }
149
150 /**
151 * @brief Sets transformation parameters
152 * @param name Parameter name
153 * @param value Parameter value
154 *
155 * Handles setting of convolution operation type and delegates other parameters to base class.
156 * Supports both enum and string values for the "operation" parameter.
157 *
158 * Common parameters include:
159 * - "impulse_response": Impulse response for convolution/deconvolution operations
160 * - "template_signal": Template signal for cross-correlation
161 * - "reference_signal": Reference signal for matched filtering
162 * - "normalize": Whether to normalize correlation results
163 * - "regularization": Regularization parameter for deconvolution stability
164 */
165 void set_transformation_parameter(const std::string& name, std::any value) override
166 {
167 if (name == "operation") {
168 if (auto r = safe_any_cast<ConvolutionOperation>(value)) {
169 m_operation = *r.value;
170 return;
171 }
172 if (auto r = safe_any_cast<std::string>(value)) {
173 if (auto e = Reflect::string_to_enum_case_insensitive<ConvolutionOperation>(*r.value)) {
174 m_operation = *e;
175 return;
176 }
177 }
178 }
179
181 }
182
183private:
184 ConvolutionOperation m_operation; ///< Current convolution operation
185 mutable std::vector<std::vector<double>> m_working_buffer; ///< Buffer for out-of-place convolution operations
186
187 /**
188 * @brief Extracts per-channel spans, applies func to each, and reconstructs.
189 * @tparam Func Callable matching either void(std::span<double>) or
190 * std::vector<double>(std::span<const double>)
191 *
192 * In-place: results are copied back into the original channel spans of @p input.
193 * Out-of-place: input is not mutated.
194 */
195 template <typename Func>
197 {
199 m_working_buffer.resize(channels.size());
200 for (size_t i = 0; i < channels.size(); ++i) {
201 if constexpr (std::is_void_v<std::invoke_result_t<Func, std::span<double>>>) {
202 m_working_buffer[i].assign(channels[i].begin(), channels[i].end());
203 func(std::span<double> { m_working_buffer[i] });
204 } else {
205 m_working_buffer[i] = func(std::span<const double> { channels[i] });
206 }
207 }
208 if (this->is_in_place())
209 for (size_t i = 0; i < channels.size(); ++i)
210 std::ranges::copy(m_working_buffer[i], channels[i].begin());
211 return create_output(
212 OperationHelper::reconstruct_from_double<InputType>(m_working_buffer, structure_info));
213 }
214
215 /**
216 * @brief Sets default parameter values for all convolution operations
217 *
218 * Initializes all possible parameters with sensible defaults to ensure
219 * the transformer works correctly regardless of the selected operation.
220 * Default values are chosen for typical signal processing scenarios.
221 */
223 {
224 this->set_parameter("impulse_response", std::vector<double> { 1.0 });
225 this->set_parameter("template_signal", std::vector<double> { 1.0 });
226 this->set_parameter("reference_signal", std::vector<double> { 1.0 });
227 this->set_parameter("normalize", true);
228 this->set_parameter("regularization", 1e-6);
229 }
230
231 /**
232 * @brief Gets a parameter value with fallback to default
233 * @tparam T Parameter type
234 * @param name Parameter name
235 * @param default_value Default value if parameter not found or wrong type
236 * @return Parameter value or default
237 */
238 template <typename T>
239 T get_parameter_or(const std::string& name, const T& default_value) const
240 {
241 auto param = this->get_transformation_parameter(name);
242 if (!param.has_value())
243 return default_value;
244
245 auto result = safe_any_cast<T>(param);
246 return result.value_or(default_value);
247 }
248
249 /**
250 * @brief Creates output with proper type conversion
251 * @param data Input data to convert
252 * @return Output with converted data type
253 *
254 * Handles type conversion between InputType and OutputType when necessary,
255 * or direct assignment when types match. Ensures convolution processing results
256 * maintain the correct output type and signal characteristics.
257 */
259 {
260 if constexpr (std::is_same_v<InputType, OutputType>) {
261 return input;
262 } else {
264 return this->convert_result(result_data, metadata);
265 }
266 }
267};
268
269}
Core::GlobalInputConfig input
Definition Config.cpp:42
std::unordered_map< std::string, std::string > metadata
Float Processing Guidelines.
glm::vec3 value
std::string name
Definition VKDevice.cpp:123
uint32_t channels
output_type convert_result(std::vector< std::vector< double > > &result_data, DataStructureInfo &metadata)
Convert processed double data back to OutputType using metadata and optional callback.
ConvolutionOperation m_operation
Current convolution operation.
ConvolutionTransformer(ConvolutionOperation op=ConvolutionOperation::DIRECT_CONVOLUTION)
Constructs a ConvolutionTransformer with specified operation.
T get_parameter_or(const std::string &name, const T &default_value) const
Gets a parameter value with fallback to default.
std::vector< std::vector< double > > m_working_buffer
Buffer for out-of-place convolution operations.
output_type apply_per_channel(input_type &input, Func &&func)
Extracts per-channel spans, applies func to each, and reconstructs.
void set_default_parameters()
Sets default parameter values for all convolution operations.
output_type transform_implementation(input_type &input) override
Applies the configured convolution operation.
TransformationType get_transformation_type() const override
Gets the transformation type.
output_type create_output(const input_type &input)
Creates output with proper type conversion.
ConvolutionTransformer(const ConvolutionTransformerConfig &config)
Construct and configure a convolution transformer in one step.
std::string get_transformer_name() const override
Gets the transformer name including the operation type.
void set_transformation_parameter(const std::string &name, std::any value) override
Sets transformation parameters.
Concrete transformer for convolution-based operations.
static std::tuple< std::vector< std::span< double > >, DataStructureInfo > extract_structured_double(T &compute_data)
Extract structured double data from Datum container or direct ComputeData with automatic container ha...
void set_strategy(TransformationStrategy strategy)
Sets the transformation strategy.
void set_intensity(double intensity)
Set transformation intensity (0.0 = no transformation, 1.0 = full transformation)
void set_scope(TransformationScope scope)
Sets the transformation scope.
virtual bool is_in_place() const
Indicates whether the transformation modifies the input data directly.
virtual std::any get_transformation_parameter(const std::string &name) const
Gets a transformation-specific parameter value.
void set_parameter(const std::string &name, std::any value) override
Type-safe parameter management with transformation-specific defaults.
void set_quality(TransformationQuality quality)
Sets the transformation quality level.
void add_transformation_key(const TransformationKey &key)
Add transformation key for multi-dimensional transformations.
virtual void set_transformation_parameter(const std::string &name, std::any value)
Transformation-specific parameter handling (override for custom parameters)
Template-flexible transformer base with instance-defined I/O types.
std::vector< double > convolve(std::span< const double > src, std::span< const double > ir)
Linear FFT convolution.
std::vector< double > cross_correlate(std::span< const double > src, std::span< const double > tmpl, bool normalize)
Cross-correlation via FFT.
std::vector< double > auto_correlate(std::span< const double > src, bool normalize)
Auto-correlation via FFT.
std::vector< double > deconvolve(std::span< const double > src, std::span< const double > ir, double regularization)
Frequency-domain deconvolution with Tikhonov regularisation.
std::vector< double > matched_filter(std::span< const double > src, std::span< const double > reference)
Matched filter (normalised cross-correlation)
constexpr std::string_view enum_to_string(EnumType value) noexcept
Universal enum to string converter using magic_enum (original case)
ConvolutionOperation
Specific convolution operations supported.
@ DIRECT_CONVOLUTION
Standard convolution.
TransformationType
Categories of transformation operations for discovery and organization.
@ CONVOLUTION
Convolution-based transformations (impulse response, filters)
Input/Output container for computation pipeline data flow with structure preservation.
Definition DataIO.hpp:26
TransformationQuality quality
Requested transformation quality.
TransformationScope scope
Portion of the input to transform.
TransformationStrategy strategy
How transformation work is performed.
std::vector< TransformationKey > keys
Transformation keys to register before processing.
std::map< std::string, std::any > parameters
Additional named parameters applied last.
double intensity
Overall transformation strength.
Initial settings shared by concrete transformer families.