MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
MathematicalTransformer.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux::Yantra {
7
8/**
9 * @enum MathematicalOperation
10 * @brief Specific mathematical operations supported
11 */
12enum class MathematicalOperation : uint8_t {
13 GAIN, ///< Linear gain/attenuation
14 OFFSET, ///< DC offset
15 POWER, ///< Power function
16 LOGARITHMIC, ///< Logarithmic transform
17 EXPONENTIAL, ///< Exponential transform
18 TRIGONOMETRIC, ///< Trigonometric functions
19 QUANTIZE, ///< Quantization/bit reduction
20 NORMALIZE, ///< Normalization
21 POLYNOMIAL ///< Polynomial transform
22};
23
24/**
25 * @class MathematicalTransformer
26 * @brief Concrete transformer for mathematical operations
27 *
28 * Handles pure mathematical transformations:
29 * - Arithmetic operations (gain, offset, scaling)
30 * - Trigonometric functions
31 * - Logarithmic and exponential transforms
32 * - Polynomial and power functions
33 * - Quantization and bit reduction
34 */
35template <ComputeData InputType = std::vector<Kakshya::DataVariant>, ComputeData OutputType = InputType>
36class MAYAFLUX_API MathematicalTransformer final : public UniversalTransformer<InputType, OutputType> {
37public:
40
41 /**
42 * @brief Constructs a MathematicalTransformer with specified operation
43 * @param op The mathematical operation to perform (default: GAIN)
44 */
45 explicit MathematicalTransformer(MathematicalOperation op = MathematicalOperation::GAIN)
46 : m_operation(op)
47 {
48 set_default_parameters();
49 }
50
51 /**
52 * @brief Gets the transformation type
53 * @return TransformationType::MATHEMATICAL
54 */
55 [[nodiscard]] TransformationType get_transformation_type() const override
56 {
57 return TransformationType::MATHEMATICAL;
58 }
59
60 /**
61 * @brief Gets the transformer name including the operation type
62 * @return String representation of the transformer name
63 */
64 [[nodiscard]] std::string get_transformer_name() const override
65 {
66 return std::string("MathematicalTransformer_").append(Utils::enum_to_string(m_operation));
67 }
68
69protected:
70 /**
71 * @brief Core transformation implementation
72 * @param input Input data to transform
73 * @return Transformed output data
74 *
75 * Performs the mathematical operation specified by m_operation on the input data.
76 * Supports both in-place and out-of-place transformations based on transformer settings.
77 */
79 {
80 switch (m_operation) {
81 case MathematicalOperation::GAIN: {
82 auto gain_factor = get_parameter_or<double>("gain_factor", 1.0);
83 if (this->is_in_place()) {
84 return create_output(transform_linear(input, gain_factor, 0.0));
85 }
86 return create_output(transform_linear(input, gain_factor, 0.0, m_working_buffer));
87 }
88
89 case MathematicalOperation::OFFSET: {
90 auto offset_value = get_parameter_or<double>("offset_value", 0.0);
91 if (this->is_in_place()) {
92 return create_output(transform_linear(input, 1.0, offset_value));
93 }
94 return create_output(transform_linear(input, 1.0, offset_value, m_working_buffer));
95 }
96
97 case MathematicalOperation::POWER: {
98 auto exponent = get_parameter_or<double>("exponent", 2.0);
99
100 if (this->is_in_place()) {
101 return create_output(transform_power(input, exponent));
102 }
103 return create_output(transform_power(input, exponent, m_working_buffer));
104 }
105
106 case MathematicalOperation::LOGARITHMIC: {
107 auto scale = get_parameter_or<double>("scale", 1.0);
108 auto input_scale = get_parameter_or<double>("input_scale", 1.0);
109 auto offset = get_parameter_or<double>("offset", 1.0);
110 auto base = get_parameter_or<double>("base", std::numbers::e);
111
112 if (this->is_in_place()) {
113 return create_output(transform_logarithmic(input, scale, input_scale, offset, base));
114 }
115 return create_output(transform_logarithmic(input, scale, input_scale, offset, m_working_buffer, base));
116 }
117
118 case MathematicalOperation::EXPONENTIAL: {
119 auto scale = get_parameter_or<double>("scale", 1.0);
120 auto rate = get_parameter_or<double>("rate", 1.0);
121 auto base = get_parameter_or<double>("base", std::numbers::e);
122
123 if (this->is_in_place()) {
124 return create_output(transform_exponential(input, scale, rate, base));
125 }
126 return create_output(transform_exponential(input, scale, rate, m_working_buffer, base));
127 }
128
129 case MathematicalOperation::TRIGONOMETRIC: {
130 auto trig_function = get_parameter_or<std::string>("trig_function", "sin");
131 auto frequency = get_parameter_or<double>("frequency", 1.0);
132 auto amplitude = get_parameter_or<double>("amplitude", 1.0);
133 auto phase = get_parameter_or<double>("phase", 0.0);
134
135 if (trig_function == "sin") {
136 if (this->is_in_place()) {
137 return create_output(transform_trigonometric(input, [](double x) { return std::sin(x); }, frequency, amplitude, phase));
138 }
139 return create_output(transform_trigonometric(input, [](double x) { return std::sin(x); }, frequency, amplitude, phase, m_working_buffer));
140 }
141 if (trig_function == "cos") {
142 if (this->is_in_place()) {
143 return create_output(transform_trigonometric(input, [](double x) { return std::cos(x); }, frequency, amplitude, phase));
144 }
145 return create_output(transform_trigonometric(input, [](double x) { return std::cos(x); }, frequency, amplitude, phase, m_working_buffer));
146 }
147 if (trig_function == "tan") {
148 if (this->is_in_place()) {
149 return create_output(transform_trigonometric(input, [](double x) { return std::tan(x); }, frequency, amplitude, phase));
150 }
151 return create_output(transform_trigonometric(input, [](double x) { return std::tan(x); }, frequency, amplitude, phase, m_working_buffer));
152 }
153 return create_output(input);
154 }
155
156 case MathematicalOperation::QUANTIZE: {
157 auto bits = get_parameter_or<uint8_t>("bits", 16);
158 if (this->is_in_place()) {
159 return create_output(transform_quantize(input, bits));
160 }
161 return create_output(transform_quantize(input, bits, m_working_buffer));
162 }
163
164 case MathematicalOperation::NORMALIZE: {
165 auto target_peak = get_parameter_or<double>("target_peak", 1.0);
166 std::pair<double, double> target_range = { -target_peak, target_peak };
167 if (this->is_in_place()) {
168 return create_output(transform_normalize(input, target_range));
169 }
170 return create_output(transform_normalize(input, target_range, m_working_buffer));
171 }
172
173 case MathematicalOperation::POLYNOMIAL: {
174 auto coefficients = get_parameter_or<std::vector<double>>("coefficients", std::vector<double> { 0.0, 1.0 });
175 if (this->is_in_place()) {
176 return create_output(transform_polynomial(input, coefficients));
177 }
178 return create_output(transform_polynomial(input, coefficients, m_working_buffer));
179 }
180
181 default:
182 return create_output(input);
183 }
184 }
185
186 /**
187 * @brief Sets transformation parameters
188 * @param name Parameter name
189 * @param value Parameter value
190 *
191 * Handles setting of operation type and delegates other parameters to base class.
192 * Supports both enum and string values for the "operation" parameter.
193 */
194 void set_transformation_parameter(const std::string& name, std::any value) override
195 {
196 if (name == "operation") {
197 if (auto op_result = safe_any_cast<MathematicalOperation>(value)) {
198 m_operation = *op_result.value;
199 return;
200 }
201 if (auto str_result = safe_any_cast<std::string>(value)) {
202 if (auto op_enum = Utils::string_to_enum_case_insensitive<MathematicalOperation>(*str_result.value)) {
203 m_operation = *op_enum;
204 return;
205 }
206 }
207 }
208
210 }
211
212private:
213 MathematicalOperation m_operation; ///< Current mathematical operation
214 mutable std::vector<std::vector<double>> m_working_buffer; ///< Buffer for out-of-place operations
215
216 /**
217 * @brief Sets default parameter values for all mathematical operations
218 *
219 * Initializes all possible parameters with sensible defaults to ensure
220 * the transformer works correctly regardless of the selected operation.
221 */
223 {
224 this->set_parameter("gain_factor", 1.0);
225 this->set_parameter("offset_value", 0.0);
226 this->set_parameter("exponent", 2.0);
227 this->set_parameter("base", std::numbers::e);
228 this->set_parameter("scale", 1.0);
229 this->set_parameter("trig_function", std::string { "sin" });
230 this->set_parameter("frequency", 1.0);
231 this->set_parameter("amplitude", 1.0);
232 this->set_parameter("phase", 0.0);
233 this->set_parameter("bits", uint8_t { 16 });
234 this->set_parameter("target_peak", 1.0);
235 this->set_parameter("coefficients", std::vector<double> { 0.0, 1.0 });
236 this->set_parameter("input_scale", 1.0);
237 this->set_parameter("offset", 1.0);
238 this->set_parameter("rate", 1.0);
239 }
240
241 /**
242 * @brief Gets a parameter value with fallback to default
243 * @tparam T Parameter type
244 * @param name Parameter name
245 * @param default_value Default value if parameter not found or wrong type
246 * @return Parameter value or default
247 */
248 template <typename T>
249 T get_parameter_or(const std::string& name, const T& default_value) const
250 {
251 auto param = this->get_transformation_parameter(name);
252 if (!param.has_value())
253 return default_value;
254
255 auto result = safe_any_cast<T>(param);
256 return result.value_or(default_value);
257 }
258
259 /**
260 * @brief Creates output with proper type conversion
261 * @param data Input data to convert
262 * @return Output with converted data type
263 *
264 * Handles type conversion between InputType and OutputType when necessary,
265 * or direct assignment when types match.
266 */
268 {
269 if constexpr (std::is_same_v<InputType, OutputType>) {
270 return input;
271 } else {
272 auto [result_data, metadata] = OperationHelper::extract_structured_double(input);
273 return this->convert_result(result_data, metadata);
274 }
275 }
276};
277}
Float Processing Guidelines.
output_type create_output(const input_type &input)
Creates output with proper type conversion.
MathematicalTransformer(MathematicalOperation op=MathematicalOperation::GAIN)
Constructs a MathematicalTransformer with specified operation.
std::string get_transformer_name() const override
Gets the transformer name including the operation type.
TransformationType get_transformation_type() const override
Gets the transformation type.
void set_default_parameters()
Sets default parameter values for all mathematical operations.
void set_transformation_parameter(const std::string &name, std::any value) override
Sets transformation parameters.
T get_parameter_or(const std::string &name, const T &default_value) const
Gets a parameter value with fallback to default.
output_type transform_implementation(input_type &input) override
Core transformation implementation.
MathematicalOperation m_operation
Current mathematical operation.
std::vector< std::vector< double > > m_working_buffer
Buffer for out-of-place operations.
Concrete transformer for mathematical operations.
Template-flexible transformer base with instance-defined I/O types.
@ POWER
Power (sum of squares)
DataType transform_polynomial(DataType &input, const std::vector< double > &coefficients)
Polynomial transformation using existing Generator::Polynomial (IN-PLACE)
DataType transform_linear(DataType &input, double a, double b)
Linear transformation y = ax + b using C++20 ranges (IN-PLACE)
MathematicalOperation
Specific mathematical operations supported.
@ GAIN
Linear gain/attenuation.
@ QUANTIZE
Quantization/bit reduction.
@ TRIGONOMETRIC
Trigonometric functions.
@ LOGARITHMIC
Logarithmic transform.
@ EXPONENTIAL
Exponential transform.
DataType transform_logarithmic(DataType &input, double a, double b, double c=1.0, double base=std::numbers::e)
Logarithmic transformation y = a * log_base(b * x + c) (IN-PLACE)
DataType transform_normalize(DataType &input, const std::pair< double, double > &target_range={ -1.0, 1.0 })
Normalize transformation using existing infrastructure (IN-PLACE)
DataType transform_exponential(DataType &input, double a, double b, double base=std::numbers::e)
Exponential transformation y = a * base^(b * x) (IN-PLACE)
DataType transform_power(DataType &input, double exponent)
Power transformation y = x^exponent (IN-PLACE)
DataType transform_quantize(DataType &input, uint8_t bits)
Quantization transformation (bit reduction) using C++20 features (IN-PLACE)
DataType transform_trigonometric(DataType &input, TrigFunc trig_func, double frequency=1.0, double amplitude=1.0, double phase=0.0)
Trigonometric transformation using specified function (IN-PLACE)
TransformationType
Categories of transformation operations for discovery and organization.
Input/Output container for computation pipeline data flow with structure preservation.
Definition DataIO.hpp:24