Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <complex>
  4. #include <cmath>
  5.  
  6. #define M_PI 3.14159265358979323846
  7.  
  8. typedef std::complex<double> complex_d;
  9. typedef std::vector<complex_d> complex_array_d;
  10.  
  11. complex_array_d fft(complex_array_d& sample) {
  12. if (sample.size() <= 1) {
  13. return sample;
  14. }
  15.  
  16. complex_array_d even;
  17. complex_array_d odd;
  18.  
  19. bool is_even = true;
  20. for (auto s : sample) {
  21. if (is_even) {
  22. even.push_back(s);
  23. }
  24. else {
  25. odd.push_back(s);
  26. }
  27. is_even = !is_even;
  28. }
  29.  
  30. complex_array_d fft_even = fft(even);
  31. complex_array_d fft_odd = fft(odd);
  32. complex_array_d result;
  33. result.resize(sample.size());
  34.  
  35. for (size_t i = 0; i < sample.size() / 2; i++) {
  36. complex_d w = std::polar(1.0, -2 * M_PI * i / sample.size());
  37. result.at(i) = fft_even.at(i) + w * fft_odd.at(i);
  38. result.at(i + sample.size() / 2) = fft_even.at(i) - w * fft_odd.at(i);
  39. }
  40. return result;
  41. }
  42.  
  43. int main(int argc, char** argv) {
  44. const int MAX_ORDER = 13;
  45. const bool PRINT_COEFS = true;
  46.  
  47. for (int o = 1; o < MAX_ORDER; o++) {
  48. const int N = 1 << o;
  49. std::cout << "N: " << N << std::endl;
  50. complex_array_d f;
  51. for (int n = 0; n < N; n++) {
  52. f.push_back(sin(2 * M_PI * n / (double)N));
  53. }
  54. complex_array_d fft_result = fft(f);
  55.  
  56. if (PRINT_COEFS) {
  57. for (auto c : fft_result) {
  58. std::cout << "real: " << c.real() << " im: " << c.imag() << std::endl;
  59. }
  60. }
  61. std::cout << std::endl;
  62. }
  63.  
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement