-
Notifications
You must be signed in to change notification settings - Fork 0
/
NFT.cpp
172 lines (137 loc) · 3.63 KB
/
NFT.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* Non-equidistant Fourier Transform
*
* Uso: Debe haber un archivo de datos con columnas x y y de la serie de tiempo a analizar.
*
* comando:
* $ ./NFT "datos.dat" f0 ff step
*
* y también:
*
* $ ./NFT "datos.dat" f0 ff step "ouput_name.dat"
*
* "datos.dat": string Archivo a leer
* f0: double Inicio del espacio de frecuencias.
* ff: double Final del espacio de frecuencias.
* step double Resolución del espacio de frecuencias.
* "ouput_name.dat": string archivo a escribir los n datos de la transformada. Se escribirán en tres columnas, una frecuencia, otra parte real y otra parte imaginaria.
*
**/
#include <iostream>
#include <fstream>
#include <math.h>
#include <vector>
#include <cstdlib>
#include <complex>
#include <cmath>
#include <algorithm>
#include <string>
#include <sstream>
using namespace std;
typedef complex<double> Complex;
Complex I=1i;
Complex Pi=3.1415926535897+I*0.00000000000000;
/**
* Transformada de los datos para una frecuencia freq
**/
Complex FourierTransform(vector<double> tdata,vector<double> ydata, double freq)
{
Complex s=0.;
for(int i=0;i<tdata.size();i++)
{
s+=ydata[i]*exp(-2.0*Pi*I*freq*tdata[i]);
}
return s;
}
int countfind(string str,string query)
{
int counter=0;
for(int i=0;i<str.size();i++)
{
string comp;
comp=str[i];
if(comp==query)
{
counter++;
}
}
return counter;
}
int main (int argc, char *argv[])
{
// Leer los argumentos
string filename = argv[1];
double f0=atof(argv[2]);
double ff=atof(argv[3]);
double step=atof(argv[4]);
string out_name = "fourier_output.dat";
if(argc==6){out_name = argv[5];}
//ver cuántas columnas tiene el archivo, y sólo agarrar las primeras 2. aceptados al parecer " " y "\t" como separadores.
ifstream datos(filename);
string line;
getline(datos,line);
int spaces = countfind(line," ");
int tabs = countfind(line,"\t");
// Leer los datos del archivo .dat y guardarlos en los vectores X y Y
vector<double> X, Y, Z;
double x, y, z;
if(tabs>1 || spaces>1)
{
while(datos >> x >> y >> z)
{
X.push_back(x);
Y.push_back(y);
Z.push_back(z);
}
}else
{
while(datos >> x >> y)
{
X.push_back(x);
Y.push_back(y);
}
}
// Normalizar los datos
double Yprom=0.000;
double Ymax=0.0;
for(int i=0;i<Y.size();i++)
{
Yprom+=Y[i];
if(Y[i]>Ymax)
{
Ymax=Y[i];
}
}
Yprom=Yprom/Y.size();
vector<double> Y2;
for(int i=0;i<Y.size();i++)
{
Y2.push_back((Y[i]-Yprom));
}
vector<double> X2;
for(int i=0;i<X.size();i++)
{
X2.push_back(X[i]-X[0]);
}
// Calcular la transformada de los datos en el espacio de frecuencias indicado y guardarla en vectores.
vector<double> FrequencySpace, RealPart, ImagPart;
double f=f0;
while(f<=ff)
{
FrequencySpace.push_back(f);
Complex transf=FourierTransform(X,Y2,f);
//cout << transf << endl;
RealPart.push_back(real(transf));
//cout << real(transf) << endl;
ImagPart.push_back(imag(transf));
f+=step;
}
// Escribir el archivo con la transformada de los datos
ofstream result(out_name);
for(int i=0; i < FrequencySpace.size(); i++)
{
result << FrequencySpace[i] << "\t" << sqrt(pow(RealPart[i],2)+pow(ImagPart[i],2)) << endl;
}
result.close();
return 0;
}