Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <cstdio>
  5. #include <cmath>
  6. #define _USE_MATH_DEFINES
  7. #include "math.h"
  8. #include <float.h>
  9. #include <vector>
  10. #include <complex>
  11. using namespace std;
  12.  
  13. double t1 = -M_PI;
  14. double t2 = M_PI;
  15. double T = t2-t1;// 2 * M_PI;
  16.  
  17. void fft(vector<complex<double>> & a) {
  18. int n = (int)a.size();
  19. if (n == 1) return;
  20.  
  21. vector<complex<double>> a0(n / 2), a1(n / 2);
  22. for (int i = 0, j = 0; i<n; i += 2, ++j) {
  23. a0[j] = a[i];
  24. a1[j] = a[i + 1];
  25. }
  26. fft(a0);
  27. fft(a1);
  28.  
  29. double ang = 2 * M_PI / n * 1;//-1
  30. complex<double> w(1), wn(cos(ang), sin(ang));
  31. for (int i = 0; i<n / 2; ++i) {
  32. a[i] = a0[i] + w * a1[i];
  33. a[i + n / 2] = a0[i] - w * a1[i];
  34.  
  35. w *= wn;
  36. }
  37. }
  38.  
  39. vector<double> Tgrid(int n) {
  40. vector<double> t(n);
  41. for (int i = 0; i < n; ++i)
  42. t[i] = t1+1.0*i*T / n;
  43. return t;
  44. }
  45.  
  46. vector<double> Wgrid(int n) {
  47. vector<double> w(n);
  48. for (int i = 0; i < n; ++i)
  49. w[i] = -1. * M_PI*n / T + 2. * M_PI*i / T;
  50. return w;
  51. }
  52.  
  53. double func(double t, double w){
  54. return cos(w*t)*sin(w*t)* sin(w*t);
  55. //return sin(21.*t) + sin(23.*t);
  56. }
  57.  
  58. vector<double> Fgrid(vector<double> t, double w, int n) {
  59. vector<double> f(n);
  60. for (int i = 0; i < n; ++i)
  61. f[i] = func(t[i], w);
  62. return f;
  63. }
  64.  
  65. double hann_window(int k, int n) {
  66. return 1. / 2.*(1 - cos(2 * M_PI*k / (n - 1)));
  67. }
  68.  
  69. double rect_window(int k, int n) {
  70. return 1.;
  71. }
  72.  
  73. void write_to_file(string file, vector<double> w, vector<double> res, int n) {
  74. ofstream plot;
  75. plot.open(file);
  76. plot << "w" << " res" << endl;
  77. for (int i = 0; i < n; ++i){
  78. plot << w[i] << " " << res[i] << endl;
  79. }
  80. plot.close();
  81. }
  82.  
  83. vector<double> ft(vector<double> t, vector<double> w, vector<double> f, int n, double win_f(int, int)) {
  84. vector<double> res(n);
  85.  
  86. for (int j = 0; j < n; ++j) {
  87. double re = 0.;
  88. double im = 0.;
  89. for (int k = 0; k < n; ++k) {
  90. re += f[k] * win_f(k, n) * cos(w[j]*t[k]);
  91. im += f[k] * win_f(k, n) * sin(w[j]*t[k]);
  92. }
  93. res[j] = re*re + im*im;
  94. }
  95. return res;
  96. }
  97.  
  98. int main(){
  99. int N = 4000;
  100. double W = 100.;
  101.  
  102. vector<double> f(N);
  103. vector<double> t(N);
  104. vector<double> w(N);
  105. vector<double> res(N);
  106.  
  107. t = Tgrid(N);
  108. w = Wgrid(N);
  109. f = Fgrid(t, W, N);
  110. ofstream timefile;
  111. timefile.open("timve.txt");
  112.  
  113. int seconds = time(NULL);
  114.  
  115. res = ft(t, w, f, N, &rect_window);
  116. seconds = time(NULL) - seconds;
  117. timefile << seconds << endl;
  118. write_to_file("plot_rect.txt", w, res, N);
  119.  
  120. //res = ft(t, w, f, N, &hann_window);
  121. //write_to_file("plot_hann.txt", w, res, N);
  122.  
  123. //N = 1024;
  124. //t = Tgrid(N);
  125. //w = Wgrid(N);
  126. //f = Fgrid(t, W, N);
  127. vector<complex<double>> fc(N);
  128. for (int i = 0; i < N; ++i)
  129. fc[i] = complex<double> (f[i], 0);
  130. vector<double> fcm(N);
  131. seconds = time(NULL);
  132. fft(fc);
  133. seconds = time(NULL) - seconds;
  134. timefile << seconds << endl;
  135. timefile.close();
  136. for (int i = 0; i < N; ++i)
  137. fcm[i] = fc[i].real()*fc[i].real() + fc[i].imag()*fc[i].imag();
  138. write_to_file("plot_ftt.txt", w, fcm, N);
  139.  
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement