I'm working on the STM32L476RG and I'm looking to perform some matrix operations on the controller, using C and CMSIS-DSP for ARM. I have 2 uint16_t arrays which I will be converting to matrices and feeding in to the matrix multiplication. My problem is that I need the output to be another uint16_t array, so I need a way to either convert the entire matrix to an array of the same dimensions, or a (heavier) way to access every individual element in the output matrix and typecast them into an array like array[x][y] = (uint16_t)output[x][y]

Can someone guide me through this, or is there a better way to tackle the problem?

  • \$\begingroup\$ Are you programming in C or C++? And what is this matrix thing you are talking about. I just leafed through my copy of "The C Programming Language" by Kernighan and Ritchie and there was no mention of matrixes. OK. I am being a bit of a smart alec. But the point is, it is not clear what this matrix thing is, so it will be hard to tell you how to convert it. \$\endgroup\$ – mkeith Jan 12 at 4:52
  • \$\begingroup\$ I have no idea what this matrix type you are talking about is, but I'm guessing you could make a pointer to it and trick C into think this matrix type is an array which lets you access it as an array. But if the matrix type isn't implemented in memory as expected it could also destroy things. \$\endgroup\$ – DKNguyen Jan 12 at 5:13
  • \$\begingroup\$ I've added the link for the particular matrix operations I'm mentioning \$\endgroup\$ – Souvik Saha Jan 12 at 6:02
  • \$\begingroup\$ Since this is C you're using, what makes you think that your 'matrix' is not already an array? C doesn't have a 'matrix' datatype - so what else could it be other than an array? There's no conversion necessary... \$\endgroup\$ – brhans Jan 12 at 13:13

The CMSIS-DSP matrix functions take integers or floats as data input, however your arrays are uint_16 so you may have data loss or wrong results due to the implicit conversions. if you used arm_mat_mult_fast_q15.

what I would do is cast my input arrays to int32_t (Q31) and then do the multiplication. (No need to convert to matrices)

//nRows1,nColumns1, nRows2, nColumns2 are the dimensions of matrices(arrays) 1 and 2
int32_t res[nRows1*nColumns2]; // if nColumns1=nRows2 
int32_t *pRes = res;
arm_matrix_instance_q31 S1 = {nRows1, nColumns1, pData1}; //pData1 is pointer to array1
arm_matrix_instance_q31 S2 = {nRows2, nColumns2, pData2}; //pData2 is pointer to array2
arm_matrix_instance_q31 SRes = {nRows1, nColumns2, pRes}; //pRes is pointer the result array

arm_mat_mult_fast_q31 ( &S1, &s2, &SRes); // your results will be in 1D array res

Note: if you need to cast your results back to uint16_t make sure the values are less than < 65536 (2^16) otherwise you will have data loss (wrong results) again.

  • \$\begingroup\$ Where can I get the int32 type? Is it the same as int32_t? \$\endgroup\$ – Souvik Saha Jan 12 at 10:46
  • \$\begingroup\$ Yes. That was a typo \$\endgroup\$ – Mohammad Jan 12 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.