-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.h
301 lines (240 loc) · 6.24 KB
/
main.h
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#pragma once
/*
Librerias
***/
#include "plot.h"
#include <iostream>
#include <fstream>
#include <string>
#include <fftw3.h>
#include <math.h>
/*
Constantes
***/
/*
Espacios de trabajo
***/
/*
Tipo de datos
***/
typedef unsigned int uint;
struct Spectrum{
float *mag; // Magnitud
float *phs; // Fase
};
struct DataSignal{
float *data, // Datos de 1 canal
**dataC; // Datos multicanal
char channel; // Cantidad de canales
size_t size; // Longitud de los datos
float med, // Media de los datos
std, // Desviacion estandar de los datos
max, // Maximo valor de los datos
min; // Minimo valor de los datos
float *medC, // Media de los datos por canal
*stdC, // Desviación estandar por canal
*maxC, // Maximo valor de los datos por canal
*minC; // Minimo valor de los datos por canal
/*********************************************************
** Cargar datos desde texto
**********************************************************/
void loadTXT(string file,size_t _size, char _channel){
fstream myfile;
uint i,j;
channel = _channel;
size = _size;
// Un solo canal
if(_channel == 1){
float aux = 0;
float band;
// Inicializacion de variables
data = new float [_size];
// Cargar
myfile.open(file.c_str());
for(i=0;i<_size;i++){
myfile >> data[i];
aux += data[i];
}
myfile.close();
// Parametros de los datos
med = aux/_size;
aux = 0;
min = data[0];
max = data[0];
for(i=0;i<_size;i++){
band = data[i] - med;
aux += band*band;
if(min>data[i]) min = data[i];
if(max<data[i]) max = data[i];
}
std = sqrt(aux/_size);
}
// Varios canales
else{
float *aux;
float aux2;
float band;
// Inicializacion de variables
dataC = new float*[_size ];
aux = new float [_channel];
medC = new float [_channel];
stdC = new float [_channel];
minC = new float [_channel];
maxC = new float [_channel];
for(i=0;i<_size; i++) dataC [i] = new float [_channel];
for(j=0;j<_channel;j++) aux [j] = 0;
// Cargar
myfile.open(file.c_str());
for(i=0;i<_size;i++) for(j=0;j<_channel;j++){
myfile >> dataC[i][j];
aux[j] += dataC[i][j];
}
myfile.close();
// Parametros de los datos
for(j=0;j<_channel;j++){
medC[j] = aux[j]/_size;
aux2 = 0;
minC[j] = dataC[0][j];
maxC[j] = dataC[0][j];
for(i=0;i<_size;i++){
band = dataC[i][j] - medC[j];
aux2 += band*band;
if(minC[j]>dataC[i][j]) minC[j] = dataC[i][j];
if(maxC[j]<dataC[i][j]) maxC[j] = dataC[i][j];
}
stdC[j] = sqrt(aux2/_size);
}
}
}
void normalize(){
uint i,j;
// Un solo canal
if(channel == 1){
for(i=0;i<size;i++) data[i] = (data[i]-med)/std;
}
// Varios canales
else{
for(i=0;i<size;i++) for(j=0;j<channel;j++)
dataC[i][j] = (dataC[i][j]-medC[j])/stdC[j];
}
}
Spectrum fft(){
Spectrum outFFT;
if(channel == 1){
uint i;
double *in;
double band;
// Inicializar variables
in = new double [size];
outFFT.mag = new float [size];
outFFT.phs = new float [size];
// Variables para FFTW
fftw_complex *out;
fftw_plan plan;
// Creando plan FFTW
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * size);
plan = fftw_plan_dft_r2c_1d(size, in, out, FFTW_ESTIMATE);
// Cargando datos (float2double)
for(i=0;i<size;i++) in[i] = (double)data[i];
// Ejecutar FFTW
fftw_execute(plan);
// Calculando Magnitud y fase
for(i=0;i<size;i++){
band = out[i][0]*out[i][0] + out[i][1]*out[i][1];
outFFT.mag[i] = sqrt(band);
outFFT.phs[i] = atan2 (out[i][1],out[i][0]) * 180 / PI;
}
}
else cout << "Error en FFT. Se esperaba 1 canal" << endl;
return outFFT;
}
Spectrum fft(char _channel){
Spectrum outFFT;
if(channel > 1){
uint i;
double *in;
double band;
// Inicializar variables
in = new double [size];
outFFT.mag = new float [size];
outFFT.phs = new float [size];
// Variables para FFTW
fftw_complex *out;
fftw_plan plan;
// Creando plan FFTW
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * size);
plan = fftw_plan_dft_r2c_1d(size, in, out, FFTW_ESTIMATE);
// Cargando datos (float2double)
for(i=0;i<size;i++) in[i] = (double)dataC[i][_channel];
// Ejecutar FFTW
fftw_execute(plan);
// Calculando Magnitud y fase
for(i=0;i<size;i++){
band = out[i][0]*out[i][0] + out[i][1]*out[i][1];
outFFT.mag[i] = sqrt(band);
outFFT.phs[i] = atan2 (out[i][1],out[i][0]) * 180 / PI;
}
}
else cout << "Error en FFT. Se esperaba mas de 1 canal" << endl;
return outFFT;
}
float *fftMag(){
float *outFFT;
if(channel == 1){
uint i;
double *in;
double band;
// Inicializar variables
in = new double [size];
outFFT = new float [size];
// Variables para FFTW
fftw_complex *out;
fftw_plan plan;
// Creando plan FFTW
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * size);
plan = fftw_plan_dft_r2c_1d(size, in, out, FFTW_ESTIMATE);
// Cargando datos (float2double)
for(i=0;i<size;i++) in[i] = (double)data[i];
// Ejecutar FFTW
fftw_execute(plan);
// Calculando Magnitud y fase
for(i=0;i<size;i++){
band = out[i][0]*out[i][0] + out[i][1]*out[i][1];
outFFT[i] = sqrt(band);
}
}
else cout << "Error en FFT. Se esperaba 1 canal" << endl;
return outFFT;
}
float *fftMag(char _channel){
float *outFFT;
if(channel > 1){
uint i;
double *in;
double band;
// Inicializar variables
in = new double [size];
outFFT = new float [size];
// Variables para FFTW
fftw_complex *out;
fftw_plan plan;
// Creando plan FFTW
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * size);
plan = fftw_plan_dft_r2c_1d(size, in, out, FFTW_ESTIMATE);
// Cargando datos (float2double)
for(i=0;i<size;i++) in[i] = (double)dataC[i][_channel];
// Ejecutar FFTW
fftw_execute(plan);
// Calculando Magnitud y fase
for(i=0;i<size;i++){
band = out[i][0]*out[i][0] + out[i][1]*out[i][1];
outFFT[i] = sqrt(band);
}
}
else cout << "Error en FFT. Se esperaba mas de 1 canal" << endl;
return outFFT;
}
};
/*
Funciones
***/