Tuesday, February 26, 2013

Scapy in mininet

Scapy is a packet forging tool/library for python.
Mininet is a network emulator.
It would be nice to use them together, however when trying to use scapy in mininet namespace it throws an exception:
*IOError: [Errno 99] Cannot assign requested address*
The solution is to bring loopback interface up. Just type
mininet> xterm h2
and then in xterm
# ifconfig lo up
now you can run scapy. In invoked xterm type:

Friday, January 18, 2013

Julian date in python

I was recently looking for simple function to convert date to Julian Date in python. Julian date is number of days since November 24, 4714 BC. It's used by astronomers and by Google in datarange queries ;] I've found working, pretty and simple function here: http://code.activestate.com/recipes/117215/ Here is the code:
import datetime

def date_to_julian_day(my_date):
    """Returns the Julian day number of a date."""
    a = (14 - my_date.month)//12
    y = my_date.year + 4800 - a
    m = my_date.month + 12*a - 3
    return my_date.day + ((153*m + 2)//5) + 365*y + y//4 - y//100 + y//400 - 32045

Wednesday, November 7, 2012

Wireshark with Django development server

If You want to use Wireshark to inspect Django requests and responses be aware that wiresharks http filter isn't by default active on 8000 port. To turn it on go to Edit->Preferences->Protocols->HTTP and add 8000 to TCP ports.

It also turns out that http filter doesn't get django development servers response-codes right. Forget about filtering by http.response.code == 500. I tested it on Ubuntu 12.04 and with default WSGIRef server it didn't work at all. If you want to filter responses by response code do it another way. What worked for me is filtering by tcp.data contains "INTERNAL SERVER ERROR"

Friday, June 8, 2012

Image Wiener filter for white noise reduction

Recently I've been googling through the web to find some information about Wiener filtering out the white Gaussian noise from computer image. There is plenty of materials about Wiener filtering in general and Wiener filtering of images too. But the problem is that materials are concentrating on quite another thing. Wiener filtering in general is about performing deconvolution on previously convoluted(blured) image, with noise added after convolution(blurring) had taken place.

The problem formally is stated as ( from Wikipedia ):

x(t) - is some input signal (unknown) at time
h(t) - is the known impulse response of a linear time-invariant system (PSF - point spread function)
n(t) - is some unknown additive noise, independent of x(t)
y(t) - observed signal (input image in our case)

Our goal is to find some g(t) so we can estimate x'(t) so:

 x'(t) is our deconvoluted, de-noised image.

Long story short solution for this is in frequency domain where:

For more information go to wiki.
N(f) and S(f) in equation above are power spectrums of noise and signal.
(power spectrum is squared magnitude of Fourier transform)

But what can we do, if we don't have any H(f) ( aka. PSF - point spread function ) and we just want Wiener filter to do noise removal for us? We can replace it with 1 and simplify equation which gives us:

And where can we find power spectra of noise ( N(f) ) and signal ( S(f) )? Thing with noise is quite easy. Since it's additive Gaussian noise we just need to know it's standard deviation to compute power spectrum (which is flat).

With signal it seems to be quite problematic. You can ask: what the fuck? Do I need to know clear signal to obtain clear signal? Luckily power spectra of real images are quite alike. It shows up that you can use previously prepared spectrum to denoise unknown images with good accuracy. So, for testing purpose just take the power spectrum of sample image.

So, enough of theory and let's get to code. I've written sample program using C++ and OpenCV. Code available at github:

here are sample effects of filter:

1. Clear image:

2. Noised image ( additive Gaussian noise - --noise-stddev=50 )

3. De-noised image:

Tuesday, April 24, 2012

Python tricks

I'm taking part in Udacity course, of computer program design with Peter Norvig. Nice thing to do in your spare time. Whole course is based on Python ;) So the first Unit is over. Material didn't get hardcore yet ( I hope it will in the future ), but I've learned some python tricks.

First one is how to assign number to an item in an elegant way. Let's say we have card ranks (first udacity unit is about poker) encoded as follows: 2 3 4 5 6 7 8 9 T J Q K A, and we want to give them priorities according to their importance: 2->2, 3->3, ... , T->10, J->11, Q->12, K->13, A->14. Nothing simplier:
rank = 'Q' #our rank to check
Second trick is how to make list have only unique elements. In python it shows to be as simple as that:
my_list = [1, 1, 2, 3, 3, 3, 4, 5]
my_list = list(set(my_list))

After this my_list has only one occurrence of each value. Be aware that the ordering of elements in list may change.

Saturday, April 21, 2012

Hello World

Yaaay I'm starting my own tech blog... More info soon
my_str = 'testing code highlighting'
for i in my_str: