-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp.c
2711 lines (2281 loc) · 67.8 KB
/
mp.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
/* Nowa implemementacja algorytmu MP dla slownikow zrandominizowanych
1997 03 21/22 04 02/05 06 18; 1997 08 01 1997 08 12/15/22/26/27
Wsparcie heurystyczne + nowa implementacja wyznaczanie iloczynow
skalarnych (bywa niestabilna)
1997 10 04 - poprawka do slownika Mallata
1997 11 22 - zmiana metody przeszukiwania slownika
1998 05 16 - poprawna interpretacja wyjatku w funkcji atan2 dla
kompilatora Borland C/C++ 4.52,+ poprawki do metody heurystycznej
1998 06 05, 06 22 - wsparcie dla wielowatkowosci (experymentalnie)
1998 07 16/18 - pelna implementacja wielowatkowosci
2000 01 06/08/10 - adaptacyjne dobieranie slowka
(koniec rozwoju tej wersji oprogramowania)
Definicja:
WINDOWSGNU - dla kompilatora Cygnus GNU C
MULTITHREAD - kompilacja w trybie wielowatkowym
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "iobook.h"
#include "rand2d.h"
#include "formulc.h"
#include "proto.h"
#include "shell.h"
#define ROCTAVE 2 /* Zakres przszukiwania oktaw */
#define RFREQ 40 /* ---//-----//------- czestotliwosci */
#define RPOSITION 40 /* ---//-----//------- polorzen*/
#define STARTDICTION 25000 /* Rozmiar slownika wstepnego */
#define MAXSCALE 4 /* Korekcja niestabilnosci (na razie) */
#define DSQR(X) ((double)(X)*(double)(X))
#define EPSYLON 1.0e-8 /* Dokladnosc wyznaczania wartosci iloczynow skalarnych */
#define SQR(X) ((X)*(X))
#define DIRAK 0 /* Typy atomow (identyfikacja) */
#define FOURIER 1
#define GABOR 3
#define ON 1
#define OFF 0
#define STRING 256
#define MAXOPTIONS 32
typedef float FLOAT; /* Dla oszczednosci pamieci */
typedef unsigned short USHORT;
#ifdef __GNUC__
__inline__
#endif
double TSQR(double x) { return x*x; }
typedef struct
{
char typ; /* Rodzaj atomu */
int scale; /* skala atomu */
int trans; /* translacja */
int iScale; /* Wskaznik skali */
int iFreq; /* Wskaznik czestotliwosci */
double frequency; /* czestotliwosc */
double modulus; /* wspolczynnik rozwiniecia */
double phase; /* faza atomu */
double amplitude; /* amplituda sygnalu */
double chirpfactor;
} BIGATOM;
typedef struct
{
FLOAT CNorm; /* Norma kosinusowa */
FLOAT SNorm; /* Norma sinusowa */
FLOAT Fc; /* Iloczyn kosinusowy z sygnalem */
FLOAT Fs; /* Iloczyn sinusowy z sygnalem */
FLOAT Modulus; /* Ostatina wartosc modulusa (dla przyspiszenia obliczen) */
FLOAT Amplitude; /* Ostatina wartosc amplitudy */
FLOAT Phase; /* ---//-----//---- fazy */
UCHAR mode;
} INFOATOM;
/* Szybka metoda genracji iloczynow skalarnych */
/* dla slownikow diadycznych */
typedef struct { /* Tablica wartosci komponentow czastkowych */
FLOAT ConstCos,
ConstK;
} HASHVAL;
static int **ConvTable,MaxIndex; /* Struktury komponentow */
static HASHVAL **HashTable;
static double ConstFactor;
static FLOAT **HashCosTable;
static USHORT *Scale,*Translate,*Frequency; /* Parametry slownika */
int StartDictionSize=STARTDICTION,Heuristic=ON,
ROctave=ROCTAVE,RFreqency=RFREQ,RPosition=RPOSITION; /* Parametry heurystyki */
int FindChirp = ON;
static double **OldAtomsTable; /* Tablica maksymalnych atomow dla heurystyki */
static int MakeFastMPDictionary(int);
static void CloseFastMPDictionary(void);
static int GetMaxScale(int);
#ifdef LINUX /* Funkcje rozwijalne w mjejscu wywolania */
/* Written by John C. Bowman <[email protected]> */
__inline double InlineSqrt(double __x)
{
register double __value;
__asm__ __volatile__
("fsqrt" : "=t" (__value): "0" (__x));
return __value;
}
__inline double InlineExp(double __x)
{
register double __value, __exponent;
__asm__ __volatile__
("fldl2e # e^x = 2^(x * log2(e))\n\t"
"fmul %%st(1) # x * log2(e)\n\t"
"fstl %%st(1)\n\t"
"frndint # int(x * log2(e))\n\t"
"fxch\n\t"
"fsub %%st(1) # fract(x * log2(e))\n\t"
"f2xm1 # 2^(fract(x * log2(e))) - 1\n\t"
: "=t" (__value), "=u" (__exponent) : "0" (__x));
__value += 1.0;
__asm__ __volatile__
("fscale" : "=t" (__value): "0" (__value), "u" (__exponent));
return __value;
}
__inline double InlineAtan2(double __y, double __x)
{
register double __value;
__asm__ __volatile__
("fpatan\n\t"
"fldl %%st(0)"
: "=t" (__value): "0" (__x), "u" (__y));
return __value;
}
__inline double InlineSin(double __x)
{
register double __value;
__asm__ __volatile__
("fsin" : "=t" (__value): "0" (__x));
return __value;
}
__inline double InlineCos(double __x)
{
register double __value;
__asm__ __volatile__
("fcos" : "=t" (__value): "0" (__x));
return __value;
}
__inline double InlineLog(double __x)
{
register double __value;
__asm__ __volatile__
("fldln2\n\t"
"fxch\n\t"
"fyl2x"
: "=t" (__value) : "0" (__x));
return __value;
}
__inline double InlineFloor(double __x)
{
register double __value;
volatile short __cw, __cwtmp;
__asm__ volatile ("fnstcw %0" : "=m" (__cw) : );
__cwtmp = (__cw & 0xf3ff) | 0x0400; /* rounding down */
__asm__ volatile ("fldcw %0" : : "m" (__cwtmp));
__asm__ volatile ("frndint" : "=t" (__value) : "0" (__x));
__asm__ volatile ("fldcw %0" : : "m" (__cw));
return __value;
}
__inline void sincos(double __x, double *__sinx, double *__cosx)
{
register double __cosr,__sinr;
__asm__ __volatile__
("fsincos": "=t" (__cosr), "=u" (__sinr) : "0" (__x));
*__sinx=__sinr; *__cosx=__cosr;
}
#define exp(X) InlineExp(X)
#define atan2(X,Y) InlineAtan2((X),(Y))
#define sin(X) InlineSin(X)
#define cos(X) InlineCos(X)
#define sqrt(X) InlineSqrt(X)
#define log(X) InlineLog(X)
#define floor(X) InlineFloor(X)
#else
#define sincos(x,Sin,Cos) { *(Sin)=sin(x); *(Cos)=cos(x); }
#ifdef PGI
#define abs(x) __builtin_abs(x)
#define atan2(x,y) __builtin_atan2(x,y)
#define atan(x) __builtin_atan(x)
#define tan(x) __builtin_tan(x)
#define cos(x) __builtin_cos(x)
#define sin(x) __builtin_sin(x)
#define fabs(x) __builtin_fabs(x)
#define sqrt(x) __builtin_sqrt(x)
#define log(x) __builtin_log(x)
#define log10(x) __builtin_log10(x)
#define exp(x) __builtin_exp(x)
#define pow(x,y) __builtin_pow(x,y)
#endif
#endif
int DictionSize,RandomType=NOFUNCRND,MaxScale,
MallatDiction=OFF,OverSampling=2;
float AdaptiveConst=0.9F;
static int NWD(register int a,register int b) /* Najwiekszy wspolny dzielnik */
{
while(a && b) if(a>b) a%=b; else b%=a;
return ((!a) ? b : a);
}
int MakeMallatDictionary(int n,int OverSamp,int load)
{
const int Halfen=(n >> 1)-1;
const int LastPos=n-1;
register int i=0,p1,p3,j,k,skok,Delta,maxT,t;
int itmp,s,Div;
p1=GetMaxScale(n); Div=1 << OverSamp;
for(j=0 ; j<p1 ; j++) if(j!=0)
{
s=1 << j; skok=(s*Div)/(itmp=NWD(Div,s)); p3=s*(1 << (j+OverSamp));
Delta=s/itmp; maxT=s*(n >> j);
for(k=0 ; k<p3 ; k+=skok) if(k!=0)
for(t=0 ; t<maxT ; t+=Delta)
{
if(load==ON)
{
Scale[i]=(USHORT)j;
Frequency[i]=(USHORT)(((double)k*Halfen)/p3);
Translate[i]=(USHORT)(((double)LastPos*t)/maxT);
}
i++;
}
}
return i;
}
void InicRandom1D(void)
{
if(DiadicStructure==ON) /* Dla slownika diadycznego */
{
MaxScale=GetMaxScale(DimBase);
if(InicRand1D(OctaveDyst,MaxScale-1)==-1)
{
fprintf(stderr,"Problems with scales generator !\n");
exit(EXIT_FAILURE);
}
}
else if(InicRand1D(OctaveDyst,DimBase-2)==-1)
{
fprintf(stderr,"Problems with scales generator !\n");
exit(EXIT_FAILURE);
}
}
int InicRandom(int Srand)
{
InicRandom1D();
switch(RandomType)
{ /* Generatory funkcji 2D p-stwa */
case FUNCRND:
if(CompileFunction()==-1)
{
fprintf(stderr,"Incorrect form of probability density (CompileFunction) !\n");
exit(EXIT_FAILURE);
}
if(prn==ON)
fprintf(stdout,"<<< INITIALIZING GENERATOR >>>\n");
return InicGen2D(fval,DimBase,DimBase/2,Srand);
case NOFUNCRND:
default:
if(Srand==OFF)
SRAND(0U); /* Dla powtarzlnosci generacji slownika */
else SRAND((unsigned short)time(NULL)); /* Inicjacja losowa */
break;
}
return 0;
}
void RandTimeAndFreq(int *Time,int *Freq,int RandOpc)
{ /* Generacja pozycji atomu w przestrzeni czas-czestosc */
switch(RandOpc) { /* Dla slownikow w pelni zrandomizowanych */
case FUNCRND:
(void)Rand2D(Time,Freq);
return;
case NOFUNCRND:
default:
*Time=RANDOM(DimBase);
*Freq=1+RANDOM((DimBase >> 1)-1);
break;
}
}
#ifdef __GNUC__
__inline
#else
static
#endif
void LoadBigAtom(BIGATOM *atom,const int DimBase,const int k)
{
const double pi2=2.0*M_PI; /* Ladowanie atomu */
if(DiadicStructure==OFF)
atom->scale=(int)Scale[k];
else
{
atom->iScale=(int)Scale[k];
atom->scale=1U << atom->iScale;
}
atom->trans=Translate[k];
atom->iFreq=Frequency[k];
atom->frequency=pi2*(double)(atom->iFreq)/(double)DimBase;
}
static void CloseDictionary(void)
{
if(Scale!=NULL)
free((void *)Scale);
if(Translate!=NULL)
free((void *)Translate);
if(Frequency!=NULL)
free((void *)Frequency);
}
#define SWAP(X,Y) tmp=(X); (X)=(Y); (Y)=tmp
static int MakeDictionary(void)
{
unsigned int Size;
int i,Time,Freq;
USHORT OldSeed,GetSeed(void); /* Tworzenie stablicowanych */
/* parametrow atomow */
if(prn==ON)
fprintf(stdout,"<<< GENERATING THE DICTIONARY >>>\n");
if(MallatDiction==ON) /* Zmiana wielkosci globalnej */
DictionSize=MakeMallatDictionary(DimBase,OverSampling,OFF);
Size=DictionSize*sizeof(USHORT);
if((Scale=(USHORT *)malloc(Size))==NULL ||
(Translate=(USHORT *)malloc(Size))==NULL ||
(Frequency=(USHORT *)malloc(Size))==NULL)
{
CloseDictionary();
return -1;
}
if(MallatDiction==ON)
{
if(prn==ON)
fprintf(stdout,"<<< DYADIC DICTIONARY %d ATOMS >>>\n",
DictionSize+(3*DimBase)/2);
(void)MakeMallatDictionary(DimBase,OverSampling,ON);
if(Heuristic==ON)
{
if(prn==ON)
fprintf(stdout,"<<< MIXING THE DICTIONARY >>>\n");
/* Na potrzeby heurystyki mieszamy */
for(i=0 ; i<DictionSize ; i++) /* atomy */
{
register USHORT tmp;
const int k=(int)((double)rand()*DictionSize/(double)RAND_MAX);
SWAP(Scale[k],Scale[i]);
SWAP(Translate[k],Translate[i]);
SWAP(Frequency[k],Frequency[i]);
}
}
return 0;
}
OldSeed=GetSeed(); /* Slownik stochastyczny */
for(i=0 ; i<DictionSize ; i++)
{
int itmp;
RandTimeAndFreq(&Time,&Freq,RandomType);
Rand1D(&itmp);
Scale[i]=(USHORT)(1+itmp);
Translate[i]=(USHORT)Time;
Frequency[i]=(USHORT)Freq;
}
SRAND(OldSeed);
return 0;
}
#undef SWAP
void ShowDictionary(char *opt) /* Drukowanie struktury slownika */
{
ULONG *TransHist,*ScaleHist=NULL,*FreqHist;
char *argv[MAXOPTIONS],filename[STRING],raportname[STRING],
napis[STRING]="dist";
const float df=0.5F*(float)(DimBase-1)/M_PI, /* Konwersja czestotliwosci */
FreqConv=((SamplingRate<=0.0F) ? 1.0F : 0.5F*SamplingRate/M_PI);
const unsigned Size=(DimBase+1U)*sizeof(ULONG);
int i,argc,opcja,gendat=OFF,genrap=ON;
float *tmptab,MaxTrans,MaxScale,MaxFreq;
FILE *stream=NULL;
BIGATOM atom;
StrToArgv(opt,argv,&argc); /* Konwersja na postac argv */
opterr=optind=0; sp=1;
while((opcja=Getopt(argc,argv,"O:d:r:"))!=EOF)
switch(opcja) {
case 'O':
(void)strcpy(napis,optarg);
break;
case 'd':
if(strcmp(optarg,"+")==0)
gendat=ON;
else if(strcmp(optarg,"-")==0)
gendat=OFF;
else
{
fprintf(stderr,"Available options -d[-|+] !\n");
FreeArgv(argv,argc);
return;
}
break;
case 'r':
if(strcmp(optarg,"+")==0)
genrap=ON;
else if(strcmp(optarg,"-")==0)
genrap=OFF;
else {
fprintf(stderr,"Available options -r[-|+] !\n");
FreeArgv(argv,argc);
return;
}
break;
default:
fprintf(stderr,"Unknown option !\n");
FreeArgv(argv,argc);
return;
}
FreeArgv(argv,argc);
sprintf(filename,"%s.dat",napis);
sprintf(raportname,"%s.rap",napis);
if(prn==ON)
fprintf(stdout,"<<< GENERATING REPORT FROM THE DICTIONARY >>>\n");
if(gendat==ON)
if((stream=fopen(filename,"wt"))==NULL)
{
fprintf(stderr,"Cannot open file %s\n",filename);
return;
}
/* Tablice rozkladu slownika (histogramy) */
if((TransHist=(ULONG *)malloc(Size))==NULL ||
(ScaleHist=(ULONG *)malloc(Size))==NULL ||
(FreqHist=(ULONG *)malloc(Size))==NULL)
{
if(TransHist!=NULL)
free((void *)TransHist);
if(ScaleHist!=NULL)
free((void *)ScaleHist);
if(stream!=NULL)
fclose(stream);
fprintf(stderr,"Bad memory allocation !\n");
return;
}
if((tmptab=MakeVector(DimBase+PARSIZE+1))==NULL)
{
fprintf(stderr,"Cannot allocate memory !\n");
if(stream!=NULL)
fclose(stream);
free((void *)TransHist); free((void *)ScaleHist); free((void *)FreqHist);
return;
}
for(i=0 ; i<DimBase ; i++)
TransHist[i]=ScaleHist[i]=FreqHist[i]=0UL;
if(MakeDictionary()==-1)
{
fprintf(stderr,"Cannot generate the dictionary !\n");
if(stream!=NULL)
fclose(stream);
free((void *)TransHist); free((void *)ScaleHist);
free((void *)FreqHist); free((void *)tmptab);
return;
}
for(i=0 ; i<DictionSize ; i++)
{
LoadBigAtom(&atom,DimBase,i);
if(gendat==ON)
fprintf(stream,"%3d %3d %6.5f\n",(int)atom.scale,
(int)atom.trans,FreqConv*atom.frequency);
if(genrap==ON)
{
ScaleHist[atom.scale]++;
TransHist[atom.trans]++;
FreqHist[(int)(0.5F+df*atom.frequency)]++;
}
}
if(stream!=NULL)
fclose(stream);
if(genrap==ON)
{
MaxTrans=MaxScale=MaxFreq=0.0F;
for(i=1 ; i<DimBase ; i++)
{
if(MaxTrans<(float)TransHist[i])
MaxTrans=TransHist[i];
if(MaxScale<(float)ScaleHist[i])
MaxScale=ScaleHist[i];
if(MaxFreq<(float)FreqHist[i])
MaxFreq=FreqHist[i];
}
MaxTrans=((MaxTrans==0.0F) ? 1.0F : 1.0F/MaxTrans);
MaxScale=((MaxScale==0.0F) ? 1.0F : 1.0F/MaxScale);
MaxFreq=((MaxFreq==0.0F) ? 1.0F : 1.0F/MaxFreq);
if((stream=fopen(raportname,"wt"))!=NULL)
{
for(i=1 ; i<DimBase ; i++)
fprintf(stream,"%3d %7.6f %7.6f %7.6f\n",i,(float)ScaleHist[i]*MaxScale,
(float)TransHist[i]*MaxTrans,(float)FreqHist[i]*MaxFreq);
fclose(stream);
}
else fprintf(stderr,"Cannot open file %s !\n",raportname);
}
free((void *)tmptab); free((void *)TransHist);
free((void *)ScaleHist); free((void *)FreqHist);
CloseDictionary();
}
UCHAR Log2(float x) /* Logarytm przy podstawie 2 x calkowite */
{
if(fabs(x)<1.0e-8)
return (UCHAR)0;
return (UCHAR)(0.5+log(fabs(x))/M_LN2);
}
void PrintBigAtom(BIGATOM *atom) /* Wydruk parametrow atomu na ekran */
{
char *name="Zly !";
switch(atom->typ) {
case DIRAK:
name="Dirac";
break;
case FOURIER:
name="Fourier";
break;
case GABOR:
name="Gabor";
break;
}
/*fprintf(stdout," %10.7g %3d %3d %8.6f %8.6f %8.6f %s\n",
atom->modulus,(int)atom->scale,
(int)atom->trans,atom->frequency,atom->phase, atom->chirpfactor,name);*/
fprintf(stdout," %10.7g %3d %8.6f %8.6f %8.6f %8.6f %s\n",
atom->modulus,(int)atom->scale,
atom->trans/SamplingRate,atom->frequency*SamplingRate/(2*M_PI),atom->phase, atom->chirpfactor*SamplingRate*SamplingRate,name);
fflush(stdout);
}
/* Procedury numeryczne */
static INFOATOM *InfoTable; /* Inforamcje o iloczynach skalarnych */
static double *GaborTab,*TmpExpTab; /* Tablice pomocnicze */
// Efective range of a Gabor Atom
/* Zakres istotnosci atomu */
static double ConstScale;
#ifdef __GNUC__
__inline
#else
static
#endif
int AtomLen(BIGATOM *atom,int DimBase)
{
const int len=(int)(ConstScale*atom->scale);
return (len>=DimBase) ? DimBase-1 : len;
}
static void MakeExpTable(register double *ExpTab,double alpha,int trans,
register int start,register int stop)
{
register int left,right; /* Szybka generacja tablicy funkcji */
register double Factor,OldExp,ConstStep; /* wykladniczej */
if(start<trans && trans<stop) /* Maksimum w srodku przedzialu */
{
*(ExpTab+trans)=OldExp=1.0;
Factor=exp(-alpha);
ConstStep=SQR(Factor); /* Symetria */
for(left=trans-1,right=trans+1 ;
start<=left && right<=stop ;
left--,right++)
{
OldExp*=Factor;
*(ExpTab+left)=OldExp;
*(ExpTab+right)=OldExp;
Factor*=ConstStep;
}
if(left>start) /* Kompensacja odstepstw od symetrii */
for( ; start<=left ; left--)
{
*(ExpTab+left)=OldExp*=Factor;
Factor*=ConstStep;
}
else for( ; right<=stop; right++)
{
*(ExpTab+right)=OldExp*=Factor;
Factor*=ConstStep;
}
return;
}
ConstStep=exp(-2.0*alpha); /* Maksimum poza przedzialem */
if(trans>=stop)
{
const int itmp=trans-stop; /* Przedzial na lewo od maksimum */
*(ExpTab+stop)=OldExp=exp(-alpha*DSQR(itmp));
Factor=exp(-alpha*(double)((itmp << 1)+1));
for(left=stop-1; start<=left ; left--)
{
*(ExpTab+left)=OldExp*=Factor;
Factor*=ConstStep;
}
}
else
{ /* Przedzial na prawo od maksimum */
const int itmp=start-trans;
*(ExpTab+start)=OldExp=exp(-alpha*DSQR(itmp));
Factor=exp(-alpha*(double)((itmp << 1)+1));
for(right=start+1; right<=stop ; right++)
{
*(ExpTab+right)=OldExp*=Factor;
Factor*=ConstStep;
}
}
}
static void DotGaborAtoms(double alpha,double freq,int trans,int start,
register int stop,register double *GaborTab,
register double *ExpTab,double *DotSin,
double *DotCos)
{
register double NewCos,dtmp,sum1,sum2;
double Sin,Cos,OldSin,OldCos;
register int i;
sincos(freq,&Sin,&Cos);
MakeExpTable(ExpTab,alpha,trans,start,stop);
sincos(freq*(double)(start-trans),&OldSin,&OldCos);
dtmp=*(ExpTab+start)**(GaborTab+start);
sum1=dtmp*OldSin;
sum2=dtmp*OldCos;
for(i=start+1 ; i<=stop ; i++) /* Iloczyny sklarne */
{ /* atomow gabora i fouriera */
NewCos=OldCos*Cos-OldSin*Sin;
OldSin=OldCos*Sin+OldSin*Cos;
OldCos=NewCos;
dtmp=*(ExpTab+i)**(GaborTab+i);
sum1+=dtmp*OldSin;
sum2+=dtmp*OldCos;
}
*DotSin=sum1;
*DotCos=sum2;
}
/* Wyznaczenie iloczynu sklarnego atomow gabora
metoda oparta na sumach Gabora (dla slownikow diadycznych)
te wielkosci nalezy zainicjowac
S1sqr=DSQR(atom1.scale);
temp=-atom1.frequency*atom1.trans+atom1.phase;
*/
static double FastGaborDot(BIGATOM *atom1,BIGATOM *atom2,INFOATOM *info,
double Factor,double S1sqr,double temp,
double *amplitude,double *phase)
{
const int index=ConvTable[atom1->iScale][atom2->iScale];
const double ConstK=Factor*HashTable[index][abs(atom1->trans-atom2->trans)].ConstK;
double Sin1,Sin2,Cos1,Cos2,Sin,Cos,Modulus;
if(ConstK>EPSYLON)
{
const double S2sqr=DSQR(atom2->scale),
tw=(S2sqr*atom1->trans+S1sqr*atom2->trans)/(S1sqr+S2sqr),
C1=ConstK*HashTable[index][atom1->iFreq+atom2->iFreq].ConstCos,
C2=ConstK*HashTable[index][abs(atom1->iFreq-atom2->iFreq)].ConstCos,
dtmp1=atom1->frequency*tw+temp,
dtmp2=atom2->frequency*(tw-(double)atom2->trans),
phase1=dtmp1-dtmp2,phase2=dtmp1+dtmp2;
sincos(phase1,&Sin1,&Cos1);
sincos(phase2,&Sin2,&Cos2);
info->Fc-=C2*Cos1+C1*Cos2;
info->Fs-=C1*Sin2-C2*Sin1;
}
else
{
*amplitude=info->Amplitude;
*phase=info->Phase;
return info->Modulus;
}
if(info->Fs==0.0 && info->Fc==0.0)/* Zabespieczenie przed wyjatkiem w BC 4.5 */
*phase=0.0;
else
*phase=atan2(-info->Fs/info->SNorm,info->Fc/info->CNorm);
info->Phase=*phase;
sincos(*phase,&Sin,&Cos);
Modulus=TSQR(info->Fc*Cos-info->Fs*Sin)/(*amplitude=
(info->CNorm*SQR(Cos)+info->SNorm*SQR(Sin)));
info->Amplitude=*amplitude;
return info->Modulus=Modulus;
}
/* Wyznaczenie iloczynow sklarnych atomu Fouriera z Gaborem
dla slownikow diadycznych */
static void FastFourierDot(BIGATOM *atom1,BIGATOM *atom2,
double *Fc,double *Fs,register double Factor,
register double temp)
{
const int index=atom2->iScale;
const double C1=Factor*HashCosTable[index][atom1->iFreq+atom2->iFreq],
C2=Factor*HashCosTable[index][abs(atom1->iFreq-atom2->iFreq)],
phase=atom1->frequency*(double)atom2->trans+temp;
double Sin,Cos;
sincos(phase,&Sin,&Cos);
*Fc=C2*Cos+C1*Cos;
*Fs=C1*Sin-C2*Sin;
}
double FindCrossAtomPhase(BIGATOM *atom1,BIGATOM *atom2,double *GaborTab,
double *TmpExpTab,int DimBase,INFOATOM *info,
double *phase,double *amplitude,double temp,
double Factor)
{
double dtmp,dtmp2,dtmp3,DotSin,DotCos,Sin,Cos,K1,trans,Modulus;
int start,stop,pozycja,itmp,itmp2,MaxStop;
switch(atom1->typ) {
case GABOR:
{
const double S1sqr=DSQR(atom1->scale), /* Wyznaczenie fazy atomu gabora */
S2sqr=DSQR(atom2->scale), /* z aktualizacja iloczynow sklarnych */
u1=(double)(atom1->trans),
u2=(double)(atom2->trans);
dtmp2=u1-u2;
K1=exp(-M_PI*SQR(dtmp2)/(dtmp=S1sqr+S2sqr));
if(K1>EPSYLON) /* Wyznaczenie obszaru istotnosci atomu */
{
if(DiadicStructure==ON && VeryFastMode==ON) /* Zwiekszenie wydajnosci algorytmu (mozliwe niestabilnosci) */
if(atom1->iScale>MAXSCALE && atom2->iScale>MAXSCALE)
return FastGaborDot(atom1,atom2,info,Factor,S1sqr,temp,
amplitude,phase);
MaxStop=3*DimBase-1;
dtmp3=log(K1/EPSYLON);
trans=floor(0.5+(S1sqr*u2+S2sqr*u1)/dtmp);
pozycja=(int)(DimBase+trans);
dtmp=1.5+sqrt(dtmp3/(M_PI*(S1sqr+S2sqr)/(S1sqr*S2sqr)));
start=pozycja-(int)dtmp;
if(start<0)
start=0;
stop=pozycja+(int)dtmp;
if(stop>MaxStop)
stop=MaxStop;
DotGaborAtoms(M_PI/DSQR(atom2->scale),atom2->frequency,
DimBase+atom2->trans,start,stop,GaborTab,TmpExpTab,
&DotSin,&DotCos);
info->Fs-=DotSin; /* Aktualizacja iloczynow skalarnych */
info->Fc-=DotCos;
}
else
{
*amplitude=info->Amplitude;
*phase=info->Phase;
return info->Modulus;
}
}
break;
case FOURIER:
if(DiadicStructure!=ON)
{
itmp=AtomLen(atom2,DimBase); /* Wyznaczamy iloczyn skalarny na */
itmp2=DimBase+atom2->trans; /* obszarze analizowanego atomu */
DotGaborAtoms(M_PI/DSQR(atom2->scale),atom2->frequency,
itmp2,itmp2-itmp,itmp2+itmp,
GaborTab,TmpExpTab,&DotSin,&DotCos);
/* Uwaga ! Zakresu nie trzeba sprawdzac */
} /* Zwiekszenie wydajnosci dla slownikow diadycznych */
else FastFourierDot(atom1,atom2,&DotCos,&DotSin,
Factor,temp);
info->Fs-=DotSin;
info->Fc-=DotCos;
break;
case DIRAK: /* Dla delty Diraka to tylko */
itmp=atom1->trans-atom2->trans; /* odpowiednie wartosci */
dtmp=(double)itmp/(double)atom2->scale;
dtmp=atom1->modulus*exp(-M_PI*SQR(dtmp));
info->Fs-=dtmp*sin(dtmp2=atom2->frequency*(double)itmp);
info->Fc-=dtmp*cos(dtmp2);
break;
}
if(info->Fs==0.0 && info->Fc==0.0)
*phase=0.0;
else
*phase=atan2(-info->Fs/info->SNorm,info->Fc/info->CNorm);
info->Phase=*phase;
sincos(*phase,&Sin,&Cos);
Modulus=TSQR(info->Fc*Cos-info->Fs*Sin)/(*amplitude=
(info->CNorm*SQR(Cos)+info->SNorm*SQR(Sin)));
info->Amplitude=*amplitude;
return info->Modulus=Modulus;
}
static void MakeGaborAtom(double *GaborFunc,register int n,BIGATOM *atom)
{
register int i;
const double ConstExp=exp(-M_PI/SQR((double)(atom->scale))),
ConstStep=SQR(ConstExp),
Ampli=atom->modulus/atom->amplitude,
AmpliCos=Ampli*cos(atom->phase),
AmpliSin=-Ampli*sin(atom->phase);
double *PtrGaborFunc=GaborFunc+atom->trans,
OldSin=0.0,OldCos=1.0,NewCos,OldExp=1.0,Factor=ConstExp,
dtmp1,dtmp2,Sin,Cos;
sincos(atom->frequency,&Sin,&Cos);
*PtrGaborFunc=AmpliCos; /* Generacja atomu Gabora */
for(i=1 ; i<n ; i++)
{
NewCos=OldCos*Cos-OldSin*Sin;
OldSin=OldCos*Sin+OldSin*Cos;
OldCos=NewCos;
OldExp*=Factor;
Factor*=ConstStep;
dtmp1=AmpliCos*OldExp*OldCos;
dtmp2=AmpliSin*OldExp*OldSin;
*(PtrGaborFunc+i)=dtmp1+dtmp2;
*(PtrGaborFunc-i)=dtmp1-dtmp2;
}
}
static void MakeFourierAtom(double *sygnal,
register int n,BIGATOM *atom)
{
register int i;
double Sin,Cos,CosPhase,SinPhase;
const double Constans=atom->modulus/atom->amplitude;
register double OldSin=0.0,OldCos=1.0,NewCos,dtmp,dtmp2;
sincos(atom->frequency,&Sin,&Cos);
sincos(atom->phase,&SinPhase,&CosPhase);
sygnal+=atom->trans;
*sygnal=CosPhase*Constans; /* Generacja atomu Fouriera */
for(i=1 ; i<n ; i++)
{
NewCos=OldCos*Cos-OldSin*Sin;
OldSin=OldCos*Sin+OldSin*Cos;
OldCos=NewCos;
dtmp=CosPhase*NewCos;
dtmp2=SinPhase*OldSin;
*(sygnal+i)=Constans*(dtmp-dtmp2);
*(sygnal-i)=Constans*(dtmp+dtmp2);
}
}
static void MakeMaximalAtom(double *GabTab,BIGATOM *atom,int DimBase)
{
switch(atom->typ)
{ /* Generacja atomu (interface) */
case GABOR:
MakeGaborAtom(GabTab+DimBase,AtomLen(atom,DimBase),atom);
break;
case FOURIER:
MakeFourierAtom(GabTab+DimBase,DimBase,atom);
break;
}
}
/* Wyznaczenie iloczynu skalarnego funkcji Gabora z sygnalem
z jednoczesnym wyznaczeniem optymalnej fazy */
#ifdef MULTITHREAD
__inline
#else
static
#endif
double FindGaborPhase(register double *f,int n,double alpha,
double freq,double *phase,double *amplitude,
INFOATOM *info)
{
register int i;
double Sin,Cos;
register double dtmp,dtmp2;
const double ConstExp=exp(-alpha),ConstStep=SQR(ConstExp);
double OldSin=0.0,OldCos=1.0,NewCos,OldExp=1.0,
Factor=ConstExp,Ks=0.0,Kc=0.0,Fs=0.0,Fc=*f,Modulus;
sincos(freq,&Sin,&Cos);
for(i=1 ; i<n ; i++)
{
// same trick from FindFourierPhase
NewCos=OldCos*Cos-OldSin*Sin;
OldSin=OldCos*Sin+OldSin*Cos;
OldCos=NewCos;
// same that exp(-((double)i*i)*alpha);
OldExp*=Factor;
Factor*=ConstStep;
dtmp=OldExp*NewCos;
dtmp2=OldExp*OldSin;
Kc+=SQR(dtmp);
Ks+=SQR(dtmp2);
Fc+=dtmp*(*(f+i)+*(f-i));
Fs+=dtmp2*(*(f+i)-*(f-i));
}
Kc=2.0*Kc+1.0;
Ks*=2.0;
if(fabs(Fs)<1.0e-10 && fabs(Fc)<1.0e-10)
*phase=0.0;
else
*phase=atan2(-Fs/Ks,Fc/Kc);
sincos(*phase,&OldSin,&OldCos);
Modulus=TSQR(Fc*OldCos-Fs*OldSin)/(*amplitude=
(Kc*SQR(OldCos)+Ks*SQR(OldSin)));