Decomposing a discrete signal into a sum of rectangle functions - MathOverflow most recent 30 from http://mathoverflow.net2013-05-22T09:58:13Zhttp://mathoverflow.net/feeds/question/63144http://www.creativecommons.org/licenses/by-nc/2.5/rdfhttp://mathoverflow.net/questions/63144/decomposing-a-discrete-signal-into-a-sum-of-rectangle-functionsDecomposing a discrete signal into a sum of rectangle functionsrodrigob2011-04-27T09:55:01Z2011-04-30T18:12:46Z
<p>Hello mathoverflow community !</p>
<p>I have a simple question that seems to have a non trivial answer.</p>
<p>Given a discrete one dimensional signal $w(x)$ defined in a finite range, and the boxcar (rectangular) function $r(x)$</p>
<p>$$r(x)=\begin{cases}
1 & \mbox{if }0\leq x \leq 1; \
0 & \mbox{elsewhere}
\end{cases}$$</p>
<p>I would like to find the coefficients $a_i,\ b_i,\ c_i $ of the sum </p>
<p>$$w' = \sum_{i=0}^{N}\ { a_i \cdot r\left(\frac{x}{b_i} - c_i\right)}$$</p>
<p>("sum of $N$ rectangles in any range and of any height")
such as $\sum_i\ \left| w_i - w_i'\right|$ is minimized (for a given $N$).</p>
<p>This problem seems related to:</p>
<ol>
<li>Discrete wavelet transform </li>
<li>$l_1$ regularized solution of an overdetermined linear system</li>
<li>Maximum subarray problem</li>
</ol>
<p>However, to my understanding it does not fit any of these cases:</p>
<ol>
<li>$r(x)$ is not a wavelet basis,</li>
<li>the problem cannot be solved (practically) as a linear system because the (finite) set of $a_i,\ b_i,\ c_i $ values is too large to compute explicitly (length of $w$ in the order of $10^4$),</li>
<li>Since $a_i$ is undefined, it does not fit as a maximum subarray problem.</li>
</ol>
<p>Right now I have an approximate solution (iteratively solving the problem via maximum subarray formulation by brute force exploring a subset of possible $a_i$ values), however the idea of "decomposing a signal as a sum of rectangles" seems general enough to think that someone has already addressed it in the past.</p>
<blockquote>
<p>Do any of you have a suggestion on how to tackle this problem ?</p>
<p>Has it already been solved in the past, by a method I am not aware of ?</p>
</blockquote>
<p>Thank you very much for your answers.</p>
http://mathoverflow.net/questions/63144/decomposing-a-discrete-signal-into-a-sum-of-rectangle-functions/63167#63167Answer by ght for Decomposing a discrete signal into a sum of rectangle functionsght2011-04-27T13:21:12Z2011-04-27T13:21:12Z<p>I think that essentially what you want to do is to find the projection of your function onto the vector space generated by the first vectors in the Haar wavelet. More specifically, the Haar wavelet with mother wavelet function $\psi(t)$ can be described as</p>
<p>$$
\psi(t)
\begin{cases}
1 & 0 \leq t < 1/2,\\
-1 & 1/2 \leq t < 1,\\
0 &\text{otherwise.}
\end{cases}
$$</p>
<p>Its scaling function $\phi(t)$ can be described as
$$
\phi(t) =
\begin{cases}
1 \quad & 0 \leq t < 1,\\
0 &\mbox{otherwise.}
\end{cases}
$$</p>
<p>The Haar systems denotes the set of Haar wavelets
$$
t \mapsto \psi_{n,k}(t)=\psi(2^n t-k)
$$
with $n \in \mathbb{N}$ and $0 \leq k < 2^n$. In Hilbert space terms, this constitutes a complete orthogonal system for the functions on the unit interval.</p>
<p>My understading is that the $w'(t)$ that you are looking for, is the projection of your signal $w(t)$ onte the subspace generated by the first $\psi_{n,k}$.</p>
http://mathoverflow.net/questions/63144/decomposing-a-discrete-signal-into-a-sum-of-rectangle-functions/63199#63199Answer by Brian Borchers for Decomposing a discrete signal into a sum of rectangle functionsBrian Borchers2011-04-27T17:14:23Z2011-04-27T17:14:23Z<p>You can formulate this problem as a 0-1 mixed integer linear programming problem. Whether this formulation would be of any practical use depends a lot on the length of the signal $w$. You can also relax the integer linear programming formulation to get a convex optimization problem that might be much more easily solvable and that might yield a practically useful solution. </p>
<p>Suppose that you're given a signal $w$ of length $m$. That is, $w$ is a vector in $R^{m}$. </p>
<p>Your goal is to minimize $\| w - w' \|_1$. </p>
<p>Construct a 0-1 matrix $A$ with each column consisting of a run of 0's, followed by a run of 1's, followed by a run of 0's. These columns are the rectangular basis signals out of which your signal will be built. There are $n=C(m,2)$ such columns (simply choose the start and end of the run of 1's.) We'll express $w'$ in terms of these basis vectors by using the constraint $w'=Ax$. </p>
<p>Now, your problem can be written as</p>
<p>$
\min \| w-w' \|_{1}
$</p>
<p>subject to</p>
<p>$
w'=Ax
$</p>
<p>$
\| x \|_{0} \leq N.
$</p>
<p>Here $\| x \|_{0}$ denotes "the number of nonzero entries in the vector $x$." It's not really a p-norm, but this notation is commonly used by folks in compressive sensing. </p>
<p>At this point, there are a couple of options. </p>
<p>An exact formulation of the sparsity constraint can be done using $n$ 0-1 integer variables $y_{i}$, $i=1, 2, \ldots n$. Pick a huge constant $M$, add the constraints </p>
<p>$
| x_{i} | \leq My_{i} \;\; i=1, 2, \ldots n,
$</p>
<p>and write $\| x \|_{0} \leq N$ as </p>
<p>$
\sum_{i=1}^{n} y_{i} \leq N.
$</p>
<p>In practice this won't be a very useful formulation unless $m$ is quite small, since $n=C(m,2)$ grows as the square of $m$. Integer programming problems with thousands of variables aren't practically solvable in most cases, although if your $N$ is very small it might help in making the problem easier for a branch and bound solver. </p>
<p>An alternative is to relax the sparsity constraint as is commonly done in compressive sensing. You'd replace the 0-norm constraint on $x$ with a constraint like
$\| x \|_{1} \leq \delta$
where $\delta$ would be adjusted until you got a suitably sparse solution. If you go this route you should probably normalize the columns of the $A$ matrix. </p>
<p>Theoretical results on compressive sensing by $L_{1}$ regularization require matrices with special properties (e.g. RIP) that this $A$ matrix simply won't have. Thus you shouldn't expect to be able to get any of the nice theoretical results to apply to your problem. </p>
http://mathoverflow.net/questions/63144/decomposing-a-discrete-signal-into-a-sum-of-rectangle-functions/63548#63548Answer by rodrigob for Decomposing a discrete signal into a sum of rectangle functionsrodrigob2011-04-30T17:59:40Z2011-04-30T18:12:46Z<p>Dear Raskolnikov, Brian and Sebastian,
thanks you very much for sharing your ideas. </p>
<p>I cross posted my question in two forums, so you can check the complete discussion by looking <a href="http://math.stackexchange.com/questions/35388" rel="nofollow">here</a> and <a href="http://mathoverflow.net/questions/63144" rel="nofollow">here</a>.</p>
<p>From the discussion I got three main elements:</p>
<ol>
<li>Haar wavelet provide a solution to my problem, although a very suboptimal one</li>
<li>This problem could be solved via an approach similar matching pursuit (but the full enumeration of the dictionary of bases needs to avoided, because it is too large in my problem)</li>
<li>Sebastian Reichelt provided code for an approach based on maximum and minimum subarray search</li>
</ol>
<p>All and all I ended up with four different solutions to the presented problem:</p>
<ol>
<li><code>explore_a</code> indicates my original solution suggested in question (quantizing the $a_i$ values and solving multiple maximum subarray problems)</li>
<li><code>brute_force</code> indicates the approach based on approximating $\left|w' - w\right|$ by $\left(w' - w\right)^2$. In this approach, it turns out that each rectangle possibility can be evaluated with only two memory reads, one multiplication and one division, making a brute force exploration tractable.</li>
<li><code>reichelt</code> indicates a port of Sebastian's code to python (which I used to test the ideas)</li>
<li><code>abs_max</code> indicates an approach where at each iteration a rectangle of a single element, placed at the maximum absolute value of the signal, is selected.</li>
</ol>
<p>All the <a href="http://lts.cr/add" rel="nofollow">code implementing these methods</a> and some example results are available at <a href="http://lts.cr/Hzg" rel="nofollow">http://lts.cr/Hzg</a></p>
<p>At each run the code will generate a new "random" signal (with a noisy step). An example signal (labeled <code>a[0:n]</code>) and the first rectangle selected by each method can be seen below.
<img src="http://lts.cr/adL" alt="Example signal"></p>
<p>Then each method is run recursively to approximate the input signal, until the approximation error is very low or the maximum number of iterations have been reached.</p>
<p>At typical result can be seen below (and another <a href="http://lts.cr/adN" rel="nofollow">here</a>)
<img src="http://lts.cr/adT" alt="Example result 1"></p>
<p>After running the code multiple time (for different random signals), the behavior seems consistent:</p>
<ol>
<li>Unsurprisingly, <code>abs_max</code> reaches convergence in a number of iterations equal to the signal length. It does so quite fast (exponential decay).</li>
<li><code>explore_a</code> decrease the energy fast initially and then tends to stagnate.</li>
<li><code>reichelt</code> is consistently worse than <code>explore_a</code>, getting stuck at a higher energy level. I hope I did not do any dumb mistake in the port from C to Python. By visual inspection the <a href="http://lts.cr/adL" rel="nofollow">first rectangle</a> <a href="http://lts.cr/adP" rel="nofollow">selected</a> seems reasonable.</li>
<li><code>brute_force</code> is consistently the method that decreases the energy the fastest. It is also consistently intersects the <code>abs_max</code> solution, which indicates that a better strategy would be to switch from one method to the other.</li>
</ol>
<p>Obviously the exact behavior changes from run to run and would certainly change depending on the method used to generate the "random" signal. However I believe these initial results are a good indicator. I feel that it is reasonable now to proceed to generate my real data, and evaluate how well/fast <code>brute_force</code> and <code>explore_a</code> run there.</p>
<p>Feel free to add comments or play around with the code. </p>
<p>Again, thank you very much for your inputs and insights ! </p>