Wednesday, November 26, 2008

Python: draw an ellipse

The following Python script draws an ellipse.
from numpy import linspace
from scipy import pi,sin,cos


def ellipse(ra,rb,ang,x0,y0,Nb=50):
'''ra - major axis length
rb - minor axis length
ang - angle
x0,y0 - position of centre of ellipse
Nb - No. of points that make an ellipse

based on matlab code ellipse.m written by D.G. Long,
Brigham Young University, based on the
CIRCLES.m original
written by Peter Blattner, Institute of Microtechnology,
University of
Neuchatel, Switzerland, blattner@imt.unine.ch
'''
xpos,ypos=x0,y0
radm,radn=ra,rb
an=ang

co,si=cos(an),sin(an)
the=linspace(0,2*pi,Nb)
X=radm*cos(the)*co-si*radn*sin(the)+xpos
Y=radm*cos(the)*si+co*radn*sin(the)+ypos
return X,Y

def test():
import pylab as p

fig = p.figure(figsize=(5,5))
p.axis([-3,3,-3,3])

#eg 1
X,Y=ellipse(2,1,pi*2.0/3.0,0,1)
p.plot(X,Y,"b.-",ms=1) # blue ellipse

#eg 2
X,Y=ellipse(2,0.2,pi/3.0,1,1)
p.plot(X,Y,"r.-",ms=1) # red ellipse

#eg 3
X,Y=ellipse(1,1,pi/3.0,-1,1,Nb=16)
p.plot(X,Y,"g.-",ms=1) # green ellipse

p.grid(True)
p.show()


if __name__ == '__main__':
test()

Note

The script is in ellipse.py.

Wednesday, November 05, 2008

Python: os.system returns 32512

The os.system() function executes operating systems's command. When the functin returns code 32512, it means that the command has not been found. One way to make it work, is to use the full path to the command.
En example:

Wednesday, October 22, 2008

Matlab: passing Cell into ansi C mex file

Let the test cell defined in matlab betest_cell={1,[2,3],[5,6,7;8,9,10;11,12,13]};

To get these values in mex C file we can write in functionvoid mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] ) {

const mxArray* temp_cell;
mxArray* cells[3];

temp_cell=prhs[0]; //cell is the only variable
//passed to mex file.
cells[0]=mxGetCell(temp_cell,0);
cells[1]=mxGetCell(temp_cell,1);
cells[2]=mxGetCell(temp_cell,2);

mexPrintf("%f,", *mxGetPr(cells[0]));
mexPrintf("\n{%f,%f}", *mxGetPr(cells[1]),
*(mxGetPr(cells[1])+1));

int N;
N= mxGetM(cells[2]);
double **Img = makeMatrixFromVector(mxGetPr(cells[2]),N);


int i,j;
for (i=0;i<N;i++) {
mexPrintf("\n");
for (j=0;j<N;j++) {
mexPrintf("\t%.1f ",Img[i][j]);
}
}
mexPrintf("\n")


}

This should give in matlab console:1.000000,
[2.000000 3.000000]
5.0 6.0 7.0
8.0 9.0 10.0
11.0 12.0 13.0

makeMatrixFromVector is defined as follows:double ** makeMatrixFromVector(double *inData,int size) {
int x,y;
double ** Img = (double**)mxMalloc(size*sizeof(double*));
for (y=0 ; y< size ; y++) {
Img[y]=(double*) mxMalloc((size)* sizeof(double));
for (x=0 ; x< size ; x++) {
Img[y][x]=*(inData+x*size+y);
}
}
return Img;
}

Monday, September 22, 2008

sed: substitute text examples

En example

Substitute text1 with text2 in file /etc/apt/sources.list:
cat /etc/apt/sources.list | sed 's/text1/text2/g' > out.txt
or with different 'slash':cat /etc/apt/sources.list | sed 's|text1|text2|g' > out.txt
Multiple substitution: cat /etc/apt/sources.list | sed -e 's/text1/text2/g' -e 's/text3/text4/g' > out.txt

Delete lines containing 'blabla' string:cat some.txt | sed -e '/blabla/d' > out.txt

Sunday, August 24, 2008

VirtualBox: backup VDI using clonevdi tool

To list virtual drives:
VBoxManage list hdds
You must get UUID of virtual drive you want to clone. As an egzamples lets assume that UUID is 973b3243-8168-41c3-bb29-1c9865eaec7c. Having this, one executes clonevdi command as follows:
VBoxManage clonevdi 973b3243-8168-41c3-bb29-1c9865eaec7c outfilename.vdi I noticed that when coping VDI to new Vbox, networking does not work good.
I managed to repair this by restarting it (my guest is Linux):
/etc/init.d/networking restart
Also sometimes this failed, because the command was restarting wrong network interface, e.g. eth4 instead of eth2. This can be change in /etc/network/interfaces

Tuesday, August 12, 2008

ubuntu-server: How to enabling public_html folder

To enable public_html folder in users home directory use the following:sudo a2enmod userdir

Saturday, June 21, 2008

Python: Isotropic fractal surface generator

#! /usr/bin/env python
from __future__ import division
import Image
from scipy import *

class FractalSurface(object):
'''Generate isotropic fractal surface image using
spectral synthesis method [1, p.]
References:
1. Yuval Fisher, Michael McGuire,
The Science of Fractal Images, 1988
'''

def __init__(self,fd=2.5, N=256):
self.N=N
self.H=1-(fd-2);
self.X=zeros((self.N,self.N),complex)
self.A=zeros((self.N,self.N),complex)
self.img=Image.Image()

def genSurface(self):
'''Spectral synthesis method
'''
N=self.N; A=self.A
powerr=-(self.H+1.0)/2.0

for i in range(int(N/2)+1):
for j in range(int(N/2)+1):

phase=2*pi*rand()

if i is not 0 or j is not 0:
rad=(i*i+j*j)**powerr*random.normal()
else:
rad=0.0

self.A[i,j]=complex(rad*cos(phase),rad*sin(phase))

if i is 0:
i0=0.0
else:
i0=N-i
if j is 0:
j0=0.0
else:
j0=N-j

self.A[i0,j0]=complex(rad*cos(phase),-rad*sin(phase))


self.A.imag[N/2][0]=0.0
self.A.imag[0,N/2]=0.0
self.A.imag[N/2][N/2]=0.0

for i in range(1,int(N/2)):
for j in range(1,int(N/2)):
phase=2*pi*rand()
rad=(i*i+j*j)**powerr*random.normal()
self.A[i,N-j]=complex(rad*cos(phase),rad*sin(phase))
self.A[N-i,j]=complex(rad*cos(phase),-rad*sin(phase))

itemp=fftpack.ifft2(self.A)
itemp=itemp-itemp.min()
self.X=itemp


def genImage(self):
#Aa=abs(Aa)
Aa=self.X
im=Aa.real/Aa.real.max()*255.0
self.img=Image.fromarray(uint8(im))
#img2=Image.fromstring("L",(N,N),uint8(im).tostring())

def showImg(self):
self.img.show()

def saveImg(self,fname="fs.tiff"):
self.img.save(fname)

def getFSimg(self):
return self.img

def main():
fs=FractalSurface()
fs.genSurface()
fs.genImage()
fs.saveImg()
fs.showImg()

if __name__ == '__main__':
main()

Example


FD=2.1




FD=2.5




FD=2.9

Tuesday, June 03, 2008

ubuntu: iptables port redirect

I want to redirect all incoming requests on port 80 to 8080. I did it using the following command:sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
Following this operation my iptables -L wasChain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

The iptables -t nat -L was:Chain PREROUTING (policy ACCEPT)
target prot opt source destination
REDIRECT tcp -- anywhere anywhere tcp dpt:www redir ports 8080

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

I saved these setings using sudo iptables-save.

Note:
This works only when some other computer tries to connect to port 80. If I tried to connect from the same server (i.e. localhost) it did not work. The reason for now is unknown, but it works, an this is good.

Thursday, April 10, 2008

bash: simple loop through files

for f in *.tiff ; do echo $f; done
Example with ImageMagick's convert program: for f in *.tiff; do convert -crop 384x384+48+35 $f out/$f; done