Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <string>
  7. #include <complex>
  8. #include <vector>
  9.  
  10.  
  11. using namespace std;
  12.  
  13. double signal(double t, double T)
  14. {
  15.     const double A = 1.0;
  16.     const double f = 20.0;
  17.     const double phi = M_PI / 2;
  18.  
  19.     return A * sin(2.0 * M_PI * f * t + phi);
  20. }
  21.  
  22. double* generateSignal(int T)
  23. {
  24.     auto tabSignal = new double[T];
  25.  
  26.     for (int i = 0; i < T; i++) {
  27.         tabSignal[i] = i / (double) T;
  28.     }
  29.  
  30.     return tabSignal;
  31. }
  32.  
  33. vector<double> dff(double* signal, int N)
  34. {
  35.     vector<double> results;
  36.  
  37.     for (int i = 0; i < N / 2; i++) {
  38.         complex<double> sum = 0;
  39.  
  40.         complex<double> e(cos(-2 * M_PI * i / N), sin(-2 * M_PI * i / N));
  41.         for (int j = 0; j < N; j++) {
  42.             complex<double> x1(cos(-2 * M_PI * i * j / N), sin(-2 * M_PI * i * j / N));
  43.  
  44.             x1 = signal[j] * x1;
  45.  
  46.             sum += x1;
  47.         }
  48.  
  49.         results.push_back(abs(sum));
  50.     }
  51.  
  52.     return results;
  53. }
  54.  
  55. vector<double> fft(double* signal, int N)
  56. {
  57.     vector<double> results;
  58.  
  59.     for (int i = 0; i < N / 2; i++) {
  60.         complex<double> sum1 = 0;
  61.         complex<double> sum2 = 0;
  62.  
  63.         complex<double> e(cos(-2 * M_PI * i / N), sin(-2 * M_PI * i / N));
  64.         for (int j = 0; j < N / 2; j++) {
  65.             double k = i;
  66.             double n = 2 * j;
  67.  
  68.             complex<double> x(cos(-2 * M_PI * k * n / N), sin(-2 * M_PI * k * n / N));
  69.  
  70.             x = signal[(int) n] * x;
  71.  
  72.             sum1 = sum1 + x;
  73.         }
  74.  
  75.         for (int j = 0; j < N / 2; j++) {
  76.             double k = i;
  77.             double n = 2 * j + 1;
  78.  
  79.             complex<double> x(cos(-2 * M_PI * k * n  / N), sin(-2 * M_PI * k * n / N));
  80.             x = signal[(int) n] * x;
  81.  
  82.             sum2 = sum2 + x;
  83.         }
  84.  
  85.         results.push_back(abs(sum1) + abs(e * sum2));
  86.     }
  87.  
  88.     return results;
  89. }
  90.  
  91. void signalToFile(const string& filePath, double* signal, int N)
  92. {
  93.     fstream file(filePath, ios::out);
  94.  
  95.     if (!file.good()) {
  96.         cerr << "ERROR: Cannot write to file " << filePath << endl << endl;
  97.         return;
  98.     }
  99.  
  100.     for (int i = 0; i < N; i++) {
  101.         file << signal[i] << endl;
  102.     }
  103.  
  104.     file.close();
  105. }
  106.  
  107. void signalToFile(const string& filePath, vector<double>& signal)
  108. {
  109.     fstream file(filePath, ios::out);
  110.  
  111.     if (!file.good()) {
  112.         cerr << "ERROR: Cannot write to file " << filePath << endl << endl;
  113.         return;
  114.     }
  115.  
  116.     for (auto & it : signal) {
  117.         file << fixed << it << endl;
  118.     }
  119.  
  120.     file.close();
  121. }
  122.  
  123. int main()
  124. {
  125.     int T = 1000;
  126.  
  127.     double* signal = generateSignal(T);
  128.     vector<double> fftSpectrum = fft(signal, T);
  129.  
  130.     signalToFile("signal.txt", signal, T);
  131.     signalToFile("spectrum.txt", fftSpectrum);
  132.  
  133.     delete[] signal;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement