b = np.arctan2(x[:, 0], -2 + x[:, 1]) return (a - b) / np.pi # define the known discontinuity bnd_vert = np.array([[0.0, 2.0], [0.0, -2.0]]) bnd_smp = np.array([[0, 1]]) # create synthetic data pnts_obs = 8 * (rbf.halton.halton(200, 2, start=1) - 0.5) sigma_obs = 0.2 * np.ones(200) u_obs = signal(pnts_obs) + np.random.normal(0.0, sigma_obs) # find the filtered solution cutoff = 0.5 soln, _ = filter(pnts_obs, u_obs, sigma=sigma_obs, cutoff=cutoff, vert=bnd_vert, smp=bnd_smp) # plot the results and true signal vals = np.linspace(-4, 4, 200) grid = np.reshape(np.meshgrid(vals, vals), (2, 200**2)).T fig, axs = plt.subplots(2, 1, figsize=(6, 10)) axs[0].scatter(pnts_obs[:, 0], pnts_obs[:, 1], c=u_obs, s=50, vmin=-1.0, vmax=1.0, cmap='viridis', zorder=1) p = axs[0].tripcolor(grid[:, 0],
x = np.linspace(0.0,1.0,768) y = np.linspace(1.0,0.0,768) x,y = np.meshgrid(x,y) points = np.array([x.flatten(),y.flatten()]).T values = np.linalg.norm(face(),axis=2)[:,256:].flatten() #values = lena().flatten().astype(float) values /= 441.7 # normalize so that the max is 1.0 signal = NearestNDInterpolator(points,values) # interpolate Lena onto new observation points and add noise points_obs = np.random.normal(0.5,0.3,(100000,2)) #points_obs = rbf.halton.halton(50000,2) u_obs = signal(points_obs) + np.random.normal(0.0,0.2,100000) # find filtered solution cutoff = 60.0 soln,_ = filter(points_obs,u_obs,cutoff=cutoff,n=20,samples=0) # plot the observed and filtered results fig,ax = plt.subplots(2,1,figsize=(6,10)) ax[0].set_aspect('equal') ax[0].set_axis_bgcolor('blue') ax[0].set_xlim((0,1)) ax[0].set_ylim((0,1)) ax[0].set_title('noisy data') ax[1].set_aspect('equal') ax[1].set_axis_bgcolor('blue') ax[1].set_xlim((0,1)) ax[1].set_ylim((0,1)) ax[1].set_title(u'filtered solution $\mathregular{(\omega_c = %s)}$' % cutoff) p1 = ax[0].scatter(points_obs[:,0],points_obs[:,1],s=4,c=u_obs, edgecolor='none',cmap='Greys_r',vmin=-0.1,vmax=1.1)
''' This script demonstrates using rbf.filter.filter to denoise and differentiate a time series ''' import numpy as np import matplotlib.pyplot as plt from rbf.filter import filter np.random.seed(1) t = np.linspace(0.0,5.0,200) u_true,udiff_true = np.sin(t),np.cos(t) # true signal sigma_obs = 0.5*np.ones(t.shape) u_obs = u_true + np.random.normal(0.0,sigma_obs) # observed # find the filtered solution u_pred,sigma_pred = filter(t[:,None],u_obs,sigma_obs, samples=1000,n=3, cutoff=0.5) # find the derivative of the filtered solution udiff_pred,sigmadiff_pred = filter(t[:,None],u_obs,sigma_obs, samples=1000,n=3, cutoff=0.5,diffs=(1,)) # plot the results fig,ax = plt.subplots(2,1) ax[0].plot(t,u_obs,'k.',label='observed',zorder=0) ax[0].plot(t,u_true,'r-',label='true signal',zorder=2) ax[0].plot(t,u_pred,'b-',label='filtered',zorder=1) ax[0].fill_between(t,u_pred-sigma_pred,u_pred+sigma_pred, color='b',alpha=0.4,edgecolor='none',zorder=1) ax[0].legend(frameon=False) ax[1].plot(t,udiff_true,'r-',label='true signal derivative',zorder=2)
this signal has a discontinuity from (0.0,-2.0) to (0.0,2.0) ''' a = np.arctan2(x[:,0], 2 + x[:,1]) b = np.arctan2(x[:,0],-2 + x[:,1]) return (a - b)/np.pi # define the known discontinuity bnd_vert = np.array([[0.0, 2.0],[0.0,-2.0]]) bnd_smp = np.array([[0,1]]) # create synthetic data pnts_obs = 8*(rbf.halton.halton(200,2,start=1) - 0.5) sigma_obs = 0.2*np.ones(200) u_obs = signal(pnts_obs) + np.random.normal(0.0,sigma_obs) # find the filtered solution cutoff = 0.5 soln,_ = filter(pnts_obs,u_obs,sigma=sigma_obs,cutoff=cutoff, vert=bnd_vert,smp=bnd_smp,check_all_edges=True) # plot the results and true signal vals = np.linspace(-4,4,200) grid = np.reshape(np.meshgrid(vals,vals),(2,200**2)).T fig,axs = plt.subplots(2,1,figsize=(6,10)) axs[0].scatter(pnts_obs[:,0],pnts_obs[:,1],c=u_obs,s=50, vmin=-1.0,vmax=1.0,cmap='viridis',zorder=1) p = axs[0].tripcolor(grid[:,0],grid[:,1],signal(grid), vmin=-1.0,vmax=1.0,cmap='viridis',zorder=0) axs[0].plot(bnd_vert[:,0],bnd_vert[:,1],'r-',lw=4) axs[0].set_xlim((-4,4));axs[0].set_ylim((-4,4)) axs[0].set_aspect('equal') axs[0].set_title(u'observed data and true signal') plt.colorbar(p,ax=axs[0]) axs[1].scatter(pnts_obs[:,0],pnts_obs[:,1],c=soln,s=50, vmin=-1.0,vmax=1.0,cmap='viridis')
return (a - b) / np.pi # define the known discontinuity bnd_vert = np.array([[0.0, 2.0], [0.0, -2.0]]) bnd_smp = np.array([[0, 1]]) # create synthetic data pnts_obs = 8 * (rbf.halton.halton(200, 2, start=1) - 0.5) sigma_obs = 0.2 * np.ones(200) u_obs = signal(pnts_obs) + np.random.normal(0.0, sigma_obs) # find the filtered solution cutoff = 0.5 soln, _ = filter(pnts_obs, u_obs, sigma=sigma_obs, cutoff=cutoff, vert=bnd_vert, smp=bnd_smp, check_all_edges=True) # plot the results and true signal vals = np.linspace(-4, 4, 200) grid = np.reshape(np.meshgrid(vals, vals), (2, 200**2)).T fig, axs = plt.subplots(2, 1, figsize=(6, 10)) axs[0].scatter(pnts_obs[:, 0], pnts_obs[:, 1], c=u_obs, s=50, vmin=-1.0, vmax=1.0, cmap='viridis', zorder=1)
x = np.linspace(0.0,1.0,512) y = np.linspace(1.0,0.0,512) x,y = np.meshgrid(x,y) points = np.array([x.flatten(),y.flatten()]).T values = lena().flatten().astype(float) values /= 255.0 # normalize so that the max is 1.0 signal = NearestNDInterpolator(points,values) # interpolate Lena onto new observation points and add noise N = 10000 points_obs = np.random.normal(0.5,0.25,(N,2)) u_obs = signal(points_obs) u_obs += np.random.normal(0.0,0.5,N) # find filtered solution soln,sigma = filter(points_obs,u_obs,cutoff=40,size=20) # plot the observed and filtered results fig,ax = plt.subplots(2,1,figsize=(6,10)) ax[0].set_aspect('equal') ax[0].set_axis_bgcolor('blue') ax[0].set_xlim((0,1)) ax[0].set_ylim((0,1)) ax[0].set_title('Noisy Data') ax[1].set_aspect('equal') ax[1].set_axis_bgcolor('blue') ax[1].set_xlim((0,1)) ax[1].set_ylim((0,1)) ax[1].set_title(u'Filtered Solution $(\omega_c = 40)$') p1 = ax[0].scatter(points_obs[:,0],points_obs[:,1],s=4,c=u_obs, edgecolor='none',cmap='Greys_r',vmin=-0.2,vmax=1.2)
differentiate a time series ''' import numpy as np import matplotlib.pyplot as plt from rbf.filter import filter np.random.seed(1) t = np.linspace(0.0, 5.0, 200) u_true, udiff_true = np.sin(t), np.cos(t) # true signal sigma_obs = 0.5 * np.ones(t.shape) u_obs = u_true + np.random.normal(0.0, sigma_obs) # observed # find the filtered solution u_pred, sigma_pred = filter(t[:, None], u_obs, sigma_obs, samples=1000, n=3, cutoff=0.5) # find the derivative of the filtered solution udiff_pred, sigmadiff_pred = filter(t[:, None], u_obs, sigma_obs, samples=1000, n=3, cutoff=0.5, diffs=(1, )) # plot the results fig, ax = plt.subplots(2, 1) ax[0].plot(t, u_obs, 'k.', label='observed', zorder=0) ax[0].plot(t, u_true, 'r-', label='true signal', zorder=2)