How to calculate a matrix? - Page 1
Forum section: electronics / projects

author: MrAureliusR
author_rank: Unknown
timestamp: January 03, 2015, 04:54:55 am
content: At least, I THINK that's what I'm asking.I'm using the Intersil ISL29125 RGB light sensor (which kicks ass, by the way). To calculate the RGB->XYZ transform (of which I only want the Y, as I understand it's essentially the Lux) they provide this little matrix:Which, if you just want the Y (Lux) part, can be simplified to this:However, after much Googling and learning about colour spaces and colour space transforms, I still can't understand how to 'solve' that matrix. I understand that each item needs to be multiplied by every other item, or something, but I don't know how it's done. Googling about matrices didn't really help, I just got a TON of random complex matrix research and other crap. I also wasn't sure how to get the C constants, but from reading about colour spaces I realize I can just use the standard RGB565 constants, at least as far as I understood it.Once I've understood the math, I can easily implement it in code, I just don't understand how to calculate matrices.Any help is greatly appreciated! I'm hoping there are some math experts out thereThanks

author: c4757p
author_rank: Super Contributor
timestamp: January 03, 2015, 05:04:38 am
content: The keyword you're looking for is "matrix multiplication".

author: T3sl4co1l
author_rank: Super Contributor
timestamp: January 03, 2015, 05:30:56 am
content: A matrix is just shorthand for a system of linear equations. You know,x + 2y + z = 0x ....... + 3z = 5and so on.Each row in the matrix equation (including the row from the "output" vector, if any) is one linear equation.All the equations, collected together, are simply grouped by coefficient and arranged into a matrix. Often in the form of Ax = b, where x and b are vectors (one column, N rows) and A is the matrix (N x N). Ax is the matrix*vector multiplication, which is simply saying, for each row of the vector x (the vector 'x' being [x; y; z] in the above case), line it up with the matrix's column of the same index, and repeat down. So, the matrix is just the coefficients, and the x-y-z are pulled out from the equation and written separately, all at once.Thus, to find one equation (one row), take that row from the matrix, and multiply it (element wise) by the x vector:[1, 2, 3][4, 5, 6] * x = 0[7, 8, 9]becomes1*x1 + 2*x2 + 3*x3 = 04*x1 + 5*x2 + 6*x3 = 07*x1 + 8*x2 + 9*x3 = 0take your pick, or, use matrix methods to solve them all at once.(note that '0' is a scalar, vector or matrix as appropriate. Whatever it is, it's always the same size and type of quantity as the left hand side, filled with zeros.)You can also have N columns and M rows, which arise for various reasons and have various properties (like possibly having a unique x,y,z solution, versus no solution (rows < cols), versus a possibly contradictory solution (rows > cols)).So, in the equation A*x = b, when you read a matrix A, the first column is a list of coefficients that get multiplied by the top element in the 'x' vector, the second column by the second element, and so on; and these are individually equated to the top (and second and so on) elements of the 'b' vector.Another way to think of it is, the result of matrix multiplication by a vector, is a vector where each element is the dot product of a matrix row with the column vector.Finally, for your application, you only need the Y row, so you only need the Cyr, Cyg, Cyb coefficients, which get multiplied respectively, and summed, with the R,G,B vector (i.e., the dot product).FWIW, full matrix multiplication is generally O(N^3) (since you have NxN elements and a 1xN vector), with some very advanced algorithms being suitable for very large matrices, or certain types (such as sparse matrices, where most of the elements are zero). For small matrices, you're more than better off writing it out "by hand", especially if you're only ever dealing with one small, constant system (the C factors can be CONSTs), rather than the generalized variable-times-variable case. And if Y is the only thing you ever need, yeah, there's nothing to it, just the Eq.2.Tim

author: zapta
author_rank: Super Contributor
timestamp: January 03, 2015, 08:49:54 am
content: They gave you the explicit formula for the Y using the coefficients from the second row in the matrix and a Y range scaler.You find the X and Z in the same way but using the three coefficients from the first and third matrix rows respectively and the respective range scaler.Each of the X, Y, Z formulas requires 3 multiplications and two additions and then a multiplication for range scaling.Just write down the three equations and forget about the matrix thing. This simple form of equations is called 'linear combination' (multiply by respective coefficient and then sum).You will need to figure out the value of the 9 coefficients in the matrix and the three range scalers. Hopefully it's in the datasheet.(BTW, you can pre calculate the range scalers into the matrix coefficients but this is not critical).

author: Mechatrommer
author_rank: Super Contributor
timestamp: January 03, 2015, 09:46:07 am
content: i typed long literature about this just and it lost window crashed, just to summarize of what is lost...1) matrix is small part, in fact its already in soup plate for you in eq 2. thats all, mathematically speaking!2) the pandora box is actually color and light measurement, not matrix.3) lux measurement is totally different to color measurement, lux is absolute, color is relative. we have lux meter but color sensor cannot get all lux level, so you set your gain (Cs)4) further delve into object radiance, you may lose more hair.

author: IanB
author_rank: Super Contributor
timestamp: January 03, 2015, 10:45:54 am
content: I'm very confused. If you only want the Y part they gave you the exact formula you need under "Calculating Lux (Eq 2)". It's all spelled out in black and white, so to speak. Where are you getting stuck?

author: MrAureliusR
author_rank: Unknown
timestamp: January 04, 2015, 04:25:18 am
content: Quote from: IanB on January 03, 2015, 10:45:54 amWell, TBH, I'm not a complete idiot and I am able to do the simplified version that's given in the second figure (at least once I get the proper coefficients). But just doing the math rote doesn't really teach me how they got that formula, or how to deal with this again when I get to it.Quote from: T3sl4co1l on January 03, 2015, 05:30:56 amThanks for the explanation, Tim! I'll have to re-read it a few times but I can tell you know what you're talking about. And thanks to everyone else for all the info. I have to keep at this until my brain gets it.EDIT:Quote from: c4757p on January 03, 2015, 05:04:38 amThanks for that link. I just watched that guys videos on matrices, the only problem is for the full matrix in figure 1 above, he says you cannot multiply those because they have differing sizes. However, I noticed if you just pull one row out of the 3x3 matrix, you end up multiplying a 1x3 matrix by a 3x1 matrix, which is legal, and which gives you (part) of the formula that they give in figure 2.What I'm wondering is how you actually multiply matrices of differing sizes. Is it possible at all?

author: Alex Eisenhut
author_rank: Super Contributor
timestamp: January 04, 2015, 04:33:53 am
content: This might helphttps://en.wikipedia.org/wiki/Cramer%27s_rule

author: Mechatrommer
author_rank: Super Contributor
timestamp: January 04, 2015, 06:21:29 am
content: Quote from: MrAureliusR on January 04, 2015, 04:25:18 amexactly that the rule. you multiply one row on the left hand with one colum on the right... [N.M] X [M.L] matrix multiplication, M must match. the result you'll get N.L matrix, ie N by L simultaneous equation. eq [2.4] X [4.3] = [2.3] ie 2 x 3 = 6 simultaneous equations. in your case [3.3] X [3.1] = [3.1] 3 x 1 = 3 simultaneous equations. simultaneous equations in the sense if X,Y,Z and R,G,B are given and you try to figure out Cs's. but since Css' come from you, this is just a plain simple algebraic equations to find X,Y,Z. do you know "simultaneous equations"? read the history because matrix is derived to simplify that solution.Quote from: MrAureliusR on January 04, 2015, 04:25:18 amsee above. no you dont simply multiply of any sizes.

author: helius
author_rank: Super Contributor
timestamp: January 04, 2015, 06:58:25 am
content: You can only multiply matrices when the left matrix has the same number of columns as the right matrix has rows. Even if the matrices are the same size, they cannot be multiplied together if they are not square. However, you could transpose one of them so that the columns on the left matched the rows on the right.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: January 04, 2015, 08:53:18 am
content: And further, note that A^T * B != B^T * A (using "^T" to indicate transpose), because assuming the row and column counts are compatible, the results are different.Some special cases of this exist, like taking the product of two vectors: x^T * y is the dot product (multiply respective pairs of elements, then take the sum), a scalar (a plain old number -- or alternately, a 1x1 matrix). The other case, x * y^T, is the outer product, an NxN matrix. (Obviously, the outer product only contains N+N amount of information, so there's an obvious pattern to the NxN entries of this matrix; but it still serves useful purposes, and can be used to express a number of interesting relations.)Tim