We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › push data through a matrix
Page Index Toggle Pages: 1
push data through a matrix (Read 2444 times)
push data through a matrix
Dec 21st, 2009, 8:59am
 
I want to move data in a matrix.
As you see in the code down here i do it field by field (z[i][j]=z[i][j-1])
It works, but it doesn’t seem to be very efficient.  Sad
Is there a Lib or some commands to do this in a faster or maybe just in an easier way?

Thank you and merry Christmas!

Code:
void moveit(){
 for(int i=0;i<B;i++){
   for(int j=L-1;j>0;j--){
     z[i][j]=z[i][j-1];
   }
 }
}
Re: push data through a matrix
Reply #1 - Dec 21st, 2009, 9:15am
 
I don't think there are many ways of doing it more efficiently than this.
Unless you are going to use processor specific instructions or if you are going to assume that the matrix is from a specific type (sparse, ..).

Can you tell us more about the size, usage and contents of the matrix?
Re: push data through a matrix
Reply #2 - Dec 21st, 2009, 9:42am
 
Matrix is [1024 x 150].
I put FFT data in the first line and render the whole matrix to an fft-diagram.
The diagram should scroll forward one step per loop.

The matrix is an float array (float [][] z = new float[1024][150]; )
Re: push data through a matrix
Reply #3 - Dec 21st, 2009, 10:35am
 
The only possible optimization I see (while using plain array) is to change access to array so it wraps around.
The idea is to set an origin in the array. Instead of access [0], you access the origin, and at [n] you access origin+n or n-arrayLen if you overflow (if origin+n>arrayLen).
Not sure if there is a real gain of speed if you do lot of access vs. shift rate (vs. array size). The only way to know is to test and see if there is an improvement!

Another way is to use a Java Collection: it will cost memory but shifting will do only a couple of reference changes.
Re: push data through a matrix
Reply #4 - Dec 21st, 2009, 1:15pm
 
Indeed, something like a linked list (http://java.sun.com/j2se/1.4.2/docs/api/java/util/LinkedList.html) would work well here.
Re: push data through a matrix
Reply #5 - Dec 22nd, 2009, 1:52am
 
Or a Queue.
Re: push data through a matrix
Reply #6 - Dec 22nd, 2009, 2:05am
 
PhiLho  wrote on Dec 22nd, 2009, 1:52am:
Or a Queue.


A Queue is an interface, and LinkedList implements it.
Re: push data through a matrix
Reply #7 - Dec 22nd, 2009, 2:42am
 
You are right! I was more thinking of ArrayDeque (Java 1.6 only!), said to be "faster than LinkedList  when used as a queue".

But anyway, I think my idea of movable origin in a plain array is probably faster, avoiding boxing/unboxing. But a little harder to write, debug and understand... Smiley
Re: push data through a matrix
Reply #8 - Dec 22nd, 2009, 2:51am
 
Yup, good point.

And yeah, a moving origin is definitely the faster way to go, but it produces more difficult code and speed gain isn't that great i think.
I'm not sure though Smiley
Re: push data through a matrix
Reply #9 - Dec 22nd, 2009, 8:56am
 
Thank you guys,
i check the idea of movable origin, still got no idea how it works...but i will work it out.
Meanwhile i did some improvements to my code...precalc the Lin->dB stuff bla bla bal...it helps a lot because the log() thing sucks a lot of power on this big array.

btw: i work on a thing like this http://www.youtube.com/watch?v=bII9fjpnZ6k&NR=1...but much prettier!  Wink
Re: push data through a matrix
Reply #10 - Dec 22nd, 2009, 9:45am
 
I have not tested it and it is not complete, but it shows the general idea:

Code:

class FFTRingBuffer
{
 float[][] fftData;
 int origin;
 
 public FFTRingBuffer(int fftSize, int bufferSize)
 {
   fftData = new float[bufferSize][fftSize];
 }

 void add(float[] fft)
 {
   fftData[origin] = fft;
   origin = (origin+1)%bufferSize;
 }
 
 float get(int i, int j)
 {
   return fftData[(origin+i)%bufferSize][j];
 }
}
Re: push data through a matrix
Reply #11 - Dec 23rd, 2009, 11:06am
 
Hi JR,

What means the "%" in the syntax?
Origin = (origin+1)%bufferSize;

I’m not really sure if i get it...but does this idea "move" the date?
If i name the fft i put into the array fft0(x) (x=position in the array) i want to move the date like this:
step 0: fft0(0)
step 1: fft0(1);fft1(0)
step 2: fft0(2);fft1(1);fft2(0)

Or is it like this:
data is put in the array at counting up position:
fft1;fft2;fft3;fft4;fft5;.....;fftn
and it just translate the beginning position of the diagram:
step 0: plot from 0 to fftsize
step 1: plot from 1 to fftsize+1
step n: plot from n to fftsize+n
if the plot reaches the end of the array it starts at the beginning again.

I like the 2nd idea, because i don’t have to move data...just move the origin of the diagram...make sense...did you mean this?
Re: push data through a matrix
Reply #12 - Dec 23rd, 2009, 2:15pm
 
kspksp wrote on Dec 23rd, 2009, 11:06am:
What means the "%" in the syntax

You can find % (modulo) by searching % in the Language reference index page.
Re: push data through a matrix
Reply #13 - Dec 23rd, 2009, 3:35pm
 
kspksp wrote on Dec 23rd, 2009, 11:06am:
I like the 2nd idea, because i don’t have to move data...just move the origin of the diagram...make sense...did you mean this


Exactly Smiley
Re: push data through a matrix
Reply #14 - Dec 25th, 2009, 11:05am
 
I did the changes...and it runs pretty well!  Cheesy

Now i got a new problem...the rendering of the 102400 faces needs to much cpu...or gpu...don't care (2^10*100 faces=102400 -> 2^10=1024=fftsize, 100=Buffersize)..i use Opengl...but ok..thats another story..

Thank you guys for the tip, specially JR!

Merry Christmas and a happy new year!
Page Index Toggle Pages: 1