import numpy def morph_array_to_size(from_array, to_size): from_size = len(from_array) to_array = numpy.zeros((to_size), dtype=float) if from_size >= to_size: # for f in range(from_size): # to_array[f] = from_array[f] for f in range(from_size): starting_to_element = int(to_size / from_size * f) ending_to_element = int(to_size / from_size * (f + 1)) if starting_to_element == ending_to_element or \ ending_to_element - to_size / from_size * (f + 1) == 0: to_array[starting_to_element] += from_array[f] else: amt1_pct = from_size / to_size * f - int(from_size / to_size * f) amt2_pct = 1 - amt1_pct to_array[starting_to_element] += from_array[f] * amt1_pct to_array[ending_to_element] += from_array[f] * amt2_pct else: for t in range(to_size): starting_from_element = int(from_size / to_size * t) ending_from_element = int(from_size / to_size * (t + 1)) if starting_from_element == ending_from_element or \ ending_from_element - from_size / to_size * (t + 1) == 0: to_array[t] += from_array[starting_from_element] * from_size / to_size else: amt1_pct = int(from_size / to_size * t) + 1 - from_size / to_size * t amt2_pct = from_size / to_size * (t + 1) - int(from_size / to_size * (t + 1)) to_array[t] += from_array[starting_from_element] * amt1_pct to_array[t] += from_array[ending_from_element] * amt2_pct return to_array