Oh darn, I missed the recursive-ness entirely. You condition on the filtered
series, not the signal itself.

In that case, I have a solution which is pretty fast, but not particularly
R-esque.

In effect your filter just says, take x but if you see a 1, sit out for the
next wait periods. This seems prone to a repeat or while loop, and I don't
think can be much improved since you'll have to run the signal "in real
time" unless I'm missing a trick

res = numeric(length(x))
i=1
while (i <= length(x) ) {
   if (x[i] == 1) {res[i] =1; i = i+wait} # this improves speed somewhat by
jumping over those spots you are going to keep = 0
   i = i + 1 # no need to re-assign the default value of zero
}

Again, unless there's a trick I'm missing, this seems about optimal since it
runs slightly better than "real-time" through the signal.

Sorry for my initial (wrong) remarks,

Michael Weylandt

On Fri, Jul 29, 2011 at 11:42 AM, R. Michael Weylandt <
michael.weyla...@gmail.com> <michael.weyla...@gmail.com> wrote:

> I'm not sure I understand what your filter intends to do, but could this
> not be done more efficiently with logicals and the which? You also might
> need the cumsum() function and diff() with the optional lag argument  if
> I've misunderstood your filter.
>
> Specifically try this:
>
> res = c(x,rep(NA,wait)) # make a copy to work on and include the extra NA
> which we might turn into zeros, but will drop later
> for (i in 1:wait) {res[which(x == 1) + i] <- 0}
> res = res[1:length(x)] # drop the extra added length.
>
> Michael Weylandt
>
>
>
> On Fri, Jul 29, 2011 at 11:16 AM, Konrad Schubert <infoc...@gmx.net>wrote:
>
>> Hi,
>> I have a question about a special recursive filter problem.
>>
>> What I have:
>>
>> - given variables:
>>  x: time series with rather randomly occuring '0' and '1'
>>  wait: non negative integer
>>
>> - a working but ineffectiv implementation (see below)
>>
>> How the implementation works (what I want):
>>
>> The filter should drill holes of distance 'wait' between the '1' in x,
>> e.g.
>>
>> x = 1 0 1 1 0 1 0 1 0 1 0 1 1 1 1
>> wait = 2
>>
>> desired result:
>>
>> result = 1 0 0 1 0 0 0 1 0 0 0 1 0 0 1
>>
>> working implementation:
>>
>>
>>  #*************************************************************************
>>  # basic informations
>>
>>  #*************************************************************************
>>
>>  # length of input vector
>>  lengthX <- length(x)
>>
>>  # stop times for the recursive filter indices
>>  stopS <- 1:lengthX + wait - 1
>>
>>  # initialize the result and the intermediate result vector
>>  # with additional length for recursive filtering
>>  result <- y <- numeric(lengthX + wait)
>>
>>
>>  #*************************************************************************
>>  # filter
>>
>>  #*************************************************************************
>>
>>  # recursive filter function
>>  for(i in 1:lengthX){
>>
>>    # present ('x') and lag ('y') filtering
>>    ans <- x[i] + sum(y[i:stopS[i]])
>>
>>    # check for the right filter answer
>>    if( ans == 1){
>>      y[wait + i] <- -1
>>      result[wait + i] <- 1
>>      }
>>  }
>>
>>
>>  #*************************************************************************
>>  # post calculation
>>
>>  #*************************************************************************
>>
>>  # remove the additional length for recursive filtering
>>  # from the returning vector
>>  result <- result[-(1:wait)]
>>
>> -----------------------------------------------------------------------
>>
>> Is there anyone how has a better idea?
>> Thank you for your help,
>> Thomas.
>> --
>>
>> ______________________________________________
>> R-help@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.
>>
>
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to