Exemplo n.º 1
0
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// getErrorDerivative
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
RealVector MultiLayerPerceptron::calcErrorAndGradient( const RealVector& input, const RealVector& target, double& error )
{
   /// The current state on the nodes will be used for backpropagation.
   error = calcError( input, target );

   if ( m_deltaE.size() == 0 )
   {
      /// Calculate the gradient components with a single backward propagation if there are no hidden layers.
      propagateBackward( m_deltaEOutput, m_deltaEInput, m_weights[ 0 ] );
      for ( size_t iInput = 0; iInput < m_weights[ 0 ].size(); ++iInput )
      {
         for ( size_t iOutput = 0; iOutput < m_weights[ 0 ][ iInput ].size(); ++iOutput )
         {
            m_weightDerivatives[ 0 ][ iInput ][ iOutput ] = m_input[ iInput ] * m_deltaEOutput[ iOutput ];
         }
      }
   }
   else
   {
      /// Calculate the derivatives of the activation function on all the hidden nodes. This is independent of
      /// backpropagation.
      calcDerivativesActivationFunc();

      /// Propagate output deltas to first hidden node.
      propagateBackward( m_deltaEOutput, m_deltaE.back(), m_weights.back() );
      for ( size_t i = 0; i < m_deltaE.back().size(); ++i )
      {
         m_deltaE.back()[ i ] *= m_dfdy.back()[ i ];
      }

      /// Calculate deltas for all hidden nodes.
      /// delta_nk = f'(y_nk) * sum_i w_nki delta_(n+1)i
      /// propageBackward calculates the sum.
      for ( size_t iHiddenLayer = m_deltaE.size() - 1; iHiddenLayer > 0; --iHiddenLayer )
      {
         propagateBackward( m_deltaE[ iHiddenLayer ], m_deltaE[ iHiddenLayer - 1 ], m_weights[ iHiddenLayer ] );
         for ( size_t iNeuron = 0; iNeuron < m_deltaE[ iHiddenLayer - 1 ].size(); ++iNeuron )
         {
            m_deltaE[ iHiddenLayer - 1 ][ iNeuron ] *= m_dfdy[ iHiddenLayer - 1 ][ iNeuron ];
         }
      }

      /// Calculate the derivatives. dE/dw_nij = x_(n-1)j * delta_i
      for ( size_t iLayer = 0; iLayer < m_weights.size(); ++iLayer )
      {
         const RealVector& sourceNeuronActivations = iLayer != 0 ? m_x[ iLayer - 1 ] : m_input;
         const RealVector& deltaVec = iLayer < m_deltaE.size() ? m_deltaE[ iLayer ] : m_deltaEOutput;
         for ( size_t iSourceNeuron = 0; iSourceNeuron < m_weights[ iLayer ].size(); ++iSourceNeuron )
         {
            for ( size_t iTargetNeuron = 0; iTargetNeuron < m_weights[ iLayer ][ iSourceNeuron ].size(); ++iTargetNeuron )
            {
               m_weightDerivatives[ iLayer ][ iSourceNeuron ][ iTargetNeuron ] = sourceNeuronActivations[ iSourceNeuron ] * deltaVec[ iTargetNeuron ];
            }
         }
      }
   }

   /// @see composeGradient
   return composeGradient();
}
Exemplo n.º 2
0
void prob2b(){
  std::cout << "Problem 2 Part B ======== " << std::endl;

  Matrix data1(10000,std::vector<double>(2));
  Matrix data2(10000,std::vector<double>(2));
  loadFile(data1, "sample_data/output1");
  loadFile(data2, "sample_data/output3");

  Matrix points1 = randSample(data1, 1000);
  Matrix points2 = randSample(data2, 1000);

  std::vector<double> mean1 = getSampleMean(points1);
  std::vector<double> mean2 = getSampleMean(points2);

  std::cout << "sample_mean1 = ";
  print_vec(mean1);
  std::cout << "sample_mean2 = ";
  print_vec(mean2);

  Matrix cov1 = getSampleVar(points1, mean1);
  Matrix cov2 = getSampleVar(points2, mean2);

  std::cout << "sample_cov1 = ";
  print_matrix(cov1);
  std::cout << "sample_cov2 = ";
  print_matrix(cov2);

  std::cout << "With estimated params: " << std::endl;
  QuadraticDiscriminant classifier1(mean1, mean2, cov1, cov2);
  calcError(classifier1,data1,data2, "sample_data/labels1");
  std::cout << std::endl;
}
Exemplo n.º 3
0
lbfgsfloatval_t evaluate(void *instance, const lbfgsfloatval_t *x, lbfgsfloatval_t *g, const int n, const lbfgsfloatval_t step) {
    LbfgsInput* inp = (LbfgsInput*)instance;    
    
    Matrix *w = formMatrixFromFloatVal(x, inp->y->nrow, inp->L);
    double fx = calcError(inp->y, w, inp->target, inp->jobs);

    Matrix *dedw = calcErrorGrad(inp->y, w, inp->target, inp->jobs);
    for (size_t i=0; i<n; i++) {
        g[i] = dedw->vals[i];
    }
    return fx; 
}
Exemplo n.º 4
0
	real Optimizer::deconvolve(const TVReal &tlog, const TVComplex &v)
	{
		//		_rm->dump("out");

		real normak, normab;
		updateNorma(tlog, v, normak, normab);
		real initError = calcError(TVReal(), TVComplex(), tlog, v, normak, normab);
		if(!initError)
		{
			initError = 1;
		}
		real error = 1;

		int iters = _rm->solve_v(
			tlog.size(), &tlog[0], &v[0],
			_periodLogS.size(), &_periodLogS[0], &_valueS[0],
			25, _work);

		error = calcError(_periodLogS, _valueS, tlog, v, normak, normab) / initError;
		return error;
	}
Exemplo n.º 5
0
void prob2a(){
  std::cout << "Problem 2 Part A ======== " << std::endl;

  Matrix points1(10000,std::vector<double>(2));
  Matrix points2(10000,std::vector<double>(2));
  loadFile(points1, "sample_data/output1");
  loadFile(points2, "sample_data/output3");

  std::vector<double> mean1 = getSampleMean(points1);
  std::vector<double> mean2 = getSampleMean(points2);

  std::cout << "sample_mean1 = ";
  print_vec(mean1);
  std::cout << "sample_mean2 = ";
  print_vec(mean2);

  Matrix cov1 = getSampleVar(points1, mean1);
  Matrix cov2 = getSampleVar(points2, mean2);

  std::cout << "sample_cov1 = ";
  print_matrix(cov1);
  std::cout << "sample_cov2 = ";
  print_matrix(cov2);

  std::cout << "With estimated params: " << std::endl;
  QuadraticDiscriminant classifier1(mean1, mean2, cov1, cov2);
  calcError(classifier1,points1,points2, "sample_data/labels1");

  std::cout << "Given params: " << std::endl;
  mean1 = {1.0,1.0};
  mean2 = {6.0,6.0};
  cov1 = {{2.0,0},{0,2.0}};
  cov2 = {{4.0,0},{0,8.0}};

  QuadraticDiscriminant classifier2(mean1, mean2, cov1, cov2);
  calcError(classifier2,points1,points2, "sample_data/labels1");
  std::cout << std::endl;
}
double BPLearning::run(std::vector <double> input,std::vector <double> output){
	//ÕýÏò¼ÆËã

	for(int i=0,n=(int)output.size();i<n;++i)
		output[i] = network.fun->f(output[i]);

	network.compute(input);
	
	double sumError = calcError(output);
	
	calcUpdate(input);

	update();

	return sumError;
}
Exemplo n.º 7
0
/*!
   main thread function. Get data buffer, perform fft and scaling,
   write to spectrum buffer.
 */
void Spectrum::processData(unsigned char *data, unsigned char bptr)
{
    unsigned int  j    = bptr * sizes.advance_size;
    unsigned char *ptr = &data[j];
    for (int i = 0; i < sizes.sample_length; i++) {
        double tmpr;
        double tmpi;
        switch (bits) {
        case 16:
        {
            // convert 16 bit
            int ii = ptr[1];
            ii = (ii << 8) | ptr[0];
            if (ii & 0x8000) ii |= ~0xffff;
            tmpr = ii / 32768.0;
            ptr += 2;
            ii   = ptr[1];
            ii   = (ii << 8) | ptr[0];
            if (ii & 0x8000) ii |= ~0xffff;
            tmpi = ii / 32768.0;
            ptr += 2;
            j   += 4;
        }
        break;
        case 24:
        {
            // convert 24 bit signed integer sample to floating point by placing data into
            // 32-bit signed int
            // data is in ptr[0](LSB) ... ptr[2](MSB)
            //
            int ii = (ptr[2] << 24) | (ptr[1] << 16) | (ptr[0] << 8);

            // divide by (2^31 - 1) - (2^8 -1) = 2147483647 - 255 = 2147483392
            // actual float range then [1.0: -1.000000119]
            tmpr = ii / 2147483392.0;
            ptr += 3;

            // repeat for other stereo channel sample
            ii   = (ptr[2] << 24) | (ptr[1] << 16) | (ptr[0] << 8);
            tmpi = ii / 2147483392.0;
            ptr += 3;
            j   += 6;
        }
        break;
        case 32:
        {
            // convert 32 bit sample to floating point
            // data is in ptr[0](LSB)..ptr[3](MSB)

            // put data into 32-bit signed int
            int ii = (ptr[3] << 24) | (ptr[2] << 16) | (ptr[1] << 8) | ptr[0];

            // divide by (2^31 - 1) = 2147483647
            tmpr = ii / 2147483647.0;
            ptr += 4;

            // repeat for other stereo channel sample
            ii   = (ptr[3] << 24) | (ptr[2] << 16) | (ptr[1] << 8) | ptr[0];
            tmpi = ii / 2147483647.0;
            ptr += 4;
            j   += 8;
        }
        break;
        default:
            tmpr = 0.;
            tmpi = 0.;
            break;
        }
        in[i][0] = tmpr * window[i];
        in[i][1] = tmpi * window[i];
        if (j == sizes.chunk_size) {
            // make buffer circular
            j   = 0;
            ptr = &data[0];
        }
    }

    // done reading raw data, emit signal so audioReader can procede
    emit(done());
#ifdef Q_OS_WIN
    (fftw_executep) (plan);
#else
    fftw_execute(plan);
#endif
    if (settings.value(s_sdr_iqcorrect[nrig],s_sdr_iqcorrect_def[nrig]).toBool()) {
        for (int i = 0; i < sizes.sample_length; i++) {
            // correct IQ imbalance
            int    j    = sizes.sample_length - i - 1;
            double real = out[i][0] + out[j][0] - (out[i][1] + out[j][1]) * errfunc[i][1] + (out[i][0] - out[j][0]) * errfunc[i][0];
            double imag = out[i][1] - out[j][1] + (out[i][1] + out[j][1]) * errfunc[i][0] + (out[i][0] - out[j][0]) * errfunc[i][1];
            spec_tmp[i] = real * real + imag * imag;
        }
    } else {
        for (int i = 0; i < sizes.sample_length; i++) {
            spec_tmp[i] = (out[i][0] * out[i][0] + out[i][1] * out[i][1]);
        }
    }

    double bga, sigma;
    measureBackground(bga, sigma, spec_tmp);

    if (settings.value(s_sdr_iqdata[nrig],s_sdr_iqdata_def[nrig]).toBool()) {
        if (calibCnt == (SIG_CALIB_FREQ - 1)) {
            measureIQError(bga, spec_tmp);
            calibCnt = 0;
        } else {
            calibCnt++;
        }
    }

    for (int i = 0; i < sizes.sample_length; i++) {
        spec_tmp[i] = log(spec_tmp[i]);
#ifdef Q_OS_LINUX
        if (isinf(spec_tmp[i])) spec_tmp[i] = -1.0e-16;
#endif
    }

    measureBackgroundLog(bga, sigma, spec_tmp);

    // put upper limit on background. Prevents display "blacking out"
    // from static crashes
    if (bga > 0.0) bga = 0.0;
    if (settings.value(s_sdr_peakdetect[nrig],s_sdr_peakdetect_def[nrig]).toBool()) {
        detectPeaks(bga, sigma, spec_tmp);
    }

    if (settings.value(s_sdr_click[nrig],s_sdr_click_def[nrig]).toBool()) {
    //if (clickFilter) {
        clickRemove(bga, sigma, spec_tmp);

        // re-measure background since click removal changes it
        // measureBackgroundLog(bga,sigma,spec_tmp);
    }


    if (settings.value(s_sdr_scale[nrig],s_sdr_scale_def[nrig]).toInt() == 2) {
        interp2(spec_tmp, tmp4, bga); // expand by 2 using linear interpolation
    } else {
        // IF offset included here
        // for (int i=0;i<sizes.display_length;i++) {
        double tmp = ((settings.value(s_sdr_offset[nrig],s_sdr_offset_def[nrig]).toInt()-addOffset) *
                      sizes.spec_length * settings.value(s_sdr_scale[nrig],s_sdr_scale_def[nrig]).toInt()) / (SAMPLE_FREQ * 1000.0);
        int offsetPix = -(int) tmp;
        for (int i = 0; i < sizes.spec_length; i++) {
            unsigned int j = (sizes.spec_length - offsetPix - MAX_H / 2 + i) % sizes.spec_length;
            spec_tmp[j] = (spec_tmp[j] - bga + 2.0) * 25.0;
            if (spec_tmp[j] < 0.0) {
                spec_tmp[j] = 0.0;
            } else if (spec_tmp[j] > 255.0) {
                spec_tmp[j] = 255.0;
            }
            tmp4[i] = spec_tmp[j];
        }
    }
    unsigned int cnt = 0;
    for (int i = 0; i < sizes.display_length; i++) {
        output[i] = (unsigned char) tmp4[i];
        cnt      += output[i];
    }
    background = cnt / sizes.display_length;  // background measurement
    emit(spectrumReady(output, background));

    if (calcErrorNext) {
        calcError(false);
        calcErrorNext = false;
    }
}
Exemplo n.º 8
0
void Spectrum::initialize(sampleSizes s, PaSampleFormat b, int nr, QString dir)
{
    nrig          = nr;
    userDirectory = dir;
    sizes         = s;
    sizeIQ        = sizes.sample_length / 8; // 512 IQ phase/gain bins
    // initialize FFTW
#ifdef Q_OS_WIN
    if (!fftwWinInit()) {
        return;
    }
    if (in) (fftw_freep) (in);
    if (out) (fftw_freep) (out);
    if (errfunc) (fftw_freep) (errfunc);
    if (plan) (fftw_destroy_planp) (plan);

    in      = (fftw_complex *) (fftw_mallocp) (sizeof(fftw_complex) * sizes.sample_length);
    out     = (fftw_complex *) (fftw_mallocp) (sizeof(fftw_complex) * sizes.sample_length);
    errfunc = (fftw_complex *) (fftw_mallocp) (sizeof(fftw_complex) * sizes.sample_length);
    plan    = (fftw_plan_dft_1dp) (sizes.sample_length, in, out, FFTW_FORWARD, FFTW_MEASURE);
#else
    if (in) fftw_free(in);
    if (out) fftw_free(out);
    if (errfunc) fftw_free(errfunc);
    if (plan) fftw_destroy_plan(plan);

    in      = (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * sizes.sample_length);
    out     = (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * sizes.sample_length);
    errfunc = (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * sizes.sample_length);
    plan    = fftw_plan_dft_1d(sizes.sample_length, in, out, FFTW_FORWARD, FFTW_MEASURE);
#endif
    switch (b) {
    case paInt16:
        bits = 16;
        break;
    case paInt24:
        bits = 24;
        break;
    case paInt32:
        bits = 32;
        break;
    default:
        bits = 16;
    }

    if (output) delete [] output;
    output = new unsigned char[sizes.display_length];

    for (int i = 0; i < SIG_N_AVG; i++) {
        if (peakAvg[i]) delete [] peakAvg[i];
        peakAvg[i] = new double[sizes.spec_length];
    }
    if (spec_smooth) delete [] spec_smooth;
    spec_smooth = new double[sizes.sample_length];
    if (spec_tmp) delete [] spec_tmp;
    spec_tmp = new double[sizes.sample_length];

    if (spec_tmp2) delete [] spec_tmp2;
    spec_tmp2 = new double[sizes.sample_length];

    for (int i = 0; i < sizes.sample_length; i++) {
        spec_tmp[i]    = 0.;
        spec_smooth[i] = 0.;
        spec_tmp2[i]   = 0.;
    }

    if (tmp4) delete [] tmp4;
    tmp4 = new double[sizes.spec_length];
    if (window) delete [] window;
    window = new double[sizes.sample_length];

    makeWindow();

    for (int i = 0; i < sizes.spec_length; i++) {
        tmp4[i] = 0.;
    }
    if (sigOnCnt) delete [] sigOnCnt;
    sigOnCnt = new int[sizes.sample_length];

    if (sigOn) delete [] sigOn;
    sigOn = new bool[sizes.sample_length];

    for (int i = 0; i < sizes.sample_length; i++) {
        sigOnCnt[i] = 2;
        sigOn[i]    = false;
    }
    if (calibSigList) delete [] calibSigList;
    calibSigList = new CalibSignal[sizeIQ];
    for (int i = 0; i < FIT_ORDER; i++) {
        aGain[i]  = 0.;
        aPhase[i] = 0.;
    }
    addOffset=0;

    // intialize to unit gain and zero phase
    aGain[0] = 1.0;
    makeGainPhase();

    readError();
    calcError(true);
}
Exemplo n.º 9
0
void BpAlgSt::start(){
	Q_ASSERT(net != NULL);
	Q_ASSERT(data != NULL);

    emit started();

    //value initialization
	running = true;
    actIter = 1;
	actTime = 0;
	timer.restart();

    double sumErr = 0;
    for(int i = 0; i < data->minPatternCount(); i++){
        output = net->layerOutput(data->inputVector(i));
        sumErr += calcError(i);
    }
    actError = sumErr;
    emit update(0, actTime, actError);

    //learning main cycle
	while(running){
		for(int i = 0; i < data->minPatternCount(); i++){
			//feedforward
			output = net->layerOutput(data->inputVector(i));
			//output layer delta calculation
			calcOutputDelta(i);
			//inner layer delta calculation
			calcInnerDelta();
			//weight adjustment
			adjustWeight();
		}

		//current time
		actTime = timer.elapsed();

        //output error calculation
        double sumErr = 0;
        for(int i = 0; i < data->minPatternCount(); i++){
            output = net->layerOutput(data->inputVector(i));
            sumErr += calcError(i);
        }
        actError = sumErr;

        //emits update signal once per each update interval
        if(actIter % updateInterv == 0){
            emit update(actIter, actTime, actError);
        }

        //stop conditions
		if(actTime >= stopTimeVal) break;
		if(actIter >= stopIter) break;
		if(actError <= stopErrorVal) break;

		actIter++;
	}

	//running flag to false
	running = false;

    //signal that tells that learning is finished
    emit stoped();
}
Exemplo n.º 10
0
  void ModifierNPTRattleDetails::doExecute(Integrator* myTheIntegrator){

    // estimate the current error in all velocity constraints
    Real error = calcError();

    // delta_t
    Real dt = myTheIntegrator->getTimestep() / Constant::TIMEFACTOR;

    // multiplicative constants for the box volume velocity and thermostat velocity
    const Real pVol_term = 0.5 * myTheIntegrator->getTimestep() * getEpsilonVel();
    const Real pEta_term = 0.5 * myTheIntegrator->getTimestep() * getEtaVel();
    const Real OneOverN_term = 1.0 + 1.0 / app->topology->atoms.size();

    int iter = 0;
    while(error > myEpsilon) {
      
      for(unsigned int i=0;i<myListOfConstraints->size();i++) {

        // find the ID#s of the two atoms in the current constraint
        int a1 = (*myListOfConstraints)[i].atom1;
        int a2 = (*myListOfConstraints)[i].atom2;

        // get the ID# of the molecule that this bond belongs to
        int Mol = app->topology->atoms[a1].molecule;

        // reciprocal atomic masses
        Real rM1 = 1/app->topology->atoms[a1].scaledMass;
        Real rM2 = 1/app->topology->atoms[a2].scaledMass;
        Real m1_over_M = 1 / (rM1 * app->topology->molecules[Mol].mass);
        Real m2_over_M = 1 / (rM2 * app->topology->molecules[Mol].mass);

        // multiplicative constants due to change in box volume
        // (for constant pressure simulations -- see Equation 56 of G. Kalibaeva,
        //  M. Ferrario, and G. Ciccotti, "Constant pressure-constant temperature molecular
        //  dynamics: a correct constrained NPT ensemble using the molecular virial", Mol. Phys.
        //  101(6), 2003, p. 765-778)
        Real Exp2_atom1 = (1 + pVol_term*OneOverN_term*m1_over_M) * exp( -pEta_term - pVol_term*OneOverN_term*m1_over_M);
        Real Exp2_atom2 = (1 + pVol_term*OneOverN_term*m2_over_M) * exp( -pEta_term - pVol_term*OneOverN_term*m2_over_M);
        
        // now lets compute the lambdas.
        // compute the current bond vector
        Vector3D rab = (app->positions)[a1] - (app->positions)[a2];
        Real rabsq = rab.normSquared();

        // compute the current velocity vector
        Vector3D vab = (app->velocities)[a1] - (app->velocities)[a2];   

        // dot product of distance and velocity vectors
        Real rvab = rab * vab;

        // compute the change in lambda
        Real gab = -rvab / (dt * (rM1 * Exp2_atom1 + rM2 * Exp2_atom2) * rabsq);
        Vector3D dp = rab * gab;

        // move the velocities based upon the multiplier
        (app->velocities)[a1] += dp * dt * rM1 * Exp2_atom1;
        (app->velocities)[a2] -= dp * dt * rM2 * Exp2_atom2;

        // the constraint adds a force to each atom since their positions
        // had to be changed.  This constraint force therefore contributes
        // to the atomic virial.  Note that the molecular virial is independent of
        // any intramolecular constraint forces.
        if (app->energies.virial()) app->energies.addVirial(dp*2,rab);
      }

      // compute the error in all the velocity constraints after this RATTLE iteration
      error = calcError();
      iter ++;
      if(iter > myMaxIter) {
        report << warning << "Rattle maxIter = " << myMaxIter
               << " reached, but still not converged ... error is "<<error<<endr;
        break;
      }
    }
  }
Exemplo n.º 11
0
cv::Mat ransac(const cv::Mat& samples, cv::TermCriteria termcrit, double thresh_outliar, double sampling_rate, double minimum_inliar_rate){
	assert(0 < sampling_rate && sampling_rate <= 1);
	assert(0 < minimum_inliar_rate && minimum_inliar_rate <= 1);


	cv::Mat best_model(cv::Size(samples.cols,1),samples.type(),cv::Scalar(0));
	size_t dim = samples.cols;
	size_t sample_num = samples.rows;

	size_t iterations = 0;
	size_t sampling_num = sample_num * sampling_rate;
	size_t minimum_inliar_num = sample_num * minimum_inliar_rate;

	double best_error = DBL_MAX;

	std::vector<size_t> sample_index(sample_num);
	for(size_t i=0;i<sample_num;i++) sample_index[i] = i;


	while( ( (termcrit.type & CV_TERMCRIT_ITER) == 0 || iterations < (size_t)termcrit.maxCount) 
		&& ( (termcrit.type & CV_TERMCRIT_EPS) == 0 || best_error > termcrit.epsilon) ){
		// main loop

		// random sampling (use first part as sampled data.)
		std::random_shuffle(sample_index.begin(),sample_index.end());

		std::vector<size_t> consensus_set(sampling_num);
		std::copy(sample_index.begin(),sample_index.begin()+sampling_num,consensus_set.begin());

		cv::Mat this_model = fitModel(samples,sample_index.begin(),sample_index.begin()+sampling_num);

		// do process for not-sampled elements.
		cv::Rect roi(0,0,dim,1);
		for(size_t _i=sampling_num;_i<sample_num;_i++){
			size_t idx = sample_index[_i];
			
			roi.y = idx;
			if(cv::norm(this_model,cv::Mat(samples,roi))<thresh_outliar){
				consensus_set.push_back(idx);
			}
		}

		// increment iteration before checking validity
		iterations++;

		// check validity
		if(consensus_set.size() < minimum_inliar_num) continue;
		if(consensus_set.size() > sampling_num){
			cv::Mat refinement = fitModel(samples,consensus_set.begin()+sampling_num,consensus_set.end());
			this_model = ( sampling_num * this_model + (consensus_set.size()-sampling_num) * refinement ) /consensus_set.size();
		}
		double this_error = calcError(samples, this_model, consensus_set.begin(),consensus_set.end());
		if(this_error < best_error){
			best_error = this_error;
			best_model = this_model;
		}

	}

	return best_model;
}
Exemplo n.º 12
0
/********************
 * remez
 *=======
 * Calculates the optimal (in the Chebyshev/minimax sense)
 * FIR filter impulse response given a set of band edges,
 * the desired reponse on those bands, and the weight given to
 * the error in those bands.
 *
 * INPUT:
 * ------
 * int      numtaps - Number of filter coefficients
 * int      numband - Number of bands in filter specification
 * double[] bands   - User-specified band edges [2 * numband]
 * double[] des     - User-specified band responses [numband]
 * double[] weight  - User-specified error weights [numband]
 * int      type    - Type of filter
 *
 * OUTPUT:
 * -------
 * double[] h       - Impulse response of final filter [numtaps]
 **************************************************************************
 *  Converted to C++ by Tony Kirke July 2001
 **************************************************************************
 *  Copyright (C) 1995  Jake Janovetz ([email protected])
 *
 *  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
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Converted to Java by Iain A Robin, June 1998 
 *************************************************************************/
double* remez_fir::remez(int numtaps, int numband,
						 double bands[], double des[], double weight[], int type) {

  double c;
  int i;
  int symmetry = (type == BANDPASS)? POSITIVE : NEGATIVE;
  int r = numtaps/2;   // number of extrema
  if ((numtaps%2 != 0) && (symmetry == POSITIVE)) r++;
  
  // Predict dense grid size in advance for array sizes
  int gridSize = 0;
  for (i=0; i<numband; i++) {
	gridSize += (int)floor(0.5+2*r*GRIDDENSITY*(bands[2*i+1] - bands[2*i]));
  }
  if (symmetry == NEGATIVE) gridSize--;
  
  double* grid = new double[gridSize];
  double* d    = new double[gridSize];
  double* w    = new double[gridSize];
  double* e    = new double[gridSize];
  double* x    = new double[r+1];
  double* y    = new double[r+1];
  double* ad   = new double[r+1];
  double* taps = new double[r+1];
  int*    ext  = new int[r+1];
  
  // Create dense frequency grid
  createDenseGrid(r, numtaps, numband, bands, des, weight,
				  gridSize, grid, d, w, symmetry);
  initialGuess(r, ext, gridSize);
  
  // For Differentiator: (fix grid)
  if (type == DIFFERENTIATOR) {
	for (i=0; i<gridSize; i++) {
	  if (d[i] > 0.0001) w[i] /= grid[i];
	}
  }
  
  // For odd or Negative symmetry filters, alter the
  // d[] and w[] according to Parks McClellan
  if (symmetry == POSITIVE) {
	if (numtaps % 2 == 0) {
	  for (i=0; i<gridSize; i++) {
		c = cos(PI * grid[i]);
		d[i] /= c;
		w[i] *= c;
	  }
	}
  }
  else {
	if (numtaps % 2 != 0) {
	  for (i=0; i<gridSize; i++) {
		c = sin(TWOPI * grid[i]);
		d[i] /= c;
		w[i] *= c;
	  }
	}
	else {
	  for (i=0; i<gridSize; i++) {
		c = sin(PI * grid[i]);
		d[i] /= c;
		w[i] *= c;
	  }
	}
  }

  // Perform the Remez Exchange algorithm
  int iter;
  for (iter=0; iter<MAXITERATIONS; iter++) {
	calcParms(r, ext, grid, d, w, ad, x, y);
	calcError(r, ad, x, y, gridSize, grid, d, w, e);
	search(r, ext, gridSize, e);
	if (isDone(r, ext, e)) break;
  }
  if (iter == MAXITERATIONS) {
	cout << "Reached maximum iteration count.\n";
	cout << "Results may be bad\n";
  }
  
  calcParms(r, ext, grid, d, w, ad, x, y);
  
  // Find the 'taps' of the filter for use with Frequency
  // Sampling.  If odd or Negative symmetry, fix the taps
  // according to Parks McClellan
  for (i=0; i<=numtaps/2; i++) {
	if (symmetry == POSITIVE) {
	  if (numtaps%2 != 0) c = 1;
	  else c = cos(PI * (double)i/numtaps);
	}
	else {
	  if (numtaps%2 != 0) c = sin(TWOPI * (double)i/numtaps);
	  else c = sin(PI * (double)i/numtaps);
	}
	taps[i] = computeA((double)i/numtaps, r, ad, x, y)*c;
  }
  // Frequency sampling design with calculated taps
  return freqSample(taps, numtaps, symmetry);
}
Exemplo n.º 13
0
/*
 * newPeaks()
 *
 * Create initial fit data structures for spline fitting from the peak array. The 
 * format of the initial values is the same as what was used for 3D-DAOSTORM.
 *
 * peaks - pointer to the initial values for the parameters for each peak.
 *           1. height
 *           2. x-center
 *           3. x-sigma
 *           4. y-center
 *           5. y-sigma
 *           6. background
 *           7. z-center
 *           8. status
 *           9. error
 *               .. repeat ..
 * n_peaks - The number of parameters (peaks).
 */
void newPeaks(double *peaks, int n_peaks, int fit_type)
{
  int i,j,n_fit_params,sx,sy,sz;
  double temp;

  if(fit_type == F2D){
    n_fit_params = 4;
  }
  else{
    n_fit_params = 5;
  }

  /* Delete old fit data structures, if they exist. */
  freePeaks();
  
  /* Reset the fit array. */
  resetFit();

  /* Allocate storage for fit data structure. */
  n_fit_data = n_peaks;
  new_fit_data = (fitData *)malloc(sizeof(fitData)*n_peaks);
  old_fit_data = (fitData *)malloc(sizeof(fitData)*n_peaks);
  sx = getXSize()/2 - 1;
  sy = getYSize()/2 - 1;
  sz = getZSize();

  /* Note the assumption here that the splines are square. */
  if((sx%2)==1){
    xoff = (double)(sx/2) - 1.0;
    yoff = (double)(sy/2) - 1.0;
  }
  else{
    xoff = (double)(sx/2) - 1.5;
    yoff = (double)(sy/2) - 1.5;
  }

  //zoff = -(double)(sz/2);
  zoff = 0.0;

  /* Initialize fit data structure. */
  for(i=0;i<n_fit_data;i++){
    new_fit_data[i].index = i;
    new_fit_data[i].n_params = n_fit_params;
    new_fit_data[i].size_x = sx;
    new_fit_data[i].size_y = sy;
    new_fit_data[i].size_z = sz;
    new_fit_data[i].status = (int)(peaks[i*NRESULTSPAR+STATUS]);
    new_fit_data[i].type = fit_type;

    new_fit_data[i].lambda = 1.0;

    mallocFitData(&(new_fit_data[i]), n_fit_params, sx*sy*(n_fit_params+1));

    for(j=0;j<n_fit_params;j++){
      new_fit_data[i].sign[j] = 0;
    }

    new_fit_data[i].clamp[CF_HEIGHT] = 100.0;
    new_fit_data[i].clamp[CF_XCENTER] = 1.0;
    new_fit_data[i].clamp[CF_YCENTER] = 1.0;
    new_fit_data[i].clamp[CF_BACKGROUND] = 20.0;
    if (fit_type == F3D){
      new_fit_data[i].clamp[CF_ZCENTER] = 2.0;
    }

    if (DEBUG){
      printf(" peaks: %.2f %.2f %.2f\n", peaks[i*NRESULTSPAR+XCENTER], peaks[i*NRESULTSPAR+YCENTER], peaks[i*NRESULTSPAR+ZCENTER]);
    }
    new_fit_data[i].params[CF_HEIGHT] = peaks[i*NRESULTSPAR+HEIGHT];
    new_fit_data[i].params[CF_XCENTER] = peaks[i*NRESULTSPAR+XCENTER] - xoff;
    new_fit_data[i].params[CF_YCENTER] = peaks[i*NRESULTSPAR+YCENTER] - yoff;
    new_fit_data[i].params[CF_BACKGROUND] = peaks[i*NRESULTSPAR+BACKGROUND];
    if (fit_type == F3D){
      new_fit_data[i].params[CF_ZCENTER] = peaks[i*NRESULTSPAR+ZCENTER] - zoff;
    }

    new_fit_data[i].xi = (int)(new_fit_data[i].params[CF_XCENTER]);
    new_fit_data[i].yi = (int)(new_fit_data[i].params[CF_YCENTER]);
    if (fit_type == F3D){
      new_fit_data[i].zi = (int)(new_fit_data[i].params[CF_ZCENTER]);
    }

    if (fit_type == F2D){
      updateFitValues2D(&(new_fit_data[i]));
    }
    else{
      updateFitValues3D(&(new_fit_data[i]));
    }

    /*
     * The derivative with respect to background term is always 1.0
     */
    for(j=0;j<(sx*sy);j++){
      new_fit_data[i].values[(CF_BACKGROUND+1)*sx*sy+j] = 1.0;
    }

    addPeak(&(new_fit_data[i]));

    mallocFitData(&(old_fit_data[i]), n_fit_params, sx*sy*(n_fit_params+1));
    copyFitData(&(new_fit_data[i]), &(old_fit_data[i]));

    if(new_fit_data[i].status == RUNNING){
      if (TESTING){
	temp = calcError(&(new_fit_data[i]), new_fit_data[i].params[CF_BACKGROUND]);
	new_fit_data[i].error = temp;
      }
      else{
	new_fit_data[i].error = 1.0e+12;
      }
      old_fit_data[i].error = 1.0e+13;
    }
    else{
      new_fit_data[i].error = peaks[i*NRESULTSPAR+IERROR];
      old_fit_data[i].error = new_fit_data[i].error;
    }
  }
}
Exemplo n.º 14
0
/*
 * iterateSpline()
 *
 * Perform one cycle of update of all the peaks.
 */
void iterateSpline(void)
{
  int i;
  fitData *new_peak, *old_peak;
  
  for(i=0;i<n_fit_data;i++){
    new_peak = &(new_fit_data[i]);
    old_peak = &(old_fit_data[i]);
    if (new_peak->status == RUNNING){

      /* Add the peak & calculate the error. */
      new_peak->error = calcError(new_peak, new_peak->params[CF_BACKGROUND]);
      //printf("error: %f %f %f\n", new_peak->error, old_peak->error, new_peak->lambda);

      if((fabs(new_peak->error - old_peak->error)/new_peak->error) < tolerance){
	new_peak->status = CONVERGED;
      }
      else{
	/* 
	 * Check if the fit is getting worse. If it is then reset to the 
	 * previous parameters & increase the lambda parameter. This will 
	 * cause the search to move downhill with higher probability but 
	 * less speed. 
	 */
	if(new_peak->error > (1.5*old_peak->error)){
	  subtractPeak(new_peak);
	  copyFitData(old_peak, new_peak);
	  new_peak->lambda = 10.0 * new_peak->lambda;
	  addPeak(new_peak);
	}
	/*
	 * If the fit is getting better and lambda is greater than 1.0
	 * then we slowly decrease lambda.
	 */
	else if(new_peak->error < old_peak->error){
	  if(new_peak->lambda > 1.0){
	    new_peak->lambda = 0.8*new_peak->lambda;
	    if(new_peak->lambda < 1.0){
	      new_peak->lambda = 1.0;
	    }
	  }
	}

	/* Calculate updated fit values. */
	updateFit(new_peak, new_peak->params[CF_BACKGROUND]);

	/* 
	 * Increase lambda in the event of Cholesky failure. 
	 */
	if(new_peak->status == CHOLERROR){
	  new_peak->lambda = 10.0 * new_peak->lambda;
	  new_peak->status = RUNNING;
	}
	else{
	  subtractPeak(new_peak);
	  copyFitData(new_peak, old_peak);
	  updatePeakParameters(new_peak);
	  if(new_peak->type == F2D){
	    updateFitValues2D(new_peak);
	  }
	  else{
	    updateFitValues3D(new_peak);
	  }
	  if(new_peak->status != BADPEAK){
	    addPeak(new_peak);
	  }
	}
      }
    }
  }
}
Exemplo n.º 15
0
double Neuron::getError() {
    calcError();
    return error;
}