【发布时间】:2018-02-19 10:33:02
【问题描述】:
我想用至少 3 个十进制值精确计算时间序列的频率。 这是一个计算整数值频率的简单示例。
#include <fftw3.h>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <fstream>
#define REAL 0
#define IMAG 1
#define NUM_POINTS 1024
void acquire_signal(double *signal, double *theta) {
/* Generate two sine waves of different frequencies and
* amplitudes.
*/
int i;
for (i = 0; i < NUM_POINTS; ++i) {
theta[i] = (double)i / (double)NUM_POINTS;
signal[i] = 1.0*sin(50.0 * 2.0 * M_PI * theta[i]) +
0.5*sin(80.0 * 2.0 * M_PI * theta[i]);
}
}
int main() {
unsigned flags{0};
double *theta = new double[NUM_POINTS];
double *signal = new double[NUM_POINTS];
fftw_complex result[NUM_POINTS/2+1];
fftw_plan plan = fftw_plan_dft_r2c_1d(NUM_POINTS,
signal,
result,
flags);
acquire_signal(signal,theta);
fftw_execute(plan);
//save signal and result
std::ofstream f1,f2;
f1.open ("signal.txt");
for (int i=0; i<NUM_POINTS; i++){
f1 <<theta[i]<<" "<<signal[i]<<"\n";
}
f1.close();
f2.open("result.txt");
for (int i=0; i<NUM_POINTS/2; i++){
double yf = 2.0/(double)(NUM_POINTS)* sqrt(result[i][REAL]*result[i][REAL]+ result[i][IMAG]* result[i][IMAG]);
f2<< (double)i << " "<<yf <<"\n";
}
f2.close();
fftw_destroy_plan(plan);
delete[] signal,theta;
return 0;
}
signal = 1.0*sin(50.350 * 2.0 * M_PI * theta[i]) +
0.5*sin(80.455 * 2.0 * M_PI * theta[i]);
改变时间和频率的单位是否合适?
例如1000*s 中的时间和kHz 中的频率?
【问题讨论】:
-
这个问题可能是关于数学而不是 c++ 或编码的吗?
-
是的,我应该把问题移到右边?
-
您的频率取决于您的采样频率和样本数量。您不能只调整精度。您需要更多点或不同的采样频率
-
只需更改数字即可将您的线路从 50 和 80 赫兹转移到 50.350 和 80.455 赫兹,假设您有 1024 行乘以 1024 赫兹。但是你仍然有 1Hz 的分辨率。
-
无论时间序列越长,我最终都会用
f2<< (double)i << " "<<yf;打印频率和幅度,这是一个整数,表示频率。i在 [0,NUM_POINTS/2] 中。我错了吗?