I am looking for some help at removing low-frequency components from a signal, through Moving Average on a sliding window. I understand thiis is a smoothing procedure that I never done in my life before .. sigh. I searched R archives and found "rollmean", "MovingAverages {TTR}", "SymmetricMA". None of the above mantioned functions seems to accept the smoothing polynomial order and the sliding window with as input parameters. Maybe I am missing something. I wonder whether there is some building blocks in R if not even a function which does it all (I do not expect that much,though). Even some literature references and/or tutorials are very welcome. Thank you so much, Maura tutti i telefonini TIM! [[alternative HTML version deleted]]
See rollapply in zoo or filter or embed in the core of R. On Thu, Feb 26, 2009 at 7:07 AM, <mauede at alice.it> wrote:> I am looking for some help at removing low-frequency components from a signal, through Moving Average on a sliding window. > I understand thiis is a smoothing procedure that I never done in my life before .. sigh. > > I searched R archives and found "rollmean", "MovingAverages {TTR}", "SymmetricMA". > None of the above mantioned functions seems to accept the smoothing polynomial order and the sliding window with as input parameters. Maybe I am missing something. > > I wonder whether there is some building blocks in R if not even a function which does it all (I do not expect that much,though). > Even some literature references and/or tutorials are very welcome. > > Thank you so much, > Maura > > > > tutti i telefonini TIM! > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
I saw Gabor's reply but have a clarification to request. You say you want to remove low frequency components but then you request smoothing functions. The term "smoothing" implies removal of high-frequency components of a series. If smoothing really is your goal then additional R resource would be smooth.spline, loess (or lowess), ksmooth, or using smoothing terms in regressions. Venables and Ripley have quite a few worked examples of such in MASS. -- David Winsemius On Feb 26, 2009, at 7:07 AM, <mauede at alice.it> wrote:> I am looking for some help at removing low-frequency components from > a signal, through Moving Average on a sliding window. > I understand thiis is a smoothing procedure that I never done in my > life before .. sigh. > > I searched R archives and found "rollmean", "MovingAverages {TTR}", > "SymmetricMA". > None of the above mantioned functions seems to accept the smoothing > polynomial order and the sliding window with as input parameters. > Maybe I am missing something. > > I wonder whether there is some building blocks in R if not even a > function which does it all (I do not expect that much,though). > Even some literature references and/or tutorials are very welcome. > > Thank you so much, > Maura > > > > tutti i telefonini TIM! > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Along similar lines, I wrote a toy script to apply any function you want in a windowed sense. Be warned that it's about 10000 times slower than loess(). # my own boxcar tool, just because. # use bfunc to specify what function to apply to the windowed # region. # basically must be valid function name and must accept # single value or vector of values # "pad" generates partial-width values at ends of x so output is # same length as input. # and make it optional for picky people boxcar<-function(x, width=5, bfunc='mean', pad=TRUE){ goodfunc<-try(bfunc<-get(bfunc),silent=TRUE) if (inherits (goodfunc,"try-error")) { stop ('"', bfunc,'"', ' is not a known function', call.=FALSE) } # paranoid, force width to be integer width<-floor(width) # fix width given definition of window() inputs width<-max(0,(width-1)) if (width%%2 == 1) cat('Warning: window is even length, hence asymmetric\n') #adjust start, end points to half-window width; keep output length right seqstart<- 1 -pad*((width+1)%/%2) seqend<- length(x)- width + pad*(width%/%2) boxout<-mapply(function(shiftx) { bfunc(window(x,max(shiftx,1), min(shiftx+width,length(x)) ) ) } ,seq(seqstart, seqend) ) return(invisible(boxout)) }