Exemple #1
0
/*
 * Arguments    : const double Yin[128]
 *                double Ypk_data[]
 *                int Ypk_size[2]
 *                double Xpk_data[]
 *                int Xpk_size[2]
 * Return Type  : void
 */
void findpeaks(const double Yin[128], double Ypk_data[], int Ypk_size[2], double
               Xpk_data[], int Xpk_size[2])
{
  int iInflect_size[1];
  double iInflect_data[128];
  int iInfite_size[1];
  double iInfite_data[128];
  int iFinite_size[1];
  double iFinite_data[128];
  int loop_ub;
  int i1;
  boolean_T Yin_data[128];
  int Yin_size[1];
  int tmp_data[256];
  int iPk_size[1];
  double iPk_data[256];
  int ib_data[128];
  int ia_data[128];
  double b_iPk_data[256];
  double c_iPk_data[256];
  double d_iPk_data[256];
  int b_iPk_size[1];
  getAllPeaks(Yin, iFinite_data, iFinite_size, iInfite_data, iInfite_size,
              iInflect_data, iInflect_size);
  iInflect_size[0] = iFinite_size[0];
  loop_ub = iFinite_size[0];
  for (i1 = 0; i1 < loop_ub; i1++) {
    iInflect_data[i1] = iFinite_data[i1];
  }

  if (!(iFinite_size[0] == 0)) {
    Yin_size[0] = iFinite_size[0];
    loop_ub = iFinite_size[0];
    for (i1 = 0; i1 < loop_ub; i1++) {
      Yin_data[i1] = (Yin[(int)iFinite_data[i1] - 1] > rtMinusInf);
    }

    eml_li_find(Yin_data, Yin_size, tmp_data, iFinite_size);
    iInflect_size[0] = iFinite_size[0];
    loop_ub = iFinite_size[0];
    for (i1 = 0; i1 < loop_ub; i1++) {
      iInflect_data[i1] = iFinite_data[tmp_data[i1] - 1];
    }
  }

  removePeaksBelowThreshold(Yin, iInflect_data, iInflect_size);
  iPk_size[0] = iInflect_size[0];
  loop_ub = iInflect_size[0];
  for (i1 = 0; i1 < loop_ub; i1++) {
    iPk_data[i1] = iInflect_data[i1];
  }

  do_vectors(iPk_data, iPk_size, iInfite_data, iInfite_size, b_iPk_data,
             iFinite_size, ia_data, iInflect_size, ib_data, Yin_size);
  c_findPeaksSeparatedByMoreThanM(Yin, b_iPk_data, iFinite_size, iPk_data,
    iPk_size);
  if (iPk_size[0] > 128) {
    memcpy(&c_iPk_data[0], &iPk_data[0], sizeof(double) << 7);
    iPk_size[0] = 128;
    for (i1 = 0; i1 < 128; i1++) {
      iPk_data[i1] = c_iPk_data[i1];
    }
  }

  for (i1 = 0; i1 < 128; i1++) {
    iFinite_data[i1] = 1.0 + (double)i1;
  }

  b_iPk_size[0] = iPk_size[0];
  loop_ub = iPk_size[0];
  for (i1 = 0; i1 < loop_ub; i1++) {
    d_iPk_data[i1] = b_iPk_data[(int)iPk_data[i1] - 1];
  }

  assignOutputs(Yin, iFinite_data, d_iPk_data, b_iPk_size, Ypk_data, Ypk_size,
                Xpk_data, Xpk_size);
}
void locateShortpreamble(const emlrtStack *sp, const real_T M_data[1224], real_T
  *preambleEstimatedLocation, real_T *numPeaks)
{
  int32_T ixstart;
  real_T thresholdNorm;
  int32_T ix;
  boolean_T exitg1;
  boolean_T b_M_data[1224];
  int32_T ii_size[1];
  int32_T ii_data[1224];
  int32_T MLocations_size_idx_0;
  int32_T loop_ub;
  real_T MLocations_data[1224];
  int16_T unnamed_idx_0;
  int32_T peaks_data[1224];
  int32_T i20;
  real_T b_MLocations_data[1224];
  int32_T MLocations_size[1];
  real_T MLocations[8];
  int32_T ib_size[1];
  int32_T ib_data[8];
  int32_T ia_size[1];
  int32_T ia_data[8];
  int32_T c_size[1];
  real_T c_data[8];
  int32_T peaks_size[1];
  emlrtStack st;
  emlrtStack b_st;
  emlrtStack c_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;

  /*  Locate of the start of the actual preamble from timing metric */
  /* % Find peaks of correlation */
  /*  Adjust threshold */
  st.site = &pp_emlrtRSI;
  b_st.site = &we_emlrtRSI;
  ixstart = 1;
  thresholdNorm = M_data[0];
  if (muDoubleScalarIsNaN(M_data[0])) {
    ix = 2;
    exitg1 = FALSE;
    while ((exitg1 == FALSE) && (ix <= 1224)) {
      ixstart = ix;
      if (!muDoubleScalarIsNaN(M_data[ix - 1])) {
        thresholdNorm = M_data[ix - 1];
        exitg1 = TRUE;
      } else {
        ix++;
      }
    }
  }

  if (ixstart < 1224) {
    while (ixstart + 1 <= 1224) {
      if (M_data[ixstart] > thresholdNorm) {
        thresholdNorm = M_data[ixstart];
      }

      ixstart++;
    }
  }

  st.site = &pp_emlrtRSI;
  thresholdNorm *= 0.6;
  st.site = &qp_emlrtRSI;
  for (ixstart = 0; ixstart < 1224; ixstart++) {
    b_M_data[ixstart] = (M_data[ixstart] > thresholdNorm);
  }

  b_st.site = &vc_emlrtRSI;
  b_eml_find(b_M_data, ii_data, ii_size);

  /*  Correct estimate to start of preamble not its center */
  MLocations_size_idx_0 = ii_size[0];
  loop_ub = ii_size[0];
  for (ixstart = 0; ixstart < loop_ub; ixstart++) {
    MLocations_data[ixstart] = (real_T)ii_data[ixstart] - 9.0;
  }

  /*  Frame Detection */
  unnamed_idx_0 = (int16_T)ii_size[0];
  loop_ub = (int16_T)ii_size[0];
  for (ixstart = 0; ixstart < loop_ub; ixstart++) {
    peaks_data[ixstart] = 0;
  }

  /*  Determine correct peak  */
  st.site = &rp_emlrtRSI;
  ix = 0;
  while (ix <= ii_size[0] - 1) {
    ixstart = ix + 1;
    emlrtDynamicBoundsCheckFastR2012b(ixstart, 1, ii_size[0], &cb_emlrtBCI, sp);
    if (ix + 1 > ii_size[0]) {
      ixstart = 1;
      i20 = 1;
    } else {
      ixstart = emlrtDynamicBoundsCheckFastR2012b(ix + 1, 1, ii_size[0],
        &db_emlrtBCI, sp);
      i20 = emlrtDynamicBoundsCheckFastR2012b(ii_size[0], 1, ii_size[0],
        &db_emlrtBCI, sp) + 1;
    }

    emlrtVectorVectorIndexCheckR2012b(ii_size[0], 1, 1, i20 - ixstart,
      &s_emlrtECI, sp);
    st.site = &sp_emlrtRSI;
    b_st.site = &eq_emlrtRSI;
    MLocations_size[0] = i20 - ixstart;
    loop_ub = i20 - ixstart;
    for (i20 = 0; i20 < loop_ub; i20++) {
      b_MLocations_data[i20] = MLocations_data[(ixstart + i20) - 1];
    }

    for (ixstart = 0; ixstart < 8; ixstart++) {
      MLocations[ixstart] = MLocations_data[ix] + (16.0 + 16.0 * (real_T)ixstart);
    }

    c_st.site = &fq_emlrtRSI;
    do_vectors(&c_st, b_MLocations_data, MLocations_size, MLocations, c_data,
               c_size, ia_data, ia_size, ib_data, ib_size);
    st.site = &sp_emlrtRSI;
    ixstart = (int16_T)ii_size[0];
    peaks_data[emlrtDynamicBoundsCheckFastR2012b(ix + 1, 1, ixstart,
      &gb_emlrtBCI, sp) - 1] = c_size[0];
    ix++;
    emlrtBreakCheckFastR2012b(emlrtBreakCheckR2012bFlagVar, sp);
  }

  /*  Have at least 5 peaks for positive match */
  /*  (TUNABLE) */
  peaks_size[0] = (int16_T)ii_size[0];
  loop_ub = (int16_T)ii_size[0];
  for (ixstart = 0; ixstart < loop_ub; ixstart++) {
    b_M_data[ixstart] = (peaks_data[ixstart] < 7);
  }

  st.site = &tp_emlrtRSI;
  eml_li_find(&st, b_M_data, peaks_size, ii_data, ii_size);
  loop_ub = ii_size[0];
  for (ixstart = 0; ixstart < loop_ub; ixstart++) {
    i20 = (int16_T)MLocations_size_idx_0;
    peaks_data[emlrtDynamicBoundsCheckFastR2012b(ii_data[ixstart], 1, i20,
      &eb_emlrtBCI, sp) - 1] = 0;
  }

  /*  Pick earliest peak in time */
  if (!(unnamed_idx_0 == 0)) {
    st.site = &up_emlrtRSI;
    b_st.site = &ir_emlrtRSI;
    ixstart = peaks_data[0];
    loop_ub = 1;
    if (unnamed_idx_0 > 1) {
      for (ix = 2; ix <= unnamed_idx_0; ix++) {
        if (peaks_data[ix - 1] > ixstart) {
          ixstart = peaks_data[ix - 1];
          loop_ub = ix;
        }
      }
    }

    *numPeaks = ixstart;
    if (ixstart > 0) {
      *preambleEstimatedLocation =
        MLocations_data[emlrtDynamicBoundsCheckFastR2012b(loop_ub, 1,
        MLocations_size_idx_0, &fb_emlrtBCI, sp) - 1];
    } else {
      *preambleEstimatedLocation = -1.0;

      /*  no desirable location found */
    }
  } else {
    *preambleEstimatedLocation = -1.0;
    *numPeaks = 0.0;
  }

  /*  Normalize max peaks found */
  st.site = &vp_emlrtRSI;
  b_st.site = &j_emlrtRSI;
  *numPeaks /= 8.0;
}