root/library/bdm/base/itpp/itmex.h @ 812

Revision 812, 27.5 kB (checked in by smidl, 14 years ago)

ITPP included into BDM


Line 
1/*!
2 * \file
3 * \brief Conversion routines between IT++ and Matlab
4 * \author Tony Ottosson and Pal Frenger
5 *
6 * -------------------------------------------------------------------------
7 *
8 * IT++ - C++ library of mathematical, signal processing, speech processing,
9 *        and communications classes and functions
10 *
11 * Copyright (C) 1995-2010  (see AUTHORS file for a list of contributors)
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 *
27 * -------------------------------------------------------------------------
28 */
29
30#ifndef ITMEX_H
31#define ITMEX_H
32
33#include <itpp/itbase.h>
34#include <mex.h>
35
36
37namespace itpp
38{
39
40//--------------------------------------------------------
41// mex -> it++
42//--------------------------------------------------------
43
44/*!
45  \addtogroup mexfiles
46  \brief Conversions between IT++ and Matlab for writing mex-files.
47  \author Tony Ottosson and Pal Frenger
48
49  These routines are used to help writng mex-files for speeding up
50  matlab simulations.
51  A simple mex-file that performs QPSK modulation is given below.
52
53  \code
54  #include <itpp/itcomm.h>
55  #include <itpp/itmex.h>
56
57  using namespace itpp;
58
59  void mexFunction(int n_output, mxArray *output[], int n_input, const mxArray *input[])
60  {
61  // Check the number of inputs and output arguments
62  if(n_output!=1) mexErrMsgTxt("Wrong number of output variables!");
63  if(n_input!=1) mexErrMsgTxt("Wrong number of input variables!");
64
65  // Convert input variables to IT++ format
66  bvec input_bits = mxArray2bvec(input[0]);
67
68  // ------------------ Start of routine ---------------------------
69  cvec output_symbols;
70  QPSK qpsk;
71
72  output_symbols = qpsk.modulate_bits(input_bits);
73  // ------------------ End of routine -----------------------------
74
75  // Create output vectors
76  output[0] = mxCreateDoubleMatrix(1,output_symbols.size(), mxCOMPLEX);
77
78  // Convert the IT++ format to Matlab format for output
79  cvec2mxArray(output_symbols, output[0]);
80  }
81  \endcode
82*/
83
84/*!
85 * \addtogroup mexfiles
86 * @{
87 */
88
89// --------------------------------------------------------
90// mex -> IT++
91// --------------------------------------------------------
92
93//! Convert the matlab-format mxArray to bin
94bin mxArray2bin(const mxArray *in);
95//! Convert the matlab-format mxArray to short
96short mxArray2short(const mxArray *in);
97//! Convert the matlab-format mxArray to int
98int mxArray2int(const mxArray *in);
99//! Convert the matlab-format mxArray to double
100double mxArray2double(const mxArray *in);
101//! Convert the matlab-format mxArray to complex<double>
102std::complex<double> mxArray2double_complex(const mxArray *in);
103//! Convert the matlab-format mxArray to string
104std::string mxArray2string(const mxArray *in);
105
106//! Convert the matlab-format mxArray to bvec
107bvec mxArray2bvec(const mxArray *in);
108//! Convert the matlab-format mxArray to svec
109svec mxArray2svec(const mxArray *in);
110//! Convert the matlab-format mxArray to ivec
111ivec mxArray2ivec(const mxArray *in);
112//! Convert the matlab-format mxArray to vec
113vec mxArray2vec(const mxArray *in);
114//! Convert the matlab-format mxArray to cvec
115cvec mxArray2cvec(const mxArray *in);
116
117//! Convert the matlab-format mxArray to bmat
118bmat mxArray2bmat(const mxArray *in);
119//! Convert the matlab-format mxArray to smat
120smat mxArray2smat(const mxArray *in);
121//! Convert the matlab-format mxArray to imat
122imat mxArray2imat(const mxArray *in);
123//! Convert the matlab-format mxArray to mat
124mat mxArray2mat(const mxArray *in);
125//! Convert the matlab-format mxArray to cmat
126cmat mxArray2cmat(const mxArray *in);
127
128// --------------------------------------------------------
129// IT++ -> mex
130// --------------------------------------------------------
131
132//! Convert bin to the matlab-format mxArray
133void bin2mxArray(const bin &in, mxArray *out);
134//! Convert short to the matlab-format mxArray
135void short2mxArray(const short &in, mxArray *out);
136//! Convert int to the matlab-format mxArray
137void int2mxArray(const int &in, mxArray *out);
138//! Convert double to the matlab-format mxArray
139void double2mxArray(const double &in, mxArray *out);
140//! Convert complex<double> to the matlab-format mxArray
141void double_complex2mxArray(const std::complex<double> &in, mxArray *out);
142//! Convert string to the matlab-format mxArray
143void string2mxArray(const std::string &in, mxArray* &out);
144
145//! Convert bvec to the matlab-format mxArray
146void bvec2mxArray(const bvec &in, mxArray *out);
147//! Convert svec to the matlab-format mxArray
148void svec2mxArray(const svec &in, mxArray *out);
149//! Convert ivec to the matlab-format mxArray
150void ivec2mxArray(const ivec &in, mxArray *out);
151//! Convert vec to the matlab-format mxArray
152void vec2mxArray(const vec &in, mxArray *out);
153//! Convert cvec to the matlab-format mxArray
154void cvec2mxArray(const cvec &in, mxArray *out);
155
156//! Convert bmat to the matlab-format mxArray
157void bmat2mxArray(const bmat &in, mxArray *out);
158//! Convert smat to the matlab-format mxArray
159void smat2mxArray(const smat &in, mxArray *out);
160//! Convert imat to the matlab-format mxArray
161void imat2mxArray(const imat &in, mxArray *out);
162//! Convert mat to the matlab-format mxArray
163void mat2mxArray(const mat &in, mxArray *out);
164//! Convert cmat to the matlab-format mxArray
165void cmat2mxArray(const cmat &in, mxArray *out);
166
167// --------------------------------------------------------
168// mex -> C
169// --------------------------------------------------------
170
171//! Convert the matlab-format mxArray to C-format pointer to short
172void mxArray2Csvec(const mxArray *in, short *out);
173//! Convert the matlab-format mxArray to C-format pointer to int
174void mxArray2Civec(const mxArray *in, int *out);
175//! Convert the matlab-format mxArray to C-format pointer to double
176void mxArray2Cvec(const mxArray *in, double *out);
177//! Convert the matlab-format mxArray to C-format pointers to double (real and imaginary parts)
178void mxArray2Ccvec(const mxArray *in, double *out_real, double *out_imag);
179
180//! Convert the matlab-format mxArray to C-format pointer to pointer to short
181void mxArray2Csmat(const mxArray *in, short **out);
182//! Convert the matlab-format mxArray to C-format pointer to pointer to int
183void mxArray2Cimat(const mxArray *in, int **out);
184//! Convert the matlab-format mxArray to C-format pointer to pointer to double
185void mxArray2Cmat(const mxArray *in, double **out);
186//! Convert the matlab-format mxArray to C-format pointer to pointer to double (real and imaginary parts)
187void mxArray2Ccmat(const mxArray *in, double **out_real, double **out_imag);
188
189// --------------------------------------------------------
190// C -> mex
191// --------------------------------------------------------
192
193//! Convert C-format pointer to short to matlab-format mxArray
194void Csvec2mxArray(short *in, mxArray *out);
195//! Convert C-format pointer to int to matlab-format mxArray
196void Civec2mxArray(int *in, mxArray *out);
197//! Convert C-format pointer to double to matlab-format mxArray
198void Cvec2mxArray(double *in, mxArray *out);
199//! Convert C-format pointers to double (real and imaginary parts) to matlab-format mxArray
200void Ccvec2mxArray(double *in_real, double *in_imag, mxArray *out);
201
202//! Convert C-format pointer to pointer to short to matlab-format mxArray
203void Csmat2mxArray(short **in, mxArray *out);
204//! Convert C-format pointer to pointer to int to matlab-format mxArray
205void Cimat2mxArray(int **in, mxArray *out);
206//! Convert C-format pointer to pointer to double to matlab-format mxArray
207void Cmat2mxArray(double **in, mxArray *out);
208//! Convert C-format pointer to pointer to double (real and imaginary parts) to matlab-format mxArray
209void Ccmat2mxArray(double **in_real, double **in_imag, mxArray *out);
210
211/*!
212 * @}
213 */
214
215
216bin mxArray2bin(const mxArray *in)
217{
218  int size;
219  double* temp = (double*) mxGetPr(in);
220  if (temp == 0) mexErrMsgTxt("mxArray2bin: Pointer to data is NULL");
221  size = mxGetNumberOfElements(in);
222  if (size != 1) mexErrMsgTxt("mxArray2bin: Size of data is not equal to one");
223
224  return (((*temp) > 0.0) ? bin(1) : bin(0));
225}
226
227short mxArray2short(const mxArray *in)
228{
229  int size;
230  double* temp = (double*) mxGetPr(in);
231  if (temp == 0) mexErrMsgTxt("mxArray2short: Pointer to data is NULL");
232  size = mxGetNumberOfElements(in);
233  if (size != 1) mexErrMsgTxt("mxArray2short: Size of data is not equal to one");
234
235  return (short)(*temp);
236}
237
238int mxArray2int(const mxArray *in)
239{
240  int size;
241  double* temp = (double*) mxGetPr(in);
242  if (temp == 0) mexErrMsgTxt("mxArray2int: Pointer to data is NULL");
243  size = mxGetNumberOfElements(in);
244  if (size != 1) mexErrMsgTxt("mxArray2int: Size of data is not equal to one");
245
246  return (int)(*temp);
247}
248
249double mxArray2double(const mxArray *in)
250{
251  int size;
252  double* temp = (double*) mxGetPr(in);
253  if (temp == 0) mexErrMsgTxt("mxArray2double: Pointer to data is NULL");
254  size = mxGetNumberOfElements(in);
255  if (size != 1) mexErrMsgTxt("mxArray2double: Size of data is not equal to one");
256
257  return (*temp);
258}
259
260std::complex<double> mxArray2double_complex(const mxArray *in)
261{
262  int size;
263  double* tempR = (double*) mxGetPr(in);
264  double* tempI = (double*) mxGetPi(in);
265
266  if ((tempR == 0) && (tempI == 0)) mexErrMsgTxt("mxArray2double_complex: Pointer to data is NULL");
267
268  size = mxGetNumberOfElements(in);
269  if (size != 1) mexErrMsgTxt("mxArray2double_complex: Size of data is not equal to one");
270
271  if (tempR == 0) {
272    return std::complex<double>(0.0 , (*tempI));
273  }
274  else if (tempI == 0) {
275    return std::complex<double>((*tempR), 0.0);
276  }
277  else {
278    return std::complex<double>((*tempR), (*tempI));
279  }
280
281}
282
283std::string mxArray2string(const mxArray *in)
284{
285  if (in == 0)
286    mexErrMsgTxt("mxArray2string: Pointer to data is NULL");
287  std::string str = mxArrayToString(in);
288  if (str.data() == 0)
289    mexErrMsgTxt("mxArray2string: Could not convert mxArray to string");
290  return str;
291}
292
293bvec mxArray2bvec(const mxArray *in)
294{
295  bvec out;
296  int i, size;
297  double* temp = (double*) mxGetPr(in);
298  if (temp == 0) mexErrMsgTxt("mxArray2bvec: Pointer to data is NULL");
299
300  size = mxGetNumberOfElements(in);
301  if (size == 0) mexErrMsgTxt("mxArray2bvec: Size of data is zero");
302
303  out.set_size(size, false);
304
305  for (i = 0; i < size; i++) {
306    out(i) = (((*temp++) > 1e-5) ? bin(1) : bin(0));
307  }
308
309  return out;
310
311}
312
313svec mxArray2svec(const mxArray *in)
314{
315  svec out;
316  int i, size;
317  double* temp = (double*) mxGetPr(in);
318  if (temp == 0) mexErrMsgTxt("mxArray2svec: Pointer to data is NULL");
319
320  size = mxGetNumberOfElements(in);
321  if (size == 0) mexErrMsgTxt("mxArray2svec: Size of data is zero");
322
323  out.set_size(size, false);
324
325  for (i = 0; i < size; i++) {
326    out(i) = (short)(*temp++);
327  }
328
329  return out;
330
331}
332
333ivec mxArray2ivec(const mxArray *in)
334{
335  ivec out;
336  int i, size;
337  double* temp = (double*) mxGetPr(in);
338  if (temp == 0) mexErrMsgTxt("mxArray2ivec: Pointer to data is NULL");
339
340  size = mxGetNumberOfElements(in);
341  if (size == 0) mexErrMsgTxt("mxArray2ivec: Size of data is zero");
342
343  out.set_size(size, false);
344
345  for (i = 0; i < size; i++) {
346    out(i) = (int)(*temp++);
347  }
348
349  return out;
350
351}
352
353vec mxArray2vec(const mxArray *in)
354{
355  vec out;
356  int i, size;
357  double* temp = (double*) mxGetPr(in);
358  if (temp == 0) mexErrMsgTxt("mxArray2vec: Pointer to data is NULL");
359
360  size = mxGetNumberOfElements(in);
361  if (size == 0) mexErrMsgTxt("mxArray2vec: Size of data is zero");
362
363  out.set_size(size, false);
364
365  for (i = 0; i < size; i++) {
366    out(i) = (*temp++);
367  }
368
369  return out;
370
371}
372
373cvec mxArray2cvec(const mxArray *in)
374{
375  cvec out;
376  int i, size;
377  double* tempR = (double*) mxGetPr(in);
378  double* tempI = (double*) mxGetPi(in);
379
380  if ((tempR == 0) && (tempI == 0)) mexErrMsgTxt("mxArray2cvec: Pointer data is NULL");
381
382  size = mxGetNumberOfElements(in);
383  if (size == 0) mexErrMsgTxt("mxArray2cvec: Size of data is zero");
384
385  out.set_size(size, false);
386
387  if (tempR == 0) {
388    for (i = 0; i < size; i++) { out(i) = std::complex<double>(0.0, (*tempI++)); }
389  }
390  else if (tempI == 0) {
391    for (i = 0; i < size; i++) { out(i) = std::complex<double>((*tempR++), 0.0); }
392  }
393  else {
394    for (i = 0; i < size; i++) { out(i) = std::complex<double>((*tempR++), (*tempI++)); }
395  }
396
397  return out;
398
399}
400
401bmat mxArray2bmat(const mxArray *in)
402{
403  bmat out;
404  int r, c, rows, cols;
405  double* temp = (double*) mxGetPr(in);
406  if (temp == 0) mexErrMsgTxt("mxArray2bmat: Pointer to data is NULL");
407
408  rows = mxGetM(in);
409  if (rows == 0) mexErrMsgTxt("mxArray2bmat: Data has zero rows");
410  cols = mxGetN(in);
411  if (cols == 0) mexErrMsgTxt("mxArray2bmat: Data has zero columns");
412
413  out.set_size(rows, cols, false);
414
415  for (c = 0; c < cols; c++) {
416    for (r = 0; r < rows; r++) {
417      out(r, c) = (((*temp++) > 0.0) ? bin(1) : bin(0));
418    }
419  }
420
421  return out;
422
423}
424
425smat mxArray2smat(const mxArray *in)
426{
427  smat out;
428  int r, c, rows, cols;
429  double* temp = (double*) mxGetPr(in);
430  if (temp == 0) mexErrMsgTxt("mxArray2smat: Pointer to data is NULL");
431
432  rows = mxGetM(in);
433  if (rows == 0) mexErrMsgTxt("mxArray2smat: Data has zero rows");
434  cols = mxGetN(in);
435  if (cols == 0) mexErrMsgTxt("mxArray2smat: Data has zero columns");
436
437  out.set_size(rows, cols, false);
438
439  for (c = 0; c < cols; c++) {
440    for (r = 0; r < rows; r++) {
441      out(r, c) = (short)(*temp++);
442    }
443  }
444
445  return out;
446
447}
448
449imat mxArray2imat(const mxArray *in)
450{
451  imat out;
452  int r, c, rows, cols;
453  double* temp = (double*) mxGetPr(in);
454  if (temp == 0) mexErrMsgTxt("mxArray2imat: Pointer to data is NULL");
455
456  rows = mxGetM(in);
457  if (rows == 0) mexErrMsgTxt("mxArray2imat: Data has zero rows");
458  cols = mxGetN(in);
459  if (cols == 0) mexErrMsgTxt("mxArray2imat: Data has zero columns");
460  out.set_size(rows, cols, false);
461
462  for (c = 0; c < cols; c++) {
463    for (r = 0; r < rows; r++) {
464      out(r, c) = (int)(*temp++);
465    }
466  }
467
468  return out;
469
470}
471
472mat mxArray2mat(const mxArray *in)
473{
474  mat out;
475  int r, c, rows, cols;
476  double* temp = (double*) mxGetPr(in);
477  if (temp == 0) mexErrMsgTxt("mxArray2mat: Pointer to data is NULL");
478
479  rows = mxGetM(in);
480  if (rows == 0) mexErrMsgTxt("mxArray2mat: Data has zero rows");
481  cols = mxGetN(in);
482  if (cols == 0) mexErrMsgTxt("mxArray2mat: Data has zero columns");
483  out.set_size(rows, cols, false);
484
485  for (c = 0; c < cols; c++) {
486    for (r = 0; r < rows; r++) {
487      out(r, c) = (*temp++);
488    }
489  }
490
491  return out;
492
493}
494
495cmat mxArray2cmat(const mxArray *in)
496{
497  cmat out;
498  int r, c, rows, cols;
499  double* tempR = (double*) mxGetPr(in);
500  double* tempI = (double*) mxGetPi(in);
501
502  if ((tempR == 0) && (tempI == 0)) mexErrMsgTxt("mxArray2cmat: Pointer to data is NULL");
503
504  rows = mxGetM(in);
505  if (rows == 0) mexErrMsgTxt("mxArray2cmat: Data has zero rows");
506  cols = mxGetN(in);
507  if (cols == 0) mexErrMsgTxt("mxArray2cmat: Data has zero columns");
508  out.set_size(rows, cols, false);
509
510  if (tempR == 0) {
511    for (c = 0; c < cols; c++) { for (r = 0; r < rows; r++) { out(r, c) = std::complex<double>(0.0 , (*tempI++)); } }
512  }
513  else if (tempI == 0) {
514    for (c = 0; c < cols; c++) { for (r = 0; r < rows; r++) { out(r, c) = std::complex<double>((*tempR++), 0.0); } }
515  }
516  else {
517    for (c = 0; c < cols; c++) { for (r = 0; r < rows; r++) { out(r, c) = std::complex<double>((*tempR++), (*tempI++)); } }
518  }
519
520  return out;
521
522}
523
524void double2mxArray(const double &in, mxArray *out)
525{
526  double* temp = (double *) mxGetPr(out);
527  if (temp == 0) mexErrMsgTxt("double2mxArray: Pointer to data is NULL");
528
529  *temp = (double) in;
530}
531
532void double_complex2mxArray(const std::complex<double> &in, mxArray *out)
533{
534  double* tempR = (double *) mxGetPr(out);
535  double* tempI = (double *) mxGetPi(out);
536  if (tempR == 0) mexErrMsgTxt("double_complex2mxArray: Pointer to real valued part is NULL");
537  if (tempI == 0) mexErrMsgTxt("double_complex2mxArray: Pointer to imaginary valued part is NULL");
538
539  *tempR = (double) in.real();
540  *tempI = (double) in.imag();
541}
542
543void string2mxArray(const std::string &in, mxArray* &out)
544{
545  if (in.data() == 0)
546    mexErrMsgTxt("string2mxArray: Pointer to string is NULL");
547  out = mxCreateString(in.data());
548  if (out == 0)
549    mexErrMsgTxt("string2mxArray: Could not convert string to mxArray");
550}
551
552void bvec2mxArray(const bvec &in, mxArray *out)
553{
554  double* temp = (double *) mxGetPr(out);
555  if (temp == 0) mexErrMsgTxt("bvec2mxArray: Pointer to data is NULL");
556  if (in.size() == 0) mexErrMsgTxt("bvec2mxArray: Size of data is zero");
557  for (int i = 0; i < in.size(); i++) {
558    if (in(i))
559      *temp++ = 1.0;
560    else
561      *temp++ = 0.0;
562  }
563}
564
565void ivec2mxArray(const ivec &in, mxArray *out)
566{
567  double* temp = (double *) mxGetPr(out);
568  if (temp == 0) mexErrMsgTxt("ivec2mxArray: Pointer to data is NULL");
569  if (in.size() == 0) mexErrMsgTxt("ivec2mxArray: Size of data is zero");
570
571  for (int i = 0; i < in.size(); i++) {
572    *temp++ = (double) in(i);
573  }
574}
575
576void vec2mxArray(const vec &in, mxArray *out)
577{
578  double* temp = (double *) mxGetPr(out);
579  if (temp == 0) mexErrMsgTxt("vec2mxArray: Pointer to data is NULL");
580  if (in.size() == 0) mexErrMsgTxt("vec2mxArray: Size of data is zero");
581
582  for (int i = 0; i < in.size(); i++) {
583    *temp++ = (double) in(i);
584  }
585}
586
587void cvec2mxArray(const cvec &in, mxArray *out)
588{
589  double* tempR = (double *) mxGetPr(out);
590  double* tempI = (double *) mxGetPi(out);
591  if (tempR == 0) mexErrMsgTxt("cvec2mxArray: Pointer to real valued part is NULL");
592  if (tempI == 0) mexErrMsgTxt("cvec2mxArray: Pointer to imaginary valued part is NULL");
593  if (in.size() == 0) mexErrMsgTxt("cvec2mxArray: Size of data is zero");
594
595  for (int i = 0; i < in.size(); i++) {
596    *tempR++ = (double) in(i).real();
597    *tempI++ = (double) in(i).imag();
598  }
599}
600
601void bmat2mxArray(const bmat &in, mxArray *out)
602{
603  int rows, cols, r, c;
604
605  double* temp = (double *) mxGetPr(out);
606  if (temp == 0) mexErrMsgTxt("bmat2mxArray: Pointer to data is NULL");
607
608  rows = in.rows();
609  cols = in.cols();
610  if (rows == 0) mexErrMsgTxt("bmat2mxArray: Data has zero rows");
611  if (cols == 0) mexErrMsgTxt("bmat2mxArray: Data has zero columns");
612
613  for (c = 0; c < cols; c++) {
614    for (r = 0; r < rows; r++) {
615      if (in(r, c))
616        *temp++ = 1.0;
617      else
618        *temp++ = 0.0;
619    }
620  }
621
622}
623
624void smat2mxArray(const smat &in, mxArray *out)
625{
626  int rows, cols, r, c;
627
628  double* temp = (double *) mxGetPr(out);
629  if (temp == 0) mexErrMsgTxt("smat2mxArray: Pointer to data is NULL");
630
631  rows = in.rows();
632  cols = in.cols();
633  if (rows == 0) mexErrMsgTxt("smat2mxArray: Data has zero rows");
634  if (cols == 0) mexErrMsgTxt("smat2mxArray: Data has zero columns");
635
636  for (c = 0; c < cols; c++) {
637    for (r = 0; r < rows; r++) {
638      *temp++ = (double) in(r, c);
639    }
640  }
641
642}
643
644void imat2mxArray(const imat &in, mxArray *out)
645{
646  int rows, cols, r, c;
647
648  double* temp = (double *) mxGetPr(out);
649  if (temp == 0) mexErrMsgTxt("imat2mxArray: Pointer to data is NULL");
650
651  rows = in.rows();
652  cols = in.cols();
653  if (rows == 0) mexErrMsgTxt("imat2mxArray: Data has zero rows");
654  if (cols == 0) mexErrMsgTxt("imat2mxArray: Data has zero columns");
655
656  for (c = 0; c < cols; c++) {
657    for (r = 0; r < rows; r++) {
658      *temp++ = (double) in(r, c);
659    }
660  }
661
662}
663
664void mat2mxArray(const mat &in, mxArray *out)
665{
666  int rows, cols, r, c;
667
668  double* temp = (double *) mxGetPr(out);
669  if (temp == 0) mexErrMsgTxt("mat2mxArray: Pointer to data is NULL");
670
671  rows = in.rows();
672  cols = in.cols();
673  if (rows == 0) mexErrMsgTxt("mat2mxArray: Data has zero rows");
674  if (cols == 0) mexErrMsgTxt("mat2mxArray: Data has zero columns");
675
676  for (c = 0; c < cols; c++) {
677    for (r = 0; r < rows; r++) {
678      *temp++ = in(r, c);
679    }
680  }
681
682}
683
684void cmat2mxArray(const cmat &in, mxArray *out)
685{
686  int rows, cols, r, c;
687
688  double* tempR = (double *) mxGetPr(out);
689  double* tempI = (double *) mxGetPi(out);
690  if (tempR == 0) mexErrMsgTxt("cvec2mxArray: Pointer to real valued part is NULL");
691  if (tempI == 0) mexErrMsgTxt("cvec2mxArray: Pointer to imaginary valued part is NULL");
692
693  rows = in.rows();
694  cols = in.cols();
695  if (rows == 0) mexErrMsgTxt("cvec2mxArray: Data has zero rows");
696  if (cols == 0) mexErrMsgTxt("cvec2mxArray: Data has zero columns");
697
698  for (c = 0; c < cols; c++) {
699    for (r = 0; r < rows; r++) {
700      *tempR++ = (double) in(r, c).real();
701      *tempI++ = (double) in(r, c).imag();
702    }
703  }
704
705}
706
707void mxArray2Csvec(const mxArray *in, short *out)
708{
709  double* temp = (double*) mxGetPr(in);
710  if (temp == 0) mexErrMsgTxt("mxArray2Csvec: Pointer to data is NULL");
711  int size = mxGetNumberOfElements(in);
712  if (size == 0) mexErrMsgTxt("mxArray2Csvec: Size of data is zero");
713  for (int i = 0; i < size; i++) { out[i] = (short)(*temp++); }
714}
715
716void mxArray2Civec(const mxArray *in, int *out)
717{
718  double* temp = (double*) mxGetPr(in);
719  if (temp == 0) mexErrMsgTxt("mxArray2Civec: Pointer to data is NULL");
720  int size = mxGetNumberOfElements(in);
721  if (size == 0) mexErrMsgTxt("mxArray2Civec: Size of data is zero");
722  for (int i = 0; i < size; i++) { out[i] = (int)(*temp++); }
723}
724
725void mxArray2Cvec(const mxArray *in, double *out)
726{
727  double* temp = (double*) mxGetPr(in);
728  if (temp == 0) mexErrMsgTxt("mxArray2Cvec: Pointer to data is NULL");
729  int size = mxGetNumberOfElements(in);
730  if (size == 0) mexErrMsgTxt("mxArray2Cvec: Size of data is zero");
731  for (int i = 0; i < size; i++) { out[i] = (*temp++); }
732}
733
734void mxArray2Ccvec(const mxArray *in, double *out_real, double *out_imag)
735{
736  double* tempR = (double*) mxGetPr(in);
737  double* tempI = (double*) mxGetPi(in);
738  if (tempR == 0) mexErrMsgTxt("mxArray2Ccvec: Pointer to real valued part is NULL");
739  if (tempI == 0) mexErrMsgTxt("mxArray2Ccvec: Pointer to imaginary valued part is NULL");
740  int size = mxGetNumberOfElements(in);
741  if (size == 0) mexErrMsgTxt("mxArray2Ccvec: Size of data is zero");
742  for (int i = 0; i < size; i++) { out_real[i] = (*tempR++); out_imag[i] = (*tempI++); }
743}
744
745void mxArray2Csmat(const mxArray *in, short **out)
746{
747  int r, c;
748  double* temp = (double*) mxGetPr(in);
749  if (temp == 0) mexErrMsgTxt("mxArray2Csmat: Pointer to data is NULL");
750  int rows = mxGetM(in);
751  if (rows == 0) mexErrMsgTxt("mxArray2Csmat: Data has zero rows");
752  int cols = mxGetN(in);
753  if (cols == 0) mexErrMsgTxt("mxArray2Csmat: Data has zero columns");
754  for (c = 0; c < cols; c++) {
755    for (r = 0; r < rows; r++) {
756      out[r][c] = (short)(*temp++);
757    }
758  }
759}
760
761void mxArray2Cimat(const mxArray *in, int **out)
762{
763  int r, c;
764  double* temp = (double*) mxGetPr(in);
765  if (temp == 0) mexErrMsgTxt("mxArray2Cimat: Pointer to data is NULL");
766  int rows = mxGetM(in);
767  if (rows == 0) mexErrMsgTxt("mxArray2Cimat: Data has zero rows");
768  int cols = mxGetN(in);
769  if (cols == 0) mexErrMsgTxt("mxArray2Cimat: Data has zero columns");
770  for (c = 0; c < cols; c++) {
771    for (r = 0; r < rows; r++) {
772      out[r][c] = (int)(*temp++);
773    }
774  }
775}
776
777void mxArray2Cmat(const mxArray *in, double **out)
778{
779  int r, c;
780  double* temp = (double*) mxGetPr(in);
781  if (temp == 0) mexErrMsgTxt("mxArray2Cmat: Pointer to data is NULL");
782  int rows = mxGetM(in);
783  if (rows == 0) mexErrMsgTxt("mxArray2Cmat: Data has zero rows");
784  int cols = mxGetN(in);
785  if (cols == 0) mexErrMsgTxt("mxArray2Cmat: Data has zero columns");
786  for (c = 0; c < cols; c++) {
787    for (r = 0; r < rows; r++) {
788      out[r][c] = (*temp++);
789    }
790  }
791}
792
793void mxArray2Ccmat(const mxArray *in, double **out_real, double **out_imag)
794{
795  int r, c;
796  double* tempR = (double*) mxGetPr(in);
797  double* tempI = (double*) mxGetPi(in);
798  if (tempR == 0) mexErrMsgTxt("mxArray2Cmat: Pointer to real valued part is NULL");
799  if (tempI == 0) mexErrMsgTxt("mxArray2Cmat: Pointer to imaginary valued part is NULL");
800  int rows = mxGetM(in);
801  if (rows == 0) mexErrMsgTxt("mxArray2Cmat: Data has zero rows");
802  int cols = mxGetN(in);
803  if (cols == 0) mexErrMsgTxt("mxArray2Cmat: Data has zero columns");
804  for (c = 0; c < cols; c++) {
805    for (r = 0; r < rows; r++) {
806      out_real[r][c] = (*tempR++);
807      out_imag[r][c] = (*tempI++);
808    }
809  }
810}
811
812void Csvec2mxArray(short *in, mxArray *out)
813{
814  double* temp = (double *) mxGetPr(out);
815  if (temp == 0) mexErrMsgTxt("Csvec2mxArray: Pointer to data is NULL");
816  int size = mxGetNumberOfElements(out);
817  if (size == 0) mexErrMsgTxt("Csvec2mxArray: Size of data is zero");
818  for (int i = 0; i < size; i++) { *temp++ = (double) in[i]; }
819}
820
821void Civec2mxArray(int *in, mxArray *out)
822{
823  double* temp = (double *) mxGetPr(out);
824  if (temp == 0) mexErrMsgTxt("Civec2mxArray: Pointer to data is NULL");
825  int size = mxGetNumberOfElements(out);
826  if (size == 0) mexErrMsgTxt("Civec2mxArray: Size of data is zero");
827  for (int i = 0; i < size; i++) { *temp++ = (double) in[i]; }
828}
829
830void Cvec2mxArray(double *in, mxArray *out)
831{
832  double* temp = (double *) mxGetPr(out);
833  if (temp == 0) mexErrMsgTxt("Cvec2mxArray: Pointer to data is NULL");
834  int size = mxGetNumberOfElements(out);
835  if (size == 0) mexErrMsgTxt("Cvec2mxArray: Size of data is zero");
836  for (int i = 0; i < size; i++) { *temp++ = in[i]; }
837}
838
839void Ccvec2mxArray(double *in_real, double *in_imag, mxArray *out)
840{
841  double* tempR = (double *) mxGetPr(out);
842  double* tempI = (double *) mxGetPi(out);
843  if (tempR == 0) mexErrMsgTxt("Ccvec2mxArray: Pointer to real valued part is NULL");
844  if (tempI == 0) mexErrMsgTxt("Ccvec2mxArray: Pointer to imaginary valued part is NULL");
845  int size = mxGetNumberOfElements(out);
846  if (size == 0) mexErrMsgTxt("Ccvec2mxArray: Size of data is zero");
847  for (int i = 0; i < size; i++) { *tempR++ = in_real[i]; *tempI++ = in_imag[i]; }
848}
849
850void Csmat2mxArray(short **in, mxArray *out)
851{
852  int r, c;
853  double* temp = (double *) mxGetPr(out);
854  if (temp == 0) mexErrMsgTxt("Csmat2mxArray: Pointer to data is NULL");
855  int rows = mxGetM(out);
856  if (rows == 0) mexErrMsgTxt("Csmat2mxArray: Data has zero rows");
857  int cols = mxGetN(out);
858  if (cols == 0) mexErrMsgTxt("Csmat2mxArray: Data has zero columns");
859  for (c = 0; c < cols; c++) {
860    for (r = 0; r < rows; r++) {
861      *temp++ = (short) in[r][c];
862    }
863  }
864}
865
866void Cimat2mxArray(int **in, mxArray *out)
867{
868  int r, c;
869  double* temp = (double *) mxGetPr(out);
870  if (temp == 0) mexErrMsgTxt("Cimat2mxArray: Pointer to data is NULL");
871  int rows = mxGetM(out);
872  if (rows == 0) mexErrMsgTxt("Cimat2mxArray: Data has zero rows");
873  int cols = mxGetN(out);
874  if (cols == 0) mexErrMsgTxt("Cimat2mxArray: Data has zero columns");
875  for (c = 0; c < cols; c++) {
876    for (r = 0; r < rows; r++) {
877      *temp++ = (int) in[r][c];
878    }
879  }
880}
881
882void Cmat2mxArray(double **in, mxArray *out)
883{
884  int r, c;
885  double* temp = (double *) mxGetPr(out);
886  if (temp == 0) mexErrMsgTxt("Cmat2mxArray: Pointer to data is NULL");
887  int rows = mxGetM(out);
888  if (rows == 0) mexErrMsgTxt("Cmat2mxArray: Data has zero rows");
889  int cols = mxGetN(out);
890  if (cols == 0) mexErrMsgTxt("Cmat2mxArray: Data has zero columns");
891  for (c = 0; c < cols; c++) {
892    for (r = 0; r < rows; r++) {
893      *temp++ = in[r][c];
894    }
895  }
896}
897
898void Ccmat2mxArray(double **in_real, double **in_imag, mxArray *out)
899{
900  int r, c;
901  double* tempR = (double *) mxGetPr(out);
902  double* tempI = (double *) mxGetPi(out);
903  if (tempR == 0) mexErrMsgTxt("Ccmat2mxArray: Pointer to real valued part is NULL");
904  if (tempI == 0) mexErrMsgTxt("Ccmat2mxArray: Pointer to imaginary valued part is NULL");
905  int rows = mxGetM(out);
906  if (rows == 0) mexErrMsgTxt("Ccmat2mxArray: Data has zero rows");
907  int cols = mxGetN(out);
908  if (cols == 0) mexErrMsgTxt("Ccmat2mxArray: Data has zero columns");
909  for (c = 0; c < cols; c++) {
910    for (r = 0; r < rows; r++) {
911      *tempR++ = in_real[r][c];
912      *tempI++ = in_imag[r][c];
913    }
914  }
915}
916
917} // namespace itpp
918
919#endif // #ifndef ITMEX_H
Note: See TracBrowser for help on using the browser.