#include "RT_TCM_AE.h"
//para1:信号 para2:平滑数
RT_TCM_AE::RT_TCM_AE(vector<float>Ae_data, int filter_size, int Frequency, int cut_threshold) :PI(3.1416), Frequency_length(65536)
{
this->len = filter_size;
this->_Frequency = Frequency;
this->_cut_threshold = cut_threshold;
filter_res.clear();
raw_data.clear();
time_value.clear();
WPT_res.clear();
raw_data = Ae_data;
filter_sum = 0;
//设置频域及时频域分析时所需的数据长度,其必须是2的幂级数
//数据过长会导致计算频域、时频域特征时堆栈溢出
if (_Frequency < Frequency_length) {
for (int i = 15; i > 0; --i) {
if (pow(2, i) < _Frequency) {
Frequency_length = pow(2, i);
break;
}
}
}
}
///功能描述:无参构造函数:调用SVM类时初始化
///param [I] 无
///返回值: 无
///备注:无
RT_TCM_AE::RT_TCM_AE() :PI(3.1416) {
}
//均值滤波+峰值计算
void RT_TCM_AE::cal_Peak() {
int length = raw_data.size();
for (int i = 0; i < length; ++i) {
filter_res.push_back(this->next(raw_data[i]));
}
////峰值提取
vector<float>peak_points = calculate_Peak(filter_res);
// cout << "峰值点数为:" << peak_points.size() << " ";
Fluctuation = calcalate_FD(peak_points, 0.7, 1.3);
}
//均值滤波步骤
float RT_TCM_AE::next(float val) {
que.push(val);
filter_sum += val;
while(que.size() > len) {
filter_sum -= que.front();//超出窗口容量,先把队首元素获取,在sum里减去
que.pop();//再把队首元素弹出
}
return filter_sum / que.size();
}
//获取峰值波动特征
float RT_TCM_AE::get_FD() {
return Fluctuation;
}
vector<float> RT_TCM_AE::calculate_Peak(vector<float> smooth_data) {
int length = smooth_data.size();
vector<float>peak_ans;
float avg = -1;
float sum = 0;
for (int i = 0; i < length; ++i) {
sum += smooth_data[i];
}
avg = float(sum / length);
vector<int> coordinate_x;
for (int i = 1; i < length - 1; ++i) {
if (smooth_data[i] > smooth_data[i - 1] && smooth_data[i] > smooth_data[i + 1] && smooth_data[i] >= avg) {//
if (!coordinate_x.empty()&&(i - coordinate_x.back() < 2000)) {
if (smooth_data[i] > peak_ans.back()) {
peak_ans.pop_back(); coordinate_x.pop_back();
coordinate_x.push_back(i); peak_ans.push_back(smooth_data[i]);
}
else {
continue;
}
}
else {
coordinate_x.push_back(i); peak_ans.push_back(smooth_data[i]);
}
}
}
return peak_ans;
}
float RT_TCM_AE::calcalate_FD(vector<float> data, float low_hold, float high_hold) {
int brow = data.size();
int vebral = 0;
float val = 0;
for (int i = 0; i < brow - 1; ++i) {
val = data[i] / data[i + 1];
if (val<=low_hold || val >= high_hold) {
vebral+=1;
}
}
//cout<<"超出范围为"<< vebral<<" ";
float ans = float(vebral)/float(brow);
return ans;
}
//判断是否加工区域
bool RT_TCM_AE::stage_cutting() {
float process_value = 0;
float decision_threshold = 0.15;//设定加工下限阈值
int datasize = raw_data.size();
for (int j = 0; j < datasize; ++j) {
process_value += raw_data[j];
}
//计算均值
float average = process_value / (float)datasize;
return average > decision_threshold;
}
//获取时域特征
vector<float> RT_TCM_AE::getTime() {
return time_value;
}
//计算时域特征
void RT_TCM_AE::cal_Time() {
float process_value = 0;
float Rms_value = 0;
float max_value = -1;
int datasize = raw_data.size();
for (int j = 0; j < datasize; ++j) {
if (raw_data[j] > max_value)max_value = raw_data[j];
process_value += raw_data[j];
Rms_value += raw_data[j] * raw_data[j];
}
//计算均值
float average = process_value / (float)datasize;
//计算均方根
float rms = Rms_value / (float)datasize;
//计算峰值因子
float perk = max_value / rms;
time_value.push_back(average);
time_value.push_back(rms);
time_value.push_back(perk);
}
////频域分析
inline void RT_TCM_AE::swap(float &a, float &b)
{
float t;
t = a;
a = b;
b = t;
}
void RT_TCM_AE::bitrp(float xreal[], float ximag[], int n)
{
// 位反转置换 Bit-reversal Permutation
int i, j, a, b, p;
for (i = 1, p = 0; i < n; i *= 2)
{
p++;
}
for (i = 0; i < n; i++)
{
a = i;
b = 0;
for (j = 0; j < p; j++)
{
b = (b << 1) + (a & 1); // b = b * 2 + a % 2;
a >>= 1; // a = a / 2;
}
if (b > i)
{
swap(xreal[i], xreal[b]);
swap(ximag[i], ximag[b]);
}
}
}
void RT_TCM_AE::FFT(float xreal[], float ximag[], int n)
{
// 快速傅立叶变换,将复数 x 变换后仍保存在 x 中,xreal, ximag 分别是 x 的实部和虚部
float *wreal = new float[_Frequency / 2];
float *wimag = new float[_Frequency / 2];
//float wreal[frequency / 2], wimag[frequency / 2];
float treal, timag, ureal, uimag, arg;
int m, k, j, t, index1, index2;
bitrp(xreal, ximag, n);
// 计算 1 的前 n / 2 个 n 次方根的共轭复数 W'j = wreal [j] + i * wimag [j] , j = 0, 1, ... , n / 2 - 1
arg = -2 * PI / n;
treal = cos(arg);
timag = sin(arg);
wreal[0] = 1.0;
wimag[0] = 0.0;
for (j = 1; j < n / 2; j++)
{
wreal[j] = wreal[j - 1] * treal - wimag[j - 1] * timag;
wimag[j] = wreal[j - 1] * timag + wimag[j - 1] * treal;
}
for (m = 2; m <= n; m *= 2)
{
for (k = 0; k < n; k += m)
{
for (j = 0; j < m / 2; j++)
{
index1 = k + j;
index2 = index1 + m / 2;
t = n * j / m; // 旋转因子 w 的实部在 wreal [] 中的下标为 t
treal = wreal[t] * xreal[index2] - wimag[t] * ximag[index2];
timag = wreal[t] * ximag[index2] + wimag[t] * xreal[index2];
ureal = xreal[index1];
uimag = ximag[index1];
xreal[index1] = ureal + treal;
ximag[index1] = uimag + timag;
xreal[index2] = ureal - treal;
ximag[index2] = uimag - timag;
}
}
}
//清除一维数组内存
delete[] wreal;
wreal = nullptr;
delete[] wimag;
wimag = nullptr;
}
void RT_TCM_AE::cal_FFT()
{
//动态数据创建实数、复数
float *xreal = new float[_Frequency];
float *ximag = new float[_Frequency];
//
//float xreal[frequency] = {}, ximag[frequency] = {};
//float xreal[frequency] = {}, ximag[frequency] = {0};
int n, i= Frequency_length;
for (int j = 0; j < i; ++j) {
xreal[j]=raw_data[j];
ximag[j]=0;
}
n = i; // 要求 n 为 2 的整数幂
while (i > 1)
{
if (i % 2)
{
printf( "%d is not a power of 2! ", n);
exit(1);
}
i /= 2;
}
FFT(xreal, ximag, n);
//printf( "FFT: i real imag\n");
float sum = 0;
for (i = 0; i < n; i++)
{
sum += xreal[i];
//printf( "%4d %8.4f %8.4f\n", i, xreal[i], ximag[i]);
}
fft_energy = sum / (float)n;
//清除一维数组内存
delete[] xreal;
xreal = nullptr;
delete[] ximag;
ximag = nullptr;
}
float RT_TCM_AE::get_FFT() {
return fft_energy;
}
//计算小波包能量比结果
void RT_TCM_AE::cal_WPT() {
vector<double>signal;
int data_size = Frequency_length;
for (int i = 0;i < data_size; ++i) {
signal.push_back(raw_data[i]);
}
std::vector<double> reconstructed_signal;
reconstructed_signal.resize(signal.size());
// Load well-known wavelet coefficients.
Wavelet wavelet;
Wavelet::GetWaveletCoefficients(&wavelet, Wavelet::WaveletType::Daubechies, 5);
// Construct wavelet packet tree of height = 3.
// This tree has 4 wavelet levels, and 4 leaves.
WaveletPacketTree tree(4, &wavelet);
tree.SetRootSignal(&signal);
tree.Decompose();
// Reconstruct signal one wavelet level at a time.
//cout << "小波包叶子为:" << tree.GetWaveletLevelCount() << endl;
vector<double>energy;
for (size_t i = 0; i < tree.GetWaveletLevelCount(); i++) {
tree.Reconstruct(i);
auto root_signal = tree.GetRootSignal();
// Add the coefficients of wavelet level i to the reconstructed signal.
std::transform(reconstructed_signal.cbegin(), reconstructed_signal.cend(), root_signal->cbegin(), reconstructed_signal.begin(), std::plus<double>());
//root_signal:最后一层小波包重构信号
double sum = 0;
for_each(root_signal->cbegin(), root_signal->cend(), [&](double x) {
sum += x;
});
sum = sqrtf(sum);
energy.push_back(sum);
}
//for_each(energy.begin(), energy.end(), [](float x) {
// cout << x << " ";
//});
double all_sum = 0;
for_each(energy.begin(), energy.end(), [&](double x) {
all_sum += x;
});
WPT_res.push_back(energy[2]/all_sum*100);
WPT_res.push_back(energy[4] / all
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于C++和QT开发的刀具破损状态检测软件源码(SVM进行判别).zip基于C++和QT开发的刀具破损状态检测软件源码(SVM进行判别).zip基于C++和QT开发的刀具破损状态检测软件源码(SVM进行判别).zip基于C++和QT开发的刀具破损状态检测软件源码(SVM进行判别).zip基于C++和QT开发的刀具破损状态检测软件源码(SVM进行判别).zip基于C++和QT开发的刀具破损状态检测软件源码(SVM进行判别).zip基于C++和QT开发的刀具破损状态检测软件源码(SVM进行判别).zip基于C++和QT开发的刀具破损状态检测软件源码(SVM进行判别).zip基于C++和QT开发的刀具破损状态检测软件源码(SVM进行判别).zip 【备注】 1、该资源内项目代码百分百可运行,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
资源推荐
资源详情
资源评论
收起资源包目录
基于C++和QT开发的刀具破损状态检测软件源码(SVM进行判别).zip (55个子文件)
项目说明.md 1KB
AcData
PCIE9758C_32.lib 16KB
qwchartview.cpp 2KB
AcData.cpp 6KB
mythread1.h 1KB
vary.h 2KB
history_data.h 2KB
MovingAverage.h 4KB
detect_status.h 894B
AcData.qrc 62B
Tree.h 4KB
history_data.cpp 6KB
mythread2.cpp 3KB
resource.h 430B
StationaryWaveletPacketTree.h 3KB
train_wave.h 966B
WaveletPacketTree.h 2KB
AcData.ui 8KB
mythread2.h 855B
AcData.vcxproj 18KB
brokentool_collect.cpp 3KB
brokentool_collect.h 796B
newtool_collect.h 831B
main.cpp 163B
GeneratedFiles
ui_AcData.h 9KB
ui_history_data.h 9KB
qrc_AcData.cpp 1KB
ui_Data_analysis.h 4KB
MovingAverage.cpp 8KB
newtool_collect.cpp 3KB
mythread3.h 995B
WaveletMath.h 9KB
RT_TCM_AE.cpp 13KB
detect_status.cpp 3KB
train_wave.cpp 2KB
mythread1.cpp 2KB
StationaryWaveletPacketTree.cc 7KB
AcData.h 2KB
StdAfx.cpp 200B
RT_TCM_AE.h 5KB
vary.cpp 438B
WaveletPacketTreeBase.h 3KB
mythread3.cpp 2KB
AcData.vcxproj.filters 5KB
PCIE9758C.h 15KB
qwchartview.h 785B
SVMModel.xml 27KB
history_data.ui 7KB
Wavelet.cc 17KB
Wavelet.h 3KB
StdAfx.h 261B
PCIE9758C_64.lib 15KB
WaveletMath.cc 6KB
WaveletPacketTree.cc 3KB
AcDataVerison5_1.sln 1KB
共 55 条
- 1
资源评论
onnx
- 粉丝: 9361
- 资源: 5586
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功