使用樸素方法計算離散傅立葉變換的 C++ 程式
在離散傅立葉變換 (DFT) 中,將函式的等間距取樣有限列表轉換成有限復正弦和係數列表。它們按頻率排序,具有相同的取樣值,將取樣函式從其原始域(通常是時間或沿線的座標)轉換為頻域。
演算法
Begin Take a variable M and initialize it to some integer Declare an array function[M] For i = 0 to M-1 do function[i] = (((a * (double) i) + (b * (double) i)) - c) Done Declare function sine[M] Declare function cosine[M] for i =0 to M-1 do cosine[i] = cos((2 * i * k * PI) / M) sine[i] = sin((2 * i * k * PI) / M) Done Declare DFT_Coeff dft_value[k] for j = 0 to k-1 do for i = 0 to M-1 do dft_value.real += function[i] * cosine[i] dft_value.img += function[i] * sine[i] Done Done Print the value End
示例程式碼
#include<iostream>
#include<math.h>
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 coefficient 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);
//System.out.print( " "+function[i] + " ");
}
cout << "Enter the max K value: ";
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[k];
cout << "The coefficients are: ";
for (int j = 0; j < k; j++) {
for (int i = 0; i < M; i++) {
dft_value[j].real += function[i] * cosine[i];
dft_value[j].img += function[i] * sine[i];
}
cout << "(" << dft_value[j].real << ") - " << "(" << dft_value[j].img <<" i)\n";
}
}輸出
Enter the coefficient of simple linear function: ax + by = c 4 5 6 Enter the max K value: 10 The coefficients are: (345) - (-1.64772e-05 i) (345) - (-1.64772e-05 i) (345) - (-1.64772e-05 i) (345) - (-1.64772e-05 i) (345) - (-1.64772e-05 i) (345) - (-1.64772e-05 i) (345) - (-1.64772e-05 i) (345) - (-1.64772e-05 i) (345) - (-1.64772e-05 i) (345) - (-1.64772e-05 i)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP