def simpleRMS(X, scales, m=1, verbose=False): """Compute RMS of detrended signal in non-overlapping windows. RMS is computed for each scale from scales array. RMS is evaluated for each scale (N elements) and in all possible non-overlapping windows of N elements. This function returns a list of arrays, one array for each scale and of a different length. Parameters ---------- X: array, time-series scales: array, scales (number of elements in a window) for RMS computation m: order of polynomial for polyfit Examples -------- >>> X = cumsum(0.1*randn(8000)) >>> scales = (2**arange(4,10)).astype('i4') >>> RMS = simpleRMS(X,scales) >>> for i,x in enumerate(RMS): subplot(len(scales),1,len(scales)-i) t = arange(x.shape[0])*scales[i] + 0.5*scales[i] step(r_[t,t[-1]],r_[x,x[-1]]) plot(xlim(),x.mean()*r_[1,1],'r',lw=2.0) """ from numpy.polynomial.polynomial import polyval as mpolyval, polyfit as mpolyfit out = [] for scale in scales: Y = rw(X, scale, scale) i = arange(scale) C = mpolyfit(i, Y.T, m) out.append(sqrt(((Y - mpolyval(i, C))**2).mean(1))) return out
def MRMS(X, scale, step, m=1, verbose=False): """Compute RMS of detrended signal in sliding windows for a signle window size. RMS is evaluated in all possible windows of 'scale' elements. Parameters ---------- X: array, time-series scale: scale (number of elements in a window) for RMS computation step: step for sliding window (number of elements between adjacent windows) m: order of polynomial for polyfit Examples -------- # >>> X = cumsum(0.1*randn(8000)) # >>> step = 32 # >>> RMS = MRMS(X,256,step) # >>> plot(arange(RMS.shape[0])*step,RMS) """ from numpy.polynomial.polynomial import polyval as mpolyval, polyfit as mpolyfit i0 = scale // 2 / step + 1 Y = rw(X[step - (scale // 2) % step:], scale, step) i = arange(scale) C = mpolyfit(i, Y.T, m) rms = sqrt(((Y - mpolyval(i, C))**2).mean(1)) return rms
def MRMS(X,scale,step,m=1,verbose=False): """Compute RMS of detrended signal in sliding windows for a signle window size. RMS is evaluated in all possible windows of 'scale' elements. Parameters ---------- X: array, time-series scale: scale (number of elements in a window) for RMS computation step: step for sliding window (number of elements between adjacent windows) m: order of polynomial for polyfit Examples -------- >>> X = cumsum(0.1*randn(8000)) >>> step = 32 >>> RMS = MRMS(X,256,step) >>> plot(arange(RMS.shape[0])*step,RMS) """ from numpy.polynomial.polynomial import polyval as mpolyval, polyfit as mpolyfit i0 = scale//2/step+1 Y = rw(X[step-(scale//2)%step:],scale,step) i = np.arange(scale) C = mpolyfit(i,Y.T,m) rms = np.sqrt(((Y-mpolyval(i,C))**2).mean(1)) return rms
def simpleRMS(X,scales,m=1,verbose=False): """Compute RMS of detrended signal in non-overlapping windows. RMS is computed for each scale from scales array. RMS is evaluated for each scale (N elements) and in all possible non-overlapping windows of N elements. This function returns a list of arrays, one array for each scale and of a different length. Parameters ---------- X: array, time-series scales: array, scales (number of elements in a window) for RMS computation m: order of polynomial for polyfit Examples -------- >>> X = cumsum(0.1*randn(8000)) >>> scales = (2**arange(4,10)).astype('i4') >>> RMS = simpleRMS(X,scales) >>> for i,x in enumerate(RMS): subplot(len(scales),1,len(scales)-i) t = arange(x.shape[0])*scales[i] + 0.5*scales[i] step(r_[t,t[-1]],r_[x,x[-1]]) plot(xlim(),x.mean()*r_[1,1],'r',lw=2.0) """ from numpy.polynomial.polynomial import polyval as mpolyval, polyfit as mpolyfit out = [] for scale in scales: Y = rw(X,scale,scale) i = np.arange(scale) C = mpolyfit(i,Y.T,m) out.append( np.sqrt(((Y-mpolyval(i,C))**2).mean(1)) ) return out
def simpleRMS(X,scales,m=1,verbose=False): from numpy.polynomial.polynomial import polyval as mpolyval, polyfit as mpolyfit out = [] for scale in scales: Y = rw(X,scale,scale) i = arange(scale) C = mpolyfit(i,Y.T,1) out.append( sqrt(((Y-mpolyval(i,C))**2).mean(1)) ) return out
def fastRMS(X,scales,m=1,verbose=False): from numpy.polynomial.polynomial import polyval as mpolyval, polyfit as mpolyfit step = scales[0] i0s = arange(0,X.shape[0],step) out = nan+zeros((len(scales),i0s.shape[0]),'f8') j = 0 for scale in scales: if verbose: print '.',scale,step s2 = scale//2 Y = rw(X,scale,step) i = arange(scale) C = mpolyfit(i,Y.T,1) rms = sqrt(((Y-mpolyval(i,C))**2).mean(1)) i0 = around(scale/2.0/step) out[j,i0:i0+rms.shape[0]] = rms j += 1 return out
def fastRMS(X, scales, m=1, verbose=False): """Compute RMS of detrended signal in sliding windows. RMS is computed for each scale from scales array. RMS is evaluated for each scale (N elements) and in all possible windows of N elements. RMS in windows which do not fully overlap with signal (edges) are not computed, result for them is set to nan. The step for sliding of windows is set to scales[0]. This is a fast vectorized version of compRMS function. Parameters ---------- X: array, time-series scales: array, scales (number of elements in a window) for RMS computation m: order of polynomial for polyfit Examples # -------- # >>> X = cumsum(0.1*randn(8000)) # >>> scales = (2**arange(4,10)).astype('i4') # >>> RMS = fastRMS(X,scales) # >>> subplot(311) # >>> plot(arange(0,X.shape[0],scales[0]),RMS.T/scales + 0.01*arange(len(scales))) # >>> subplot(312) # >>> mRMS = ma.array(RMS,mask=isnan(RMS)) # >>> loglog(scales,mRMS.mean(1),'o-',ms=5.0,lw=0.5) # >>> subplot(313) # >>> for q in [-3,-1,1,3]: # loglog(scales,((mRMS**q).mean(1))**(1.0/q),'o-',ms=5.0,lw=0.5) """ step = scales[0] out = nan + zeros((len(scales), X.shape[0] // step), 'f8') j = 0 for scale in scales: if verbose: print '.', scale, step i0 = scale // 2 / step + 1 Y = rw(X[step - (scale // 2) % step:], scale, step) i = arange(scale) C = mpolyfit(i, Y.T, m) rms = sqrt(((Y - mpolyval(i, C))**2).mean(1)) out[j, i0:i0 + rms.shape[0]] = rms j += 1 return out
def fastRMS(X,scales,m=1,verbose=False): """Compute RMS of detrended signal in sliding windows. RMS is computed for each scale from scales array. RMS is evaluated for each scale (N elements) and in all possible windows of N elements. RMS in windows which do not fully overlap with signal (edges) are not computed, result for them is set to nan. The step for sliding of windows is set to scales[0]. This is a fast vectorized version of compRMS function. Parameters ---------- X: array, time-series scales: array, scales (number of elements in a window) for RMS computation m: order of polynomial for polyfit Examples -------- >>> X = cumsum(0.1*randn(8000)) >>> scales = (2**arange(4,10)).astype('i4') >>> RMS = fastRMS(X,scales) >>> subplot(311) >>> plot(arange(0,X.shape[0],scales[0]),RMS.T/scales + 0.01*arange(len(scales))) >>> subplot(312) >>> mRMS = ma.array(RMS,mask=isnan(RMS)) >>> loglog(scales,mRMS.mean(1),'o-',ms=5.0,lw=0.5) >>> subplot(313) >>> for q in [-3,-1,1,3]: loglog(scales,((mRMS**q).mean(1))**(1.0/q),'o-',ms=5.0,lw=0.5) """ from numpy.polynomial.polynomial import polyval as mpolyval, polyfit as mpolyfit step = scales[0] out = np.nan+np.zeros((len(scales),X.shape[0]//step),'f8') j = 0 for scale in scales: if verbose: print '.',scale,step i0 = scale//2/step+1 Y = rw(X[step-(scale//2)%step:],scale,step) i = np.arange(scale) C = mpolyfit(i,Y.T,m) rms = np.sqrt(((Y-mpolyval(i,C))**2).mean(1)) out[j,i0:i0+rms.shape[0]] = rms j += 1 return out