Exemple #1
0
//swap input vector
bool Reverse::algorithm() {

  KstVectorPtr input = inputVector(INPUT);
  KstVectorPtr output  = outputVector(OUTPUT);
  output->resize(input->length());
  int length = input->length();

  for (int i = 0; i < length; i++){
    output->value()[length-i-1] = input->value()[i];
  }

  return true;
}
Exemple #2
0
// Remove some elements from the vector starting from vector[0]
bool Trim::algorithm() {
  KstVectorPtr input = inputVector(INPUT);
  KstScalarPtr remove = inputScalar(REMOVE);
  KstVectorPtr cut = outputVector(CUT);
  bool rc = false;

  if (input->length() > remove->value()) {
    int cutSize = (int)input->length() - (int)remove->value();
    cut->resize( cutSize, false );
    for (int j=0; j<cutSize; j++) {
      cut->value()[j] = input->value()[(int)remove->value()+j];
    }

    rc = true;
  }

  return rc;
}
bool Differentiation::algorithm() {

  KstVectorPtr inputvector  = inputVector(INPUTVECTOR);
  KstScalarPtr time_step    = inputScalar(TIME_STEP);
  KstVectorPtr derivative   = outputVector(DERIVATIVE);

  /* Memory allocation */
  if (derivative->length() != inputvector->length()) {
    derivative->resize(inputvector->length(), true);
  }

  derivative->value()[0] = (inputvector->value()[1] - inputvector->value()[0]) / time_step->value();

  int i = 1;
  for (; i < inputvector->length()-1; i++) {
      derivative->value()[i] = (inputvector->value()[i+1] - inputvector->value()[i-1])/(2*time_step->value());
  }

  derivative->value()[i] = (inputvector->value()[i] - inputvector->value()[i-1]) / time_step->value();

  return true;
}
Exemple #4
0
bool NoiseAddition::algorithm() {
  KstVectorPtr array    = inputVector(ARRAY);
  KstScalarPtr sigma    = inputScalar(SIGMA);
  KstVectorPtr output   = outputVector(OUTPUT);

  const gsl_rng_type* pGeneratorType;
  gsl_rng* pRandomNumberGenerator;
  double* pResult[1];
  int iRetVal = false;
  int iLength = array->length();

  pResult[0] = 0L;

  if (iLength > 0) {
    if (output->length() != iLength) {
      output->resize(iLength, false);
      pResult[0] = (double*)realloc( output->value(), iLength * sizeof( double ) );
    } else {
      pResult[0] = output->value();
    }
  }

  pGeneratorType = gsl_rng_default;
  pRandomNumberGenerator = gsl_rng_alloc( pGeneratorType );
  if (pRandomNumberGenerator != NULL) {
    if (pResult[0] != NULL) {
      for (int i=0; i<iLength; i++) {
        output->value()[i] = array->value()[i] + gsl_ran_gaussian( pRandomNumberGenerator, sigma->value() );
      }

      iRetVal = true;
    }
    gsl_rng_free( pRandomNumberGenerator );
  }

  return iRetVal;
}
Exemple #5
0
bool KstEquation::FillY(bool force) {
  int v_shift=0, v_new;
  int i0=0;
  int ns;

  writeLockInputsAndOutputs();

  // determine value of Interp
  if (_doInterp) {
    ns = (*_xInVector)->length();
    for (KstVectorMap::ConstIterator i = VectorsUsed.begin(); i != VectorsUsed.end(); ++i) {
      if (i.data()->length() > ns) {
        ns = i.data()->length();
      }
    }
  } else {
    ns = (*_xInVector)->length();
  }

  if (_ns != (*_xInVector)->length() || ns != (*_xInVector)->length() ||
      (*_xInVector)->numShift() != (*_xInVector)->numNew()) {
    _ns = ns;

    KstVectorPtr xv = *_xOutVector;
    KstVectorPtr yv = *_yOutVector;
    if (!xv->resize(_ns)) {
      // FIXME: handle error?
      unlockInputsAndOutputs();
      return false;    
    }
    if (!yv->resize(_ns)) {
      // FIXME: handle error?
      unlockInputsAndOutputs();
      return false;
    }
    yv->zero();
    i0 = 0; // other vectors may have diffent lengths, so start over
    v_shift = _ns;
  } else {
    // calculate shift and new samples
    // only do shift optimization if all used vectors are same size and shift
    v_shift = (*_xInVector)->numShift();
    v_new = (*_xInVector)->numNew();

    for (KstVectorMap::ConstIterator i = VectorsUsed.begin(); i != VectorsUsed.end(); ++i) {
      if (v_shift != i.data()->numShift()) {
        v_shift = _ns;
      }
      if (v_new != i.data()->numNew()) {
        v_shift = _ns;
      }
      if (_ns != i.data()->length()) {
        v_shift = _ns;
      }
    }

    if (v_shift > _ns/2 || force) {
      i0 = 0;
      v_shift = _ns;
    } else {
      KstVectorPtr xv = *_xOutVector;
      KstVectorPtr yv = *_yOutVector;
      for (int i = v_shift; i < _ns; i++) {
        yv->value()[i - v_shift] = yv->value()[i];
        xv->value()[i - v_shift] = xv->value()[i];
      }
      i0 = _ns - v_shift;
    }
  }

  _numShifted = (*_yOutVector)->numShift() + v_shift;
  if (_numShifted > _ns) {
    _numShifted = _ns;
  }

  _numNew = _ns - i0 + (*_yOutVector)->numNew();
  if (_numNew > _ns) {
    _numNew = _ns;
  }

  (*_xOutVector)->setNewAndShift(_numNew, _numShifted);
  (*_yOutVector)->setNewAndShift(_numNew, _numShifted);

  double *rawxv = (*_xOutVector)->value();
  double *rawyv = (*_yOutVector)->value();
  KstVectorPtr iv = (*_xInVector);

  Equation::Context ctx;
  ctx.sampleCount = _ns;
  ctx.xVector = iv;

  if (!_pe) {
    if (_equation.isEmpty()) {
      unlockInputsAndOutputs();
      return true;
    }

    QMutexLocker ml(&Equation::mutex());
    yy_scan_string(_equation.latin1());
    int rc = yyparse();
    _pe = static_cast<Equation::Node*>(ParsedEquation);
    if (_pe && rc == 0) {
      Equation::FoldVisitor vis(&ctx, &_pe);
      KstStringMap sm;
      _pe->collectObjects(VectorsUsed, ScalarsUsed, sm);
      ParsedEquation = 0L;
    } else {
      delete (Equation::Node*)ParsedEquation;
      ParsedEquation = 0L;
      _pe = 0L;
      unlockInputsAndOutputs();
      return false;
    }
  }

  for (ctx.i = i0; ctx.i < _ns; ++ctx.i) {
    rawxv[ctx.i] = iv->value(ctx.i);
    ctx.x = iv->interpolate(ctx.i, _ns);
    rawyv[ctx.i] = _pe->value(&ctx);
  }

  if (!(*_xOutVector)->resize(iv->length())) {
    // FIXME: handle error?
    unlockInputsAndOutputs();
    return false;    
  }

  unlockInputsAndOutputs();
  return true;
}
Exemple #6
0
KstObject::UpdateType EventMonitorEntry::update(int updateCounter) {
  Q_ASSERT(myLockStatus() == KstRWLock::WRITELOCKED);

  bool force = dirty();
  setDirty(false);

  if (KstObject::checkUpdateCounter(updateCounter) && !force) {
    return lastUpdateResult();
  }

  writeLockInputsAndOutputs();

  if (!_pExpression) {
    reparse();
  }

  KstVectorPtr xv = *_xVector;
  KstVectorPtr yv = *_yVector;
  int ns = 1;

  for (KstVectorMap::ConstIterator i = _vectorsUsed.begin(); i != _vectorsUsed.end(); ++i) {
    ns = qMax(ns, i.value()->length());
  }

  double *rawValuesX = 0L;
  double *rawValuesY = 0L;
  if (xv && yv) {
    if (xv->resize(ns)) {
      rawValuesX = xv->value();
    }

    if (yv->resize(ns)) {
      rawValuesY = yv->value();
    }
  }

  Equation::Context ctx;
  ctx.sampleCount = ns;
  ctx.x = 0.0;

  if (needToEvaluate()) {
    if (_pExpression) {
      for (ctx.i = _numDone; ctx.i < ns; ++ctx.i) {
        const double value = _pExpression->value(&ctx);
        if (value != 0.0) { // The expression evaluates to true
          log(ctx.i);
          if (rawValuesX && rawValuesY) {
            rawValuesX[ctx.i] = ctx.i;
            rawValuesY[ctx.i] = 1.0;
          }
        } else {
          if (rawValuesX && rawValuesY) {
            rawValuesX[ctx.i] = ctx.i;
            rawValuesY[ctx.i] = 0.0;
          }
        }
      }
      _numDone = ns;
      logImmediately();
    }
  } else {
    _numDone = ns;
  }

  if (xv) {
    xv->setDirty();
    xv->update(updateCounter);
  }

  if (yv) {
    yv->setDirty();
    yv->update(updateCounter);
  }

  unlockInputsAndOutputs();

  return setLastUpdateResult(NO_CHANGE);
}
bool CrossCorrelate::algorithm() {

  KstVectorPtr array_one    = inputVector(ARRAY_ONE);
  KstVectorPtr array_two    = inputVector(ARRAY_TWO);
  KstVectorPtr step_value   = outputVector(STEP_VALUE);
  KstVectorPtr correlated   = outputVector(CORRELATED);

  if (array_one->length() <= 0               ||
      array_two->length() <= 0               ||
      array_one->length() != array_two->length()) {
      return false;
  }

  double* pdArrayOne;
  double* pdArrayTwo;
  double* pdResult[2];
  double  dReal;
  double  dImag;

  int iLength;
  int iLengthNew;

  bool iReturn = false;

  //
  // zero-pad the array...
  //
  iLength  = array_one->length();
  iLength *= 2;

  step_value->resize(array_one->length(), false);
  correlated->resize(array_two->length(), false);

  //
  // round iLength up to the nearest power of two...
  //
  iLengthNew = 64;
  while( iLengthNew < iLength && iLengthNew > 0) {
    iLengthNew *= 2;
  }
  iLength = iLengthNew;

  if (iLength <= 0)
    return false;

  pdArrayOne = new double[iLength];
  pdArrayTwo = new double[iLength];
  if (pdArrayOne != NULL && pdArrayTwo != NULL) {
    //
    // zero-pad the two arrays...
    //
    memset( pdArrayOne, 0, iLength * sizeof( double ) );
    memcpy( pdArrayOne, array_one->value(), array_one->length() * sizeof( double ) );

    memset( pdArrayTwo, 0, iLength * sizeof( double ) );
    memcpy( pdArrayTwo, array_two->value(), array_two->length() * sizeof( double ) );

    //
    // calculate the FFTs of the two functions...
    //
    if (gsl_fft_real_radix2_transform( pdArrayOne, 1, iLength ) == 0) {
      if (gsl_fft_real_radix2_transform( pdArrayTwo, 1, iLength ) == 0) {
        //
        // multiply one FFT by the complex conjugate of the other...
        //
        for (int i=0; i<iLength/2; i++) {
          if (i==0 || i==(iLength/2)-1) {
            pdArrayOne[i] = pdArrayOne[i] * pdArrayTwo[i];
          } else {
            dReal = pdArrayOne[i] * pdArrayTwo[i] + pdArrayOne[iLength-i] * pdArrayTwo[iLength-i];
            dImag = pdArrayOne[i] * pdArrayTwo[iLength-i] - pdArrayOne[iLength-i] * pdArrayTwo[i];

            pdArrayOne[i] = dReal;
            pdArrayOne[iLength-i] = dImag;
          }
        }

        //
        // do the inverse FFT...
        //
        if (gsl_fft_halfcomplex_radix2_inverse( pdArrayOne, 1, iLength ) == 0) {
          if (step_value->length() != array_one->length()) {
            pdResult[0] = (double*)realloc( step_value->value(), array_one->length() * sizeof( double ) );
          } else {
            pdResult[0] = step_value->value();
          }

          if (correlated->length() != array_two->length()) {
            pdResult[1] = (double*)realloc( correlated->value(), array_two->length() * sizeof( double ) );
          } else {
            pdResult[1] = correlated->value();
          }

          if (pdResult[0] != NULL && pdResult[1] != NULL) {
            for (int i = 0; i < array_one->length(); ++i) {
              step_value->value()[i] = pdResult[0][i];
            }
            for (int i = 0; i < array_two->length(); ++i) {
              correlated->value()[i] = pdResult[1][i];
            }

            for (int i = 0; i < array_one->length(); i++) {
                step_value->value()[i] = (double)( i - ( array_one->length() / 2 ) );
            }

            memcpy( &(correlated->value()[array_one->length() / 2]),
                    &(pdArrayOne[0]),
                    ( ( array_one->length() + 1 ) / 2 ) * sizeof( double ) );

            memcpy( &(correlated->value()[0]),
                    &(pdArrayOne[iLength - (array_one->length() / 2)]),
                    ( array_one->length() / 2 ) * sizeof( double ) );

            iReturn = true;
          }
        }
      }
    }
  }
  delete[] pdArrayOne;
  delete[] pdArrayTwo;

  return iReturn;
}
void CrossPowerSpectrum::crossspectrum() {
  KstVectorPtr v1 = *_inputVectors.find(VECTOR_ONE);
  KstVectorPtr v2 = *_inputVectors.find(VECTOR_TWO);

  KstScalarPtr fft = *_inputScalars.find(FFT_LENGTH);
  KstScalarPtr sample = *_inputScalars.find(SAMPLE_RATE);

  KstVectorPtr real = *_outputVectors.find(REAL);
  KstVectorPtr imaginary = *_outputVectors.find(IMAGINARY);
  KstVectorPtr frequency = *_outputVectors.find(FREQUENCY);

  double SR = sample->value(); // sample rate
  double df;
  int i,  xps_len;
  double *a,  *b;
  double mean_a,  mean_b;
  int dv0,  dv1,  v_len;
  int i_subset,  n_subsets;
  int i_samp,  copyLen;
  double norm_factor;

  /* parse fft length */
  xps_len = int( fft->value() - 0.99);
  if ( xps_len > KSTPSDMAXLEN ) xps_len = KSTPSDMAXLEN;
  if ( xps_len<2 ) xps_len = 2;
  xps_len = int ( pow( 2,  xps_len ) );

  /* input vector lengths */
  v_len = ( ( v1->length() < v2->length() ) ?
            v1->length() : v2->length() );
  dv0 = v_len/v1->length();
  dv1 = v_len/v2->length();

  while ( xps_len > v_len ) xps_len/=2;

  // allocate the lengths
  if ( real->length() != xps_len ) {
    real->resize( xps_len, false );
    imaginary->resize( xps_len, false );
    frequency->resize( xps_len, false );
  }

  /* Fill the frequency and zero the xps */
  df = SR/( 2.0*double( xps_len-1 ) );
  for ( i=0; i<xps_len; i++ ) {
    frequency->value()[i] = double( i ) * df;
    real->value()[i] = 0.0;
    imaginary->value()[i] = 0.0;
  }

  /* allocate input arrays */
  int ALen = xps_len * 2;
  a = new double[ALen];
  b = new double[ALen];

  /* do the fft's */
  n_subsets = v_len/xps_len + 1;

  for ( i_subset=0; i_subset<n_subsets; i_subset++ ) {
        /* copy each chunk into a[] and find mean */
    if (i_subset*xps_len + ALen <= v_len) {
      copyLen = ALen;
    } else {
      copyLen = v_len - i_subset*xps_len;
    }
    mean_b = mean_a = 0;
    for (i_samp = 0; i_samp < copyLen; i_samp++) {
      i = ( i_samp + i_subset*xps_len )/dv0;
      mean_a += (
        a[i_samp] = v1->value()[i]
        );
      i = ( i_samp + i_subset*xps_len )/dv1;
      mean_b += (
        b[i_samp] = v2->value()[i]
        );
    }
    if (copyLen>1) {
      mean_a/=(double)copyLen;
      mean_b/=(double)copyLen;
    }

    /* Remove Mean and apodize */
    for (i_samp=0; i_samp<copyLen; i_samp++) {
      a[i_samp] -= mean_a;
      b[i_samp] -= mean_b;
    }

    for (;i_samp < ALen; i_samp++) {
      a[i_samp] = 0.0;
      b[i_samp] = 0.0;
    }

    /* fft */
    rdft(ALen, 1, a);
    rdft(ALen, 1, b);

    /* sum each bin into psd[] */
    real->value()[0] += ( a[0]*b[0] );
    real->value()[xps_len-1] += ( a[1]*b[1] );
    for (i_samp=1; i_samp<xps_len-1; i_samp++) {
      real->value()[i_samp]+= ( a[i_samp*2] * b[i_samp*2] +
                                   a[i_samp*2+1] * b[i_samp*2+1] );
      imaginary->value()[i_samp]+= ( -a[i_samp*2] * b[i_samp*2+1] +
                                   a[i_samp*2+1] * b[i_samp*2] );
    }// (a+ci)(b+di)* = ab+cd +i(-ad + cb)
  }

  /* renormalize */
  norm_factor = 1.0/((double(SR)*double(xps_len))*double(n_subsets));
  for ( i=0; i<xps_len; i++ ) {
    real->value()[i]*=norm_factor;
    imaginary->value()[i]*=norm_factor;
  }

  /* free */
  delete[] b;
  delete[] a;
//   return 0;
}
Exemple #9
0
bool Phase::algorithm() {

  KstVectorPtr time     = inputVector(TIME);
  KstVectorPtr data_i   = inputVector(DATA_I);
  KstScalarPtr period   = inputScalar(PERIOD);
  KstScalarPtr zero     = inputScalar(ZERO);
  KstVectorPtr phase    = outputVector(PHASE);
  KstVectorPtr data_o   = outputVector(DATA_O);

  double* pResult[2];
  double  dPhasePeriod = period->value();
  double dPhaseZero = zero->value();
  int iLength;

  bool iRetVal = false;

  if (dPhasePeriod > 0.0) {
    if (time->length() == data_i->length()) {
      iLength = time->length();

      if (phase->length() != iLength) {
        phase->resize(iLength, true);
        pResult[0] = (double*)realloc( phase->value(), iLength * sizeof( double ) );
      } else {
        pResult[0] = phase->value();
      }

      if (data_o->length() != iLength) {
        data_o->resize(iLength, true);
        pResult[1] = (double*)realloc( data_o->value(), iLength * sizeof( double ) );
      } else {
        pResult[1] = data_o->value();
      }

      if (pResult[0] != NULL && pResult[1] != NULL) {
        for (int i = 0; i < phase->length(); ++i) {
          phase->value()[i] = pResult[0][i];
        }
        for (int i = 0; i < data_o->length(); ++i) {
          data_o->value()[i] = pResult[1][i];
        }

        /*
        determine the phase...
        */
        for (int i=0; i<iLength; i++) {
          phase->value()[i] = fmod( ( time->value()[i] - dPhaseZero ) / dPhasePeriod, 1.0 );
        }

        /*
        sort by phase...
        */
        memcpy( data_o->value(), data_i->value(), iLength * sizeof( double ) );
        double* sort[2];
        sort[0] = phase->value();
        sort[1] = data_o->value();
        quicksort( sort, 0, iLength-1 );

        iRetVal = true;
      }
    }
  }

  return iRetVal;
}
Exemple #10
0
bool Normalization::algorithm() {
  KstVectorPtr vectorIn = inputVector(VECTOR_IN);
  KstVectorPtr vectorOut = outputVector(VECTOR_OUT);
  double *arr;
  double *Yi;
  int iLength = vectorIn->length();
  int w = 1;

  arr = new double[iLength];
  Yi = new double[iLength];

  for(int i=0; i<iLength; i++)
  {
    Yi[i] = vectorIn->value()[i];
  }

  //
  // exclude peak values
  //
  for(int loop=0; loop<2; loop++)
  {
    for(int i=0; i<iLength; i++)
    {
      arr[i] = Yi[i];
    }

    for(int i=0; i<iLength; i++)
    {
      if(isMin(Yi, i, iLength) || isMax(Yi, i, iLength))
      {
        excludePts(arr, i, w, iLength);
      }
    }

    searchHighPts(arr, iLength);
    interpolate(Yi, arr, iLength);
  }

  //
  // do a piecewise linear fit
  //
  vectorOut->resize(iLength, false);

  int L = 3;
  double cof[2] = { 0.0, 0.0 };

  for(int i=0; i<iLength; i=i+L)
  {
    fit(i, L, iLength, Yi, cof, vectorOut);
  }

  //
  // normalize
  //
  for(int i=0; i<iLength; i++)
  {
    vectorOut->value()[i] = vectorIn->value()[i] / vectorOut->value()[i];
  }

  //
  // exclude off points
  //
  for(int i=0; i<iLength; i++)
  {
    if(vectorOut->value()[i] < 0.0 || vectorOut->value()[i] > 1.2)
    {
      vectorOut->value()[i] = NOPOINT;
    }
  }

  delete[] arr;
  delete[] Yi;

  return true;
}