Matrix Inversion using SAM3x-EK and CMSIS DSP library

Discussion around product based on ARM Cortex M3 core.

Moderators: nferre, ncollot

hulaman
Posts: 3
Joined: Tue Feb 12, 2013 11:51 pm

Matrix Inversion using SAM3x-EK and CMSIS DSP library

Fri Mar 15, 2013 5:32 pm

Hi
I have been trying to use CMSIS DSP Library for a matrix manipulation application. My matrices are all float32_t types. One of them is a 50x1 matrix, 3 of them are 50x5 size matrices and other two are 5x5 matrices. Basically, I perform transpose, multiplication and inversion. I have included my code below:

Code: Select all

        float32_t x_1 = 0;
	float32_t x_2 = 0;
	float32_t x_3 = 0;
	float32_t x_4 = 0;
	float32_t x_5 = 0;
	int MOV_WIN = 50,i,j,index;
	uint32_t srcRows, srcColumns;	/* Temporary variables */
	arm_matrix_instance_f32 A;		/* Matrix A Instance */
	arm_matrix_instance_f32 AT;		/* Matrix AT(A transpose) instance */
	arm_matrix_instance_f32 ATMA;	/* Matrix ATMA( AT multiply with A) instance */
	arm_matrix_instance_f32 ATMAI;	/* Matrix ATMAI(Inverse of ATMA) instance */
	arm_matrix_instance_f32 B;		/* Matrix B instance */
	arm_matrix_instance_f32 X;		/* Matrix X(Unknown Matrix) instance */
	arm_status status; 
 
	/* Initialise A Matrix Instance with numRows, numCols and data array(A_f32) */ 
	srcRows = 50; 
    srcColumns = 5; 
	arm_mat_init_f32(&A, srcRows, srcColumns, (float32_t *)A_f32); 
 
	/* Initialise Matrix Instance AT with numRows, numCols and data array(AT_f32) */ 
	srcRows = 5; 
    srcColumns = 50; 
	arm_mat_init_f32(&AT, srcRows, srcColumns, AT_f32); 
 
 
	 /* Initialise ATMA Matrix Instance with numRows, numCols and data array(ATMA_f32) */
	 srcRows = 5;
	 srcColumns = 5;
	 arm_mat_init_f32(&ATMA, srcRows, srcColumns, ATMA_f32);
	 
	 /* Initialise ATMAI Matrix Instance with numRows, numCols and data array(ATMAI_f32) */
	 srcRows = 5;
	 srcColumns = 5;
	 arm_mat_init_f32(&ATMAI, srcRows, srcColumns, ATMAI_f32);
	 
	/* Initialise B Matrix Instance with numRows, numCols and data array(B_f32) */
	srcRows = 50;
	srcColumns = 1;
	arm_mat_init_f32(&B, srcRows, srcColumns, (float32_t *)B_f32);
	
	/* Initialise X Matrix Instance with numRows, numCols and data array(X_f32) */
	srcRows = 5;
	srcColumns = 1;
	arm_mat_init_f32(&X, srcRows, srcColumns, X_f32);
	
	int ctr = 1;
	
	//keep on reading until the user exits
	while (strcmp(a,"XXX") != 0)
	{	
		ctr = ctr + 1;
		puts("s");
		strcpy(send_string,"");
		uart_read_delim(' ', a, 1);
		voltage = strtod(a, (char **) NULL);	
		uart_read_delim(' ', a, 1);
		current = strtod(a, (char **) NULL);	
				
		
		/* Shift the matrices */		
		int i,index, j = MOV_WIN-1;		
		for (i = 0;i<j;i++){
			B.pData[i] = B.pData[i+1];
			A.pData[i] = A.pData[i+1];
		}
		index = 50;
		B.pData[49] = voltage;
		A.pData[index - 1] = - B.pData[index-2];
		
		for (i = 0;i<j;i++){
			A.pData[index+i] = A.pData[index+i+1];
		}
		index = MOV_WIN*2;
		for (i = 0;i<j;i++){
			A.pData[index+i] = A.pData[index+i+1];
		}
		A.pData[index - 1] = - B.pData[index-3];
		index = MOV_WIN*3;
		for (i = 0;i<j;i++){
			A.pData[index+i] = A.pData[index+i+1];
		}
		A.pData[index - 1] = current;
		j= index;
		index = MOV_WIN*4;
		
		for (i = 0;i<j;i++){
			A.pData[index+i] = A.pData[index+i+1];
		}
		A.pData[index - 1] = A.pData[j-2];
		index = MOV_WIN*5;
		A.pData[index - 1] = A.pData[j-3];
	
		//hx8347a_draw_string(10,80,"shifted"); 
       [color=#400040] x_1 = B.pData[49];[/color]			

               /* calculation of A transpose */
		status = arm_mat_trans_f32(&A, &AT);
		
		if (status != ARM_MATH_SUCCESS){
			//hx8347a_draw_string(10,100,"Size Mismatch"); 
		}else{		
			//hx8347a_draw_string(10,100,"transposed"); 
		}			
		[color=#000080]x_2 = B.pData[49];[/color]
		
		/* calculation of AT Multiply with A */
		status = arm_mat_mult_f32(&AT, &A, &ATMA);
		if (status != ARM_MATH_SUCCESS){
			//hx8347a_draw_string(10,120,"Size Mismatch");
		}else{
			//hx8347a_draw_string(10,120,"mat mul done");
		}
		
		
		[color=#0000BF]x_3 = B.pData[49];[/color]
		
		/* calculation of Inverse((Transpose(A) * A) */
		status = arm_mat_inverse_f32(&ATMA, &ATMAI);
		
		if (status == ARM_MATH_SINGULAR){
			//hx8347a_draw_string(10,140,"SINGULAR");
			
		}else{
			//hx8347a_draw_string(10,140,"not SINGULAR");
		
			/* calculation of (Inverse((Transpose(A) * A)) *  Transpose(A)) */
			status = arm_mat_mult_f32(&ATMAI, &AT, &ATMA);
		
			/* calculation ((Inverse((Transpose(A) * A)) *  Transpose(A)) * B) */
			status = arm_mat_mult_f32(&ATMA, &B, &X);
		
		}
When I print out the values of x_1, x_2, x_3 - I get the correct value of B.pData[49] on x_1 but I get 0 for both x_2 and x_3. Is this some kind of insufficient memory issue? Any idea why I may be getting zeros for x_2 and x_3? Thanks a lot for the help.

Return to “SAM3 Cortex-M3 MCU”

Who is online

Users browsing this forum: No registered users and 3 guests