greenthumbtack greenthumbtack - 5 years ago 374
Python Question

Zero-padding data until its length is equal to a power of 2

I'm trying to write a function that can zero-pad some data until its length is equal to the closest higher power of 2. I also want to be able to do a certain number of iterations of this. Right now this is my function:

def pad_to_next_2n(array, iterations = 1):
n = 1
while n <= iterations:
l = len(array)
padding = l +1 #incase array is already length equal to 2^n
while np.log2(padding) % 1 != 0:
padding += 1
if l % 2 == 0:
total_padding = padding - l
array = np.pad(array, (total_padding/2,), 'constant')
n += 1
else:
total_padding = padding - l
left_padding = (total_padding - 1)/2
right_padding = total_padding - left_padding
print total_padding
print left_padding
print right_padding
array = np.pad(array, (left_padding, right_padding), 'constant')
n += 1
return array


This does work but it's really slow for iterations higher than 5. I was wondering if anyone could help to improve the speed of this or see a better way to do it. I believe the biggest problem is coming from the

while np.log2(padding) % 1 != 0:
padding += 1


Part but I'm not sure how to make that more efficient.

Answer Source

You don't have to do a lot of what you are doing - you just want to find out the next power of 2 that you need to extend the length of your array to, and then you can call pad on the array to pad it to the length you need in one go.

This uses shift_bit_length from another question about fastest way to get next power of 2 in Python.

import numpy as np

def shift_bit_length(x):
    return 1<<(x-1).bit_length()

def padpad(data, iterations = 1):
    narray = data
    for i in xrange(iterations):
        length = len(narray)
        diff = shift_bit_length(length + 1) - length
        if length % 2 == 0:
            pad_width = diff / 2
        else:
            # need an uneven padding for odd-number lengths
            left_pad = diff / 2
            right_pad = diff - left_pad
            pad_width = (left_pad, right_pad)
        narray = np.pad(narray, pad_width, 'constant')
    return narray

Some tests:

>> arr = np.array([1, 2])

>> padpad(arr, 1)
Out[2]: array([0, 1, 2, 0])

>> len(padpad(arr, 1)
Out[3]: 4

>> padpad(arr, 5)
Out[4]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0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