MATLAB's smooth implementation (n-point moving average) in NumPy/Python -


matlab's smooth function, default, smooths data using 5-point moving average. best way same in python? example, if data

0 0.823529411764706 0.852941176470588 0.705882352941177 0.705882352941177 0.676470588235294 0.676470588235294 0.500000000000000 0.558823529411765 0.647058823529412 0.705882352941177 0.705882352941177 0.617647058823529 0.705882352941177 0.735294117647059 0.735294117647059 0.588235294117647 0.588235294117647 1 0.647058823529412 0.705882352941177 0.764705882352941 0.823529411764706 0.647058823529412 0.735294117647059 0.794117647058824 0.794117647058824 0.705882352941177 0.676470588235294 0.794117647058824 0.852941176470588 0.735294117647059 0.647058823529412 0.647058823529412 0.676470588235294 0.676470588235294 0.529411764705882 0.676470588235294 0.794117647058824 0.882352941176471 0.735294117647059 0.852941176470588 0.823529411764706 0.764705882352941 0.558823529411765 0.588235294117647 0.617647058823529 0.647058823529412 0.588235294117647 0.617647058823529 0.647058823529412 0.794117647058824 0.823529411764706 0.647058823529412 0.617647058823529 0.647058823529412 0.676470588235294 0.764705882352941 0.676470588235294 0.647058823529412 0.705882352941177 0.764705882352941 0.705882352941177 0.500000000000000 0.529411764705882 0.529411764705882 0.647058823529412 0.676470588235294 0.588235294117647 0.735294117647059 0.794117647058824 0.852941176470588 0.764705882352941 

the smoothed data should be

0 0.558823529411765 0.617647058823530 0.752941176470588 0.723529411764706 0.652941176470588 0.623529411764706 0.611764705882353 0.617647058823530 0.623529411764706 0.647058823529412 0.676470588235294 0.694117647058824 0.700000000000000 0.676470588235294 0.670588235294118 0.729411764705882 0.711764705882353 0.705882352941177 0.741176470588235 0.788235294117647 0.717647058823529 0.735294117647059 0.752941176470588 0.758823529411765 0.735294117647059 0.741176470588235 0.752941176470588 0.764705882352941 0.752941176470588 0.741176470588235 0.735294117647059 0.711764705882353 0.676470588235294 0.635294117647059 0.641176470588236 0.670588235294118 0.711764705882353 0.723529411764706 0.788235294117647 0.817647058823530 0.811764705882353 0.747058823529412 0.717647058823530 0.670588235294118 0.635294117647059 0.600000000000000 0.611764705882353 0.623529411764706 0.658823529411765 0.694117647058824 0.705882352941176 0.705882352941176 0.705882352941176 0.682352941176471 0.670588235294118 0.676470588235294 0.682352941176471 0.694117647058824 0.711764705882353 0.700000000000000 0.664705882352941 0.641176470588236 0.605882352941177 0.582352941176471 0.576470588235294 0.594117647058824 0.635294117647059 0.688235294117647 0.729411764705882 0.747058823529412 0.803921568627451 0.764705882352941 

the syntax in matlab

smooth(data) 

i want same in python unable find function this.

matlab's smoooth func same averaging across sliding windows of length 5, except way treats 2 elems @ either ends. per linked docs, boundary cases computed these formulae -

yy = smooth(y) smooths data in column vector y .. first few elements of yy given  yy(1) = y(1) yy(2) = (y(1) + y(2) + y(3))/3 yy(3) = (y(1) + y(2) + y(3) + y(4) + y(5))/5 yy(4) = (y(2) + y(3) + y(4) + y(5) + y(6))/5 ... 

so, replicate same implementation on numpy/python, can use numpy's 1d convolution getting sliding windowed summations , divide them window length give average results. then, append special case treated values boundary elems.

thus, have implementation handle generic window sizes, -

def smooth(a,wsz):     out0 = np.convolve(a,np.ones(wsz,dtype=int),'valid')/wsz         r = np.arange(1,wsz-1,2)     start = np.cumsum(a[:wsz-1])[::2]/r     stop = (np.cumsum(a[:-wsz:-1])[::2]/r)[::-1]     return np.concatenate((  start , out0, stop  )) 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -