41 void four1(
float data[],
unsigned long nn,
int isign)
43 unsigned long n,mmax,m,j,i;
44 double wtemp,wr,wpr,wpi,wi,theta;
58 while (m >= 2 && j > m)
69 unsigned long istep=mmax << 1;
70 theta=isign*(6.28318530717959/mmax);
72 wpr = -2.0*wtemp*wtemp;
78 for (i=m;i<=
n;i+=istep)
81 tempr=(float) (wr*
data[j]-wi*
data[j+1]);
82 tempi=(float) (wr*
data[j+1]+wi*
data[j]);
88 wr=(wtemp=wr)*wpr-wi*wpi+wr;
89 wi=wi*wpr+wtemp*wpi+wi;
104 unsigned long i,i1,i2,i3,i4,np3;
105 float c1=0.5,c2,h1r,h1i,h2r,h2i;
106 double wr,wi,wpr,wpi,wtemp,theta;
107 theta=3.141592653589793/(double) (
n>>1);
112 wtemp=sin(0.5*theta);
113 wpr = -2.0*wtemp*wtemp;
118 for (i=2;i<=(n>>2);i++)
120 i4=1+(i3=np3-(i2=1+(i1=i+i-1)));
125 data[i1]=(float)(h1r+wr*h2r-wi*h2i);
126 data[i2]=(float)(h1i+wr*h2i+wi*h2r);
127 data[i3]=(float)(h1r-wr*h2r+wi*h2i);
128 data[i4]=(float)( -h1i+wr*h2i+wi*h2r);
129 wr=(wtemp=wr)*wpr-wi*wpi+wr;
130 wi=wi*wpr+wtemp*wpi+wi;
158 int j, j1, j2, j3, k, k1, ks, l, m;
159 FFT_TYPE wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
160 FFT_TYPE x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
163 while ((l << 1) <
n) {
165 for (j = 0; j <= l - 2; j += 2) {
170 x0i =
a[j + 1] +
a[j1 + 1];
172 x1i =
a[j + 1] -
a[j1 + 1];
174 x2i =
a[j2 + 1] +
a[j3 + 1];
176 x3i =
a[j2 + 1] -
a[j3 + 1];
178 a[j + 1] = x0i + x2i;
180 a[j2 + 1] = x0i - x2i;
182 a[j1 + 1] = x1i + x3r;
184 a[j3 + 1] = x1i - x3r;
188 for (j = m; j <= l + m - 2; j += 2) {
193 x0i =
a[j + 1] +
a[j1 + 1];
195 x1i =
a[j + 1] -
a[j1 + 1];
197 x2i =
a[j2 + 1] +
a[j3 + 1];
199 x3i =
a[j2 + 1] -
a[j3 + 1];
201 a[j + 1] = x0i + x2i;
203 a[j2 + 1] = x0r - x2r;
206 a[j1] = wk1r * (x0r - x0i);
207 a[j1 + 1] = wk1r * (x0r + x0i);
210 a[j3] = wk1r * (x0i - x0r);
211 a[j3 + 1] = wk1r * (x0i + x0r);
215 for (k = (m << 1); k <=
n - m; k += m) {
219 wk1i =
w[(k1 << 1) + 1];
222 wk3r = wk1r - 2 * wk2i * wk1i;
223 wk3i = 2 * wk2i * wk1r - wk1i;
224 for (j = k; j <= l + k - 2; j += 2) {
229 x0i =
a[j + 1] +
a[j1 + 1];
231 x1i =
a[j + 1] -
a[j1 + 1];
233 x2i =
a[j2 + 1] +
a[j3 + 1];
235 x3i =
a[j2 + 1] -
a[j3 + 1];
237 a[j + 1] = x0i + x2i;
240 a[j2] = wk2r * x0r - wk2i * x0i;
241 a[j2 + 1] = wk2r * x0i + wk2i * x0r;
244 a[j1] = wk1r * x0r - wk1i * x0i;
245 a[j1 + 1] = wk1r * x0i + wk1i * x0r;
248 a[j3] = wk3r * x0r - wk3i * x0i;
249 a[j3 + 1] = wk3r * x0i + wk3i * x0r;
256 for (j = 0; j <= l - 2; j += 2) {
259 x0i =
a[j + 1] -
a[j1 + 1];
261 a[j + 1] +=
a[j1 + 1];
270 int j, j1, j2, j3, k, k1, ks, l, m;
271 FFT_TYPE wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
272 FFT_TYPE x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
275 while ((l << 1) <
n) {
277 for (j = 0; j <= l - 2; j += 2) {
282 x0i =
a[j + 1] +
a[j1 + 1];
284 x1i =
a[j + 1] -
a[j1 + 1];
286 x2i =
a[j2 + 1] +
a[j3 + 1];
288 x3i =
a[j2 + 1] -
a[j3 + 1];
290 a[j + 1] = x0i + x2i;
292 a[j2 + 1] = x0i - x2i;
294 a[j1 + 1] = x1i - x3r;
296 a[j3 + 1] = x1i + x3r;
300 for (j = m; j <= l + m - 2; j += 2) {
305 x0i =
a[j + 1] +
a[j1 + 1];
307 x1i =
a[j + 1] -
a[j1 + 1];
309 x2i =
a[j2 + 1] +
a[j3 + 1];
311 x3i =
a[j2 + 1] -
a[j3 + 1];
313 a[j + 1] = x0i + x2i;
315 a[j2 + 1] = x2r - x0r;
318 a[j1] = wk1r * (x0i + x0r);
319 a[j1 + 1] = wk1r * (x0i - x0r);
322 a[j3] = wk1r * (x0r + x0i);
323 a[j3 + 1] = wk1r * (x0r - x0i);
327 for (k = (m << 1); k <=
n - m; k += m) {
331 wk1i =
w[(k1 << 1) + 1];
334 wk3r = wk1r - 2 * wk2i * wk1i;
335 wk3i = 2 * wk2i * wk1r - wk1i;
336 for (j = k; j <= l + k - 2; j += 2) {
341 x0i =
a[j + 1] +
a[j1 + 1];
343 x1i =
a[j + 1] -
a[j1 + 1];
345 x2i =
a[j2 + 1] +
a[j3 + 1];
347 x3i =
a[j2 + 1] -
a[j3 + 1];
349 a[j + 1] = x0i + x2i;
352 a[j2] = wk2r * x0r + wk2i * x0i;
353 a[j2 + 1] = wk2r * x0i - wk2i * x0r;
356 a[j1] = wk1r * x0r + wk1i * x0i;
357 a[j1 + 1] = wk1r * x0i - wk1i * x0r;
360 a[j3] = wk3r * x0r + wk3i * x0i;
361 a[j3 + 1] = wk3r * x0i - wk3i * x0r;
368 for (j = 0; j <= l - 2; j += 2) {
371 x0i =
a[j + 1] -
a[j1 + 1];
373 a[j + 1] +=
a[j1 + 1];
392 delta = atan(1.0f) / nwh;
395 w[nwh] = cos(delta * nwh);
397 for (j = 2; j <= nwh - 2; j += 2) {
422 delta = atan(1.0f) / nch;
424 c[nch] = 0.5f * cos(delta * nch);
425 for (j = 1; j <= nch - 1; j++) {
426 c[j] = 0.5f * cos(delta * j);
427 c[nc - j] = 0.5f * sin(delta * j);
439 int j, j1, k, k1, l, m, m2;
445 while ((m << 2) < l) {
447 for (j = 0; j <= m - 1; j++) {
448 ip[m + j] = ip[j] + l;
453 for (k = 1; k <= m - 1; k++) {
454 for (j = 0; j <= k - 1; j++) {
455 j1 = (j << 1) + ip[k];
456 k1 = (k << 1) + ip[j];
460 a[j1 + 1] =
a[k1 + 1];
467 for (k = 1; k <= m - 1; k++) {
468 for (j = 0; j <= k - 1; j++) {
469 j1 = (j << 1) + ip[k];
470 k1 = (k << 1) + ip[j];
474 a[j1 + 1] =
a[k1 + 1];
482 a[j1 + 1] =
a[k1 + 1];
497 if (
n > (ip[0] << 2)) {
517 for (k = (
n >> 1) - 2; k >= 2; k -= 2) {
523 xi =
a[k + 1] +
a[j + 1];
524 yr = wkr * xr + wki * xi;
525 yi = wkr * xi - wki * xr;
541 for (k = 1; k <= m - 1; k++) {
543 wkr =
c[kk] -
c[nc - kk];
544 wki =
c[kk] +
c[nc - kk];
546 xr = wki *
a[k] - wkr *
a[j];
547 a[k] = wkr *
a[k] + wki *
a[j];
561 for (k = 1; k <= m - 1; k++) {
563 wkr =
c[kk] -
c[nc - kk];
564 wki =
c[kk] +
c[nc - kk];
566 xr = wki *
a[j] - wkr *
a[k];
567 a[j] = wkr *
a[j] + wki *
a[k];
590 a[1] = 0.5f * (
a[0] -
a[1]);
618 for (k = (
n >> 1) - 2; k >= 2; k -= 2) {
624 xi =
a[k + 1] +
a[j + 1];
625 yr = wkr * xr - wki * xi;
626 yi = wkr * xi + wki * xr;
736 int n, nw, nc, n1h, i, j, i2;
749 if (n2 > (nc << 2)) {
755 for (i = 1; i <= n1h - 1; i++) {
757 xi =
a[i][0] -
a[j][0];
760 xi =
a[j][1] -
a[i][1];
764 for (j = 0; j <= n2 - 2; j += 2) {
765 for (i = 0; i <= n1 - 1; i++) {
768 t[i2 + 1] =
a[i][j + 1];
770 cdft(n1 << 1, isgn,
t, ip,
w);
771 for (i = 0; i <= n1 - 1; i++) {
774 a[i][j + 1] =
t[i2 + 1];
777 for (i = 0; i <= n1 - 1; i++) {
778 rdft(n2, isgn,
a[i], ip,
w);
781 for (i = 0; i <= n1 - 1; i++) {
782 rdft(n2, isgn,
a[i], ip,
w);
784 for (j = 0; j <= n2 - 2; j += 2) {
785 for (i = 0; i <= n1 - 1; i++) {
788 t[i2 + 1] =
a[i][j + 1];
790 cdft(n1 << 1, isgn,
t, ip,
w);
791 for (i = 0; i <= n1 - 1; i++) {
794 a[i][j + 1] =
t[i2 + 1];
797 for (i = 1; i <= n1h - 1; i++) {
799 a[j][0] = 0.5f * (
a[i][0] -
a[j][0]);
801 a[j][1] = 0.5f * (
a[i][1] +
a[j][1]);
880 if (
n > (ip[0] << 2)) {
883 for (i = 0; i <= n1 - 1; i++) {
884 cdft(n2, isgn,
a[i], ip,
w);
886 for (j = 0; j <= n2 - 2; j += 2) {
887 for (i = 0; i <= n1 - 1; i++) {
890 t[i2 + 1] =
a[i][j + 1];
892 cdft(n1 << 1, isgn,
t, ip,
w);
893 for (i = 0; i <= n1 - 1; i++) {
896 a[i][j + 1] =
t[i2 + 1];
910 return ::exp( -0.5 *
square((
x-mu)/
std) ) / (
std*2.506628274631000502415765284811) ;
919 #if defined(HAVE_ERF) 931 const double a1 = -1.26551223,
a2 = 1.00002368,
932 a3 = 0.37409196, a4 = 0.09678418,
933 a5 = -0.18628806, a6 = 0.27886807,
934 a7 = -1.13520398, a8 = 1.48851587,
935 a9 = -0.82215223, a10 = 0.17087277;
938 double z = ::fabs(
x);
940 if (
z <= 0)
return v;
942 double t = 1.0/(1.0+0.5*
z);
946 if (
x < 0)
v = 2.0-
v;
957 #if defined(HAVE_ERF) 972 else return dim * pow( 1.0 - 2.0/(9*dim) + sqrt(2.0/(9*dim))*
normalQuantile(P), 3 );
982 for (
unsigned int i=2;i<=
n;i++)
995 for (
unsigned int i=2;i<=
n;i++)
996 retLog += ::log( (
double)
n );
998 return ::exp(retLog);
1011 unsigned long n = (
unsigned long)in_realData.size();
1017 memcpy( &auxVect[1], &in_realData[0],
n *
sizeof(auxVect[0]));
1021 unsigned int n_2 = 1 + (
n / 2);
1023 out_FFT_Re.resize(n_2);
1024 out_FFT_Im.resize(n_2);
1025 out_FFT_Mag.resize(n_2);
1027 for (
unsigned int i=0;i<n_2;i++)
1030 out_FFT_Re[i] = auxVect[2];
1031 else out_FFT_Re[i] = auxVect[1+i*2];
1033 if (i==0 || i==(n_2-1))
1035 else out_FFT_Im[i] = auxVect[1+i*2+1];
1037 out_FFT_Mag[i] = sqrt(
square(out_FFT_Re[i])+
square(out_FFT_Im[i]) );
1050 static const double a[6] =
1052 -3.969683028665376e+01, 2.209460984245205e+02,
1053 -2.759285104469687e+02, 1.383577518672690e+02,
1054 -3.066479806614716e+01, 2.506628277459239e+00
1056 static const double b[5] =
1058 -5.447609879822406e+01, 1.615858368580409e+02,
1059 -1.556989798598866e+02, 6.680131188771972e+01,
1060 -1.328068155288572e+01
1062 static const double c[6] =
1064 -7.784894002430293e-03, -3.223964580411365e-01,
1065 -2.400758277161838e+00, -2.549732539343734e+00,
1066 4.374664141464968e+00, 2.938163982698783e+00
1068 static const double d[4] =
1070 7.784695709041462e-03, 3.224671290700398e-01,
1071 2.445134137142996e+00, 3.754408661907416e+00
1084 u = u*(((((
a[0]*
t+
a[1])*
t+
a[2])*
t+
a[3])*
t+
a[4])*
t+
a[5])
1085 /(((((
b[0]*
t+
b[1])*
t+
b[2])*
t+
b[3])*
t+
b[4])*
t+1);
1090 t = sqrt(-2*::log(
q));
1091 u = (((((
c[0]*
t+
c[1])*
t+
c[2])*
t+
c[3])*
t+
c[4])*
t+
c[5])
1092 /((((d[0]*
t+d[1])*
t+d[2])*
t+d[3])*
t+1);
1099 t =
t* 2.506628274631000502415765284811 * ::exp(u*u/2);
1102 return (
p > 0.5 ? -u : u);
1110 static const double a[5] =
1112 1.161110663653770e-002,3.951404679838207e-001,2.846603853776254e+001,
1113 1.887426188426510e+002,3.209377589138469e+003
1115 static const double b[5] =
1117 1.767766952966369e-001,8.344316438579620e+000,1.725514762600375e+002,
1118 1.813893686502485e+003,8.044716608901563e+003
1120 static const double c[9] =
1122 2.15311535474403846e-8,5.64188496988670089e-1,8.88314979438837594e00,
1123 6.61191906371416295e01,2.98635138197400131e02,8.81952221241769090e02,
1124 1.71204761263407058e03,2.05107837782607147e03,1.23033935479799725E03
1126 static const double d[9] =
1128 1.00000000000000000e00,1.57449261107098347e01,1.17693950891312499e02,
1129 5.37181101862009858e02,1.62138957456669019e03,3.29079923573345963e03,
1130 4.36261909014324716e03,3.43936767414372164e03,1.23033935480374942e03
1132 static const double p[6] =
1134 1.63153871373020978e-2,3.05326634961232344e-1,3.60344899949804439e-1,
1135 1.25781726111229246e-1,1.60837851487422766e-2,6.58749161529837803e-4
1137 static const double q[6] =
1139 1.00000000000000000e00,2.56852019228982242e00,1.87295284992346047e00,
1140 5.27905102951428412e-1,6.05183413124413191e-2,2.33520497626869185e-3
1148 if (
y <= 0.46875* 1.4142135623730950488016887242097 )
1152 y = u*((((
a[0]*
z+
a[1])*
z+
a[2])*
z+
a[3])*
z+
a[4])
1153 /((((
b[0]*
z+
b[1])*
z+
b[2])*
z+
b[3])*
z+
b[4]);
1157 z = ::exp(-
y*
y/2)/2;
1161 y =
y/ 1.4142135623730950488016887242097 ;
1166 /((((((((d[0]*
y+d[1])*
y+d[2])*
y+d[3])*
y+d[4])*
y+d[5])*
y+d[6])*
y+d[7])*
y+d[8]);
1173 z =
z* 1.4142135623730950488016887242097 /
y;
1176 /(((((
q[0]*
y+
q[1])*
y+
q[2])*
y+
q[3])*
y+
q[4])*
y+
q[5]);
1177 y =
z*( 0.564189583547756286948 -
y);
1179 return (u < 0.0 ?
y : 1-
y);
1197 size_t dim1 = in_data.getRowCount();
1198 size_t dim2 = in_data.getColCount();
1209 a =
new float_ptr[dim1];
1210 for (i=0;i<dim1;i++)
1213 for (j=0;j<dim2;j++)
1214 a[i][j] = in_data.get_unsafe(i,j);
1218 ip =
new int[(int)ceil(20+2+sqrt((
FFT_TYPE)max(dim1,dim2/2)))];
1220 w =
new FFT_TYPE[max(dim1/2,dim2/4)+dim2/4+20];
1228 out_real.setSize(dim1,dim2);
1229 out_imag.setSize(dim1,dim2);
1235 for (i=1;i<dim1;i++)
1236 for (j=1;j<dim2/2;j++)
1238 out_real.set_unsafe(i,j,(
float)
a[i][j*2]); out_real.set_unsafe(dim1-i,dim2-j,(
float)
a[i][j*2]);
1239 out_imag.set_unsafe(i,j,(
float)-
a[i][j*2+1]); out_imag.set_unsafe(dim1-i,dim2-j,(
float)
a[i][j*2+1]);
1244 for (j=1;j<dim2/2;j++)
1246 out_real.set_unsafe(0,j,(
float)
a[0][j*2]); out_real.set_unsafe(0,dim2-j,(
float)
a[0][j*2]);
1247 out_imag.set_unsafe(0,j,(
float)-
a[0][j*2+1]); out_imag.set_unsafe(0,dim2-j,(
float)
a[0][j*2+1]);
1255 for (i=1;i<dim1/2;i++)
1257 out_real.set_unsafe(i,0,(
float)
a[i][0]); out_real.set_unsafe(dim1-i,0,(
float)
a[i][0]);
1258 out_imag.set_unsafe(i,0,(
float)-
a[i][1]); out_imag.set_unsafe(dim1-i,0,(
float)
a[i][1]);
1259 out_real.set_unsafe(i,dim2/2,(
float)
a[dim1-i][1]); out_real.set_unsafe(dim1-i,dim2/2,(
float)
a[dim1-i][1]);
1260 out_imag.set_unsafe(i,dim2/2,(
float)
a[dim1-i][0]); out_imag.set_unsafe(dim1-i,dim2/2,(
float)-
a[dim1-i][0]);
1267 out_real.set_unsafe(0,0,(
float)
a[0][0]);
1268 out_real.set_unsafe(0,dim2/2,(
float)
a[0][1]);
1269 out_real.set_unsafe(dim1/2,0,(
float)
a[dim1/2][0]);
1270 out_real.set_unsafe(dim1/2,dim2/2,(
float)
a[dim1/2][1]);
1274 for (i=0;i<dim1;i++)
delete[]
a[i];
1296 ASSERT_(in_real.getColCount()==in_imag.getColCount());
1297 ASSERT_(in_real.getRowCount()==in_imag.getRowCount());
1300 size_t dim1 = in_real.getRowCount();
1301 size_t dim2 = in_real.getColCount();
1315 a =
new float_ptr[dim1];
1316 for (i=0;i<dim1;i++)
a[i] =
new FFT_TYPE[dim2];
1321 for (i=1;i<dim1;i++)
1322 for (j=1;j<dim2/2;j++)
1324 a[i][2*j ] = in_real.get_unsafe(i,j);
1325 a[i][2*j+1] = -in_imag.get_unsafe(i,j);
1331 for (j=1;j<dim2/2;j++)
1333 a[0][2*j ] = in_real.get_unsafe(0,j);
1334 a[0][2*j+1] = -in_imag.get_unsafe(0,j);
1342 for (i=1;i<dim1/2;i++)
1344 a[i][0] = in_real.get_unsafe(i,0);
1345 a[i][1] = -in_imag.get_unsafe(i,0);
1346 a[dim1-i][1] = in_real.get_unsafe(i,dim2/2);
1347 a[dim1-i][0] = in_imag.get_unsafe(i,dim2/2);
1354 a[0][0] = in_real.get_unsafe(0,0);
1355 a[0][1] = in_real.get_unsafe(0,dim2/2);
1356 a[dim1/2][0] = in_real.get_unsafe(dim1/2,0);
1357 a[dim1/2][1] = in_real.get_unsafe(dim1/2,dim2/2);
1360 ip =
new int[(int)ceil(20+2+sqrt((
FFT_TYPE)max(dim1,dim2/2)))];
1362 w =
new FFT_TYPE[max(dim1/2,dim2/4)+dim2/4+20];
1366 rdft2d((
int)dim1,(
int)dim2,-1,
a,
t,ip,
w);
1370 out_data.setSize(dim1,dim2);
1374 for (i=0;i<dim1;i++)
1375 for (j=0;j<dim2;j++)
1376 out_data.set_unsafe(i,j,(
float)(
a[i][j]*
scale));
1379 for (i=0;i<dim1;i++)
delete[]
a[i];
1401 ASSERT_(in_real.getRowCount() == in_imag.getRowCount() )
1402 ASSERT_(in_real.getColCount() == in_imag.getColCount() )
1405 size_t dim1 = in_real.getRowCount();
1406 size_t dim2 = in_real.getColCount();
1410 float ang1 = (float)(
sign*
M_2PI/dim1 );
1411 float ang2 = (float)(
sign*
M_2PI/dim2 );
1414 float scale =
sign==1 ? (1.0f/(dim1*dim2)):1;
1416 out_real.setSize(dim1,dim2);
1417 out_imag.setSize(dim1,dim2);
1419 for (k1=0;k1<dim1;k1++)
1421 for (k2=0;k2<dim2;k2++)
1425 for (n1=0;n1<dim1;n1++)
1428 for (n2=0;n2<dim2;n2++)
1433 R+= w_r*in_real.get_unsafe(n1,n2) - w_i*in_imag.get_unsafe(n1,n2);
1434 I+= w_i*in_real.get_unsafe(n1,n2) + w_r*in_imag.get_unsafe(n1,n2);
1441 out_real.set_unsafe(k1,k2,
R*
scale);
1442 out_imag.set_unsafe(k1,k2,I*
scale);
1459 ASSERT_(in_real.getRowCount() == in_imag.getRowCount() )
1460 ASSERT_(in_real.getColCount() == in_imag.getColCount() )
1463 size_t dim1 = in_real.getRowCount();
1464 size_t dim2 = in_real.getColCount();
1471 if (dim1IsPower2 && dim2IsPower2)
1482 static int *ip=NULL;
1487 static int alreadyInitSize1 = -1, alreadyInitSize2 = -1;
1489 if (alreadyInitSize1!=(
int)dim1 || alreadyInitSize2!=(int)dim2)
1494 for (i=0;i<dim1;i++)
delete[]
a[i];
1497 if (ip)
delete[] ip;
1501 alreadyInitSize1 = (int)dim1;
1502 alreadyInitSize2 = (int)dim2;
1504 a =
new float_ptr[dim1];
1505 for (i=0;i<dim1;i++)
a[i] =
new FFT_TYPE[2*dim2];
1508 ip =
new int[(int)ceil(20+2+sqrt((
FFT_TYPE)max(dim1,dim2/2)))];
1510 w =
new FFT_TYPE[max(dim1/2,dim2/4)+dim2/4+20];
1515 for (i=0;i<dim1;i++)
1516 for (j=0;j<dim2;j++)
1518 a[i][2*j+0] = in_real.get_unsafe(i,j);
1519 a[i][2*j+1] = in_imag.get_unsafe(i,j);
1525 cdft2d((
int)dim1,(
int)(2*dim2),1,
a,
t,ip,
w);
1529 out_real.setSize(dim1,dim2);
1530 out_imag.setSize(dim1,dim2);
1535 for (i=0;i<dim1;i++)
1536 for (j=0;j<dim2;j++)
1538 out_real.set_unsafe(i,j,(
float)
a[i][j*2+0]);
1539 out_imag.set_unsafe(i,j,(
float)
a[i][j*2+1]);
1548 printf(
"Using general DFT...\n");
1568 ASSERT_(in_real.getRowCount() == in_imag.getRowCount() )
1569 ASSERT_(in_real.getColCount() == in_imag.getColCount() )
1572 size_t dim1 = in_real.getRowCount();
1573 size_t dim2 = in_real.getColCount();
1580 if (dim1IsPower2 && dim2IsPower2)
1591 static int *ip=NULL;
1599 static int alreadyInitSize1 = -1, alreadyInitSize2 = -1;
1601 if (alreadyInitSize1!=(
int)dim1 || alreadyInitSize2!=(int)dim2)
1606 for (i=0;i<dim1;i++)
delete[]
a[i];
1609 if (ip)
delete[] ip;
1613 alreadyInitSize1 = (int)dim1;
1614 alreadyInitSize2 = (int)dim2;
1616 a =
new float_ptr[dim1];
1617 for (i=0;i<dim1;i++)
a[i] =
new FFT_TYPE[2*dim2];
1619 ip =
new int[(int)ceil(20+2+sqrt((
FFT_TYPE)max(dim1,dim2/2)))];
1621 w =
new FFT_TYPE[max(dim1/2,dim2/4)+dim2/4+20];
1626 for (i=0;i<dim1;i++)
1627 for (j=0;j<dim2;j++)
1629 a[i][2*j+0] = in_real.get_unsafe(i,j);
1630 a[i][2*j+1] = in_imag.get_unsafe(i,j);
1635 cdft2d((
int)dim1,(
int)(2*dim2),-1,
a,
t,ip,
w);
1639 out_real.setSize(dim1,dim2);
1640 out_imag.setSize(dim1,dim2);
1647 for (i=0;i<dim1;i++)
1648 for (j=0;j<dim2;j++)
1652 out_real.set_unsafe(i,j,(
float)(
a[i][j*2+0]));
1653 out_imag.set_unsafe(i,j,(
float)(
a[i][j*2+1]));
1660 out_imag.set_unsafe(0,0,0);
1668 printf(
"Using general DFT...\n");
1681 #if defined(__BORLANDC__) || defined(_MSC_VER) 1682 return 0!=_isnan((
double)
v);
1683 #elif defined(__GNUC__) 1694 #if defined(__BORLANDC__) || defined(_MSC_VER) 1695 return 0!=_isnan(
v);
1696 #elif defined(__GNUC__) 1697 return std::isnan(
v);
1707 #if defined(__BORLANDC__) || defined(_MSC_VER) 1708 return 0!=_finite(
v);
1709 #elif defined(__GNUC__) 1710 return std::isfinite(
v);
1721 #if defined(__BORLANDC__) || defined(_MSC_VER) 1722 return 0!=_finite(
v);
1723 #elif defined(__GNUC__) 1731 #ifdef HAVE_LONG_DOUBLE 1737 #if MRPT_CHECK_VISUALC_VERSION(14) || defined(__GNUC__) 1738 return std::isnan(f);
1740 return 0!=_isnan(f);
1749 #if MRPT_CHECK_VISUALC_VERSION(14) || defined(__GNUC__) 1750 return std::isfinite(f);
1752 return 0!=_finite(f);
1764 if (!f.
readLine(str))
return false;
1766 const char *
s = str.c_str();
1768 char *nextTok, *context;
1769 const char *delim=
" \t";
1773 while (nextTok != NULL)
1775 d.push_back( atoi(nextTok) );
1788 if (!f.
readLine(str))
return false;
1790 const char *
s = str.c_str();
1792 char *nextTok, *context;
1793 const char *delim=
" \t";
1797 while (nextTok != NULL)
1799 d.push_back( atof(nextTok) );
1816 ASSERT_( logWeights.size()==logLikelihoods.size() );
1818 if ( !logWeights.size() )
1824 double SUM1=0, SUM2=0, tmpVal;
1826 for (itLW=logWeights.begin(),itLL=logLikelihoods.begin();itLW!=logWeights.end();itLW++,itLL++)
1828 tmpVal = *itLW - lw_max;
1829 SUM1+=std::exp( tmpVal );
1830 SUM2+=std::exp( tmpVal + *itLL - ll_max );
1833 double res = -std::log(SUM1) + std::log(SUM2) + ll_max;
1846 size_t N = logLikelihoods.size();
1853 for (
size_t i=0;i<N;i++)
1854 SUM1+=exp( logLikelihoods[i] - ll_max );
1856 double res = log(SUM1) - log (static_cast<double>(N)) + ll_max;
1866 if (angles.empty())
return 0;
1868 int W_phi_R=0,W_phi_L=0;
1869 double phi_R=0,phi_L=0;
1873 for (CVectorDouble::Index i=0;i<angles.size();i++)
1875 double phi = angles[i];
1876 if (abs(phi)>1.5707963267948966192313216916398)
1879 if (phi<0) phi = (
M_2PI + phi);
1895 if (W_phi_L) phi_L /=
static_cast<double>(W_phi_L);
1896 if (W_phi_R) phi_R /=
static_cast<double>(W_phi_R);
1902 return ((phi_L * W_phi_L + phi_R * W_phi_R )/(W_phi_L+W_phi_R));
1908 const float &stdCount,
1909 const string &style,
1910 const size_t &nEllipsePoints )
1926 const float &stdCount,
1927 const string &style,
1928 const size_t &nEllipsePoints )
1937 std::vector<float> X,Y,COS,SIN;
1943 X.resize(nEllipsePoints);
1944 Y.resize(nEllipsePoints);
1945 COS.resize(nEllipsePoints);
1946 SIN.resize(nEllipsePoints);
1949 for (Cos=COS.begin(),Sin=SIN.begin(),ang=0;Cos!=COS.end();++Cos,++Sin, ang+= (
M_2PI/(nEllipsePoints-1)) )
1951 *Cos = (float)cos(ang);
1952 *Sin = (float)sin(ang);
1955 cov.eigenVectors(eigVec,eigVal);
1956 eigVal = eigVal.array().sqrt().matrix();
1957 M = eigVal * eigVec.adjoint();
1961 for (
x=X.begin(),
y=Y.begin(), Cos=COS.begin(),Sin=SIN.begin();
x!=X.end(); ++
x,++
y,++Cos,++Sin)
1963 *
x = (float)(
mean[0] + stdCount * (*Cos * M(0,0) + *Sin * M(1,0)) );
1964 *
y = (float)(
mean[1] + stdCount * (*Cos * M(0,1) + *Sin * M(1,1)) );
1969 str +=
string(
"plot([ ");
1970 for (
x=X.begin();
x!=X.end();++
x)
1973 if (
x!=(X.end()-1)) str +=
format(
",");
1976 for (
y=Y.begin();
y!=Y.end();++
y)
1979 if (
y!=(Y.end()-1)) str +=
format(
",");
1982 str +=
format(
"],'%s');\n",style.c_str());
2004 const size_t lx =
size(A,2);
2005 const size_t ly =
size(A,1);
2016 for (
y = 0;
y<ly;
y++)
2017 for (
x = 0;
x<lx;
x++)
2019 float r1 = I1_R.get_unsafe(
y,
x);
2020 float r2 = I2_R.get_unsafe(
y,
x);
2022 float ii1 = I1_I.get_unsafe(
y,
x);
2023 float ii2 = I2_I.get_unsafe(
y,
x);
2026 I2_R.set_unsafe(
y,
x, (r1*r2+ii1*ii2)/den);
2027 I2_I.set_unsafe(
y,
x, (ii2*r1-r2*ii1)/den);
2034 out_corr.setSize(ly,lx);
2035 for (
y = 0;
y<ly;
y++)
2036 for (
x = 0;
x<lx;
x++)
2048 const double Ax = x1-x0;
2049 const double Ay = y1-y0;
2051 double r = y0+Ay*(
x-x0)/Ax;
2063 void mrpt::math::medianFilter(
const std::vector<double> &inV, std::vector<double> &outV,
const int &_winSize,
const int &numberOfSigmas )
2066 ASSERT_( (
int)inV.size() >= _winSize );
2068 size_t winSize = _winSize;
2073 size_t sz = inV.size();
2076 std::vector<double> aux(winSize);
2077 size_t mpoint = winSize/2;
2078 for(
size_t k = 0; k < sz; ++k )
2082 size_t idx_to_start = std::max(
size_t(0), k-mpoint );
2083 size_t n_elements =
std::min(
std::min( winSize, sz+mpoint-k), k+mpoint+1 );
2085 aux.resize(n_elements);
2086 for(
size_t m = idx_to_start,
n = 0; m < idx_to_start+n_elements; ++m, ++
n )
2089 std::sort( aux.begin(), aux.end() );
2091 size_t auxSz = aux.size();
2092 size_t auxMPoint = auxSz/2;
2093 outV[k] = (auxSz%2) ? (aux[auxMPoint]) : (0.5*(aux[auxMPoint-1]+aux[auxMPoint]));
2109 lans = lans + std::log(arg / j);
2110 dans = std::exp(lans);
2114 dans = dans * arg / j;
2122 ASSERTMSG_(noncentrality >= 0.0 && arg >= 0.0 &&
eps > 0.0,
"noncentralChi2PDF_CDF(): parameters must be positive.")
2124 if (arg == 0.0 && degreesOfFreedom > 0)
2125 return std::make_pair(0.0, 0.0);
2128 double b1 = 0.5 * noncentrality,
2131 lnrtpi2 = 0.22579135264473,
2132 probability, density, lans, dans, pans,
sum, am, hold;
2133 unsigned int maxit = 500,
2135 if(degreesOfFreedom % 2)
2138 lans = -0.5 * (arg + std::log(arg)) - lnrtpi2;
2139 dans = std::exp(lans);
2146 dans = std::exp(lans);
2151 if(degreesOfFreedom == 0)
2154 degreesOfFreedom = 2;
2156 sum = 1.0 / ao - 1.0 - am;
2157 density = am * dans;
2158 probability = 1.0 + am * pans;
2163 degreesOfFreedom = degreesOfFreedom - 1;
2165 sum = 1.0 / ao - 1.0;
2166 while(i < degreesOfFreedom)
2168 degreesOfFreedom = degreesOfFreedom + 1;
2173 for(++m; m<maxit; ++m)
2178 density = density + am * dans;
2180 probability = probability + hold;
2181 if((pans *
sum < eps2) && (hold < eps2))
2186 return std::make_pair(0.5 * ao * density,
std::min(1.0, std::max(0.0, ao * probability)));
2196 const double a = degreesOfFreedom + noncentrality;
2197 const double b = (
a + noncentrality) /
square(
a);
2198 const double t = (std::pow((
double)arg /
a, 1.0/3.0) - (1.0 - 2.0 / 9.0 *
b)) / std::sqrt(2.0 / 9.0 *
b);
void BASE_IMPEXP memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) MRPT_NO_THROWS
An OS and compiler independent version of "memcpy".
bool readLine(std::string &str)
Reads one string line from the file (until a new-line character)
double BASE_IMPEXP averageWrap2Pi(const CVectorDouble &angles)
Computes the average of a sequence of angles in radians taking into account the correct wrapping in t...
double BASE_IMPEXP chi2CDF(unsigned int degreesOfFreedom, double arg)
std::string BASE_IMPEXP MATLAB_plotCovariance2D(const CMatrixFloat &cov22, const CVectorFloat &mean, const float &stdCount, const std::string &style=std::string("b"), const size_t &nEllipsePoints=30)
Generates a string with the MATLAB commands required to plot an confidence interval (ellipse) for a 2...
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
This class is a "CSerializable" wrapper for "CMatrixTemplateNumeric<double>".
bool BASE_IMPEXP isNaN(float v) MRPT_NO_THROWS
void cdft(int n, int isgn, FFT_TYPE *a, int *ip, FFT_TYPE *w)
Copyright(C) 1997 Takuya OOURA (email: ooura@mmm.t.u-tokyo.ac.jp).
#define MRPT_END_WITH_CLEAN_UP(stuff)
void bitrv2(int n, int *ip, FFT_TYPE *a)
Copyright(C) 1997 Takuya OOURA (email: ooura@mmm.t.u-tokyo.ac.jp).
void rdft(int n, int isgn, FFT_TYPE *a, int *ip, FFT_TYPE *w)
GLenum GLenum GLenum GLenum GLenum scale
void BASE_IMPEXP dft2_real(const CMatrixFloat &in_data, CMatrixFloat &out_real, CMatrixFloat &out_imag)
Compute the 2D Discrete Fourier Transform (DFT) of a real matrix, returning the real and imaginary pa...
#define MRPT_CHECK_NORMAL_NUMBER(val)
double BASE_IMPEXP normalCDF(double p)
Evaluates the Gaussian cumulative density function.
#define THROW_EXCEPTION(msg)
GLdouble GLdouble GLdouble GLdouble q
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
This CStream derived class allow using a file as a read/write binary stream, creating it if the file ...
void BASE_IMPEXP fft_real(CVectorFloat &in_realData, CVectorFloat &out_FFT_Re, CVectorFloat &out_FFT_Im, CVectorFloat &out_FFT_Mag)
Computes the FFT of a 2^N-size vector of real numbers, and returns the Re+Im+Magnitude parts...
#define MRPT_NO_THROWS
C++11 noexcept: Used after member declarations.
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
This file implements miscelaneous matrix and matrix/vector operations, and internal functions in mrpt...
const Scalar * const_iterator
void myGeneralDFT(int sign, const CMatrixFloat &in_real, const CMatrixFloat &in_imag, CMatrixFloat &out_real, CMatrixFloat &out_imag)
void cftbsub(int n, FFT_TYPE *a, FFT_TYPE *w)
GLubyte GLubyte GLubyte GLubyte w
void makect(int nc, int *ip, FFT_TYPE *c)
Copyright(C) 1997 Takuya OOURA (email: ooura@mmm.t.u-tokyo.ac.jp).
T square(const T x)
Inline function for the square of a number.
double BASE_IMPEXP factorial(unsigned int n)
Computes the factorial of an integer number and returns it as a double value (internally it uses loga...
bool BASE_IMPEXP isNaN(float f) MRPT_NO_THROWS
Returns true if the number is NaN.
double BASE_IMPEXP normalQuantile(double p)
Evaluates the Gaussian distribution quantile for the probability value p=[0,1].
double BASE_IMPEXP erfc(const double x)
The complementary error function of a Normal distribution.
void makewt(int nw, int *ip, FFT_TYPE *w)
This base provides a set of functions for maths stuff.
T round2up(T val)
Round up to the nearest power of two of a given number.
void four1(float data[], unsigned long nn, int isign)
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
double BASE_IMPEXP erf(const double x)
The error function of a Normal distribution.
Eigen::Matrix< typename MATRIX::Scalar, MATRIX::ColsAtCompileTime, MATRIX::ColsAtCompileTime > cov(const MATRIX &v)
Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...
double BASE_IMPEXP averageLogLikelihood(const CVectorDouble &logLikelihoods)
A numerically-stable method to compute average likelihood values with strongly different ranges (unwe...
void BASE_IMPEXP cross_correlation_FFT(const CMatrixFloat &A, const CMatrixFloat &B, CMatrixFloat &out_corr)
Correlation of two matrixes using 2D FFT.
void cdft2d(int n1, int n2, int isgn, FFT_TYPE **a, FFT_TYPE *t, int *ip, FFT_TYPE *w)
Copyright(C) 1997 Takuya OOURA (email: ooura@mmm.t.u-tokyo.ac.jp).
CONTAINER::Scalar sum(const CONTAINER &v)
Computes the sum of all the elements.
uint64_t BASE_IMPEXP factorial64(unsigned int n)
Computes the factorial of an integer number and returns it as a 64-bit integer number.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
void dstsub(int n, FFT_TYPE *a, int nc, FFT_TYPE *c)
CONTAINER::Scalar maximum(const CONTAINER &v)
void rftfsub(int n, FFT_TYPE *a, int nc, FFT_TYPE *c)
GLsizei const GLchar ** string
T wrapToPi(T a)
Modifies the given angle to translate it into the ]-pi,pi] range.
void realft(float data[], unsigned long n)
int sign(T x)
Returns the sign of X as "1" or "-1".
void BASE_IMPEXP idft2_complex(const CMatrixFloat &in_real, const CMatrixFloat &in_imag, CMatrixFloat &out_real, CMatrixFloat &out_imag)
Compute the 2D inverse Discrete Fourier Transform (DFT).
void noncentralChi2OneIteration(T arg, T &lans, T &dans, T &pans, unsigned int &j)
bool BASE_IMPEXP isFinite(float f) MRPT_NO_THROWS
Returns true if the number is non infinity.
unsigned __int64 uint64_t
void cftfsub(int n, FFT_TYPE *a, FFT_TYPE *w)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void BASE_IMPEXP idft2_real(const CMatrixFloat &in_real, const CMatrixFloat &in_imag, CMatrixFloat &out_data)
Compute the 2D inverse Discrete Fourier Transform (DFT)
double BASE_IMPEXP chi2inv(double P, unsigned int dim=1)
The "quantile" of the Chi-Square distribution, for dimension "dim" and probability 0<P<1 (the inverse...
GLdouble GLdouble GLdouble r
bool BASE_IMPEXP loadVector(utils::CFileStream &f, std::vector< int > &d)
Loads one row of a text file as a numerical std::vector.
std::pair< double, double > BASE_IMPEXP noncentralChi2PDF_CDF(unsigned int degreesOfFreedom, double noncentrality, double arg, double eps=1e-7)
Returns the 'exact' PDF (first) and CDF (second) of a Non-central chi-squared probability distributio...
A matrix of dynamic size.
void rftbsub(int n, FFT_TYPE *a, int nc, FFT_TYPE *c)
void BASE_IMPEXP medianFilter(const std::vector< double > &inV, std::vector< double > &outV, const int &winSize, const int &numberOfSigmas=2)
bool BASE_IMPEXP isFinite(float v) MRPT_NO_THROWS
double BASE_IMPEXP interpolate2points(const double x, const double x0, const double y0, const double x1, const double y1, bool wrap2pi=false)
Linear interpolation/extrapolation: evaluates at "x" the line (x0,y0)-(x1,y1).
void BASE_IMPEXP dft2_complex(const CMatrixFloat &in_real, const CMatrixFloat &in_imag, CMatrixFloat &out_real, CMatrixFloat &out_imag)
Compute the 2D Discrete Fourier Transform (DFT) of a complex matrix, returning the real and imaginary...
double BASE_IMPEXP normalPDF(double x, double mu, double std)
Evaluates the univariate normal (Gaussian) distribution at a given point "x".
#define ASSERTMSG_(f, __ERROR_MSG)
GLsizei GLsizei GLenum GLenum const GLvoid * data
GLubyte GLubyte GLubyte a
double BASE_IMPEXP chi2PDF(unsigned int degreesOfFreedom, double arg, double accuracy=1e-7)
char BASE_IMPEXP * strtok(char *str, const char *strDelimit, char **context) MRPT_NO_THROWS
An OS-independent method for tokenizing a string.
void dctsub(int n, FFT_TYPE *a, int nc, FFT_TYPE *c)
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
void rdft2d(int n1, int n2, int isgn, FFT_TYPE **a, FFT_TYPE *t, int *ip, FFT_TYPE *w)
Copyright(C) 1997 Takuya OOURA (email: ooura@mmm.t.u-tokyo.ac.jp).
double BASE_IMPEXP noncentralChi2CDF(unsigned int degreesOfFreedom, double noncentrality, double arg)
float FFT_TYPE
Copyright(C) 1997 Takuya OOURA (email: ooura@mmm.t.u-tokyo.ac.jp).