直接计算 DFT 系数的 C++ 程序
在离散傅立叶变换(DFT)中,将函数的等距样本的有限列表转换为复正弦曲线有限组合的系数列表。它们按具有相同样本值的频率排序,将采样函数从其原始域(通常是时间或沿线的位置)转换为频域。
算法
Begin
Declare three variables which are the coefficient of linear equation and max value
Read the variables
Define a class with two variables real, img
Create a constructor and set real, img to zero
Take a variable M and initialize it to some integer
Create function[M]
For i=0 to M do
function[i] = (((a * (double) i) + (b * (double) i)) - c)
Declare function sine[M]
Declare function cosine[M]
for i = 0 to M do
cosine[i] = cos((2 * i * k * PI) / M)
sine[i] = sin((2 * i * k * PI) / M)
for i = 0 to M do
dft_value.real += function[i] * cosine[i]
dft_value.img += function[i] * sine[i]
Print the value
End示例代码
#include输出结果#include using namespace std; #define PI 3.14159265 class DFT_Coeff { public: double real, img; DFT_Coeff() { real = 0.0; img = 0.0; } }; int main(int argc, char **argv) { int M = 10; cout << "Enter the coeff of simple linear function:\n"; cout << "ax + by = c\n"; double a, b, c; cin >> a >> b >> c; double function[M]; for (int i = 0; i < M; i++) { function[i] = (((a * (double) i) + (b * (double) i)) - c); } cout << "输入最大K值: "; int k; cin >> k; double cosine[M]; double sine[M]; for (int i = 0; i < M; i++) { cosine[i] = cos((2 * i * k * PI) / M); sine[i] = sin((2 * i * k * PI) / M); } DFT_Coeff dft_value; cout << "系数是: "; for (int i = 0; i < M; i++) { dft_value.real += function[i] * cosine[i]; dft_value.img += function[i] * sine[i]; } cout << "(" << dft_value.real << ") - " << "(" << dft_value.img << " i)"; }
Enter the coeff of simple linear function: ax + by = c 4 6 7 输入最大K值: 4 系数是: (-50) - (-16.246 i)