C++Qt5.14.2版本不用UI形式绘制出频率响应曲线图和幅度响应曲线图
时间: 2024-03-07 13:48:15 浏览: 57
基于qt5.14.2版本Qchart绘制图表
要绘制频率响应曲线和幅度响应曲线,可以使用Qt自带的QCustomPlot库。下面是一个简单的例子,展示如何在不使用UI的情况下使用QCustomPlot绘制频率响应曲线和幅度响应曲线:
```cpp
#include <QApplication>
#include <QMainWindow>
#include "qcustomplot.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow window;
window.setWindowTitle("Frequency Response");
// create plot widget
QCustomPlot *plot = new QCustomPlot(&window);
window.setCentralWidget(plot);
// set plot title and axis labels
plot->plotLayout()->insertRow(0);
plot->plotLayout()->addElement(0, 0, new QCPTextElement(plot, "Frequency Response", QFont("sans", 12, QFont::Bold)));
plot->xAxis->setLabel("Frequency");
plot->yAxis->setLabel("Amplitude");
// create data for frequency response curve
QVector<double> frequency(1001), response(1001);
for (int i = 0; i < 1001; i++)
{
frequency[i] = i / 10.0;
response[i] = qSin(frequency[i] / 10.0) / (frequency[i] / 10.0);
}
// add frequency response curve to plot
QCPGraph *freqResponse = plot->addGraph();
freqResponse->setData(frequency, response);
freqResponse->setPen(QPen(Qt::blue));
// create data for amplitude response curve
QVector<double> amplitude(101), dB(101);
for (int i = 0; i < 101; i++)
{
amplitude[i] = i / 100.0;
dB[i] = 20.0 * qLn(amplitude[i]) / qLn(10.0);
}
// add amplitude response curve to plot
QCPGraph *ampResponse = plot->addGraph();
ampResponse->setData(amplitude, dB);
ampResponse->setPen(QPen(Qt::red));
// set plot ranges and axes scales
plot->xAxis->setRange(0, 100);
plot->yAxis->setRange(-30, 10);
plot->xAxis->setScaleType(QCPAxis::stLogarithmic);
plot->yAxis->setScaleType(QCPAxis::stLogarithmic);
plot->yAxis->setNumberFormat("eb");
plot->yAxis->setNumberPrecision(0);
// show the window
window.show();
return a.exec();
}
```
在这个例子中,我们创建了一个`QCustomPlot`对象,并将其设置为主窗口的中央部件。然后,我们设置了绘图的标题和坐标轴标签,并创建了用于绘制频率响应曲线和幅度响应曲线的数据。最后,我们将这些数据添加到绘图中,并设置了绘图的范围和刻度。运行程序后,您应该能够看到绘制的频率响应曲线和幅度响应曲线。
阅读全文