Numpy has a padding function with a pad_width
parameter that does the following:
pad_width
: Number of values padded to the edges of each axis. ((before_1, after_1), ... (before_N, after_N))
unique pad widths for each axis. (before, after)
or ((before, after),)
yields same before and after pad for each axis. (pad,)
or int
is a shortcut for before = after = pad width for all axes.
Is there an equivalent function in Julia with similar functionality for zero-padding? Creating a 2D matrix with complex data and zero-padding in Python:
# Python Implementation
import numpy as np
data = np.random.random((620, 327)) + np.random.random((620, 327)) * 1j
padWidths = ((12,12),(327,327))
# Returns an array of size (644, 981) as the pad_widths parameter specified
# zero-padding of length 644 = 620 + 12 + 12 and 981 = 327 + 327 + 327
zeroPaddedData = np.pad(data, padWidths)
Performing a similar analysis with a 2D complex array in Julia:
# Julia Implementation
using Random
using PaddedViews
using ImageFiltering
data = randn(ComplexF32, (620, 327))
padWidth = ((12,12),(327,327))
# This returns an array of size (620,327)
zeroPaddedDataOne= PaddedView(0, data,(620,327))
# This returns an array of size (620,981)
zeroPaddedDataTwo = padarray(data, Fill(0,(0,327)))
# This returns an array of size (644,327)
zeroPaddedDataThree= padarray(data, Fill(0,(12,0)))
# This returns an array of size (644,981)
zeroPaddedDataFour = padarray(data, Fill(0,(12,327)))
# This doesn't work as you can't pass in a tuple of tuples into an array with 2-dimensions
zeroPaddedDataFive = padarray(data, Fill(0,padWidth))
zeroPaddedDataSix = PaddedView(0, data,padWidth)
It appears that one solution is to use
zeroPaddedData = padarray(data, Fill(0,(12,327)))
to match the functionality of pad_width
in Numpy (which, instead of passing in a tuple of tuples, is a single tuple containing the amount of padding to perform along each dimension of the array). Is this the recommended approach to match the pad_width
parameter in Numpy?