Advertisement
Bibodui

БПФ Илья

Dec 24th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <complex>
  4. #include <algorithm>
  5. #include <corecrt_math_defines.h>
  6. #include <string>
  7. #define _USE_MATH_DEFINES
  8. using namespace std;
  9.  
  10. void FFT(vector < complex < double>>& a)
  11. {
  12.     int n = a.size();
  13.  
  14.     if (n == 1) return;
  15.  
  16.     vector <complex<double>> a0(n / 2), a1(n / 2);
  17.     for (int i = 0, j = 0; i < n; i += 2, j++)
  18.     {
  19.         a0[j] = a[i];
  20.         a1[j] = a[i + 1];
  21.     }
  22.  
  23.     FFT(a0);
  24.     FFT(a1);
  25.  
  26.     double angle = 2 * M_PI / n;
  27.     complex<double> tmp(1), temp(cos(angle), sin(angle));
  28.     for (int i = 0; i < n / 2; i++)
  29.     {
  30.         a[i] = a0[i] + tmp * a1[i];
  31.         a[i + n / 2] = a0[i] - tmp * a1[i];
  32.         tmp *= temp;
  33.     }
  34. }
  35. template <typename T>
  36. void write_vector(const vector<T>& V)
  37. {
  38.     cout << "The numbers in the vector are: " << endl;
  39.     for (int i = 0; i < V.size(); i++)
  40.         cout << V[i] << " ";
  41. }
  42. int main()
  43. {
  44.     int n;
  45.     setlocale(LC_ALL, "Russian");
  46.     cout << "Введите 2^k элементов" << endl;
  47.     cout << "Введите значения в векторе.Чтобы закончить ввод, введите q";
  48.     vector<complex<double>> v;
  49.     int input;
  50.     vector<int> V;
  51.     while (cin >> input)
  52.     {
  53.         v.push_back(input);
  54.     }
  55.     cout << endl;
  56.     cin.clear();
  57.     FFT(v);
  58.     cout << "Вектор после выполнения Преобразования Фурье:" << endl;
  59.  
  60.     for (int i = 0; i < v.size(); i++)
  61.     {
  62.         char znak = 0;
  63.         if (v[i].imag() > 0) znak = '+';
  64.         else znak = ' ';
  65.  
  66.         cout << v[i].real();
  67.         if (v[i].imag() > 0) cout << '+' << v[i].imag() <<'i'<< endl;
  68.         if (v[i].imag() < 0) cout << v[i].imag() <<'i'<< endl;
  69.         else cout << endl;
  70.     }
  71.     return 0;
  72. }
  73.  
  74.  
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement