osipyonok

DFT

Feb 19th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include "stdafx.h"//vs2010
  2.  
  3. #include <iostream>
  4. #include <complex>
  5. #include <cassert>
  6. #include <vector>
  7. #include <conio.h>
  8. #include <windows.h>
  9. #include "decimal.h"
  10. #include "gnuplot_i.hpp"
  11.  
  12. using namespace std;
  13. using namespace dec;
  14.  
  15. #define M_PI 3.14159265358979323846
  16. #define dec10 decimal<10>
  17. #define dabs(a) fabs((a).getAsDouble())
  18. #define FOUT
  19. #define cyrillic setlocale(LC_CTYPE, "rus")
  20.  
  21. inline string dtos(double d){ostringstream strs; strs << d; return strs.str();}
  22.  
  23. void wait(){
  24.     FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
  25.     #ifndef FOUT
  26.         cout << endl << "Press any key to continue..." << endl;
  27.     #endif
  28.     _getch();
  29. }
  30.  
  31. int main(){
  32.     cyrillic;
  33.     freopen("input.txt","r",stdin);
  34.     freopen("output.txt","w",stdout);
  35.  
  36.     vector<dec10> x;
  37.     dec10 tmp;
  38.  
  39.     while(cin >> tmp){
  40.         x.push_back(tmp);
  41.     }
  42.  
  43.     int n = x.size();
  44.  
  45.     vector<dec10> dft(n + 1);
  46.  
  47.     for(int i = 0 ; i < n ; ++i){
  48.         dec10 real = dec10(0);
  49.         for(int j = 0 ; j < n ; ++j){
  50.             dec10 angle = dec10((2 * M_PI * i * j) / n);
  51.             real += x[j] * dec10(cos(angle.getAsDouble()));
  52.         }
  53.         dft[i] = real;
  54.     }
  55.  
  56.     vector<pair<dec10 , dec10>> mx; //{coordinate , value}
  57.  
  58.     for(int i = 0 ; i < n / 2 ; ++i){
  59.         if(!i){
  60.             if(dft[i] > dft[i + 1]){
  61.                 mx.push_back(make_pair(i * 0.01 , dft[i]));
  62.             }
  63.         }else{
  64.             if(dft[i] > dft[i + 1] && dft[i] > dft[i - 1]){
  65.                 mx.push_back(make_pair(i * 0.01 , dft[i]));
  66.             }
  67.         }
  68.     }
  69.  
  70.     for(int i = 0 ; i < mx.size() ; ++i){
  71.         cout << mx[i].second << " ";   
  72.     }
  73.  
  74.     reverse(dft.begin() , dft.end());
  75.     dft.pop_back();
  76.     reverse(dft.begin() , dft.end());
  77.  
  78.     string to_plot = "[0:5] ";
  79.     int cur = 0;
  80.  
  81.     for(double i = 0. ; fabs(i - 5.00) > 1e-3 ; i += 0.01){
  82.         if(fabs(i - 4.99) <= 1e-3){
  83.             to_plot += dtos(dabs(dft[cur]));
  84.             break;
  85.         }
  86.         to_plot += "x>=";
  87.         to_plot += dtos(i);
  88.         to_plot += "&x<";
  89.         to_plot += dtos(i + 0.01);
  90.         to_plot += "?";
  91.         to_plot += dtos(dabs(dft[cur++]));
  92.         to_plot += ":";
  93.     }
  94.  
  95.     Gnuplot gnu("lines");
  96.  
  97.     gnu.plot_equation(to_plot,"dft");
  98.  
  99.     wait();
  100.  
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment