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 › draw values of an 2D array
Page Index Toggle Pages: 1
draw values of an 2D array (Read 600 times)
draw values of an 2D array
Feb 6th, 2010, 1:30am
 
hi,

i've a little problem.
i have a 2D array, i ad every frame an value to this array.
now i whant to draw this array.
the array has 500 col's and 15 row's, each col has 15 row's.
by row is y-axis, and col is the x-axis.

Code:
int colmax = 500;
int rowmax = 15;
int[][] sgram = new int[rowmax][colmax];
int[] sgram2 =new int[rowmax];

int col;
int leftedge;
int b;
int currMaxVal;
int maxValPos;

for(int i = 0; i < rowmax ; i++)
 {
   sgram2[i] = (int)Math.round(100*fft.getBand(i));
   if ((int)Math.round(fft.getBand(i)) > (currMaxVal)){currMaxVal=((int)Math.round(fft.getBand(i)));
   maxValPos = i;
   }
   }
 noStroke();
  colorMode(HSB, 140);

  color a = color(maxValPos, currMaxVal, currMaxVal);
   
   noStroke();
 
 currMaxVal =0;
 maxValPos = 0;

sgram[b][col] = (a);

  b=b+1;
 if (b==rowmax){b=0;

 // next time will be the next column
 col = col + 1;
 // wrap back to the first column when we get to the end
 if (col == colmax) { col = 0; }
}


i will draw the y-axis everytime i ad a new value.
when the row iss full, i will shift it 1 step left and begin to draw a new row.

i tried it like "full spectrum analyz" sketch. but it dont realy work fine.
Code:
// Draw points.  
 // leftedge is the column in the ring-filled array that is drawn at the extreme left
 // start from there, and draw to the end of the array
 for (int i = 0; i < colmax-leftedge; i++) {
   for (int j = 0; j < rowmax; j++) {
stroke(sgram[j][i+leftedge]);
point(i,height-j);
   }
 }
 // Draw the rest of the image as the beginning of the array (up to leftedge)
 for (int i = 0; i < leftedge; i++) {
   for (int j = 0; j < rowmax; j++) {
stroke(sgram[j][i]);
point(i+colmax-leftedge,height-j);
   }
 }
 // Next time around, we move the left edge over by one, to have the whole thing
 // scroll left
 leftedge = leftedge + 1;
 // Make sure it wraps around
 if (leftedge == colmax) { leftedge = 0; }
}


did anybody have a sample??

regars maik

Re: draw values of an 2D array
Reply #1 - Feb 7th, 2010, 1:01am
 
wow i made it Cheesy
here my code.
Code:



import ddf.minim.analysis.*;
import ddf.minim.*;

Minim minim;
AudioInput in;
FFT fft;
int colmax = 100;
int rowmax = 25;
int[][] sgram = new int[rowmax][colmax];
float[] sgram2 =new float[256];

int col;
int leftedge;
int b;
float currMaxVal;
int maxValPos;


void setup()
{
 frameRate(63);
 size(500, 125);
 
// set colormode
colorMode(HSB, 255,255,255,255);

// get the audio
 minim = new Minim(this);
 in = minim.getLineIn(Minim.STEREO,2048);
fft = new FFT(in.bufferSize(), in.sampleRate());
 fft.window(FFT.HAMMING);
}

void draw()
{
 background(0);
 
 // perform a forward FFT on the samples in the input buffer
 fft.forward(in.mix);
 // fill array with spectrum
 for(int i = 0; i<256 ; i++)
 {
   sgram2[i] = (fft.getBand(i));
   if ((fft.getBand(i)) > (currMaxVal)){currMaxVal=((fft.getBand(i)));
   maxValPos = i;
   }
   }
   //set the color by: highest value and the freq-band
   color a = color(maxValPos,255 ,currMaxVal );
   // set values to zero
   currMaxVal =0;
   maxValPos = 0;
 //fill the last column of the array with the color
   sgram[b][99] = (a);
   // count up the rows
   b=b+1;
   // when row is full, shift array 1 step left
 if (b==rowmax){b=0;
 for(int z = 0; z < 99 ; z++){
   for(int b = 0;b < 25; b++){
   sgram[b][z]=sgram[b][z+1];
}
 }
 // count up the column
 col = col + 1;
 // wrap back to the first column when we get to the end
 if (col == colmax) { col = 0; }
 // draw the array ;)
}
for (int i = 0; i < colmax; i++) {
   for (int j = 0; j < rowmax; j++) {

fill(sgram[j][i]);
rect(i*5,height-(j*5),5,5);
   }
 }
}


void stop()
{
 // always close Minim audio classes when you finish with them
 in.close();
 minim.stop();

 super.stop();
}





regards maik
Page Index Toggle Pages: 1