Почему это должно быть закодировано? Запрос похож на это:
GET /url HTTP/1.1
(Ignoring headers)
существует 3 поля, разделенные пробелом. Если Вы помещаете пространство в свой URL:
GET /url end_url HTTP/1.1
Вы знаете, имеют 4 поля, сервер HTTP скажет Вам, что это - неверный запрос.
GET /url%20end_url HTTP/1.1
3 поля => допустимый
Примечание: в строке запроса (после того, как?), пространство обычно кодируется как +
GET /url?var=foo+bar HTTP/1.1
, а не
GET /url?var=foo%20bar HTTP/1.1
Я просмотрел R's src / library / stats / src / Trunmed. c
несколько раз, так как я тоже хотел нечто подобное в автономной подпрограмме C ++ class / C. Обратите внимание, что на самом деле это две реализации в одной, см. src / library / stats / man / runmed.Rd
(источник файла справки), в котором говорится:
\details{
Apart from the end values, the result \code{y = runmed(x, k)} simply has
\code{y[j] = median(x[(j-k2):(j+k2)])} (k = 2*k2+1), computed very
efficiently.
The two algorithms are internally entirely different:
\describe{
\item{"Turlach"}{is the Härdle-Steiger
algorithm (see Ref.) as implemented by Berwin Turlach.
A tree algorithm is used, ensuring performance \eqn{O(n \log
k)}{O(n * log(k))} where \code{n <- length(x)} which is
asymptotically optimal.}
\item{"Stuetzle"}{is the (older) Stuetzle-Friedman implementation
which makes use of median \emph{updating} when one observation
enters and one leaves the smoothing window. While this performs as
\eqn{O(n \times k)}{O(n * k)} which is slower asymptotically, it is
considerably faster for small \eqn{k} or \eqn{n}.}
}
}
Было бы неплохо увидеть это повторно. используется более автономно. Вы работаете волонтером? Я могу помочь с некоторыми битами R.
Редактировать 1 : Помимо ссылки на старую версию Trunmed.c выше, вот текущие копии SVN
Srunmed.c
(для Stuetzle version) Trunmed.c
(для версии Turlach) runmed.R
для функции R, вызывающей эти Edit 2 : у Райана Тибширани есть некоторый код на C и Fortran на быстром объединении медианных значений , что может быть подходящей отправной точкой для оконного подхода.
Обратите внимание, что на самом деле это две реализации в одной, см. src / library / stats / man / runmed.Rd
(источник файла справки), в котором говорится:
\details{
Apart from the end values, the result \code{y = runmed(x, k)} simply has
\code{y[j] = median(x[(j-k2):(j+k2)])} (k = 2*k2+1), computed very
efficiently.
The two algorithms are internally entirely different:
\describe{
\item{"Turlach"}{is the Härdle-Steiger
algorithm (see Ref.) as implemented by Berwin Turlach.
A tree algorithm is used, ensuring performance \eqn{O(n \log
k)}{O(n * log(k))} where \code{n <- length(x)} which is
asymptotically optimal.}
\item{"Stuetzle"}{is the (older) Stuetzle-Friedman implementation
which makes use of median \emph{updating} when one observation
enters and one leaves the smoothing window. While this performs as
\eqn{O(n \times k)}{O(n * k)} which is slower asymptotically, it is
considerably faster for small \eqn{k} or \eqn{n}.}
}
}
Было бы неплохо увидеть это повторно. используется более автономно. Вы работаете волонтером? Я могу помочь с некоторыми битами R.
Редактировать 1 : Помимо ссылки на старую версию Trunmed.c выше, вот текущие копии SVN
Srunmed.c
(для Stuetzle version) Trunmed.c
(для версии Turlach) runmed.R
для функции R, вызывающей эти Edit 2 : у Райана Тибширани есть некоторый код на C и Fortran на быстром объединении медианных значений , что может быть подходящей отправной точкой для оконного подхода.
Обратите внимание, что на самом деле это две реализации в одной, см. src / library / stats / man / runmed.Rd
(источник файла справки), в котором говорится:
\details{
Apart from the end values, the result \code{y = runmed(x, k)} simply has
\code{y[j] = median(x[(j-k2):(j+k2)])} (k = 2*k2+1), computed very
efficiently.
The two algorithms are internally entirely different:
\describe{
\item{"Turlach"}{is the Härdle-Steiger
algorithm (see Ref.) as implemented by Berwin Turlach.
A tree algorithm is used, ensuring performance \eqn{O(n \log
k)}{O(n * log(k))} where \code{n <- length(x)} which is
asymptotically optimal.}
\item{"Stuetzle"}{is the (older) Stuetzle-Friedman implementation
which makes use of median \emph{updating} when one observation
enters and one leaves the smoothing window. While this performs as
\eqn{O(n \times k)}{O(n * k)} which is slower asymptotically, it is
considerably faster for small \eqn{k} or \eqn{n}.}
}
}
Было бы неплохо увидеть это повторно. используется более автономно. Вы работаете волонтером? Я могу помочь с некоторыми битами R.
Редактировать 1 : Помимо ссылки на старую версию Trunmed.c выше, вот текущие копии SVN
Srunmed.c
(для Stuetzle version) Trunmed.c
(для версии Turlach) runmed.R
для функции R, вызывающей эти Edit 2 : Райан Тибширани имеет некоторый код C и Fortran на быстром объединении медианных значений , что может быть подходящей отправной точкой для оконного подхода.
Rd (источник файла справки), в котором написано\details{
Apart from the end values, the result \code{y = runmed(x, k)} simply has
\code{y[j] = median(x[(j-k2):(j+k2)])} (k = 2*k2+1), computed very
efficiently.
The two algorithms are internally entirely different:
\describe{
\item{"Turlach"}{is the Härdle-Steiger
algorithm (see Ref.) as implemented by Berwin Turlach.
A tree algorithm is used, ensuring performance \eqn{O(n \log
k)}{O(n * log(k))} where \code{n <- length(x)} which is
asymptotically optimal.}
\item{"Stuetzle"}{is the (older) Stuetzle-Friedman implementation
which makes use of median \emph{updating} when one observation
enters and one leaves the smoothing window. While this performs as
\eqn{O(n \times k)}{O(n * k)} which is slower asymptotically, it is
considerably faster for small \eqn{k} or \eqn{n}.}
}
}
. Было бы неплохо, если бы это было повторно использовано более автономным способом. Вы работаете волонтером? Я могу помочь с некоторыми битами R.
Редактировать 1 : Помимо ссылки на старую версию Trunmed.c выше, вот текущие копии SVN
Srunmed.c
(для Stuetzle version) Trunmed.c
(для версии Turlach) runmed.R
для функции R, вызывающей эти Edit 2 : Райан Тибширани имеет некоторый код C и Fortran на быстром распределении медианных значений , что может быть подходящей отправной точкой для оконного подхода.
Rd (источник файла справки), в котором написано\details{
Apart from the end values, the result \code{y = runmed(x, k)} simply has
\code{y[j] = median(x[(j-k2):(j+k2)])} (k = 2*k2+1), computed very
efficiently.
The two algorithms are internally entirely different:
\describe{
\item{"Turlach"}{is the Härdle-Steiger
algorithm (see Ref.) as implemented by Berwin Turlach.
A tree algorithm is used, ensuring performance \eqn{O(n \log
k)}{O(n * log(k))} where \code{n <- length(x)} which is
asymptotically optimal.}
\item{"Stuetzle"}{is the (older) Stuetzle-Friedman implementation
which makes use of median \emph{updating} when one observation
enters and one leaves the smoothing window. While this performs as
\eqn{O(n \times k)}{O(n * k)} which is slower asymptotically, it is
considerably faster for small \eqn{k} or \eqn{n}.}
}
}
. Было бы неплохо, если бы это было повторно использовано более автономным способом. Вы работаете волонтером? Я могу помочь с некоторыми битами R.
Редактировать 1 : Помимо ссылки на старую версию Trunmed.c выше, вот текущие копии SVN
Srunmed.c
(для Stuetzle version) Trunmed.c
(для версии Turlach) runmed.R
для функции R, вызывающей эти Edit 2 : Райан Тибширани имеет некоторый код C и Fortran на быстром объединении медианных значений , что может быть подходящей отправной точкой для оконного подхода.
If you have the ability to reference values as a function of points in time, you could sample values with replacement, applying bootstrapping to generate a bootstrapped median value within confidence intervals. This may let you calculate an approximated median with greater efficiency than constantly sorting incoming values into a data structure.
Если вам просто требуется сглаженное среднее, можно быстро / легко умножить последнее значение на x, а среднее значение на (1-x), а затем сложить их. Затем это становится новым средним.
править: Не то, что просил пользователь, и не так статистически достоверно, но достаточно хорошо для множества применений.
Я оставлю его здесь (несмотря на отрицательные голоса) для поиска!
Вот простой алгоритм квантованных данных (несколько месяцев спустя):
""" median1.py: moving median 1d for quantized, e.g. 8-bit data
Method: cache the median, so that wider windows are faster.
The code is simple -- no heaps, no trees.
Keywords: median filter, moving median, running median, numpy, scipy
See Perreault + Hebert, Median Filtering in Constant Time, 2007,
http://nomis80.org/ctmf.html: nice 6-page paper and C code,
mainly for 2d images
Example:
y = medians( x, window=window, nlevel=nlevel )
uses:
med = Median1( nlevel, window, counts=np.bincount( x[0:window] ))
med.addsub( +, - ) -- see the picture in Perreault
m = med.median() -- using cached m, summ
How it works:
picture nlevel=8, window=3 -- 3 1s in an array of 8 counters:
counts: . 1 . . 1 . 1 .
sums: 0 1 1 1 2 2 3 3
^ sums[3] < 2 <= sums[4] <=> median 4
addsub( 0, 1 ) m, summ stay the same
addsub( 5, 1 ) slide right
addsub( 5, 6 ) slide left
Updating `counts` in an `addsub` is trivial, updating `sums` is not.
But we can cache the previous median `m` and the sum to m `summ`.
The less often the median changes, the faster;
so fewer levels or *wider* windows are faster.
(Like any cache, run time varies a lot, depending on the input.)
See also:
scipy.signal.medfilt -- runtime roughly ~ window size
http://stackoverflow.com/questions/1309263/rolling-median-algorithm-in-c
"""
from __future__ import division
import numpy as np # bincount, pad0
__date__ = "2009-10-27 oct"
__author_email__ = "denis-bz-py at t-online dot de"
#...............................................................................
class Median1:
""" moving median 1d for quantized, e.g. 8-bit data """
def __init__( s, nlevel, window, counts ):
s.nlevel = nlevel # >= len(counts)
s.window = window # == sum(counts)
s.half = (window // 2) + 1 # odd or even
s.setcounts( counts )
def median( s ):
""" step up or down until sum cnt to m-1 < half <= sum to m """
if s.summ - s.cnt[s.m] < s.half <= s.summ:
return s.m
j, sumj = s.m, s.summ
if sumj <= s.half:
while j < s.nlevel - 1:
j += 1
sumj += s.cnt[j]
# print "j sumj:", j, sumj
if sumj - s.cnt[j] < s.half <= sumj: break
else:
while j > 0:
sumj -= s.cnt[j]
j -= 1
# print "j sumj:", j, sumj
if sumj - s.cnt[j] < s.half <= sumj: break
s.m, s.summ = j, sumj
return s.m
def addsub( s, add, sub ):
s.cnt[add] += 1
s.cnt[sub] -= 1
assert s.cnt[sub] >= 0, (add, sub)
if add <= s.m:
s.summ += 1
if sub <= s.m:
s.summ -= 1
def setcounts( s, counts ):
assert len(counts) <= s.nlevel, (len(counts), s.nlevel)
if len(counts) < s.nlevel:
counts = pad0__( counts, s.nlevel ) # numpy array / list
sumcounts = sum(counts)
assert sumcounts == s.window, (sumcounts, s.window)
s.cnt = counts
s.slowmedian()
def slowmedian( s ):
j, sumj = -1, 0
while sumj < s.half:
j += 1
sumj += s.cnt[j]
s.m, s.summ = j, sumj
def __str__( s ):
return ("median %d: " % s.m) + \
"".join([ (" ." if c == 0 else "%2d" % c) for c in s.cnt ])
#...............................................................................
def medianfilter( x, window, nlevel=256 ):
""" moving medians, y[j] = median( x[j:j+window] )
-> a shorter list, len(y) = len(x) - window + 1
"""
assert len(x) >= window, (len(x), window)
# np.clip( x, 0, nlevel-1, out=x )
# cf http://scipy.org/Cookbook/Rebinning
cnt = np.bincount( x[0:window] )
med = Median1( nlevel=nlevel, window=window, counts=cnt )
y = (len(x) - window + 1) * [0]
y[0] = med.median()
for j in xrange( len(x) - window ):
med.addsub( x[j+window], x[j] )
y[j+1] = med.median()
return y # list
# return np.array( y )
def pad0__( x, tolen ):
""" pad x with 0 s, numpy array or list """
n = tolen - len(x)
if n > 0:
try:
x = np.r_[ x, np.zeros( n, dtype=x[0].dtype )]
except NameError:
x += n * [0]
return x
#...............................................................................
if __name__ == "__main__":
Len = 10000
window = 3
nlevel = 256
period = 100
np.set_printoptions( 2, threshold=100, edgeitems=10 )
# print medians( np.arange(3), 3 )
sinwave = (np.sin( 2 * np.pi * np.arange(Len) / period )
+ 1) * (nlevel-1) / 2
x = np.asarray( sinwave, int )
print "x:", x
for window in ( 3, 31, 63, 127, 255 ):
if window > Len: continue
print "medianfilter: Len=%d window=%d nlevel=%d:" % (Len, window, nlevel)
y = medianfilter( x, window=window, nlevel=nlevel )
print np.array( y )
# end median1.py