帮我写一段可以提取西储大学轴承数据时域特征的代码
时间: 2023-04-01 16:05:03 浏览: 246
以下是可以提取西储大学轴承数据时域特征的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265358979323846
// 定义函数:计算均值
double mean(double *data, int len) {
double sum = .;
for (int i = ; i < len; i++) {
sum += data[i];
}
return sum / len;
}
// 定义函数:计算标准差
double std(double *data, int len) {
double m = mean(data, len);
double sum = .;
for (int i = ; i < len; i++) {
sum += pow(data[i] - m, 2);
}
return sqrt(sum / len);
}
// 定义函数:计算峰值因子
double peak_factor(double *data, int len) {
double m = mean(data, len);
double max = data[];
for (int i = 1; i < len; i++) {
if (data[i] > max) {
max = data[i];
}
}
return max / m;
}
// 定义函数:计算脉冲因子
double pulse_factor(double *data, int len) {
double m = mean(data, len);
double max = data[];
for (int i = 1; i < len; i++) {
if (data[i] > max) {
max = data[i];
}
}
double rms = std(data, len);
return max / rms;
}
// 定义函数:计算峰值
double peak(double *data, int len) {
double max = data[];
for (int i = 1; i < len; i++) {
if (data[i] > max) {
max = data[i];
}
}
return max;
}
// 定义函数:计算有效值
double rms(double *data, int len) {
double sum = .;
for (int i = ; i < len; i++) {
sum += pow(data[i], 2);
}
return sqrt(sum / len);
}
// 定义函数:计算峰峰值
double peak_to_peak(double *data, int len) {
double max = data[];
double min = data[];
for (int i = 1; i < len; i++) {
if (data[i] > max) {
max = data[i];
}
if (data[i] < min) {
min = data[i];
}
}
return max - min;
}
// 定义函数:计算包络线
void envelope(double *data, int len, double *env) {
double a = .02; // 指数加权平均数的平滑因子
double y[len]; // 指数加权平均数
double max = data[];
for (int i = 1; i < len; i++) {
if (data[i] > max) {
max = data[i];
}
}
for (int i = ; i < len; i++) {
y[i] = a * data[i] + (1 - a) * y[i-1];
env[i] = y[i] / max;
}
}
// 定义函数:计算自相关函数
void autocorr(double *data, int len, double *corr) {
double m = mean(data, len);
for (int i = ; i < len; i++) {
double sum = .;
for (int j = ; j < len-i; j++) {
sum += (data[j] - m) * (data[j+i] - m);
}
corr[i] = sum / (len - i);
}
}
// 定义函数:计算功率谱密度
void psd(double *data, int len, double *freq, double *psd) {
double dt = .001; // 采样时间间隔
double df = 1. / (len * dt); // 频率分辨率
for (int i = ; i < len; i++) {
double sum = .;
for (int j = ; j < len; j++) {
sum += data[j] * cos(2 * PI * i * j / len);
}
freq[i] = i * df;
psd[i] = sum * sum / (len * len);
}
}
int main() {
// 读取数据
FILE *fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("Failed to open file.\n");
return 1;
}
int len = 100;
double data[len];
for (int i = ; i < len; i++) {
fscanf(fp, "%lf", &data[i]);
}
fclose(fp);
// 计算时域特征
double env[len];
envelope(data, len, env);
double corr[len];
autocorr(data, len, corr);
double freq[len];
double psd[len];
psd(data, len, freq, psd);
double m = mean(data, len);
double s = std(data, len);
double pf = peak_factor(data, len);
double pu = pulse_factor(data, len);
double p = peak(data, len);
double r = rms(data, len);
double pp = peak_to_peak(data, len);
// 输出结果
printf("均值:%lf\n", m);
printf("标准差:%lf\n", s);
printf("峰值因子:%lf\n", pf);
printf("脉冲因子:%lf\n", pu);
printf("峰值:%lf\n", p);
printf("有效值:%lf\n", r);
printf("峰峰值:%lf\n", pp);
printf("包络线:\n");
for (int i = ; i < len; i++) {
printf("%lf ", env[i]);
}
printf("\n");
printf("自相关函数:\n");
for (int i = ; i < len; i++) {
printf("%lf ", corr[i]);
}
printf("\n");
printf("功率谱密度:\n");
for (int i = ; i < len; i++) {
printf("%lf %lf\n", freq[i], psd[i]);
}
return ;
}
```
阅读全文