GEN~ Hadamard Matrix

Ko Sv's icon

Hi there,

I'm trying to learn a bit coding in gen~. Still not sure why not just implementing it in C/C++...
Anyway.. how would you folks implement a matrix (for example a Hadamard matrix) or array in gen~?

Any hints would be great!

Thanks, Koko

Edit: or in GenExpr?

Mihail S's icon

only the way is just manually typing or any other more elegant ways?

example of 2x2 but you can scale it to "any" size

scalefactor= 1/sqrt(2);

Out1 = scalefactor * (in1+in2);

Out2=scalefactor *(in1-in2);

Graham Wakefield's icon

If you want to generate the matrix outside of gen~ and load it in, the usual way of getting a lot of data into gen~ is via a buffer~. If you are generating it in a Jitter matrix, you can use a jit.buffer~ in the same way.

If you want to generate the matrix inside of gen~, the usual way would be store it in a Data object. Data is just like buffer~ but is internal to the gen~ patch.

E.g. for 4x8 matrix, you could declare a data mat 4 8 object, and then write to it with poke mat, where the inputs are the value, column, row. Might be better in a codebox, since you probably only want to do this at patch startup (which you can test for via the elapsed built-in variable, which counts samples since patch load).

You can read out of the matrix using peek mat with column and row inputs. You can also read a whole column at once using peek mat @channels 8.

Here's an example that fills a data with random values, which should give you the basic idea:

Data mat(4, 8);
// only run at patch load:
trig = (elapsed == 0);
if (trig) {
  // loop over the columns (data/buffer frames):
  for (c=0; c<dim(mat); c+=1) {
    // loop over the rows (data/buffer channels):
    for (r=0; r<channels(mat); r+=1) {
      // the value to write -- just random here for the demo
      value = noise();
      // write into mat
      poke(mat, value, c, r);
    }
  }
}
// output a trigger to let other parts of a patch know that the mat is ready:
out1 = trig;
Mihail S's icon

Graham thank you so much!

we've been waiting book 2 :)

any insides on it?