Skip to main content

Python implementation of the CoSaMP algorithm

Project description

CoSaMP

CoSaMP algorithm in Python.

Installation

How to install:

pip install cosamp

or

pip3 install cosamp

Dependencies: numpy.

Usage example

@Input:  Phi - Sampling matrix
          u   - Noisy sample vector
          s   - Sparsity vector

@Return: A s-sparse approximation "a" of the target signal

Using CoSaMP algorithm to reconstruct a high-frequency signal from sparse measurements:

import numpy as np
import scipy.linalg
import scipy.signal
import matplotlib.pyplot as plt
from cosamp import cosamp


n = 100 # number of measurements
t = np.linspace(0.0, 1.0, num=n)

x = np.sin(91*2*np.pi*t) + np.sin(412*2*np.pi*t) # original signal (to be reconstructed)

# randomly sample signal
p = 103 # random sampling (Note that this is one eighth of the Shannon–Nyquist rate!)
aquis = np.round((n-1) * np.random.rand(p)).astype(int)
y = x[aquis] # our compressed measurement from the random sampling

# Here {y} = [C]{x} = [C][Phi]{s}, where Phi is the inverse discrete cosine transform

Phi = scipy.fft.dct(np.eye(n), axis=0, norm='ortho')
CPhi = Phi[aquis,:]

# l1 minimization (through linear programming)
s = cosamp.cosamp(CPhi, y, 10) # obtain the sparse vector through CoSaMP algorithm
xrec = scipy.fft.idct(s, axis=0, norm='ortho') # Reconstructed signal



figw, figh = 7.0, 5.0 # figure width and height
plt.figure(figsize=(figw, figh))
plt.plot(t, s)
plt.title('Sparse vector $s$')
plt.show()


# Visualize the compressed-sensing reconstruction signal
figw, figh = 7.0, 5.0 # figure width and height
plt.figure(figsize=(figw, figh))
plt.plot(t, x,    'b', label='Original signal')
plt.plot(t, xrec, 'r', label='Reconstructed signal')
plt.xlim(0.4, 0.5)
legend = plt.legend(loc='upper center', shadow=True, fontsize='x-large')
# Put a nicer background color on the legend.
legend.get_frame().set_facecolor('C0')
plt.show()

sparse vector result

Other info

MATLAB versions of the algorithm are readly available (see here for instance). This Python method is based on the MATLAB routine written by Prof. Bob L. Sturm.

The original Needell and Tropp's 2008 paper can be found here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cosamp-0.0.2.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

cosamp-0.0.2-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file cosamp-0.0.2.tar.gz.

File metadata

  • Download URL: cosamp-0.0.2.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.10

File hashes

Hashes for cosamp-0.0.2.tar.gz
Algorithm Hash digest
SHA256 bb010324054aed2ee1c94ce887934cf495ef9243a0425590b5bca9d8e43503e2
MD5 e1739d6ccfc76e1a7c15c4a601b4429e
BLAKE2b-256 19587567ad5549c4c1a18995fdae9e1bd330f67a645c2b7da7e2d7e33359039c

See more details on using hashes here.

File details

Details for the file cosamp-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: cosamp-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.10

File hashes

Hashes for cosamp-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8ee71e63ed5c8c3ecaeea8bcee2eafbb4ee85341ba03ec9b06a72667194d89f1
MD5 f0f05077c0110e1dd384b44090fc4e45
BLAKE2b-256 92ebcbd915ed36a51e3cd7b1c21bb5ea45448a5c2f07d1f86a43663bcbdb0757

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page