Qt C++ 数据预处理笔记(1)——去均值
2023-12-27 10:51:20
#include "ZeroMeaningWorker.h"
#include "qdir.h"
#include "qelapsedtimer.h"
ZeroMeaningWorker::ZeroMeaningWorker(QString oStrDir, int iRow, QString oStrFileName, int iSecsStart, int iSecsEnd, QObject* parent)
: QObject{parent}, oStrDir(oStrDir), iRow(iRow), oStrFileName(oStrFileName), iSecsStart(iSecsStart), iSecsEnd(iSecsEnd)
{
}
void ZeroMeaningWorker::run()
{
oHead = PF::readHead(oStrFileName);
double dMean = this->calMean();
QElapsedTimer time;
time.start();
//获取新的文件名,梳妆滤波后的文件
QString oStrHead = PF::readHeadString(oStrFileName);
QStringList aoStrHead = oStrHead.split(',', Qt::KeepEmptyParts);
aoStrHead.replace(RAW_HEADER_DATA_CNT, QString::number(oHead.uiFS * (iSecsEnd - iSecsStart)));
aoStrHead.replace(RAW_HEADER_START_TIME, QString::number(oHead.iStartTimestamp + iSecsStart));
aoStrHead.replace(RAW_HEADER_SAMP_TIME, QString::number(iSecsEnd - iSecsStart));
QString oStrHeadNew = aoStrHead.join(",");
int iIndexStart = oStrFileName.lastIndexOf("_");
int iIndexEnd = oStrFileName.lastIndexOf(".");
QString oStrIndex = oStrFileName.mid(iIndexStart + 1, iIndexEnd - iIndexStart - 1);
QString oStrBaseName = QString("%1_L(%2)_S(%3)_D(%4)_CH%5_%6_%7_%8.dat")
.arg(oHead.oStrTaskName)
.arg(oHead.oStrLine)
.arg(oHead.oStrSite)
.arg(oHead.uiDev)
.arg(oHead.uiCh)
.arg(oHead.oStrRAW_COMPONENT)
.arg(QDateTime::fromSecsSinceEpoch(oHead.iStartTimestamp + iSecsStart).toString("yyyyMMddHHmmss"))
.arg(oStrIndex);
QString oStrFileNameNew = oStrDir + QDir::separator() + oStrBaseName;
qDebugV0() << oStrFileNameNew;
QFile oFileNew(oStrFileNameNew);
if( !oFileNew.open(QIODevice::WriteOnly) )
{
qDebugV0() << oStrFileName << " 打开失败";
return ;
}
oFileNew.write(oStrHeadNew.toUtf8(), LENGTH_178_BYTE);
QDataStream oStreamNew(&oFileNew);
oStreamNew.setByteOrder(QDataStream::BigEndian);
oStreamNew.setFloatingPointPrecision(QDataStream::SinglePrecision);
QFile oFileRaw(oStrFileName);
if( !oFileRaw.open(QIODevice::ReadOnly) )
{
qDebugV0() << oStrFileName << " 打开失败";
return ;
}
QDataStream oStreamRaw(&oFileRaw);
oStreamRaw.setByteOrder(QDataStream::BigEndian);
oStreamRaw.setFloatingPointPrecision(QDataStream::SinglePrecision);
oStreamRaw.skipRawData(LENGTH_178_BYTE);//作者是靠占位符来占据首行的。
for(int i = 0; i < iSecsStart; ++i)
{
oStreamRaw.skipRawData(oHead.uiFS * LENGTH_4_BYTE);
}
quint32 uiPercentage = PROGRESS_STEP;
oStreamRaw.startTransaction();
oStreamNew.startTransaction();
float value = 0.0;
for(qint64 i = 0; i < (iSecsEnd - iSecsStart); i++)
{
for(qint64 j = 0; j < oHead.uiFS; ++j)
{
oStreamRaw >> value;
oStreamNew << (value - dMean);
}
float fProgress = ((float)i / ((float)oHead.uiFS * (iSecsEnd - iSecsStart))) * 100;
if(fProgress >= uiPercentage)
{
emit sigProgress(iRow, "去均值~ ", fProgress);
uiPercentage = uiPercentage + PROGRESS_STEP;
}
}
oStreamRaw.commitTransaction();
oFileRaw.close();
oStreamNew.commitTransaction();
oFileNew.flush();
oFileNew.close();
emit sigProgress(iRow, "去均值完毕!", 100);
qint64 milsec = time.elapsed();
emit sigMsg((QString("第%1行\t加载采样点数:%2\t耗时:%3s")
.arg(iRow + 1)
.arg(oHead.uiFS * (iSecsEnd - iSecsStart))
.arg(QString::number(milsec / 1000.00, 'f', 3))));
}
double ZeroMeaningWorker::calMean()
{
double dAvg = 0.0000;
double dSum = 0.0000;
QFile oFile(oStrFileName);
if( !oFile.open(QIODevice::ReadOnly) )
{
qDebugV0() << oStrFileName << " 打开失败";
return 0.00;
}
QDataStream oStream(&oFile);
oStream.setByteOrder(QDataStream::BigEndian);
oStream.setFloatingPointPrecision(QDataStream::SinglePrecision);
oStream.skipRawData(LENGTH_178_BYTE);//作者是靠占位符来占据首行的。
for(int i = 0; i < iSecsStart; ++i)
{
oStream.skipRawData(oHead.uiFS * LENGTH_4_BYTE);
}
quint32 uiPercentage = PROGRESS_STEP;
oStream.startTransaction();
float value = 0.0;
for(qint64 i = 0; i < (iSecsEnd - iSecsStart); i++)
{
for(qint64 j = 0; j < oHead.uiFS; ++j)
{
oStream >> value;
dSum += value;
}
float fProgress = ((float)i / ((float)oHead.uiFS * (iSecsEnd - iSecsStart))) * 100;
if(fProgress >= uiPercentage)
{
emit sigProgress(iRow, "计算均值~ ", fProgress);
uiPercentage = uiPercentage + PROGRESS_STEP;
}
}
dAvg = dSum / (double)(oHead.uiFS * (iSecsEnd - iSecsStart));
oStream.commitTransaction();
oFile.close();
emit sigProgress(iRow, "均值计算完毕!", 100);
return dAvg;
}
#ifndef ZEROMEANINGWORKER_H
#define ZEROMEANINGWORKER_H
#include "PF.h"
#include "qrunnable.h"
#include <QObject>
class ZeroMeaningWorker : public QObject, public QRunnable
{
Q_OBJECT
public:
explicit ZeroMeaningWorker(QString oStrDir, int iRow, QString oStrFileName, int iSecsStart, int iSecsEnd, QObject* parent = nullptr);
void run();
double calMean();
signals:
void sigMsg(QString);
void sigProgress(int, QString, float);
private:
QString oStrDir;
int iRow;
QString oStrFileName;
int iSecsStart;
int iSecsEnd;
HEAD oHead;
};
#endif // ZEROMEANINGWORKER_H
UI 线程代码:
//去均值
void MainWindow::on_pushButtonZeroMeaning_clicked()
{
QList<int> aiRow = PF::getSelectedRows(ui->tableWidget);
if(aiRow.isEmpty())
{
return;
}
//选结果存放目录
QString oStrPath = poSet->value("PATH_SAVE").toString();
if(oStrPath.isEmpty())
{
oStrPath = QCoreApplication::applicationDirPath();
}
QString oStrDir = QFileDialog::getExistingDirectory(this, tr("选择去均值结果保存目录!"), oStrPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if(!oStrDir.isEmpty())
{
poSet->setValue("PATH_SAVE", oStrDir);
}
else
{
this->recvMsg("请选保存目录!");
QMessageBox::warning(nullptr, "警告", "请选保存目录!");
return;
}
for(int i = 0; i < aiRow.count(); ++i)
{
QString oStrFileName = ui->tableWidget->item(aiRow.at(i), TABLE_HEADER_FILE_NAME)->data(Qt::DisplayRole).toString();
int iSecsStart = ((QSpinBox*)ui->tableWidget->cellWidget(aiRow.at(i), TABLE_HEADER_START_SECOND))->value();
int iSecsEnd = ((QSpinBox*)ui->tableWidget->cellWidget(aiRow.at(i), TABLE_HEADER_END_SECOND ))->value();
ZeroMeaningWorker* poWorker = new ZeroMeaningWorker(oStrDir, aiRow.at(i), oStrFileName, iSecsStart, iSecsEnd);
poWorker->setParent(nullptr);
connect(poWorker, &ZeroMeaningWorker::sigMsg, this, &MainWindow::recvMsg);
connect(poWorker, &ZeroMeaningWorker::sigProgress, this, &MainWindow::recvProgress);
QThreadPool::globalInstance()->start(poWorker);
}
PF::scrollToRigth(ui->tableWidget);
}
文章来源:https://blog.csdn.net/tadpole_java/article/details/135238103
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!