FFT (in MATLAB) to C++ Model to QML
-
Hello, I want to take FFT in simulink and somehow make my C++ model get all that data. I will just get the data and put in constuctor just like I am doing with sine wave.
How can I do this? or any alternative
Everything found online was connect QML and Simulink.
I want to Simulink -> C++ Model -> QMLI have made a C++ model that looks like this:
DataModel.h
#ifndef DATAMODEL_H #define DATAMODEL_H #include <QObject> #include <QAbstractTableModel> #include <QList> #include <QPointF> class DataModel : public QAbstractTableModel { Q_OBJECT public: enum { ValueRole = Qt::DisplayRole }; explicit DataModel(QObject *parent = nullptr); int rowCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const; QVariant data(const QModelIndex &index, int role) const; QHash<int, QByteArray> roleNames() const; signals: private: QList <QPointF> m_points; }; #endif // DATAMODEL_H
#include "DataModel.h" #include "math.h" DataModel::DataModel(QObject *parent) : QAbstractTableModel{parent} { // code to produce sine for (int i = 0; i < 100; ++i) m_points.append(QPointF(i, qSin(i)*0.9)); } int DataModel::rowCount(const QModelIndex &parent) const { return m_points.size(); } int DataModel::columnCount(const QModelIndex &parent) const { return 2; } QVariant DataModel::data(const QModelIndex &index, int role) const { //if (!index.isValid() || role != Qt::DisplayRole) // return QVariant(); const QPointF p = m_points[index.row()]; if (index.column() == 0) return p.x(); return p.y(); } QHash<int, QByteArray> DataModel::roleNames() const { QHash<int, QByteArray> mappings; mappings [ValueRole] = "value"; return mappings; }
main.cpp
#include <QApplication> #include <QQmlApplicationEngine> #include "DataModel.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); QQmlApplicationEngine engine; DataModel MyModel; qmlRegisterSingletonInstance ("hello.com",1,0,"MyData",&MyModel); const QUrl url(u"qrc:/CPPModelForChart/main.qml"_qs); QObject::connect( &engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
main.qml
import QtQuick import QtCharts import hello.com as Peter Window { width: 640 height: 480 visible: true title: qsTr("Hello World") ChartView { anchors.fill: parent LineSeries { id: lineSeries axisX: ValuesAxis { min: 0 max: 10 } axisY: ValuesAxis { min: -1 max: 1 } //useOpenGL: true } VXYModelMapper { xColumn: 0 yColumn: 1 firstRow: 0 series: lineSeries model: Peter.MyData } } }
-
I'm not an expert for QML, but I might help with some of your other questions. If you insist on using Matlab/Simulink, I have heard that there are so called mex files which will export your Matlab programs to be used from C/C++. This way you could try to export functionality from Matlab to be used by your program.
I would rather suggest to look for an alternative. The goto library for FFT would be FFTW (https://www.fftw.org/).
-
BTW: I see you are using the QCharts module. This is only available under the GPL or commercial license, not LGPL. If you don't have the commercial license this automatically makes your software open source under the GPL. It is not a problem if you are using the software for yourself, but you always have to provide your source code to anybody who has the software. (If this is not your intention, there is Qwt for charts (though I don't think it supports QML directly.)
-
I'm not an expert for QML, but I might help with some of your other questions. If you insist on using Matlab/Simulink, I have heard that there are so called mex files which will export your Matlab programs to be used from C/C++. This way you could try to export functionality from Matlab to be used by your program.
I would rather suggest to look for an alternative. The goto library for FFT would be FFTW (https://www.fftw.org/).
@SimonSchroeder I tried to export my MATLAB code as C++ files. And I got a lot of header and cpp files. Now I don't know how to put them in Qt.
So, I'll use FFTW -
@SimonSchroeder I tried to export my MATLAB code as C++ files. And I got a lot of header and cpp files. Now I don't know how to put them in Qt.
So, I'll use FFTW@Sajjad-Ali said in FFT (in MATLAB) to C++ Model to QML:
Now I don't know how to put them in Qt
You don't put this code in Qt.
You either build it as part of your application or as a library and link your application with that library.