MayaFlux 0.1.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
8/**
9 * @enum ConvolutionOperation
10 * @brief Specific convolution operations supported
11 */
12enum class ConvolutionOperation : uint8_t {
13 DIRECT_CONVOLUTION, ///< Standard convolution
14 CROSS_CORRELATION, ///< Cross-correlation
15 MATCHED_FILTER, ///< Matched filtering
16 DECONVOLUTION, ///< Deconvolution
17 AUTO_CORRELATION ///< Auto-correlation
18};
19
20/**
21 * @class ConvolutionTransformer
22 * @brief Concrete transformer for convolution-based operations
23 *
24 * Handles various convolution operations:
25 * - Direct convolution and cross-correlation
26 * - Impulse response application
27 * - Matched filtering and signal detection
28 * - Deconvolution and restoration
29 */
30template <ComputeData InputType = std::vector<Kakshya::DataVariant>, ComputeData OutputType = InputType>
31class MAYAFLUX_API ConvolutionTransformer final : public UniversalTransformer<InputType, OutputType> {
32public:
35
36 /**
37 * @brief Constructs a ConvolutionTransformer with specified operation
38 * @param op The convolution operation to perform (default: DIRECT_CONVOLUTION)
39 */
40 explicit ConvolutionTransformer(ConvolutionOperation op = ConvolutionOperation::DIRECT_CONVOLUTION)
41 : m_operation(op)
42 {
43 set_default_parameters();
44 }
45
46 /**
47 * @brief Gets the transformation type
48 * @return TransformationType::CONVOLUTION
49 */
50 [[nodiscard]] TransformationType get_transformation_type() const override
51 {
52 return TransformationType::CONVOLUTION;
53 }
54
55 /**
56 * @brief Gets the transformer name including the operation type
57 * @return String representation of the transformer name
58 */
59 [[nodiscard]] std::string get_transformer_name() const override
60 {
61 return std::string("ConvolutionTransformer_").append(Utils::enum_to_string(m_operation));
62 }
63
64protected:
65 /**
66 * @brief Core transformation implementation for convolution operations
67 * @param input Input data to transform using convolution methods
68 * @return Transformed output data
69 *
70 * Performs the convolution operation specified by m_operation on the input data.
71 * Operations include direct convolution, cross-correlation, matched filtering,
72 * deconvolution, and auto-correlation. These operations are fundamental for
73 * signal processing tasks such as filtering, pattern matching, and system
74 * identification. Supports both in-place and out-of-place transformations.
75 */
77 {
78 switch (m_operation) {
79 case ConvolutionOperation::DIRECT_CONVOLUTION: {
80 auto impulse_response = get_parameter_or<std::vector<double>>("impulse_response", std::vector<double> { 1.0 });
81
82 if (this->is_in_place()) {
83 return create_output(transform_convolve(input, impulse_response));
84 }
85 return create_output(transform_convolve(input, impulse_response, m_working_buffer));
86 }
87
88 case ConvolutionOperation::CROSS_CORRELATION: {
89 auto template_signal = get_parameter_or<std::vector<double>>("template_signal", std::vector<double> { 1.0 });
90 auto normalize = get_parameter_or<bool>("normalize", true);
91
92 if (this->is_in_place()) {
93 return create_output(transform_cross_correlate(input, template_signal, normalize));
94 }
95 return create_output(transform_cross_correlate(input, template_signal, normalize, m_working_buffer));
96 }
97
98 case ConvolutionOperation::MATCHED_FILTER: {
99 auto reference_signal = get_parameter_or<std::vector<double>>("reference_signal", std::vector<double> { 1.0 });
100
101 if (this->is_in_place()) {
102 return create_output(transform_matched_filter(input, reference_signal));
103 }
104 return create_output(transform_matched_filter(input, reference_signal, m_working_buffer));
105 }
106
107 case ConvolutionOperation::DECONVOLUTION: {
108 auto impulse_response = get_parameter_or<std::vector<double>>("impulse_response", std::vector<double> { 1.0 });
109 auto regularization = get_parameter_or<double>("regularization", 1e-6);
110
111 if (this->is_in_place()) {
112 return create_output(transform_deconvolve(input, impulse_response, regularization));
113 }
114 return create_output(transform_deconvolve(input, impulse_response, regularization, m_working_buffer));
115 }
116
117 case ConvolutionOperation::AUTO_CORRELATION: {
118 auto normalize = get_parameter_or<bool>("normalize", true);
119
120 if (this->is_in_place()) {
121 return create_output(transform_auto_correlate_fft(input, normalize));
122 }
123 return create_output(transform_auto_correlate_fft(input, m_working_buffer, normalize));
124 }
125
126 default:
127 return create_output(input);
128 }
129 }
130
131 /**
132 * @brief Sets transformation parameters
133 * @param name Parameter name
134 * @param value Parameter value
135 *
136 * Handles setting of convolution operation type and delegates other parameters to base class.
137 * Supports both enum and string values for the "operation" parameter.
138 *
139 * Common parameters include:
140 * - "impulse_response": Impulse response for convolution/deconvolution operations
141 * - "template_signal": Template signal for cross-correlation
142 * - "reference_signal": Reference signal for matched filtering
143 * - "normalize": Whether to normalize correlation results
144 * - "regularization": Regularization parameter for deconvolution stability
145 */
146 void set_transformation_parameter(const std::string& name, std::any value) override
147 {
148 if (name == "operation") {
149 if (auto op_result = safe_any_cast<ConvolutionOperation>(value)) {
150 m_operation = *op_result.value;
151 return;
152 }
153 if (auto str_result = safe_any_cast<std::string>(value)) {
154 if (auto op_enum = Utils::string_to_enum_case_insensitive<ConvolutionOperation>(*str_result.value)) {
155 m_operation = *op_enum;
156 return;
157 }
158 }
159 }
160
162 }
163
164private:
165 ConvolutionOperation m_operation; ///< Current convolution operation
166 mutable std::vector<std::vector<double>> m_working_buffer; ///< Buffer for out-of-place convolution operations
167
168 /**
169 * @brief Sets default parameter values for all convolution operations
170 *
171 * Initializes all possible parameters with sensible defaults to ensure
172 * the transformer works correctly regardless of the selected operation.
173 * Default values are chosen for typical signal processing scenarios.
174 */
176 {
177 this->set_parameter("impulse_response", std::vector<double> { 1.0 });
178 this->set_parameter("template_signal", std::vector<double> { 1.0 });
179 this->set_parameter("reference_signal", std::vector<double> { 1.0 });
180 this->set_parameter("normalize", true);
181 this->set_parameter("regularization", 1e-6);
182 }
183
184 /**
185 * @brief Gets a parameter value with fallback to default
186 * @tparam T Parameter type
187 * @param name Parameter name
188 * @param default_value Default value if parameter not found or wrong type
189 * @return Parameter value or default
190 */
191 template <typename T>
192 T get_parameter_or(const std::string& name, const T& default_value) const
193 {
194 auto param = this->get_transformation_parameter(name);
195 if (!param.has_value())
196 return default_value;
197
198 auto result = safe_any_cast<T>(param);
199 return result.value_or(default_value);
200 }
201
202 /**
203 * @brief Creates output with proper type conversion
204 * @param data Input data to convert
205 * @return Output with converted data type
206 *
207 * Handles type conversion between InputType and OutputType when necessary,
208 * or direct assignment when types match. Ensures convolution processing results
209 * maintain the correct output type and signal characteristics.
210 */
212 {
213 if constexpr (std::is_same_v<InputType, OutputType>) {
214 return input;
215 } else {
216 auto [result_data, metadata] = OperationHelper::extract_structured_double(input);
217 return this->convert_result(result_data, metadata);
218 }
219 }
220};
221
222}
Float Processing Guidelines.
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.
void set_default_parameters()
Sets default parameter values for all convolution operations.
output_type transform_implementation(input_type &input) override
Core transformation implementation for convolution operations.
TransformationType get_transformation_type() const override
Gets the transformation type.
output_type create_output(const input_type &input)
Creates output with proper type conversion.
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.
Template-flexible transformer base with instance-defined I/O types.
DataType transform_auto_correlate_fft(DataType &input, bool normalize=true)
Auto-correlation using FFT (IN-PLACE)
ConvolutionOperation
Specific convolution operations supported.
@ DIRECT_CONVOLUTION
Standard convolution.
DataType transform_matched_filter(DataType &input, const std::vector< double > &reference_signal)
Matched filter using cross-correlation for signal detection (IN-PLACE)
DataType transform_deconvolve(DataType &input, const std::vector< double > &impulse_to_remove, double regularization=1e-6)
Deconvolution using frequency domain division (IN-PLACE) Useful for removing known impulse responses.
DataType transform_cross_correlate(DataType &input, const std::vector< double > &template_signal, bool normalize=true)
Cross-correlation using FFT (convolution with time-reversed impulse) (IN-PLACE)
TransformationType
Categories of transformation operations for discovery and organization.
DataType transform_convolve(DataType &input, const std::vector< double > &impulse_response)
Convolution transformation using existing infrastructure with C++20 ranges (IN-PLACE)
void normalize(std::vector< double > &data, double target_peak)
Normalize single-channel data to specified peak level (in-place)
Definition Yantra.cpp:558
Input/Output container for computation pipeline data flow with structure preservation.
Definition DataIO.hpp:24