1 /*
2  * Copyright (c) 2020 Stephanos Ioannidis <root@stephanos.io>
3  * Copyright (C) 2010-2020 ARM Limited or its affiliates. All rights reserved.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/ztest.h>
9 #include <zephyr/kernel.h>
10 #include <stdlib.h>
11 #include <arm_math.h>
12 #include "../../common/test_common.h"
13 
14 #include "fir_f32.pat"
15 
16 #define SNR_ERROR_THRESH	((float32_t)120)
17 #define REL_ERROR_THRESH	(3.0e-5)
18 
19 #define COEFF_PADDING		(4)
20 
ZTEST(filtering_fir_f32,test_arm_fir_f32)21 ZTEST(filtering_fir_f32, test_arm_fir_f32)
22 {
23 	size_t sample_index, block_index;
24 	size_t block_size, tap_count;
25 	size_t sample_count = ARRAY_SIZE(in_config) / 2;
26 	size_t length = ARRAY_SIZE(ref_val);
27 	const uint16_t *config = in_config;
28 	const float32_t *input = (const float32_t *)in_val;
29 	const float32_t *coeff = (const float32_t *)in_coeff;
30 	const float32_t *ref = (const float32_t *)ref_val;
31 	float32_t *state, *output_buf, *output;
32 	arm_fir_instance_f32 inst;
33 #if defined(CONFIG_ARMV8_1_M_MVEF) && defined(CONFIG_FPU)
34 	float32_t coeff_padded[32];
35 	int round;
36 #endif
37 
38 	/* Allocate buffers */
39 	state = malloc(2 * 47 * sizeof(float32_t));
40 	zassert_not_null(state, ASSERT_MSG_BUFFER_ALLOC_FAILED);
41 
42 	output_buf = malloc(length * sizeof(float32_t));
43 	zassert_not_null(output_buf, ASSERT_MSG_BUFFER_ALLOC_FAILED);
44 
45 	output = output_buf;
46 
47 	/* Enumerate samples */
48 	for (sample_index = 0; sample_index < sample_count; sample_index++) {
49 		/* Resolve sample configurations */
50 		block_size = config[0];
51 		tap_count = config[1];
52 
53 #if defined(CONFIG_ARMV8_1_M_MVEF) && defined(CONFIG_FPU)
54 		/* Copy coefficients and pad to zero */
55 		memset(coeff_padded, 127, sizeof(coeff_padded));
56 		round = tap_count / COEFF_PADDING;
57 		if ((round * COEFF_PADDING) < tap_count) {
58 			round++;
59 		}
60 		round = round * COEFF_PADDING;
61 		memset(coeff_padded, 0, round * sizeof(float32_t));
62 		memcpy(coeff_padded, coeff, tap_count * sizeof(float32_t));
63 #endif
64 
65 		/* Initialise instance */
66 #if defined(CONFIG_ARMV8_1_M_MVEF) && defined(CONFIG_FPU)
67 		arm_fir_init_f32(&inst, tap_count, coeff_padded, state, block_size);
68 #else
69 		arm_fir_init_f32(&inst, tap_count, coeff, state, block_size);
70 #endif
71 
72 		/* Reset input pointer */
73 		input = (const float32_t *)in_val;
74 
75 		/* Enumerate blocks */
76 		for (block_index = 0; block_index < 2; block_index++) {
77 			/* Run test function */
78 			arm_fir_f32(&inst, input, output, block_size);
79 
80 			/* Increment pointers */
81 			input += block_size;
82 			output += block_size;
83 		}
84 
85 		/* Increment pointers */
86 		coeff += tap_count;
87 		config += 2;
88 	}
89 
90 	/* Validate output */
91 	zassert_true(
92 		test_snr_error_f32(length, output_buf, ref, SNR_ERROR_THRESH),
93 		ASSERT_MSG_SNR_LIMIT_EXCEED);
94 
95 	zassert_true(
96 		test_rel_error_f32(length, output_buf, ref, REL_ERROR_THRESH),
97 		ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
98 
99 	/* Free buffers */
100 	free(state);
101 	free(output_buf);
102 }
103 
104 ZTEST_SUITE(filtering_fir_f32, NULL, NULL, NULL, NULL, NULL);
105