Reputation: 703
I have a M×N numpy array. Before a FFT I need to pad it with zeros to get a (M+K)×(N+K) array where the M and N first indices are the original data and the K last indices along each dimension are zeros.
Using Cython, is there an efficient way to do so without loosing the np.ndarray[DTYPE_t, ndim=2]
type ?
Upvotes: 0
Views: 409
Reputation: 231395
One way of padding an array is to create a target of the right size, and copy values:
In [137]: M,N,K = 10,20,5
In [138]: source = np.arange(M*N).reshape(M,N)
In [139]: target = np.zeros((M+K, N+K), dtype=source.dtype)
In [140]: target[:M, :N] = source
You can't get any more memory efficient than that.
I don't see how cython
will help with memory use, since memory use is determined by the size of source and target arrays, not the coping method.
I'm not even sure it will be faster, especially if you want target
to be a numpy array that can be used in interpreted Python. If both source and target are typed memoryview, the copy might be faster, but we'd have to test that.
Upvotes: 1
Reputation: 114
The following will turn your array into a padded version without changing its type:
array = np.hstack((array,np.zeros((np.size(array,0),K))))
array = np.vstack((array,np.zeros((K,np.size(array,1)))))
Upvotes: 0