forked from lppier/DSPFunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSPFunctions.hpp
574 lines (494 loc) · 14.3 KB
/
DSPFunctions.hpp
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
* DSPFunctions.hpp
*
* Created on: Jan 25, 2016
* Author: pier
*/
#ifndef DSPFUNCTIONS_HPP_
#define DSPFUNCTIONS_HPP_
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <memory>
#include "ipp.h"
struct xyz
{
double x;
double y;
double z;
} typedef xyz;
struct lla
{
double lat;
double lon;
double alt;
} typedef lla;
template<typename T>
std::vector<std::size_t> sort_indexes(const std::vector<T> &v, bool ascending);
template<typename T>
std::vector<T> subVector(const std::vector<T> &vecIn, int firstIdx, int lastIdx);
template<typename T>
void printVec(const std::vector<T> &vecIn);
template<typename T>
void printArray(T *array, std::size_t size);
template<typename T>
std::size_t indexOfLargestElement(T arr[], std::size_t size, T *largestVal);
template<typename T>
std::size_t indexOfLargestElement(T arr[], std::size_t size);
template<typename T>
void circshift(T *in, T *out, int xdim, int ydim, int xshift, int yshift);
template<typename T>
void circshift1D_OP(T *in, T *out, int ydim, int yshift);
template<typename T>
void ifftshift1D(T *in, T *out, int ydim);
template<typename T>
void fftshift1D(T *in, T *out, int ydim);
template<typename T>
void ifftshift2D(T *in, T *out, int xdim, int ydim);
template<typename T>
void fftshift2D(T *in, T *out, int xdim, int ydim);
template<typename T>
void detrend(T *y, int m);
template<typename T>
void matrixTranspose(T *a, int rows, int cols, T *b);
void subArray(const Ipp32f *arrayIn, int start, int end, Ipp32f *subArrayOut);
void cumulativeSum(Ipp32f *array, int size);
void printArrayIpp32fc(Ipp32fc *array, std::size_t size);
void Common_TaylorWin(float *wt, int len);
void simple_transpose_32fc(Ipp32fc *src, Ipp32fc *dst, int nrows, int ncols);
std::vector<int> primeFactors(int n);
int findNFFT(int n);
/**
* Index sort
*
* Returns index of data after data is sorted.
* Used in Phase Gradient Algorithm.
*
* Inputs:
* v - vector of values to be sorted
*
* Outputs:
* Returns vector of indices (std::size_t type) of original data elements, sorted.
*
*/
template<typename T>
std::vector<std::size_t> sort_indexes(const std::vector<T> &v, bool ascending)
{
// initialize original index locations
std::vector<std::size_t> idx(v.size());
for (std::size_t i = 0; i != idx.size(); ++i)
idx[i] = i;
if (ascending)
{
sort(idx.begin(), idx.end(), [&v](std::size_t i1, std::size_t i2)
{ return v[i1] < v[i2]; });
}
else
{
sort(idx.begin(), idx.end(), [&v](std::size_t i1, std::size_t i2)
{ return v[i1] > v[i2]; }); //-- descending order
}
return idx;
}
/**
* subVector
*
* Returns subset of input vector given by firstIdx and lastIdx.
*
* Inputs:
* vecIn - Input vector
* firstIdx - location of beginning of subset vector
* lastIdx - location of end of subset vector
*
* Outputs:
* Returns a subset of the input vector.
*
*/
template<typename T>
std::vector<T> subVector(const std::vector<T> &vecIn, int firstIdx, int lastIdx)
{
typename std::vector<T>::const_iterator first = vecIn.begin() + firstIdx; // matlab: rgBin0(1:NrgBin1);
typename std::vector<T>::const_iterator last = vecIn.begin() + lastIdx;
typename std::vector<T> tempVec(first, last);
return tempVec;
}
template<typename T>
void printVec(const std::vector<T> &vecIn)
{
std::cout << "Vector size : " << vecIn.size() << std::endl;
std::cout << "[";
for (unsigned int i = 0; i < vecIn.size(); i++)
std::cout << vecIn[i] << " ";
std::cout << "]" << std::endl;
}
template<typename T>
void printArray(T *array, std::size_t size)
{
std::cout << "Array size : " << size << std::endl;
std::cout << "[";
std::cout << std::scientific;
for (unsigned int i = 0; i < size; i++)
std::cout << i << ": " << array[i] << " ";
std::cout << "]" << std::endl;
}
/**
* indexOfLargestElement
*
* Returns the index of the largest element in the array.
*
* Inputs:
* arr[] - input array
* size - size of array
* largestVal - pointer to the address to store the largest value
*
* Outputs:
* Returns largestIndexValue
*/
template<typename T>
std::size_t indexOfLargestElement(T arr[], std::size_t size, T *largestVal)
{
std::size_t largestIndex = 0;
for (std::size_t index = largestIndex; index < size; index++)
{
if (arr[largestIndex] < arr[index])
{
largestIndex = index;
}
}
*largestVal = arr[largestIndex];
return largestIndex;
}
/**
* indexOfLargestElement
*
* Returns the index of the largest element in the array.
*
* Inputs:
* arr[] - input array
* size - size of array
* largestVal - pointer to the address to store the largest value
*
* Outputs:
* Returns largestIndexValue
*
* Note: Overloaded for cases where you don't need the actual value, just the index.
*/
template<typename T>
std::size_t indexOfLargestElement(T arr[], std::size_t size)
{
std::size_t largestIndex = 0;
for (std::size_t index = largestIndex; index < size; index++)
{
if (arr[largestIndex] < arr[index])
{
largestIndex = index;
}
}
return largestIndex;
}
/**
* circshift
*
* Does a 2D circular shift like the Matlab command.
*
* Inputs:
* out* - pointer to a buffer for the result
* in* - pointer to the input data
* xdim - size of the x-dimension
* ydim - size of the y-dimension
* xshift - shifting to be done along x-dimension
* yshift - shifting to be done along y-dimension
*
* Can be further optimized using std::rotate
*/
template<typename T>
inline void circshift(T *in, T *out, int xdim, int ydim, int xshift, int yshift)
{
if (xshift == 0 && yshift == 0)
{
out = in; //-- no change
return;
}
for (int i = 0; i < xdim; i++)
{
int ii = (i + xshift) % xdim;
if (ii < 0)
ii = xdim + ii;
for (int j = 0; j < ydim; j++)
{
int jj = (j + yshift) % ydim;
if (jj < 0)
jj = ydim + jj;
out[ii * ydim + jj] = in[i * ydim + j];
}
}
}
/**
* Does 1D Circshift (in-place)
*
* @param in Input array of values, circshift done directly on this
* @param ydim Length of the array
* @param yshift Amount to be shifted (+ve is shift right, -ve is shift left)
*/
template<typename T>
inline void circshift1D_IP(T *in, int ydim, int yshift)
{
if (yshift == 0)
return;
if (yshift > 0) // shift right
{
//std::rotate(&in[0], &in[ydim - yshift - 1], &in[ydim - 1]);
std::rotate(in, in + (ydim - yshift), in + ydim);
}
else if (yshift < 0) // shift left
{
yshift = abs(yshift);
//std::rotate(&in[0], &in[yshift], &in[ydim - 1]);
std::rotate(in, in + yshift, in + ydim);
}
return;
}
/**
* Does 1D Circshift (out-of-place)
*
* @param in Input array of values
* @param out Circshifted array of values
* @param ydim Length of the array
* @param yshift Amount to be shifted (+ve is shift right, -ve is shift left)
*/
template<typename T>
inline void circshift1D_OP(T *in, T *out, int ydim, int yshift)
{
if (yshift == 0)
{
out = in; //-- no change
return;
}
memcpy(out, in, ydim * sizeof(T));
if (yshift > 0) // shift right
{
//std::rotate(&out[0], &out[ydim - yshift], &out[ydim]); // TODO check indices may be ydim-yshift
std::rotate(out, out + (ydim - yshift), out + ydim); // C++ idiom: out + ydim is not used, out + ydim -1 is referenced
}
else if (yshift < 0) // shift left
{
yshift = abs(yshift);
//std::rotate(&out[0], &out[yshift], &out[ydim - 1]);
std::rotate(out, out + yshift, out + ydim); // TODO check
}
return;
// for (int j = 0; j < ydim; j++)
// {
// int jj = (j + yshift) % ydim;
// if (jj < 0)
// jj = ydim + jj;
// out[jj] = in[j];
// }
}
/**
* Does 1D ifftshift
* Note: T* out must already by memory allocated!!
*/
template<typename T>
inline void ifftshift1D(T *in, T *out, int ydim)
{
//-- (ydim & 1)==0
int pivot = (ydim % 2 == 0) ? (ydim / 2) : ((ydim + 1) / 2);
//circshift1D(in, out, ydim, shiftBy);
int rightHalf = ydim-pivot;
int leftHalf = pivot;
memcpy(out, in+(pivot), sizeof(T)*rightHalf);
memcpy(out+rightHalf, in, sizeof(T)*leftHalf);
}
/**
* Does 1D fftshift
* Note: T* out must already by memory allocated!!
*/
template<typename T>
inline void fftshift1D(T *in, T *out, int ydim)
{
int pivot = (ydim % 2 == 0) ? (ydim / 2) : ((ydim - 1) / 2);
//circshift1D(in, out, ydim, shiftBy);
int rightHalf = ydim-pivot;
int leftHalf = pivot;
memcpy(out, in+(pivot), sizeof(T)*rightHalf);
memcpy(out+rightHalf, in, sizeof(T)*leftHalf);
}
/**
* Slow due to the circshift, but works.
*/
template<typename T>
inline void ifftshift2D(T *in, T *out, int xdim, int ydim)
{
int shiftYBy = (ydim % 2 == 0) ? (ydim / 2) : ((ydim + 1) / 2);
int shiftXBy = (xdim % 2 == 0) ? (xdim / 2) : ((xdim + 1) / 2);
circshift(in, out, xdim, ydim, shiftXBy, shiftYBy);
}
/**
* Slow due to the circshift, but works
*/
template<typename T>
inline void fftshift2D(T *in, T *out, int xdim, int ydim)
{
int shiftYBy = (ydim % 2 == 0) ? (ydim / 2) : ((ydim - 1) / 2);
int shiftXBy = (xdim % 2 == 0) ? (xdim / 2) : ((xdim - 1) / 2);
circshift(in, out, xdim, ydim, shiftXBy, shiftYBy);
}
/************************************************************************************
Function : void detrend_IP(T *y, T *x, int m)
Description : Remove the linear trend of the input floating point data. Note that this
will initialize a work buffer inside the function. So if you are calling
this many, many times, create your work buffer in the calling scope and call
detrend(T *y, T*x, int m) instead to avoid initializing memory over and over
again.
Inputs : y - Floating point input data
m - Input data length
Outputs : y - Data with linear trend removed
Copyright : DSO National Laboratories
History : 01/02/2008, TCK, Adapted from HYC code
01/12/2008, TCK, Added in return value
25/01/2016, Pier, Changed into template type, removed need for work buffer
*************************************************************************************/
template<typename T>
void detrend_IP(T *y, int m)
{
T xmean, ymean;
int i;
T temp;
T Sxy;
T Sxx;
T grad;
T yint;
std::unique_ptr<T[]> x(new T[m]);
/********************************
Set the X axis Liner Values
*********************************/
for (i = 0; i < m; i++)
x[i] = i;
/********************************
Calculate the mean of x and y
*********************************/
xmean = 0;
ymean = 0;
for (i = 0; i < m; i++)
{
xmean += x[i];
ymean += y[i];
}
xmean /= m;
ymean /= m;
/********************************
Calculate Covariance
*********************************/
temp = 0;
for (i = 0; i < m; i++)
temp += x[i] * y[i];
Sxy = temp / m - xmean * ymean;
temp = 0;
for (i = 0; i < m; i++)
temp += x[i] * x[i];
Sxx = temp / m - xmean * xmean;
/********************************
Calculate Gradient and Y intercept
*********************************/
grad = Sxy / Sxx;
yint = -grad * xmean + ymean;
/********************************
Removing Linear Trend
*********************************/
for (i = 0; i < m; i++)
y[i] = y[i] - (grad * i + yint);
}
/************************************************************************************
Function : void detrend_OP(T *y, T *x, int m)
Description : Remove the linear trend of the input floating point data
Inputs : y - Floating point input data
x - Work buffer (must be initialized in calling scope!)
m - Input data length
Outputs : y - Data with linear trend removed
Copyright : DSO National Laboratories
History : 01/02/2008, TCK, Adapted from HYC code
01/12/2008, TCK, Added in return value
25/01/2016, Pier, Changed into template type
*************************************************************************************/
template<typename T>
void detrend_OP(T *y, T*x, int m)
{
T xmean, ymean;
int i;
T temp;
T Sxy;
T Sxx;
T grad;
T yint;
/********************************
Set the X axis Liner Values
*********************************/
for (i = 0; i < m; i++)
x[i] = i;
/********************************
Calculate the mean of x and y
*********************************/
xmean = 0;
ymean = 0;
for (i = 0; i < m; i++)
{
xmean += x[i];
ymean += y[i];
}
xmean /= m;
ymean /= m;
/********************************
Calculate Covariance
*********************************/
temp = 0;
for (i = 0; i < m; i++)
temp += x[i] * y[i];
Sxy = temp / m - xmean * ymean;
temp = 0;
for (i = 0; i < m; i++)
temp += x[i] * x[i];
Sxx = temp / m - xmean * xmean;
/********************************
Calculate Gradient and Y intercept
*********************************/
grad = Sxy / Sxx;
yint = -grad * xmean + ymean;
/********************************
Removing Linear Trend
*********************************/
for (i = 0; i < m; i++)
y[i] = y[i] - (grad * i + yint);
}
/**
* Works but stupid and slow. Take a look at simple_transpose_32fc
*/
template<typename T>
void matrixTranspose(T *in, int rows, int cols, T *out)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
*(out + (j * rows) + i) = *(in + (i * cols) + j); // a[i][j] == a + i * col + j
}
}
}
/*!
* Same as matlab's diff in 1 dimension
* f X is a vector, then diff(X) returns a vector, one element shorter than X, of differences between adjacent elements:
*[X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)]
*
* @param in Input vector
* @param out Output vector
* @param noOfElements Self-explanatory
*/
template<typename T>
void diff(T *in, T *out, int noOfElements)
{
for (int i = 0; i < noOfElements - 1; i++)
out[i] = in[i + 1] - in[i];
}
#endif /* DSPFUNCTIONS_HPP_ */