I’ve used MatplotLib for plotting in Python.
This time I wanted to make some simple apps and plot the results in C++ and luckily there is interface to matplotlib for C++.
Here is their github repo.
If you have Matplotlib working you just need to include C++ header from repo above and may plot similarly easy as in Python.
Here is simple exercise to generate and plot sine wave.
Simple structure to keep x and y coordinates in one place:
struct Samples
{
Samples(unsigned int size) : x(size), y(size)
{
}
std::vector<double> x;
std::vector<double> y;
};
Then function to generate sine:
Samples generateSine(double A, double phi, double fSignal, double fSampling, double nT)
{
double Tsignal = 1.0 / fSignal;
unsigned int n = ceil(fSampling * Tsignal * nT);
Samples s(n);
for(int i = 0; i < n; ++i)
{
s.x.at(i) = i / fSampling;
s.y.at(i) = A * sin(s.x.at(i) * fSignal * 2 * M_PI + phi);
}
return s;
}
Then all of this together used with matplotlibcpp:
int main()
{
Samples s1 = generateSine(2,0,50,500,2);
Samples s2 = generateSine(2,M_PI/2,60,500,2);
plt::plot(s1.x, s1.y, "r");
plt::plot(s2.x, s2.y, "g");
plt::show();
}
