added histgrm + small bugfixes

master
per.andreas.brodtkorb 14 years ago
parent 813a7c7b24
commit 0570d84923

@ -14,6 +14,7 @@ from numpy import (abs, amax, any, logical_and, arange, linspace, atleast_1d,
from scipy.special import gammaln from scipy.special import gammaln
import types import types
import warnings import warnings
from wafo import plotbackend
try: try:
import wafo.c_library as clib import wafo.c_library as clib
@ -26,7 +27,7 @@ __all__ = ['JITImport', 'DotDict', 'Bunch', 'printf', 'sub_dict_select',
'parse_kwargs', 'ecross', 'findtc', 'findtp', 'findcross', 'parse_kwargs', 'ecross', 'findtc', 'findtp', 'findcross',
'findextrema', 'findrfc', 'rfcfilter', 'common_shape', 'argsreduce', 'findextrema', 'findrfc', 'rfcfilter', 'common_shape', 'argsreduce',
'stirlerr', 'getshipchar', 'betaloge', 'gravity', 'nextpow2', 'stirlerr', 'getshipchar', 'betaloge', 'gravity', 'nextpow2',
'discretize', 'pol2cart', 'cart2pol', 'ndgrid', 'meshgrid'] 'discretize', 'pol2cart', 'cart2pol', 'ndgrid', 'meshgrid', 'histgrm']
class JITImport(object): class JITImport(object):
''' '''
@ -1849,6 +1850,66 @@ def tranproc(x, f, x0, *xi):
warnings.warn('Transformation of derivatives of order>4 not supported.') warnings.warn('Transformation of derivatives of order>4 not supported.')
return y #y0,y1,y2,y3,y4 return y #y0,y1,y2,y3,y4
def histgrm(data, n=None, odd=False, scale=False, lintype='b-'):
'''HISTGRM Plot histogram
CALL: binwidth = histgrm(x,N,odd,scale)
binwidth = the width of each bin
Parameters
-----------
x = the data
n = approximate number of bins wanted
(default depending on length(x))
odd = placement of bins (0 or 1) (default 0)
scale = argument for scaling (default 0)
scale = 1 yields the area 1 under the histogram
lintype : specify color and lintype, see PLOT for possibilities.
Example:
R=rndgumb(2,2,1,100);
histgrm(R,20,0,1)
hold on
x=linspace(-3,16,200);
plot(x,pdfgumb(x,2,2),'r')
hold off
'''
x = np.atleast_1d(data)
if n is None:
n = np.ceil(4 * np.sqrt(np.sqrt(len(x))))
mn = x.min()
mx = x.max()
d = (mx - mn) / n * 2
e = np.floor(np.log(d) / np.log(10));
m = np.floor(d / 10 ** e)
if m > 5:
m = 5
elif m > 2:
m = 2
d = m * 10 ** e
mn = (np.floor(mn / d) - 1) * d - odd * d / 2
mx = (np.ceil(mx / d) + 1) * d + odd * d / 2
limits = np.arange(mn, mx, d)
bin, limits = np.histogram(data, bins=limits, normed=scale) #, new=True)
limits.shape = (-1, 1)
xx = limits.repeat(3, axis=1)
xx.shape = (-1,)
xx = xx[1:-1]
bin.shape = (-1, 1)
yy = bin.repeat(3, axis=1)
#yy[0,0] = 0.0 # pdf
yy[:, 0] = 0.0 # histogram
yy.shape = (-1,)
yy = np.hstack((yy, 0.0))
plotbackend.plotbackend.plot(xx, yy, lintype, limits, limits * 0)
binwidth = d
return binwidth
def _test_find_cross(): def _test_find_cross():
t = findcross([0, 0, 1, -1, 1], 0) t = findcross([0, 0, 1, -1, 1], 0)

@ -895,7 +895,7 @@ class TimeSeries(WafoData):
""" """
fs = 1. / (2 * self.sampling_period()) fs = 1. / (2 * self.sampling_period())
S, f = psd(self.data.ravel(), Fs=fs, *args, **kwargs) S, f = psd(self.data.ravel(), Fs=fs, *args, **kwargs)
fact = 2.0 * pi fact = 2 * 2.0 * pi
w = fact * f w = fact * f
return _wafospec.SpecData1D(S / fact, w) return _wafospec.SpecData1D(S / fact, w)
def trdata(self, method='nonlinear', **options): def trdata(self, method='nonlinear', **options):

Loading…
Cancel
Save