Commit 167f3274 authored by mzed's avatar mzed
Browse files

cleaning up

parent de55df02
......@@ -65,7 +65,7 @@ T dtw<T>::getCost(const std::vector<std::vector<T> > &seriesX, const std::vector
};
template<typename T>
warpPath dtw<T>::calculatePath(int seriesXsize, int seriesYsize) {
warpPath dtw<T>::calculatePath(int seriesXsize, int seriesYsize) const {
warpPath warpPath;
int i = seriesXsize - 1;
int j = seriesYsize - 1;
......
......@@ -32,7 +32,7 @@ public:
private:
inline T distanceFunction(const std::vector<T> &pointX, const std::vector<T> &point);
std::vector<std::vector<T> > costMatrix;
warpPath calculatePath(int seriesXsize, int seriesYsize);
warpPath calculatePath(int seriesXsize, int seriesYsize) const;
};
#endif /* dtw_h */
......@@ -614,6 +614,13 @@ Module.StreamBuffer.prototype = {
maximum: function () {
return this.rapidStream.maximum();
},
/**
* Count the number of zero crossings in the buffer.
* @return {number} number of zero crossings.
*/
numZeroCrossings: function () {
return this.rapidStream.numZeroCrossings();
},
/**
* Calculate the sum of all values in the buffer.
* @return {number} sum.
......
......@@ -23,6 +23,7 @@ EMSCRIPTEN_BINDINGS(rapidStream_module) {
.function("acceleration", &rapidStream<double>::acceleration)
.function("minimum", &rapidStream<double>::minimum)
.function("maximum", &rapidStream<double>::maximum)
.function("numZeroCrossings", &rapidStream<double>::numZeroCrossings)
.function("sum", &rapidStream<double>::sum)
.function("mean", &rapidStream<double>::mean)
.function("standardDeviation", &rapidStream<double>::standardDeviation)
......
......@@ -54,7 +54,7 @@ warpPath fastDTW<T>::getWarpPath(const std::vector<std::vector<T>> &seriesX, con
};
template<typename T>
std::vector<std::vector<T> > fastDTW<T>::downsample(const std::vector<std::vector<T>> &series, T resolution) {
inline std::vector<std::vector<T> > fastDTW<T>::downsample(const std::vector<std::vector<T>> &series, T resolution) {
std::vector<std::vector<T> > shrunkenSeries;
for (int i = 0; i < series.size(); ++i) {
if (i % 2 == 0) {
......
......@@ -53,7 +53,7 @@ private:
* @param resolution (not used)
* @return downsampled series
`*/
static std::vector<std::vector<T> > downsample(const std::vector<std::vector<T>> &series, T resolution);
inline static std::vector<std::vector<T> > downsample(const std::vector<std::vector<T>> &series, T resolution);
};
......
......@@ -15,7 +15,7 @@
#endif
template<typename T>
rapidStream<T>::rapidStream(int window_size) {
rapidStream<T>::rapidStream (int window_size) {
windowSize = window_size;
windowIndex = 0;
circularWindow = new T[window_size];
......@@ -24,8 +24,8 @@ rapidStream<T>::rapidStream(int window_size) {
}
//Baysian Filter setup
bayesFilt.diffusion = powf(10., -2);
bayesFilt.jump_rate = powf(10., -10);
bayesFilt.diffusion = powf (10., -2);
bayesFilt.jump_rate = powf (10., -10);
bayesFilt.mvc[0] = 1.;
bayesFilt.init();
......@@ -62,22 +62,22 @@ void rapidStream<T>::pushToWindow(T input) {
}
template<typename T>
inline T rapidStream<T>::calcCurrentVel(int i) {
return circularWindow[(i + windowIndex) % windowSize] - circularWindow[(i + windowIndex - 1) % windowSize];
inline T rapidStream<T>::calcCurrentVel(int i) const {
return circularWindow[ (i + windowIndex) % windowSize] - circularWindow[ (i + windowIndex - 1) % windowSize];
}
template<typename T>
T rapidStream<T>::velocity() {
return calcCurrentVel(-1);
T rapidStream<T>::velocity() const {
return calcCurrentVel (-1);
};
template<typename T>
T rapidStream<T>::acceleration() {
return calcCurrentVel(-2) - calcCurrentVel(-3);
T rapidStream<T>::acceleration() const {
return calcCurrentVel (-2) - calcCurrentVel (-3);
};
template<typename T>
T rapidStream<T>::minimum() {
T rapidStream<T>::minimum() const {
T minimum = std::numeric_limits<T>::infinity();
for (int i = 0; i < windowSize; ++i) {
if (circularWindow[i] < minimum) {
......@@ -88,7 +88,7 @@ T rapidStream<T>::minimum() {
}
template<typename T>
T rapidStream<T>::maximum() {
T rapidStream<T>::maximum() const {
T maximum = std::numeric_limits<T>::min();
for (int i = 0; i < windowSize; ++i) {
if (circularWindow[i] > maximum) {
......@@ -99,22 +99,48 @@ T rapidStream<T>::maximum() {
}
template<typename T>
T rapidStream<T>::sum() {
uint32_t rapidStream<T>::numZeroCrossings() const {
uint32_t zeroCrossings = 0;
//Is the begininng positive, negative, or 0?
int previous = 1;
if (circularWindow[windowIndex] < 0) {
previous = -1;
} else if (circularWindow[windowIndex] == 0) {
++zeroCrossings;
previous = 0;
}
for (int i =1; i < windowSize; ++i) {
int index = (windowIndex + i) % windowSize;
if (circularWindow[index] < 0 && previous >=0) { //Transition to negative
++zeroCrossings;
previous = -1;
} else if (circularWindow[index] > 0 && previous <= 0){ //Transition to positive
++zeroCrossings;
previous = 1;
} else { //Sample == 0
previous = 0;
}
}
return zeroCrossings;
}
template<typename T>
T rapidStream<T>::sum() const {
T newSum = 0;
for(int i = 0; i < windowSize; ++i)
{
for (int i = 0; i < windowSize; ++i) {
newSum += circularWindow[i];
}
return newSum;
}
template<typename T>
T rapidStream<T>::mean() {
T rapidStream<T>::mean() const {
return sum()/windowSize;
}
template<typename T>
T rapidStream<T>::standardDeviation() {
T rapidStream<T>::standardDeviation() const {
T newMean = mean();
T standardDeviation = 0.;
for(int i = 0; i < windowSize; ++i) {
......@@ -124,7 +150,7 @@ T rapidStream<T>::standardDeviation() {
}
template<typename T>
T rapidStream<T>::rms() {
T rapidStream<T>::rms() const {
T rms = 0;
for (int i = 0; i < windowSize; ++i) {
rms += (circularWindow[i] * circularWindow[i]);
......@@ -136,19 +162,19 @@ T rapidStream<T>::rms() {
template<typename T>
T rapidStream<T>::bayesFilter(T input) {
std::vector<float> inputVec = { float(input) };
bayesFilt.update(inputVec);
return T(bayesFilt.output[0]);
bayesFilt.update (inputVec);
return T (bayesFilt.output[0]);
}
template<typename T>
void rapidStream<T>::bayesSetDiffusion(float diffusion) {
bayesFilt.diffusion = powf(10., diffusion);
bayesFilt.diffusion = powf (10., diffusion);
bayesFilt.init();
}
template<typename T>
void rapidStream<T>::bayesSetJumpRate(float jump_rate) {
bayesFilt.jump_rate = powf(10., jump_rate);
bayesFilt.jump_rate = powf (10., jump_rate);
bayesFilt.init();
}
......@@ -160,10 +186,10 @@ void rapidStream<T>::bayesSetMVC(float mvc) {
template<typename T>
T rapidStream<T>::minVelocity() {
T rapidStream<T>::minVelocity() const {
T minVel = std::numeric_limits<T>::infinity();
for (int i = 0; i < windowSize; ++i) {
T currentVel = calcCurrentVel(i);
T currentVel = calcCurrentVel (i);
if ( currentVel < minVel) {
minVel = currentVel;
}
......@@ -172,10 +198,10 @@ T rapidStream<T>::minVelocity() {
}
template<typename T>
T rapidStream<T>::maxVelocity() {
T rapidStream<T>::maxVelocity() const {
T maxVel = std::numeric_limits<T>::lowest();
for (int i = 0; i < windowSize; ++i) {
T currentVel = calcCurrentVel(i);
T currentVel = calcCurrentVel (i);
if (currentVel > maxVel) {
maxVel = currentVel;
}
......@@ -184,11 +210,11 @@ T rapidStream<T>::maxVelocity() {
}
template<typename T>
T rapidStream<T>::minAcceleration() {
T rapidStream<T>::minAcceleration() const {
T minAccel = std::numeric_limits<T>::infinity();
T lastVel = calcCurrentVel(1);
T lastVel = calcCurrentVel (1);
for (int i = 2; i < windowSize; ++i) {
T currentVel = calcCurrentVel(i);
T currentVel = calcCurrentVel (i);
T currentAccel = currentVel - lastVel;
lastVel = currentVel;
if (currentAccel < minAccel) {
......@@ -199,11 +225,11 @@ T rapidStream<T>::minAcceleration() {
}
template<typename T>
T rapidStream<T>::maxAcceleration() {
T rapidStream<T>::maxAcceleration() const {
T maxAccel = std::numeric_limits<T>::lowest();
T lastVel = calcCurrentVel(1);
for (int i = 2; i < windowSize; ++i) {
T currentVel = calcCurrentVel(i);
T currentVel = calcCurrentVel (i);
T currentAccel = currentVel - lastVel;
lastVel = currentVel;
if (currentAccel > maxAccel) {
......
......@@ -24,7 +24,7 @@ public:
* Create a circular buffer with an arbitrary number of elements.
* @param int: number of elements to hold in the buffer
*/
rapidStream(int windowSize);
rapidStream (int windowSize);
~rapidStream();
......@@ -36,82 +36,87 @@ public:
/** Add a value to a circular buffer whose size is defined at creation.
* @param double: value to be pushed into circular buffer.
*/
void pushToWindow(T input);
void pushToWindow (T input);
/** Calculate the first-order difference (aka velocity) between the last two inputs.
* @return double: difference between last two inputs.
*/
T velocity();
T velocity() const;
/** Calculate the second-order difference (aka acceleration) over the last three inputs.
* @return double: acceleration over the last three inputs.
*/
T acceleration();
T acceleration() const;
/** Find the minimum value in the buffer.
* @return double: minimum.
*/
T minimum();
T minimum() const;
/** Find the maximum value in the buffer.
* @return double: maximum.
*/
T maximum();
T maximum() const;
/** Count the number of zero crossings in the buffer.
* @return int: number of zero crossings.
*/
uint32_t numZeroCrossings() const;
/** Calculate the sum of all values in the buffer.
* @return double: sum.
* @return T: sum.
*/
T sum();
T sum() const;
/** Calculate the mean of all values in the buffer.
* @return double: mean.
*/
T mean();
T mean() const;
/** Calculate the standard deviation of all values in the buffer.
* @return double: standard deviation.
*/
T standardDeviation();
T standardDeviation() const;
/** Calculate the root mean square of the values in the buffer
* @return double: rms
*/
T rms();
T rms() const;
/** Non-linear Baysian filtering for EMG envelope extraction.
* @return current envelope value
*/
T bayesFilter(T inputValue);
void bayesSetDiffusion(float logDiffusion);
void bayesSetJumpRate(float jump_rate);
void bayesSetMVC(float mvc);
T bayesFilter (T inputValue);
void bayesSetDiffusion (float logDiffusion);
void bayesSetJumpRate (float jump_rate);
void bayesSetMVC (float mvc);
/** Calculate the minimum first-order difference over consecutive inputs in the buffer.
* @return double: minimum velocity.
*/
T minVelocity();
T minVelocity() const;
/** Calculate the maximum first-order difference over consecutive inputs in the buffer.
* @return double: maximum velocity.
*/
T maxVelocity();
T maxVelocity() const;
/** Calculate the minimum second-order difference over consecutive inputs in the buffer.
* @return double: minimum acceleration.
*/
T minAcceleration();
T minAcceleration() const;
/** Calculate the maximum second-order difference over consecutive inputs in the buffer.
* @return double: maximum acceleration.
*/
T maxAcceleration();
T maxAcceleration() const;
private:
uint32_t windowSize;
std::atomic<uint32_t> windowIndex;
T *circularWindow;
T calcCurrentVel(int i);
inline T calcCurrentVel (int i) const;
BayesianFilter bayesFilt;
};
......
......@@ -50,7 +50,7 @@ searchWindow<T>::searchWindow(const int seriesXSize, const int seriesYSize, cons
}
template<typename T>
void searchWindow<T>::markVisited(int col, int row) {
inline void searchWindow<T>::markVisited(int col, int row) {
if (row <= maxY && col < minMaxValues.size()) { //Don't mark beyond the edge of the window
if (minMaxValues[col].first == -1) {
minMaxValues[col].first = row;
......
......@@ -27,7 +27,7 @@ public:
private:
int maxY;
void markVisited(int col, int row);
inline void markVisited(int col, int row);
void expandWindow(int searchRadius);
};
......
......@@ -394,21 +394,36 @@ describe('RapidLib Signal Processing', function () {
});
myStream.reset();
myStream.push(0);
myStream.push(1);
myStream.push(3)
myStream.push(3)
myStream.push(-10)
myStream.push(3);
myStream.push(29);
it('velocity should be 1', function () {
expect(myStream.velocity()).to.equal(1);
expect(myStream.velocity()).to.equal(26);
});
let accelStream = new rapidMix.StreamBuffer;
accelStream.push(0);
accelStream.push(0);
accelStream.push(-10);
accelStream.push(1);
accelStream.push(1);
accelStream.push(11);
it('acceleration should be 10'/*, function () {
it('acceleration should be 10', function () {
expect(accelStream.acceleration()).to.equal(10);
}*/);
});
let zeroStream = new rapidMix.StreamBuffer(6);
zeroStream.push(1);
zeroStream.push(0);
zeroStream.push(-1);
zeroStream.push(0);
zeroStream.push(1);
zeroStream.push(-1);
it('zeroCrossings should be 3', function () {
expect(zeroStream.numZeroCrossings()).to.equal(3);
});
describe('when streaming to statStream', function () {
let statStream = new rapidMix.StreamBuffer(5);
statStream.push(1.1);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment