-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
559 lines (453 loc) · 18.6 KB
/
example.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/****************************************************************
** OrangeBot Project
*****************************************************************
** /
** /
** /
** ______ \
** \
** \
*****************************************************************
** DHT Discrete hartley Transform
*****************************************************************
** Understand working and meaning of the Hartley Transform
****************************************************************/
/****************************************************************
** DESCRIPTION
****************************************************************
** The HT Hartley Transform is similar to the FT Fourier transform
** it works with real numbers instead of complex ones.
**
** HT Kenrel
** cas(x) = cos(x) +sin(x)
**
** FT Kernel
** cos(x) +i*sin(x)
****************************************************************/
/****************************************************************
** HISTORY VERSION
****************************************************************
**
****************************************************************/
/****************************************************************
** KNOWN BUGS
****************************************************************
**
****************************************************************/
/****************************************************************
** TODO
****************************************************************
**
****************************************************************/
/****************************************************************
** INCLUDES
****************************************************************/
//Standard C Libraries
#include <cmath> //for sin, cos,
//Standard C++ libraries
#include <iostream> //cout
#include <vector> //vector
//
#include "my_utils.h"
/****************************************************************
** NAMESPACES
****************************************************************/
//Never use a whole namespace. Use only what you need from it.
using std::cout;
using std::cerr;
//using std::endl; //endl is resource intensive. use "\n"
using std::vector;
using std::abs;
using std::sin;
using std::sqrt;
using std::atan2;
/****************************************************************
** DEFINES
****************************************************************/
#define N_SAMPLES 16
/****************************************************************
** MACROS
****************************************************************/
/****************************************************************
** PROTOTYPES
****************************************************************/
//Create a vector of length N filled with N periods of SIN with a given initial phase
extern vector<double> make_sin_vector( int size, double amplitude, double periods, double phase );
//Compute the hartley tansform and normalize the result by the number of elements
extern vector<double> hartley_transform( vector<double> in, bool f_normalize );
//overload of DHT without normalization
extern vector<double> hartley_transform( vector<double> in );
//Show content of vector
extern bool show_vector( vector<double> data );
//Show the meaning of the content of the Hartley Transform
extern bool show_hartley_transform_meaning( vector<double> data );
/****************************************************************
** GLOBAL VARIABILES
****************************************************************/
//C++ does not have a standard definition of PI (jarring)
const double pi = 3.14159265358979323846264338327950;
/****************************************************************
** FUNCTIONS
****************************************************************/
/****************************************************************
** MAIN
****************************************************************
** INPUT:
** OUTPUT:
** RETURN:
** DESCRIPTION:
****************************************************************/
int main()
{
///----------------------------------------------------------------
/// STATIC VARIABILE
///----------------------------------------------------------------
///----------------------------------------------------------------
/// LOCAL VARIABILE
///----------------------------------------------------------------
vector<double> signal, transform, anti_transform;
///----------------------------------------------------------------
/// CHECK AND INITIALIZATIONS
///----------------------------------------------------------------
///SETUP
double num_periods = 4.0;
double amplitude = 1.0;
double phase = 0.0 *2.0 *pi;
///----------------------------------------------------------------
/// BODY
///----------------------------------------------------------------
printf("OrangeBot Projects\n");
cout << "Parameters\n";
cout << "number of periods of sin inside the vector: " << ENG_NUM_CSTR(num_periods) << "\n";
cout << "amplitude of the wave: " << ENG_NUM_CSTR(amplitude) << "\n";
cout << "phase of the wave: " << ENG_NUM_CSTR(phase) << "\n";
cout << "\nSignal\n";
//Fill vector with a sin
signal = make_sin_vector( N_SAMPLES, amplitude, num_periods, phase );
show_vector( signal );
cout << "Transform\n";
//Apply the transform
transform = hartley_transform( signal, true );
show_vector( transform );
cout << "Anti-Transform\n";
//Anti transform to original signal
anti_transform = hartley_transform( transform );
show_vector( anti_transform );
cout << "Hartley Transform Meaning\n";
show_hartley_transform_meaning( transform );
///----------------------------------------------------------------
/// FINALIZATIONS
///----------------------------------------------------------------
return 0;
} //end function: main
/****************************************************************************
** make_sin_vector
*****************************************************************************
** PARAMETER:
** RETURN:
** DESCRIPTION:
** Create a vector of length N filled with N periods of SIN with a given initial phase
****************************************************************************/
vector<double> make_sin_vector( int size, double amplitude, double periods, double phase )
{
///--------------------------------------------------------------------------
/// STATIC VARIABILE
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// LOCAL VARIABILE
///--------------------------------------------------------------------------
//Counter
int t;
//Return vector filled with a sin
vector<double> ret;
///--------------------------------------------------------------------------
/// CHECK
///--------------------------------------------------------------------------
if (size <= 0)
{
//return empty vector
return ret;
}
///--------------------------------------------------------------------------
/// INITIALIZATIONS
///--------------------------------------------------------------------------
//Create vector
ret = vector<double>(size);
//if: vector still empty
if (ret.empty() == true)
{
//FAIL: failed to create vector
return ret;
}
///--------------------------------------------------------------------------
/// BODY
///--------------------------------------------------------------------------
//for: every element
for (t = 0;t < size;t++)
{
//Fill the vector with a sin of the given stats
ret[t] = amplitude *sin( phase +2.0 *pi *periods *t /size );
} //end for: every element
///--------------------------------------------------------------------------
/// FINALIZATIONS
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// RETURN
///--------------------------------------------------------------------------
//Return vector
return ret;
} //end function: make_sin_vector
/****************************************************************************
** Hartley Transform
*****************************************************************************
** PARAMETER:
** RETURN:
** DESCRIPTION:
** naive implementation of hartley transform
****************************************************************************/
vector<double> hartley_transform( vector<double> in, bool f_normalize )
{
///--------------------------------------------------------------------------
/// STATIC VARIABILE
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// LOCAL VARIABILE
///--------------------------------------------------------------------------
//Counters
unsigned int t, ti;
//argument of the CAS function
double arg;
//accumulator
double acc;
//Normalization factor
double norm;
//return vector
vector<double> out;
///--------------------------------------------------------------------------
/// CHECK
///--------------------------------------------------------------------------
//if: vectors are not valid
if (in.empty() == true)
{
cerr << "ERR: bad input vector\n";
//FAIL
return out;
}
///--------------------------------------------------------------------------
/// INITIALIZATIONS
///--------------------------------------------------------------------------
//Initialize output vector to the same size of the input
out = vector<double>( in.size() );
//if: failed to create vector
if (out.empty() == true)
{
cerr << "ERR: could not create output vector\n";
//FAIL
return out;
}
//if: normalization is active
if (f_normalize == true)
{
//Compute normalization factor
norm = 1.0 *in.size();
}
else
{
//neutral normalization
norm = 1.0;
}
///--------------------------------------------------------------------------
/// BODY
///--------------------------------------------------------------------------
//for: every element
for (t = 0;t<in.size();t++)
{
//Initialize accumulator
acc = 0.0;
//for: every element
for (ti = 0;ti < in.size();ti++)
{
//Argument of the CAS function
arg = 2.0 *pi *t *ti /(in.size());
//Calculate and accumulate the CAS of the element
acc += in[ti] *(cos(arg) +sin(arg));
} //end for: every element
//Write back result
out[t] = acc / norm;
} //end for: every element
///--------------------------------------------------------------------------
/// FINALIZATIONS
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// RETURN
///--------------------------------------------------------------------------
return out; //OK
} //end function: hartley_transform
/****************************************************************************
** Hartley Transform
*****************************************************************************
** PARAMETER:
** RETURN:
** DESCRIPTION:
** Wrapper without normalization
****************************************************************************/
inline vector<double> hartley_transform( vector<double> in )
{
//Execute regular function with full arguments. disable normalization.
return hartley_transform( in, false );
} //end function: hartley_transform
/****************************************************************************
** show_vector
*****************************************************************************
** PARAMETER:
** RETURN:
** DESCRIPTION:
** Show content of vector
****************************************************************************/
bool show_vector( vector<double> data )
{
///--------------------------------------------------------------------------
/// STATIC VARIABILE
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// LOCAL VARIABILE
///--------------------------------------------------------------------------
unsigned int t;
///--------------------------------------------------------------------------
/// CHECK
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// INITIALIZATIONS
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// BODY
///--------------------------------------------------------------------------
cout << "size: " << data.size() << "\n";
for(t = 0;t < data.size();t++)
{
cout << ENG_NUM_CSTR( data[t] ) << " | ";
}
cout << "\n";
///--------------------------------------------------------------------------
/// FINALIZATIONS
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// RETURN
///--------------------------------------------------------------------------
return false; //OK
} //end function: show_vector
/****************************************************************************
** show_hartley_transform_meaning
*****************************************************************************
** PARAMETER:
** RETURN:
** DESCRIPTION:
** Show the meaning of the content of the Hartley Transform
****************************************************************************/
bool show_hartley_transform_meaning( vector<double> data )
{
///--------------------------------------------------------------------------
/// STATIC VARIABILE
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// LOCAL VARIABILE
///--------------------------------------------------------------------------
//counter
int t;
//Size of the transform
int size;
//Temp vectors
vector<double> amplitude;
vector<double> phase;
///--------------------------------------------------------------------------
/// CHECK
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// INITIALIZATIONS
///--------------------------------------------------------------------------
size = data.size();
//Create vectors
amplitude = vector<double>( size/2 +1 );
phase = vector<double>( size/2 );
///--------------------------------------------------------------------------
/// BODY
///--------------------------------------------------------------------------
cout << "Size: " << size << "\n";
//constant frequency
amplitude[0] = abs( data[0] );
//frequencies with twin components
for (t = 1;t < size/2;t++)
{
amplitude[t] = sqrt( 2.0 *(data[t] *data[t] +data[size -t] *data[size -t ]) );
}
//maximum frequency. just 1 component
amplitude[size/2] = abs( data[size/2] );
for (t = 1;t < size/2;t++)
{
phase[t] = pi/4.0 +atan2( data[t], data[size -t] );
phase[t] = CLIP_PI( phase[t], pi );
}
for (t = 0;t < size/2 +1;t++)
{
//
cout << "Amplitude f" << t << " : " << ENG_NUM_CSTR( amplitude[t]);
if ((t > 0) && (t < size/2))
{
cout << " | Phase p" << t << " : " << ENG_NUM_CSTR( phase[t]);
}
cout << "\n";
}
cout << "\n\n\n";
cout << "NOTES\n";
cout << "element 0 is zero frequency. It's equivalent to the average of the signal\n";
cout << "element 1 is f1. it's the slowest sin wave with 1 period over the whole vector\n";
cout << "element size-1 is again f1. each frequency has two components.\n";
cout << "element 2 is f2. a sine wave that fits two periods inside the vector\n";
cout << "element size-2 is again f2.\n";
cout << "element 3 is f3. a sin with three periods. frequency grow toward the center\n";
cout << "element 0 and element size/2 have no twin component\n";
cout << "element of index size/2 is the highest frequency component.\n";
cout << "the highest frequency component is a sin with a period every two elements\n";
cout << "amplitude of a twin frequency is: sqrt(2*(fa^2 + fb^2))\n";
cout << " example. amplitude of f1 is sqrt( 2* (v[1]^2 +v[size-1]^2))\n";
cout << "phase of a twin frequency is: pi/4 +atan2( fa, fb )\n";
///--------------------------------------------------------------------------
/// FINALIZATIONS
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// RETURN
///--------------------------------------------------------------------------
return false;
} //end function: show_hartley_transform_meaning
/****************************************************************************
**
*****************************************************************************
** PARAMETER:
** RETURN:
** DESCRIPTION:
****************************************************************************/
void f( void )
{
///--------------------------------------------------------------------------
/// STATIC VARIABILE
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// LOCAL VARIABILE
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// CHECK
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// INITIALIZATIONS
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// BODY
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// FINALIZATIONS
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// RETURN
///--------------------------------------------------------------------------
return;
} //end function: