Skip to content

Commit eafbf10

Browse files
committed
SciPy's next_fast_len is public now; use that.
1 parent 408c0f9 commit eafbf10

File tree

1 file changed

+2
-53
lines changed

1 file changed

+2
-53
lines changed

scopeplot.py

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from numpy import asarray, linspace, arange, pad
1111
import matplotlib.pyplot as plt
1212
from scipy.signal import resample
13+
from scipy.fftpack import next_fast_len
1314

1415
# minimum resampling factor
1516
RS = 2
@@ -20,58 +21,6 @@ def _ceildiv(a, b):
2021
return -(-a // b)
2122

2223

23-
def _next_regular(target):
24-
"""
25-
Find the next regular number greater than or equal to target.
26-
Regular numbers are composites of the prime factors 2, 3, and 5.
27-
Also known as 5-smooth numbers or Hamming numbers, these are the optimal
28-
size for inputs to FFTPACK.
29-
30-
Target must be a positive integer.
31-
"""
32-
assert abs(int(target)) == target
33-
34-
if target <= 6:
35-
return target
36-
37-
# Quickly check if it's already a power of 2
38-
if not (target & (target-1)):
39-
return target
40-
41-
match = float('inf') # Anything found will be smaller
42-
p5 = 1
43-
while p5 < target:
44-
p35 = p5
45-
while p35 < target:
46-
# Ceiling integer division, avoiding conversion to float
47-
# (quotient = ceil(target / p35))
48-
quotient = -(-target // p35)
49-
50-
# Quickly find next power of 2 >= quotient
51-
try:
52-
p2 = 2**((quotient - 1).bit_length())
53-
except AttributeError:
54-
# Fallback for Python <2.7
55-
p2 = 2**(len(bin(quotient - 1)) - 2)
56-
57-
N = p2 * p35
58-
if N == target:
59-
return N
60-
elif N < match:
61-
match = N
62-
p35 *= 3
63-
if p35 == target:
64-
return p35
65-
if p35 < match:
66-
match = p35
67-
p5 *= 5
68-
if p5 == target:
69-
return p5
70-
if p5 < match:
71-
match = p5
72-
return match
73-
74-
7524
def _get_weights(N):
7625
"""
7726
Sample weights for a chunk with N samples per chunk. Determined by the
@@ -197,7 +146,7 @@ def scopeplot(x, width=800, height=400, range_=None, cmap=None, plot=None):
197146
MIN_PAD = 5 # TODO: what should this be? Seems subjective.
198147

199148
# Make input an optimal length for fast processing
200-
pad_amount = _next_regular(N + MIN_PAD) - N
149+
pad_amount = next_fast_len(N + MIN_PAD) - N
201150

202151
x = pad(x, (0, pad_amount), 'constant')
203152

0 commit comments

Comments
 (0)