Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
if (n>=12) skew(2,3)=params(12);
}
// fix the translation so that the centre is not moved
ColumnVector ktrans(3);
ktrans = centre - skew.SubMatrix(1,3,1,3)*centre;
skew.SubMatrix(1,3,4,4) = ktrans;
aff = aff * skew * scale;
return 0;
}
float rms_deviation(const Matrix& affmat1, const Matrix& affmat2,
const ColumnVector& centre, const float rmax)
{
Tracer trcr("rms_deviation");
Matrix isodiff(4,4);
try {
isodiff = affmat1*affmat2.i() - Identity(4);
} catch(...) {
cerr << "RMS_DEVIATION ERROR:: Could not invert matrix" << endl;
exit(-5);
}
Matrix adiff(3,3);
adiff = isodiff.SubMatrix(1,3,1,3);
ColumnVector tr(3);
tr = isodiff.SubMatrix(1,3,4,4) + adiff*centre;
float rms = std::sqrt( (tr.t() * tr).AsScalar() +
(rmax*rmax/5.0)*Trace(adiff.t()*adiff) );
return rms;
}
float rms_deviation(const Matrix& affmat1, const Matrix& affmat2,
const float rmax)
{
ColumnVector centre(3);
centre = 0;
return rms_deviation(affmat1,affmat2,centre,rmax);
}
// helper function - calls nifti, but with FSL default case
Mark Jenkinson
committed
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
Matrix Mat44ToNewmat(mat44 m)
{
Matrix r(4,4);
for(unsigned short i = 0; i < 4; ++i)
for(unsigned short j = 0; j < 4; ++j)
r(i+1, j+1) = m.m[i][j];
return r;
}
mat44 NewmatToMat44(const Matrix& m)
{
mat44 r;
for(unsigned short i = 0; i < 4; ++i)
for(unsigned short j = 0; j < 4; ++j)
r.m[i][j] = m(i+1, j+1);
return r;
}
void get_axis_orientations(const Matrix& sform_mat, int sform_code,
const Matrix& qform_mat, int qform_code,
int& icode, int& jcode, int& kcode)
Matrix vox2mm(4,4);
if (sform_code!=NIFTI_XFORM_UNKNOWN) {
vox2mm = sform_mat;
} else if (qform_code!=NIFTI_XFORM_UNKNOWN) {
vox2mm = qform_mat;
} else {
// ideally should be sampling_mat(), but for orientation it doesn't matter
vox2mm = Identity(4);
vox2mm(1,1) = -vox2mm(1,1);
mat44 v2mm;
for (int ii=0; ii<4; ii++) { for (int jj=0; jj<4; jj++) {
v2mm.m[ii][jj] = vox2mm(ii+1,jj+1);
} }
nifti_mat44_to_orientation(v2mm,&icode,&jcode,&kcode);
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
Matrix mat44_to_newmat(mat44 inmat)
{
Matrix retmat(4,4);
for (int ii=0; ii<4; ii++) {
for (int jj=0; jj<4; jj++) {
retmat(ii+1,jj+1) = inmat.m[ii][jj];
}
}
return retmat;
}
mat44 newmat_to_mat44(const Matrix& inmat)
{
mat44 retmat;
for (int ii=0; ii<4; ii++) {
for (int jj=0; jj<4; jj++) {
retmat.m[ii][jj] = inmat(ii+1,jj+1);
}
}
return retmat;
}
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
// Matlab style functions for percentiles, quantiles and median
// AUG 06 CB
ColumnVector seq(const int num)
{
ColumnVector res(num);
for(int ctr =1; ctr<num; ctr++)
res(ctr) = ctr;
return res;
}
float interp1(const ColumnVector& x, const ColumnVector& y, float xi)
// Look-up function for data table defined by x, y
// Returns the values yi at xi using linear interpolation
// Assumes that x is sorted in ascending order
{
float ans;
if(xi >= x.Maximum())
ans = y(x.Nrows());
else
if(xi <= x.Minimum())
ans = y(1);
else{
int ind=1;
while(xi >= x(ind))
ind++;
float xa = x(ind-1), xb = x(ind), ya = y(ind-1), yb = y(ind);
ans = ya + (xi - xa)/(xb - xa) * (yb - ya);
}
return ans;
}
float quantile(const ColumnVector& in, int which)
{
float p;
switch (which)
{
case 0 : p = 0.0; break;
case 1 : p = 25.0; break;
case 2 : p = 50.0; break;
case 3 : p = 75.0; break;
case 4 : p =100.0; break;
default: p = 0.0;
}
return percentile(in,p);
}
float percentile(const ColumnVector& in, float p)
{
ColumnVector y = in;
int num = y.Nrows();
ColumnVector xx,yy,sequence,a(1),b(1),c(1),d(1);
sequence = 100*(seq(num)-0.5)/num; a << y(1); b << y(num); c = 0; d = 100;
xx = (c & sequence & d);
yy = (a & y & b);
return interp1(xx,yy,p);
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
ReturnMatrix quantile(const Matrix& in, int which)
{
int num = in.Ncols();
Matrix res(1,num);
for (int ctr=1; ctr<=num; ctr++){
ColumnVector tmp = in.Column(ctr);
res(1,ctr) = quantile(tmp,which);
}
res.Release();
return res;
}
ReturnMatrix percentile(const Matrix& in, float p)
{
int num = in.Ncols();
Matrix res(1,num);
for (int ctr=1; ctr<=num; ctr++){
ColumnVector tmp = in.Column(ctr);
res(1,ctr) = percentile(tmp,p);
}
res.Release();
return res;
}
void cart2sph(const ColumnVector& dir, float& th, float& ph)
Mark Jenkinson
committed
float mag=sqrt(dir(1)*dir(1)+dir(2)*dir(2)+dir(3)*dir(3));
if(mag==0){
ph=M_PI/2;
th=M_PI/2;
}
else{
if(dir(1)==0 && dir(2)>=0) ph=M_PI/2;
else if(dir(1)==0 && dir(2)<0) ph=-M_PI/2;
Mark Jenkinson
committed
else if(dir(1)>0) ph=atan(dir(2)/dir(1));
else if(dir(2)>0) ph=atan(dir(2)/dir(1))+M_PI;
else ph=atan(dir(2)/dir(1))-M_PI;
Mark Jenkinson
committed
else if(dir(3)>0) th=atan(sqrt(dir(1)*dir(1)+dir(2)*dir(2))/dir(3));
else th=atan(sqrt(dir(1)*dir(1)+dir(2)*dir(2))/dir(3))+M_PI;
}
}
void cart2sph(const Matrix& dir,ColumnVector& th,ColumnVector& ph)
{
if(th.Nrows()!=dir.Ncols()){
th.ReSize(dir.Ncols());
}
if(ph.Nrows()!=dir.Ncols()){
ph.ReSize(dir.Ncols());
}
Mark Jenkinson
committed
float mag=sqrt(dir(1,i)*dir(1,i)+dir(2,i)*dir(2,i)+dir(3,i)*dir(3,i));
if(mag==0){
ph(i)=M_PI/2;
th(i)=M_PI/2;
}
else{
if(dir(1,i)==0 && dir(2,i)>=0) ph(i)=M_PI/2;
else if(dir(1,i)==0 && dir(2,i)<0) ph(i)=-M_PI/2;
Mark Jenkinson
committed
else if(dir(1,i)>0) ph(i)=atan(dir(2,i)/dir(1,i));
else if(dir(2,i)>0) ph(i)=atan(dir(2,i)/dir(1,i))+M_PI;
else ph(i)=atan(dir(2,i)/dir(1,i))-M_PI;
Mark Jenkinson
committed
else if(dir(3,i)>0) th(i)=atan(sqrt(dir(1,i)*dir(1,i)+dir(2,i)*dir(2,i))/dir(3,i));
else th(i)=atan(sqrt(dir(1,i)*dir(1,i)+dir(2,i)*dir(2,i))/dir(3,i))+M_PI;
// added by SJ
void cart2sph(const vector<ColumnVector>& dir,ColumnVector& th,ColumnVector& ph)
{
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
ph.ReSize(dir.size());
}
//double _2pi=2*M_PI;
double _pi2=M_PI/2;
int j=1;
for (unsigned int i=0;i<dir.size();i++) {
float mag=std::sqrt(dir[i](1)*dir[i](1)+dir[i](2)*dir[i](2)+dir[i](3)*dir[i](3));
if(mag==0){
ph(j)=_pi2;
th(j)=_pi2;
}
else{
if(dir[i](1)==0 && dir[i](2)>=0) ph(j)=_pi2;
else if(dir[i](1)==0 && dir[i](2)<0) ph(j)=-_pi2;
else if(dir[i](1)>0) ph(j)=std::atan(dir[i](2)/dir[i](1));
else if(dir[i](2)>0) ph(j)=std::atan(dir[i](2)/dir[i](1))+M_PI;
else ph(j)=std::atan(dir[i](2)/dir[i](1))-M_PI;
//ph(j)=fmod(ph(j),_2pi);
if(dir[i](3)==0) th(j)=_pi2;
else if(dir[i](3)>0) th(j)=std::atan(std::sqrt(dir[i](1)*dir[i](1)+dir[i](2)*dir[i](2))/dir[i](3));
else th(j)=std::atan(std::sqrt(dir[i](1)*dir[i](1)+dir[i](2)*dir[i](2))/dir[i](3))+M_PI;
//th(j)=fmod(th(j),M_PI);
}
j++;
}
}
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
// Added by CFB --- Matlab style Matrix functions
ReturnMatrix ones(const int dim1, const int dim2)
{
int tdim = dim2;
if(tdim<0){tdim=dim1;}
Matrix res(dim1,tdim); res = 1.0;
res.Release();
return res;
}
ReturnMatrix zeros(const int dim1, const int dim2)
{
int tdim = dim2;
if(tdim<0){tdim=dim1;}
Matrix res(dim1,tdim); res = 0.0;
res.Release();
return res;
}
ReturnMatrix repmat(const Matrix &mat, const int rows, const int cols)
{
Matrix res = mat;
for(int ctr = 1; ctr < cols; ctr++){res |= mat;}
Matrix tmpres = res;
Mark Jenkinson
committed
for(int ctr = 1; ctr < rows; ctr++){res &= tmpres;}
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
res.Release();
return res;
}
ReturnMatrix dist2(const Matrix &mat1, const Matrix &mat2)
{
Matrix res(mat1.Ncols(),mat2.Ncols());
for(int ctr1 = 1; ctr1 <= mat1.Ncols(); ctr1++)
for(int ctr2 =1; ctr2 <= mat2.Ncols(); ctr2++)
{
ColumnVector tmp;
tmp=mat1.Column(ctr1)-mat2.Column(ctr2);
res(ctr1,ctr2) = std::sqrt(tmp.SumSquare());
}
res.Release();
return res;
}
ReturnMatrix abs(const Matrix& mat)
{
Matrix res = mat;
for (int mc=1; mc<=mat.Ncols(); mc++) {
for (int mr=1; mr<=mat.Nrows(); mr++) {
res(mr,mc)=std::abs(res(mr,mc));
}
}
res.Release();
return res;
}
ReturnMatrix sqrt(const Matrix& mat)
{
Matrix res = mat;
bool neg_flag = false;
for (int mc=1; mc<=mat.Ncols(); mc++) {
for (int mr=1; mr<=mat.Nrows(); mr++) {
if(res(mr,mc)<0){ neg_flag = true; }
res(mr,mc)=std::sqrt(std::abs(res(mr,mc)));
}
}
if(neg_flag){
//cerr << " Matrix contained negative elements" << endl;
//cerr << " return sqrt(abs(X)) instead" << endl;
}
res.Release();
return res;
}
ReturnMatrix log(const Matrix& mat)
{
Matrix res = mat;
bool neg_flag = false;
for (int mc=1; mc<=mat.Ncols(); mc++) {
for (int mr=1; mr<=mat.Nrows(); mr++) {
if(res(mr,mc)<0){ neg_flag = true; }
res(mr,mc)=std::log(std::abs(res(mr,mc)));
}
}
if(neg_flag){
// cerr << " Matrix contained negative elements" << endl;
// cerr << " return log(abs(X)) instead" << endl;
}
res.Release();
return res;
}
ReturnMatrix exp(const Matrix& mat)
{
Matrix res = mat;
for (int mc=1; mc<=mat.Ncols(); mc++) {
for (int mr=1; mr<=mat.Nrows(); mr++) {
res(mr,mc)=std::exp(res(mr,mc));
}
}
res.Release();
return res;
}
ReturnMatrix tanh(const Matrix& mat)
{
Matrix res = mat;
for (int mc=1; mc<=mat.Ncols(); mc++) {
for (int mr=1; mr<=mat.Nrows(); mr++) {
res(mr,mc)=std::tanh(res(mr,mc));
}
}
res.Release();
return res;
}
ReturnMatrix pow(const Matrix& mat, const double exp)
{
Matrix res = mat;
for (int mc=1; mc<=mat.Ncols(); mc++) {
for (int mr=1; mr<=mat.Nrows(); mr++) {
res(mr,mc)=std::pow(res(mr,mc),exp);
}
}
res.Release();
return res;
}
ReturnMatrix max(const Matrix& mat)
{
Matrix res;
if(mat.Nrows()>1){
res=zeros(1,mat.Ncols());
res=mat.Row(1);
for(int mc=1; mc<=mat.Ncols();mc++){
for(int mr=2; mr<=mat.Nrows();mr++){
if(mat(mr,mc)>res(1,mc)){res(1,mc)=mat(mr,mc);}
}
}
}
else{
res=zeros(1);
res=mat(1,1);
for(int mc=2; mc<=mat.Ncols(); mc++){
if(mat(1,mc)>res(1,1)){res(1,1)=mat(1,mc);}
}
}
res.Release();
return res;
}
ReturnMatrix max(const Matrix& mat,ColumnVector& index)
{
index.ReSize(mat.Nrows());
index=1;
Matrix res;
if(mat.Nrows()>1){
res=zeros(1,mat.Ncols());
res=mat.Row(1);
for(int mc=1; mc<=mat.Ncols();mc++){
for(int mr=2; mr<=mat.Nrows();mr++){
if(mat(mr,mc)>res(1,mc))
{
res(1,mc)=mat(mr,mc);
index(mr)=mc;
}
}
}
}
else{
res=zeros(1);
res=mat(1,1);
for(int mc=2; mc<=mat.Ncols(); mc++){
if(mat(1,mc)>res(1,1))
{
res(1,1)=mat(1,mc);
index(1)=mc;
}
}
}
res.Release();
return res;
}
ReturnMatrix min(const Matrix& mat)
{
Matrix res;
if(mat.Nrows()>1){
res=zeros(1,mat.Ncols());
res=mat.Row(1);
for(int mc=1; mc<=mat.Ncols();mc++){
for(int mr=2; mr<=mat.Nrows();mr++){
if(mat(mr,mc)<res(1,mc)){res(1,mc)=mat(mr,mc);}
}
}
}
else{
res=zeros(1);
res=mat(1,1);
for(int mc=2; mc<=mat.Ncols(); mc++){
if(mat(1,mc)<res(1,1)){res(1,1)=mat(1,mc);}
}
}
res.Release();
return res;
}
ReturnMatrix sum(const Matrix& mat, const int dim)
{
Matrix tmp;
if (dim == 1) {tmp=mat;}
else {tmp=mat.t();}
Matrix res(1,tmp.Ncols());
res = 0.0;
for (int mc=1; mc<=tmp.Ncols(); mc++) {
for (int mr=1; mr<=tmp.Nrows(); mr++) {
res(1,mc) += tmp(mr,mc);
}
}
if (!(dim == 1)) {res=res.t();}
res.Release();
return res;
}
ReturnMatrix mean(const Matrix& mat, const int dim)
{
Matrix tmp;
if (dim == 1) {tmp=mat;}
else {tmp=mat.t();}
int N = tmp.Nrows();
Matrix res(1,tmp.Ncols());
res = 0.0;
for (int mc=1; mc<=tmp.Ncols(); mc++) {
for (int mr=1; mr<=tmp.Nrows(); mr++) {
res(1,mc) += tmp(mr,mc)/N;
}
}
if (!(dim == 1)) {res=res.t();}
res.Release();
return res;
}
ReturnMatrix var(const Matrix& mat, const int dim)
{
Matrix tmp;
if (dim == 1) {tmp=mat;}
else {tmp=mat.t();}
int N = tmp.Nrows();
Matrix res(1,tmp.Ncols());
res = 0.0;
if(N>1){
tmp -= ones(tmp.Nrows(),1)*mean(tmp,1);
for (int mc=1; mc<=tmp.Ncols(); mc++)
for (int mr=1; mr<=tmp.Nrows(); mr++)
res(1,mc) += tmp(mr,mc) / (N-1) * tmp(mr,mc);
}
if (!(dim == 1)) {res=res.t();}
res.Release();
return res;
}
ReturnMatrix stdev(const Matrix& mat, const int dim)
{
return sqrt(var(mat,dim));
}
ReturnMatrix gt(const Matrix& mat1,const Matrix& mat2)
{
Mark Jenkinson
committed
int ctrcol = std::min(mat1.Ncols(),mat2.Ncols());
int ctrrow = std::min(mat1.Nrows(),mat2.Nrows());
Matrix res(ctrrow,ctrcol);
res=0.0;
for (int ctr1 = 1; ctr1 <= ctrrow; ctr1++) {
for (int ctr2 =1; ctr2 <= ctrcol; ctr2++) {
if( mat1(ctr1,ctr2) > mat2(ctr1,ctr2)){
res(ctr1,ctr2) = 1.0;
}
}
}
res.Release();
return res;
}
ReturnMatrix lt(const Matrix& mat1,const Matrix& mat2)
{
Mark Jenkinson
committed
int ctrcol = std::min(mat1.Ncols(),mat2.Ncols());
int ctrrow = std::min(mat1.Nrows(),mat2.Nrows());
Matrix res(ctrrow,ctrcol);
res=0.0;
for (int ctr1 = 1; ctr1 <= ctrrow; ctr1++) {
for (int ctr2 =1; ctr2 <= ctrcol; ctr2++) {
if( mat1(ctr1,ctr2) < mat2(ctr1,ctr2)){
res(ctr1,ctr2) = 1.0;
}
}
}
res.Release();
return res;
}
ReturnMatrix geqt(const Matrix& mat1,const Matrix& mat2)
{
Mark Jenkinson
committed
int ctrcol = std::min(mat1.Ncols(),mat2.Ncols());
int ctrrow = std::min(mat1.Nrows(),mat2.Nrows());
Matrix res(ctrrow,ctrcol);
res=0.0;
for (int ctr1 = 1; ctr1 <= ctrrow; ctr1++) {
for (int ctr2 =1; ctr2 <= ctrcol; ctr2++) {
if( mat1(ctr1,ctr2) >= mat2(ctr1,ctr2)){
res(ctr1,ctr2) = 1.0;
}
}
}
res.Release();
return res;
}
ReturnMatrix geqt(const Matrix& mat,const float a)
{
int ncols = mat.Ncols();
int nrows = mat.Nrows();
Matrix res(nrows,ncols);
res=0.0;
for (int ctr1 = 1; ctr1 <= nrows; ctr1++) {
for (int ctr2 =1; ctr2 <= ncols; ctr2++) {
if( mat(ctr1,ctr2) >= a){
res(ctr1,ctr2) = 1.0;
}
}
}
res.Release();
return res;
}
ReturnMatrix leqt(const Matrix& mat1,const Matrix& mat2)
{
Mark Jenkinson
committed
int ctrcol = std::min(mat1.Ncols(),mat2.Ncols());
int ctrrow = std::min(mat1.Nrows(),mat2.Nrows());
Matrix res(ctrrow,ctrcol);
res=0.0;
for (int ctr1 = 1; ctr1 <= ctrrow; ctr1++) {
for (int ctr2 =1; ctr2 <= ctrcol; ctr2++) {
if( mat1(ctr1,ctr2) <= mat2(ctr1,ctr2)){
res(ctr1,ctr2) = 1.0;
}
}
}
res.Release();
return res;
}
ReturnMatrix eq(const Matrix& mat1,const Matrix& mat2)
{
Mark Jenkinson
committed
int ctrcol = std::min(mat1.Ncols(),mat2.Ncols());
int ctrrow = std::min(mat1.Nrows(),mat2.Nrows());
Matrix res(ctrrow,ctrcol);
res=0.0;
for (int ctr1 = 1; ctr1 <= ctrrow; ctr1++) {
for (int ctr2 =1; ctr2 <= ctrcol; ctr2++) {
if( mat1(ctr1,ctr2) == mat2(ctr1,ctr2)){
res(ctr1,ctr2) = 1.0;
}
}
}
res.Release();
return res;
}
ReturnMatrix neq(const Matrix& mat1,const Matrix& mat2)
{
Mark Jenkinson
committed
int ctrcol = std::min(mat1.Ncols(),mat2.Ncols());
int ctrrow = std::min(mat1.Nrows(),mat2.Nrows());
Matrix res(ctrrow,ctrcol);
res=0.0;
for (int ctr1 = 1; ctr1 <= ctrrow; ctr1++) {
for (int ctr2 =1; ctr2 <= ctrcol; ctr2++) {
if( mat1(ctr1,ctr2) != mat2(ctr1,ctr2)){
res(ctr1,ctr2) = 1.0;
}
}
}
res.Release();
return res;
}
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
ReturnMatrix SD(const Matrix& mat1,const Matrix& mat2)
{
if((mat1.Nrows() != mat2.Nrows()) ||
(mat1.Ncols() != mat2.Ncols()) ){
cerr <<"MISCMATHS::SD - matrices are of different dimensions"<<endl;
exit(-1);
}
Matrix ret(mat1.Nrows(),mat1.Ncols());
for (int r = 1; r <= mat1.Nrows(); r++) {
for (int c =1; c <= mat1.Ncols(); c++) {
if( mat2(r,c)==0)
ret(r,c)=0;
else
ret(r,c) = mat1(r,c)/mat2(r,c);
}
}
ret.Release();
return ret;
}
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
ReturnMatrix vox_to_vox(const ColumnVector& xyz1,const ColumnVector& dims1,const ColumnVector& dims2,const Matrix& xfm){
ColumnVector xyz1_mm(4),xyz2_mm,xyz2(3);
xyz1_mm<<xyz1(1)*dims1(1)<<xyz1(2)*dims1(2)<<xyz1(3)*dims1(3)<<1;
xyz2_mm=xfm*xyz1_mm;
xyz2_mm=xyz2_mm/xyz2_mm(4);
xyz2<<xyz2_mm(1)/dims2(1)<<xyz2_mm(2)/dims2(2)<<xyz2_mm(3)/dims2(3);
xyz2.Release();
return xyz2;
}
ReturnMatrix mni_to_imgvox(const ColumnVector& mni,const ColumnVector& mni_origin,const Matrix& mni2img, const ColumnVector& img_dims){
ColumnVector mni_new_origin(4),img_mm;//homogeneous
ColumnVector img_vox(3);
mni_new_origin<<mni(1)+mni_origin(1)<<mni(2)+mni_origin(2)<<mni(3)+mni_origin(3)<<1;
img_mm=mni2img*mni_new_origin;
img_vox<<img_mm(1)/img_dims(1)<<img_mm(2)/img_dims(2)<<img_mm(3)/img_dims(3);
img_vox.Release();
return img_vox;
}
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
ReturnMatrix remmean(const Matrix& mat, const int dim)
{
Matrix res;
if (dim == 1) {res=mat;}
else {res=mat.t();}
Matrix Mean;
Mean = mean(res);
for (int ctr = 1; ctr <= res.Nrows(); ctr++) {
for (int ctr2 =1; ctr2 <= res.Ncols(); ctr2++) {
res(ctr,ctr2)-=Mean(1,ctr2);
}
}
if (dim != 1) {res=res.t();}
res.Release();
return res;
}
void remmean(const Matrix& mat, Matrix& demeanedmat, Matrix& Mean, const int dim)
{
if (dim == 1) {demeanedmat=mat;}
else {demeanedmat=mat.t();}
Mean = mean(demeanedmat);
for (int ctr = 1; ctr <= demeanedmat.Nrows(); ctr++) {
for (int ctr2 =1; ctr2 <= demeanedmat.Ncols(); ctr2++) {
demeanedmat(ctr,ctr2)-=Mean(1,ctr2);
}
}
if (dim != 1){demeanedmat = demeanedmat.t();Mean = Mean.t();}
}
ReturnMatrix cov(const Matrix& mat, const int norm)
{
SymmetricMatrix res;
Matrix tmp;
int N;
tmp=remmean(mat);
if (norm == 1) {N = mat.Nrows();}
else {N = mat.Nrows()-1;}
res << tmp.t()*tmp;
res = res/N;
res.Release();
return res;
}
ReturnMatrix corrcoef(const Matrix& mat, const int norm)
{
SymmetricMatrix res;
SymmetricMatrix C;
C = cov(mat,norm);
Matrix D;
D=diag(C);
D=pow(sqrt(D*D.t()),-1);
res << SP(C,D);
res.Release();
return res;
}
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
ReturnMatrix flipud(const Matrix& mat)
{
Matrix rmat(mat.Nrows(),mat.Ncols());
for(int j=1;j<=mat.Ncols();j++)
for(int i=1;i<=mat.Nrows();i++)
rmat(i,j)=mat(mat.Nrows()-i+1,j);
rmat.Release();
return rmat;
}
ReturnMatrix fliplr(const Matrix& mat)
{
Matrix rmat(mat.Nrows(),mat.Ncols());
for(int j=1;j<=mat.Ncols();j++)
for(int i=1;i<=mat.Nrows();i++)
rmat(i,j)=mat(i,mat.Ncols()-j+1);
rmat.Release();
return rmat;
}
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
void symm_orth(Matrix &Mat)
{
SymmetricMatrix Metric;
Metric << Mat.t()*Mat;
Metric = Metric.i();
Matrix tmpE;
DiagonalMatrix tmpD;
EigenValues(Metric,tmpD,tmpE);
Mat = Mat * tmpE * sqrt(abs(tmpD)) * tmpE.t();
}
void powerspectrum(const Matrix &Mat1, Matrix &Result, bool useLog)
//calculates the powerspectrum for every column of Mat1
{
Matrix res;
for(int ctr=1; ctr <= Mat1.Ncols(); ctr++)
{
ColumnVector tmpCol;
tmpCol=Mat1.Column(ctr);
ColumnVector FtmpCol_real;
ColumnVector FtmpCol_imag;
ColumnVector tmpPow;
RealFFT(tmpCol,FtmpCol_real,FtmpCol_imag);
tmpPow = pow(FtmpCol_real,2)+pow(FtmpCol_imag,2);
tmpPow = tmpPow.Rows(2,tmpPow.Nrows());
if(useLog){tmpPow = log(tmpPow);}
if(res.Storage()==0){res= tmpPow;}else{res|=tmpPow;}
}
Result=res;
}
void element_mod_n(Matrix& Mat,double n)
{
//represent each element in modulo n (useful for wrapping phases (n=2*M_PI))
double tmp;
for( int j=1;j<=Mat.Ncols();j++){
tmp = ( Mat(i,j) - rounddouble(Mat(i,j)/n)*n );
Mat(i,j)= tmp > 0 ? tmp : tmp + n;
}
}
}
}
Mark Jenkinson
committed
return (int)pow(2,ceil(log(float(n))/log(float(2))));
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
}
void xcorr(const Matrix& p_ts, Matrix& ret, int lag, int p_zeropad)
{
Tracer tr("MISCMATHS::xcorr");
int sizeTS = p_ts.Nrows();
int numTS = p_ts.Ncols();
if(p_zeropad == 0)
p_zeropad = sizeTS;
if(lag == 0)
lag = sizeTS;
ColumnVector x(p_zeropad);
x = 0;
ColumnVector fft_real;
ColumnVector fft_im;
ColumnVector dummy(p_zeropad);
ColumnVector dummy2;
dummy = 0;
ColumnVector realifft(p_zeropad);
ret.ReSize(lag,numTS);
ret = 0;
for(int i = 1; i <= numTS; i++)
{
x.Rows(1,sizeTS) = p_ts.Column(i);
FFT(x, dummy, fft_real, fft_im);
for(int j = 1; j <= p_zeropad; j++)
{
// (x+iy)(x-iy) = x^2 + y^2
fft_real(j) = fft_real(j)*fft_real(j) + fft_im(j)*fft_im(j);
fft_im(j) = 0;
}
FFTI(fft_real, fft_im, realifft, dummy2);
float varx = var(x.Rows(1,sizeTS)).AsScalar();
ret.Column(i) = realifft.Rows(1,lag);
Mark Jenkinson
committed
for(int j = 1; j <= lag-1; j++)
{
// Correction to make autocorr unbiased and normalised
Mark Jenkinson
committed
ret(j,i) = ret(j,i)/((sizeTS-j)*varx);
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
}
}
}
ReturnMatrix xcorr(const Matrix& p_ts, int lag, int p_zeropad )
{
Matrix r;
xcorr(p_ts,r,lag,p_zeropad);
r.Release();
return r;
}
void detrend(Matrix& p_ts, int p_level)
{
Tracer trace("MISCMATHS::detrend");
int sizeTS = p_ts.Nrows();
// p_ts = b*a + e (OLS regression)
// e is detrended data
Matrix a(sizeTS, p_level+1);
// Create a
for(int t = 1; t <= sizeTS; t++)
{
for(int l = 0; l <= p_level; l++)
Mark Jenkinson
committed
a(t,l+1) = pow((float)t/sizeTS,l);
}
// Form residual forming matrix R:
Matrix R = Identity(sizeTS)-a*pinv(a);
Mark Jenkinson
committed
for(int t = 1; t <= sizeTS; t++)
Mark Jenkinson
committed
p_ts.Column(t) = R*p_ts.Column(t);
}
}
ReturnMatrix read_vest(string p_fname)
{
ifstream in;
in.open(p_fname.c_str(), ios::in);
if(!in) throw Exception(string("Unable to open "+p_fname).c_str());
int numWaves = 0;
int numPoints = 0;
string str;
while(true)
{
if(!in.good()) throw Exception(string(p_fname+" is not a valid vest file").c_str());