Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.53 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <complex>
  5. #include <fftw3.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "fstream"
  9. #include <string>
  10. #include "windows.h"
  11. #include "math.h"
  12.  
  13. using namespace std;
  14. ofstream fout;
  15.  
  16.  
  17. #define REAL 0
  18. #define IMAG 1
  19. #define pi 3.14159
  20.  
  21. void fft(fftw_complex * in, fftw_complex *out, int N) {
  22.     fftw_plan plan = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
  23.     fftw_execute(plan);
  24.     fftw_destroy_plan(plan);
  25.     fftw_cleanup();
  26. }
  27.  
  28.  
  29. struct compexNumber {
  30.     double Re;
  31.     double Im;
  32. };
  33.  
  34. void sinus() {
  35.     double N;
  36.     double A;
  37.     cout << "enter N" << endl;
  38.     cin >> N;
  39.     cout << "enter A" << endl;
  40.     cin >> A;
  41.     fout.open("1.txt");
  42.     double m;
  43.     double T = 10;
  44.     for (double i = 0; i < N; i++) {
  45.         m = A * sin(3.14 * 2 * i / N);
  46.         fout << i << " " << m << endl;
  47.     }
  48.     fout.close();
  49. }
  50. void delta() {
  51.     fout.open("2.txt");
  52.     for (int i = 0; i <= 10; i++) {
  53.         if (i == 0) {
  54.             fout << i << " " << "1" << endl;
  55.         }
  56.         else {
  57.             fout << i << " " << "0" << endl;
  58.         }
  59.     }
  60.     fout.close();
  61. }
  62.  
  63. void heaviside() {
  64.     fout.open("3.txt");
  65.     for (int i = -10; i <= 10; i++) {
  66.         if (i < 0) {
  67.             fout << i << " " << "0" << endl;
  68.         }
  69.         else {
  70.             fout << i << " " << "1" << endl;
  71.         }
  72.     }
  73.     fout.close();
  74. }
  75.  
  76. void randSignal() {
  77.     fout.open("4.txt");
  78.     double y;
  79.     for (double i = 0; i < 1024; i++) {
  80.         y = rand() % 1000;
  81.         y = y / 1000 - 0.5;
  82.         fout << i << " " << y << endl;
  83.     }
  84.     fout.close();
  85. }
  86.  
  87. void rectangularSignal() {
  88.     fout.open("5.txt");
  89.     double D;
  90.     cout << "enter D%" << endl;
  91.     cin >> D;
  92.     D /= 100;
  93.     for (double i = 0; i < 1024; i++) {
  94.         if (i / 1024 <= D) {
  95.             fout << i << " " << "1" << endl;
  96.         }
  97.         if (i / 1024 > D) {
  98.             fout << i << " " << "0" << endl;
  99.         }
  100.     }
  101.     fout.close();
  102. }
  103.  
  104. void radioImpuls() {
  105.     fout.open("6.txt");
  106.     double m;
  107.     for (double i = 0; i < 1024; i++) {
  108.         if (i <= 341 || i > 681) {
  109.             fout << i << " " << "0" << endl;
  110.         }
  111.         if (i > 341 && i <= 681) {
  112.             m = sin(3.14 * 2 * (i - 341) / 68);
  113.             fout << i << " " << m << endl;
  114.         }
  115.     }
  116.     fout.close();
  117. }
  118. void radioImpulsZatux() {
  119.     fout.open("7.txt");
  120.     double m;
  121.     for (double i = 0; i < 1024; i++) {
  122.         m = sin(3.14 * 2 * i / 128);
  123.         m = m / pow(2, i / 250);
  124.         fout << i << " " << m << endl;
  125.     }
  126.     fout.close();
  127. }
  128.  
  129. void randDigital() {
  130.     fout.open("8.txt");
  131.     double m;
  132.     for (double i = 0; i <= 99; i++) {
  133.         m = rand() % 2;
  134.         fout << i << " " << m << endl;
  135.     }
  136.     fout.close();
  137. }
  138.  
  139. void signalNoise() {// 1 0.1
  140.     fout.open("9.txt");
  141.     double A, N, m, y;
  142.     cout << "enter A" << endl;
  143.     cin >> A;
  144.     cout << "enter N(noise)" << endl;
  145.     cin >> N;
  146.     for (double i = 0; i < 1000; i++) {
  147.         m = A * sin(3.14 * 2 * i / 1000);
  148.         y = rand() % 1000;
  149.         y = y / 1000 - 0.5;
  150.         m = m + (N*y * 2);
  151.         fout << i << " " << m << endl;
  152.     }
  153.     system("pause");
  154.     fout.close();
  155. }
  156.  
  157. void Furie(char buff[50], fftw_complex *in) {
  158.     fstream fin(buff);
  159.     double i, m;
  160.     double E = 0;
  161.     vector<double> signal;
  162.     int count = 0;
  163.     if (fin.is_open()) { // если файл не открыт
  164.         while (fin >> i >> m)
  165.         {
  166.             in[count][0] = m;
  167.             in[count][1] = 0;
  168.             count++;
  169.             signal.push_back(m);
  170.         }
  171.     }
  172.     double N = signal.size();
  173.     vector<compexNumber> Xk(N);
  174.     double step = 2 * pi / N;
  175.     for (int k = 0; k < N; k++) {
  176.         compexNumber sum;
  177.         sum.Im = 0;
  178.         sum.Re = 0;
  179.         for (int n = 0; n < N; n++) {
  180.             compexNumber e;
  181.             e.Re = cos(step*k*n);
  182.             e.Im = -sin(step*k*n);
  183.             sum.Im += e.Im*signal[n];
  184.             sum.Re += e.Re*signal[n];
  185.         }
  186.         Xk[k] = sum;
  187.     }
  188.     vector<double> Ak(N);
  189.     vector<double> Uk(N);
  190.     for (int i = 0; i < N; i++) {
  191.         Ak[i] = sqrt(pow(Xk[i].Re, 2) + pow(Xk[i].Im, 2));
  192.         Uk[i] = -atan(Xk[i].Im / Xk[i].Re) / 2 / pi * 360;
  193.  
  194.     }
  195.     fout.open("Ak.txt");
  196.  
  197.     for (int i = 0; i < N; i++) {
  198.         E += pow(Ak[i], 2);
  199.     }
  200.     E /= N;
  201.     for (int i = 0; i < N / 2; i++) {
  202.         if (Ak[i] < E / pow(N, 0.8)) {//тут править коэффициент для обнуления гармоник
  203.             Ak[i] = 0;
  204.             Uk[i] = 0;
  205.         }
  206.         fout << i << " " << Ak[i] / N << endl;
  207.     }
  208.     fout.close();
  209.     fout.open("Uk.txt");
  210.     for (int i = 0; i < N / 2; i++) {
  211.         fout << i << " " << Uk[i] << endl;
  212.     }
  213.  
  214.     fout.close();
  215. }
  216.  
  217. void WriteFile(fftw_complex *out, int N) {
  218.     vector<double> Ak(N);
  219.     vector<double> Uk(N);
  220.     for (int i = 0; i < N; i++) {
  221.         Ak[i] = sqrt(pow(out[i][0], 2) + pow(out[i][1], 2));
  222.         Uk[i] = -atan(out[i][1] / out[i][0]) / 2 / pi * 360;
  223.     }
  224. //  fout.close();
  225.     fout.open("BPFAk.txt");
  226.     double E;
  227.     E = 0;
  228.     for (int i = 0; i < N; i++) {
  229.         E += pow(Ak[i], 2);
  230.     }
  231.     E /= N;
  232.     for (int i = 0; i < N / 2; i++) {
  233.         if (Ak[i] < 0) {//тут править коэффициент для обнуления гармоник 1:2 2:0 3:0.65 4:0 5:1.1 6:0.5 7:0.5 8:0
  234.             Ak[i] = 0;
  235.             Uk[i] = 0;
  236.         }
  237.         fout << i << " " << Ak[i]<< endl;
  238.     }
  239.     fout.close();
  240.     fout.open("BPFUk.txt");
  241.     for (int i = 0; i < N / 2; i++) {
  242.         fout << i << " " << Uk[i] << endl;
  243.     }
  244.  
  245.     fout.close();
  246. }
  247. int main()
  248. {
  249.     const int N = 1000;//тут объявл кол-во точек 1:1024 2:11 3:21 4:1024 5:1024 6:1024 7:1024 8:100 9:1000
  250.     fftw_complex in[N], out[N];
  251.     sinus();
  252.     delta();
  253.     heaviside();
  254.     randSignal();
  255.     rectangularSignal();
  256.     radioImpuls();
  257.     radioImpulsZatux();
  258.     randDigital();
  259.     signalNoise();
  260.     char buff[50] = "9.txt";
  261.     double start = GetTickCount();
  262.     Furie(buff, in);
  263.     double finish = GetTickCount();
  264.     cout << "Furie worked :" << finish - start << endl;
  265.     start = GetTickCount();
  266.     fft(in, out, N);
  267.     finish = GetTickCount();
  268.     cout << "BPF worked :" << finish - start << endl;
  269.     WriteFile(out, N);
  270.     system("pause");
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement