def plot(self, logarithmic=False):
"""Plot a graphical representation of the peaks
Arguments:
(none)
"""
import pylab as pl
pl.figure()
pl.plot(self.x)
pl.hold('on')
pl.plot(self.pos[self.keep], self.val[self.keep], 'og')
pl.plot(self.pos[np.logical_not(self.keep)],
self.val[np.logical_not(self.keep)], 'om')
if hasattr(self, 'bounds'):
lmins = np.unique(self.bounds.flatten())
lminvals = self.x[lmins]
pl.plot(lmins, lminvals, 'or')
if hasattr(self, 'fpos'):
pl.plot(self.fpos[self.keep], self.fval[self.keep], 'dg')
pl.hold('off')
if logarithmic:
pl.gca().set_yscale('log')
python类hold()的实例源码
def two_plot_time_freq_mag(self, minlen=10):
part = [pp for pp in self.partial if len(pp.f) > minlen]
pl.figure()
ax1 = pl.subplot(211)
pl.hold(True)
ax2 = pl.subplot(212, sharex=ax1)
pl.hold(True)
for pp in part:
ax1.plot(pp.start_idx + np.arange(len(pp.f)), np.array(pp.f))
ax2.plot(pp.start_idx + np.arange(len(pp.f)),
20*np.log10(np.array(pp.mag)))
ax1.hold(False)
# ax1.xlabel('Time (s)')
ax1.set_ylabel('Frequency (Hz)')
ax2.set_xlabel('Time (s)')
ax2.set_ylabel('Frequency (Hz)')
# pl.show()
return pl.gca()
def plot_time_freq_mag(self, minlen=10, cm=pl.cm.rainbow):
cadd = 30
cmax = 256
ccur = 0
part = [pp for pp in self.partial if len(pp.f) > minlen]
pl.figure()
pl.hold(True)
for pp in part:
# pl.plot(pp.start_idx + np.arange(len(pp.f)), np.array(pp.f))
mag = 100 + 20*np.log10(np.array(pp.mag))
pl.scatter(pp.start_idx + np.arange(len(pp.f)), np.array(pp.f),
s=mag, c=cm(ccur), lw=0)
ccur = np.mod(ccur + cadd, cmax)
pl.hold(False)
pl.xlabel('Time (s)')
pl.ylabel('Frequency (Hz)')
pl.show()
def test_arms_via_lognormal (self):
""" Test by sampling from a lognormal distribution."""
N = 5000
params = [ (0.0, 1.0, numpy.inf), (-2.0, 0.5, numpy.inf), (2.0, 2.0, 1000.0) ]
for mu, sig, x_max in params:
print "==========================\nSampling lognormal mean %.4f sd %.4f" % (mu, sig)
ln = distributions.LogNormal (mu, sig)
s_ex = []
while len(s_ex) < N:
x = ln.sample (1)
if x < x_max: s_ex.append (x)
sampling.reset_statistics()
s_ars = sampling.arms (ln.lpdf, ln.dx_lpdf, 0.0, x_max, n=N, num_points=5)
pylab.hold(False)
pylab.plot (s_ars)
pylab.savefig('iter.png')
pylab.plot (s_ars[1:N-1], s_ars[2:N], 'ro')
pylab.savefig('lag1.png')
s_ex.sort()
s_ars.sort()
stats = sampling.statistics()
print "ARMS: Num MH rejections = %d" % (stats['mh_rejects'])
self.assertEquals (N, stats['values_generated'])
self.assertTrue (0 < stats['mh_rejects'])
netutils.check_quantiles (self, s_ex, s_ars, N)
def _test_graph():
i = 10000
x = np.linspace(0,3.7*pi,i)
y = (0.3*np.sin(x) + np.sin(1.3 * x) + 0.9 * np.sin(4.2 * x) + 0.06 *
np.random.randn(i))
y *= -1
x = range(i)
_max, _min = peakdetect(y,x,750, 0.30)
xm = [p[0] for p in _max]
ym = [p[1] for p in _max]
xn = [p[0] for p in _min]
yn = [p[1] for p in _min]
plot = pylab.plot(x,y)
pylab.hold(True)
pylab.plot(xm, ym, 'r+')
pylab.plot(xn, yn, 'g+')
_max, _min = peak_det_bad.peakdetect(y, 0.7, x)
xm = [p[0] for p in _max]
ym = [p[1] for p in _max]
xn = [p[0] for p in _min]
yn = [p[1] for p in _min]
pylab.plot(xm, ym, 'y*')
pylab.plot(xn, yn, 'k*')
pylab.show()
def plot(self):
"""Plot a graphical representation of the peaks
Arguments:
(none)
"""
import pylab as pl
pl.figure()
pl.plot(self.x)
pl.hold('on')
pl.plot(self.pos,self.val,'o')
pl.hold('off')
def plot_time_freq(self, minlen=10):
part = [pp for pp in self.partial if len(pp.f) > minlen]
pl.figure()
pl.hold(True)
for pp in part:
pl.plot(pp.start_idx + np.arange(len(pp.f)), np.array(pp.f))
pl.hold(False)
pl.xlabel('Time (s)')
pl.ylabel('Frequency (Hz)')
# pl.show()
return pl.gca()
def peakdetect_parabole(y_axis, x_axis, points = 9):
"""
Function for detecting local maximas and minmias in a signal.
Discovers peaks by fitting the model function: y = k (x - tau) ** 2 + m
to the peaks. The amount of points used in the fitting is set by the
points argument.
Omitting the x_axis is forbidden as it would make the resulting x_axis
value silly if it was returned as index 50.234 or similar.
will find the same amount of peaks as the 'peakdetect_zero_crossing'
function, but might result in a more precise value of the peak.
keyword arguments:
y_axis -- A list containg the signal over which to find peaks
x_axis -- A x-axis whose values correspond to the y_axis list and is used
in the return to specify the postion of the peaks.
points -- (optional) How many points around the peak should be used during
curve fitting, must be odd (default: 9)
return -- two lists [max_peaks, min_peaks] containing the positive and
negative peaks respectively. Each cell of the lists contains a list
of: (position, peak_value)
to get the average peak value do: np.mean(max_peaks, 0)[1] on the
results to unpack one of the lists into x, y coordinates do:
x, y = zip(*max_peaks)
"""
# check input data
x_axis, y_axis = _datacheck_peakdetect(x_axis, y_axis)
# make the points argument odd
points += 1 - points % 2
#points += 1 - int(points) & 1 slower when int conversion needed
# get raw peaks
max_raw, min_raw = peakdetect_zero_crossing(y_axis)
# define output variable
max_peaks = []
min_peaks = []
max_ = _peakdetect_parabole_fitter(max_raw, x_axis, y_axis, points)
min_ = _peakdetect_parabole_fitter(min_raw, x_axis, y_axis, points)
max_peaks = map(lambda x: [x[0], x[1]], max_)
max_fitted = map(lambda x: x[-1], max_)
min_peaks = map(lambda x: [x[0], x[1]], min_)
min_fitted = map(lambda x: x[-1], min_)
#pylab.plot(x_axis, y_axis)
#pylab.hold(True)
#for max_p, max_f in zip(max_peaks, max_fitted):
# pylab.plot(max_p[0], max_p[1], 'x')
# pylab.plot(max_f[0], max_f[1], 'o', markersize = 2)
#for min_p, min_f in zip(min_peaks, min_fitted):
# pylab.plot(min_p[0], min_p[1], 'x')
# pylab.plot(min_f[0], min_f[1], 'o', markersize = 2)
#pylab.show()
return [max_peaks, min_peaks]