00001 /* ---------------------------------------------------------------------- 00002 * Copyright (C) 2011 ARM Limited. All rights reserved. 00003 * 00004 * $Date: 15. December 2011 00005 * $Revision: V2.0.0 00006 * 00007 * Project: Cortex-R4 DSP Library 00008 * Title: arm_matrix_example_f32.c 00009 * 00010 * Description: Example code demonstrating least square fit to data 00011 * using matrix functions 00012 * 00013 * Target Processor: Cortex-R4 00014 * 00015 * Version 2.0.0 2011/12/15 00016 * Final release. 00017 * 00018 * ---------------------------------------------------------------------------- */ 00019 00071 #include "arm_math.h" 00072 #include "math_helper.h" 00073 00074 #define SNR_THRESHOLD 90 00075 00076 /* -------------------------------------------------------------------------------- 00077 * Test input data(Cycles) taken from FIR Q15 module for differant cases of blockSize 00078 * and tapSize 00079 * --------------------------------------------------------------------------------- */ 00080 00081 const float32_t B_f32[4] = 00082 { 00083 782.0, 7577.0, 470.0, 4505.0 00084 }; 00085 00086 /* -------------------------------------------------------------------------------- 00087 * Formula to fit is C1 + C2 * numTaps + C3 * blockSize + C4 * numTaps * blockSize 00088 * -------------------------------------------------------------------------------- */ 00089 00090 const float32_t A_f32[16] = 00091 { 00092 /* Const, numTaps, blockSize, numTaps*blockSize */ 00093 1.0, 32.0, 4.0, 128.0, 00094 1.0, 32.0, 64.0, 2048.0, 00095 1.0, 16.0, 4.0, 64.0, 00096 1.0, 16.0, 64.0, 1024.0, 00097 }; 00098 00099 00100 /* ---------------------------------------------------------------------- 00101 * Temporary buffers for storing intermediate values 00102 * ------------------------------------------------------------------- */ 00103 /* Transpose of A Buffer */ 00104 float32_t AT_f32[16]; 00105 /* (Transpose of A * A) Buffer */ 00106 float32_t ATMA_f32[16]; 00107 /* Inverse(Transpose of A * A) Buffer */ 00108 float32_t ATMAI_f32[16]; 00109 /* Test Output Buffer */ 00110 float32_t X_f32[4]; 00111 00112 /* ---------------------------------------------------------------------- 00113 * Reference ouput buffer C1, C2, C3 and C4 taken from MATLAB 00114 * ------------------------------------------------------------------- */ 00115 const float32_t xRef_f32[4] = {73.0, 8.0, 21.25, 2.875}; 00116 00117 float32_t snr; 00118 00119 00120 /* ---------------------------------------------------------------------- 00121 * Max magnitude FFT Bin test 00122 * ------------------------------------------------------------------- */ 00123 00124 int32_t main(void) 00125 { 00126 00127 arm_matrix_instance_f32 A; /* Matrix A Instance */ 00128 arm_matrix_instance_f32 AT; /* Matrix AT(A transpose) instance */ 00129 arm_matrix_instance_f32 ATMA; /* Matrix ATMA( AT multiply with A) instance */ 00130 arm_matrix_instance_f32 ATMAI; /* Matrix ATMAI(Inverse of ATMA) instance */ 00131 arm_matrix_instance_f32 B; /* Matrix B instance */ 00132 arm_matrix_instance_f32 X; /* Matrix X(Unknown Matrix) instance */ 00133 00134 uint32_t srcRows, srcColumns; /* Temporary variables */ 00135 arm_status status; 00136 00137 /* Initialise A Matrix Instance with numRows, numCols and data array(A_f32) */ 00138 srcRows = 4; 00139 srcColumns = 4; 00140 arm_mat_init_f32(&A, srcRows, srcColumns, (float32_t *)A_f32); 00141 00142 /* Initialise Matrix Instance AT with numRows, numCols and data array(AT_f32) */ 00143 srcRows = 4; 00144 srcColumns = 4; 00145 arm_mat_init_f32(&AT, srcRows, srcColumns, AT_f32); 00146 00147 /* calculation of A transpose */ 00148 status = arm_mat_trans_f32(&A, &AT); 00149 00150 00151 /* Initialise ATMA Matrix Instance with numRows, numCols and data array(ATMA_f32) */ 00152 srcRows = 4; 00153 srcColumns = 4; 00154 arm_mat_init_f32(&ATMA, srcRows, srcColumns, ATMA_f32); 00155 00156 /* calculation of AT Multiply with A */ 00157 status = arm_mat_mult_f32(&AT, &A, &ATMA); 00158 00159 /* Initialise ATMAI Matrix Instance with numRows, numCols and data array(ATMAI_f32) */ 00160 srcRows = 4; 00161 srcColumns = 4; 00162 arm_mat_init_f32(&ATMAI, srcRows, srcColumns, ATMAI_f32); 00163 00164 /* calculation of Inverse((Transpose(A) * A) */ 00165 status = arm_mat_inverse_f32(&ATMA, &ATMAI); 00166 00167 /* calculation of (Inverse((Transpose(A) * A)) * Transpose(A)) */ 00168 status = arm_mat_mult_f32(&ATMAI, &AT, &ATMA); 00169 00170 /* Initialise B Matrix Instance with numRows, numCols and data array(B_f32) */ 00171 srcRows = 4; 00172 srcColumns = 1; 00173 arm_mat_init_f32(&B, srcRows, srcColumns, (float32_t *)B_f32); 00174 00175 /* Initialise X Matrix Instance with numRows, numCols and data array(X_f32) */ 00176 srcRows = 4; 00177 srcColumns = 1; 00178 arm_mat_init_f32(&X, srcRows, srcColumns, X_f32); 00179 00180 /* calculation ((Inverse((Transpose(A) * A)) * Transpose(A)) * B) */ 00181 status = arm_mat_mult_f32(&ATMA, &B, &X); 00182 00183 /* Comparison of reference with test output */ 00184 snr = arm_snr_f32((float32_t *)xRef_f32, X_f32, 4); 00185 00186 /*------------------------------------------------------------------------------ 00187 * Initialise status depending on SNR calculations 00188 *------------------------------------------------------------------------------*/ 00189 if( snr > SNR_THRESHOLD) 00190 { 00191 status = ARM_MATH_SUCCESS; 00192 } 00193 else 00194 { 00195 status = ARM_MATH_TEST_FAILURE; 00196 } 00197 00198 00199 /* ---------------------------------------------------------------------- 00200 ** Loop here if the signals fail the PASS check. 00201 ** This denotes a test failure 00202 ** ------------------------------------------------------------------- */ 00203 if( status != ARM_MATH_SUCCESS) 00204 { 00205 while(1); 00206 } 00207 } 00208