QT 频谱转换绘制

QT绘制频谱主要涉及到两个方面 绘图和 和转换,用到了qcustomplot和fftw主要记录一下这两个工具的用法

1 qcustomplot 使用方法

1)QcustomPlot是QT提供的一个第三方库,在使用前需要在QcustomPlot官网上进行下载

2)把解压完的QcustomPlot压缩包中的qcustomplot.h和qcustomplot.cpp文件添加到工程文件中来

3)创建一个weiget提升为QcustomPlot

4)初始化代码

void MainWindow::customPlot_init()
{
    QLinearGradient plotGradient;
    plotGradient.setStart(0, 0);
    plotGradient.setFinalStop(0, 350);
    plotGradient.setColorAt(0, QColor(50, 50, 50));
    plotGradient.setColorAt(1, QColor(50, 50, 50));

    ui->qcustomplot->setBackground(plotGradient);
    ui->qcustomplot->xAxis->setTickLabelColor(Qt::white);
    ui->qcustomplot->yAxis->setTickLabelColor(Qt::white);
    ui->qcustomplot->xAxis->setBasePen(QPen(QColor(140, 140, 140),1));
    ui->qcustomplot->yAxis->setBasePen(QPen(QColor(140, 140, 140),1));
    ui->qcustomplot->xAxis2->setBasePen(QPen(QColor(140, 140, 140),1));
    ui->qcustomplot->yAxis2->setBasePen(QPen(QColor(140, 140, 140),1));
    ui->qcustomplot->xAxis2->setVisible(true);
    ui->qcustomplot->xAxis2->setTickLabels(false);
    ui->qcustomplot->yAxis2->setVisible(true);
    ui->qcustomplot->yAxis2->setTickLabels(false);

    ui->qcustomplot->addGraph();
    ui->qcustomplot->graph(0)->setPen(QPen(Qt::green));
    //ui->qcustomplot->graph(0)->setData(data_x, data_y);

//    ui->qcustomplot->addGraph();
//    ui->qcustomplot->graph(1)->setPen(QPen(Qt::yellow));
    //ui->qcustomplot->graph(1)->setData(data_x, data_y2);

    double xAxis_Max = 2100;
    double xAxis_Min = 0;
    double yAxis_Max = 100;
    double yAxis_Min = 0;

    ui->qcustomplot->xAxis->setRange(xAxis_Min,xAxis_Max);
    ui->qcustomplot->yAxis->setRange(yAxis_Min,yAxis_Max);
    ui->qcustomplot->setInteractions(QCP::iRangeZoom | QCP::iSelectPlottables | QCP::iSelectItems );
    ui->qcustomplot->setSelectionRectMode(QCP::srmZoom);
    ui->qcustomplot->showTracer(true);
}

2 fftw用法

1)可以直接去它的官网下载他的预编译版本

2)根据需求编译自身需要的版本

3)接入到自己项目中(代码如下)

void MainWindow::testfft()
{
    int N = 2048;
    fftw_complex *in, *out;
    fftw_plan p;
    in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * N);
    out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * N);
    p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
    QVector<double> in_1,in_2;
    int index=0;
    double max=0;
    int n;
    for (n = 0; n < N; n++)
    {
        in[n][1] = 10*sin(40*3.1415926*n/800);
        in[n][0] = 10*cos(40*3.1415926*n/800);
        in_1<<in[n][1];
        in_2<<in[n][0];
    }

    fftw_execute(p);
    fftw_destroy_plan(p);

    QVector<double> x1,y1;

    for (n = 0; n < N; n++)
    {
        //printf("%3.2lf+%3.2lfi    ", out[n][0], out[n][1]);
        x1<<n;
        //y1<<out[n][0];
        double value = 10*log10(out[n][0]*out[n][0]+out[n][1]*out[n][1]);
        y1<<value;
        if(value >max) max = value;
    }
    for(n = 0; n < N/2; n++)
    {
        int i = n, j = N/2+n;
        double temp = 0;
        temp = y1[i];
        y1[i] = y1[j];
        y1[j] = temp;
        if(y1[i] == max) index = i;
        if(y1[j] == max) index = j;
    }
    printf("\n");
    fftw_free(in);
    fftw_free(out);

    //画图
    ui->qcustomplot->graph(0)->setData(x1,y1);
    ui->qcustomplot2->graph(0)->setData(x1,in_1);
    ui->qcustomplot2->graph(1)->setData(x1,in_2);

    QCPGraph* graph = ui->qcustomplot->graph(0);
    AxisTag* mTag1 = new AxisTag(graph->valueAxis());
    mTag1->setPen(graph->pen());
    mTag1->updatePosition(index,max);
    mTag1->setText(QString::number(max, 'f', 2));
    ui->qcustomplot->replot();
    ui->qcustomplot2->replot();

}

最终效果图

  • 5
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值