Skip to content

Commit 18c6ccd

Browse files
committed
Updating to integral filter.
1 parent ea58f54 commit 18c6ccd

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

Devlpr.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,32 @@ int Devlpr::lastValue()
3434
return buf[bufInd];
3535
}
3636

37-
float filt[155] = {0.001513386020877483, 0.0065815188379678286, 0.002386245075165573, 0.007566874896062961, 0.0019103137662135668, 0.006992501751352943, -4.495537256296272e-05, 0.0051834687785310895, -0.002648935758060934, 0.0033582848135394524, -0.004403527605449877, 0.0030360303136534005, -0.003970335570216464, 0.005088770411351987, -0.0010939546033377785, 0.009000196239377003, 0.0029797542078670355, 0.012873573885430926, 0.0059781068429677255, 0.014341550635307414, 0.005839078055311389, 0.011962177185195152, 0.0020540826800157686, 0.00628910346542397, -0.0037297389859886764, -0.00012797925377133632, -0.008360410071453508, -0.0039928957513214345, -0.008878628957170818, -0.0031739484856050243, -0.004358329258205972, 0.0018195934592311605, 0.0032359982373611025, 0.007774757946784205, 0.009842353743397264, 0.010355335400477728, 0.01147309137873063, 0.006565601478880059, 0.006608133637858301, -0.0032987826122814258, -0.002619853769121824, -0.01545390682749638, -0.011297979146828542, -0.024490682105516394, -0.01435098160629302, -0.026385833993863368, -0.009505783044341567, -0.02096941545317657, 0.0011335663648171434, -0.01233293740414986, 0.01195399467829908, -0.006767237737413448, 0.016825048377839883, -0.009258440470070626, 0.012608322724518551, -0.020500324958948334, 0.0011602156609500842, -0.03620786400461252, -0.011438446353649424, -0.04927237222277001, -0.018137619347845493, -0.053662062433447046, -0.014898493047407273, -0.047894076917303914, -0.0031289843896549954, -0.0360514341092553, 0.010898270201266994, -0.025659125374167382, 0.019426167695941163, -0.023527141221818995, 0.017546298160676748, -0.031878568213663964, 0.006034030764155489, -0.04698185315200764, -0.008967266965349297, -0.06112055411706036, -0.01929407495907671, 0.9363988033051267, -0.019294074959076694, -0.06112055411706037, -0.008967266965349314, -0.04698185315200765, 0.006034030764155483, -0.03187856821366395, 0.01754629816067676, -0.023527141221819026, 0.019426167695941157, -0.025659125374167382, 0.010898270201266975, -0.03605143410925534, -0.003128984389654987, -0.04789407691730391, -0.014898493047407264, -0.053662062433447046, -0.018137619347845493, -0.04927237222277002, -0.011438446353649422, -0.036207864004612525, 0.0011602156609500855, -0.020500324958948334, 0.012608322724518554, -0.00925844047007063, 0.01682504837783989, -0.006767237737413443, 0.011953994678299076, -0.012332937404149856, 0.0011335663648171465, -0.020969415453176564, -0.009505783044341567, -0.026385833993863354, -0.014350981606293018, -0.02449068210551641, -0.011297979146828535, -0.01545390682749638, -0.002619853769121825, -0.0032987826122814244, 0.006608133637858307, 0.006565601478880057, 0.011473091378730633, 0.010355335400477728, 0.009842353743397265, 0.007774757946784199, 0.0032359982373610994, 0.0018195934592311596, -0.004358329258205973, -0.003173948485605027, -0.008878628957170814, -0.003992895751321437, -0.008360410071453508, -0.0001279792537713359, -0.0037297389859886764, 0.00628910346542397, 0.00205408268001577, 0.011962177185195154, 0.005839078055311389, 0.014341550635307412, 0.0059781068429677255, 0.012873573885430931, 0.0029797542078670333, 0.009000196239377003, -0.0010939546033377785, 0.005088770411351986, -0.003970335570216465, 0.003036030313653403, -0.004403527605449876, 0.0033582848135394537, -0.002648935758060934, 0.005183468778531092, -4.4955372562961925e-05, 0.0069925017513529425, 0.0019103137662135673, 0.007566874896062961, 0.002386245075165573, 0.00658151883796783, 0.0015133860208774836};
37+
int filt[93] = {24, 23, 100, 109, 130, 128, 81, 68, -39, -41, -181, -140, -280, -170, -294, -109, -225, 14, -124, 134, -61, 184, -87, 136, -205, 14, -365, -115, -491, -176, -523, -132, -450, -4, -320, 140, -215, 218, -204, 181, -306, 43, -479, -128, -637, -244, 9329, -244, -637, -128, -479, 43, -306, 181, -204, 218, -215, 140, -320, -4, -450, -132, -523, -176, -491, -115, -365, 14, -205, 136, -87, 184, -61, 134, -124, 14, -225, -109, -294, -170, -280, -140, -181, -41, -39, 68, 81, 128, 130, 109, 100, 23, 24};
38+
float filtFactor = 10000.0; // the multiplier on the integral filter
3839
float Devlpr::lastValueFiltered()
3940
{
41+
// should help to roughly recenter data (use quick integral math)
42+
long sampAvg = emgRunningSum / BUFSIZE;
4043
// the buffer is the same size as the filter so filtered data
4144
// will be roughly len/2 ticks in the past
42-
float ret = 0.0;
45+
long ret = 0;
4346
int currInd = bufInd + 1; // start at the tail
4447
// the filter is also symmetric, so convolve=linear combination
4548
for (int i = 0; i < BUFSIZE; i++) {
4649
// check the current circular buffer index
4750
if (currInd >= BUFSIZE) { // faster than modulo
4851
currInd = 0;
4952
}
53+
// we will need to coerce the ints to longs to avoid overflow
54+
long filtVal = (long)filt[i];
55+
long sampVal = (long)buf[currInd] - sampAvg;
5056
// do the multiply/add
51-
ret = ret + (filt[i] * buf[currInd]);
57+
ret = ret + (filtVal * sampVal);
5258
// and update the current buffer index for next
5359
currInd++;
5460
}
55-
return ret;
61+
// our integral filter is multiplied by some factor, so correct
62+
return ret / filtFactor;
5663
}
5764

5865
float Devlpr::windowAvg()

Devlpr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Devlpr
1313
float windowAvg();
1414
private:
1515
// general buffer bookkeeping
16-
static const byte BUFSIZE = 155; // size of filter
16+
static const byte BUFSIZE = 93; // size of filter
1717
byte bufInd;
1818
int buf[BUFSIZE];
1919
unsigned long lastTickMicros = 0;

0 commit comments

Comments
 (0)