#format wiki #language en [[TableOfContents]] [[Anchor(convolve)]] === convolve() === {{{#!python numbers=disable >>> from scipy import * >>> x=r_[1:4] >>> h1=zeros(15); h1[0]=1 >>> h2=zeros(15); h2[6]=1 >>> h3=zeros(15); h3[12]=1 >>> convolve(h1+h2+h3,x) array([1, 2, 3, 0, 0, 0, 1, 2, 3, 0, 0, 0, 1, 2, 3, 0, 0]) }}} [[Anchor(fft)]] === fft() === {{{#!python numbers=disable >>> from scipy import * >>> from pylab import * >>> >>> sample_rate=1000.00 >>> t=r_[0:0.6:1/sample_rate] >>> N=len(t) >>> s=sin(2*pi*50*t)+sin(2*pi*70*t+pi/4) >>> S=fft(s) >>> f=sample_rate*r_[0:(N/2)]/N >>> n=len(f) >>> plot(f,abs(S[0:n])/N) >>> show() }}} [[Anchor(spline)]] === UnivariateSpline() === {{{#!python numbers=disable >>> import numpy >>> import scipy >>> import scipy.interpolate >>> >>> x = numpy.arange(10,dtype='float32') * 0.3 >>> y = numpy.cos(x) >>> >>> sp = scipy.interpolate.UnivariateSpline(x,y) >>> sp(0.5) # should be a good approximation of cos(0.5)=0.8775 array([ 0.88198072]) }}} [[Anchor(spline)]] === splrep() and splev() === {{{#!python numbers=disable >>> import numpy >>> import scipy >>> import scipy.interpolate >>> >>> x = numpy.arange(10,dtype='float32') * 0.3 >>> y = numpy.cos(x) >>> >>> rep = scipy.interpolate.splrep(x,y) >>> scipy.interpolate.splev(0.5,rep) # should be a good approximation of cos(0.5)=0.8775 0.881980721213 }}} [[Anchor(integrate)]] === integrate.quad() === {{{#!python numbers=disable >>> from scipy import * >>> value, err = integrate.quad(func=pow, a=0., b=1., args=(5,)) >>> value # integral of x^5 over [0,1] 0.16666666666666669 }}} [[Anchor(fmin)]] === optimize.fmin() === {{{#!python numbers=disable from pylab import * from numpy import * from scipy.optimize import fmin ## Parametric function: 'v' is the parameter vector, 'x' the independent variable fp = lambda v, x: v[0]/(x**v[1])*sin(v[2]*x) ## Noisy function (used to generate data to fit) v_real = [1.5, 0.1, 2.] fn = lambda x: fp(v_real, x) ## Error function e = lambda v, x, y: ((fp(v,x)-y)**2).sum() ## Generating noisy data to fit n = 30 xmin = 0.1 xmax = 5 x = linspace(xmin,xmax,n) y = fn(x) + rand(len(x))*0.2*(fn(x).max()-fn(x).min()) ## Initial parameter value v0 = [3., 1, 4.] ## Fitting v = fmin(e, v0, args=(x,y),maxiter=10000, maxfun=10000) ## Plot def plot_fit(): print 'Estimater parameters: ', v print 'Real parameters: ', v_real X = linspace(xmin,xmax,n*5) plot(x,y,'ro', X, fp(v,X)) plot_fit() show() }}} [[Anchor(leastsq)]] === optimize.leastsq() === {{{#!python numbers=disable from pylab import * from numpy import * from scipy.optimize import leastsq ## Parametric function: 'v' is the parameter vector, 'x' the independent varible fp = lambda v, x: v[0]/(x**v[1])*sin(v[2]*x) ## Noisy function (used to generate data to fit) v_real = [1.5, 0.1, 2.] fn = lambda x: fp(v_real, x) ## Error function e = lambda v, x, y: (fp(v,x)-y) ## Generating noisy data to fit n = 30 xmin = 0.1 xmax = 5 x = linspace(xmin,xmax,n) y = fn(x) + rand(len(x))*0.2*(fn(x).max()-fn(x).min()) ## Initial parameter value v0 = [3., 1, 4.] ## Fitting v, success = leastsq(e, v0, args=(x,y), maxfev=10000) ## Plot def plot_fit(): print 'Estimater parameters: ', v print 'Real parameters: ', v_real X = linspace(xmin,xmax,n*5) plot(x,y,'ro', X, fp(v,X)) plot_fit() show() }}}