Skip to content

Commit

Permalink
feat(math): update gaussian to have getter/setter methods instead of …
Browse files Browse the repository at this point in the history
…public members
  • Loading branch information
finger563 committed Jul 31, 2024
1 parent 6245807 commit 337c0ac
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 39 deletions.
101 changes: 72 additions & 29 deletions components/math/example/main/math_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,40 +76,83 @@ extern "C" void app_main(void) {

logger.info("=== gaussian ===");
{
//! [gaussian example]
std::array<float, 4> gammas = {
0.10f,
0.15f,
0.20f,
0.25f,
};
espp::Gaussian gaussian({
.gamma = gammas[0],
.alpha = 1.0f, // default
.beta = 0.5f, // default
});
float t = 0;
fmt::print("% t");
for (auto g : gammas) {
fmt::print(", gaussian({})", g);
{
//! [gaussian example]
std::array<float, 4> gammas = {
0.10f,
0.15f,
0.20f,
0.25f,
};
espp::Gaussian gaussian({
.gamma = gammas[0],
.alpha = 1.0f, // default
.beta = 0.5f, // default
});
float t = 0;
fmt::print("% t");
for (auto g : gammas) {
fmt::print(", gaussian({})", g);
}
fmt::print("\n");
float increment = 0.05f;
int num_increments = 1.0f / increment;
for (int i = 0; i <= num_increments; i++) {
fmt::print("{}", t);
for (auto g : gammas) {
// update the gamma
gaussian.set_gamma(g);
// evaluate it
float v = gaussian(t);
// print it
fmt::print(", {}", v);
}
fmt::print("\n");
t += increment;
}
//! [gaussian example]
}
fmt::print("\n");
float increment = 0.05f;
int num_increments = 1.0f / increment;
for (int i = 0; i <= num_increments; i++) {
fmt::print("{}", t);

{
//! [gaussian fade in fade out example]
std::array<float, 8> gammas = {
0.10f, 0.15f, 0.20f, 0.25f, 0.30f, 0.35f, 0.40f, 0.45f,
};
espp::Gaussian fade_in({
.gamma = gammas[0],
.alpha = 1.0f, // default
.beta = 1.0f, // default
});
espp::Gaussian fade_out({
.gamma = gammas[0],
.alpha = 1.0f, // default
.beta = 0.0f, // default
});
float t = 0;
fmt::print("% t");
for (auto g : gammas) {
// update the gamma
gaussian.gamma = g;
// evaluate it
float v = gaussian(t);
// print it
fmt::print(", {}", v);
fmt::print(", fade_in({}), fade_out({})", g, g);
}
fmt::print("\n");
t += increment;
float increment = 0.05f;
int num_increments = 1.0f / increment;
for (int i = 0; i <= num_increments; i++) {
fmt::print("{}", t);
for (auto g : gammas) {
// update the gamma
fade_in.set_gamma(g);
fade_out.set_gamma(g);
// evaluate it
float in = fade_in(t);
float out = fade_out(t);
// print it
fmt::print(", {}, {}", in, out);
}
fmt::print("\n");
t += increment;
}
//! [gaussian fade in fade out example]
}
//! [gaussian example]
}

logger.info("=== range mapper ===");
Expand Down
81 changes: 71 additions & 10 deletions components/math/include/gaussian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace espp {
*
* \section gaussian_ex1 Example
* \snippet math_example.cpp gaussian example
* \section gaussian_ex2 Fade-In/Fade-Out Example
* \snippet math_example.cpp gaussian fade in fade out example
*/
class Gaussian {
public:
Expand All @@ -32,19 +34,19 @@ class Gaussian {
* @param config Config structure for the gaussian.
*/
explicit Gaussian(const Config &config)
: gamma(config.gamma)
, alpha(config.alpha)
, beta(config.beta) {}
: gamma_(config.gamma)
, alpha_(config.alpha)
, beta_(config.beta) {}

/**
* @brief Evaluate the gaussian at \p t.
* @param t The evaluation parameter, [0, 1].
* @return The gaussian evaluated at \p t.
*/
float at(float t) const {
float tmb_y = (t - beta) / gamma; // (t - B) / y
float tmb_y = (t - beta_) / gamma_; // (t - B) / y
float power = -0.5f * tmb_y * tmb_y; // -(t - B)^2 / 2y^2
return alpha * exp(power);
return alpha_ * exp(power);
}

/**
Expand All @@ -55,10 +57,69 @@ class Gaussian {
*/
float operator()(float t) const { return at(t); }

float gamma; ///<! Slope of the gaussian, range [0, 1]. 0 is more of a thin spike from 0 up to
/// max output (alpha), 1 is more of a small wave around the max output (alpha).
float alpha; ///<! Max amplitude of the gaussian output, defautls to 1.0.
float beta; ///<! Shifting / Beta value for the gaussian, default to be
/// symmetric at 0.5 in range [0,1].
/**
* @brief Update the gaussian configuration.
* @param config The new configuration.
*/
void update(const Config &config) {
gamma_ = config.gamma;
alpha_ = config.alpha;
beta_ = config.beta;
}

/**
* @brief Set the configuration of the gaussian.
* @param config The new configuration.
*/
void set_config(const Config &config) { update(config); }

/**
* @brief Get the current configuration of the gaussian.
* @return The current configuration.
*/
Config get_config() const { return {.gamma = gamma_, .alpha = alpha_, .beta = beta_}; }

/**
* @brief Get the gamma value.
* @return The gamma value.
*/
float get_gamma() const { return gamma_; }

/**
* @brief Get the alpha value.
* @return The alpha value.
*/
float get_alpha() const { return alpha_; }

/**
* @brief Get the beta value.
* @return The beta value.
*/
float get_beta() const { return beta_; }

/**
* @brief Set the gamma value.
* @param gamma The new gamma value.
*/
void set_gamma(float gamma) { gamma_ = gamma; }

/**
* @brief Set the alpha value.
* @param alpha The new alpha value.
*/
void set_alpha(float alpha) { alpha_ = alpha; }

/**
* @brief Set the beta value.
* @param beta The new beta value.
*/
void set_beta(float beta) { beta_ = beta; }

protected:
float gamma_; ///<! Slope of the gaussian, range [0, 1]. 0 is more of a thin spike from 0 up to
/// max output (alpha), 1 is more of a small wave around the max output (alpha).
float alpha_; ///<! Max amplitude of the gaussian output, defautls to 1.0.
float beta_; ///<! Shifting / Beta value for the gaussian, default to be
/// symmetric at 0.5 in range [0,1].
};
} // namespace espp

0 comments on commit 337c0ac

Please sign in to comment.