Compressed Sensing : A python demo

1,506 views
Skip to first unread message

Dilawar Singh

unread,
Sep 6, 2017, 9:49:43 AM9/6/17
to wncc_iitb

TL;DR. Dont read. Only useful for signal/image-processing/information theory people.

Can you solve Ax = b for x when number of rows are less than number of columns in A i.e there are more variables than equations? Such a system is called under-determined system. For examples, give me 3 numbers whose average is 3. Or solve x/3 + y/3 + z/3 = 3. You are right! There are many solutions to this problem!

One adds more constraints and hope for unique solution. Give me a solution which has minimum length (minimize L2-norm)? Answer is 3,3,3 (Generalized Inverse). Give me a solution which is most sparse (least number of non-zero entries)? Answer is 9,0,0 (or 0,0,9).

Many signals are sparse (in some domain). Images are sparse in Fourier/Wavelets domain; neural activity is sparse in time-domain, activity of programming is sparse in spatial-domain. Can we solve under-determined Ax = b for sparse x. Or formally,

minxL0(x), st Ax = b

where L0 is the 0-norm i.e. number of non-zero entries.

This problem is hard to solve. L0 is not a convex-function. Probably one can use convex relaxation trick and replace the non-convex function with convex one.

minxL1(x), st Ax = b

It is now well-understood that under certain assumptions on A, we can recover  by solving this tractable convex-optimization problem rather than the original NP hard problem. Greedy strategy based solvers works even faster though they are not proven to be as accurate as this one.

1 Somewhat formal statement

Skip it if formatting is not good.

If xN (size N) is S − sparse then an under-determined system AkNxN = bk -- where b is vector of k observations -- can be solved exactly given that A (measurement matrix) has following properties:

  • y2 ∼ =‖x2 where y = Ax for any sparse signal x i.e application of measurements matrix does not change the length of sparse signal x 'very much'. See Restricted Isometric Property in sec. 3.1 .

Lets there is a sparse signal x of length N. Its sparse. We take a dot-product of x with a random vector Ai of size N i.e. bi = Ai * x. We make k such measurements. We can put all k Ai into a matrix A and represent the process by a linear system:


$\begin{bmatrix} A_1 \\ A_2 \\ \vdots \\ A_k \end{bmatrix} * x = \begin{bmatrix} b_1 \\ b_2 \\ \vdots \\ b_k \end{bmatrix} \qquad(1)$

Note that each Ai has the dimension 1 × N. We can rewrite eq. 1 as following:

Ax = b  (2)
where dimension of A are k × N and k < <N. This is an under-determined system with the condition that x is sparse. Can we recover x (of size N) from b (of size k < < N)?

Figure fig. 1 shows a real system. Compressed sensing says that x can be recovered by solving the following linear program.

minxx1 given Ax = b  (3)

2 A demonstration using Python

Data for fig. 2 is generated by script ./compressed_sensing.py and for fig. 3, data is generated by script ./magic_reconstruction.py. Both of these scripts also generate similar figures.

Dependencies In addition to scipy, numpy, and matplotlib, we also need pyCSalgo. It is available via pip: pip install pyCSalgo --user.

Code is available at github

2.1 Algorithm

  1. input : sparse signal x of size N
  2. Generate a random matrix (measurement matrix) A of size k × N.
  3. Make k measurements of x using A. That is b = Ax. Note that each measurement of x (i.e. entry of b) is some linear combination of values of x given by Aix where Ai is ith row.
  4. Now find x by solving minxx1given Ax = b.

For step 4, we are using function l1eq_pd from Python library pyCSalgo. This is re-implementation of l1magic routine written in Matlab by Candes. When k > >2S where S is the sparsity of x, we can recover x quite well. In practice, k ≥ 4S almost always gives exact result.

2.2 Demo 1: Sparse in time domain

pasted1Figure 1: x is the sparse signal of length 256. Using measurements matrix A, we made 64 random measurements of x namely b. The sparse solution of eq. 2 is the solution of minxx1given Ax = b.

Solution

pasted2Figure 2: Solution by CS which is almost equal to the original signal.

2.3 Sparse in Fourier domain

pasted3Figure 3: CS solution when signal is sparse in Fourier domain (DCT: Discrete Cosine Domain). Note that we took only 200 samples for a signal of size 2000. The recovery is pretty good. You can play both signal on phone and wouldn't notice a difference.


Appendix: Restricted isometric property (RIP)

Restricted isometric property (RIP) of a matrix A is defined as the following.

Given δs ∈ (0, 1), for any sub-matrix As of A, and for any sparse vector y, if following holds

(1 − δs)‖y2 ≤ ‖Asy2 ≤ (1 + δs)‖y2
then matrix A is said to satify srestricted isometric property with restricted isometric constant δs. Note that a matrix A with such a property does not change the length of singal x 'very much'. This enables us to sense two different sparse signal x1 and x2 such that Ax1 and Ax2 are almost likely to be different.

References

To learn about compressed sensing, I recommend following articles

There are many many other great articles and papers on this subject. There are some dedicated web-pages also.

- Dilawar

Dilawar Singh

unread,
Sep 7, 2017, 12:08:11 AM9/7/17
to Web and Coding Club IIT Bombay
Here is another demo : http://www.pyrunner.com/weblog/2016/05/26/compressed-sensing-python/

It has demo of images and uses cvxpy library. It is also much better written and exhaustive.

- Dilawar

Kumar Ayush

unread,
Sep 7, 2017, 1:03:32 AM9/7/17
to Web and Coding Club IIT Bombay

Thank you Dilawar. I hope you don't mind if I add your content to
wncc-iitb.org/wiki

Even being long, it is a good start for people wanting to learn about this

Regards
Ayush


--
--
The website for the club is http://wncc-iitb.org/
To post to this group, send email to wncc...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dilawar Singh

unread,
Sep 7, 2017, 2:22:20 AM9/7/17
to Web and Coding Club IIT Bombay
On Thursday, September 7, 2017 at 10:33:32 AM UTC+5:30, cheekujodhpur wrote:

Thank you Dilawar. I hope you don't mind if I add your content to
wncc-iitb.org/wiki


Sure. Go ahead.
Reply all
Reply to author
Forward
0 new messages