As mentioned in Zero pad numpy array, this function zero pads an array:
A = np.array([1,2,3,4,5])
B = np.pad(A, (0, 3), 'constant') # array([1, 2, 3, 4, 5, 0, 0, 0])
Question: Is there a helper function in numpy
or scipy
that automatically zero pads an array to the next power-of-2 size? (Useful for FFT purposes, etc.)
Of course I can do something like
def padpower2(A):
return np.pad(A, (0, int(2**np.ceil(np.log2(len(A)))) - len(A)), 'constant')
but it's alway tricky to remember/rewrite, so having a ready-to-use numpy
function would be better.