Experiences about Signal Processing with Numerical Python

Charles Boncelet boncelet at udel.edu
Tue Mar 14 20:41:02 EST 2000


Johannes Nix wrote:

> Hi,
>
> I have looked a little bit into Python as an alternative to do
> research about signal processing algorithms

My requirements are different from yours, but I've been using Python,
NumPy, and PIL for various projects for about 6 months or so:

1. Python and its friends make for a very rich environment.  One can
do things easily that can't be done in Matlab alone.  For instance, in
an information security research project I have combined  image processing

with encryption (currently m2crypto).  Or, (one still on the drawing
board),
automatically download data from the web and process it with Numpy, and
post the results on a web page.  Being able to do these things in the
same language/environment is terrific.

2. C loops are much faster than python loops.  If speed is a concern, try
to do as much as possible with native Numpy or PIL routines.
Unfortunately,
this sometimes means searching through the manuals to find the right
routine.  E.g, on a recent project I needed to find a ordered subset of k
items
taken without replacement from l possible items.  (Both k and l are on the

order of 1 million.)  I wrote the following:

    import random
    def permute(l,k=None):
        """return a permutation of k objects taken from l.  If k=None,
then
        all objects in l are used. l is altered in the process."""
        if k == None:
            k = len(l)
        elif k > len(l):
            raise ArgumentError, "k must be not be greater than len(l)"

        n = len(l)-1
        nfirst = len(l)-k
        while n >= nfirst:
            j = random.randint(0,n)
            l[n], l[j] = l[j],l[n]
            n = n-1
        return l[-k:]

It works fine, but is too slow.  After searching through the manuals, I
found that
the following call was available (slightly less general than above, but
adequate
for my needs):

    import RandomArray
    RandomArray.permutation(len(l))[:k]

Even though this version loops over len(l) > k internally, it is much
faster.

3. Sometimes to get speed you have to encode your algorithm in C and
import
it into Python.  I have not done this yet, but others say it is (usually)
straightforward.

4. Numpy and friends are still being built, while Matlab is a mature
product with
thousands (if not millions) of users.  This means that you still have to
build some
of your own Python based tools.  That's good if you are a tool builder,
bad if you
are a tool user. Of course, python is free and Matlab is not.

5. As for plots, I use gist for interactive things and gnuplot for
hardcopy.

    Charlie Boncelet

------
Charles Boncelet, University of Delaware,
On sabbatical at ADFA, Canberra Australia,
Home Page: http://www.ece.udel.edu/~boncelet/






More information about the Python-list mailing list