QWT和FFT的编译这里不做赘述,后面可能会写fft的编译,qwt的编译只需要,qmake和make install,即可编译成功。这篇文章代码,连同上一篇文章的代码即可显示实时曲线。
上一篇《**基于QT嵌入式ARM数据采集卡上位机(二)—— 页面布局**》
下一篇《(项目实战)基于QT嵌入式ARM数据采集卡上位机(四)—— 网络传输协议(UDP组播)》
- (项目实战)基于QT嵌入式ARM数据采集卡上位机(三)——qwt和fft的基础配置使用
- 1. qwt设置代码
- 2. FFT基础使用
- 整合项目
由于使用是第三方库,所以我们需要在 .pro文件中添加
include(qwt/qwt.pri) include(fftw2/fftw2.pri)
注意:这里需要添加你自己环境下第三方库的路径。
1. qwt设置代码在使用qwt时,需要在.pro文件中添加
CONFIG += qwt
添加qwt.pri文件
# QWT 安装路径 QWT_INSTALL_PATH = $$PWD # QWT 插件相关库所在路径(例如:xx.lib) QWT_LIB_PATH = $$QWT_INSTALL_PATH/lib # QWT 插件相关头文件所在路径(例如:xx.h) QWT_INCLUDE_PATH = $$QWT_INSTALL_PATH/include INCLUDEPATH += $$QWT_INCLUDE_PATH LIBS += -L$$QWT_LIB_PATH -lqwt LIBS += -L$$QWT_LIB_PATH -lQt5Concurrent
baseplot.cpp文件对应qwt的使用,代码如下
#include "baseplot.h" #include "qwt_picker_machine.h" #include "qwt_scale_widget.h" #include "qwt_plot_layout.h" #include "qwt_text.h" #include "qwt_scale_engine.h" #include#include #include #include #include #include //#include BasePlot::BasePlot(QString str,QWidget *parent) : QWidget(parent) { baseplot = new QwtPlot(); baseplot->setTitle(str); // baseplotsettitle("QString str"); basePlotCurve = new QwtPlotCurve("I"); BasePlotInitGui(baseplot,basePlotCurve); basePlotCurve->setLegendAttribute(basePlotCurve->LegendShowLine );//show the line style showlegend(); } BasePlot::BasePlot(QWidget *parent): QWidget(parent) { baseplot = new QwtPlot(); basePlotCurve = new QwtPlotCurve("I"); BasePlotInitGui(baseplot,basePlotCurve); } void BasePlot::plotwave(QVector x, QVector y) { // qDebug() << x < setSamples(x,y); baseplot->replot(); } void BasePlot::plotcurve() { baseplot->replot(); } void BasePlot::BasePlotInitGui(QwtPlot *basePlot,QwtPlotCurve *baseplotcurve) { QHBoxLayout *hlayout = new QHBoxLayout(this); hlayout->addWidget(basePlot); baseplotcurve->attach(basePlot); baseplotcurve->setPen(QPen(Qt::blue)); baseplotcurve->setPen(QColor(80,128,128),1.0,Qt::DashLine); this->setStyleSheet(" border-radius:8px;"); this->setStyleSheet("font-family: Palatino Linotype;"); this->setAttribute(Qt::WA_TranslucentBackground,true); //设置窗体透明 QSizePolicy sizepolicy; sizepolicy.setVerticalPolicy(QSizePolicy::Ignored); sizepolicy.setHorizontalPolicy(QSizePolicy::Ignored);// basePlot->setSizePolicy(sizepolicy); } void BasePlot::baseplotsettitle(QString str) { baseplot->setTitle(str); } void BasePlot::setXfixd() { } void BasePlot::AttachPlotCurve(QwtPlotCurve *baseplotcurve,const QPen &pen) { baseplotcurve->attach(baseplot); baseplotcurve->setPen(pen); baseplotcurve->setLegendAttribute(basePlotCurve->LegendShowLine );//show the line style } void BasePlot::showlegend() { QwtLegend *legend = new QwtLegend; baseplot->insertLegend(legend, QwtPlot::RightLegend ); }
baseplot.h文件
#ifndef BASEPLOT_H #define BASEPLOT_H #include2. FFT基础使用#include "qwt_plot.h" #include "qwt_plot_curve.h" #include "qwt_plot_grid.h" #include "qwt_plot_picker.h" #include "qwt_plot_marker.h" #include "qwt_plot_zoomer.h" #include "qwt_plot_panner.h" #include #include #include class BasePlot : public QWidget { Q_OBJECT public: explicit BasePlot(QString str,QWidget *parent = nullptr); explicit BasePlot(QWidget *parent = nullptr); void plotwave(QVector x,QVector y); void plotcurve(); void baseplotsettitle(QString str); void setXfixd(); void AttachPlotCurve(QwtPlotCurve *baseplotcurve,const QPen &pen); void showlegend(); // void setStyle(); signals: private: QwtPlot *baseplot; QwtPlotCurve *basePlotCurve; QTimer replottimer; QTimer dataproduce; QVector m_xDataVector; // x坐标数据 QVector m_yDataVector; // y坐标数据 unsigned int timecnt = 0; void BasePlotInitGui(QwtPlot *basePlot,QwtPlotCurve *baseplotcurve); }; #endif // BASEPLOT_H
在使用之前,应先添加 fftw2.pri,以保证qt能够找到第三方链接库
# FFTW 安装路径 FFTW_INSTALL_PATH = $$PWD # FFTW 插件相关库所在路径(例如:xx.lib) FFTW_LIB_PATH = $$FFTW_INSTALL_PATH/lib # FFTW 插件相关头文件所在路径(例如:xx.h) FFTW_INCLUDE_PATH = $$FFTW_INSTALL_PATH/include INCLUDEPATH += $$FFTW_INCLUDE_PATH LIBS += -L$$FFTW_LIB_PATH -lfftw3
sllfft.cpp
#include "sllfft.h" #includesllFFT::sllFFT(qint16 len) { nLen = len; fftInit(nLen); } void sllFFT::pushdata(QVector valueVector) { static QVector fft_In; for (int i=0; i = nLen){ fftunit(fft_In,nLen); emit processCompleted(this->xAxis, this->outvalue); // qDebug() << outvalue.size(); fft_In.resize(0); // qDebug()<<""<<"t_vector"< Value,qint16 len) { QVector psd_amp(len); for (quint16 i = 0;i < len;i ++) { psd_amp[i] = 0.5*(1-qCos(2*M_PI*i/(len-1)))*qSqrt(1.633); Value[i]=Value[i]*psd_amp[i]; } plan_fft = fftw_plan_dft_r2c_1d(len, &Value[0], out_fft, FFTW_ESTIMATE); fftw_execute(plan_fft);// 执行变换 QVector psd_amptemp(len/2); for (quint16 i = 0;i < len/2;i ++) { psd_amptemp[i] = qSqrt(((out_fft[i][0]*out_fft[i][0]+out_fft[i][1]*out_fft[i][1])) /(len*DATA_SAMPLING_FREQ/4)); } fftw_destroy_plan(plan_fft); outvalue.resize(psd_amptemp.size()); // qDebug() << xAxis.size() << outvalue.size(); for (int i=0;i sllfft.h
#ifndef SLLFFT_H #define SLLFFT_H #include整合项目#include "fftw3.h" #include #include class sllFFT : public QObject { #define DATA_SAMPLING_FREQ 1000 Q_OBJECT public: explicit sllFFT(qint16 len); void pushdata(QVector valueVector); signals: void processCompleted(QVector t_vectorX, QVector t_vectorY); private: qint16 nLen; QVector > outvaluetemp; QVector outvalue; QVector xAxis; fftw_plan plan_fft; fftw_complex *out_fft; void fftInit(qint16 len); void fftunit(QVector Value,qint16 len); char AddData(double value); }; #endif // SLLFFT_H 在function.cpp中增加以下函数内容
void functionui::checkboxstyle(QCheckBox *box) { box->setStyleSheet("QCheckBox::indicator {width: 22px; height: 22px;}" "QCheckBox::indicator:checked {image: url(:/icon/redselct.jpg);}"); } void functionui::signalprintplot(QVectorx, QVector y) { signalplot->plotwave(x,y); } void functionui::pushfftdata(QVector , QVector y) { fft->pushdata(y); } void functionui::fftslot(QVector x, QVector y) { fftplot->plotwave(fftXaxis,y); } void functionui::threaddestroy() { qDebug() << "testbutton"; thread->quit(); thread->wait(); } void functionui::texteditshow(QString str) { showtext->insertPlainText(str); showtext->insertPlainText("n"); } void functionui::texteditshow(quint8 *value) { qDebug() << *value; QString str; for(quint16 i = 0; i < 5; i ++){ str.append(*(value+i)); } showtext->insertPlainText(str); showtext->insertPlainText("n"); } void functionui::texteditshow(QVector value) { Yaxis.append(value); if(Yaxis.size() >= 980) Yaxis.remove(0,20); emit fftsignal(Xaxis, Yaxis); if(showchanel1sign) curve->setSamples(Xaxis,Yaxis); if(showchanel2sign) chanel2curve->setSamples(Xaxis,Yaxis); if(showchanelsign) chanel3curve->setSamples(Xaxis,Yaxis); signalplot->plotcurve(); }



