VectorDouble TimeseriesBuffer::update(const VectorDouble &x){
    
#ifdef GRT_SAFE_CHECKING
    if( !initialized ){
        errorLog << "update(const VectorDouble &x) - Not Initialized!" << endl;
        return VectorDouble();
    }
    
    if( x.size() != numInputDimensions ){
        errorLog << "update(const VectorDouble &x)- The Number Of Input Dimensions (" << numInputDimensions << ") does not match the size of the input vector (" << x.size() << ")!" << endl;
        return VectorDouble();
    }
#endif
    
    //Add the new data to the buffer
    dataBuffer.push_back( x );
    
    //Search the buffer for the zero crossing features
    UINT colIndex = 0;
    for(UINT j=0; j<numInputDimensions; j++){
        for(UINT i=0; i<dataBuffer.getSize(); i++){
            featureVector[ colIndex++ ] = dataBuffer[i][j];
        }
    }
    
    //Flag that the feature data has been computed
    if( dataBuffer.getBufferFilled() ){
        featureDataReady = true;
    }else featureDataReady = false;

    return featureVector;
}
VectorDouble HighPassFilter::filter(const VectorDouble &x){
    
#ifdef GRT_SAFE_CHECKING
    if( !initialized ){
        errorLog << "filter(const VectorDouble &x) - Not Initialized!" << endl;
        return VectorDouble();
    }
    
    if( x.size() != numInputDimensions ){
        errorLog << "filter(const VectorDouble &x) - The Number Of Input Dimensions (" << numInputDimensions << ") does not match the size of the input vector (" << x.size() << ")!" << endl;
        return VectorDouble();
    }
#endif
    
    for(UINT n=0; n<numInputDimensions; n++){
        //Compute the new output
        processedData[n] = filterFactor * (yy[n] + x[n] - xx[n]) * gain;
        
        //Store the current input
        xx[n] = x[n];
        
        //Store the current output
        yy[n] = processedData[n];
    }
    return processedData;
}
VectorDouble MovingAverageFilter::filter(const VectorDouble &x){
    
    //If the filter has not been initialised then return 0, otherwise filter x and return y
    if( !initialized ){
        errorLog << "filter(const VectorDouble &x) - The filter has not been initialized!" << endl;
        return VectorDouble();
    }
    
    if( x.size() != numInputDimensions ){
        errorLog << "filter(const VectorDouble &x) - The size of the input vector (" << x.size() << ") does not match that of the number of dimensions of the filter (" << numInputDimensions << ")!" << endl;
        return VectorDouble();
    }
    
    if( ++inputSampleCounter > filterSize ) inputSampleCounter = filterSize;
    
    //Add the new value to the buffer
    dataBuffer.push_back( x );
    
    for(unsigned int j=0; j<numInputDimensions; j++){
        processedData[j] = 0;
        for(unsigned int i=0; i<inputSampleCounter; i++) {
            processedData[j] += dataBuffer[i][j];
        }
        processedData[j] /= double(inputSampleCounter);
    }
    
    return processedData;
}
VectorDouble MatrixDouble::multiple(const VectorDouble &b) const{
    
    const unsigned int M = rows;
    const unsigned int N = cols;
    const unsigned int K = (unsigned int)b.size();
    
    if( N != K ){
        warningLog << "multiple(vector b) - The size of b (" << b.size() << ") does not match the number of columns in this matrix (" << N << ")" << std::endl;
        return VectorDouble();
    }
    
    VectorDouble c(M);
    const double *pb = &b[0];
    double *pc = &c[0];
    
    unsigned int i,j = 0;
    for(i=0; i<rows; i++){
        pc[i] = 0;
        for(j=0; j<cols; j++){
            pc[i] += dataPtr[i*cols+j]*pb[j];
        }
    }
    
    return c;
}
bool MovingAverageFilter::init(UINT filterSize,UINT numDimensions){
    
    //Cleanup the old memory
    initialized = false;
    inputSampleCounter = 0;
    
    if( filterSize == 0 ){
        errorLog << "init(UINT filterSize,UINT numDimensions) - Filter size can not be zero!" << endl;
        return false;
    }
    
    if( numDimensions == 0 ){
        errorLog << "init(UINT filterSize,UINT numDimensions) - The number of dimensions must be greater than zero!" << endl;
        return false;
    }
    
    //Resize the filter
    this->filterSize = filterSize;
    this->numInputDimensions = numDimensions;
    this->numOutputDimensions = numDimensions;
    processedData.clear();
    processedData.resize(numDimensions,0);
    initialized = dataBuffer.resize( filterSize, VectorDouble(numInputDimensions,0) );
    
    if( !initialized ){
        errorLog << "init(UINT filterSize,UINT numDimensions) - Failed to resize dataBuffer!" << endl;
    }
    
    return initialized;
}
Beispiel #6
0
VectorDouble RandomForests::getFeatureWeights( const bool normWeights ) const{

    if( !trained ) return VectorDouble();

    VectorDouble weights( numInputDimensions, 0 );

    for(UINT i=0; i<forestSize; i++){
        if( !forest[i]->computeFeatureWeights( weights ) ){
            warningLog << "getFeatureWeights( const bool normWeights ) - Failed to compute weights for tree: " << i << endl;
        }
    }

    //Normalize the weights
    if( normWeights ){
        double sum = Util::sum( weights );
        if( sum > 0.0 ){
            const double norm = 1.0 / sum;
            for(UINT j=0; j<numInputDimensions; j++){
                weights[j] *= norm;
            }
        }
    }

    return weights;
}
bool TimeseriesBuffer::init(UINT bufferSize,UINT numDimensions){
    
    initialized = false;
    featureDataReady = false;
    
    if( bufferSize == 0 ){
        errorLog << "init(UINT bufferSize,UINT numDimensions) - The bufferSize must be greater than zero!" << endl;
        return false;
    }
    
    if( numDimensions == 0 ){
        errorLog << "init(UINT bufferSize,UINT numDimensions) - The numDimensions must be greater than zero!" << endl;
        return false;
    }
    
    //Setup the databuffer
    numInputDimensions = numDimensions;
    numOutputDimensions = bufferSize * numInputDimensions;
    this->bufferSize = bufferSize;
    dataBuffer.resize( bufferSize, VectorDouble(numInputDimensions,0) );
    featureVector.resize(numOutputDimensions,0);
    
    //Flag that the timeseries buffer has been initialized
    initialized = true;
    
    return true;
}
Beispiel #8
0
bool ThresholdDetection::init(UINT bufferLength,UINT numDimensions,double alpha,double beta){
    
    initialized = false;
    
    this->bufferLength = bufferLength;
    this->numInputDimensions = numDimensions;
    this->alpha = alpha;
    this->beta = beta;
    this->inNoise = true;
    featureDataReady = false;
    
    //Set the number of output dimensions
    numOutputDimensions = 5 * numInputDimensions + 1;
    
    //Resize the feature vector
    featureVector.resize(numOutputDimensions);
    
    //Resize the raw data buffer
    dataBuffer.resize( bufferLength, VectorDouble(numInputDimensions,0) );

    //Flag that the time domain features has been initialized
    initialized = true;
    
    return true;
}
VectorDouble stddev(VectorDouble v) {
    double sum = std::accumulate(v.begin(), v.end(), 0.0);
    double mean = sum / v.size();

    double sq_sum = std::inner_product(v.begin(), v.end(), v.begin(), 0.0);
    double stdev = std::sqrt(sq_sum / v.size() - mean * mean);
    
    return VectorDouble(1, stdev);
}
VectorDouble LowPassFilter::filter(const VectorDouble &x){
    
    if( !initialized ){
        errorLog << "filter(const VectorDouble &x) - Not Initialized!" << endl;
        return VectorDouble();
    }
    
    if( x.size() != numInputDimensions ){
        errorLog << "filter(const VectorDouble &x) - The Number Of Input Dimensions (" << numInputDimensions << ") does not match the size of the input vector (" << x.size() << ")!" << endl;
        return VectorDouble();
    }
    
    for(UINT n=0; n<numInputDimensions; n++){
        processedData[n] = (x[n] * filterFactor) + (yy[n] * (1.0 - filterFactor)) * gain;
        yy[n] = processedData[n];
    }
    return processedData;
}
Beispiel #11
0
bool TimeDomainFeatures::init(UINT bufferLength,UINT numFrames,UINT numDimensions,bool offsetInput,bool useMean,bool useStdDev,bool useEuclideanNorm,bool useRMS){
    
    initialized = false;
    
    if( numFrames > bufferLength ){
        errorLog << "init(...) - The number of numFrames parameter can not be larger than the buffer length parameter!" << endl;
        return false;
    }
    if( bufferLength % numFrames != 0 ){
        errorLog << "init(...) - The buffer length parameter must be divisible with no remainders by the number of numFrames parameter!" << endl;
        return false;
    }
    
    this->bufferLength = bufferLength;
    this->numFrames = numFrames;
    this->numInputDimensions = numDimensions;
    this->offsetInput = offsetInput;
    this->useMean = useMean;
    this->useStdDev = useStdDev;
    this->useEuclideanNorm = useEuclideanNorm;
    this->useRMS = useRMS;
    featureDataReady = false;
    
    //Set the number of output dimensions
    numOutputDimensions = 0;
    if( useMean ){
        numOutputDimensions += numInputDimensions*numFrames;
    }
    if( useStdDev ){
        numOutputDimensions += numInputDimensions*numFrames;
    }
    if( useEuclideanNorm ){
        numOutputDimensions += numInputDimensions*numFrames;
    }
    if( useRMS ){
        numOutputDimensions += numInputDimensions*numFrames;
    }
    if( numOutputDimensions == 0 ){
        errorLog << "init(...) - The numOutputDimensions is zero!" << endl;
        return false;
    }
    
    //Resize the feature vector
    featureVector.resize(numOutputDimensions);
    
    //Resize the raw data buffer
    dataBuffer.resize( bufferLength, VectorDouble(numInputDimensions,0) );

    //Flag that the time domain features has been initialized
    initialized = true;
    
    return true;
}
double MovingAverageFilter::filter(const double x){
    
    //If the filter has not been initialised then return 0, otherwise filter x and return y
    if( !initialized ){
        errorLog << "filter(const double x) - The filter has not been initialized!" << endl;
        return 0;
    }
    
    VectorDouble y = filter(VectorDouble(1,x));
    
    if( y.size() == 0 ) return 0;
    return y[0];
}
Beispiel #13
0
double Derivative::computeDerivative(const double x){
    
    if( numInputDimensions != 1 ){
        errorLog << "computeDerivative(const double x) - The Number Of Input Dimensions is not 1! NumInputDimensions: " << numInputDimensions << endl;
        return 0;
    }
    
    VectorDouble y = computeDerivative( VectorDouble(1,x) );
    
    if( y.size() == 0 ) return 0 ;
    
	return y[0];
}
Beispiel #14
0
double LeakyIntegrator::update(const double x){
    
    if( numInputDimensions != 1 ){
        errorLog << "update(const double x) - The Number Of Input Dimensions is not 1! NumInputDimensions: " << numInputDimensions << endl;
        return 0;
    }
    
    y = update( VectorDouble(1,x) );
    
    if( y.size() == 0 ) return 0 ;
    
	return y[0];
}
double Derivative::computeDerivative(const double x){
    
#ifdef GRT_SAFE_CHECKING
    if( numInputDimensions != 1 ){
        errorLog << "computeDerivative(const double x) - The Number Of Input Dimensions is not 1! NumInputDimensions: " << numInputDimensions << endl;
        return 0;
    }
#endif
    
    vector< double > y = computeDerivative( VectorDouble(1,x) );
    
    if( y.size() == 0 ) return 0 ;
    
	return y[0];
}
Beispiel #16
0
double LowPassFilter::filter(double x){
    
#ifdef GRT_SAFE_CHECKING
    //If the filter has not been initialised then return 0, otherwise filter x and return y
    if( !initialized ){
        errorLog << "filter(double x) - The filter has not been initialized!" << endl;
        return 0;
    }
#endif
    
    VectorDouble y = filter(VectorDouble(1,x));
    
    if( y.size() == 0 ) return 0;
    return y[0];

}
Beispiel #17
0
VectorDouble Derivative::getDerivatives(UINT derivativeOrder){ 
    
    switch( derivativeOrder ){
        case 0:
            return processedData;
            break;
        case( FIRST_DERIVATIVE ):
            return yy; 
            break;
        case( SECOND_DERIVATIVE ):
            return yyy; 
            break;
        default:
            warningLog << "getDerivative(UINT derivativeOrder) - Unkown derivativeOrder: " << derivativeOrder << endl;
            break;
    }
    
    return VectorDouble();
}
Beispiel #18
0
VectorDouble Classifier::getNullRejectionThresholds() const{ 
    if( trained ) return nullRejectionThresholds;
    return VectorDouble(); 
}
Beispiel #19
0
VectorDouble TimeDomainFeatures::update(double x){
	return update(VectorDouble(1,x));
}
VectorDouble TimeseriesBuffer::update(double x){
	return update(VectorDouble(1,x));
}
UINT KMeansQuantizer::quantize(double inputValue){
	return quantize( VectorDouble(1,inputValue) );
}
Beispiel #22
0
VectorDouble ANBC::getNullRejectionThresholds() const{
    if( !trained ) return VectorDouble();
    return nullRejectionThresholds;
}
Beispiel #23
0
UINT RBMQuantizer::quantize(const double inputValue){
	return quantize( VectorDouble(1,inputValue) );
}
VectorDouble Regressifier::getRegressionData() const{ 
    if( trained ){ 
        return regressionData; 
    } 
    return VectorDouble(); 
}
VectorDouble MovementTrajectoryFeatures::update(double x){
	return update(VectorDouble(1,x));
}
Beispiel #26
0
VectorDouble Classifier::getClassLikelihoods() const{ 
    if( trained ) return classLikelihoods;
    return VectorDouble(); 
}
Beispiel #27
0
VectorDouble Classifier::getClassDistances() const{ 
    if( trained ) return classDistances; 
    return VectorDouble(); 
}
Beispiel #28
0
VectorDouble ThresholdDetection::update(double x){
	return update(VectorDouble(1,x));
}
bool MovementTrajectoryFeatures::init(UINT trajectoryLength,UINT numCentroids,UINT featureMode,UINT numHistogramBins,UINT numDimensions,bool useTrajStartAndEndValues,bool useWeightedMagnitudeValues){
    
    initialized = false;
    
    if( numCentroids > trajectoryLength ){
        errorLog << "init(...) - The number of centroids parameter can not be larger than the trajectory length parameter!" << endl;
        return false;
    }
    if( trajectoryLength % numCentroids != 0 ){
        errorLog << "init(...) - The trajectory length parameter must be divisible with no remainders by the number of centroids parameter!" << endl;
        return false;
    }
    
    if( featureMode == CENTROID_ANGLE_2D && numDimensions % 2 != 0 ){
        errorLog << "init(...) - If the featureMode is CENTROID_ANGLE_2D then the numberOfDimensions should be divisble by 2 (as each pair of points should represent {x,y})!" << endl;
        return false;
    }
    
    if( numHistogramBins == 0 && featureMode == CENTROID_ANGLE_2D ){
        errorLog << "init(...) - If the featureMode is CENTROID_ANGLE_2D then the numHistogramBins parameter must greater than 0!" << endl;
        return false;
    }
    
    //Setup the search variables
    this->trajectoryLength = trajectoryLength;
    this->numCentroids = numCentroids;
    this->featureMode = featureMode;
    this->numHistogramBins = numHistogramBins;
    this->numInputDimensions = numDimensions;
    this->useTrajStartAndEndValues = useTrajStartAndEndValues;
    this->useWeightedMagnitudeValues = useWeightedMagnitudeValues;
    featureDataReady = false;
    
    //Set the number of output dimensions
    numOutputDimensions = 0;
    switch( featureMode ){
        case CENTROID_VALUE:
            //In the centroid value mode the useTrajStartAndEndValues is ignored (as the start and end centroids are used by default)
            numOutputDimensions = numInputDimensions*numCentroids;
            break;
        case NORMALIZED_CENTROID_VALUE:
            numOutputDimensions = numInputDimensions*numCentroids;
            if( useTrajStartAndEndValues ){
                numOutputDimensions += numInputDimensions*2;
            }
            break;
        case CENTROID_DERIVATIVE:
            numOutputDimensions = numInputDimensions*(numCentroids-1);
            if( useTrajStartAndEndValues ){
                numOutputDimensions += numInputDimensions*2;
            }            
            break;
        case CENTROID_ANGLE_2D:
            numOutputDimensions = numHistogramBins*(numDimensions/2);
            break;
        default:
            errorLog << "init(...)- Unknown featureMode!" << endl;
            return false;
            break;
    }
    
    if( numOutputDimensions == 0 ){
        errorLog << "init(...) - The numOutputDimensions is zero!" << endl;
        return false;
    }
    
    //Resize the feature vector
    featureVector.resize(numOutputDimensions);
    
    //Resize the raw trajectory data buffer
    trajectoryDataBuffer.resize( trajectoryLength, VectorDouble(numInputDimensions,0) );
    
    //Resize the centroids buffer
    centroids.resize(numCentroids,numInputDimensions);

    //Flag that the zero crossing counter has been initialized
    initialized = true;
    
    return true;
}