Skip to content

Commit

Permalink
Add diode modeling function
Browse files Browse the repository at this point in the history
  • Loading branch information
AkiyukiOkayasu committed Mar 10, 2023
1 parent f590a48 commit a4b2def
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/ame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
28 changes: 28 additions & 0 deletions include/ame_Circuit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
Analog circuit modelings
@file ame_Circuit.hpp
@author Akiyuki Okayasu ([email protected])
@copyright Copyright (c) 2021 - Akiyuki Okayasu
AME is released under the MIT license.
*/

#pragma once

#include <concepts>

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 <std::floating_point FloatType>
inline FloatType diode (FloatType x)
{
return static_cast<FloatType> (0.2) * (std::exp (x * static_cast<FloatType> (0.05 / 0.0259)) - static_cast<FloatType> (1.0));
}
} // namespace ame
14 changes: 14 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit a4b2def

Please sign in to comment.