Comparing to the matplotlib.h, the root lib. is another good choice if you want to numerically analysis or visualize the data in C++ program. Here is a simple demo that shows a waveform and a spectrum together. In last, for a purpose of testing the plotting speed of the root core, we run the same data N times to calculate the time.
-
root_exercise.cpp : initialize the Classes and modules.
-
fftw3.h & libfftw3-3.dll & libfftw3-3.lib : It's a high efficiency DFT program. if you like to know details, please refer to https://www.fftw.org/
-
rootplot.h & rootplot.cpp : It's a simple class to integrate the root core functions for 2D numerical plot. website: https://root.cern/
-
install VS2019 for Cpp coding.
-
install root from the official download website. downlading the *.exe release version for windows base.
ps: my version is "root_v6.24.06". my install dir: C:\root_v6.24.06
- add windows environmental parameters as following
PATH: C:\root_v6.24.06\bin
- open the CPP project property, adding the include and lib address:
Additional include Directories: C:\root_v6.24.06\include
Additional Library Directories: C:\root_v6.24.06\lib; <PRJ_DIR>/lib/
Add dependencies: C:\root_v6.24.06\lib*.lib; libfftw3-3.lib
To begining with, we first generate the data from .csv and initialize the root core.
int window_size = 2048; //sample length of display
//initialize rootplot core
m_rootplot = new rootplot(window_size);
m_rootplot->init(&argc, argv);
ifstream infile("../data/T40M_250MSPS.csv", ios::in);
string line;
vector<struct Point> DATA_40M;
getline(infile, line);
while (getline(infile, line)) {
stringstream ss(line);
string str;
Point adc_data;
getline(ss, str, ',');
adc_data.x = stod(str) * 4; //unit: ns
getline(ss, str, ',');
adc_data.y = stold(str); //unit V
DATA_40M.push_back(adc_data);
}
Here is to prepare the DFT data to plot later by FFTW3 API
//prepare DFT Data
int size = DATA_40M.size();
SetDFT(size);
SetWindow(HANNING, window_data, size);
std::vector<Point> FFT_Data;
int min_freq = 1e3;//unit: kHz
int max_freq = 8e4;
FFT_Data = performDFT(&DATA_40M, min_freq, max_freq);
if (FFT_Data.size() < window_size || size < window_size) {
printf("error1\r\n");
return 0;
}
Finally, I use rootplot to visualize the data and test the plotting speed.
//performance test
double tt_start = clock();
int test_counter = 0;
int N = 100;
while (test_counter < N) {
//if needed, you can change the data here
//codes
//
//
m_rootplot->plt(&FFT_Data, &DATA_40M);
test_counter++;
}
double tt_end = clock();
double duration = (tt_end - tt_start);
printf("draw_time(%d frames) = %.2f ms\r\n",N, duration);
N=100, the cost time = ~2000 ms (matplotlib needs around 4000-5000 ms)