Advertisement
Guest User

Untitled

a guest
Sep 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <ctime>
  5. #include <iostream>
  6. #include <iomanip>
  7. using namespace std;
  8. const double PI = 3.14159265359;
  9.  
  10.  
  11. void DirectFourierTransformation(double* const massiveF, double** const matrixA, const int SIZE)
  12. {
  13.     for(int k = 0; k < SIZE; ++k)
  14.     {
  15.         for(int j = 0; j < SIZE; ++j)
  16.         {
  17.             matrixA[0][k] = matrixA[0][k] + cos(-2.0 * PI * k * j / SIZE) * massiveF[j];
  18.             matrixA[1][k] = matrixA[1][k] + sin(-2.0 * PI * k * j / SIZE) * massiveF[j];
  19.         }
  20.         matrixA[0][k] = matrixA[0][k] / SIZE;
  21.         matrixA[1][k] = matrixA[1][k] / SIZE;
  22.     }
  23.  
  24. }
  25.  
  26. void InverseFourierTransform(double ** const matrixA, double ** const matrixF, const int SIZE)
  27. {
  28.     for(int k = 0; k < SIZE; ++k)
  29.     {
  30.         for(int j = 0; j < SIZE; ++j)
  31.         {
  32.             matrixF[0][k] = matrixF[0][k] + cos(2.0 * PI * k * j / SIZE) * matrixA[0][j] - sin(2.0 * PI * k * j / SIZE) * matrixA[1][j];
  33.             matrixF[1][k] = matrixF[1][k] +  cos(2.0 * PI * k * j / SIZE) * matrixA[1][j] + sin(2.0 * PI * k * j / SIZE) * matrixA[0][j];
  34.         }
  35.     }
  36. }
  37.  
  38. void Semi_FastFourierTransformation(double* const massivef, double** const matrixA, const int SIZE)
  39. {
  40.     int p1, p2;
  41.     p1 = (int) sqrt(SIZE);
  42.     while (SIZE % p1)
  43.         --p1;
  44.     p2 = SIZE / p1;
  45.    
  46.     for(int k = 0; k < SIZE; ++k)
  47.     {
  48.         int k1 = k % p1, k2 = k / p1;
  49.         for(int j2 = 0; j2 < p2; ++j2)
  50.         {
  51.             double A[2]{};
  52.             for(int j1 = 0; j1 < p1; ++j1)
  53.             {
  54.                 A[0] += cos(-2.0 * PI * j1 * k1 / p1) * massivef[j2 + p2 * j1];
  55.                 A[1] += sin(-2.0 * PI * j1 * k1 / p1) * massivef[j2 + p2 * j1];
  56.             }
  57.             matrixA[0][k] += (A[0] * cos(-2.0 * PI * j2 / (p1 * p2) * (k1 + p1 * k2)) - A[1] * sin(-2.0 * PI * j2 / (p1 * p2) * (k1 + p1 * k2))) / p1;
  58.             matrixA[1][k] += (A[0] * sin(-2.0 * PI * j2 / (p1 * p2) * (k1 + p1 * k2)) + A[1] * cos(-2.0 * PI * j2 / (p1 * p2) * (k1 + p1 * k2))) / p1;
  59.         }
  60.         matrixA[0][k] /= p2;
  61.         matrixA[1][k] /= p2;
  62.     }
  63. }
  64.  
  65. void FastFourierTransform(double* massivef, double** const matrixA, const int SIZE)
  66. {
  67.     int r = (int)log2(SIZE);
  68.     int* k = new int[r];
  69.     for(int km = 0; km < SIZE; ++km)
  70.     {
  71.        
  72.     }
  73. }
  74.  
  75.  
  76. int main()
  77. {
  78.     srand(time(NULL));
  79.     int SIZE;
  80.     cout << "INPUT SIZE: ";
  81.     cin >> SIZE;
  82.     double* massivef = new double[SIZE];
  83.     double** matrixA = new double*[2];
  84.     double** matrixF = new double*[2];
  85.     for(int i = 0; i < SIZE; i++)
  86.     {
  87.         massivef[i] = rand()%100;
  88.         if(i < 2)
  89.         {
  90.             matrixA[i] = new double[SIZE];
  91.             matrixF[i] = new double[SIZE];
  92.         }
  93.     }
  94.     for(int i = 0; i < 2; i++)
  95.     {
  96.         for(int j = 0; j < SIZE; j++)
  97.         {
  98.             matrixF[i][j] = 0;
  99.             matrixA[i][j] = 0;
  100.         }
  101.     }
  102.    
  103.     cout << endl << " Massive: ";
  104.     for (int i = 0; i < SIZE; ++i)
  105.         cout << massivef[i] << setw(5);
  106.     cout << endl << endl;
  107.    
  108.     DirectFourierTransformation(massivef, matrixA, SIZE);
  109.    
  110.     cout << "DirectFourierTransformation: " << endl;
  111.     for (int i = 0; i < SIZE; ++i)
  112.         cout << "( " << matrixA[0][i] << setw(5) << " , i" << matrixA[1][i] << " )" << endl;
  113.  
  114.     InverseFourierTransform(matrixA, matrixF, SIZE);
  115.    
  116.     cout << endl << "InverseFourierTransform: " << endl;
  117.     for (int i = 0; i < SIZE; ++i)
  118.         cout << "( " << matrixF[0][i]  << setw(5) << " , i" << matrixF[1][i] << " )" << endl;
  119.     cout << endl;
  120.    
  121.     for(int i = 0; i < 2; i++)
  122.         for(int j = 0; j < SIZE; j++)
  123.             matrixA[i][j] = 0;
  124.    
  125.     Semi_FastFourierTransformation(massivef, matrixA, SIZE);
  126.    
  127.     cout << endl << "Semi_FastFourierTransformation: " << endl;
  128.     for (int i = 0; i < SIZE; ++i)
  129.         cout << "( " << matrixA[0][i] << setw(5) << " , i" << matrixA[1][i] << " )" << endl;
  130.     cout << endl;
  131.    
  132.     for(int i = 0; i < 2; i++)
  133.         for(int j = 0; j < SIZE; j++)
  134.             matrixA[i][j] = 0;
  135.    
  136.     for(int i = 0; i < 2; i++)
  137.     {
  138.         delete[] matrixA[i];
  139.         delete[] matrixF[i];   
  140.     }
  141.     delete [] matrixA;
  142.     delete [] matrixF;
  143.     delete [] massivef;
  144.     system("pause");
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement