Finally the formula is multiplied with the constant n:
nS
1
= nS
0
+(x
n
− x
0
)((n− 1)x
n
+ (n+ 1)x
0
− 2nm
0
).
(16)
For the calculation nS
0
and nm
0
as well as the last
n samples have to be stored. Storing these samples
might be also required for other signal processing
steps like baseline correction, normalization and ar-
tifact correction. If the used samples are integers,
the update can be calculated accurately without any
rounding errors. This is usually the case for EEG
and EMG signal processing. For the calculation with
floating point or fixed point numbers, the multiplica-
tion with (x
n
− x
0
) might be problematic. To finally
divide by n or n
2
is not problematic, for two reasons:
First of all, for EMG movement detection, we use the
variance as a filter, hence, it does not matter if it is
scaled by n
2
and second we fixed n and therefore can
calculate
1
n
and
1
n
2
beforehand with the required accu-
racy.
3 EMPIRICAL RESULTS
In this section, we compare the proposed method and
the naiveway of calculating the variance for a window
of fixed size with n samples. In the proposed formula,
the complexity is O(k) where k is the size of the time
series. Compared to O(nk) for the naive way, here the
complexity also depends on the window size since the
variances have to be completely recalculated for each
new sample.
In order to verify this and to point out the problem
with the naive way, we performed tests on artificial
data. The data, passed to the two algorithms, was a
sinusoidal wave with a frequency of 5 Hz sampled
with 5 kHz for 1 minute (300000 samples). With both
approaches, the moving variance for each window us-
ing 8 different sizes (2, 10, 50, 100, 150, 200, 250, 300
samples) was calculated with 10 repetitions for each
window size. The total execution time was measured.
Results are shown in Table 1.
All calculations were performed on an Apple
iMac with an Intel Core i5 CPU @2.7 GHz and 16 GB
of RAM running Mac OS X 10.7.5. The implemen-
tation was performed in Python using version 2.6.8.
The results show that for small window sizes up to
10 samples, the naive calculation of the variance out-
performs the proposed method. With increasing win-
dow sizes, the computational time needed increases,
too. For a size of 300samples the time exceeds the
length of the time series by 10.6s making an online
processing with this approach impossible. For the
proposed method, the results show that its calculation
time is independent of the window sizes.
4 MOVEMENT PREDICTION
WITH EMG
In a previous study, we could show that EMG sig-
nals can be used to reliably predict voluntary move-
ments of the right human arm. We recorded EMG
data from the right arm of 8 healthy, right-handed and
normal or corrected-to-normalvision male subjects of
age 26.0 ± 3.3. For each subject 6 runs containing
40 movements each were recorded. We investigated
two movements speeds, slow and fast, and therefore 3
sets each, were recorded for every subject. For further
details on the experiments please refer to (Tabie and
Kirchner, 2013). The variance was used as non linear
filter for preprocessing the EMG signals. The prepro-
cessed signals were passed to an adaptive threshold
for the detection of movement onsets. The adaptive
threshold is given as
T
n
(t) = µ
n
+ pσ
n
, (17)
where µ and σ are the mean and the standard devia-
tion, for a window of n samples ending at time point t,
respectively, and p is a sensitivity factor. The variance
filter was calculated similar
V
m
(t) = σ
m
2
, (18)
where σ
2
m
is the variance for a window of m samples
ending at time point t. The window sizes where op-
timized and resulted in values n = 20000 (adaptive
threshold) and m = 100 (variance filter).
We analyzed the needed time for each run of this
study for the here presented approach and the naive
one. The results are shown in Table 2. The du-
rations for each run varied in a range from 5.3 to
12.3minutes. The needed computational times for
our approach varied in range from 1.0 to 2.3 minutes
and for the naive one from 404.8 to 944.3minutes
(≈ 6.7 to 15.7 hours). Thus, the naive approach is
not capable of online EMG processing for movement
prediction in contrast to our approach. The long dura-
tions for the naive approach can be explained by the
large window size (20000 samples) for the standard
deviation in the adaptive threshold.
The presented results were calculated on the basis
of results explained in Section 3. From those results
we derived a formula to calculate the computational
time for each method based on the window size and
the length of a set. Even though the standard devia-
tion used in the adaptive threshold is not exactly the
variance its calculation is basically the same since it
is the square root of the variance. Therefore, it is fea-
sible to determine the needed time by looking at the
time of the variance calculation.
MemoryandProcessingEfficientFormulaforMovingVarianceCalculationinEEGandEMGSignalProcessing
43