Example #1
0
def plotsig(x, samplingfreq_hz=None, hold=False, axis=0, welch=0, **kwargs):
    """
	Makes two subplots, showing time-series <x> in the upper panel and its
	amplitude spectrum in the lower panel.  Set <hold> in order to re-use
	a previous figure.
	
	Any additional keyword arguments are passed through to pylab.plot for
	both subplots.
	"""###
    fs = getfs(samplingfreq_hz)
    if fs == None: fs = getfs(x, 2.0)
    if hasattr(x, 'x'): x = x.x
    elif hasattr(x, 'y'): x = x.y

    if not isnumpyarray(x):
        axis = 0
        if isinstance(x[0], list) or isinstance(x[0], tuple): axis = 1
        x = numpy.array(x, dtype='float')

    xwin = x = project(x, axis).swapaxes(0, axis)
    nsamp = x.shape[0]

    class Unfinished(Exception):
        pass

    if welch == 1: xwin = x * project(hanning(nsamp), len(x.shape) - 1)
    elif welch > 0: raise Unfinished, "Welch periodogram not yet implemented"

    t = numpy.arange(0, nsamp) / float(fs)
    ap = fft2ap(fft(xwin, axis=0), samplingfreq_hz=fs, axis=0)
    f = ap['freq_hz']
    a = 20.0 * numpy.log10(ap['amplitude'])

    pylab = load_pylab()
    if not hold: pylab.clf()

    pylab.subplot(2, 1, 1)
    h1 = pylab.plot(t, x, **kwargs)
    ax = pylab.gca()
    ax.set_xlim(t[0], t[-1])
    ax.xaxis.grid(True)
    ax.yaxis.grid(True)

    pylab.subplot(2, 1, 2)
    a[numpy.isinf(
        a
    )] = numpy.nan  # crude workaround---pylab.plot can't cope with infinite values
    h2 = pylab.plot(f, a, **kwargs)
    ax = pylab.gca()
    ax.set_xlim(f[0], f[-1])
    ax.xaxis.grid(True)
    ax.yaxis.grid(True)

    pylab.draw()
def plotsig(x, samplingfreq_hz=None, hold=False, axis=0, welch=0, **kwargs):
	"""
	Makes two subplots, showing time-series <x> in the upper panel and its
	amplitude spectrum in the lower panel.  Set <hold> in order to re-use
	a previous figure.
	
	Any additional keyword arguments are passed through to pylab.plot for
	both subplots.
	"""###
	fs = getfs(samplingfreq_hz)
	if fs==None: fs = getfs(x,2.0)
	if hasattr(x, 'x'): x = x.x
	elif hasattr(x, 'y'): x = x.y
	
	if not isnumpyarray(x):
		axis = 0
		if isinstance(x[0], list) or isinstance(x[0], tuple): axis = 1
		x = numpy.array(x,dtype='float')

	xwin = x = project(x,axis).swapaxes(0, axis)
	nsamp = x.shape[0]
	
	class Unfinished(Exception): pass
	if welch==1: xwin = x * project(hanning(nsamp),len(x.shape)-1)
	elif welch > 0: raise Unfinished, "Welch periodogram not yet implemented"
	
	t = numpy.arange(0, nsamp) / float(fs)
	ap = fft2ap(fft(xwin,axis=0),samplingfreq_hz=fs,axis=0)
	f = ap['freq_hz']
	a = 20.0 * numpy.log10(ap['amplitude'])

	pylab = load_pylab()
	if not hold: pylab.clf()
	
	pylab.subplot(2,1,1)
	h1 = pylab.plot(t,x,**kwargs)
	ax = pylab.gca()
	ax.set_xlim(t[0], t[-1])
	ax.xaxis.grid(True)
	ax.yaxis.grid(True)
	
	pylab.subplot(2,1,2)
	a[numpy.isinf(a)] = numpy.nan # crude workaround---pylab.plot can't cope with infinite values
	h2 = pylab.plot(f,a,**kwargs)
	ax = pylab.gca()
	ax.set_xlim(f[0], f[-1])
	ax.xaxis.grid(True)
	ax.yaxis.grid(True)

	pylab.draw()
Example #3
0
	def __init__(self, freq_hz, samplingfreq_hz, order=10, type='bandpass', method=scipy.signal.filter_design.butter, **kwargs):
		"""
		Returns a new online causal filter object f with coefficient arrays
		f.b and f.a computed by the specified <method>, given the <order> and
		<freq_hz> parameters, the latter being interpreted in the context of
		the specified <samplingfreq_hz>. Any additional keyword arguments are
		passed through to the <method>.
		
		For example, a 50 Hz notch filter for 250Hz data:
		
		  f = causalfilter([48,52], 250, order=10, type='bandstop')
		
		  x1_filtered = f.apply(x1)  # x1 and x2 are channels-by-samples
		  x2_filtered = f.apply(x2)  # and are consecutive packets: x1
		                             # is "remembered" when filtering x2
		
		"""###
		self.type = scipy.signal.filter_design.band_dict[type]
		self.order = int(order)
		self.order -= self.order%2
		if not isinstance(freq_hz,list) and not isinstance(freq_hz,tuple): freq_hz = [freq_hz]
		self.freq_hz = map(float, freq_hz)
		self.samplingfreq_hz = getfs(samplingfreq_hz)
		self.method = method
		self.kwargs = kwargs
		lims = map((lambda x: x / self.samplingfreq_hz * 2.0), self.freq_hz);
		# TODO:  for firwin, might need to transform optional arg <width> in the same way?
		self.b, self.a = method(N=self.order/2.0, Wn=tuple(lims), btype=self.type, output='ba', **kwargs)
		self.state = None
		self.samples_filtered = 0