Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- #include <complex>
- #include <fftw3.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "fstream"
- #include <string>
- #include "windows.h"
- #include "math.h"
- using namespace std;
- ofstream fout;
- #define REAL 0
- #define IMAG 1
- #define pi 3.14159
- void fft(fftw_complex * in, fftw_complex *out, int N) {
- fftw_plan plan = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
- fftw_execute(plan);
- fftw_destroy_plan(plan);
- fftw_cleanup();
- }
- struct compexNumber {
- double Re;
- double Im;
- };
- void sinus() {
- double N;
- double A;
- cout << "enter N" << endl;
- cin >> N;
- cout << "enter A" << endl;
- cin >> A;
- fout.open("1.txt");
- double m;
- double T = 10;
- for (double i = 0; i < N; i++) {
- m = A * sin(3.14 * 2 * i / N);
- fout << i << " " << m << endl;
- }
- fout.close();
- }
- void delta() {
- fout.open("2.txt");
- for (int i = 0; i <= 10; i++) {
- if (i == 0) {
- fout << i << " " << "1" << endl;
- }
- else {
- fout << i << " " << "0" << endl;
- }
- }
- fout.close();
- }
- void heaviside() {
- fout.open("3.txt");
- for (int i = -10; i <= 10; i++) {
- if (i < 0) {
- fout << i << " " << "0" << endl;
- }
- else {
- fout << i << " " << "1" << endl;
- }
- }
- fout.close();
- }
- void randSignal() {
- fout.open("4.txt");
- double y;
- for (double i = 0; i < 1024; i++) {
- y = rand() % 1000;
- y = y / 1000 - 0.5;
- fout << i << " " << y << endl;
- }
- fout.close();
- }
- void rectangularSignal() {
- fout.open("5.txt");
- double D;
- cout << "enter D%" << endl;
- cin >> D;
- D /= 100;
- for (double i = 0; i < 1024; i++) {
- if (i / 1024 <= D) {
- fout << i << " " << "1" << endl;
- }
- if (i / 1024 > D) {
- fout << i << " " << "0" << endl;
- }
- }
- fout.close();
- }
- void radioImpuls() {
- fout.open("6.txt");
- double m;
- for (double i = 0; i < 1024; i++) {
- if (i <= 341 || i > 681) {
- fout << i << " " << "0" << endl;
- }
- if (i > 341 && i <= 681) {
- m = sin(3.14 * 2 * (i - 341) / 68);
- fout << i << " " << m << endl;
- }
- }
- fout.close();
- }
- void radioImpulsZatux() {
- fout.open("7.txt");
- double m;
- for (double i = 0; i < 1024; i++) {
- m = sin(3.14 * 2 * i / 128);
- m = m / pow(2, i / 250);
- fout << i << " " << m << endl;
- }
- fout.close();
- }
- void randDigital() {
- fout.open("8.txt");
- double m;
- for (double i = 0; i <= 99; i++) {
- m = rand() % 2;
- fout << i << " " << m << endl;
- }
- fout.close();
- }
- void signalNoise() {// 1 0.1
- fout.open("9.txt");
- double A, N, m, y;
- cout << "enter A" << endl;
- cin >> A;
- cout << "enter N(noise)" << endl;
- cin >> N;
- for (double i = 0; i < 1000; i++) {
- m = A * sin(3.14 * 2 * i / 1000);
- y = rand() % 1000;
- y = y / 1000 - 0.5;
- m = m + (N*y * 2);
- fout << i << " " << m << endl;
- }
- system("pause");
- fout.close();
- }
- void Furie(char buff[50], fftw_complex *in) {
- fstream fin(buff);
- double i, m;
- double E = 0;
- vector<double> signal;
- int count = 0;
- if (fin.is_open()) { // если файл не открыт
- while (fin >> i >> m)
- {
- in[count][0] = m;
- in[count][1] = 0;
- count++;
- signal.push_back(m);
- }
- }
- double N = signal.size();
- vector<compexNumber> Xk(N);
- double step = 2 * pi / N;
- for (int k = 0; k < N; k++) {
- compexNumber sum;
- sum.Im = 0;
- sum.Re = 0;
- for (int n = 0; n < N; n++) {
- compexNumber e;
- e.Re = cos(step*k*n);
- e.Im = -sin(step*k*n);
- sum.Im += e.Im*signal[n];
- sum.Re += e.Re*signal[n];
- }
- Xk[k] = sum;
- }
- vector<double> Ak(N);
- vector<double> Uk(N);
- for (int i = 0; i < N; i++) {
- Ak[i] = sqrt(pow(Xk[i].Re, 2) + pow(Xk[i].Im, 2));
- Uk[i] = -atan(Xk[i].Im / Xk[i].Re) / 2 / pi * 360;
- }
- fout.open("Ak.txt");
- for (int i = 0; i < N; i++) {
- E += pow(Ak[i], 2);
- }
- E /= N;
- for (int i = 0; i < N / 2; i++) {
- if (Ak[i] < E / pow(N, 0.8)) {//тут править коэффициент для обнуления гармоник
- Ak[i] = 0;
- Uk[i] = 0;
- }
- fout << i << " " << Ak[i] / N << endl;
- }
- fout.close();
- fout.open("Uk.txt");
- for (int i = 0; i < N / 2; i++) {
- fout << i << " " << Uk[i] << endl;
- }
- fout.close();
- }
- void WriteFile(fftw_complex *out, int N) {
- vector<double> Ak(N);
- vector<double> Uk(N);
- for (int i = 0; i < N; i++) {
- Ak[i] = sqrt(pow(out[i][0], 2) + pow(out[i][1], 2));
- Uk[i] = -atan(out[i][1] / out[i][0]) / 2 / pi * 360;
- }
- // fout.close();
- fout.open("BPFAk.txt");
- double E;
- E = 0;
- for (int i = 0; i < N; i++) {
- E += pow(Ak[i], 2);
- }
- E /= N;
- for (int i = 0; i < N / 2; i++) {
- if (Ak[i] < 0) {//тут править коэффициент для обнуления гармоник 1:2 2:0 3:0.65 4:0 5:1.1 6:0.5 7:0.5 8:0
- Ak[i] = 0;
- Uk[i] = 0;
- }
- fout << i << " " << Ak[i]<< endl;
- }
- fout.close();
- fout.open("BPFUk.txt");
- for (int i = 0; i < N / 2; i++) {
- fout << i << " " << Uk[i] << endl;
- }
- fout.close();
- }
- int main()
- {
- const int N = 1000;//тут объявл кол-во точек 1:1024 2:11 3:21 4:1024 5:1024 6:1024 7:1024 8:100 9:1000
- fftw_complex in[N], out[N];
- sinus();
- delta();
- heaviside();
- randSignal();
- rectangularSignal();
- radioImpuls();
- radioImpulsZatux();
- randDigital();
- signalNoise();
- char buff[50] = "9.txt";
- double start = GetTickCount();
- Furie(buff, in);
- double finish = GetTickCount();
- cout << "Furie worked :" << finish - start << endl;
- start = GetTickCount();
- fft(in, out, N);
- finish = GetTickCount();
- cout << "BPF worked :" << finish - start << endl;
- WriteFile(out, N);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement