-
Notifications
You must be signed in to change notification settings - Fork 1
/
linARfilterPred.c
2762 lines (2405 loc) · 84.4 KB
/
linARfilterPred.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
/**
* @file linARfilterPred.c
* @brief linear auto-regressive predictive filter
*
* Implements Empirical Orthogonal Functions
*
*
*/
/* ================================================================== */
/* ================================================================== */
/* MODULE INFO */
/* ================================================================== */
/* ================================================================== */
// module default short name
// all CLI calls to this module functions will be <shortname>.<funcname>
// if set to "", then calls use <funcname>
#define MODULE_SHORTNAME_DEFAULT "larpf"
// Module short description
#define MODULE_DESCRIPTION "Linear auto-regressive predictive filters"
#include <assert.h>
#include <ctype.h>
#include <gsl/gsl_multifit.h>
#include <gsl/gsl_multimin.h>
#include <malloc.h>
#include <math.h>
#include <sched.h>
#include <semaphore.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fitsio.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_cblas.h>
#include <gsl/gsl_eigen.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_vector.h>
#include <time.h>
#include "CommandLineInterface/CLIcore.h"
#include "CommandLineInterface/timeutils.h"
#include "COREMOD_arith/COREMOD_arith.h"
#include "COREMOD_iofits/COREMOD_iofits.h"
#include "COREMOD_memory/COREMOD_memory.h"
#include "COREMOD_tools/COREMOD_tools.h"
#include "info/info.h"
#include "linopt_imtools/linopt_imtools.h"
#include "statistic/statistic.h"
#include "linARfilterPred/linARfilterPred.h"
#include "build_linPF.h"
#include "applyPF.h"
#ifdef HAVE_CUDA
#include "cudacomp/cudacomp.h"
#endif
/* ================================================================== */
/* ================================================================== */
/* INITIALIZE LIBRARY */
/* ================================================================== */
/* ================================================================== */
// Module initialization macro in CLIcore.h
// macro argument defines module name for bindings
//
INIT_MODULE_LIB(linARfilterPred)
/* ================================================================== */
/* ================================================================== */
/* COMMAND LINE INTERFACE (CLI) FUNCTIONS */
/* ================================================================== */
/* ================================================================== */
errno_t LINARFILTERPRED_LoadASCIIfiles_cli()
{
if(CLI_checkarg(1, 1) + CLI_checkarg(2, 1) + CLI_checkarg(3, 2) +
CLI_checkarg(4, 2) + CLI_checkarg(5, 5) ==
0)
{
LINARFILTERPRED_LoadASCIIfiles(data.cmdargtoken[1].val.numf,
data.cmdargtoken[2].val.numf,
data.cmdargtoken[3].val.numl,
data.cmdargtoken[4].val.numl,
data.cmdargtoken[5].val.string);
return CLICMD_SUCCESS;
}
else
{
return CLICMD_INVALID_ARG;
}
}
errno_t LINARFILTERPRED_SelectBlock_cli()
{
if(CLI_checkarg(1, 4) + CLI_checkarg(2, 4) + CLI_checkarg(3, 2) +
CLI_checkarg(4, 3) ==
0)
{
LINARFILTERPRED_SelectBlock(data.cmdargtoken[1].val.string,
data.cmdargtoken[2].val.string,
data.cmdargtoken[3].val.numl,
data.cmdargtoken[4].val.string);
return CLICMD_SUCCESS;
}
else
{
return CLICMD_INVALID_ARG;
}
}
errno_t linARfilterPred_repeat_shift_X_cli()
{
if(CLI_checkarg(1, 4) + CLI_checkarg(2, 2) + CLI_checkarg(3, 3) == 0)
{
linARfilterPred_repeat_shift_X(data.cmdargtoken[1].val.string,
data.cmdargtoken[2].val.numl,
data.cmdargtoken[3].val.string);
return CLICMD_SUCCESS;
}
else
{
return CLICMD_INVALID_ARG;
}
}
errno_t LINARFILTERPRED_Build_LinPredictor_cli()
{
if(CLI_checkarg(1, 4) + CLI_checkarg(2, 2) + CLI_checkarg(3, 1) +
CLI_checkarg(4, 1) + CLI_checkarg(5, 1) + CLI_checkarg(6, 3) +
CLI_checkarg(7, 2) + CLI_checkarg(8, 1) + CLI_checkarg(9, 2) ==
0)
{
LINARFILTERPRED_Build_LinPredictor(data.cmdargtoken[1].val.string,
data.cmdargtoken[2].val.numl,
data.cmdargtoken[3].val.numf,
data.cmdargtoken[4].val.numf,
data.cmdargtoken[5].val.numf,
data.cmdargtoken[6].val.string,
1,
data.cmdargtoken[7].val.numl,
data.cmdargtoken[8].val.numf,
data.cmdargtoken[9].val.numl);
return CLICMD_SUCCESS;
}
else
{
return CLICMD_INVALID_ARG;
}
}
errno_t LINARFILTERPRED_Apply_LinPredictor_cli()
{
if(CLI_checkarg(1, 4) + CLI_checkarg(2, 4) + CLI_checkarg(3, 1) +
CLI_checkarg(4, 3) ==
0)
{
LINARFILTERPRED_Apply_LinPredictor(data.cmdargtoken[1].val.string,
data.cmdargtoken[2].val.string,
data.cmdargtoken[3].val.numf,
data.cmdargtoken[4].val.string);
return CLICMD_SUCCESS;
}
else
{
return CLICMD_INVALID_ARG;
}
}
errno_t LINARFILTERPRED_ScanGain_cli()
{
if(CLI_checkarg(1, 4) + CLI_checkarg(2, 1) + CLI_checkarg(3, 1) == 0)
{
LINARFILTERPRED_ScanGain(data.cmdargtoken[1].val.string,
data.cmdargtoken[2].val.numf,
data.cmdargtoken[3].val.numf);
return CLICMD_SUCCESS;
}
else
{
return CLICMD_INVALID_ARG;
}
}
errno_t LINARFILTERPRED_PF_updatePFmatrix_cli()
{
if(CLI_checkarg(1, 4) + CLI_checkarg(2, 5) + CLI_checkarg(3, 1) == 0)
{
LINARFILTERPRED_PF_updatePFmatrix(data.cmdargtoken[1].val.string,
data.cmdargtoken[2].val.string,
data.cmdargtoken[3].val.numf);
return CLICMD_SUCCESS;
}
else
{
return CLICMD_INVALID_ARG;
}
}
errno_t LINARFILTERPRED_PF_RealTimeApply_cli()
{
if(CLI_checkarg(1, 4) + CLI_checkarg(2, 2) + CLI_checkarg(3, 2) +
CLI_checkarg(4, 4) + CLI_checkarg(5, 2) + CLI_checkarg(6, 5) +
CLI_checkarg(7, 2) + CLI_checkarg(8, 2) + CLI_checkarg(9, 2) +
CLI_checkarg(10, 2) + CLI_checkarg(11, 1) + CLI_checkarg(12, 2) ==
0)
{
LINARFILTERPRED_PF_RealTimeApply(data.cmdargtoken[1].val.string,
data.cmdargtoken[2].val.numl,
data.cmdargtoken[3].val.numl,
data.cmdargtoken[4].val.string,
data.cmdargtoken[5].val.numl,
data.cmdargtoken[6].val.string,
data.cmdargtoken[7].val.numl,
data.cmdargtoken[8].val.numl,
data.cmdargtoken[9].val.numl,
data.cmdargtoken[10].val.numl,
data.cmdargtoken[11].val.numf,
data.cmdargtoken[12].val.numl);
return CLICMD_SUCCESS;
}
else
{
return CLICMD_INVALID_ARG;
}
}
static errno_t init_module_CLI()
{
RegisterCLIcommand(
"pfloadascii",
__FILE__,
LINARFILTERPRED_LoadASCIIfiles_cli,
"load ascii files to PF input",
"<tstart> <dt> <NBpt> <NBfr> <output>",
"pfloadascii 200.0 0.001 10000 4 pfin",
"long LINARFILTERPRED_LoadASCIIfiles(double tstart, double dt, long "
"NBpt, long NBfr, const char *IDoutname)");
RegisterCLIcommand(
"mselblock",
__FILE__,
LINARFILTERPRED_SelectBlock_cli,
"select modes belonging to a block",
"<input mode values> <block map> <selected block> <output>",
"mselblock modevals blockmap 23 blk23modevals",
"long LINARFILTERPRED_SelectBlock(const char *IDin_name, const char "
"*IDblknb_name, long blkNB, "
"const char *IDout_name)");
RegisterCLIcommand("imrepshiftx",
__FILE__,
linARfilterPred_repeat_shift_X_cli,
"repeat and shift image, extend along X axis",
"<input image> <NBstep> <output image>",
"imrepshiftx imin 5 imout",
"long linARfilterPred_repeat_shift_X(const char "
"*IDin_name, long NBstep, const char *IDout_name)");
RegisterCLIcommand(
"mkARpfilt",
__FILE__,
LINARFILTERPRED_Build_LinPredictor_cli,
"Make linear auto-regressive filter",
"<input data> <PForder> <PFlag> <SVDeps> <regularization param> "
"<output filters> <LOOPmode> <LOOPgain> "
"<testmode>",
"mkARpfilt indata 5 2.4 0.0001 0.0 outPF 0 0.1 1",
"int LINARFILTERPRED_Build_LinPredictor(const char *IDin_name, long "
"PForder, float PFlag, double SVDeps, "
"double RegLambda, const char *IDoutPF, int outMode, int LOOPmode, "
"float LOOPgain, int testmode)");
/* strcpy(data.cmd[data.NBcmd].key,"applyPfiltRT");
strcpy(data.cmd[data.NBcmd].module,__FILE__);
data.cmd[data.NBcmd].fp = LINARFILTERPRED_Apply_LinPredictor_RT_cli;
strcpy(data.cmd[data.NBcmd].info,"Apply real-time linear predictive filter");
strcpy(data.cmd[data.NBcmd].syntax,"<input data> <predictor filter> <output>");
strcpy(data.cmd[data.NBcmd].example,"applyPfiltRT indata Pfilt outPF");
strcpy(data.cmd[data.NBcmd].Ccall,"long LINARFILTERPRED_Apply_LinPredictor_RT(const char *IDfilt_name, const char *IDin_name, const char *IDout_name)");
data.NBcmd++;
*/
RegisterCLIcommand("applyARpfilt",
__FILE__,
LINARFILTERPRED_Apply_LinPredictor_cli,
"Apply linear auto-regressive filter",
"<input data> <predictor> <PFlag> <prediction>",
"applyARpfilt indata Pfilt 2.4 outPF",
"long LINARFILTERPRED_Apply_LinPredictor(const char "
"*IDfilt_name, const char *IDin_name, float "
"PFlag, const char *IDout_name)");
RegisterCLIcommand(
"mscangain",
__FILE__,
LINARFILTERPRED_ScanGain_cli,
"scan gain",
"<input mode values> <multiplicative factor (leak)> <latency [frame]>",
"mscangain olwfsmeas 0.98 2.65",
"LINARFILTERPRED_ScanGain(char* IDin_name, float multfact, float "
"framelag)");
RegisterCLIcommand("linARPFMupdate",
__FILE__,
LINARFILTERPRED_PF_updatePFmatrix_cli,
"update predictive filter matrix",
"<input 3D predictor> <output 2D matrix> <update coeff>",
"linARPFMupdate outPF PFMat 0.1",
"long LINARFILTERPRED_PF_updatePFmatrix(const char "
"*IDPF_name, const char *IDPFM_name, float alpha)");
RegisterCLIcommand(
"linARapplyRT",
__FILE__,
LINARFILTERPRED_PF_RealTimeApply_cli,
"Real-time apply predictive filter",
"<input open loop coeffs stream> <offset index> <trigger semaphore "
"index> <2D predictive "
"matrix> <filter order> <output stream> <nbGPU> <loop> <NBiter> "
"<savemode> <timelag> <PFindex>",
"linARapplyRT modevalOL 0 2 PFmat 5 outPFmodeval 0 0 0 0 1.8 0",
"long LINARFILTERPRED_PF_RealTimeApply(const char *IDmodevalOL_name, "
"long IndexOffset, int "
"semtrig, const char *IDPFM_name, long NBPFstep, const char "
"*IDPFout_name, int nbGPU, long "
"loop, long NBiter, int SAVEMODE, float tlag, long PFindex)");
CLIADDCMD_LinARfilterPred__build_linPF();
CLIADDCMD_LinARfilterPred__applyPF();
// add atexit functions here
return RETURN_SUCCESS;
}
/* =============================================================================================== */
/* =============================================================================================== */
/* */
/* 1. INITIALIZATION */
/* */
/* =============================================================================================== */
/* =============================================================================================== */
/* =============================================================================================== */
/* =============================================================================================== */
/* */
/* 2. I/O TOOLS */
/* */
/* =============================================================================================== */
/* =============================================================================================== */
int NBwords(const char sentence[])
{
int counted = 0; // result
// state:
const char *it = sentence;
int inword = 0;
do
switch(*it)
{
case '\0':
case ' ':
case '\t':
case '\n':
case '\r':
if(inword)
{
inword = 0;
counted++;
}
break;
default:
inword = 1;
}
while(*it++);
return counted;
}
/**
* @brief load ascii file(s) into image cube
*
* resamples sequence(s) of data points
* INPUT FILES HAVE TO BE NAMED seq000.dat, seq001.dat etc...
*
* file starts at tstart, sampling = dt
* NBpt per file
* NBfr files
*/
long LINARFILTERPRED_LoadASCIIfiles(
double tstart, double dt, long NBpt, long NBfr, const char *IDoutname)
{
FILE *fp;
long NBfiles;
double runtime;
char fname[200];
struct stat fstat;
int fOK;
long NBvarin[200];
long fcnt;
FILE *fparray[200];
long kk;
size_t linesiz = 0;
char *linebuf = 0;
//ssize_t linelen=0;
//int ret;
long vcnt;
double ftime0[200];
double var0[200][200];
double ftime1[200];
double var1[200][200];
double varC[200][200];
float alpha;
long nbvar;
long fr;
char imoutname[200];
FILE *fpout;
imageID IDout[200];
//int HPfilt = 1; // high pass filter
float HPgain = 0.005;
long ii;
long kkpt, kkfr;
runtime = tstart;
fOK = 1;
NBfiles = 0;
nbvar = 0;
while(fOK == 1)
{
sprintf(fname, "seq%03ld.dat", NBfiles);
if(stat(fname, &fstat) == 0)
{
printf("Found file %s\n", fname);
fflush(stdout);
fp = fopen(fname, "r");
//linelen =
if(getline(&linebuf, &linesiz, fp) == -1)
{
PRINT_ERROR("getline error");
}
fclose(fp);
NBvarin[NBfiles] = NBwords(linebuf) - 1;
free(linebuf);
linebuf = NULL;
printf(" NB variables = %ld\n", NBvarin[NBfiles]);
nbvar += NBvarin[NBfiles];
NBfiles++;
}
else
{
printf("No more files\n");
fflush(stdout);
fOK = 0;
}
}
printf("NBfiles = %ld\n", NBfiles);
for(fcnt = 0; fcnt < NBfiles; fcnt++)
{
sprintf(fname, "seq%03ld.dat", fcnt);
printf(" %03ld OPENING FILE %s\n", fcnt, fname);
fflush(stdout);
fparray[fcnt] = fopen(fname, "r");
}
kk = 0; // time
runtime = tstart;
for(fcnt = 0; fcnt < NBfiles; fcnt++)
{
if(fscanf(fparray[fcnt], "%lf", &ftime0[fcnt]) != 1)
{
PRINT_ERROR("fscanf error");
}
for(vcnt = 0; vcnt < NBvarin[fcnt]; vcnt++)
{
if(fscanf(fparray[fcnt], "%lf", &var0[fcnt][vcnt]) != 1)
{
PRINT_ERROR("fscanf error");
}
}
if(fscanf(fparray[fcnt], "\n") != 0)
{
PRINT_ERROR("fscanf error");
}
if(fscanf(fparray[fcnt], "%lf", &ftime1[fcnt]) != 1)
{
PRINT_ERROR("fscanf error");
}
for(vcnt = 0; vcnt < NBvarin[fcnt]; vcnt++)
{
if(fscanf(fparray[fcnt], "%lf", &var1[fcnt][vcnt]) != 1)
{
PRINT_ERROR("fscanf error");
}
}
if(fscanf(fparray[fcnt], "\n") != 0)
{
PRINT_ERROR("fscanf error");
}
printf("FILE %ld : \n", fcnt);
printf(" time : %20f %20f\n", ftime0[fcnt], ftime1[fcnt]);
fflush(stdout);
for(vcnt = 0; vcnt < NBvarin[fcnt]; vcnt++)
{
printf(" variable %3ld : %20f %20f\n",
vcnt,
var0[fcnt][vcnt],
var1[fcnt][vcnt]);
varC[fcnt][vcnt] = var0[fcnt][vcnt];
}
printf("\n");
}
for(fr = 0; fr < NBfr; fr++)
{
sprintf(imoutname, "%s_%03ld", IDoutname, fr);
create_3Dimage_ID(imoutname, nbvar, 1, NBpt, &(IDout[fr]));
}
fpout = fopen("out.txt", "w");
kk = 0;
kkpt = 0;
kkfr = 0;
while(kkfr < NBfr)
{
fprintf(fpout, "%20f", runtime);
ii = 0;
for(fcnt = 0; fcnt < NBfiles; fcnt++)
{
while(ftime1[fcnt] < runtime)
{
ftime0[fcnt] = ftime1[fcnt];
for(vcnt = 0; vcnt < NBvarin[fcnt]; vcnt++)
{
var0[fcnt][vcnt] = var1[fcnt][vcnt];
}
if(fscanf(fparray[fcnt], "%lf", &ftime1[fcnt]) != 1)
{
PRINT_ERROR("fscanf error");
}
for(vcnt = 0; vcnt < NBvarin[fcnt]; vcnt++)
{
if(fscanf(fparray[fcnt], "%lf", &var1[fcnt][vcnt]) != 1)
{
PRINT_ERROR("fscanf error");
}
}
if(fscanf(fparray[fcnt], "\n") != 0)
{
PRINT_ERROR("fscanf error");
}
}
if(kk == 0)
for(vcnt = 0; vcnt < NBvarin[fcnt]; vcnt++)
{
varC[fcnt][vcnt] = var0[fcnt][vcnt];
}
alpha = (runtime - ftime0[fcnt]) / (ftime1[fcnt] - ftime0[fcnt]);
for(vcnt = 0; vcnt < NBvarin[fcnt]; vcnt++)
{
fprintf(fpout,
" %20f",
(1.0 - alpha) * var0[fcnt][vcnt] +
alpha * var1[fcnt][vcnt] - varC[fcnt][vcnt]);
varC[fcnt][vcnt] = (1.0 - HPgain) * varC[fcnt][vcnt] +
HPgain * ((1.0 - alpha) * var0[fcnt][vcnt] +
alpha * var1[fcnt][vcnt]);
data.image[IDout[kkfr]].array.F[kkpt * nbvar + ii] =
(1.0 - alpha) * var0[fcnt][vcnt] +
alpha * var1[fcnt][vcnt] - varC[fcnt][vcnt];
ii++;
}
}
fprintf(fpout, "\n");
kk++;
kkpt++;
runtime += dt;
if(kkpt == NBpt)
{
kkpt = 0;
kkfr++;
}
}
fclose(fpout);
for(fcnt = 0; fcnt < NBfiles; fcnt++)
{
fclose(fparray[fcnt]);
}
return (NBfiles);
}
// select block on first dimension
imageID LINARFILTERPRED_SelectBlock(const char *IDin_name,
const char *IDblknb_name,
long blkNB,
const char *IDout_name)
{
imageID IDin;
imageID IDblknb;
uint8_t naxis;
long m;
long NBmodes1;
uint32_t *sizearray;
uint32_t xsize, ysize, zsize;
unsigned long cnt;
imageID IDout;
//char imname[200];
long mmax;
printf("Selecting block %ld ...\n", blkNB);
fflush(stdout);
IDin = image_ID(IDin_name);
IDblknb = image_ID(IDblknb_name);
naxis = data.image[IDin].md[0].naxis;
mmax = data.image[IDblknb].md[0].size[0];
if(data.image[IDin].md[0].size[0] != data.image[IDblknb].md[0].size[0])
{
printf(
"WARNING: block index file and telemetry have different sizes\n");
fflush(stdout);
mmax = data.image[IDin].md[0].size[0];
if(data.image[IDblknb].md[0].size[0] < mmax)
{
mmax = data.image[IDblknb].md[0].size[0];
}
}
NBmodes1 = 0;
for(m = 0; m < mmax; m++)
{
if(data.image[IDblknb].array.UI16[m] == blkNB)
{
NBmodes1++;
}
}
sizearray = (uint32_t *) malloc(sizeof(uint32_t) * naxis);
if(sizearray == NULL)
{
PRINT_ERROR("malloc returns NULL pointer");
abort();
}
for(uint8_t axis = 0; axis < naxis; axis++)
{
sizearray[axis] = data.image[IDin].md[0].size[axis];
}
sizearray[0] = NBmodes1;
create_image_ID(IDout_name,
naxis,
sizearray,
_DATATYPE_FLOAT,
0,
0,
0,
&IDout);
xsize = data.image[IDin].md[0].size[0];
if(naxis > 1)
{
ysize = data.image[IDin].md[0].size[1];
}
else
{
ysize = 1;
}
if(naxis > 2)
{
zsize = data.image[IDin].md[0].size[2];
}
else
{
zsize = 1;
}
cnt = 0;
for(uint32_t jj = 0; jj < ysize; jj++)
for(uint32_t kk = 0; kk < zsize; kk++)
for(uint32_t ii = 0; ii < mmax; ii++)
if(data.image[IDblknb].array.UI16[ii] == blkNB)
{
//printf("%ld / %ld cnt = %8ld / %ld\n", ii, xsize, cnt, NBmodes1*ysize*zsize);
//fflush(stdout);
data.image[IDout].array.F[cnt] =
data.image[IDin]
.array.F[kk * xsize * ysize + jj * ysize + ii];
cnt++;
}
free(sizearray);
return (IDout);
}
/* =============================================================================================== */
/* =============================================================================================== */
/* */
/* 3. BUILD PREDICTIVE FILTER */
/* */
/* =============================================================================================== */
/* =============================================================================================== */
/** @brief Expand 2D image/matrix in X direction by repeat and shift
*
*/
imageID linARfilterPred_repeat_shift_X(const char *IDin_name,
long NBstep,
const char *IDout_name)
{
imageID IDin;
uint32_t xsize, ysize;
imageID IDout;
uint32_t xsizeout, ysizeout;
uint32_t *imsizeout;
IDin = image_ID(IDin_name);
xsize = data.image[IDin].md[0].size[0];
ysize = data.image[IDin].md[0].size[1];
xsizeout = xsize * NBstep;
ysizeout = ysize - NBstep;
imsizeout = (uint32_t *) malloc(sizeof(uint32_t) * 2);
if(imsizeout == NULL)
{
PRINT_ERROR("malloc returns NULL pointer");
abort();
}
imsizeout[0] = xsizeout;
imsizeout[1] = ysizeout;
create_image_ID(IDout_name, 2, imsizeout, _DATATYPE_FLOAT, 1, 0, 0, &IDout);
free(imsizeout);
long step;
for(step = 0; step < NBstep; step++)
{
for(uint32_t ii = 0; ii < xsize; ii++)
{
for(uint32_t jjout = 0; jjout < ysize - NBstep; jjout++)
{
data.image[IDout]
.array.F[jjout * xsizeout + step * xsize + ii] =
data.image[IDin]
.array.F[(jjout + NBstep - step - 1) * xsize + ii];
}
}
}
return IDout;
}
/** ## Purpose
*
* Build predictive filter from real-time AO telemetry
*
*
* ## Masking
*
* Optional input and output pixel masks select active input & output
*
*
* ## Loop mode
*
* If LOOPmode = 1, operate in a loop, and re-run filter computation everytime IDin_name changes
*
*
* ## Input parameters: dynamic mode
*
* if <IFoutPF_name>_PFparam image exist, read parameters from it: PFlag, SVDeps, RegLambda, LOOPgain
* create it in shared memory by default
*
*
* @return If testmode=2, write 3D output filter
* @return output filter image indentifier
*
*/
imageID LINARFILTERPRED_Build_LinPredictor(const char *IDin_name,
long PForder,
float PFlag,
double SVDeps,
double RegLambda,
const char *IDoutPF_name,
__attribute__((unused)) int outMode,
int LOOPmode,
float LOOPgain,
int testmode)
{
/// ---
/// # Code Description
imageID IDin;
imageID IDmatA;
//imageID IDout;
imageID IDinmask;
imageID IDoutmask;
long nbspl; // Number of samples
long NBpixin, NBpixout;
long NBmvec, NBmvec1;
long mvecsize;
long xsize, ysize;
long *pixarray_x;
long *pixarray_y;
long *pixarray_xy;
long *outpixarray_x;
long *outpixarray_y;
long *outpixarray_xy;
double *ave_inarray;
int REG = 0; // 1 if regularization
long m, pix, k0, dt;
int Save = 0;
long xysize;
long IDmatC;
//int use_magma = 1; // use MAGMA library if available
//int magmacomp = 0;
//imageID IDfiltC;
// float *valfarray;
float alpha;
long PFpix;
//char filtname[200];
//char filtfname[200];
//imageID ID_Pfilt;
float val, val0;
long ind1;
imageID IDoutPF2D; // averaged with previous filters
imageID IDoutPF2Draw; // individual filter
char IDoutPF_name_raw[200];
// long IDoutPF3D;
// char IDoutPF_name3D[500];
long NB_SVD_Modes;
int DC_MODE = 0; // 1 if average value of each mode is removed
long NBiter, iter;
long semtrig = 2;
uint32_t *imsizearray;
//char fname[200];
//time_t t;
//struct tm *uttime;
//struct timespec timenow;
struct timespec t0;
struct timespec t1;
struct timespec t2;
struct timespec tdiff;
double tdiffv01; // waiting time
double tdiffv12; // computing time
imageID IDPFparam; // parameters in shared memory (optional)
char imname[200];
int ExternalPFparam;
float PFlag_run;
float SVDeps_run;
float RegLambda_run;
float LOOPgain_run;
float gain;
uint32_t *imsize;
long IDincp;
long inNBelem;
list_variable_ID();
int PSINV_MODE = 0;
long IDv;
if((IDv = variable_ID("_SVD_PSINV")) != -1)
{
PSINV_MODE = (int)(data.variable[IDv].value.f + 0.1);
printf("PSINV_MODE = %d\n", PSINV_MODE);
}
float PSINV_s = 1.0e-6;
if((IDv = variable_ID("_SVD_s")) != -1)
{
PSINV_s = data.variable[IDv].value.f;
printf("PSINV_s = %f\n", PSINV_s);
}
float PSINV_tol = 1.0;
if((IDv = variable_ID("_SVD_tol")) != -1)
{
PSINV_tol = data.variable[IDv].value.f;
printf("PSINV_tol = %f\n", PSINV_tol);
}
/// ## Reading Parameters from Image
/// If image named <IDoutPF_name>_PFparam exists, the predictive filter
/// parameters are read from it instead of the function arguments. \n
/// This mode is particularly useful in LOOP mode if the user needs
/// to change the parameters between LOOP iterations.\n
sprintf(imname, "%s_PFparam", IDoutPF_name);
imsize = (uint32_t *) malloc(sizeof(uint32_t) * 2);
if(imsize == NULL)
{
PRINT_ERROR("malloc returns NULL pointer");
abort();
}
imsize[0] = 4;
imsize[1] = 1;
create_image_ID(imname, 2, imsize, _DATATYPE_FLOAT, 1, 0, 0, &IDPFparam);
free(imsize);
if((IDPFparam = image_ID(imname)) != -1)
{
ExternalPFparam = 1;
data.image[IDPFparam].array.F[0] = PFlag;
data.image[IDPFparam].array.F[1] = SVDeps;
data.image[IDPFparam].array.F[2] = RegLambda;
data.image[IDPFparam].array.F[3] = LOOPgain;
}
else
{
ExternalPFparam = 0;
}
LOOPgain_run = LOOPgain;
if(LOOPmode == 0)
{
LOOPgain_run = 1.0;
NBiter = 1;
}
else
{
NBiter = 100000000;
}
//sprintf(IDoutPF_name3D, "%s_3D", IDoutPF_name);
/// ## Selecting input values
/// The goal of this function is to build a linear link between
/// input and output variables. \n
/// Input variables values are provided by the input telemetry image
/// which is first read to measure dimensions, and allocate memory.\n
/// Note that an optional variable selection step allows only a
/// subset of the telemetry variables to be considered.
/// ### Read input telemetry image IDin_name to measure xsize, ysize and number of samples