Go Down

Topic: Some (two) useful formulas that I cannot find on the internet (Read 776 times) previous topic - next topic

nikkoderic

When I was working on a code on scanning matrix piano keyboard, I had the need to find an element number based on the row, column and column size.

For example:
Find the order (left to right) of the element at (2,4) in a 5 columns table.
//(row, column)

[1, 5, 3, 7, 9]
[2, 4, 5, 6, 7]
[1, 3, 5, 3, 2]

We know that the value at (2, 4) is "6" but how do we know its order (if it's the 1st element, 2nd, 3rd... etc...) in the table?

I painstakingly tried to "find" the formula for this (cause I can't find it on the internet and I don't know what the keyword is) but here you go.

// Finding the element number (or order) by row and column
// ---------------------------------------------------------
// E(number) = C(total) * R - ( C(total) - C )
// -----
// E(number) - element number of the given row and column (from left to right)
// C(total) - total column of the table
// R - row number of the element
// C - column number of the element

And another useful formula that I "created" (or formulated)...
I cant explain it though. English is not my native language. But I will call it...

"Delay to MIDI Velocity Value For Velocity Sensitive Keyboard That Uses Two Switches And Determine The Time Difference Of Those Switches To Know How Fast/Hard The Key Was Struck"

// Finding the equivalent velocity using the given maximum delay and determined delay of a key
// -------------------------------------------------------------------------------------------------
// E(velocity) = ( ( D(maximum) - D(key) ) / D(maximum) ) * 127
// -----
// E(velocity) - resulting velocity from the given delay
// D(maximum) - maximum delay to be considered as 0 velocity
// D(key) - given delay of the key
// 127 - maximum midi note on velocity
//

Grammar and mathematical corrections are very very welcome.

el_supremo

If the number of columns in the array is N and you want the (i,j) element (counting them as you showed - not as in C) the order is:
Code: [Select]
(i-1)*N + j
which for (2,4) gives 9.


Pete

nikkoderic

#2
Jun 17, 2013, 07:06 pm Last Edit: Jun 17, 2013, 07:21 pm by nikkoderic Reason: 1
   1  2  3  4  5
1 [1, 5, 3, 7, 9]
2 [2, 4, 5, 6, 7]
3 [1, 3, 5, 3, 2]


9 because the element at (2, 4) (which is number "6") is at the 9th order from left to right. I don't know if I'm using the correct term "order" but it is something like...

   1         2       3       4        5
1 [1st,   2nd,    3rd,   4th,   5th]
2 [6th,   7th,    8th,   9th,  10th]
3 [11st, 12nd, 13rd,  14th, 15th]

Woooh! I really do hate math but I think the equation is good for now.

Useful for two dimensional arrays i think.

robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Fexduino

Here's a snippet of my code:
Code: [Select]

int LEDMatrix::Get(int x, int y)
{
int offset = (y * rows) + x;
return *TranslationBuffer[offset];
}


I'm using a LED strip arranged as a LED matrix that is wired "zig-zag". Zig-zag meaning that each odd row is reversed. Here's the code that I wrote to create a pointer array that takes this into account.

Code: [Select]

void LEDMatrix::InitTranslationBuffer()
{
BackBuffer = (int*)malloc(leds * sizeof(int));
FrontBuffer = (int*)malloc(leds * sizeof(int));
TranslationBuffer = (int**)malloc(leds * sizeof(int*));

for (int i = 0; i < leds; i++)
{
int y = i / rows;
int x = i % cols;

if (y % 2 == 1)
{
int b = ((y + 1) * rows) - x - 1;
TranslationBuffer[i] = &FrontBuffer[b];
}
else
{
TranslationBuffer[i] = &FrontBuffer[i];
}
}
}

nikkoderic


Mind you, arrays in C are zero based.


Adjust accordingly hahahaha! Maybe add 1 to the rows and columns before using the equation.

----

Yeah, for those who will be using the equation, just be reminded that arrays starts with zero. You know how to adjust "accordingly" right? =) Arrays starting with zero just confuses me sometimes when my codes get complicated.

Fexduino

Don't. Rewrite your equations to incorporate this notion. Look at my implementation for calculating the array index. You generally don't want to be adding and subtracting to support your equation but the other way around. On an Arduino this can become even more important because you want to minimize the processor load as much as you can (saves time and power).


nikkoderic

But I will leave it up to them. I mean, not all mathematical formulas are written solely for programmer's use. Programmers should also learn how to manipulate equations that are not meant for programming (like the y=mx+b) to fit their programming needs. I admit that I'm just lazy rewriting the equation for that "starting with zero" dilemma (if i used the term right). I don't think adding and subtracting that small would greatly affect the processor speed but yeah it's a good practice. Also, you know the official name of the equation? I can't find it on the internet.

Go Up