plt.title('Sparse Coefficient (' + dictype + ')') plt.colorbar() plt.subplot(224) plt.imshow(Phi) plt.title('Measurement matrix (' + mestype + ')') plt.tight_layout() plt.show() # ===========way 1================= Y = np.matmul(Phi, X) X1 = pys.romp(Y, Phi, k=k1, alpha=alpha, normalize=True, tol=1e-16, verbose=True) if dictype is not None: X1 = np.matmul(D, X1) X1 = pys.scale(X1, sto=[0, 255], sfrom=sfrom, istrunc=True) X1 = X1.astype(X.dtype) PSNR1, MSE1, Vpeak1, dtype = pys.psnr(X, X1, Vpeak=None, mode='rich') print("---PSNR1, MSE1, Vpeak1, dtype: ", PSNR1, MSE1, Vpeak1, dtype) # ===========way 2================= Y = np.matmul(Phi, X)
k4 = 100 R = 4 alpha = 1e-6 M = np.size(y) N = int(M * R) ff = np.linspace(0, Fs, int(Ns * R)) A = pys.gaussian((M, N)) A = pys.odctdict((M, N), isnorm=True) # A = pys.dctdict(N) # A = pys.odctndict((M, N)) print(A.shape) x1 = pys.romp(y, A, k=k1, alpha=alpha, verbose=False) y1 = np.matmul(A, x1) x2 = pys.romp(y, A, k=k2, alpha=alpha, verbose=False) y2 = np.matmul(A, x2) x3 = pys.romp(y, A, k=k3, alpha=alpha, verbose=False) y3 = np.matmul(A, x3) x4 = pys.romp(y, A, k=k4, alpha=alpha, verbose=False) y4 = np.matmul(A, x4) print("---MSE(y, y1) with k = " + str(k1) + ": ", pys.mse(y, y1)) print("---MSE(y, y2) with k = " + str(k2) + ": ", pys.mse(y, y2)) print("---MSE(y, y3) with k = " + str(k3) + ": ", pys.mse(y, y3)) print("---MSE(y, y4) with k = " + str(k4) + ": ", pys.mse(y, y4))
def cs1d(y, Phi, Psi=None, optim='OMP', k=1000, tol=1e-8, osshape=None, verbose=True): r"""Solves the 1-d Compressive Sensing problem for sparse signal :math:`\bm x` .. math:: {\bm y} = {\bm \Phi}{\bm x} + {\bf n}, for non-sparse signal :math:`{\bm x} = {\bm \Psi}{\bm z}` .. math:: {\bm y} = {\bm \Phi \Psi}{\bm z} + {\bm n}, see https://iridescent.ink/aitrace/SignalProcessing/Sparse/LinearCompressiveSensing/index.html for details. Arguments ---------------------- y : ndarray the mesurements :math:`{\bm y}`, if ndarray, each colum is a mesurement Phi : 2darray the mesurement matrix :math:`{\bm \Phi}` Keyword Arguments ---------------------- Psi : 2darray the dictionary :math:`{\bm \Psi}` (default: {None}) optim : str optimization method, OMP, LASSO (default: {'OMP'}) k : integer sparse degree for OMP, max iter for LASSO (default: size of :math:`{\bm x}` ) tol : float tolerance of error (LASSO) (default: {1e-8}) osshape : tuple orignal signal shape, such as an H-W image (default: {None}) verbose : bool show log info (default: {True}) Returns ---------------------- x : ndarray reconstructed signal with sieof osshape """ print("================in cs1d================") if y is None: print("===No raw data!") if verbose: print("===Type of Phi, y: ", Phi.dtype, y.dtype) cplxFlag = False if Psi is not None: # =================step1: Construct=========== if verbose: print("===Construct sensing matrix: A = Phi*D...") A = np.matmul(Phi, Psi) Phi = None if verbose: print("===Done!...") else: A = Phi Phi = None if np.iscomplex(A).any() or np.iscomplex(y).any(): cplxFlag = True if verbose: print("===Convert complex to real...") y = np.concatenate((np.real(y), np.imag(y)), axis=0) ReA = np.real(A) ImA = np.imag(A) A1 = np.concatenate((ReA, -ImA), axis=1) A2 = np.concatenate((ImA, ReA), axis=1) A = np.concatenate((A1, A2), axis=0) if verbose: print("===Done!") # if np.ndim(y) > 1: # y = y.flatten() if optim is 'OMP': print(A.shape, y.shape) z = pys.romp(y, A, k=k, alpha=1e-6, normalize=False, tol=1e-16, verbose=verbose) if cplxFlag: z = np.split(z, 2) z = z[0] + 1j * z[1] if verbose: print("===size of z: ", z.shape) if Psi is not None: x = np.matmul(Psi, z) else: x = z z = None if osshape is not None: x = np.reshape(x, osshape) if verbose: print("===shape of x: ", x.shape) print("===Done!") return x
plt.subplot(224) plt.imshow(Phi) plt.title('Measurement matrix (' + mestype + ')') plt.tight_layout() plt.show() # ===========way 1================= Y = np.matmul(Phi, X) # XX = pys.omp0( # Y, A, normalize=True, tol=1e-38, verbose=True) # XX = pys.omp( # Y, A, k=k, normalize=True, tol=1e-16, verbose=True) X1 = pys.romp(Y, A, k=k1, alpha=alpha, normalize=True, tol=1e-16, verbose=True) if dictype is not None: X1 = np.matmul(D, X1) X1 = pys.scale(X1, sto=[0, 255], sfrom=sfrom, istrunc=True) X1 = X1.astype(X.dtype) PSNR1, MSE1, Vpeak1, dtype = pys.psnr(X, X1, Vpeak=None, mode='rich') X2 = pys.romp(Y, A, k=k2, alpha=alpha, normalize=True, tol=1e-16, verbose=True) if dictype is not None: X2 = np.matmul(D, X2) X2 = pys.scale(X2, sto=[0, 255], sfrom=sfrom, istrunc=True) X2 = X2.astype(X.dtype) PSNR2, MSE2, Vpeak2, dtype = pys.psnr(X, X2, Vpeak=None, mode='rich')
X[:, n] = x if vecflag: X = np.reshape(X, N) if verbose: print("===Done!") return X if __name__ == '__main__': import pysparse as pys M = 3 N = 4 x = np.array([-0.5, 0.01, 3.1, 0.8]) A = pys.gaussian((M, N)) # A = pys.odctdict((M, N)) y = np.matmul(A, x) print("---A (Mesurement Matrix): ", A) print("---y (Mesurement Vector): ", y) print("---x (Orignal Signal): ", x) x = pys.romp(y, A, k=2, alpha=0.0001, verbose=False) print("---x (Reconstructed Signal): ", x) x = pys.omp(y, A, k=3, verbose=False) print("---x (Reconstructed Signal): ", x)