Example #1
0
void sammon(sammonStackData *SD, const real_T x[1000000], real_T y[2000], real_T
            *E)
{
  real_T B;
  int32_T i;
  real_T dv0[1000];
  int32_T b_i;
  boolean_T exitg1;
  real_T alpha1;
  real_T beta1;
  char_T TRANSB;
  char_T TRANSA;
  ptrdiff_t m_t;
  ptrdiff_t n_t;
  ptrdiff_t k_t;
  ptrdiff_t lda_t;
  ptrdiff_t ldb_t;
  ptrdiff_t ldc_t;
  double * alpha1_t;
  double * Aia0_t;
  double * Bib0_t;
  double * beta1_t;
  double * Cic0_t;
  real_T g[2000];
  real_T y2[2000];
  real_T b_y[2000];
  real_T c_y[2000];
  real_T d_y[2000];
  real_T e_y[2000];
  real_T b_g[2000];
  int32_T j;
  int32_T b_j;
  boolean_T exitg2;
  real_T dv1[1000];
  real_T E_new;

  /* #codgen */
  /*  */
  /*  SAMMON - apply Sammon's nonlinear mapping  */
  /*  */
  /*     Y = SAMMON(X) applies Sammon's nonlinear mapping procedure on */
  /*     multivariate data X, where each row represents a pattern and each column */
  /*     represents a feature.  On completion, Y contains the corresponding */
  /*     co-ordinates of each point on the map.  By default, a two-dimensional */
  /*     map is created.  Note if X contains any duplicated rows, SAMMON will */
  /*     fail (ungracefully).  */
  /*  */
  /*     [Y,E] = SAMMON(X) also returns the value of the cost function in E (i.e. */
  /*     the stress of the mapping). */
  /*  */
  /*     An N-dimensional output map is generated by Y = SAMMON(X,N) . */
  /*  */
  /*     A set of optimisation options can also be specified using a third */
  /*     argument, Y = SAMMON(X,N,OPTS) , where OPTS is a structure with fields: */
  /*  */
  /*        MaxIter        - maximum number of iterations */
  /*        TolFun         - relative tolerance on objective function */
  /*        MaxHalves      - maximum number of step halvings */
  /*        Input          - {'raw','distance'} if set to 'distance', X is  */
  /*                         interpreted as a matrix of pairwise distances. */
  /*        Display        - {'off', 'on', 'iter'} */
  /*        Initialisation - {'pca', 'random'} */
  /*  */
  /*     The default options structure can be retrieved by calling SAMMON with */
  /*     no parameters. */
  /*  */
  /*     References : */
  /*  */
  /*        [1] Sammon, John W. Jr., "A Nonlinear Mapping for Data Structure */
  /*            Analysis", IEEE Transactions on Computers, vol. C-18, no. 5, */
  /*            pp 401-409, May 1969. */
  /*  */
  /*     See also : SAMMON_TEST */
  /*  */
  /*  File        : sammon.m */
  /*  */
  /*  Date        : Monday 12th November 2007. */
  /*  */
  /*  Author      : Gavin C. Cawley and Nicola L. C. Talbot */
  /*  */
  /*  Description : Simple vectorised MATLAB implementation of Sammon's non-linear */
  /*                mapping algorithm [1]. */
  /*  */
  /*  References  : [1] Sammon, John W. Jr., "A Nonlinear Mapping for Data */
  /*                    Structure Analysis", IEEE Transactions on Computers, */
  /*                    vol. C-18, no. 5, pp 401-409, May 1969. */
  /*  */
  /*  History     : 10/08/2004 - v1.00 */
  /*                11/08/2004 - v1.10 Hessian made positive semidefinite */
  /*                13/08/2004 - v1.11 minor optimisation */
  /*                12/11/2007 - v1.20 initialisation using the first n principal */
  /*                                   components. */
  /*  */
  /*  Thanks      : Dr Nick Hamilton ([email protected]) for supplying the */
  /*                code for implementing initialisation using the first n */
  /*                principal components (introduced in v1.20). */
  /*  */
  /*  To do       : The current version does not take advantage of the symmetry */
  /*                of the distance matrix in order to allow for easy */
  /*                vectorisation.  This may not be a good choice for very large */
  /*                datasets, so perhaps one day I'll get around to doing a MEX */
  /*                version using the BLAS library etc. for very large datasets. */
  /*  */
  /*  Copyright   : (c) Dr Gavin C. Cawley, November 2007. */
  /*  */
  /*     This program is free software; you can redistribute it and/or modify */
  /*     it under the terms of the GNU General Public License as published by */
  /*     the Free Software Foundation; either version 2 of the License, or */
  /*     (at your option) any later version. */
  /*  */
  /*     This program 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 General Public License for more details. */
  /*  */
  /*     You should have received a copy of the GNU General Public License */
  /*     along with this program; if not, write to the Free Software */
  /*     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  /*  */
  /*  use the default options structure */
  /*  create distance matrix unless given by parameters */
  emlrtPushRtStackR2012b(&emlrtRSI, emlrtRootTLSGlobal);
  euclid(SD, x, x, SD->f2.D);
  emlrtPopRtStackR2012b(&emlrtRSI, emlrtRootTLSGlobal);

  /*  remaining initialisation */
  B = b_sum(SD->f2.D);
  eye(SD->f2.delta);
  for (i = 0; i < 1000000; i++) {
    SD->f2.D[i] += SD->f2.delta[i];
  }

  rdivide(1.0, SD->f2.D, SD->f2.Dinv);
  emlrtPushRtStackR2012b(&b_emlrtRSI, emlrtRootTLSGlobal);
  randn(y);
  emlrtPopRtStackR2012b(&b_emlrtRSI, emlrtRootTLSGlobal);
  emlrtPushRtStackR2012b(&c_emlrtRSI, emlrtRootTLSGlobal);
  b_euclid(SD, y, y, SD->f2.d);
  eye(SD->f2.delta);
  for (i = 0; i < 1000000; i++) {
    SD->f2.d[i] += SD->f2.delta[i];
  }

  emlrtPopRtStackR2012b(&c_emlrtRSI, emlrtRootTLSGlobal);
  rdivide(1.0, SD->f2.d, SD->f2.dinv);
  for (i = 0; i < 1000000; i++) {
    SD->f2.b_D[i] = SD->f2.D[i] - SD->f2.d[i];
  }

  power(SD->f2.b_D, SD->f2.delta);
  for (i = 0; i < 1000000; i++) {
    SD->f2.d[i] = SD->f2.delta[i] * SD->f2.Dinv[i];
  }

  d_sum(SD->f2.d, dv0);
  *E = e_sum(dv0);

  /*  get on with it */
  b_i = 0;
  exitg1 = FALSE;
  while ((exitg1 == FALSE) && (b_i < 500)) {
    /*  compute gradient, Hessian and search direction (note it is actually */
    /*  1/4 of the gradient and Hessian, but the step size is just the ratio */
    /*  of the gradient and the diagonal of the Hessian so it doesn't matter). */
    for (i = 0; i < 1000000; i++) {
      SD->f2.delta[i] = SD->f2.dinv[i] - SD->f2.Dinv[i];
    }

    emlrtPushRtStackR2012b(&d_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&i_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&j_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&k_emlrtRSI, emlrtRootTLSGlobal);
    alpha1 = 1.0;
    beta1 = 0.0;
    TRANSB = 'N';
    TRANSA = 'N';
    for (i = 0; i < 2000; i++) {
      SD->f2.y_old[i] = 1.0;
      SD->f2.deltaone[i] = 0.0;
    }

    emlrtPushRtStackR2012b(&l_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    m_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&l_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&m_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    n_t = (ptrdiff_t)(2);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&m_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&n_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    k_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&n_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&o_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    lda_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&o_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&p_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    ldb_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&p_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&q_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    ldc_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&q_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&r_emlrtRSI, emlrtRootTLSGlobal);
    alpha1_t = (double *)(&alpha1);
    emlrtPopRtStackR2012b(&r_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&s_emlrtRSI, emlrtRootTLSGlobal);
    Aia0_t = (double *)(&SD->f2.delta[0]);
    emlrtPopRtStackR2012b(&s_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&t_emlrtRSI, emlrtRootTLSGlobal);
    Bib0_t = (double *)(&SD->f2.y_old[0]);
    emlrtPopRtStackR2012b(&t_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&u_emlrtRSI, emlrtRootTLSGlobal);
    beta1_t = (double *)(&beta1);
    emlrtPopRtStackR2012b(&u_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&v_emlrtRSI, emlrtRootTLSGlobal);
    Cic0_t = (double *)(&SD->f2.deltaone[0]);
    emlrtPopRtStackR2012b(&v_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&w_emlrtRSI, emlrtRootTLSGlobal);
    dgemm(&TRANSA, &TRANSB, &m_t, &n_t, &k_t, alpha1_t, Aia0_t, &lda_t, Bib0_t,
          &ldb_t, beta1_t, Cic0_t, &ldc_t);
    emlrtPopRtStackR2012b(&w_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&k_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&j_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&i_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&d_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&e_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&i_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&j_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&k_emlrtRSI, emlrtRootTLSGlobal);
    alpha1 = 1.0;
    beta1 = 0.0;
    TRANSB = 'N';
    TRANSA = 'N';
    memset(&g[0], 0, 2000U * sizeof(real_T));
    emlrtPushRtStackR2012b(&l_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    m_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&l_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&m_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    n_t = (ptrdiff_t)(2);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&m_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&n_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    k_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&n_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&o_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    lda_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&o_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&p_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    ldb_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&p_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&q_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    ldc_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&q_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&r_emlrtRSI, emlrtRootTLSGlobal);
    alpha1_t = (double *)(&alpha1);
    emlrtPopRtStackR2012b(&r_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&s_emlrtRSI, emlrtRootTLSGlobal);
    Aia0_t = (double *)(&SD->f2.delta[0]);
    emlrtPopRtStackR2012b(&s_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&t_emlrtRSI, emlrtRootTLSGlobal);
    Bib0_t = (double *)(&y[0]);
    emlrtPopRtStackR2012b(&t_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&u_emlrtRSI, emlrtRootTLSGlobal);
    beta1_t = (double *)(&beta1);
    emlrtPopRtStackR2012b(&u_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&v_emlrtRSI, emlrtRootTLSGlobal);
    Cic0_t = (double *)(&g[0]);
    emlrtPopRtStackR2012b(&v_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&w_emlrtRSI, emlrtRootTLSGlobal);
    dgemm(&TRANSA, &TRANSB, &m_t, &n_t, &k_t, alpha1_t, Aia0_t, &lda_t, Bib0_t,
          &ldb_t, beta1_t, Cic0_t, &ldc_t);
    emlrtPopRtStackR2012b(&w_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&k_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&j_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&i_emlrtRSI, emlrtRootTLSGlobal);
    for (i = 0; i < 2000; i++) {
      g[i] -= y[i] * SD->f2.deltaone[i];
    }

    emlrtPopRtStackR2012b(&e_emlrtRSI, emlrtRootTLSGlobal);
    c_power(SD->f2.dinv, SD->f2.delta);
    b_power(y, y2);
    emlrtPushRtStackR2012b(&f_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&i_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&j_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&k_emlrtRSI, emlrtRootTLSGlobal);
    alpha1 = 1.0;
    beta1 = 0.0;
    TRANSB = 'N';
    TRANSA = 'N';
    memset(&b_y[0], 0, 2000U * sizeof(real_T));
    emlrtPushRtStackR2012b(&l_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    m_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&l_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&m_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    n_t = (ptrdiff_t)(2);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&m_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&n_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    k_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&n_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&o_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    lda_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&o_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&p_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    ldb_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&p_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&q_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    ldc_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&q_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&r_emlrtRSI, emlrtRootTLSGlobal);
    alpha1_t = (double *)(&alpha1);
    emlrtPopRtStackR2012b(&r_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&s_emlrtRSI, emlrtRootTLSGlobal);
    Aia0_t = (double *)(&SD->f2.delta[0]);
    emlrtPopRtStackR2012b(&s_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&t_emlrtRSI, emlrtRootTLSGlobal);
    Bib0_t = (double *)(&y2[0]);
    emlrtPopRtStackR2012b(&t_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&u_emlrtRSI, emlrtRootTLSGlobal);
    beta1_t = (double *)(&beta1);
    emlrtPopRtStackR2012b(&u_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&v_emlrtRSI, emlrtRootTLSGlobal);
    Cic0_t = (double *)(&b_y[0]);
    emlrtPopRtStackR2012b(&v_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&w_emlrtRSI, emlrtRootTLSGlobal);
    dgemm(&TRANSA, &TRANSB, &m_t, &n_t, &k_t, alpha1_t, Aia0_t, &lda_t, Bib0_t,
          &ldb_t, beta1_t, Cic0_t, &ldc_t);
    emlrtPopRtStackR2012b(&w_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&k_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&j_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&i_emlrtRSI, emlrtRootTLSGlobal);
    for (i = 0; i < 2000; i++) {
      c_y[i] = 2.0 * y[i];
    }

    emlrtPushRtStackR2012b(&i_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&j_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&k_emlrtRSI, emlrtRootTLSGlobal);
    alpha1 = 1.0;
    beta1 = 0.0;
    TRANSB = 'N';
    TRANSA = 'N';
    memset(&d_y[0], 0, 2000U * sizeof(real_T));
    emlrtPushRtStackR2012b(&l_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    m_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&l_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&m_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    n_t = (ptrdiff_t)(2);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&m_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&n_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    k_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&n_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&o_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    lda_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&o_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&p_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    ldb_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&p_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&q_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    ldc_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&q_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&r_emlrtRSI, emlrtRootTLSGlobal);
    alpha1_t = (double *)(&alpha1);
    emlrtPopRtStackR2012b(&r_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&s_emlrtRSI, emlrtRootTLSGlobal);
    Aia0_t = (double *)(&SD->f2.delta[0]);
    emlrtPopRtStackR2012b(&s_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&t_emlrtRSI, emlrtRootTLSGlobal);
    Bib0_t = (double *)(&y[0]);
    emlrtPopRtStackR2012b(&t_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&u_emlrtRSI, emlrtRootTLSGlobal);
    beta1_t = (double *)(&beta1);
    emlrtPopRtStackR2012b(&u_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&v_emlrtRSI, emlrtRootTLSGlobal);
    Cic0_t = (double *)(&d_y[0]);
    emlrtPopRtStackR2012b(&v_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&w_emlrtRSI, emlrtRootTLSGlobal);
    dgemm(&TRANSA, &TRANSB, &m_t, &n_t, &k_t, alpha1_t, Aia0_t, &lda_t, Bib0_t,
          &ldb_t, beta1_t, Cic0_t, &ldc_t);
    emlrtPopRtStackR2012b(&w_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&k_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&j_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&i_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&i_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&j_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&k_emlrtRSI, emlrtRootTLSGlobal);
    alpha1 = 1.0;
    beta1 = 0.0;
    TRANSB = 'N';
    TRANSA = 'N';
    for (i = 0; i < 2000; i++) {
      SD->f2.y_old[i] = 1.0;
      e_y[i] = 0.0;
    }

    emlrtPushRtStackR2012b(&l_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    m_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&l_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&m_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    n_t = (ptrdiff_t)(2);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&m_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&n_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    k_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&n_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&o_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    lda_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&o_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&p_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    ldb_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&p_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&q_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    ldc_t = (ptrdiff_t)(1000);
    emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&q_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&r_emlrtRSI, emlrtRootTLSGlobal);
    alpha1_t = (double *)(&alpha1);
    emlrtPopRtStackR2012b(&r_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&s_emlrtRSI, emlrtRootTLSGlobal);
    Aia0_t = (double *)(&SD->f2.delta[0]);
    emlrtPopRtStackR2012b(&s_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&t_emlrtRSI, emlrtRootTLSGlobal);
    Bib0_t = (double *)(&SD->f2.y_old[0]);
    emlrtPopRtStackR2012b(&t_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&u_emlrtRSI, emlrtRootTLSGlobal);
    beta1_t = (double *)(&beta1);
    emlrtPopRtStackR2012b(&u_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&v_emlrtRSI, emlrtRootTLSGlobal);
    Cic0_t = (double *)(&e_y[0]);
    emlrtPopRtStackR2012b(&v_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPushRtStackR2012b(&w_emlrtRSI, emlrtRootTLSGlobal);
    dgemm(&TRANSA, &TRANSB, &m_t, &n_t, &k_t, alpha1_t, Aia0_t, &lda_t, Bib0_t,
          &ldb_t, beta1_t, Cic0_t, &ldc_t);
    emlrtPopRtStackR2012b(&w_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&k_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&j_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&i_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&f_emlrtRSI, emlrtRootTLSGlobal);
    for (i = 0; i < 2000; i++) {
      SD->f2.y_old[i] = ((b_y[i] - SD->f2.deltaone[i]) - c_y[i] * d_y[i]) + y2[i]
        * e_y[i];
      b_g[i] = -g[i];
    }

    b_abs(SD->f2.y_old, y2);
    for (i = 0; i < 2000; i++) {
      SD->f2.deltaone[i] = y2[i];
      SD->f2.y_old[i] = y[i];
    }

    b_rdivide(b_g, SD->f2.deltaone, y2);

    /*  use step-halving procedure to ensure progress is made */
    j = 1;
    b_j = 0;
    exitg2 = FALSE;
    while ((exitg2 == FALSE) && (b_j < 20)) {
      j = b_j + 1;
      for (i = 0; i < 2000; i++) {
        y[i] = SD->f2.y_old[i] + y2[i];
      }

      emlrtPushRtStackR2012b(&g_emlrtRSI, emlrtRootTLSGlobal);
      b_euclid(SD, y, y, SD->f2.d);
      eye(SD->f2.delta);
      for (i = 0; i < 1000000; i++) {
        SD->f2.d[i] += SD->f2.delta[i];
      }

      emlrtPopRtStackR2012b(&g_emlrtRSI, emlrtRootTLSGlobal);
      rdivide(1.0, SD->f2.d, SD->f2.dinv);
      for (i = 0; i < 1000000; i++) {
        SD->f2.b_D[i] = SD->f2.D[i] - SD->f2.d[i];
      }

      power(SD->f2.b_D, SD->f2.delta);
      for (i = 0; i < 1000000; i++) {
        SD->f2.d[i] = SD->f2.delta[i] * SD->f2.Dinv[i];
      }

      d_sum(SD->f2.d, dv1);
      E_new = e_sum(dv1);
      if (E_new < *E) {
        exitg2 = TRUE;
      } else {
        for (i = 0; i < 2000; i++) {
          y2[i] *= 0.5;
        }

        b_j++;
        emlrtBreakCheckFastR2012b(emlrtBreakCheckR2012bFlagVar,
          emlrtRootTLSGlobal);
      }
    }

    /*  bomb out if too many halving steps are required */
    if ((j == 20) || (muDoubleScalarAbs((*E - E_new) / *E) < 1.0E-9)) {
      exitg1 = TRUE;
    } else {
      /*  evaluate termination criterion */
      /*  report progress */
      *E = E_new;
      b_i++;
      emlrtBreakCheckFastR2012b(emlrtBreakCheckR2012bFlagVar, emlrtRootTLSGlobal);
    }
  }

  /*  fiddle stress to match the original Sammon paper */
  *E *= 0.5 / B;
}
Example #2
0
/* Function Definitions */
static void b_equalizeOFDM(testMACRouterStackData *SD, const emlrtStack *sp,
  const creal_T recv_data[1280], const real_T tx_longPreamble[53], const real_T
  tx_pilots[48], const real_T c_tx_pilotLocationsWithoutGuard[4], const real_T
  tx_dataSubcarrierIndexies_data[48], const int32_T
  tx_dataSubcarrierIndexies_size[2], c_struct_T *estimate, OFDMDemodulator_1
  *hPreambleDemod, OFDMDemodulator_1 *hDataDemod, creal_T R[576],
  emxArray_creal_T *Rraw)
{
  OFDMDemodulator_1 *obj;
  const mxArray *y;
  static const int32_T iv198[2] = { 1, 45 };

  const mxArray *m49;
  char_T cv241[45];
  int32_T i;
  static const char_T cv242[45] = { 'M', 'A', 'T', 'L', 'A', 'B', ':', 's', 'y',
    's', 't', 'e', 'm', ':', 'm', 'e', 't', 'h', 'o', 'd', 'C', 'a', 'l', 'l',
    'e', 'd', 'W', 'h', 'e', 'n', 'R', 'e', 'l', 'e', 'a', 's', 'e', 'd', 'C',
    'o', 'd', 'e', 'g', 'e', 'n' };

  const mxArray *b_y;
  static const int32_T iv199[2] = { 1, 4 };

  char_T cv243[4];
  static const char_T cv244[4] = { 's', 't', 'e', 'p' };

  const mxArray *c_y;
  static const int32_T iv200[2] = { 1, 51 };

  char_T cv245[51];
  static const char_T cv246[51] = { 'M', 'A', 'T', 'L', 'A', 'B', ':', 's', 'y',
    's', 't', 'e', 'm', ':', 'm', 'e', 't', 'h', 'o', 'd', 'C', 'a', 'l', 'l',
    'e', 'd', 'W', 'h', 'e', 'n', 'L', 'o', 'c', 'k', 'e', 'd', 'R', 'e', 'l',
    'e', 'a', 's', 'e', 'd', 'C', 'o', 'd', 'e', 'g', 'e', 'n' };

  const mxArray *d_y;
  static const int32_T iv201[2] = { 1, 5 };

  char_T cv247[5];
  static const char_T cv248[5] = { 's', 'e', 't', 'u', 'p' };

  int8_T fullGrid[64];
  int32_T k;
  static const int8_T iv202[11] = { 0, 1, 2, 3, 4, 5, 59, 60, 61, 62, 63 };

  int8_T ii_data[64];
  int32_T ii;
  boolean_T exitg2;
  boolean_T guard2 = FALSE;
  int8_T b_ii_data[64];
  creal_T recv[64];
  emxArray_creal_T *RLongFirst;
  const mxArray *e_y;
  static const int32_T iv203[2] = { 1, 45 };

  const mxArray *f_y;
  static const int32_T iv204[2] = { 1, 4 };

  const mxArray *g_y;
  static const int32_T iv205[2] = { 1, 51 };

  const mxArray *h_y;
  static const int32_T iv206[2] = { 1, 5 };

  boolean_T exitg1;
  boolean_T guard1 = FALSE;
  creal_T b_recv[64];
  emxArray_creal_T *RLongSecond;
  emxArray_creal_T *b_R;
  creal_T c_R[106];
  real_T b_tx_longPreamble[106];
  creal_T RNormal[106];
  real_T dv13[106];
  real_T dv14[106];
  real_T REnergy[53];
  creal_T RConj[53];
  creal_T b_RConj[53];
  const mxArray *i_y;
  static const int32_T iv207[2] = { 1, 45 };

  const mxArray *j_y;
  static const int32_T iv208[2] = { 1, 4 };

  const mxArray *k_y;
  static const int32_T iv209[2] = { 1, 51 };

  const mxArray *l_y;
  static const int32_T iv210[2] = { 1, 5 };

  int8_T b_fullGrid[768];
  static const int16_T iv211[48] = { 11, 25, 39, 53, 75, 89, 103, 117, 139, 153,
    167, 181, 203, 217, 231, 245, 267, 281, 295, 309, 331, 345, 359, 373, 395,
    409, 423, 437, 459, 473, 487, 501, 523, 537, 551, 565, 587, 601, 615, 629,
    651, 665, 679, 693, 715, 729, 743, 757 };

  boolean_T c_fullGrid[768];
  int32_T ii_size[1];
  int32_T c_ii_data[768];
  real_T d_ii_data[768];
  int32_T b_ii_size[1];
  creal_T RXPilots[48];
  creal_T preambleGainsFull[636];
  int32_T ia;
  int32_T iv212[3];
  static const int8_T iv213[3] = { 48, 12, 1 };

  int32_T b_Rraw[3];
  creal_T PilotNormal[48];
  real_T pilotEqGains_re;
  real_T pilotEqGains_im;
  real_T a[48];
  real_T PilotEnergy[48];
  creal_T b_PilotNormal[48];
  creal_T pilotEqGains[576];
  creal_T preambleGainsFull_data[576];
  creal_T b_preambleGainsFull_data[576];
  creal_T c_preambleGainsFull_data[576];
  real_T preambleGainsFull_data_re;
  real_T preambleGainsFull_data_im;
  emlrtStack st;
  emlrtStack b_st;
  emlrtStack c_st;
  emlrtStack d_st;
  st.prev = sp;
  st.tls = sp->tls;
  b_st.prev = &st;
  b_st.tls = st.tls;
  c_st.prev = &b_st;
  c_st.tls = b_st.tls;
  d_st.prev = &c_st;
  d_st.tls = c_st.tls;
  emlrtHeapReferenceStackEnterFcnR2012b(sp);

  /*  equalizeOFDM: Equalize the entire OFDM frame through the use of both */
  /*  the long preamble from the 802.11a standard and four pilot tones in */
  /*  the data frames themselves.  The gains from the pilots are */
  /*  interpolated across frequency to fill the data frame and apply gains */
  /*  to all data subcarriers. */
  /*     %% Use Long Preamble frame to estimate channel in frequency domain */
  /*  Separate out received preambles */
  emlrtVectorVectorIndexCheckR2012b(1280, 1, 1, 160, &y_emlrtECI, sp);

  /*  Demod */
  st.site = &xr_emlrtRSI;
  obj = hPreambleDemod;
  if (!obj->isReleased) {
  } else {
    y = NULL;
    m49 = mxCreateCharArray(2, iv198);
    for (i = 0; i < 45; i++) {
      cv241[i] = cv242[i];
    }

    emlrtInitCharArrayR2013a(&st, 45, m49, cv241);
    emlrtAssign(&y, m49);
    b_y = NULL;
    m49 = mxCreateCharArray(2, iv199);
    for (i = 0; i < 4; i++) {
      cv243[i] = cv244[i];
    }

    emlrtInitCharArrayR2013a(&st, 4, m49, cv243);
    emlrtAssign(&b_y, m49);
    b_st.site = &cb_emlrtRSI;
    c_error(&b_st, message(&b_st, y, b_y, &emlrtMCI), &emlrtMCI);
  }

  if (!obj->isInitialized) {
    b_st.site = &cb_emlrtRSI;
    if (!obj->isInitialized) {
    } else {
      c_y = NULL;
      m49 = mxCreateCharArray(2, iv200);
      for (i = 0; i < 51; i++) {
        cv245[i] = cv246[i];
      }

      emlrtInitCharArrayR2013a(&b_st, 51, m49, cv245);
      emlrtAssign(&c_y, m49);
      d_y = NULL;
      m49 = mxCreateCharArray(2, iv201);
      for (i = 0; i < 5; i++) {
        cv247[i] = cv248[i];
      }

      emlrtInitCharArrayR2013a(&b_st, 5, m49, cv247);
      emlrtAssign(&d_y, m49);
      c_st.site = &cb_emlrtRSI;
      c_error(&c_st, message(&c_st, c_y, d_y, &emlrtMCI), &emlrtMCI);
    }

    c_st.site = &cb_emlrtRSI;
    obj->isInitialized = TRUE;
    d_st.site = &db_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    memset(&fullGrid[0], 1, sizeof(int8_T) << 6);
    for (k = 0; k < 11; k++) {
      fullGrid[iv202[k]] = 0;
    }

    d_st.site = &fs_emlrtRSI;
    i = 0;
    ii = 1;
    exitg2 = FALSE;
    while ((exitg2 == FALSE) && (ii < 65)) {
      guard2 = FALSE;
      if (fullGrid[ii - 1] == 1) {
        i++;
        ii_data[i - 1] = (int8_T)ii;
        if (i >= 64) {
          exitg2 = TRUE;
        } else {
          guard2 = TRUE;
        }
      } else {
        guard2 = TRUE;
      }

      if (guard2 == TRUE) {
        ii++;
      }
    }

    if (1 > i) {
      i = 0;
    }

    for (k = 0; k < i; k++) {
      b_ii_data[k] = ii_data[k];
    }

    for (k = 0; k < i; k++) {
      ii_data[k] = b_ii_data[k];
    }

    d_st.site = &fs_emlrtRSI;
    k = obj->pDataLinearIndices->size[0];
    obj->pDataLinearIndices->size[0] = i;
    emxEnsureCapacity(&d_st, (emxArray__common *)obj->pDataLinearIndices, k,
                      (int32_T)sizeof(real_T), &pb_emlrtRTEI);
    for (k = 0; k < i; k++) {
      obj->pDataLinearIndices->data[k] = ii_data[k];
    }

    c_st.site = &cb_emlrtRSI;
  }

  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  c_st.site = &cb_emlrtRSI;
  c_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  c_st.site = &cb_emlrtRSI;
  c_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  memcpy(&recv[0], &recv_data[192], sizeof(creal_T) << 6);
  emxInit_creal_T(&st, &RLongFirst, 3, &yb_emlrtRTEI, TRUE);
  b_st.site = &cb_emlrtRSI;
  OFDMDemodulator_stepImpl(&b_st, obj, recv, RLongFirst);

  /* First half of long preamble */
  st.site = &yr_emlrtRSI;
  obj = hPreambleDemod;
  if (!obj->isReleased) {
  } else {
    e_y = NULL;
    m49 = mxCreateCharArray(2, iv203);
    for (i = 0; i < 45; i++) {
      cv241[i] = cv242[i];
    }

    emlrtInitCharArrayR2013a(&st, 45, m49, cv241);
    emlrtAssign(&e_y, m49);
    f_y = NULL;
    m49 = mxCreateCharArray(2, iv204);
    for (i = 0; i < 4; i++) {
      cv243[i] = cv244[i];
    }

    emlrtInitCharArrayR2013a(&st, 4, m49, cv243);
    emlrtAssign(&f_y, m49);
    b_st.site = &cb_emlrtRSI;
    c_error(&b_st, message(&b_st, e_y, f_y, &emlrtMCI), &emlrtMCI);
  }

  if (!obj->isInitialized) {
    b_st.site = &cb_emlrtRSI;
    if (!obj->isInitialized) {
    } else {
      g_y = NULL;
      m49 = mxCreateCharArray(2, iv205);
      for (i = 0; i < 51; i++) {
        cv245[i] = cv246[i];
      }

      emlrtInitCharArrayR2013a(&b_st, 51, m49, cv245);
      emlrtAssign(&g_y, m49);
      h_y = NULL;
      m49 = mxCreateCharArray(2, iv206);
      for (i = 0; i < 5; i++) {
        cv247[i] = cv248[i];
      }

      emlrtInitCharArrayR2013a(&b_st, 5, m49, cv247);
      emlrtAssign(&h_y, m49);
      c_st.site = &cb_emlrtRSI;
      c_error(&c_st, message(&c_st, g_y, h_y, &emlrtMCI), &emlrtMCI);
    }

    c_st.site = &cb_emlrtRSI;
    obj->isInitialized = TRUE;
    d_st.site = &db_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    memset(&fullGrid[0], 1, sizeof(int8_T) << 6);
    for (k = 0; k < 11; k++) {
      fullGrid[iv202[k]] = 0;
    }

    d_st.site = &fs_emlrtRSI;
    i = 0;
    ii = 1;
    exitg1 = FALSE;
    while ((exitg1 == FALSE) && (ii < 65)) {
      guard1 = FALSE;
      if (fullGrid[ii - 1] == 1) {
        i++;
        ii_data[i - 1] = (int8_T)ii;
        if (i >= 64) {
          exitg1 = TRUE;
        } else {
          guard1 = TRUE;
        }
      } else {
        guard1 = TRUE;
      }

      if (guard1 == TRUE) {
        ii++;
      }
    }

    if (1 > i) {
      i = 0;
    }

    for (k = 0; k < i; k++) {
      b_ii_data[k] = ii_data[k];
    }

    for (k = 0; k < i; k++) {
      ii_data[k] = b_ii_data[k];
    }

    d_st.site = &fs_emlrtRSI;
    k = obj->pDataLinearIndices->size[0];
    obj->pDataLinearIndices->size[0] = i;
    emxEnsureCapacity(&d_st, (emxArray__common *)obj->pDataLinearIndices, k,
                      (int32_T)sizeof(real_T), &pb_emlrtRTEI);
    for (k = 0; k < i; k++) {
      obj->pDataLinearIndices->data[k] = ii_data[k];
    }

    c_st.site = &cb_emlrtRSI;
  }

  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  c_st.site = &cb_emlrtRSI;
  c_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  c_st.site = &cb_emlrtRSI;
  c_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  memcpy(&b_recv[0], &recv_data[256], sizeof(creal_T) << 6);
  emxInit_creal_T(&st, &RLongSecond, 3, &ac_emlrtRTEI, TRUE);
  emxInit_creal_T(&st, &b_R, 3, &pb_emlrtRTEI, TRUE);
  b_st.site = &cb_emlrtRSI;
  OFDMDemodulator_stepImpl(&b_st, obj, b_recv, RLongSecond);

  /* Second half of long preamble */
  /*     %% Preamble Equalization */
  /*  Get Equalizer tap gains */
  k = RLongFirst->size[0];
  ii = RLongSecond->size[0];
  emlrtDimSizeEqCheckFastR2012b(k, ii, &x_emlrtECI, sp);
  st.site = &as_emlrtRSI;
  k = b_R->size[0] * b_R->size[1] * b_R->size[2];
  b_R->size[0] = RLongFirst->size[0];
  b_R->size[1] = 2;
  b_R->size[2] = 1;
  emxEnsureCapacity(&st, (emxArray__common *)b_R, k, (int32_T)sizeof(creal_T),
                    &pb_emlrtRTEI);
  i = RLongFirst->size[0];
  for (k = 0; k < i; k++) {
    b_R->data[k] = RLongFirst->data[k];
  }

  emxFree_creal_T(&RLongFirst);
  i = RLongSecond->size[0];
  for (k = 0; k < i; k++) {
    b_R->data[k + b_R->size[0]] = RLongSecond->data[k];
  }

  emxFree_creal_T(&RLongSecond);

  /*  Calculate Equalizer Taps with preamble symbols */
  /*  Calculate non-normalized channel gains */
  for (k = 0; k < 53; k++) {
    ii = b_R->size[0];
    i = 1 + k;
    emlrtDynamicBoundsCheckFastR2012b(i, 1, ii, &lb_emlrtBCI, &st);
  }

  i = b_R->size[0];
  for (k = 0; k < 2; k++) {
    for (ii = 0; ii < 53; ii++) {
      c_R[ii + 53 * k] = b_R->data[ii + i * k];
    }
  }

  emxFree_creal_T(&b_R);
  for (k = 0; k < 53; k++) {
    b_tx_longPreamble[k] = tx_longPreamble[k];
    b_tx_longPreamble[53 + k] = tx_longPreamble[k];
  }

  b_st.site = &bt_emlrtRSI;
  b_rdivide(c_R, b_tx_longPreamble, RNormal);

  /*  Known is the original Long Preamble symbols  */
  /*  Scale channel gains */
  b_st.site = &ct_emlrtRSI;
  d_abs(RNormal, dv13);
  memcpy(&dv14[0], &dv13[0], 106U * sizeof(real_T));
  b_st.site = &ct_emlrtRSI;
  b_power(dv14, dv13);
  b_st.site = &ct_emlrtRSI;
  c_mean(dv13, REnergy);
  b_st.site = &dt_emlrtRSI;
  d_mean(RNormal, RConj);
  for (k = 0; k < 53; k++) {
    RConj[k].im = -RConj[k].im;
    b_RConj[k] = RConj[k];
  }

  b_st.site = &et_emlrtRSI;
  c_rdivide(b_RConj, REnergy, RConj);

  /*  Separate data from preambles */
  /* recvData = recv(length(tx.preambles)+1:length(tx.preambles)+(hDataDemod.NumSymbols)*(tx.FFTLength+hDataDemod.CyclicPrefixLength)); */
  emlrtVectorVectorIndexCheckR2012b(1280, 1, 1, 960, &w_emlrtECI, sp);

  /*  CG */
  /*  OFDM Demod */
  st.site = &bs_emlrtRSI;
  obj = hDataDemod;
  if (!obj->isReleased) {
  } else {
    i_y = NULL;
    m49 = mxCreateCharArray(2, iv207);
    for (i = 0; i < 45; i++) {
      cv241[i] = cv242[i];
    }

    emlrtInitCharArrayR2013a(&st, 45, m49, cv241);
    emlrtAssign(&i_y, m49);
    j_y = NULL;
    m49 = mxCreateCharArray(2, iv208);
    for (i = 0; i < 4; i++) {
      cv243[i] = cv244[i];
    }

    emlrtInitCharArrayR2013a(&st, 4, m49, cv243);
    emlrtAssign(&j_y, m49);
    b_st.site = &cb_emlrtRSI;
    c_error(&b_st, message(&b_st, i_y, j_y, &emlrtMCI), &emlrtMCI);
  }

  if (!obj->isInitialized) {
    b_st.site = &cb_emlrtRSI;
    if (!obj->isInitialized) {
    } else {
      k_y = NULL;
      m49 = mxCreateCharArray(2, iv209);
      for (i = 0; i < 51; i++) {
        cv245[i] = cv246[i];
      }

      emlrtInitCharArrayR2013a(&b_st, 51, m49, cv245);
      emlrtAssign(&k_y, m49);
      l_y = NULL;
      m49 = mxCreateCharArray(2, iv210);
      for (i = 0; i < 5; i++) {
        cv247[i] = cv248[i];
      }

      emlrtInitCharArrayR2013a(&b_st, 5, m49, cv247);
      emlrtAssign(&l_y, m49);
      c_st.site = &cb_emlrtRSI;
      c_error(&c_st, message(&c_st, k_y, l_y, &emlrtMCI), &emlrtMCI);
    }

    c_st.site = &cb_emlrtRSI;
    g_SystemProp_matlabCodegenSetAn(obj);
    c_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    b_SystemCore_validateProperties();
    c_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    d_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    c_st.site = &cb_emlrtRSI;
    memset(&b_fullGrid[0], 1, 768U * sizeof(int8_T));
    for (k = 0; k < 12; k++) {
      for (ii = 0; ii < 11; ii++) {
        b_fullGrid[iv202[ii] + (k << 6)] = 0;
      }

      b_fullGrid[32 + (k << 6)] = 0;
    }

    d_st.site = &st_emlrtRSI;
    d_st.site = &st_emlrtRSI;
    for (k = 0; k < 48; k++) {
      b_fullGrid[iv211[k]] = 2;
    }

    d_st.site = &fs_emlrtRSI;
    for (k = 0; k < 768; k++) {
      c_fullGrid[k] = (b_fullGrid[k] == 1);
    }

    eml_find(c_fullGrid, c_ii_data, ii_size);
    b_ii_size[0] = ii_size[0];
    i = ii_size[0];
    for (k = 0; k < i; k++) {
      d_ii_data[k] = c_ii_data[k];
    }

    d_st.site = &fs_emlrtRSI;
    h_SystemProp_matlabCodegenSetAn(&d_st, obj, d_ii_data, b_ii_size);
    c_st.site = &cb_emlrtRSI;
  }

  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  c_st.site = &cb_emlrtRSI;
  c_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  c_st.site = &cb_emlrtRSI;
  c_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  b_st.site = &cb_emlrtRSI;
  b_OFDMDemodulator_stepImpl(SD, &b_st, obj, *(creal_T (*)[960])&recv_data[320],
    Rraw, RXPilots);

  /*  Expand equalizer gains to full frame size */
  st.site = &cs_emlrtRSI;
  i = 0;
  for (ii = 0; ii < 12; ii++) {
    ia = 0;
    for (k = 0; k < 53; k++) {
      preambleGainsFull[i] = RConj[ia];
      b_st.site = &ng_emlrtRSI;
      ia++;
      b_st.site = &og_emlrtRSI;
      i++;
    }
  }

  /*  Isolate pilot gains from preamble equalizer */
  /*  Needed to correctly adjust pilot gains */
  /*  Apply preamble equalizer gains to data and pilots */
  /* Correct pilots */
  for (i = 0; i < 3; i++) {
    iv212[i] = iv213[i];
  }

  for (k = 0; k < 3; k++) {
    b_Rraw[k] = Rraw->size[k];
  }

  emlrtSizeEqCheckNDR2012b(iv212, b_Rraw, &v_emlrtECI, sp);

  /* Correct data */
  /*     %% Pilot Equalization */
  /*  Get pilot-based equalizer gains */
  st.site = &ds_emlrtRSI;

  /*  Calculate Equalizer Taps with pilot symbols */
  /*  Calculate non-normalized channel gains */
  b_st.site = &vt_emlrtRSI;
  c_st.site = &k_emlrtRSI;
  for (k = 0; k < 12; k++) {
    for (ii = 0; ii < 4; ii++) {
      pilotEqGains_re = preambleGainsFull[((int32_T)
        c_tx_pilotLocationsWithoutGuard[ii] + 53 * k) - 1].re * RXPilots[ii + (k
        << 2)].re - preambleGainsFull[((int32_T)
        c_tx_pilotLocationsWithoutGuard[ii] + 53 * k) - 1].im * RXPilots[ii + (k
        << 2)].im;
      pilotEqGains_im = preambleGainsFull[((int32_T)
        c_tx_pilotLocationsWithoutGuard[ii] + 53 * k) - 1].re * RXPilots[ii + (k
        << 2)].im + preambleGainsFull[((int32_T)
        c_tx_pilotLocationsWithoutGuard[ii] + 53 * k) - 1].im * RXPilots[ii + (k
        << 2)].re;
      if (pilotEqGains_im == 0.0) {
        PilotNormal[ii + (k << 2)].re = pilotEqGains_re / tx_pilots[ii + (k << 2)];
        PilotNormal[ii + (k << 2)].im = 0.0;
      } else if (pilotEqGains_re == 0.0) {
        PilotNormal[ii + (k << 2)].re = 0.0;
        PilotNormal[ii + (k << 2)].im = pilotEqGains_im / tx_pilots[ii + (k << 2)];
      } else {
        PilotNormal[ii + (k << 2)].re = pilotEqGains_re / tx_pilots[ii + (k << 2)];
        PilotNormal[ii + (k << 2)].im = pilotEqGains_im / tx_pilots[ii + (k << 2)];
      }
    }
  }

  /*  Scale channel gains */
  b_st.site = &wt_emlrtRSI;
  for (k = 0; k < 48; k++) {
    c_st.site = &qc_emlrtRSI;
    d_st.site = &co_emlrtRSI;
    a[k] = muDoubleScalarHypot(PilotNormal[k].re, PilotNormal[k].im);
  }

  b_st.site = &wt_emlrtRSI;
  c_st.site = &n_emlrtRSI;
  d_st.site = &hp_emlrtRSI;
  for (k = 0; k < 48; k++) {
    d_st.site = &o_emlrtRSI;
    PilotEnergy[k] = a[k] * a[k];
  }

  b_st.site = &xt_emlrtRSI;
  c_st.site = &k_emlrtRSI;

  /*  Interpolate to data carrier size */
  for (k = 0; k < 48; k++) {
    if (-PilotNormal[k].im == 0.0) {
      b_PilotNormal[k].re = PilotNormal[k].re / PilotEnergy[k];
      b_PilotNormal[k].im = 0.0;
    } else if (PilotNormal[k].re == 0.0) {
      b_PilotNormal[k].re = 0.0;
      b_PilotNormal[k].im = -PilotNormal[k].im / PilotEnergy[k];
    } else {
      b_PilotNormal[k].re = PilotNormal[k].re / PilotEnergy[k];
      b_PilotNormal[k].im = -PilotNormal[k].im / PilotEnergy[k];
    }
  }

  b_st.site = &yt_emlrtRSI;
  resample(SD, &b_st, b_PilotNormal, pilotEqGains);

  /*  Apply Equalizer from Pilots */
  for (k = 0; k < 12; k++) {
    for (ii = 0; ii < 48; ii++) {
      preambleGainsFull_data[ii + 48 * k] = preambleGainsFull[((int32_T)
        tx_dataSubcarrierIndexies_data[tx_dataSubcarrierIndexies_size[0] * ii] +
        53 * k) - 1];
    }
  }

  for (k = 0; k < 12; k++) {
    memcpy(&b_preambleGainsFull_data[48 * k], &preambleGainsFull_data[48 * k],
           48U * sizeof(creal_T));
  }

  i = Rraw->size[0];
  for (k = 0; k < 12; k++) {
    for (ii = 0; ii < 48; ii++) {
      c_preambleGainsFull_data[ii + 48 * k].re = b_preambleGainsFull_data[ii +
        48 * k].re * Rraw->data[ii + i * k].re - b_preambleGainsFull_data[ii +
        48 * k].im * Rraw->data[ii + i * k].im;
      c_preambleGainsFull_data[ii + 48 * k].im = b_preambleGainsFull_data[ii +
        48 * k].re * Rraw->data[ii + i * k].im + b_preambleGainsFull_data[ii +
        48 * k].im * Rraw->data[ii + i * k].re;
    }
  }

  for (k = 0; k < 12; k++) {
    for (ii = 0; ii < 48; ii++) {
      pilotEqGains_re = pilotEqGains[ii + 48 * k].re;
      pilotEqGains_im = pilotEqGains[ii + 48 * k].im;
      preambleGainsFull_data_re = c_preambleGainsFull_data[ii + 48 * k].re;
      preambleGainsFull_data_im = c_preambleGainsFull_data[ii + 48 * k].im;
      R[ii + 48 * k].re = pilotEqGains_re * preambleGainsFull_data_re -
        pilotEqGains_im * preambleGainsFull_data_im;
      R[ii + 48 * k].im = pilotEqGains_re * preambleGainsFull_data_im +
        pilotEqGains_im * preambleGainsFull_data_re;
    }
  }

  /*  Save Gains for visualization */
  estimate->pilotEqGains.size[0] = 48;
  estimate->pilotEqGains.size[1] = 12;
  for (k = 0; k < 576; k++) {
    estimate->pilotEqGains.data[k] = pilotEqGains[k];
  }

  estimate->preambleEqGains.size[0] = 53;
  for (k = 0; k < 53; k++) {
    estimate->preambleEqGains.data[k] = RConj[k];
  }

  emlrtHeapReferenceStackLeaveFcnR2012b(sp);
}
Example #3
0
/*
 * Arguments    : double blat
 *                double blon
 *                double ranges
 *                double azm
 *                double major_axis
 *                double esquared
 *                double *plat
 *                double *plon
 * Return Type  : void
 */
void cget_latlon(double blat, double blon, double ranges, double azm, double
                 major_axis, double esquared, double *plat, double *plon)
{
  double c1;
  double c2;
  double D;
  double P;
  double ss;
  double S;
  double phi1;
  int ellipse;
  double onef;
  double f;
  double f4;
  double al12;
  int ii_size_idx_0;
  int ii_size_idx_1;
  int is1_size_idx_0;
  int is1_size_idx_1;
  signed char iv0[1];
  int loop_ub;
  int i0;
  signed char iv1[1];
  double th1;
  double costh1;
  double sinth1;
  double sina12;
  int im1_size_idx_0;
  int im1_size_idx_1;
  signed char iv2[1];
  signed char iv3[1];
  double cosa12;
  double M;
  double N;
  double dv0[1];
  double tmp_data[1];
  double dv1[1];
  int tmp_size[2];
  double b_tmp_data[1];
  int b_tmp_size[2];
  double c_tmp_data[1];
  double dv2[1];
  double dv3[1];
  double dv4[1];
  double dv5[1];
  int c_tmp_size[2];
  double dv6[1];
  double s1;
  double d0;
  double d;
  double dv7[1];
  double u;
  double V;
  double sind;
  double ds;
  double cosds;
  double sinds;
  double dv8[1];
  double al21;
  double phi2;
  double de;
  double b_ellipse;
  double lam2;

  /* CGET_LATLON        Get latitudes and longitudes along a line */
  /*  */
  /*        function [plat,plon]=cget_latlon(blat,blon,ranges,azm,major_axis); */
  /*  */
  /*  This is a translation to matlab of the USGS PROJ-4.4 geographic */
  /*  projection library to compute positions along a series of ranges */
  /*  along a given azimuth from a reference point. */
  /*  Uses ellipsoidal earth model set to Clark 1966 standard. */
  /*  */
  /*  Inputs: */
  /*        blat,blon       lat,lon coordinates of start point */
  /*                        in degrees (scalar) */
  /*  */
  /*        ranges          range in nmi (scalar or vector) */
  /*        azm             single-valued (same size as range) (degrees) */
  /*        major_axes - skip for an elliptical earth, set=0 for spherical Earth */
  /*  */
  /*  Outputs: */
  /*        plat,plon       lat,lons along path */
  /*  */
  c1 = 0.0;
  c2 = 0.0;
  D = 0.0;
  P = 0.0;
  ss = 0.0;
  if (major_axis == 0.0) {
    major_axis = 6.3782064E+6;
    esquared = 0.0;
  }

  /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  /*  now do the stuff that used to be in .c */
  S = ranges * 1852.0;
  phi1 = blat * 0.017453292519943295;
  ellipse = (esquared != 0.0);
  if (ellipse == 1) {
    onef = sqrt(1.0 - esquared);
    f = 1.0 - onef;
    f4 = (1.0 - onef) / 4.0;
  } else {
    onef = 1.0;
    f = 0.0;
    f4 = 0.0;
  }

  al12 = azm * 0.017453292519943295;
  cadjlon(&al12);
  if (fabs(al12) > 1.5707963267948966) {
    ii_size_idx_0 = 1;
    ii_size_idx_1 = 1;
  } else {
    ii_size_idx_0 = 0;
    ii_size_idx_1 = 0;
  }

  is1_size_idx_0 = ii_size_idx_0;
  is1_size_idx_1 = ii_size_idx_1;
  iv0[0] = 0;
  loop_ub = ii_size_idx_0 * ii_size_idx_1;
  i0 = 0;
  while (i0 <= loop_ub - 1) {
    iv0[0] = 1;
    i0 = 1;
  }

  if (fabs(al12) <= 1.5707963267948966) {
    ii_size_idx_0 = 1;
    ii_size_idx_1 = 1;
  } else {
    ii_size_idx_0 = 0;
    ii_size_idx_1 = 0;
  }

  iv1[0] = iv0[0];
  loop_ub = ii_size_idx_0 * ii_size_idx_1;
  i0 = 0;
  while (i0 <= loop_ub - 1) {
    iv1[0] = 0;
    i0 = 1;
  }

  if (ellipse == 1) {
    th1 = atan(onef * tan(phi1));
  } else {
    th1 = phi1;
  }

  costh1 = cos(th1);
  sinth1 = sin(th1);
  sina12 = sin(al12);
  if (fabs(sina12) < 1.0E-10) {
    ii_size_idx_0 = 1;
    ii_size_idx_1 = 1;
  } else {
    ii_size_idx_0 = 0;
    ii_size_idx_1 = 0;
  }

  im1_size_idx_0 = ii_size_idx_0;
  im1_size_idx_1 = ii_size_idx_1;
  iv2[0] = 0;
  loop_ub = ii_size_idx_0 * ii_size_idx_1;
  i0 = 0;
  while (i0 <= loop_ub - 1) {
    iv2[0] = 1;
    i0 = 1;
  }

  if (fabs(sina12) >= 1.0E-10) {
    ii_size_idx_0 = 1;
    ii_size_idx_1 = 1;
  } else {
    ii_size_idx_0 = 0;
    ii_size_idx_1 = 0;
  }

  iv3[0] = iv2[0];
  loop_ub = ii_size_idx_0 * ii_size_idx_1;
  i0 = 0;
  while (i0 <= loop_ub - 1) {
    iv3[0] = 0;
    i0 = 1;
  }

  if (iv3[0] == 1) {
    sina12 = 0.0;
    if (fabs(al12) < 1.5707963267948966) {
      cosa12 = 1.0;
    } else {
      cosa12 = -1.0;
    }

    M = 0.0;
  } else {
    cosa12 = cos(al12);
    M = costh1 * sina12;
  }

  N = costh1 * cosa12;
  if (ellipse == 1) {
    /*    if merid(j) == 1 */
    dv0[0] = 0.0;
    loop_ub = im1_size_idx_0 * im1_size_idx_1;
    i0 = 0;
    while (i0 <= loop_ub - 1) {
      dv0[0] = f4;
      i0 = 1;
    }

    tmp_data[0] = 0.0;
    loop_ub = im1_size_idx_0 * im1_size_idx_1;
    i0 = 0;
    while (i0 <= loop_ub - 1) {
      tmp_data[0] = 1.0 - dv0[0];
      i0 = 1;
    }

    dv1[0] = tmp_data[0];
    loop_ub = im1_size_idx_0 * im1_size_idx_1;
    i0 = 0;
    while (i0 <= loop_ub - 1) {
      dv1[0] = tmp_data[0] * tmp_data[0];
      i0 = 1;
    }

    tmp_size[0] = im1_size_idx_0;
    tmp_size[1] = im1_size_idx_1;
    loop_ub = im1_size_idx_0 * im1_size_idx_1;
    i0 = 0;
    while (i0 <= loop_ub - 1) {
      tmp_data[0] = dv0[0];
      i0 = 1;
    }

    loop_ub = im1_size_idx_0 * im1_size_idx_1;
    i0 = 0;
    while (i0 <= loop_ub - 1) {
      b_tmp_data[0] = dv1[0];
      i0 = 1;
    }

    b_rdivide(tmp_data, tmp_size, b_tmp_data, c_tmp_data, b_tmp_size);
    dv2[0] = 0.0;
    loop_ub = b_tmp_size[0] * b_tmp_size[1];
    for (i0 = 0; i0 < loop_ub; i0++) {
      dv2[0] = c_tmp_data[i0];
    }

    /*    else  */
    dv3[0] = 0.0;
    loop_ub = ii_size_idx_0 * ii_size_idx_1;
    i0 = 0;
    while (i0 <= loop_ub - 1) {
      dv3[0] = f * M;
      i0 = 1;
    }

    c1 = dv3[0];
    dv4[0] = dv0[0];
    loop_ub = ii_size_idx_0 * ii_size_idx_1;
    i0 = 0;
    while (i0 <= loop_ub - 1) {
      dv4[0] = f4 * (1.0 - M * M);
      i0 = 1;
    }

    c2 = dv4[0];
    dv5[0] = dv1[0];
    loop_ub = ii_size_idx_0 * ii_size_idx_1;
    i0 = 0;
    while (i0 <= loop_ub - 1) {
      dv5[0] = (1.0 - dv4[0]) * ((1.0 - dv4[0]) - dv3[0] * M);
      i0 = 1;
    }

    D = dv5[0];
    c_tmp_size[0] = ii_size_idx_0;
    c_tmp_size[1] = ii_size_idx_1;
    loop_ub = ii_size_idx_0 * ii_size_idx_1;
    i0 = 0;
    while (i0 <= loop_ub - 1) {
      tmp_data[0] = (1.0 + 0.5 * dv3[0] * M) * dv4[0];
      i0 = 1;
    }

    loop_ub = ii_size_idx_0 * ii_size_idx_1;
    i0 = 0;
    while (i0 <= loop_ub - 1) {
      b_tmp_data[0] = dv5[0];
      i0 = 1;
    }

    b_rdivide(tmp_data, c_tmp_size, b_tmp_data, c_tmp_data, b_tmp_size);
    dv6[0] = dv2[0];
    loop_ub = b_tmp_size[0] * b_tmp_size[1];
    for (i0 = 0; i0 < loop_ub; i0++) {
      dv6[0] = c_tmp_data[i0];
    }

    P = dv6[0];

    /*    end */
  }

  if (iv3[0] == 1) {
    s1 = 1.5707963267948966 - th1;
  } else {
    if (fabs(M) >= 1.0) {
      d0 = 0.0;
    } else {
      d0 = acos(M);
    }

    s1 = sinth1 / sin(d0);
    if (fabs(s1) >= 1.0) {
      s1 = 0.0;
    } else {
      s1 = acos(s1);
    }
  }

  if (ellipse == 1) {
    d = rdivide(S, D * major_axis);
    dv7[0] = d;
    loop_ub = is1_size_idx_0 * is1_size_idx_1;
    i0 = 0;
    while (i0 <= loop_ub - 1) {
      dv7[0] = -d;
      i0 = 1;
    }

    u = 2.0 * (s1 - dv7[0]);
    V = cos(u + dv7[0]);
    sind = sin(dv7[0]);
    ds = (dv7[0] + c2 * c2 * sind * cos(dv7[0]) * (2.0 * V * V - 1.0)) - 2.0 * P
      * V * (1.0 - 2.0 * P * cos(u)) * sind;
    ss = (s1 + s1) - ds;
  } else {
    ds = S / major_axis;
    dv7[0] = ds;
    loop_ub = is1_size_idx_0 * is1_size_idx_1;
    i0 = 0;
    while (i0 <= loop_ub - 1) {
      dv7[0] = -ds;
      i0 = 1;
    }

    ds = dv7[0];
  }

  cosds = cos(ds);
  sinds = sin(ds);
  dv8[0] = sinds;
  loop_ub = is1_size_idx_0 * is1_size_idx_1;
  i0 = 0;
  while (i0 <= loop_ub - 1) {
    dv8[0] = -sinds;
    i0 = 1;
  }

  al21 = N * cosds - sinth1 * dv8[0];
  if (iv3[0] == 1) {
    phi2 = atan(tan((1.5707963267948966 + s1) - ds) / onef);
    if (al21 > 0.0) {
      if (iv1[0] == 1) {
        de = 3.1415926535897931;
      } else {
        phi2 = -phi2;
        de = 0.0;
      }
    } else if (iv1[0] == 1) {
      phi2 = -phi2;
      de = 0.0;
    } else {
      de = 3.1415926535897931;
    }
  } else {
    al21 = atan(M / al21);
    if (al21 > 0.0) {
      al21 += 3.1415926535897931;
    }

    if (al12 < 0.0) {
      al21 -= 3.1415926535897931;
    }

    cadjlon(&al21);
    if (ellipse == 1) {
      b_ellipse = onef * M;
    } else {
      b_ellipse = M;
    }

    phi2 = atan(-(sinth1 * cosds + N * dv8[0]) * sin(al21) / b_ellipse);
    de = rt_atan2d_snf(dv8[0] * sina12, costh1 * cosds - sinth1 * dv8[0] *
                       cosa12);
    if (ellipse == 1) {
      if (iv1[0] == 1) {
        de += c1 * ((1.0 - c2) * ds + c2 * dv8[0] * cos(ss));
      } else {
        de -= c1 * ((1.0 - c2) * ds - c2 * dv8[0] * cos(ss));
      }
    }
  }

  lam2 = blon * 0.017453292519943295 + de;
  cadjlon(&lam2);
  *plon = lam2 * 57.295779513082323;
  *plat = phi2 * 57.295779513082323;
}