static double tune_trx_phase_offset(struct iio_device *ldev, int *ret,
			long long cal_freq, long long cal_tone,
			double sign, double abort,
			void (*tune)(struct iio_device *, gdouble))
{
	int i;
	double offset, mag;
	double phase = 0.0, increment;

	for (i = 0; i < 10; i++) {

		get_markers(&offset, &mag);
		get_markers(&offset, &mag);

		increment = calc_phase_offset(cal_freq, cal_tone, offset, mag);
		increment *= sign;

		phase += increment;

		phase = scale_phase_0_360(phase);
		tune(ldev, phase);

		DBG("Step: %i increment %f Phase: %f\n", i, increment, phase);

		if (fabs(offset) < 0.001)
			break;
	}

	if (fabs(offset) > 0.1)
		*ret = -EFAULT;
	else
		*ret = 0;

	return phase * sign;
}
static double tune_trx_phase_offset(struct iio_device *ldev, int *ret,
			long long cal_freq, long long cal_tone,
			double sign, double abort,
			void (*tune)(struct iio_device *, gdouble))
{
	long long y, y1, y2, delta = LLONG_MAX,
		min_delta = LLONG_MAX, x1;
	int i, offset, pos = 0, neg = 0;
	double min_phase, phase = 0.0, step = 1.0;

	for (i = 0; i < 30; i++) {

		get_markers(&offset, &y, &y1, &y2, &x1);
		get_markers(&offset, &y, &y1, &y2, &x1);

		if (i == 0) {
			phase = calc_phase_offset(cal_freq, cal_tone, offset, y);
			tune(ldev, phase * sign);
			continue;
		}


		if (offset != 0) {
			phase += (360.0 / ((cal_freq / cal_tone) / offset) / 2);
			tune(ldev, phase * sign);
			continue;
		}

		delta = abs(y1) - abs(y2);

		if (delta < min_delta) {
			min_delta = delta;
			min_phase = phase;
		}

		if (x1 > 0) {
			if (pos == 1) {
				step /= 2;
				pos = 0;
			}
			phase -= step;
			neg = 1;
		} else {
			if (neg == 1) {
				step /= 2;
				neg = 0;
			}
			phase += step;
			pos = 1;
		}

		if (step < abort)
			break;

		DBG("Step: %f Phase: %f, min_Phase: %f\ndelta %d, pdelta %d, min_delta %d\n",
		    step, phase, min_phase, (int)delta, (int)min_delta);

		tune(ldev, phase * sign);
	}

	if (offset)
		*ret = -EFAULT;
	else
		*ret = 0;

	return phase * sign;
}
Exemple #3
0
NeuralNetwork* Phenotype::get_network(Individual* individual) {
    NeuralNetwork* network = new NeuralNetwork();

    // Locate markers
    vector< pair<int, int> > raw_markers = get_markers(individual);
    vector< pair<int, int> > markers;
    for (int i = 0; i < raw_markers.size(); i++) {
        if (get_slice_size(individual, raw_markers[i]) >= 7) {
            markers.push_back(raw_markers[i]);
        }
    }
    
    // Create input neurons
    for (int i = 0; i < individual->input_units_; i++) {
        Neuron neuron;
            
        neuron.label_ = i;
            
        network->input_neurons_.push_back(neuron);
    }
    
    // Create hidden neurons
    for (int i = 0; i < markers.size(); i++) {
        Neuron neuron;
            
        neuron.label_ = get_label(individual, markers[i]);
        
        neuron.bias_ = get_bias(individual, markers[i]);
        
        neuron.output_ = neuron.bias_;
            
        network->hidden_neurons_.push_back(neuron);
    }
    
    // Create output neurons
    for (int i = 0; i < individual->output_units_; i++) {
        Neuron neuron;
            
        neuron.label_ = i;
        
        neuron.bias_ = individual->genes_[(individual->genes_.size()-individual->output_units_) + i];
            
        network->output_neurons_.push_back(neuron);
    }
    
    // Create links
    for (int i = 0; i < markers.size(); i++) {
        Neuron& neuron = network->hidden_neurons_[i];

        int connections = (get_slice_size(individual, markers[i]) - 2) / 3;
        
        for (int j = 0; j < connections; j++) {
            signed char key = get_nth_key(individual, markers[i], j);
            int label = get_nth_label(individual, markers[i], j);
            signed char weight = get_nth_weight(individual, markers[i], j);
            
            // Link to input/output layer
            if (key > 0) {
                // Input
                if (label > 0) {
                    Neuron& input = network->input_neurons_[label % network->input_neurons_.size()];
                    
                    input.outputs_.push_back(&neuron);
                    neuron.inputs_.push_back(pair<signed char, Neuron*>(weight, &input));
                }
                // Output
                else {
                    Neuron& output = network->output_neurons_[abs(label) % network->output_neurons_.size()];
                    
                    neuron.outputs_.push_back(&output);
                    output.inputs_.push_back(pair<signed char, Neuron*>(weight, &neuron));
                }

            }
            // Link to hidden layer
            else {
                int hidden_label = get_hidden_label(individual, label);
                Neuron* hidden = get_closest_neuron(hidden_label, network->hidden_neurons_);
                
                hidden->outputs_.push_back(&neuron);
                neuron.inputs_.push_back(pair<signed char, Neuron*>(weight, hidden));
            }
        }
    }
    
    return network;
}