Example #1
0
def test_bp(par):
    """Basis pursuit (BP) problem:

    minimize ||x||_1 subject to Ax = b

    """
    np.random.seed(0)

    # Create random m-by-n encoding matrix
    n = par['n']
    m = par['m']
    k = par['k']

    A, A1 = np.linalg.qr(np.random.randn(n, m), 'reduced')
    if m > n:
        A = A1.copy()
    A = A.astype(par['dtype'])

    # Create sparse vector
    p = np.random.permutation(m)
    p = p[0:k]
    x = np.zeros(m, dtype=par['dtype'])
    x[p] = np.random.randn(k)

    # Set up vector b, and run solver
    b = A.dot(x)
    xinv, _, _, _ = spg_bp(A, b, verbosity=0)
    assert xinv.dtype == par['dtype']
    assert_array_almost_equal(x, xinv, decimal=3)

    # Run solver with subspace minimization
    xinv, _, _, _ = spg_bp(A, b, subspace_min=True, verbosity=0)
    assert xinv.dtype == par['dtype']
    assert_array_almost_equal(x, xinv, decimal=3)
Example #2
0
def test_weighted_bp(par):
    """Weighted Basis pursuit (WBP) problem:

    minimize ||y||_1 subject to AW^{-1}y = b

    or

    minimize ||Wx||_1 subject to Ax = b

    """
    # Create random m-by-n encoding matrix
    n = par['n']
    m = par['m']
    k = par['k']

    A, A1 = np.linalg.qr(np.random.randn(n, m), 'reduced')
    if m > n:
        A = A1.copy()

    # Create sparse vector
    p = np.random.permutation(m)
    p = p[0:k]
    x = np.zeros(m)
    x[p] = np.random.randn(k)

    # Set up weights w and vector b
    w = 0.1*np.random.rand(m) + 0.1 # Weights
    b = A.dot(x / w)  # Signal
    xinv, _, _, _ = spg_bp(A, b, iter_lim=1000, weights=w, verbosity=0)

    # Reconstructed solution, with weighting
    xinv *= w
    assert_array_almost_equal(x, xinv, decimal=3)
Example #3
0
    # % -----------------------------------------------------------
    # % Solve the basis pursuit (BP) problem:
    # %
    # %    minimize ||x||_1 subject to Ax = b
    # %
    # % -----------------------------------------------------------
    print('%s ' % ('-'*78))
    print('Solve the basis pursuit (BP) problem:      ')
    print('                                              ')
    print('  minimize ||x||_1 subject to Ax = b')
    print('                                              ')
    print('%s%s ' % ('-'*78, '\n'))

    # % Set up vector b, and run solver
    b = A.dot(x0) # signal
    x,resid,grad,info = spg_bp(A, b)

    figure()
    plot(x,'b')
    hold(True)
    plot(x0,'ro')
    legend(('Recovered coefficients','Original coefficients'))
    title('(a) Basis Pursuit')

    print('%s%s%s' % ('-'*35,' Solution ','-'*35))
    print('See figure 1(a)')
    print('%s%s ' % ('-'*78, '\n'))


    # % -----------------------------------------------------------
    # % Solve the basis pursuit denoise (BPDN) problem:
Example #4
0
def basis_pursuit(t, y, fmin=None, fmax=None, nfreqs=5000, polyorder=2, method="basis", tau=0.1,
				  noise=True):

	# preprocess

	ndata = np.size(y)

	tmin = t.min()
	t -= tmin

	yscale = y.max()-y.min()
	ymin = y.min()

	y = (y-ymin)/(yscale)-0.5

	trange = np.nanmax(t)-np.nanmin(t)
	dt = np.abs(np.nanmedian(t-np.roll(t,-1)))
	nt = np.size(t)

	if fmin is None:
		fmin = 1./trange
	if fmax is None:
		fmax = 2./dt

	freqs = np.linspace(fmin,fmax,nfreqs)
	df = np.abs(np.nanmedian(freqs-np.roll(freqs,-1)))

	if noise is True:
		ndirac = ndata
	else:
		ndirac = 0

	X = np.zeros((nt,nfreqs*2+polyorder+1+ndirac))

	# set up matrix of sines and cosines
	for j in range(nfreqs):
		X[:,j] = np.sin(t*freqs[j])
		X[:,nfreqs+j] = np.cos(t*freqs[j])

	# now do polynomial bits
	for j in range(polyorder+1):
		pp = t**(polyorder-j)
		X[:,nfreqs*2 +j] = pp/np.abs(pp.max())

	# now do the dirac delta functions

	for j in range(ndirac):
		X[j,-ndirac+j] = 1.

	if method == "basis":
		x,resid,grad,info = spg_bp(X, y)

	elif method == "lasso":
		tau = 0.1
		x, resid, grad, info = spg_lasso(X, y, tau)

	else:
		print("Did not select a method")
		return 0

	sines = x[:nfreqs]
	cosines = x[nfreqs:2*nfreqs]
	power = (sines**2 + cosines**2)

	if noise:
  		diracs = x[-ndirac:]
	else:
  		diracs = None

	output = {'freqs':freqs,
  			  'sines': sines,
			  'cosines': cosines,
			  'power':power,
			  'polys':x[2*nfreqs:2*nfreqs+polyorder+1],
			  'diracs':diracs,
			  'resid':resid,
			  'grad':grad,
			  'info':info,
			  'coeffs':x,
			  'matrix':X,
			  'model':(np.dot(X,x)+0.5)*yscale+ymin
			  }

	return output
Example #5
0
    t = np.linspace(-np.pi * frac, np.pi * frac, n)
    x = np.fft.ifft(np.fft.fft(2.5 * np.sin(t)))

    # Create dictionary, D
    m = int(n / 2)
    D = np.fft.fft(np.eye(n, n))

    # Create A
    np.random.seed(0)
    idx = np.random.permutation(n)
    idx = idx[0:m]
    A = D[idx, :]

    # Sense b in the time domain
    b = x[idx]

    # Run spgl1 solver to get coefficients
    c, resid, grad, info = spg_bp(A, b)

    # Reconstruct estimate of x
    x_hat = D.dot(c)

    # Get estimate from b
    x_b = np.zeros(x.shape, dtype='complex')
    x_b[idx] = b

    plt.plot(np.abs(x))
    plt.plot(np.abs(x_b))
    plt.plot(np.abs(x_hat))
    plt.show()
Example #6
0
x, resid, grad, info = spgl1.spg_lasso(A, b, tau, verbosity=1)

print('%s%s%s' % ('-' * 35, ' Solution ', '-' * 35))
print('nonzeros(x) = %i,   ||x||_1 = %12.6e,   ||x||_1 - pi = %13.6e' %
      (np.sum(np.abs(x) > 1e-5), np.linalg.norm(
          x, 1), np.linalg.norm(x, 1) - np.pi))
print('%s' % ('-' * 80))

###############################################################################
# Solve the basis pursuit (BP) problem:
#
#  .. math::
#     min.  ||\mathbf{x}||_1 \quad subject \quad  to \quad
#     \mathbf{Ax} = \mathbf{b}
b = A.dot(x0)
x, resid, grad, info = spgl1.spg_bp(A, b, verbosity=2)

plt.figure()
plt.plot(x, 'b')
plt.plot(x0, 'ro')
plt.legend(('Recovered coefficients', 'Original coefficients'))
plt.title('Basis Pursuit')

plt.figure()
plt.plot(info['xnorm1'], info['rnorm2'], '.-k')
plt.xlabel(r'$||x||_1$')
plt.ylabel(r'$||r||_2$')
plt.title('Pareto curve')

plt.figure()
plt.plot(np.arange(info['niters']),