From a4b2defa178b10d277246ce5262704f1b0e2cff2 Mon Sep 17 00:00:00 2001 From: AkiyukiOkayasu Date: Fri, 10 Mar 2023 15:18:08 +0900 Subject: [PATCH] Add diode modeling function --- include/ame.hpp | 1 + include/ame_Circuit.hpp | 28 ++++++++++++++++++++++++++++ test/test.cpp | 14 ++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 include/ame_Circuit.hpp diff --git a/include/ame.hpp b/include/ame.hpp index 16955f2..86a2efc 100644 --- a/include/ame.hpp +++ b/include/ame.hpp @@ -29,6 +29,7 @@ #include "ame_Ambisonics.hpp" #include "ame_AudioBuffer.hpp" #include "ame_Biquad.hpp" +#include "ame_Circuit.hpp" #include "ame_Conversion.hpp" #include "ame_DcBlock.hpp" #include "ame_Delay.hpp" diff --git a/include/ame_Circuit.hpp b/include/ame_Circuit.hpp new file mode 100644 index 0000000..84506ec --- /dev/null +++ b/include/ame_Circuit.hpp @@ -0,0 +1,28 @@ +/** + Analog circuit modelings + @file ame_Circuit.hpp + @author Akiyuki Okayasu (akiyuki.okayasu@gmail.com) + @copyright Copyright (c) 2021 - Akiyuki Okayasu + + AME is released under the MIT license. +*/ + +#pragma once + +#include + +namespace ame +{ +/** diode modeling. + https://jatinchowdhury18.medium.com/complex-nonlinearities-epsiode-2-harmonic-exciter-cd883d888a43 + + @param x + @return FloatType + @note When the input is 1.0, the output is larger than 1.0 +*/ +template +inline FloatType diode (FloatType x) +{ + return static_cast (0.2) * (std::exp (x * static_cast (0.05 / 0.0259)) - static_cast (1.0)); +} +} // namespace ame \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp index ece5e72..7c22ead 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -9,6 +9,20 @@ using doctest::Approx; +TEST_CASE ("Circuit modeling") +{ + SUBCASE ("Diode") + { + CHECK_GT (ame::diode (1.0f), 1.0); + CHECK_EQ (ame::diode (0.5f), Approx (0.325089f)); + CHECK_EQ (ame::diode (0.1f), Approx (0.042589f)); + CHECK_EQ (ame::diode (0.0f), Approx (0.0f)); + CHECK_EQ (ame::diode (-0.25f), Approx (-0.076568f)); + CHECK_EQ (ame::diode (-0.8f), Approx (-0.157312f)); + CHECK_GT (0.0f, ame::diode (-1.0f)); + } +} + TEST_CASE ("Radian / Degree") { CHECK_EQ (ame::rad2deg (0.0f), Approx (0.0f));