VectorDouble ThresholdDetection::update(const VectorDouble &x){ if( !initialized ){ errorLog << "update(const VectorDouble &x) - Not Initialized!" << endl; return vector<double>(); } 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 vector<double>(); } if (inNoise) { //Add the new data to the data buffer dataBuffer.push_back( x ); } //Only flag that the feature data is ready if the data is full if( dataBuffer.getBufferFilled() ){ featureDataReady = true; }else featureDataReady = false; VectorDouble meanFeatures(numInputDimensions,0); VectorDouble stdDevFeatures(numInputDimensions,0); for(UINT n=0; n<numInputDimensions; n++){ for(UINT i=0; i<bufferLength; i++){ //Update the mean meanFeatures[n] += dataBuffer[i][n]; } meanFeatures[n] /= bufferLength; //Update the std dev for(UINT i=0; i<bufferLength; i++){ stdDevFeatures[n] += (dataBuffer[i][n]-meanFeatures[n]) * (dataBuffer[i][n]-meanFeatures[n]); } double norm = bufferLength>1 ? bufferLength-1 : 1; stdDevFeatures[n] = sqrt( stdDevFeatures[n]/norm ); } // TODO(damellis): do something intelligent when numInputDimensions > 1 for(UINT n=0; n<numInputDimensions; n++){ if( x[n] > meanFeatures[n] + alpha * stdDevFeatures[n] ){ inNoise = false; } if (x[n] < meanFeatures[n] + beta * stdDevFeatures[n] ){ inNoise = true; } } //Update the features UINT index = 0; featureVector[index++] = inNoise ? 0.0 : 1.0; for(UINT n=0; n<numInputDimensions; n++){ featureVector[index++] = x[n]; featureVector[index++] = meanFeatures[n]; featureVector[index++] = stdDevFeatures[n]; featureVector[index++] = meanFeatures[n] + alpha * stdDevFeatures[n]; featureVector[index++] = meanFeatures[n] + beta * stdDevFeatures[n]; } return featureVector; }
VectorDouble TimeDomainFeatures::update(const VectorDouble &x){ if( !initialized ){ errorLog << "update(const VectorDouble &x) - Not Initialized!" << endl; return vector<double>(); } 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 vector<double>(); } //Add the new data to the data buffer dataBuffer.push_back( x ); //Only flag that the feature data is ready if the data is full if( dataBuffer.getBufferFilled() ){ featureDataReady = true; }else featureDataReady = false; MatrixDouble meanFeatures(numInputDimensions,numFrames); MatrixDouble stdDevFeatures(numInputDimensions,numFrames); MatrixDouble normFeatures(numInputDimensions,numFrames); MatrixDouble rmsFeatures(numInputDimensions,numFrames); MatrixDouble data(bufferLength,numInputDimensions); if( offsetInput ){ for(UINT n=0; n<numInputDimensions; n++){ data[0][n] = dataBuffer[0][n]; for(UINT i=1; i<bufferLength; i++){ data[i][n] = dataBuffer[i][n]-dataBuffer[0][n]; } } }else{ for(UINT n=0; n<numInputDimensions; n++){ for(UINT i=0; i<bufferLength; i++){ data[i][n] = dataBuffer[i][n]; } } } if( useMean || useStdDev ){ meanFeatures.setAllValues(0); stdDevFeatures.setAllValues(0); } if( useEuclideanNorm ) normFeatures.setAllValues(0); if( useRMS ) rmsFeatures.setAllValues(0); UINT frameSize = bufferLength / numFrames; UINT frame = 0; UINT index = 0; for(UINT n=0; n<numInputDimensions; n++){ frame = 0; index = 0; for(UINT i=0; i<bufferLength; i++){ //Update the mean meanFeatures[n][frame] += data[i][n]; //Update the norm features if( useEuclideanNorm ) normFeatures[n][frame] += data[i][n]*data[i][n]; //Update the rms features if( useRMS ) rmsFeatures[n][frame] += data[i][n]*data[i][n]; if( ++index == frameSize ){ frame++; index = 0; } } //Update the mean for(UINT j=0; j<numFrames; j++){ meanFeatures[n][j] /= frameSize; } //Update the std dev if needed if( useStdDev ){ frame = 0; index = 0; for(UINT i=0; i<bufferLength; i++){ stdDevFeatures[n][frame] += (data[i][n]-meanFeatures[n][frame]) * (data[i][n]-meanFeatures[n][frame]); if( ++index == frameSize ){ frame++; index = 0; } } double norm = frameSize>1 ? frameSize-1 : 1; for(UINT j=0; j<numFrames; j++){ stdDevFeatures[n][j] = sqrt( stdDevFeatures[n][j]/norm ); } } //Update the euclidean norm if needed if( useEuclideanNorm ){ for(UINT j=0; j<numFrames; j++){ normFeatures[n][j] = sqrt( normFeatures[n][j] ); } } //Update the rms if needed if( useRMS ){ for(UINT j=0; j<numFrames; j++){ rmsFeatures[n][j] = sqrt( rmsFeatures[n][j] / frameSize ); } } } //Update the features index = 0; frame = 0; for(UINT n=0; n<numInputDimensions; n++){ for(UINT j=0; j<numFrames; j++){ if( useMean ){ featureVector[index++] = meanFeatures[n][j]; } if( useStdDev ){ featureVector[index++] = stdDevFeatures[n][j]; } if( useEuclideanNorm ){ featureVector[index++] = normFeatures[n][j]; } if( useRMS ){ featureVector[index++] = rmsFeatures[n][j]; } } } return featureVector; }