%pylab inline from scipy import interpolate def f(x): return np.sin(x) - 0.3 * x n_m = 10 x_m = arange(n_m) y_m = f(x_m) + 0.15*randn(n_m) # simulated measured signal x_real = linspace(0, n_m-1, 100) y_real = f(x_real) lin_interp = interpolate.interp1d(x_m, y_m) # return linear interpolation function y_est_lin = lin_interp(x_real) # evaluate at desired points cub_interp = interpolate.interp1d(x_m, y_m, kind='cubic') # return cubic interpolation function y_est_cub = cub_interp(x_real) fig, ax = subplots(figsize=(10,5)) ax.plot(x_real, y_real, 'k:', label='actual') ax.plot(x_m, y_m, 'bo', label='measured') ax.plot(x_real, y_est_lin, 'g-', label='linear') ax.plot(x_real, y_est_cub, 'r-', label='cubic') legend(loc='lower left') from scipy import stats x = linspace(-5, 5, 100) P = stats.norm() # normal distribution fig, ax = subplots(2,1, sharex=True) ax[0].plot(x, P.pdf(x)) ax[0].set_title('probability density function') ax[1].plot(x, P.cdf(x)) ax[1].set_title('cumulative density function') s_sample = P.rvs(size=1000) r_sample = randn(500) h = hist(s_sample, bins=50) h1 = hist(r_sample, bins=50, color='red', alpha=0.6) t_stat, p_value = stats.ttest_ind(s_sample, r_sample) print 't_stat:', t_stat print 'p_value:', p_value from scipy import stats np = 50 x = linspace(-10, 20, np) a0, b0 = 0.75, -3 y = a0 * x + b0 y_noise = y + randn(np)*2 resu = stats.linregress(x, y_noise) a, b = resu[0:2] y_fit = a*x + b eq_lbl = '$y = %7.2f x %+7.2f$' % (a,b) plot(x, y_noise, 'ko') plot(x, y_fit, 'b-', label=eq_lbl) legend(loc='upper left') # same task using polynomial fit a, b = polyfit(x, y_noise, 1) y_fit = polyval((a,b), x) eq_lbl = '$y = %7.2f x %+7.2f$' % (a,b) plot(x, y_noise, 'ko') plot(x, y_fit,'b-', label=eq_lbl) legend(loc='upper left') a0, b0, c0 = 0.5, -1.4, 8.0 y = a0*x**2 + b0*x + c0 y_noise = y + randn(np)*10 plot(x, y_noise, 'ko', mfc='none') # mfc <-> markerfacecolor a, b, c = polyfit(x, y_noise, 2) eq_lbl = r'$%.2f x^2 %+.2f x %+.2f$' % (a, b, c) y_fit = polyval((a,b,c), x) plot(x, y_fit, 'b-', label=eq_lbl, lw=2) legend(loc='upper left') import numpy as np x = linspace(-1, 1, 100) pure = sin(15*x) corrupt = pure + 0.25*randn(100) # moving average wgt = np.ones(5) smooth = numpy.convolve(wgt/wgt.sum(), corrupt, mode='same') fig, ax = subplots(figsize=(10,4)) ax.plot(x, pure, 'k-', label='original') ax.plot(x, corrupt, label='corrupted') ax.plot(x, smooth, 'r-', label='smoothed') ax.set_ylim([-2,3]) legend() length = 25 figure(figsize=(8,8)) subplot(221) plot(np.hanning(length)) title('Hanning') subplot(222) plot(np.hamming(length)) title('Hamming') subplot(223) plot(np.bartlett(length)) title('Bartlett') subplot(224) plot(np.blackman(length)) title('Blackman')