Last active
October 28, 2022 07:39
-
-
Save Simon-L/19298baf021ae89ff4d426950134d11c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
#include <matplot/matplot.h> | |
#include "chowdsp_wdf/chowdsp_wdf.h" | |
using namespace chowdsp::wdft; | |
class vca_1 | |
{ | |
public: | |
vca_1() = default; | |
float _vcc = 0.0; | |
void prepare (double sampleRate) | |
{ | |
C1.prepare ((float) sampleRate); | |
} | |
void reset() | |
{ | |
C1.reset(); | |
} | |
void setCircuitParams () | |
{ | |
} | |
void setCircuitElements (float vcc, float vinRes, float vccRes) | |
{ | |
_vcc = vcc; | |
Vcc.setVoltage(vcc); | |
Vin.setVoltage (0.0); | |
Vin.setResistanceValue(vinRes); | |
Vcc.setResistanceValue(vccRes); | |
} | |
inline float processSample (float x) | |
{ | |
Vcc.setVoltage(_vcc); | |
Vin.setVoltage (x); | |
d1.incident (P1.reflected()); | |
P1.incident (d1.reflected()); | |
return current<float> (d1); | |
} | |
ResistiveVoltageSourceT<float> Vcc; | |
ResistiveVoltageSourceT<float> Vin; | |
CapacitorT<float> C1 { 22.0e-9f }; | |
WDFSeriesT<float, decltype (Vin), decltype (C1)> S1 { Vin, C1 }; | |
PolarityInverterT<float, decltype (S1)> inv {S1}; | |
WDFParallelT<float, decltype (Vcc), decltype (inv)> P1 { Vcc, inv }; | |
PolarityInverterT<float, decltype (P1)> inv2 {P1}; | |
// DiodeT<float, decltype (inv2)> d1 { inv2, 0.0115e-9f }; | |
DiodeT<float, decltype (inv2)> d1 { inv2, 2.52e-9f }; | |
}; // vca_1 | |
class vca_2 | |
{ | |
public: | |
vca_2() = default; | |
void prepare (double sampleRate) | |
{ | |
C1.prepare ((float) sampleRate); | |
} | |
void reset() | |
{ | |
C1.reset(); | |
} | |
void setCircuitParams () | |
{ | |
} | |
void setCircuitElements (float iinRes, float venvRes) | |
{ | |
Iin.setResistanceValue(iinRes); | |
Venv.setResistanceValue(venvRes); | |
} | |
inline float processSample (float iin, float venv) | |
{ | |
Iin.setCurrent (iin); | |
Venv.setVoltage(venv); | |
d1.incident (S1.reflected()); | |
S1.incident (d1.reflected()); | |
return voltage<float> (invS1) + voltage<float> (C1); | |
} | |
ResistiveVoltageSourceT<float> Venv; | |
ResistiveCurrentSourceT<float> Iin; | |
CapacitorT<float> C1 { 2.7e-9f }; | |
WDFParallelT<float, decltype (C1), decltype (Iin)> P1 { C1, Iin }; | |
PolarityInverterT<float, decltype (C1)> invC1 {C1}; | |
WDFSeriesT<float, decltype (Venv), decltype (P1)> S1 { Venv, P1 }; | |
PolarityInverterT<float, decltype (S1)> invS1 {S1}; | |
DiodeT<float, decltype (invS1)> d1 { invS1, 2.52e-9f }; | |
}; // vca_2 | |
using namespace matplot; | |
std::vector<float> venv; | |
std::vector<float> in; | |
std::vector<float> in2; | |
std::vector<float> in3; | |
std::vector<float> in4; | |
std::vector<float> out; | |
std::vector<float> out2; | |
std::vector<float> out3; | |
vca_1 vca1; | |
vca_2 vca2; | |
int main() { | |
float m_time = 0.0; | |
float m_amplitude = 0.23; | |
float m_frequency = 440.0; | |
float sr = 48000.0; | |
float oneOverSr = 1/sr; | |
float venvStep = 2.089e-3; | |
float venvStart = 6.0; | |
venv.resize(1024); | |
for (int i = 0; i < 1024; ++i) { | |
venv[i] = venvStart - i * venvStep; | |
} | |
vca1.prepare(sr); | |
vca2.prepare(sr); | |
// Vcc Vinres Vccres | |
vca1.setCircuitElements(12.0, 100.0, 2200000.0); | |
// Iinres Venvres | |
vca2.setCircuitElements(20.0e6, 15.0e3); | |
in.resize(1024); | |
in2.resize(1024); | |
in3.resize(1024); | |
in4.resize(1024); | |
out.resize(1024); | |
out2.resize(1024); | |
out3.resize(1024); | |
for (int i = 0; i < 1024; ++i) | |
{ | |
float value = m_amplitude * sin(2 * M_PI * m_frequency * m_time); | |
in[i] = value; | |
m_time += oneOverSr; | |
vca1.processSample(in[i]); | |
out[i] = (current<float> (vca1.d1) * 1e-12) * 1.6727; | |
vca2.processSample(out[i], venv[i]); | |
out2[i] = voltage<float> (vca2.C1) * -1.0 + voltage<float> (vca2.d1); | |
out3[i] = voltage<float> (vca1.d1); | |
} | |
for (int i = 0; i < 1024; ++i) { | |
} | |
// plot(in); | |
// plt::plot(in2, "red"); | |
// plt::plot(in3, "green"); | |
// hold(on); | |
// plot(out)->use_y2(true); | |
// plot(out3); | |
// plot(out); | |
plot(out2); | |
// plot(venv, "red"); | |
show(); | |
} | |
// int main() { | |
// auto x = linspace(0, 10); | |
// auto y1 = transform(x, [](double x) { return sin(3 * x); }); | |
// plot(x, y1); | |
// hold(on); | |
// auto z = transform(x, [](double x) { return sin(3 * x) * exp(0.5 * x); }); | |
// plot(x, z)->use_y2(true); | |
// y2lim({-150, 150}); | |
// show(); | |
// return 0; | |
// } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Untar this in current dir | |
# https://github.com/alandefreitas/matplotplusplus/releases/download/v1.1.0/matplotplusplus-1.1.0-Linux.tar.gz | |
LDFLAGS = lib/libmatplot.a lib/Matplot++/libnodesoup.a -lpng -lz -ltiff -ljpeg | |
CXXFLAGS = -std=c++17 -I../chowdsp_wdf/include -Iinclude | |
SRC = main.cpp | |
OUT = main | |
$(OUT): $(SRC) | |
$(CXX) $(CXXFLAGS) -o $(SRC).o -c $(SRC) | |
$(CXX) $(SRC).o $(LDFLAGS) -o $(OUT) | |
run: $(OUT) | |
./$(OUT) | |
clean: | |
rm $(OUT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment