runningSum.h 704 B

1234567891011121314151617181920212223242526272829303132
  1. void sumFilter(double *inpt,double *outp,int N,int b,int a,int nrm){
  2. //J contains from -b to a-1
  3. int Je=0,k=a+b+1;
  4. double *J=(double*)R_alloc(sizeof(double),k),cumsum=0.;
  5. //J contains values from
  6. //Put befores
  7. for(int e=-b-1;e<N+a;e++){
  8. if(e>=0 && e<N) cumsum+=inpt[e];
  9. if(e>=a){
  10. if(!nrm)
  11. outp[e-a]=cumsum-J[Je];
  12. else
  13. outp[e-a]=(cumsum-J[Je])/((double)k); //TODO: Fix this
  14. }
  15. J[Je]=cumsum; //Store cumsum in head
  16. Je=(Je+1)%k;
  17. }
  18. }
  19. SEXP C_runningSum(SEXP X,SEXP B,SEXP A){
  20. int N=length(X);
  21. int a=INTEGER(A)[0];
  22. int b=INTEGER(B)[0];
  23. SEXP Ans=PROTECT(allocVector(REALSXP,N));
  24. double *x=REAL(X),*ans=REAL(Ans);
  25. sumFilter(x,ans,N,b,a,3);
  26. UNPROTECT(1);
  27. return(Ans);
  28. }