已知截止频率Pf,增益Gain,增益因子Q,利用Qt qcustomplot库分别画出LowShelf滤波和HighShelf在20Hz到20kHz区间范围内的频响曲线
时间: 2024-03-18 13:40:54 浏览: 91
一个用Qt来绘制频谱图
4星 · 用户满意度95%
好的,我会给出利用Qt qcustomplot库分别画出LowShelf滤波和HighShelf在20Hz到20kHz区间范围内的频响曲线的代码。
首先,我们需要导入qcustomplot库并创建一个QCustomPlot对象。然后,我们需要设置坐标轴的范围和标签。以下是创建坐标轴的代码:
```cpp
// 创建QCustomPlot对象
QCustomPlot *customPlot = new QCustomPlot(this);
customPlot->setGeometry(10, 10, 500, 300);
// 创建对数坐标轴
customPlot->xAxis->setScaleType(QCPAxis::stLogarithmic);
customPlot->yAxis->setScaleType(QCPAxis::stLogarithmic);
customPlot->xAxis->setRange(20, 20000);
customPlot->yAxis->setRange(-40, 20);
customPlot->xAxis->setLabel("Frequency (Hz)");
customPlot->yAxis->setLabel("Gain (dB)");
```
接下来,我们需要计算LowShelf滤波器和HighShelf滤波器的频响曲线。以下是计算频响曲线的代码:
```cpp
// 计算LowShelf滤波器频响曲线
QVector<double> xLow, yLow;
for (double i = 20; i <= 20000; i *= 1.01) {
double gainLow = 10 * log10(qPow(Q, 2)/((qPow(i, 2)/qPow(Pf, 2)) + 1 - (i/Pf) * Q));
double gainLowShelf = Gain + gainLow;
xLow.append(i);
yLow.append(gainLowShelf);
}
// 计算HighShelf滤波器频响曲线
QVector<double> xHigh, yHigh;
for (double i = 20; i <= 20000; i *= 1.01) {
double gainHigh = 10 * log10(qPow(i, 2)/((qPow(Pf, 2))/qPow(Q, 2) + (i/Pf) * Q + 1));
double gainHighShelf = Gain + gainHigh;
xHigh.append(i);
yHigh.append(gainHighShelf);
}
```
最后,我们可以使用addGraph()函数将LowShelf滤波器和HighShelf滤波器的频响曲线绘制到qcustomplot上。以下是完整的代码示例:
```cpp
// 创建QCustomPlot对象
QCustomPlot *customPlot = new QCustomPlot(this);
customPlot->setGeometry(10, 10, 500, 300);
// 创建对数坐标轴
customPlot->xAxis->setScaleType(QCPAxis::stLogarithmic);
customPlot->yAxis->setScaleType(QCPAxis::stLogarithmic);
customPlot->xAxis->setRange(20, 20000);
customPlot->yAxis->setRange(-40, 20);
customPlot->xAxis->setLabel("Frequency (Hz)");
customPlot->yAxis->setLabel("Gain (dB)");
// 计算LowShelf滤波器频响曲线
QVector<double> xLow, yLow;
for (double i = 20; i <= 20000; i *= 1.01) {
double gainLow = 10 * log10(qPow(Q, 2)/((qPow(i, 2)/qPow(Pf, 2)) + 1 - (i/Pf) * Q));
double gainLowShelf = Gain + gainLow;
xLow.append(i);
yLow.append(gainLowShelf);
}
// 计算HighShelf滤波器频响曲线
QVector<double> xHigh, yHigh;
for (double i = 20; i <= 20000; i *= 1.01) {
double gainHigh = 10 * log10(qPow(i, 2)/((qPow(Pf, 2))/qPow(Q, 2) + (i/Pf) * Q + 1));
double gainHighShelf = Gain + gainHigh;
xHigh.append(i);
yHigh.append(gainHighShelf);
}
// 绘制频响曲线
customPlot->addGraph();
customPlot->graph(0)->setData(xLow, yLow);
customPlot->graph(0)->setPen(QPen(Qt::blue));
customPlot->addGraph();
customPlot->graph(1)->setData(xHigh, yHigh);
customPlot->graph(1)->setPen(QPen(Qt::red));
customPlot->replot();
```
这样,我们就可以在20Hz到20kHz区间范围内绘制出LowShelf滤波器和HighShelf滤波器的频响曲线了。
阅读全文