]> git.parisson.com Git - timeside.git/commitdiff
feat(analyzer/utils.py): add a function to evaluate the next power of two
authorThomas Fillon <thomas@parisson.com>
Thu, 19 Jun 2014 13:51:25 +0000 (15:51 +0200)
committerThomas Fillon <thomas@parisson.com>
Thu, 19 Jun 2014 13:51:25 +0000 (15:51 +0200)
timeside/analyzer/utils.py

index 497126d8084e855e80e1a1258ea0d51e4ce37d50..b8ad23c086f93ddce1d50e00645cabf657a77f1d 100644 (file)
@@ -223,3 +223,15 @@ def entropy(serie, nbins=10, base=np.exp(1), approach='unbiased'):
         nbias = nbias / np.log(base)
         sigma = sigma / np.log(base)
         return estimate
+
+
+def nextpow2(value):
+    """Compute the nearest power of two greater or equal to the input value"""
+    if value >= 1:
+        return 2**np.ceil(np.log2(value)).astype(int)
+    elif value > 0:
+        return 1
+    elif value == 0:
+        return 0
+    else:
+        raise ValueError('Value must be positive')