Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
stabilizeCounts/stabilizecounts.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
36 lines (32 sloc)
916 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Rcpp.h> | |
//compute variance with bessel correction for a single bin | |
inline double binValue(int* counts, int len){ | |
long tot = 0; | |
long tot2 = 0; | |
double denom = len; | |
for (int i = 0; i < len; ++i){ | |
int c = counts[i]; | |
tot += c; | |
tot2 += c*c; | |
} | |
double mean = tot/denom; | |
double var = (tot2 - mean*mean*denom)/(denom-1); | |
return mean/sqrt(var); | |
} | |
// [[Rcpp::export]] | |
Rcpp::NumericVector stabilizeBins(Rcpp::IntegerVector sig, int binsize){ | |
int* C = sig.begin(); | |
int siglen = sig.length(); | |
int nbins = siglen / binsize; | |
bool incompleteBin = siglen % binsize != 0; | |
Rcpp::NumericVector binnedSig(nbins + (incompleteBin?1:0)); | |
//loop on the bins | |
int bin = 0; | |
for (; bin < nbins; ++bin){ | |
binnedSig[bin] = binValue(C + bin*binsize, binsize); | |
} | |
if (incompleteBin) { | |
binnedSig[nbins] = binValue(C + nbins*binsize, siglen - nbins*binsize); | |
} | |
return binnedSig; | |
} |