コード例 #1
0
def fftfilt( d, fs, band=(58,62), mode='bandstop', axis=0, dB=20 ):
	"""
	Filter brutally by weighting the fft and inverse-transforming.
	"""###
	if not isinstance( mode, ( tuple, list ) ): mode = [ mode ]
	if not isinstance( band[ 0 ], ( tuple, list ) ): band = [ band ]
	if not isinstance( dB, ( tuple, list ) ): dB = [ dB ]
	if len( mode ) == 1: mode = mode * len( band )
	if len( dB ) == 1: dB = dB * len( band )
	
	D = fft( d, axis=axis )
	f = numpy.abs( fftfreqs( d.shape[ axis ], fs ) )
	shape = [ 1 for x in d.shape ]
	shape[ axis ] = len( f )
	f.shape = shape
	w = f * 0 + 1
	for mode, band, dB in zip( mode, band, dB ):
		band = list( band )
		depth = 10.0 ** ( -abs( dB ) / 20.0 )
		if   mode == 'highpass': w = w * ( depth + ( 1.0 - depth ) * shoulder( f, band + [ fs, fs ] ) )
		elif mode == 'lowpass':  w = w * ( depth + ( 1.0 - depth ) * shoulder( f, [ -fs, -fs ] + band ) )
		elif mode == 'bandpass': w = w * ( depth + ( 1.0 - depth ) * shoulder( f, band ) )
		elif mode == 'bandstop': w = w * (  1.0  - ( 1.0 - depth ) * shoulder( f, band ) )
		else: raise ValueError( 'unrecognized filtering mode "%s"' % mode )
	d = numpy.real( ifft( D * w, axis=axis ) )
	return d
コード例 #2
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()
コード例 #3
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()