BBMBB

Fourier Analysis Rectangle Signal

Sep 22nd, 2020
946
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define _USE_MATH_DEFINES
  2.  
  3. #include <iostream>
  4. #include <cmath>
  5. #include <fstream>
  6.  
  7. int main()
  8. {
  9.     const double begin = 0.0;
  10.     const double end = 2 * M_PI;
  11.     const double step = 0.0001;
  12.     double c_step = begin;
  13.     double temp = 0.0;
  14.  
  15.     const int n = 15000;        //Number of summed signals
  16.  
  17.     std::ofstream file;
  18.     file.open("Fourier.csv");
  19.  
  20.     file << "t" << ";" << "f(x)" << std::endl;
  21.  
  22.     while (c_step <= end) {
  23.        
  24.         for (int i = 1; i <= n; i++) {
  25.             temp = temp + 0.5 * (sin((2 * i - 1) * end * c_step)) / (2 * i - 1);
  26.         }
  27.        
  28.         std::cout << "Progress: " << (c_step / end) * 100 << "%" << std::endl;
  29.  
  30.         file << c_step << ";" << temp << std::endl;
  31.         c_step += step;
  32.         temp = 0.0;
  33.     }
  34.  
  35.     file.close();
  36.  
  37. }
  38.  
  39.  
RAW Paste Data