Downsample a 1D numpy array Downsample a 1D numpy array numpy numpy

Downsample a 1D numpy array


In the simple case where your array's size is divisible by the downsampling factor (R), you can reshape your array, and take the mean along the new axis:

import numpy as npa = np.array([1.,2,6,2,1,7])R = 3a.reshape(-1, R)=> array([[ 1.,  2.,  6.],         [ 2.,  1.,  7.]])a.reshape(-1, R).mean(axis=1)=> array([ 3.        ,  3.33333333])

In the general case, you can pad your array with NaNs to a size divisible by R, and take the mean using scipy.nanmean.

import math, scipyb = np.append(a, [ 4 ])b.shape=> (7,)pad_size = math.ceil(float(b.size)/R)*R - b.sizeb_padded = np.append(b, np.zeros(pad_size)*np.NaN)b_padded.shape=> (9,)scipy.nanmean(b_padded.reshape(-1,R), axis=1)=> array([ 3.        ,  3.33333333,  4.])


Here are a few approaches using either linear interpolation or the Fourier method. These methods support upsampling as well as downsampling.

import numpy as npimport matplotlib.pyplot as pltfrom scipy.signal import resamplefrom scipy.interpolate import interp1ddef ResampleLinear1D(original, targetLen):    original = np.array(original, dtype=np.float)    index_arr = np.linspace(0, len(original)-1, num=targetLen, dtype=np.float)    index_floor = np.array(index_arr, dtype=np.int) #Round down    index_ceil = index_floor + 1    index_rem = index_arr - index_floor #Remain    val1 = original[index_floor]    val2 = original[index_ceil % len(original)]    interp = val1 * (1.0-index_rem) + val2 * index_rem    assert(len(interp) == targetLen)    return interpif __name__=="__main__":    original = np.sin(np.arange(256)/10.0)    targetLen = 100    # Method 1: Use scipy interp1d (linear interpolation)    # This is the simplest conceptually as it just uses linear interpolation. Scipy    # also offers a range of other interpolation methods.    f = interp1d(np.arange(256), original, 'linear')    plt.plot(np.apply_along_axis(f, 0, np.linspace(0, 255, num=targetLen)))    # Method 2: Use numpy to do linear interpolation    # If you don't have scipy, you can do it in numpy with the above function    plt.plot(ResampleLinear1D(original, targetLen))    # Method 3: Use scipy's resample    # Converts the signal to frequency space (Fourier method), then back. This    # works efficiently on periodic functions but poorly on non-periodic functions.    plt.plot(resample(original, targetLen))    plt.show()


If array size is not divisible by downsampling factor (R), reshaping (splitting) of array can be done using np.linspace followed by mean of each subarray.

input_arr = np.arange(531)R = 150 (number of split)split_arr = np.linspace(0, len(input_arr), num=R+1, dtype=int)dwnsmpl_subarr = np.split(input_arr, split_arr[1:])dwnsmpl_arr = np.array( list( np.mean(item) for item in dwnsmpl_subarr[:-1] ) )