-
Notifications
You must be signed in to change notification settings - Fork 0
/
do_dft2.c
3644 lines (3406 loc) · 177 KB
/
do_dft2.c
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////////////
// //
// _ _ __ _ ____ //
// __| | ___ __| | / _|| |_ |___ \ ___ //
// / _` | / _ \ / _` || |_ | __| __) | / __| //
// | (_| || (_) | | (_| || _|| |_ / __/ _| (__ //
// \__,_| \___/_____\__,_||_| \__||_____|(_)\___| //
// |_____| //
// //
//////////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2013 by S.F.T. Inc. - All rights reserved //
// Use, copying, and distribution of this software may be licensed //
// using a GPLv2, GPLv3, MIT, or BSD license, as appropriate. //
// //
// OR - if you prefer - just use/distribute it without ANY license. //
// But I'd like some credit for it. A favorable mention is appreciated. //
// //
//////////////////////////////////////////////////////////////////////////////
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
//**************************************************************************
// build command: cc -O3 -o do_dft2 do_dft2.c -lm -lpthread
//
// you can add '-D' directives as appropriate (such as '-DUSE_FAST_SINCOS')
//**************************************************************************
// approximate value of pi beyond precision of double
#define _PI_ 3.1415926535897932384626433832795028841971693993751
#define MAX_HARMONIC 4096
//#define USE_FAST_SINCOS /* define this out to use the fast sin/cos instead of the 'libc' sin/cos */
//#define TEST_FAST_SINCOS /* define this to include the test '-!' for fast sin/cos */
#ifdef __gnu_linux__
#define HAS_SINCOS /* this should work for all GNU-LINUX implementations */
// NOTE: if this causes build problems, please let me know.
#endif // __gnu_linux__
// NOTE: FreeBSD's libc does not have 'sincos', nor do any of the ports
// for gcc, as far as I can tell. It's a GNU extension.
#ifdef USE_FAST_SINCOS
#define sin fast_sin
#define cos fast_cos
#define sincos fast_sincos
double dDebugY = 0.0;
int wDebugIndex = -1;
void fast_sincos(double dTheta, double *pSin, double *pCos);
double fast_cos(double);
double fast_sin(double);
#ifndef HAS_SINCOS
#define HAS_SINCOS
#endif // HAS_SINCOS
static const float _SIN_VALUES_[];
static const float * const _COS_VALUES_;
#endif // USE_FAST_SINCOS
typedef struct _XY_
{
double dX;
double dY;
} XY;
typedef struct _MY_XY_
{
XY *pData;
int nItems; // # of items
int nSize; //memory block size
} MY_XY;
typedef struct _WORK_UNIT_
{
double *pdA, *pdB;
XY *pData;
int nVal;
double dC, dX0, dXY; // for retest and for scaling X(Xnew = X * dXY + dX0, use 0.0 and 1.0 to leave X as - is)
long lStart, lEnd;
double dRval; // NOTE: cannot be 'passed in', initial value will be 0.0
volatile long lState; // initially zero, non - zero when thread has finished
pthread_t idThread; // caller waits on this object; object must be free 'd by caller via pthread_detach
// call pthread_join when finished to properly clean up and get err return
} WORK_UNIT;
WORK_UNIT *create_work_unit(double *pdA, double *pdB, double dC, XY * pData, int nVal,
double dX0, double dXY, long lStart, long lEnd,
void *(*callback) (void *), int iThreadFlag)
{
WORK_UNIT *pRval = (WORK_UNIT *) malloc(sizeof(WORK_UNIT));
if(!pRval)
{
return NULL;
}
pRval->pdA = pdA;
pRval->pdB = pdB;
pRval->dC = dC;
pRval->pData = pData;
pRval->nVal = nVal;
pRval->dX0 = dX0;
pRval->dXY = dXY;
pRval->lStart = lStart;
pRval->lEnd = lEnd;
pRval->dRval = 0.0;
pRval->lState = 0;
pRval->idThread = 0; // initially
if(!iThreadFlag) // direct call, useful for first work unit(after spawning others)
{
// fprintf(stderr, "TEMPORARY: call direct\n");
// fflush(stderr);
if(callback(pRval))
{
free(pRval);
return NULL;
}
return pRval; // so I can get the return info
}
// fprintf(stderr, "TEMPORARY: spawn thread\n");
// fflush(stderr);
if(pthread_create(&(pRval->idThread), NULL, callback, (void *)pRval))
{
if(pRval->idThread)
{
pthread_cancel(pRval->idThread);
pRval->idThread = 0;
}
free(pRval);
return NULL;
}
return pRval;
}
// determining # of cpus (workaround)
#define THREAD_COUNT 16
static unsigned long long MyGetTick(void)
{
static unsigned long long iMyTick = 0;
static unsigned long long lLastTickCount;
static int iFirstTime = 1;
struct timeval tv;
long long lTick;
gettimeofday(&tv, NULL);
lTick = (long long)tv.tv_sec * (long long)1000000 + (long long)tv.tv_usec;
if(iFirstTime)
{
lLastTickCount = lTick;
iMyTick = lTick;
iFirstTime = 0;
}
else
{
iMyTick += lTick - lLastTickCount; //TODO:improve this for wraparound?
lLastTickCount = lTick;
}
return (iMyTick);
}
static void *thread_proc(void *pNothing)
{
volatile long *pMe = pNothing ? (volatile long *)pNothing : (volatile long *)&(pMe);
volatile long long l1;
unsigned long long lStart;
int i1;
usleep(1000);
if(pNothing)
{
while(!*pMe)
{
usleep(100);
}
}
lStart = MyGetTick();
for(i1 = 0; i1 < 1000000; i1++)
{
l1 += *pMe;
}
if(pNothing)
{
*((long *)pNothing) = MyGetTick() - lStart;
return 0;
}
return (void *)(MyGetTick() - lStart);
}
int cpu_count0(void)
{
pthread_t thr[THREAD_COUNT] = {NULL};
static volatile long lResult[THREAD_COUNT] = {0};
long lResult0, lResultTTL;
int i1;
lResult0 = (long)thread_proc(0);
usleep(1000);
for(i1 = 0; i1 < THREAD_COUNT; i1++)
{
thr[i1] = NULL; //make sure
if(pthread_create(&(thr[i1]), NULL, thread_proc, (void *)&(lResult[i1])))
{
while(i1 >= 0)
{
if(thr[i1])
{
pthread_cancel(thr[i1]);
}
i1--;
}
return -2;
}
}
for(i1 = 0; i1 < THREAD_COUNT; i1++)
{
lResult[i1] = 1;
}
for(i1 = 0, lResultTTL = 0; i1 < THREAD_COUNT; i1++)
{
if(pthread_join(thr[i1], NULL))
{
fprintf(stderr, "thread canceled\n");
pthread_cancel(thr[i1]);
continue;
}
lResultTTL += lResult[i1];
}
// printf("%ld %ld\n", lResultTTL, lResult0);
lResultTTL += lResultTTL / 2; // for round-off, add 75 %
lResultTTL /= (THREAD_COUNT * lResult0);
// printf("Total CPU: %ld\n", lResultTTL);
return (int)lResultTTL;
}
static int my_int_cmp(const void *p1, const void *p2)
{
int i1 = *((const int *)p1) - *((const int *)p2);
return i1 > 0 ? 1 : (i1 < 0 ? -1 : 0);
}
int cpu_count(void)
{
int i1, i2[THREAD_COUNT];
for(i1 = 0; i1 < THREAD_COUNT; i1++)
{
i2[i1] = cpu_count0();
}
qsort(i2, sizeof(i2[0]), THREAD_COUNT, my_int_cmp);
return i2[THREAD_COUNT / 2];
}
/////////////////////////////////////////////////////////////////////////////
// FUNCTION: dFourier
//
// on entry 'aVal' is array of XY, 'nVal' is # of entries in aVal,
// nH is # harmonic (sin,cos) coefficients to generate [excluding '0']
// and 'dA' and 'dB' are the sin and cos arrays, and 'dC' is the 'C0' value.
// and 'nWU' is the # of 'work units' (using pthreads)
//
// note: list must be sorted by X value, no duplicate X values
//
/////////////////////////////////////////////////////////////////////////////
void *dFourier_work(void *pV)
{
WORK_UNIT *pW = (WORK_UNIT *) pV;
int i1, i2, i3;
double dX, dY, dX0, dXY;
XY *aVal;
int nVal;
double dRval, *dA, *dB;
if(!pV)
{
return NULL;
}
dRval = 0;
aVal = pW->pData;
nVal = pW->nVal;
dA = pW->pdA;
dB = pW->pdB;
dX0 = pW->dX0;
dXY = pW->dXY;
for(i1 = pW->lStart, i3 = pW->lEnd; i1 <= i3; i1++)
{
for(i2 = 0; i2 < nVal; i2++)
{
if(!i1)
{
dRval += aVal[i2].dY;
// printf("temporary: item %d X=%g\n",i2, (double)(dX0 + aVal[i2].dX * dXY));
}
else
{
double dS, dC, dXNew = i1 * (dX0 + aVal[i2].dX * dXY);
#ifdef HAS_SINCOS // GNU linux and when I do 'fast sin/cos'
sincos(dXNew, &dS, &dC); // NOTE: 'sincos' should be slightly faster than individual calls
#else // HAS_SINCOS
dS = sin(dXNew);
dC = cos(dXNew);
#endif // HAS_SINCOS
dA[i1 - 1] += aVal[i2].dY * dC;
dB[i1 - 1] += aVal[i2].dY * dS;
}
}
}
pW->dRval = dRval;
pW->lState = 1; // to say I 'm done
return 0;
}
void dFourier(XY * aVal, int nVal, int nH, double *dC, double *dA, double *dB, int nWU, int iAutoScale)
{
int i1, i2, iW;
double dX, dY, dX0, dXY;
WORK_UNIT *aW[THREAD_COUNT] = {0};
*dC = 0.0;
// fprintf(stderr, "call to dFourier, %d work units\n", nWU);
// fflush(stderr);
// usleep(10000);
// assume sorted list, calculate X0, XY for x = -PI to PI
if(!iAutoScale)
{
dXY = 1.0;
dX0 = 0.0;
}
else
{
dXY = 2.0 * _PI_ / (aVal[nVal - 1].dX + (aVal[nVal - 1].dX - aVal[0].dX) / (nVal - 1));
dX0 = -dXY * aVal[0].dX - _PI_; // derived from -_PI_ == dX0 + dXY * aVal[0].dX
}
if(nH < nWU)
{
nWU = nH;
}
if(nVal < 1024)
{
nWU = 1;
}
for(i1 = 0; i1 < nH; i1++)
{
dA[i1] = dB[i1] = 0.0; // zero this out
}
for(iW = 0, i1 = 0; iW < nWU; iW++)
{
i2 = (iW + 1) * nH / nWU; // next i1
if(i2 <= i1)
{
continue; // just in case
}
if(i2 >= nH || (iW == (nWU - 1)))
{
i2 = nH + 1; // to make sure I capture the last data point in the work unit
}
// fprintf(stderr, "temporary: work unit %d\n", iW);
// fflush(stderr);
aW[iW] = create_work_unit(dA, dB, 0.0, aVal, nVal, dX0, dXY, i1, i2 - 1,
dFourier_work, iW < (nWU - 1) ? 1 : 0);
i1 = i2; // "next"
if(i2 >= nH)
{
break; // safety(for now)
}
}
//now we must wait for all of the work units to complete
for(iW = 0; iW < nWU; iW++)
{
if(!aW[iW])
{
continue;
}
if(aW[iW]->idThread)
{
pthread_join(aW[iW]->idThread, NULL); // ignore any error for now
// pthread_detach(&(aW[iW].idThread);
*dC += aW[iW]->dRval; // returned C0 value(when applicable) adds into 'dC'
}
else // not a thread, keep 'dC' return here also
{
*dC += aW[iW]->dRval; // returned C0 value(when applicable) adds into 'dC'
}
free(aW[iW]);
aW[iW] = NULL; // by convention
}
// fix up arrays and whatnot
*dC /= nVal; // C0 must be half A[0] i.e Y = A[0] / 2 +[sum n = 1 - ?] An *cos(n * X) + Bn * sin(n * X)
// see http : //en.wikipedia.org / wiki / Fourier_series
for(i1 = 0; i1 < nH; i1++)
{
dA[i1] *= 2.0 / nVal;
dB[i1] *= 2.0 / nVal;
}
}
// FUNCTION:xy_comp - sort compare for 'XY' structure
int xy_comp(const void *p1, const void *p2)
{
int iRval = ((XY *) p1)->dX - ((XY *) p2)->dX;
if(iRval > 0)
{
return 1;
}
if(iRval < 0)
{
return -1;
}
return 0;
}
//FUNCTION:get_xy_data - file input of X and Y values(space delimiter)
MY_XY get_xy_data(FILE * pIn)
{
char tbuf[512];
double dX, dY;
MY_XY xyNULL = {NULL, 0, 0}, xy = {NULL, 0, 0};
while(fgets(tbuf, sizeof(tbuf), pIn))
{
if(!xy.pData ||
xy.nItems * sizeof(xy.pData[0]) >= xy.nSize)
{
if(xy.nItems > 1024)
{
xy.nSize = (xy.nItems * 2) * sizeof(xy.pData[0]);
}
else
{
xy.nSize = 2048 * sizeof(xy.pData[0]);
}
if(xy.pData)
{
void *p1 = realloc(xy.pData, xy.nSize + 1);
if(!p1)
{
free(xy.pData);
return xyNULL;
}
xy.pData = (XY *) p1;
}
else
{
xy.pData = (XY *) malloc(xy.nSize);
if(!xy.pData)
{
return xyNULL;
}
}
}
dX = dY = 0.0;
sscanf(tbuf, "%lg %lg\n", &dX, &dY);
// printf("TEMPORARY: data point %d %g %g %s\n", xy.nItems, dX, dY, tbuf);
xy.pData[xy.nItems].dX = dX;
xy.pData[xy.nItems].dY = dY;
xy.nItems++;
}
// sort data by X
qsort(xy.pData, xy.nItems, sizeof(xy.pData[0]), xy_comp);
return xy;
}
void usage(void)
{
fprintf(stderr,
" Copyright (c) 2013 by S.F.T. Inc. - All rights reserved\n"
" Use, copying, and distribution of this software may be licensed\n"
" using a GPLv2, GPLv3, MIT, or BSD license, as appropriate.\n"
"\n"
" OR - if you prefer - just use/distribute it without ANY license.\n"
" But I'd like some credit for it. A favorable mention is appreciated.\n"
"\n"
"USAGE: do_dft -h\n"
" do_dft [-a|-s m,n][-t nthrd][input_file [input_file [...]]]\n"
"where 'input_file' is the name of a file containing rows of sorted X and Y\n"
" values delimited by white-space and terminated with LF\n"
" and '-a' indicates 'auto scale X' to 0-2pi\n"
" and '-s' specifies a range of 'm to n'\n"
" and '-t' indicates how many threads you want to use\n"
" and '-h' instructs do_dft to print this information\n"
" (if no file or '-h' specified, input is 'stdin')\n");
}
// check_callback - offloads work for multiple threads to check data and calc std dev
void *check_callback(void *pV)
{
WORK_UNIT *pW = (WORK_UNIT *) pV;
int i1, i2, i3;
double dX, dY, dX0, dXY, dC;
XY *aVal;
int nHarm;
double dErr, *pdA, *pdB;
if(!pV)
{
return 0;
}
dErr = 0.0;
aVal = pW->pData;
nHarm = pW->nVal;
pdA = pW->pdA;
pdB = pW->pdB;
dC = pW->dC;
dX0 = pW->dX0;
dXY = pW->dXY;
for(i1 = pW->lStart; i1 < pW->lEnd; i1++)
{
double dCheck = dC;
for(i2 = 0; i2 < nHarm; i2++)
{
dCheck += pdA[i2] * cos((i2 + 1) * (aVal[i1].dX * dXY + dX0))
+ pdB[i2] * sin((i2 + 1) * (aVal[i1].dX * dXY + dX0));
}
// printf(" data point %d\t%g\t%g\t%g\n", i1, xy.pData[i1].dX, xy.pData[i1].dY, dCheck);
dErr += (dCheck - aVal[i1].dY) * (dCheck - aVal[i1].dY);
}
pW->dRval = dErr;
pW->lState = 1;
return 0;
}
////////////////
// MAIN
///////////////
int main(int argc, char *argv[])
{
double dC, dXY, dX0, *pdA = NULL, *pdB = NULL, dErr;
int i1, i2, iW;
FILE *pIn = stdin;
int nHarm, nThread = 0;
WORK_UNIT *aW[THREAD_COUNT];
double dScale1 = 0.0, dScale2 = 0.0;
int bDoScale = 0;
double dXFactor, dXOffset;
char tbuf[256];
while(argc > 1)
{
const char *p1 = argv[1];
char *p2;
if(*p1 != '-')
{
break;
}
p1++;
#if defined(TEST_FAST_SINCOS) && defined(USE_FAST_SINCOS)
#undef sin /* I need the original versions for this section */
#undef cos /* so I must un-define them, then re-define them */
#define SINCOS_ACCURACY 0.000001
if(*p1 == '!')
{
// this is a special switch that allows self-testing the fast sin/cos functions
double d1, dS, dC;
int i2 = 0;
unsigned long long llTick;
for(i1=0, d1=-2.0 * _PI_ - 0.1; d1 < 2.0 * _PI_ + 0.1; d1 += 0.00001, i1+=4)
{
fast_sincos(d1, &dS, &dC);
if(fabs(cos(d1) - dC) > SINCOS_ACCURACY)
{
fprintf(stderr, "cos(%0.3f) delta = %0.7f wIndex=%d y=%0.6f\n", d1,
fabs(cos(d1) - dC), wDebugIndex, dDebugY);
}
else
{
i2++;
}
if(fabs(sin(d1) - dS) > SINCOS_ACCURACY)
{
fprintf(stderr, "sin(%0.3f) delta = %0.7f wIndex=%d y=%0.6f\n", d1,
fabs(sin(d1) - dS), wDebugIndex, dDebugY);
}
else
{
i2++;
}
if(fabs(cos(d1) - fast_cos(d1)) > SINCOS_ACCURACY)
{
fprintf(stderr, "cos(%0.3f) delta = %0.7f wIndex=%d y=%0.6f\n", d1,
fabs(cos(d1) - fast_cos(d1)), wDebugIndex, dDebugY);
}
else
{
i2++;
}
if(fabs(sin(d1) - fast_sin(d1)) > SINCOS_ACCURACY)
{
fprintf(stderr, "sin(%0.3f) delta = %0.7f wIndex=%d y=%0.6f\n", d1,
fabs(sin(d1) - fast_sin(d1)), wDebugIndex, dDebugY);
}
else
{
i2++;
}
}
fprintf(stderr, "Tested %d values, %d in spec\n", i1, i2);
llTick = MyGetTick();
for(i1=0, d1=-2.0 * _PI_ - 0.1; d1 < 2.0 * _PI_ + 0.1; d1 += 0.00001, i1+=4)
{
fast_sincos(d1, &dS, &dC);
}
fprintf(stderr, "fast_sincos took %lld msecs\n", MyGetTick() - llTick);
llTick = MyGetTick();
for(i1=0, d1=-2.0 * _PI_ - 0.1; d1 < 2.0 * _PI_ + 0.1; d1 += 0.00001, i1+=4)
{
*((volatile double *)&dS) = sin(d1);
*((volatile double *)&dC) = cos(d1);
}
fprintf(stderr, "sin,cos took %lld msecs\n", MyGetTick() - llTick);
return 0;
}
#define sin fast_sin
#define cos fast_cos
#endif // USE_FAST_SINCOS, TEST_FAST_SINCOS
if(!*p1)
{
usage();
return -1;
}
while(*p1)
{
if(*p1 == 'h')
{
usage();
if(p1[1] || argc > 2)
{
return -1;
}
else
{
return 0;
}
}
else if(*p1 == 'a') // autoscale
{
bDoScale = -1;
}
else if(*p1 == 's') // set scale
{
bDoScale = 1;
p1++;
if(!*p1)
{
argc--;
argv++;
if(argc < 2)
{
usage();
return -1;
}
p1 = argv[1];
}
p2 = tbuf;
while(*p1 && *p1 != ',')
{
*(p2++) = *(p1++);
}
*p2 = 0;
dScale1 = atof(tbuf);
if(*p1 != ',' || !p1[1])
{
usage();
return -1;
}
p1++;
p2 = tbuf;
while(*p1 && *p1 != ',')
{
*(p2++) = *(p1++);
}
*p2 = 0;
dScale2 = atof(tbuf);
break; //the parsing stops here for this term
}
else if(*p1 == 't') // # of threads
{
p1++;
if(*p1)
{
nThread = atoi(p1);
}
else
{
argc--;
argv++;
if(argc < 2)
{
usage();
return -1;
}
nThread = atoi(argv[1]);
}
if(nThread <= 0)
{
usage();
return -2;
}
break; // the parsing stops here for this term
}
// TODO: other options, like cycle count maybe ?
else
{
usage();
return -2; // unknown option
}
p1++;
}
argc--; // in anticipation of other options, this is the loop counter for it
argv++;
}
if(!nThread)
{
nThread = cpu_count();
//fprintf(stderr, "TEMPORARY: %d threads\n");
}
while(argc > 1 || pIn == stdin)
{
MY_XY xy;
if(argc > 1)
{
pIn = fopen(argv[1], "r");
if(!pIn)
{
argv++;
argc--;
continue;
}
printf("FILE: %s\n", argv[1]);
argv++;
argc--;
}
xy = get_xy_data(pIn);
fclose(pIn);
pIn = NULL;
if(!xy.pData || !xy.nItems)
{
continue;
}
if(bDoScale > 0)
{
if(xy.pData[xy.nItems - 1].dX > xy.pData[0].dX)
{
//assume data is sorted
dXFactor = ((dScale2 - dScale1) // the delta scale(normally - pi to pi for autoscale)
/ (xy.pData[xy.nItems - 1].dX - xy.pData[0].dX)) // the delta X
* (double)(xy.nItems - 1)
/ (double)(xy.nItems); // last data point represents "not quite 2 * pi"
dXOffset = dScale1 - xy.pData[0].dX * dXFactor;
for(i1 = 0; i1 < xy.nItems; i1++)
{
xy.pData[i1].dX = xy.pData[i1].dX * dXFactor + dXOffset;
}
}
fprintf(stderr, "TEMPORARY: scaling factors: m=%g b=%g\n", dXFactor, dXOffset);
}
else if(bDoScale < 0)
{
fprintf(stderr, "TEMPORARY: autoscale -PI to PI\n");
}
nHarm = xy.nItems / 2 > MAX_HARMONIC
? MAX_HARMONIC : xy.nItems / 2;
if(xy.nItems < 2 || nHarm < 1)
{
continue;
}
pdA = (double *)malloc(sizeof(*pdA) * (nHarm + 1) * 2);
if(!pdA)
{
fprintf(stderr, "out of memory for work buffers\n");
return -3;
}
pdB = pdA + nHarm + 1;
dFourier(xy.pData, xy.nItems, nHarm, &dC, pdA, pdB, nThread, bDoScale < 0 ? 1 : 0);
printf("harm #\t magnitude\t phase (deg)\t offset (C0)=%g\n", dC);
for(i1 = 0; i1 < nHarm; i1++)
{
printf(" %3d\t"
// "%7g\t%7g\t"
"%15.6f\t%15.6f\n",
i1 + 1,
//dA[i1],
//dB[i1],
sqrt(pdA[i1] * pdA[i1] + pdB[i1] * pdB[i1]),
atan2(pdB[i1], pdA[i1]) * 180 / _PI_ + 180);
// NOTE: atan result for cosine will be - 180, sin - 90
// because the analysis is - PI to PI
// adding 180 will give you 0, 90
}
// figure out relative error (i.e. std deviation) and report it
// if() TODO - make this optional
// {
dXY = 2.0 * _PI_ / (xy.pData[xy.nItems - 1].dX + (xy.pData[xy.nItems - 1].dX - xy.pData[0].dX) / (xy.nItems - 1));
dX0 = -dXY * xy.pData[0].dX - _PI_; // derived from -_PI_ == dX0 + dXY * aVal[0].dX
for(i1 = 0, iW = 0; iW < nThread; iW++)
{
i2 = (iW + 1) * xy.nItems / nThread;
if(i2 > xy.nItems)
{
i2 = xy.nItems;
}
aW[iW] = create_work_unit(pdA, pdB, dC, xy.pData, nHarm, dX0, dXY,
i1, i2 - 1, check_callback, iW < (nThread - 1));
if(!aW[iW])
{
fprintf(stderr, "threading error on data check\n");
return -3;
}
i1 = i2; // next group
}
for(iW = 0, dErr = 0.0; iW < nThread; iW++)
{
if(!aW[iW])
{
continue;
}
if(aW[iW]->idThread)
{
pthread_join(aW[iW]->idThread, NULL); // ignore any error for now
//pthread_detach(&(aW[iW].idThread);
dErr += aW[iW]->dRval; // returned C0 value(when applicable) adds into 'dC'
}
else // not a thread, keep 'dC' return here also
{
dErr += aW[iW]->dRval; // returned C0 value(when applicable) adds into 'dC'
}
free(aW[iW]);
aW[iW] = NULL; // by convention
}