zeabrah’s lair

7 February, 2011

A’trous 2D wavelet decomposition in python using SciPy

Filed under: dev — Tags: , , — alexeybrazhe @ 7:21 pm

Simple 2D A’trous wavelet decomposition using B3 spline in python with SciPy:

_b3spline1d = np.array([1./16, 1./4, 3./8, 1./4, 1./16])
__x = _b3spline1d.reshape(1,-1)
_b3spl2d = np.dot(__x.T,__x)

def atrous2d(arr, lev, kernel=_b3spl2d, boundary='symm'):
    "Do 2d a'trous wavelet transform with B3-spline scaling function"
    approx = signal.convolve2d(arr, kernel,
                               mode='same',
                               boundary=boundary)  # approximation
    w = arr - approx                               # wavelet details
    if lev <= 0: return arr
    if lev == 1: return [w, approx]
    else:        return [w] + atrous1(approx,lev-1, upscale(kernel), boundary)

5 March, 2010

image-funcut

Filed under: dev, Uncategorized — Tags: — alexeybrazhe @ 12:41 pm

I have started and been developing a software project for image series
analysis, called image-funcut. I now realise that the name for it is
quite unfortunate, as it is too prone for rather bad spoonerisms. Or may
be it’s a virtue?

14 June, 2008

leakage in R-chain

Filed under: dev — Tags: , — alexeybrazhe @ 11:01 pm

From time to time I will post here some little code snippets from what I write.
All code is under GPL ;)

This time it is a leakage resitance calculator:


(defun make-R-elem (R1 R2)
  (list R1 R2))

(defun axial-R (elem)
  (car elem))

(defun leak-R (elem)
  (cadr elem))

(defun make-R-chain (axial leak &key (number 10))
  (do ((chain nil)
       (n 0  (1+ n)))
      ((> n number) (nreverse chain))
    (push (make-R-elem axial leak) chain)))

(defun leak-resistance (chain)
 "Leakage resistanse of the repeated  circut:
       o---/\/\/\---+------o
         R1 (axial) |
                    /
                    \
                    / R2 (leakage)
                    \
                    /
                    |
                   --- Gnd
   elements are in a list: ((R1 R2) (R1 R2))
  "
  (let ((elem (car chain)))
   (if (null (cdr chain))
    (+ (axial-R elem) (leak-R elem))
    (+ (axial-R elem)
       (/ 1.0 (+ (/ 1.0 (leak-R elem))
                 (/ 1.0 (leak-resistance (cdr chain)))))))))

5 June, 2008

New test

Filed under: Uncategorized — Tags: , , — alexeybrazhe @ 6:51 pm

Let’s try it once again.

At least, tags work now. That’s something.

Blogging from emacs

Filed under: emacs — Tags: , — alexeybrazhe @ 11:48 am

Let’s see if it works.

Update: It does. However, the tags/categories do not work and I don’t know how to fix it.

I added tags and categories from the browser now. Pity

23 May, 2008

On how to define an ODE system in matplotlib

Filed under: dev — Tags: , , , , , — alexeybrazhe @ 8:27 pm

Recently, I needed to make some plots with the solution of a system of ordinary differential equations (ODE) for a textbook. I know a great tool to easily make beautiful scientific plots — matplotlib/pylab (www.matplotlib.sf.net). A very simplistic function for integrating ODE comes with it — rk4. I know, that there are better tools for python provided with scipy, but it is not installed on my workstation (which I don’t have root access to), I am too lazy to compile it locally.

OK, so we have this rk4 function. It takes 3 arguments: a function dx/dt = f(x,t), x0 (x at time zero) and time points, t. In the case of a system of ODEs, y x, x0 and t are vectors of floats. First argument, a function, can take only two arguments: x and current time. Not that much flexibility, eh?

Naturally, one would want to set some parameters for the ODE system. How to capture/pass them? First, I saw the solution to make a class, which would store parameters and provide a method for integration:

## -- `Dumb class style' --
## Call example:
##
##>> fhn_slow_instance = FH_N_slow()
##>> rk4(fhn_slow_instance.system, state_zero, time_vector)
##
## where rk4 is a simple ODE integrator from pylab/matplotlib

class FH_N_slow:
    def __init__(self, I = 0.6, p=(1.0, 1.7, 0.5)):
        self.I = I
        self.p = p
    def dv(v,w): return v - v**3/3. - w + self.I
    def dw(v,w): return 0.08*(polyval(self.p,v) - w)
    def system(self, state, time):
        v,w = state
        return self.dv(v,w), self.dw(v,w)

FH_N_slow — is a modification of the FitzHugh-Nagumo model of a firing neuron which allows for long interspike intervals thanks to parabolic nullcline for w and a `narrow’ channel between the two nullclines. The original idea of that was coined by Hindmarsh and Rose some 20+ years before, but they had a strange form of the FitzHugh equations (not like the canonical representation).

This example seems to be OK, but can we simplify it? It seems natural to define a function within a closure of parameters to capture them. So, we need closures. I ended up with something like this:

## -- `Functional style' --
## Doesn't it look like a closure in Lisps?
## Call example:
##
##>> rk4(fhn_slow1(), state_zero, time_vector)

def fhn_slow1(I=0.6, p = (1.0,  1.7,  0.5)):
    def dv(v,w): return v - v**3/3. - w + I
    def dw(v,w): return 0.08*(polyval(p,v) - w)
    def system(state, time):
        v,w = state
        return dv(v,w), dw(v,w)
    return wrapper

Our function fhn_slow1 returns another function, closed over the values for I and p.
This looks neat to me.

And here is the phase portrait and the time realisation of the v (membrane potential) variable:

Phase portrait (left) and time realisation (right). Note the `narrow channel\' between the two nullclines

9 May, 2008

BO test

Filed under: Uncategorized — alexeybrazhe @ 12:30 pm

The mostly robust statistical test is a ‘BO’ test. BO stands for ‘bloody obvious’.

26 April, 2008

An interesting interview with D. Knuth

Filed under: Uncategorized — Tags: , , — alexeybrazhe @ 8:21 pm

I’ve recently read a very interesting interview with D. Knuth at

http://www.informit.com/articles/article.aspx?p=1193856

A thing to think about: literate programming.

9 April, 2008

Filed under: Uncategorized — Tags: , — alexeybrazhe @ 4:52 pm

“sometimes it takes a tough man to make a tender chicken”

25 March, 2008

A test note from n800

Filed under: Uncategorized — Tags: — alexeybrazhe @ 2:19 pm

I have recently acquired a n800 tablet. Lots of fun!

Older Posts »

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.