File: Modulate.cpp

package info (click to toggle)
codec2 1.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,376 kB
  • sloc: ansic: 436,819; cpp: 2,091; objc: 1,736; sh: 1,510; python: 1,405; asm: 683; makefile: 605
file content (136 lines) | stat: -rw-r--r-- 4,031 bytes parent folder | download | duplicates (3)
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
/* File: Modulate.c

   Description: Complex K-dimensional M-ary modulator

   The calling syntax is:
      [output] = Modulate( input, S_matrix )

   Where:
      output   = K by N vector of modulated symbols

      input    = 1 by N*log2(M) vector of data bits 
	  S_Matrix = K by M complex matrix containing the constellation signals
	             Each Column is one signal, each signal is K-dimensional

      Note: For legacy purposes, S_matrix can be a M by 1 matrix when signal
	  set can be characterized by a one-dimensional complex value (QAM, etc).

   Copyright (C) 2005-2006, Matthew C. Valenti

   Last updated on May 6, 2006

   Function Modulate is part of the Iterative Solutions 
   Coded Modulation Library. The Iterative Solutions Coded Modulation 
   Library is free software; you can redistribute it and/or modify it 
   under the terms of the GNU Lesser General Public License as published 
   by the Free Software Foundation; either version 2.1 of the License, 
   or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
  
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
#include <math.h>
#include <mex.h>
#include <Matrix.h>
#include <stdlib.h>

/* Input Arguments
prhs[0] is input
prhs[1] is S_matrix */

/* Output Arguments
plhs[0] is output */

/* main function that interfaces with MATLAB */
void mexFunction(
				 int            nlhs,
				 mxArray       *plhs[],
				 int            nrhs,
				 const mxArray *prhs[] )
{
  int    M, K;
  int    number_symbols, number_bits, bits_per_symbol;
  double *input, *Sr, *Si, *output_r, *output_i;
  int    i,j,temp_int,index;
  int    *data_int;

  /* make sure there are enough inputs */
  if (nrhs<2)
	  mexErrMsgTxt("Usage: [output] = Modulate( input, S_matrix )");

  /* read in input bits */
  number_bits = mxGetN(prhs[0]);
  input = mxGetPr(prhs[0]);

  /* read in the constellation matrix */
  
  /* for legacy purposes, see if it is a single column */
  if ( mxGetN( prhs[1] ) == 1 ) {
	  M = mxGetM( prhs[1] );
	  K = 1;
  } else {
	  M = mxGetN( prhs[1] );
	  K = mxGetM( prhs[1] );
  }  

  /* get the real part */
  Sr = mxGetPr(prhs[1]);

  /* if not complex, set imagainary part to zero */
  if (!mxIsComplex(prhs[1]) )
	  Si = (double*)calloc( M*K, sizeof(double) );
  else
	  Si = mxGetPi(prhs[1]);

  /* determine the number of bits per symbol 
     for future development: check to make sure M is a power of 2 */
  bits_per_symbol = 0;
  temp_int = M;
  while (temp_int>1) {
	 temp_int = temp_int/2;
	 bits_per_symbol++;
  }
  
  /* determine the number of output symbols */
  number_symbols = number_bits/bits_per_symbol + (number_bits%bits_per_symbol>0);

  /* read in the input data and cast to int */
  data_int = (int*)calloc( number_symbols*bits_per_symbol, sizeof(int) );
  for (i=0;i<number_bits;i++) 
	  data_int[i] = input[i];

  /* create the complex output matrix */
  plhs[0]=mxCreateDoubleMatrix( K, number_symbols, mxCOMPLEX);
  output_r = mxGetPr( plhs[0] );
  output_i = mxGetPi( plhs[0] );

  /* determine output */
  for (i=0;i<number_symbols;i++) { /* create each modulated symbol */
	  index = 0;
	  for (j=0;j<bits_per_symbol;j++) { /* go through each associated bit */
		  index = index << 1; /* shift to the left (multiply by 2) */
		  index += data_int[i*bits_per_symbol+j];
	  }
	  /* assign the K-dimensional output symbol */
	  for (j=0;j<K;j++) {
		  output_r[K*i+j] = Sr[K*index+j];
		  output_i[K*i+j] = Si[K*index+j];
	  }
  }

  /* free memory */
  free( data_int );
  
  if(!mxIsComplex(prhs[1]) )
     free(Si);

}