'Slow' python script ... or am i very demanding?

Hi there.

I made a script that reads in a 8x8 floating point value 'matrix' or, in fact, a list of length 64, over serial.
This works perfectly fine: 10 fps, which is the max rate of the connected sensor.

Thats ok. What is not ok: i bilinearly interpolate the matrix in the linino part using python, until i have a 57x57 matrix.
On the linino, this results in a frame drop towards a dramatic 1-1.5 fps. Apperently it is too cpu-unfriendly in python for the linino to handle this properly, although in my humble opinion, the code is not particularly heavy.

Below is the piece of code used for the iteration proces, as illustration. It is part of a custom made Matrix class, in which i 'simulate' matrix properties. In fact, it is just a 1D list, but using the class allows me to do stuff such as set/getpixel(y,x)
The class itself is called Mat, and is initialized with Mat(ydimension,xdimension)

def interpolate(self,iterations):
	tmpmat1=None
	tmpmat2=None
	#Function to interpolate a matrix
	#First copy self to matrix that is being used in iteration
	tmpmat1=self 
	
	for i in range(0,iterations):
		#create a matrix in the size of a 1-time interpolated matrix (ynew=yold*2-1  xnew=xold*2-1)
		tmpmat2=Mat(tmpmat1.rows*2-1,tmpmat1.columns*2-1)
		#Fill with zeros
		tmpmat2.setZero()
	
		#Project the original points onto the interpolated size matrix
		for y in range(1,tmpmat1.rows+1):
			for x in range(1,tmpmat1.columns+1):
				tmpmat2.setpx((y-1)*2+1,(x-1)*2+1,tmpmat1.getpx(y,x))
		
		#Fill the gaps in the rows
		for y in range(1,tmpmat2.rows+1,2):
			for x in range(2,tmpmat2.columns,2):
				tmpmat2.setpx(y,x,(tmpmat2.getpx(y,x-1)+tmpmat2.getpx(y,x+1))/2)
		
		#Fill the gaps in the columns
		for y in range(2,tmpmat2.rows,2):
			for x in range(1,tmpmat2.columns+1):
				tmpmat2.setpx(y,x,(tmpmat2.getpx(y-1,x)+tmpmat2.getpx(y+1,x))/2)
		
		tmpmat1=tmpmat2 #
		del tmpmat2 #needed?
		return tmpmat1

I tried to use NumPy as i assume this library is much optimized for this kind of stuff, but unfortunately numpy is not available on the yun and i cant get it to work either (can be my fault).

Does anyone have a suggestion? I fear that the plain python is just a bit too slow for large iterations

Arduino TRE

Thanks to the 1-GHz Sitara AM335x processor, Arduino developers get up to 100 times more performance with the Sitara-processor-based TRE than they do on the Arduino Leonardo or Uno.
It could load Ubuntu: The world's most popular free OS.
CPU run twice faster than Raspberry Pi and few times faster than Yun.
512 MB RAM at Tre v.s. 64 MB RAM at Yun
TRE has same Microcontroller as Yun, Atmel ATmega32u4 which allow you connected with sensor.

The Arduino TRE is expected to be available in spring 2014.

Have you tried it without the usage of float? Depending on the precision you need, you could multiply your incoming data by 100 or 1000, do the math, and devide back.
Not sure I this is possible or will improve things.

mamu:
Have you tried it without the usage of float? Depending on the precision you need, you could multiply your incoming data by 100 or 1000, do the math, and devide back.
Not sure I this is possible or will improve things.

The CPU at Yun has no FPU, anything to deal with float might be slow. this could be work around.

The CPU at TRE has NEON floating-point accelerator, will lift the limit.

Further speed up;-

Python Just-in-Time compiler PyPy

Speed
The current implementation of NumPy on PyPy is reasonably fast - it ranges from roughly the same speed to 2-5x faster for stacked large array operations to 100-300x for accessing NumPy array elements one by one. The exact speed depends very much how NumPy is used, but the target would be to be within an order of magnitude from handwritten C. To achieve this, we would need to teach our JIT backends how to use modern vector instructions, like SSE or AVM. Hence, we split the proposal into two parts, first part covers compatibility with a reasonable approach to keeping current speed achievements, second part is about teaching the JIT how to vectorize certain operations, which should bring PyPy's NumPy as a very competitive tool compared to other available solutions for numerical computations, like matlab or C++ array libraries.

http://pypy.org/numpydonate.html