球球来个人一次性把NVH中怎么用讲明白啊
#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265358979323846;
void fft(vector<complex<double>>& a, bool invert) { // 定义 FFT 函数
int n = a.size();
if (n == 1) return;
vector<complex<double>> a0(n / 2), a1(n / 2);
for (int i = 0, j = 0; i < n; i += 2, j++) {
a0[j] = a[i];
a1[j] = a[i + 1];
}
fft(a0, invert);
fft(a1, invert);
double angle = 2 * PI / n * (invert ? -1 : 1);
complex<double> w(1), wn(cos(angle), sin(angle));
for (int i = 0; i < n / 2; i++) {
a[i] = a0[i] + w * a1[i];
a[i + n / 2] = a0[i] - w * a1[i];
if (invert) {
a[i] /= 2;
a[i + n / 2] /= 2;
}
w *= wn;
}
}
vector<complex<double>> fft(const vector<complex<double>>& a) { // 定义快速傅里叶变换函数
vector<complex<double>> A = a;
fft(A, false);
return A;
}
vector<complex<double>> ifft(const vector<complex<double>>& A) { // 定义逆快速傅里叶变换函数
vector<complex<double>> a = A;
fft(a, true);
return a;
}
vector<double> generateData(int n) { // 生成原始数据
vector<double> data(n);
for (int i = 0; i < n; i++) {
data[i] = sin(2 * PI * i / n) + 0.5 * sin(4 * PI * i / n);
}
return data;
}
vector<double> getMagnitudeSpectrum(const vector<complex<double>>& fftResult) { // 获取频谱幅度
vector<double> magSpectrum(fftResult.size());
for (int i = 0; i < fftResult.size(); i++) {
magSpectrum[i] = abs(fftResult[i]);
}
return magSpectrum;
}
int main() { // 主函数
int n = 1024; // 数据点数
vector<double> data = generateData(n); // 生成原始数据
vector<complex<double>> a(n);
for (int i = 0; i < n; i++) {
a[i] = complex<double>(data[i], 0);
}
vector<complex<double>> A = fft(a); // 进行快速傅里叶变换
vector<double> magSpectrum = getMagnitudeSpectrum(A); // 获取频谱幅度
for (int i = 0; i < magSpectrum.size(); i++) {
cout << magSpectrum[i] << " "; // 输出频谱幅度
}
cout << endl;
return 0;
}
测试生成: