Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy interpolation to increase array size

this question is related with my previous question How to use numpy interpolation to increase a vector size, but this time I'm looking for a method to do increase the 2D array size and not a vector.

The idea is that I have couples of coordinates (x;y) and I want to smooth the line with a desired number of (x;y) pairs

for a Vector solution I use the answer of @AGML user with very good results

from scipy.interpolate import UnivariateSpline

def enlargeVector(vector, size):
    old_indices = np.arange(0,len(a))
    new_length = 11
    new_indices = np.linspace(0,len(a)-1,new_length)
    spl = UnivariateSpline(old_indices,a,k=3,s=0)
    return spl(new_indices)
like image 712
efirvida Avatar asked Dec 06 '25 02:12

efirvida


1 Answers

You can use the function map_coordinates from the scipy.ndimage.interpolation module.

import numpy as np
from scipy.ndimage.interpolation import map_coordinates

A = np.random.random((10,10))
new_dims = []
for original_length, new_length in zip(A.shape, (100,100)):
    new_dims.append(np.linspace(0, original_length-1, new_length))

coords = np.meshgrid(*new_dims, indexing='ij')
B = map_coordinates(A, coords)
like image 107
aganders3 Avatar answered Dec 08 '25 14:12

aganders3