mirror of
https://github.com/Fishwaldo/bl_mcu_sdk.git
synced 2025-07-23 05:08:45 +00:00
[feat][nmsis] add nmsis component and nn,dsp demo
This commit is contained in:
parent
b2aada479b
commit
5d1126d0f0
989 changed files with 286224 additions and 0 deletions
6
examples/dsp/StatisticsFunctions_part1/CMakeLists.txt
Normal file
6
examples/dsp/StatisticsFunctions_part1/CMakeLists.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
set(TARGET_REQUIRED_PRIVATE_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/..)
|
||||
list(APPEND TARGET_REQUIRED_SRCS absmax.c absmin.c max.c)
|
||||
list(APPEND TARGET_REQUIRED_SRCS mean.c min.c power.c rms.c std.c var.c)
|
||||
set(TARGET_REQUIRED_LIBS nmsis)
|
||||
set(mains main.c)
|
||||
generate_bin()
|
167
examples/dsp/StatisticsFunctions_part1/absmax.c
Normal file
167
examples/dsp/StatisticsFunctions_part1/absmax.c
Normal file
|
@ -0,0 +1,167 @@
|
|||
#include "ref.h"
|
||||
|
||||
void ref_absmax_f32(
|
||||
const float32_t * pSrc,
|
||||
uint32_t blockSize,
|
||||
float32_t * pResult,
|
||||
uint32_t * pIndex)
|
||||
{
|
||||
float32_t maxVal, out; /* Temporary variables to store the output value. */
|
||||
uint32_t blkCnt, outIndex; /* Loop counter */
|
||||
|
||||
/* Initialise index value to zero. */
|
||||
outIndex = 0U;
|
||||
|
||||
/* Load first input value that act as reference value for comparision */
|
||||
out = fabsf(*pSrc++);
|
||||
|
||||
/* Initialize blkCnt with number of samples */
|
||||
blkCnt = (blockSize - 1U);
|
||||
|
||||
|
||||
while (blkCnt > 0U)
|
||||
{
|
||||
/* Initialize maxVal to the next consecutive values one by one */
|
||||
maxVal = fabsf(*pSrc++);
|
||||
|
||||
/* compare for the maximum value */
|
||||
if (out < maxVal)
|
||||
{
|
||||
/* Update the maximum value and it's index */
|
||||
out = maxVal;
|
||||
outIndex = blockSize - blkCnt;
|
||||
}
|
||||
|
||||
/* Decrement loop counter */
|
||||
blkCnt--;
|
||||
}
|
||||
|
||||
/* Store the maximum value and it's index into destination pointers */
|
||||
*pResult = out;
|
||||
*pIndex = outIndex;
|
||||
}
|
||||
|
||||
void ref_absmax_q7(
|
||||
const q7_t * pSrc,
|
||||
uint32_t blockSize,
|
||||
q7_t * pResult,
|
||||
uint32_t * pIndex)
|
||||
{
|
||||
q7_t maxVal, out; /* Temporary variables to store the output value. */
|
||||
uint32_t blkCnt, outIndex; /* Loop counter */
|
||||
|
||||
|
||||
/* Initialise index value to zero. */
|
||||
outIndex = 0U;
|
||||
/* Load first input value that act as reference value for comparision */
|
||||
out = (*pSrc > 0) ? *pSrc : ((*pSrc == (q7_t) 0x80) ? (q7_t) 0x7f : -*pSrc);
|
||||
pSrc++;
|
||||
|
||||
/* Initialize blkCnt with number of samples */
|
||||
blkCnt = (blockSize - 1U);
|
||||
|
||||
while (blkCnt > 0U)
|
||||
{
|
||||
/* Initialize maxVal to the next consecutive values one by one */
|
||||
maxVal = (*pSrc > 0) ? *pSrc : ((*pSrc == (q7_t) 0x80) ? (q7_t) 0x7f : -*pSrc);
|
||||
pSrc++;
|
||||
|
||||
/* compare for the maximum value */
|
||||
if (out < maxVal)
|
||||
{
|
||||
/* Update the maximum value and it's index */
|
||||
out = maxVal;
|
||||
outIndex = blockSize - blkCnt;
|
||||
}
|
||||
|
||||
/* Decrement loop counter */
|
||||
blkCnt--;
|
||||
}
|
||||
|
||||
/* Store the maximum value and it's index into destination pointers */
|
||||
*pResult = out;
|
||||
*pIndex = outIndex;
|
||||
}
|
||||
|
||||
void ref_absmax_q15(
|
||||
const q15_t * pSrc,
|
||||
uint32_t blockSize,
|
||||
q15_t * pResult,
|
||||
uint32_t * pIndex)
|
||||
{
|
||||
q15_t maxVal, out; /* Temporary variables to store the output value. */
|
||||
uint32_t blkCnt, outIndex; /* Loop counter */
|
||||
|
||||
/* Initialise index value to zero. */
|
||||
outIndex = 0U;
|
||||
/* Load first input value that act as reference value for comparision */
|
||||
out = (*pSrc > 0) ? *pSrc : ((*pSrc == (q15_t) 0x8000) ? 0x7fff : -*pSrc);
|
||||
pSrc++;
|
||||
|
||||
/* Initialize blkCnt with number of samples */
|
||||
blkCnt = (blockSize - 1U);
|
||||
|
||||
while (blkCnt > 0U)
|
||||
{
|
||||
/* Initialize maxVal to the next consecutive values one by one */
|
||||
maxVal = (*pSrc > 0) ? *pSrc : ((*pSrc == (q15_t) 0x8000) ? 0x7fff : -*pSrc);
|
||||
pSrc++;
|
||||
|
||||
/* compare for the maximum value */
|
||||
if (out < maxVal)
|
||||
{
|
||||
/* Update the maximum value and it's index */
|
||||
out = maxVal;
|
||||
outIndex = blockSize - blkCnt;
|
||||
}
|
||||
|
||||
/* Decrement loop counter */
|
||||
blkCnt--;
|
||||
}
|
||||
|
||||
/* Store the maximum value and it's index into destination pointers */
|
||||
*pResult = out;
|
||||
*pIndex = outIndex;
|
||||
}
|
||||
|
||||
void ref_absmax_q31(
|
||||
const q31_t * pSrc,
|
||||
uint32_t blockSize,
|
||||
q31_t * pResult,
|
||||
uint32_t * pIndex)
|
||||
{
|
||||
q31_t maxVal, out; /* Temporary variables to store the output value. */
|
||||
uint32_t blkCnt, outIndex; /* Loop counter */
|
||||
|
||||
|
||||
/* Initialise index value to zero. */
|
||||
outIndex = 0U;
|
||||
/* Load first input value that act as reference value for comparision */
|
||||
out = (*pSrc > 0) ? *pSrc : ((*pSrc == INT32_MIN) ? INT32_MAX : -*pSrc);
|
||||
pSrc++;
|
||||
|
||||
/* Initialize blkCnt with number of samples */
|
||||
blkCnt = (blockSize - 1U);
|
||||
|
||||
while (blkCnt > 0U)
|
||||
{
|
||||
/* Initialize maxVal to the next consecutive values one by one */
|
||||
maxVal = (*pSrc > 0) ? *pSrc : ((*pSrc == INT32_MIN) ? INT32_MAX : -*pSrc);
|
||||
pSrc++;
|
||||
|
||||
/* compare for the maximum value */
|
||||
if (out < maxVal)
|
||||
{
|
||||
/* Update the maximum value and it's index */
|
||||
out = maxVal;
|
||||
outIndex = blockSize - blkCnt;
|
||||
}
|
||||
|
||||
/* Decrement loop counter */
|
||||
blkCnt--;
|
||||
}
|
||||
|
||||
/* Store the maximum value and it's index into destination pointers */
|
||||
*pResult = out;
|
||||
*pIndex = outIndex;
|
||||
}
|
165
examples/dsp/StatisticsFunctions_part1/absmin.c
Normal file
165
examples/dsp/StatisticsFunctions_part1/absmin.c
Normal file
|
@ -0,0 +1,165 @@
|
|||
#include "ref.h"
|
||||
|
||||
void ref_absmin_f32(
|
||||
const float32_t * pSrc,
|
||||
uint32_t blockSize,
|
||||
float32_t * pResult,
|
||||
uint32_t * pIndex)
|
||||
{
|
||||
float32_t minVal, out; /* Temporary variables to store the output value. */
|
||||
uint32_t blkCnt, outIndex; /* Loop counter */
|
||||
|
||||
/* Initialise index value to zero. */
|
||||
outIndex = 0U;
|
||||
|
||||
/* Load first input value that act as reference value for comparision */
|
||||
out = fabsf(*pSrc++);
|
||||
|
||||
/* Initialize blkCnt with number of samples */
|
||||
blkCnt = (blockSize - 1U);
|
||||
|
||||
while (blkCnt > 0U)
|
||||
{
|
||||
/* Initialize minVal to the next consecutive values one by one */
|
||||
minVal = fabsf(*pSrc++);
|
||||
|
||||
/* compare for the minimum value */
|
||||
if (out > minVal)
|
||||
{
|
||||
/* Update the minimum value and it's index */
|
||||
out = minVal;
|
||||
outIndex = blockSize - blkCnt;
|
||||
}
|
||||
|
||||
/* Decrement loop counter */
|
||||
blkCnt--;
|
||||
}
|
||||
|
||||
/* Store the minimum value and it's index into destination pointers */
|
||||
*pResult = out;
|
||||
*pIndex = outIndex;
|
||||
}
|
||||
|
||||
void ref_absmin_q7(
|
||||
const q7_t * pSrc,
|
||||
uint32_t blockSize,
|
||||
q7_t * pResult,
|
||||
uint32_t * pIndex)
|
||||
{
|
||||
q7_t minVal, out; /* Temporary variables to store the output value. */
|
||||
uint32_t blkCnt, outIndex; /* Loop counter */
|
||||
|
||||
/* Initialise index value to zero. */
|
||||
outIndex = 0U;
|
||||
/* Load first input value that act as reference value for comparision */
|
||||
out = (*pSrc > 0) ? *pSrc : ((*pSrc == (q7_t) 0x80) ? (q7_t) 0x7f : -*pSrc);
|
||||
pSrc++;
|
||||
|
||||
/* Initialize blkCnt with number of samples */
|
||||
blkCnt = (blockSize - 1U);
|
||||
|
||||
while (blkCnt > 0U)
|
||||
{
|
||||
/* Initialize minVal to the next consecutive values one by one */
|
||||
minVal = (*pSrc > 0) ? *pSrc : ((*pSrc == (q7_t) 0x80) ? (q7_t) 0x7f : -*pSrc);
|
||||
pSrc++;
|
||||
|
||||
/* compare for the minimum value */
|
||||
if (out > minVal)
|
||||
{
|
||||
/* Update the minimum value and it's index */
|
||||
out = minVal;
|
||||
outIndex = blockSize - blkCnt;
|
||||
}
|
||||
|
||||
/* Decrement loop counter */
|
||||
blkCnt--;
|
||||
}
|
||||
|
||||
/* Store the minimum value and it's index into destination pointers */
|
||||
*pResult = out;
|
||||
*pIndex = outIndex;
|
||||
}
|
||||
|
||||
void ref_absmin_q15(
|
||||
const q15_t * pSrc,
|
||||
uint32_t blockSize,
|
||||
q15_t * pResult,
|
||||
uint32_t * pIndex)
|
||||
{
|
||||
q15_t minVal, out; /* Temporary variables to store the output value. */
|
||||
uint32_t blkCnt, outIndex; /* Loop counter */
|
||||
|
||||
|
||||
/* Initialise index value to zero. */
|
||||
outIndex = 0U;
|
||||
/* Load first input value that act as reference value for comparision */
|
||||
out = (*pSrc > 0) ? *pSrc : ((*pSrc == (q15_t) 0x8000) ? 0x7fff : -*pSrc);
|
||||
pSrc++;
|
||||
|
||||
/* Initialize blkCnt with number of samples */
|
||||
blkCnt = (blockSize - 1U);
|
||||
|
||||
while (blkCnt > 0U)
|
||||
{
|
||||
/* Initialize minVal to the next consecutive values one by one */
|
||||
minVal = (*pSrc > 0) ? *pSrc : ((*pSrc == (q15_t) 0x8000) ? 0x7fff : -*pSrc);
|
||||
pSrc++;
|
||||
|
||||
/* compare for the minimum value */
|
||||
if (out > minVal)
|
||||
{
|
||||
/* Update the minimum value and it's index */
|
||||
out = minVal;
|
||||
outIndex = blockSize - blkCnt;
|
||||
}
|
||||
|
||||
/* Decrement loop counter */
|
||||
blkCnt--;
|
||||
}
|
||||
|
||||
/* Store the minimum value and it's index into destination pointers */
|
||||
*pResult = out;
|
||||
*pIndex = outIndex;
|
||||
}
|
||||
|
||||
void ref_absmin_q31(
|
||||
const q31_t * pSrc,
|
||||
uint32_t blockSize,
|
||||
q31_t * pResult,
|
||||
uint32_t * pIndex)
|
||||
{
|
||||
q31_t minVal, out; /* Temporary variables to store the output value. */
|
||||
uint32_t blkCnt, outIndex; /* Loop counter */
|
||||
|
||||
/* Initialise index value to zero. */
|
||||
outIndex = 0U;
|
||||
/* Load first input value that act as reference value for comparision */
|
||||
out = (*pSrc > 0) ? *pSrc : ((*pSrc == INT32_MIN) ? INT32_MAX : -*pSrc);
|
||||
pSrc++;
|
||||
|
||||
/* Initialize blkCnt with number of samples */
|
||||
blkCnt = (blockSize - 1U);
|
||||
|
||||
while (blkCnt > 0U)
|
||||
{
|
||||
/* Initialize minVal to the next consecutive values one by one */
|
||||
minVal = (*pSrc > 0) ? *pSrc : ((*pSrc == INT32_MIN) ? INT32_MAX : -*pSrc);
|
||||
pSrc++;
|
||||
|
||||
/* compare for the minimum value */
|
||||
if (out > minVal)
|
||||
{
|
||||
/* Update the minimum value and it's index */
|
||||
out = minVal;
|
||||
outIndex = blockSize - blkCnt;
|
||||
}
|
||||
|
||||
/* Decrement loop counter */
|
||||
blkCnt--;
|
||||
}
|
||||
|
||||
/* Store the minimum value and it's index into destination pointers */
|
||||
*pResult = out;
|
||||
*pIndex = outIndex;
|
||||
}
|
11
examples/dsp/StatisticsFunctions_part1/array.h
Normal file
11
examples/dsp/StatisticsFunctions_part1/array.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
// Array for StatisticsFunctions part1
|
||||
#define ARRAY_SIZE 230
|
||||
|
||||
float32_t f32_array[ARRAY_SIZE] = {
|
||||
-94.118553, 91.433388, -2.924870, 60.056095, -71.622734, -15.647743, 83.147102, 58.441467, 99.898483, 31.148140, -92.857666, 69.825859, 86.798653, 35.747032, 51.548027, 48.626495, -21.554596, 31.095577, -65.762665, 41.209217, -93.633430, -44.615402, -90.765724, 0};
|
||||
q7_t q7_array[ARRAY_SIZE] = {
|
||||
-104, 82, 49, -47, 115, -120, -16, -31, 67, 75, -81, -3, -14, 37, 53, 65, -58, 46, 39, -87, -98, -1, 117, 0};
|
||||
q15_t q15_array[ARRAY_SIZE] = {
|
||||
-41, 21, -71, 64, -63, 1, 50, 100, 117, 12, -93, -90, -63, 87, -63, 80, -66, 109, -39, -78, -64, 29, -7, 0};
|
||||
q31_t q31_array[ARRAY_SIZE] = {
|
||||
-9722, 21681, 5587, 3258, 27341, -14036, 16855, 16628, -7836, 4444, -27797, -29233, 2018, 18295, 28443, -24255, 4510, -2007, -31988, -10675, -22140, 19286, -12373, 0};
|
484
examples/dsp/StatisticsFunctions_part1/main.c
Normal file
484
examples/dsp/StatisticsFunctions_part1/main.c
Normal file
|
@ -0,0 +1,484 @@
|
|||
//
|
||||
// Created by lujun on 19-6-28.DELTAF32
|
||||
//
|
||||
|
||||
#include "riscv_math.h"
|
||||
#include "array.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include "../common.h"
|
||||
|
||||
#include "../HelperFunctions/math_helper.c"
|
||||
#include "../HelperFunctions/ref_helper.c"
|
||||
|
||||
#include <stdio.h>
|
||||
#define DELTAF32 (0.05f)
|
||||
#define DELTAQ31 (63)
|
||||
#define DELTAQ15 (1)
|
||||
#define DELTAQ7 (1)
|
||||
|
||||
#define ENABLE_ALL
|
||||
#define F32
|
||||
#define Q31
|
||||
#define Q15
|
||||
#define Q7
|
||||
|
||||
int test_flag_error = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
q63_t q63_out, q63_out_ref;
|
||||
float32_t f32_out, f32_out_ref;
|
||||
q31_t q31_out, q31_out_ref;
|
||||
q15_t q15_out, q15_out_ref;
|
||||
q7_t q7_out, q7_out_ref;
|
||||
uint32_t index, index_ref;
|
||||
|
||||
BENCH_INIT;
|
||||
#if defined F32 || defined ENABLE_ALL
|
||||
//***************************** f32 *************************
|
||||
// max
|
||||
BENCH_START(riscv_max_f32);
|
||||
riscv_max_f32(f32_array, ARRAY_SIZE, &f32_out, &index);
|
||||
BENCH_END(riscv_max_f32);
|
||||
ref_max_f32(f32_array, ARRAY_SIZE, &f32_out_ref, &index_ref);
|
||||
if ((fabs(f32_out - f32_out_ref) > DELTAF32) || (index != index_ref)) {
|
||||
BENCH_ERROR(riscv_max_f32);
|
||||
printf("value expect: %f, actual: %f\nindex expect: %f, actual: %f",
|
||||
f32_out_ref, f32_out, index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_max_f32);
|
||||
// max_no_idx
|
||||
BENCH_START(riscv_max_no_idx_f32);
|
||||
riscv_max_no_idx_f32(f32_array, ARRAY_SIZE, &f32_out);
|
||||
BENCH_END(riscv_max_no_idx_f32);
|
||||
ref_max_no_idx_f32(f32_array, ARRAY_SIZE, &f32_out_ref);
|
||||
if ((fabs(f32_out - f32_out_ref) > DELTAF32) ) {
|
||||
BENCH_ERROR(riscv_max_no_idx_f32);
|
||||
printf("value expect: %f, actual: %f\nindex expect: %f, actual: %f",
|
||||
f32_out_ref, f32_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_max_no_idx_f32);
|
||||
// mean
|
||||
BENCH_START(riscv_mean_f32);
|
||||
riscv_mean_f32(f32_array, ARRAY_SIZE, &f32_out);
|
||||
BENCH_END(riscv_mean_f32);
|
||||
ref_mean_f32(f32_array, ARRAY_SIZE, &f32_out_ref);
|
||||
if (fabs(f32_out - f32_out_ref) > DELTAF32) {
|
||||
BENCH_ERROR(riscv_mean_f32);
|
||||
printf("expect: %f, actual: %f\n", f32_out_ref, f32_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_mean_f32);
|
||||
// min
|
||||
BENCH_START(riscv_min_f32);
|
||||
riscv_min_f32(f32_array, ARRAY_SIZE, &f32_out, &index);
|
||||
BENCH_END(riscv_min_f32);
|
||||
ref_min_f32(f32_array, ARRAY_SIZE, &f32_out_ref, &index_ref);
|
||||
if ((fabs(f32_out - f32_out_ref) > DELTAF32) || (index != index_ref)) {
|
||||
BENCH_ERROR(riscv_min_f32);
|
||||
printf("value expect: %f, actual: %f\nindex expect: %f, actual: %f",
|
||||
f32_out_ref, f32_out, index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_min_f32);
|
||||
// power
|
||||
BENCH_START(riscv_power_f32);
|
||||
riscv_power_f32(f32_array, ARRAY_SIZE, &f32_out);
|
||||
BENCH_END(riscv_power_f32);
|
||||
ref_power_f32(f32_array, ARRAY_SIZE, &f32_out_ref);
|
||||
for (int i = 0; i < ARRAY_SIZE; i++)
|
||||
if (fabs(f32_out - f32_out_ref) > DELTAF32) {
|
||||
BENCH_ERROR(riscv_power_f32);
|
||||
printf("index: %d, expect: %f, actual: %f\n", i, f32_out_ref,
|
||||
f32_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_power_f32);
|
||||
// rms
|
||||
BENCH_START(riscv_rms_f32);
|
||||
riscv_rms_f32(f32_array, ARRAY_SIZE, &f32_out);
|
||||
BENCH_END(riscv_rms_f32);
|
||||
ref_rms_f32(f32_array, ARRAY_SIZE, &f32_out_ref);
|
||||
if (fabs(f32_out - f32_out_ref) > DELTAF32) {
|
||||
BENCH_ERROR(riscv_rms_f32);
|
||||
printf("expect: %f, actual: %f\n", f32_out_ref, f32_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_rms_f32);
|
||||
// std
|
||||
BENCH_START(riscv_std_f32);
|
||||
riscv_std_f32(f32_array, ARRAY_SIZE, &f32_out);
|
||||
BENCH_END(riscv_std_f32);
|
||||
ref_std_f32(f32_array, ARRAY_SIZE, &f32_out_ref);
|
||||
if (fabs(f32_out - f32_out_ref) > DELTAF32) {
|
||||
BENCH_ERROR(riscv_std_f32);
|
||||
printf("expect: %f, actual: %f\n", f32_out_ref, f32_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_std_f32);
|
||||
// var
|
||||
BENCH_START(riscv_var_f32);
|
||||
riscv_var_f32(f32_array, ARRAY_SIZE, &f32_out);
|
||||
BENCH_END(riscv_var_f32);
|
||||
ref_var_f32(f32_array, ARRAY_SIZE, &f32_out_ref);
|
||||
if (fabs(f32_out - f32_out_ref) > DELTAF32) {
|
||||
BENCH_ERROR(riscv_var_f32);
|
||||
printf("expect: %f, actual: %f\n", f32_out_ref, f32_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_var_f32);
|
||||
//absmax
|
||||
BENCH_START(riscv_absmax_f32);
|
||||
riscv_absmax_f32(f32_array, ARRAY_SIZE, &f32_out, &index);
|
||||
BENCH_END(riscv_absmax_f32);
|
||||
ref_absmax_f32(f32_array, ARRAY_SIZE, &f32_out_ref, &index_ref);
|
||||
if (fabs(f32_out - f32_out_ref) > DELTAF32) {
|
||||
BENCH_ERROR(riscv_absmax_f32);
|
||||
printf("expect: %f, actual: %f\n", f32_out_ref, f32_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
if (fabs(index - index_ref) > DELTAF32) {
|
||||
BENCH_ERROR(riscv_absmax_f32);
|
||||
printf("expect: %f, actual: %f\n", index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_absmax_f32);
|
||||
//absmin
|
||||
BENCH_START(riscv_absmin_f32);
|
||||
riscv_absmin_f32(f32_array, ARRAY_SIZE, &f32_out, &index);
|
||||
BENCH_END(riscv_absmin_f32);
|
||||
ref_absmin_f32(f32_array, ARRAY_SIZE, &f32_out_ref, &index_ref);
|
||||
if (fabs(f32_out - f32_out_ref) > DELTAF32) {
|
||||
BENCH_ERROR(riscv_absmin_f32);
|
||||
printf("expect: %f, actual: %f\n", f32_out_ref, f32_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
if (fabs(index - index_ref) > DELTAF32) {
|
||||
BENCH_ERROR(riscv_absmin_f32);
|
||||
printf("expect: %d, actual: %d\n", index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_absmin_f32);
|
||||
#endif
|
||||
#if defined Q31 || defined ENABLE_ALL
|
||||
// ********************************* q31 *****************************
|
||||
// max
|
||||
BENCH_START(riscv_max_q31);
|
||||
riscv_max_q31(q31_array, ARRAY_SIZE, &q31_out, &index);
|
||||
BENCH_END(riscv_max_q31);
|
||||
ref_max_q31(q31_array, ARRAY_SIZE, &q31_out_ref, &index_ref);
|
||||
if ((labs(q31_out - q31_out_ref) > DELTAQ31) || (index != index_ref)) {
|
||||
BENCH_ERROR(riscv_max_q31);
|
||||
printf("value expect: %x, actual: %x\nindex expect: %x, actual: %x\n",
|
||||
q31_out_ref, q31_out, index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_max_q31);
|
||||
// mean
|
||||
BENCH_START(riscv_mean_q31);
|
||||
riscv_mean_q31(q31_array, ARRAY_SIZE, &q31_out);
|
||||
BENCH_END(riscv_mean_q31);
|
||||
ref_mean_q31(q31_array, ARRAY_SIZE, &q31_out_ref);
|
||||
if (labs(q31_out - q31_out_ref) > DELTAQ31) {
|
||||
BENCH_ERROR(riscv_mean_q31);
|
||||
printf("expect: %x, actual: %x\n", q31_out_ref, q31_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_mean_q31);
|
||||
// min
|
||||
BENCH_START(riscv_min_q31);
|
||||
riscv_min_q31(q31_array, ARRAY_SIZE, &q31_out, &index);
|
||||
BENCH_END(riscv_min_q31);
|
||||
ref_min_q31(q31_array, ARRAY_SIZE, &q31_out_ref, &index_ref);
|
||||
if ((labs(q31_out - q31_out_ref) > DELTAQ31) || (index != index_ref)) {
|
||||
BENCH_ERROR(riscv_min_q31);
|
||||
printf("value expect: %x, actual: %x\nindex expect: %d, actual: %d",
|
||||
q31_out_ref, q31_out, index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_min_q31);
|
||||
// power
|
||||
BENCH_START(riscv_power_q31);
|
||||
riscv_power_q31(q31_array, ARRAY_SIZE, &q63_out);
|
||||
BENCH_END(riscv_power_q31);
|
||||
ref_power_q31(q31_array, ARRAY_SIZE, &q63_out_ref);
|
||||
if (labs(q31_out - q31_out_ref) > DELTAQ31) {
|
||||
BENCH_ERROR(riscv_power_q31);
|
||||
printf("expect: %x, actual: %x\n", q63_out_ref, q63_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_power_q31);
|
||||
// rms
|
||||
BENCH_START(riscv_rms_q31);
|
||||
riscv_rms_q31(q31_array, ARRAY_SIZE, &q31_out);
|
||||
BENCH_END(riscv_rms_q31);
|
||||
ref_rms_q31(q31_array, ARRAY_SIZE, &q31_out_ref);
|
||||
if (labs(q31_out - q31_out_ref) > DELTAQ31) {
|
||||
BENCH_ERROR(riscv_rms_q31);
|
||||
printf("expect: %x, actual: %x\n", q31_out_ref, q31_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_rms_q31);
|
||||
// std
|
||||
BENCH_START(riscv_std_q31);
|
||||
riscv_std_q31(q31_array, ARRAY_SIZE, &q31_out);
|
||||
BENCH_END(riscv_std_q31);
|
||||
ref_std_q31(q31_array, ARRAY_SIZE, &q31_out_ref);
|
||||
if (labs(q31_out - q31_out_ref) > DELTAQ31) {
|
||||
BENCH_ERROR(riscv_std_q31);
|
||||
printf("expect: %x, actual: %x\n", q31_out_ref, q31_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_std_q31);
|
||||
// var
|
||||
BENCH_START(riscv_var_q31);
|
||||
riscv_var_q31(q31_array, ARRAY_SIZE, &q31_out);
|
||||
BENCH_END(riscv_var_q31);
|
||||
ref_var_q31(q31_array, ARRAY_SIZE, &q31_out_ref);
|
||||
if (labs(q31_out - q31_out_ref) > DELTAQ31) {
|
||||
BENCH_ERROR(riscv_var_q31);
|
||||
printf("expect: %x, actual: %x\n", q31_out_ref, q31_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_var_q31);
|
||||
//absmax
|
||||
BENCH_START(riscv_absmax_q31);
|
||||
riscv_absmax_q31(q31_array, ARRAY_SIZE, &q31_out, &index);
|
||||
BENCH_END(riscv_absmax_q31);
|
||||
ref_absmax_q31(q31_array, ARRAY_SIZE, &q31_out_ref, &index_ref);
|
||||
if (fabs(q31_out - q31_out_ref) > DELTAQ31) {
|
||||
BENCH_ERROR(riscv_absmax_q31);
|
||||
printf("expect: %f, actual: %f\n", q31_out_ref, q31_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
if (fabs(index - index_ref) > DELTAQ31) {
|
||||
BENCH_ERROR(riscv_absmax_q31);
|
||||
printf("expect: %f, actual: %f\n", index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_absmax_q31);
|
||||
//absmin
|
||||
BENCH_START(riscv_absmin_q31);
|
||||
riscv_absmin_q31(q31_array, ARRAY_SIZE, &q31_out, &index);
|
||||
BENCH_END(riscv_absmin_q31);
|
||||
ref_absmin_q31(q31_array, ARRAY_SIZE, &q31_out_ref, &index_ref);
|
||||
if (fabs(q31_out - q31_out_ref) > DELTAQ31) {
|
||||
BENCH_ERROR(riscv_absmin_q31);
|
||||
printf("expect: %f, actual: %f\n", q31_out_ref, q31_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
if (fabs(index - index_ref) > DELTAQ31) {
|
||||
BENCH_ERROR(riscv_absmin_q31);
|
||||
printf("expect: %f, actual: %f\n", index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_absmin_q31);
|
||||
#endif
|
||||
#if defined Q15 || defined ENABLE_ALL
|
||||
// ********************************* q15 *****************************
|
||||
// max
|
||||
BENCH_START(riscv_max_q15);
|
||||
riscv_max_q15(q15_array, ARRAY_SIZE, &q15_out, &index);
|
||||
BENCH_END(riscv_max_q15);
|
||||
ref_max_q15(q15_array, ARRAY_SIZE, &q15_out_ref, &index_ref);
|
||||
if ((abs(q15_out - q15_out_ref) > DELTAQ15) || (index != index_ref)) {
|
||||
BENCH_ERROR(riscv_max_q15);
|
||||
printf("value expect: %x, actual: %x\nindex expect: %d, actual: %d",
|
||||
q15_out_ref, q15_out, index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_max_q15);
|
||||
// mean
|
||||
BENCH_START(riscv_mean_q15);
|
||||
riscv_mean_q15(q15_array, ARRAY_SIZE, &q15_out);
|
||||
BENCH_END(riscv_mean_q15);
|
||||
ref_mean_q15(q15_array, ARRAY_SIZE, &q15_out_ref);
|
||||
if (abs(q15_out - q15_out_ref) > DELTAQ15) {
|
||||
BENCH_ERROR(riscv_mean_q15);
|
||||
printf("expect: %x, actual: %x\n", q15_out_ref, q15_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_mean_q15);
|
||||
// min
|
||||
BENCH_START(riscv_min_q15);
|
||||
riscv_min_q15(q15_array, ARRAY_SIZE, &q15_out, &index);
|
||||
BENCH_END(riscv_min_q15);
|
||||
ref_min_q15(q15_array, ARRAY_SIZE, &q15_out_ref, &index_ref);
|
||||
if ((abs(q15_out - q15_out_ref) > DELTAQ15) || (index != index_ref)) {
|
||||
BENCH_ERROR(riscv_min_q15);
|
||||
printf("value expect: %x, actual: %x\nindex expect: %d, actual: %d",
|
||||
q15_out_ref, q15_out, index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_min_q15);
|
||||
// power
|
||||
BENCH_START(riscv_power_q15);
|
||||
riscv_power_q15(q15_array, ARRAY_SIZE, &q63_out);
|
||||
BENCH_END(riscv_power_q15);
|
||||
ref_power_q15(q15_array, ARRAY_SIZE, &q63_out_ref);
|
||||
if (abs(q15_out - q15_out_ref) > DELTAQ15) {
|
||||
BENCH_ERROR(riscv_power_q15);
|
||||
printf("expect: %x, actual: %x\n", q63_out_ref, q63_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_power_q15);
|
||||
// rms
|
||||
BENCH_START(riscv_rms_q15);
|
||||
riscv_rms_q15(q15_array, ARRAY_SIZE, &q15_out);
|
||||
BENCH_END(riscv_rms_q15);
|
||||
ref_rms_q15(q15_array, ARRAY_SIZE, &q15_out_ref);
|
||||
if (abs(q15_out - q15_out_ref) > DELTAQ15) {
|
||||
BENCH_ERROR(riscv_rms_q15);
|
||||
printf("expect: %x, actual: %x\n", q15_out_ref, q15_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_rms_q15);
|
||||
// std
|
||||
BENCH_START(riscv_std_q15);
|
||||
riscv_std_q15(q15_array, ARRAY_SIZE, &q15_out);
|
||||
BENCH_END(riscv_std_q15);
|
||||
ref_std_q15(q15_array, ARRAY_SIZE, &q15_out_ref);
|
||||
if (abs(q15_out - q15_out_ref) > DELTAQ15) {
|
||||
BENCH_ERROR(riscv_std_q15);
|
||||
printf("expect: %x, actual: %x\n", q15_out_ref, q15_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_std_q15);
|
||||
// var
|
||||
BENCH_START(riscv_var_q15);
|
||||
riscv_var_q15(q15_array, ARRAY_SIZE, &q15_out);
|
||||
BENCH_END(riscv_var_q15);
|
||||
ref_var_q15(q15_array, ARRAY_SIZE, &q15_out_ref);
|
||||
if (abs(q15_out - q15_out_ref) > DELTAQ15) {
|
||||
BENCH_ERROR(riscv_var_q15);
|
||||
printf("expect: %x, actual: %x\n", q15_out_ref, q15_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_var_q15);
|
||||
//absmax
|
||||
BENCH_START(riscv_absmax_q15);
|
||||
riscv_absmax_q15(q15_array, ARRAY_SIZE, &q15_out, &index);
|
||||
BENCH_END(riscv_absmax_q15);
|
||||
ref_absmax_q15(q15_array, ARRAY_SIZE, &q15_out_ref, &index_ref);
|
||||
if (fabs(q15_out - q15_out_ref) > DELTAQ15) {
|
||||
BENCH_ERROR(riscv_absmax_q15);
|
||||
printf("expect: %f, actual: %f\n", q15_out_ref, q15_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
if (fabs(index - index_ref) > DELTAQ15) {
|
||||
BENCH_ERROR(riscv_absmax_q15);
|
||||
printf("expect: %f, actual: %f\n", index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_absmax_q15);
|
||||
//absmin
|
||||
BENCH_START(riscv_absmin_q15);
|
||||
riscv_absmin_q15(q15_array, ARRAY_SIZE, &q15_out, &index);
|
||||
BENCH_END(riscv_absmin_q15);
|
||||
ref_absmin_q15(q15_array, ARRAY_SIZE, &q15_out_ref, &index_ref);
|
||||
if (fabs(q15_out - q15_out_ref) > DELTAQ15) {
|
||||
BENCH_ERROR(riscv_absmin_q15);
|
||||
printf("expect: %f, actual: %f\n", q15_out_ref, q15_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
if (fabs(index - index_ref) > DELTAQ15) {
|
||||
BENCH_ERROR(riscv_absmin_q15);
|
||||
printf("expect: %f, actual: %f\n", index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_absmin_q15);
|
||||
#endif
|
||||
#if defined Q7 || defined ENABLE_ALL
|
||||
// ********************************* q7 *****************************
|
||||
// max
|
||||
BENCH_START(riscv_max_q7);
|
||||
riscv_max_q7(q7_array, ARRAY_SIZE, &q7_out, &index);
|
||||
BENCH_END(riscv_max_q7);
|
||||
ref_max_q7(q7_array, ARRAY_SIZE, &q7_out_ref, &index_ref);
|
||||
if ((abs(q7_out - q7_out_ref) > DELTAQ7) || (index != index_ref)) {
|
||||
BENCH_ERROR(riscv_max_q7);
|
||||
printf("value expect: %x, actual: %x\nindex expect: %d, actual: %d",
|
||||
q7_out_ref, q7_out, index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_max_q7);
|
||||
// mean
|
||||
BENCH_START(riscv_mean_q7);
|
||||
riscv_mean_q7(q7_array, ARRAY_SIZE, &q7_out);
|
||||
BENCH_END(riscv_mean_q7);
|
||||
ref_mean_q7(q7_array, ARRAY_SIZE, &q7_out_ref);
|
||||
if (abs(q7_out - q7_out_ref) > DELTAQ7) {
|
||||
BENCH_ERROR(riscv_mean_q7);
|
||||
printf("expect: %x, actual: %x\n", q7_out_ref, q7_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_mean_q7);
|
||||
// min
|
||||
BENCH_START(riscv_min_q7);
|
||||
riscv_min_q7(q7_array, ARRAY_SIZE, &q7_out, &index);
|
||||
BENCH_END(riscv_min_q7);
|
||||
ref_min_q7(q7_array, ARRAY_SIZE, &q7_out_ref, &index_ref);
|
||||
if ((abs(q7_out - q7_out_ref) > DELTAQ7) || (index != index_ref)) {
|
||||
BENCH_ERROR(riscv_min_q7);
|
||||
printf("value expect: %x, actual: %x\nindex expect: %d, actual: %d",
|
||||
q7_out_ref, q7_out, index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_min_q7);
|
||||
// power
|
||||
BENCH_START(riscv_power_q7);
|
||||
riscv_power_q7(q7_array, ARRAY_SIZE, &q31_out);
|
||||
BENCH_END(riscv_power_q7);
|
||||
ref_power_q7(q7_array, ARRAY_SIZE, &q31_out_ref);
|
||||
if (abs(q31_out - q31_out_ref) > DELTAQ7) {
|
||||
BENCH_ERROR(riscv_power_q7);
|
||||
printf("expect: %x, actual: %x\n", q63_out_ref, q63_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_power_q7);
|
||||
//absmax
|
||||
BENCH_START(riscv_absmax_q7);
|
||||
riscv_absmax_q7(q7_array, ARRAY_SIZE, &q7_out, &index);
|
||||
BENCH_END(riscv_absmax_q7);
|
||||
ref_absmax_q7(q7_array, ARRAY_SIZE, &q7_out_ref, &index_ref);
|
||||
if (fabs(q7_out - q7_out_ref) > DELTAQ7) {
|
||||
BENCH_ERROR(riscv_absmax_q7);
|
||||
printf("expect: %f, actual: %f\n", q7_out_ref, q7_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
if (fabs(index - index_ref) > DELTAQ7) {
|
||||
BENCH_ERROR(riscv_absmax_q7);
|
||||
printf("expect: %f, actual: %f\n", index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_absmax_q7);
|
||||
//absmin
|
||||
BENCH_START(riscv_absmin_q7);
|
||||
riscv_absmin_q7(q7_array, ARRAY_SIZE, &q7_out, &index);
|
||||
BENCH_END(riscv_absmin_q7);
|
||||
ref_absmin_q7(q7_array, ARRAY_SIZE, &q7_out_ref, &index_ref);
|
||||
if (fabs(q7_out - q7_out_ref) > DELTAQ7) {
|
||||
BENCH_ERROR(riscv_absmin_q7);
|
||||
printf("expect: %f, actual: %f\n", q7_out_ref, q7_out);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
if (fabs(index - index_ref) > DELTAQ7) {
|
||||
BENCH_ERROR(riscv_absmin_q7);
|
||||
printf("expect: %f, actual: %f\n", index_ref, index);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_absmin_q7);
|
||||
printf("all tests are passed,well done!\n");
|
||||
#endif
|
||||
BENCH_FINISH;
|
||||
if (test_flag_error) {
|
||||
printf("test error apprears, please recheck.\n");
|
||||
return 1;
|
||||
} else {
|
||||
printf("all test are passed. Well done!\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
77
examples/dsp/StatisticsFunctions_part1/max.c
Normal file
77
examples/dsp/StatisticsFunctions_part1/max.c
Normal file
|
@ -0,0 +1,77 @@
|
|||
#include "ref.h"
|
||||
|
||||
void ref_max_f32(float32_t *pSrc, uint32_t blockSize, float32_t *pResult,
|
||||
uint32_t *pIndex)
|
||||
{
|
||||
uint32_t i, ind = 0;
|
||||
float32_t max = -FLT_MAX;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
if (max < pSrc[i]) {
|
||||
max = pSrc[i];
|
||||
ind = i;
|
||||
}
|
||||
}
|
||||
*pResult = max;
|
||||
*pIndex = ind;
|
||||
}
|
||||
|
||||
void ref_max_no_idx_f32(float32_t *pSrc, uint32_t blockSize, float32_t *pResult)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
float32_t max = -FLT_MAX;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
if (max < pSrc[i]) {
|
||||
max = pSrc[i];
|
||||
}
|
||||
}
|
||||
*pResult = max;
|
||||
}
|
||||
|
||||
void ref_max_q31(q31_t *pSrc, uint32_t blockSize, q31_t *pResult,
|
||||
uint32_t *pIndex)
|
||||
{
|
||||
uint32_t i, ind = 0;
|
||||
q31_t max = INT_MIN;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
if (max < pSrc[i]) {
|
||||
max = pSrc[i];
|
||||
ind = i;
|
||||
}
|
||||
}
|
||||
*pResult = max;
|
||||
*pIndex = ind;
|
||||
}
|
||||
|
||||
void ref_max_q15(q15_t *pSrc, uint32_t blockSize, q15_t *pResult,
|
||||
uint32_t *pIndex)
|
||||
{
|
||||
uint32_t i, ind = 0;
|
||||
q15_t max = SHRT_MIN;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
if (max < pSrc[i]) {
|
||||
max = pSrc[i];
|
||||
ind = i;
|
||||
}
|
||||
}
|
||||
*pResult = max;
|
||||
*pIndex = ind;
|
||||
}
|
||||
|
||||
void ref_max_q7(q7_t *pSrc, uint32_t blockSize, q7_t *pResult, uint32_t *pIndex)
|
||||
{
|
||||
uint32_t i, ind = 0;
|
||||
q7_t max = SCHAR_MIN;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
if (max < pSrc[i]) {
|
||||
max = pSrc[i];
|
||||
ind = i;
|
||||
}
|
||||
}
|
||||
*pResult = max;
|
||||
*pIndex = ind;
|
||||
}
|
45
examples/dsp/StatisticsFunctions_part1/mean.c
Normal file
45
examples/dsp/StatisticsFunctions_part1/mean.c
Normal file
|
@ -0,0 +1,45 @@
|
|||
#include "ref.h"
|
||||
|
||||
void ref_mean_f32(float32_t *pSrc, uint32_t blockSize, float32_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
float32_t sum = 0;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
sum += pSrc[i];
|
||||
}
|
||||
*pResult = sum / (float32_t)blockSize;
|
||||
}
|
||||
|
||||
void ref_mean_q31(q31_t *pSrc, uint32_t blockSize, q31_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
q63_t sum = 0;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
sum += pSrc[i];
|
||||
}
|
||||
*pResult = (q31_t)(sum / (int32_t)blockSize);
|
||||
}
|
||||
|
||||
void ref_mean_q15(q15_t *pSrc, uint32_t blockSize, q15_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
q31_t sum = 0;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
sum += pSrc[i];
|
||||
}
|
||||
*pResult = (q15_t)(sum / (int32_t)blockSize);
|
||||
}
|
||||
|
||||
void ref_mean_q7(q7_t *pSrc, uint32_t blockSize, q7_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
q31_t sum = 0;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
sum += pSrc[i];
|
||||
}
|
||||
*pResult = (q7_t)(sum / (int32_t)blockSize);
|
||||
}
|
64
examples/dsp/StatisticsFunctions_part1/min.c
Normal file
64
examples/dsp/StatisticsFunctions_part1/min.c
Normal file
|
@ -0,0 +1,64 @@
|
|||
#include "ref.h"
|
||||
|
||||
void ref_min_f32(float32_t *pSrc, uint32_t blockSize, float32_t *pResult,
|
||||
uint32_t *pIndex)
|
||||
{
|
||||
uint32_t i, ind = 0;
|
||||
float32_t min = FLT_MAX;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
if (min > pSrc[i]) {
|
||||
min = pSrc[i];
|
||||
ind = i;
|
||||
}
|
||||
}
|
||||
*pResult = min;
|
||||
*pIndex = ind;
|
||||
}
|
||||
|
||||
void ref_min_q31(q31_t *pSrc, uint32_t blockSize, q31_t *pResult,
|
||||
uint32_t *pIndex)
|
||||
{
|
||||
uint32_t i, ind = 0;
|
||||
q31_t min = INT_MAX;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
if (min > pSrc[i]) {
|
||||
min = pSrc[i];
|
||||
ind = i;
|
||||
}
|
||||
}
|
||||
*pResult = min;
|
||||
*pIndex = ind;
|
||||
}
|
||||
|
||||
void ref_min_q15(q15_t *pSrc, uint32_t blockSize, q15_t *pResult,
|
||||
uint32_t *pIndex)
|
||||
{
|
||||
uint32_t i, ind = 0;
|
||||
q15_t min = SHRT_MAX;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
if (min > pSrc[i]) {
|
||||
min = pSrc[i];
|
||||
ind = i;
|
||||
}
|
||||
}
|
||||
*pResult = min;
|
||||
*pIndex = ind;
|
||||
}
|
||||
|
||||
void ref_min_q7(q7_t *pSrc, uint32_t blockSize, q7_t *pResult, uint32_t *pIndex)
|
||||
{
|
||||
uint32_t i, ind = 0;
|
||||
q7_t min = SCHAR_MAX;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
if (min > pSrc[i]) {
|
||||
min = pSrc[i];
|
||||
ind = i;
|
||||
}
|
||||
}
|
||||
*pResult = min;
|
||||
*pIndex = ind;
|
||||
}
|
45
examples/dsp/StatisticsFunctions_part1/power.c
Normal file
45
examples/dsp/StatisticsFunctions_part1/power.c
Normal file
|
@ -0,0 +1,45 @@
|
|||
#include "ref.h"
|
||||
|
||||
void ref_power_f32(float32_t *pSrc, uint32_t blockSize, float32_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
float32_t sumsq = 0;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
sumsq += pSrc[i] * pSrc[i];
|
||||
}
|
||||
*pResult = sumsq;
|
||||
}
|
||||
|
||||
void ref_power_q31(q31_t *pSrc, uint32_t blockSize, q63_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
q63_t sumsq = 0;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
sumsq += ((q63_t)pSrc[i] * pSrc[i]) >> 14;
|
||||
}
|
||||
*pResult = sumsq;
|
||||
}
|
||||
|
||||
void ref_power_q15(q15_t *pSrc, uint32_t blockSize, q63_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
q63_t sumsq = 0;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
sumsq += (q63_t)pSrc[i] * pSrc[i];
|
||||
}
|
||||
*pResult = sumsq;
|
||||
}
|
||||
|
||||
void ref_power_q7(q7_t *pSrc, uint32_t blockSize, q31_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
q31_t sumsq = 0;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
sumsq += (q31_t)pSrc[i] * pSrc[i];
|
||||
}
|
||||
*pResult = sumsq;
|
||||
}
|
54
examples/dsp/StatisticsFunctions_part1/rms.c
Normal file
54
examples/dsp/StatisticsFunctions_part1/rms.c
Normal file
|
@ -0,0 +1,54 @@
|
|||
#include "ref.h"
|
||||
|
||||
void ref_rms_f32(float32_t *pSrc, uint32_t blockSize, float32_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
float32_t sumsq = 0;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
sumsq += pSrc[i] * pSrc[i];
|
||||
}
|
||||
*pResult = sqrtf(sumsq / (float32_t)blockSize);
|
||||
}
|
||||
|
||||
void ref_rms_q31(q31_t *pSrc, uint32_t blockSize, q31_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
uint64_t sumsq = 0; /* accumulator (can get never negative. changed type
|
||||
from q63 to uint64 */
|
||||
q63_t tmp1;
|
||||
q31_t tmp2;
|
||||
|
||||
float help_float;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
sumsq += (q63_t)pSrc[i] * pSrc[i];
|
||||
}
|
||||
tmp1 = (sumsq / (q63_t)blockSize) >> 31;
|
||||
tmp2 = ref_sat_q31(tmp1);
|
||||
|
||||
/* GCC M0 problem: __aeabi_f2iz(QNAN) returns not 0 */
|
||||
help_float = (sqrtf((float)tmp2 / 2147483648.0f) * 2147483648.0f);
|
||||
/* Checking for a NAN value in help_float */
|
||||
if (((*((int *)(&help_float))) & 0x7FC00000) == 0x7FC00000) {
|
||||
help_float = 0;
|
||||
}
|
||||
*pResult = (q31_t)(help_float);
|
||||
|
||||
// *pResult = (q31_t)(sqrtf((float)tmp2 / 2147483648.0f) * 2147483648.0f);
|
||||
}
|
||||
|
||||
void ref_rms_q15(q15_t *pSrc, uint32_t blockSize, q15_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
q63_t sumsq = 0;
|
||||
q31_t tmp1;
|
||||
q15_t tmp2;
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
sumsq += (q63_t)pSrc[i] * pSrc[i];
|
||||
}
|
||||
tmp1 = (sumsq / (q63_t)blockSize) >> 15;
|
||||
tmp2 = ref_sat_q15(tmp1);
|
||||
*pResult = (q15_t)(sqrtf((float)tmp2 / 32768.0f) * 32768.0f);
|
||||
}
|
62
examples/dsp/StatisticsFunctions_part1/std.c
Normal file
62
examples/dsp/StatisticsFunctions_part1/std.c
Normal file
|
@ -0,0 +1,62 @@
|
|||
#include "ref.h"
|
||||
|
||||
void ref_std_f32(float32_t *pSrc, uint32_t blockSize, float32_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
float32_t sum = 0, sumsq = 0;
|
||||
|
||||
if (blockSize == 1) {
|
||||
*pResult = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
sum += pSrc[i];
|
||||
sumsq += pSrc[i] * pSrc[i];
|
||||
}
|
||||
*pResult = sqrtf((sumsq - sum * sum / (float32_t)blockSize) /
|
||||
((float32_t)blockSize - 1));
|
||||
}
|
||||
|
||||
void ref_std_q31(q31_t *pSrc, uint32_t blockSize, q31_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
q63_t sum = 0, sumsq = 0;
|
||||
q31_t in;
|
||||
|
||||
if (blockSize == 1) {
|
||||
*pResult = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
in = pSrc[i] >> 8;
|
||||
sum += in;
|
||||
sumsq += (q63_t)in * in;
|
||||
}
|
||||
sumsq /= (q63_t)(blockSize - 1);
|
||||
sum = sum * sum / (q63_t)(blockSize * (blockSize - 1));
|
||||
*pResult = (q31_t)(sqrtf((float)((sumsq - sum) >> 15) / 2147483648.0f) *
|
||||
2147483648.0f);
|
||||
}
|
||||
|
||||
void ref_std_q15(q15_t *pSrc, uint32_t blockSize, q15_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
q31_t sum = 0;
|
||||
q63_t sumsq = 0;
|
||||
|
||||
if (blockSize == 1) {
|
||||
*pResult = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
sum += pSrc[i];
|
||||
sumsq += (q63_t)pSrc[i] * pSrc[i];
|
||||
}
|
||||
sumsq /= (q63_t)(blockSize - 1);
|
||||
sum = (q31_t)((q63_t)sum * sum / (q63_t)(blockSize * (blockSize - 1)));
|
||||
*pResult = (q15_t)(
|
||||
sqrtf((float)ref_sat_q15((sumsq - sum) >> 15) / 32768.0f) * 32768.0f);
|
||||
}
|
59
examples/dsp/StatisticsFunctions_part1/var.c
Normal file
59
examples/dsp/StatisticsFunctions_part1/var.c
Normal file
|
@ -0,0 +1,59 @@
|
|||
#include "ref.h"
|
||||
|
||||
void ref_var_f32(float32_t *pSrc, uint32_t blockSize, float32_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
float32_t sum = 0, sumsq = 0;
|
||||
|
||||
if (blockSize == 1) {
|
||||
*pResult = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
sum += pSrc[i];
|
||||
sumsq += pSrc[i] * pSrc[i];
|
||||
}
|
||||
*pResult =
|
||||
(sumsq - sum * sum / (float32_t)blockSize) / ((float32_t)blockSize - 1);
|
||||
}
|
||||
|
||||
void ref_var_q31(q31_t *pSrc, uint32_t blockSize, q31_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
q63_t sum = 0, sumsq = 0;
|
||||
q31_t in;
|
||||
|
||||
if (blockSize == 1) {
|
||||
*pResult = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
in = pSrc[i] >> 8;
|
||||
sum += in;
|
||||
sumsq += (q63_t)in * in;
|
||||
}
|
||||
*pResult =
|
||||
(sumsq - sum * sum / (q31_t)blockSize) / ((q31_t)blockSize - 1) >> 15;
|
||||
}
|
||||
|
||||
void ref_var_q15(q15_t *pSrc, uint32_t blockSize, q15_t *pResult)
|
||||
{
|
||||
uint32_t i;
|
||||
q31_t sum = 0;
|
||||
q63_t sumsq = 0;
|
||||
|
||||
if (blockSize == 1) {
|
||||
*pResult = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < blockSize; i++) {
|
||||
sum += pSrc[i];
|
||||
sumsq += (q63_t)pSrc[i] * pSrc[i];
|
||||
}
|
||||
*pResult = (q31_t)((sumsq - (q63_t)sum * sum / (q63_t)blockSize) /
|
||||
((q63_t)blockSize - 1)) >>
|
||||
15;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue