1 /*
2  * Copyright (c) 2021 Stephanos Ioannidis <root@stephanos.io>
3  * Copyright (C) 2010-2021 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 "f32.pat"
15 
16 #define SNR_ERROR_THRESH	((float32_t)120)
17 #define REL_ERROR_THRESH	(8.0e-5)
18 
test_arm_linear_interp_f32(void)19 void test_arm_linear_interp_f32(void)
20 {
21 	arm_linear_interp_instance_f32 inst;
22 	size_t index;
23 	size_t length = ARRAY_SIZE(ref_linear);
24 	const float32_t *input = (const float32_t *)in_linear_x;
25 	float32_t *output;
26 
27 	/* Initialise instance */
28 	inst.nValues = ARRAY_SIZE(in_linear_y);
29 	inst.x1 = 0.0;
30 	inst.xSpacing = 1.0;
31 	inst.pYData = (float32_t *)in_linear_y;
32 
33 	/* Allocate output buffer */
34 	output = malloc(length * sizeof(float32_t));
35 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
36 
37 	/* Run test function */
38 	for (index = 0; index < length; index++) {
39 		output[index] = arm_linear_interp_f32(&inst,
40 						      input[index]);
41 	}
42 
43 	/* Validate output */
44 	zassert_true(
45 		test_snr_error_f32(length, output, (float32_t *)ref_linear,
46 			SNR_ERROR_THRESH),
47 		ASSERT_MSG_SNR_LIMIT_EXCEED);
48 
49 	zassert_true(
50 		test_rel_error_f32(length, output, (float32_t *)ref_linear,
51 			REL_ERROR_THRESH),
52 		ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
53 
54 	/* Free output buffer */
55 	free(output);
56 }
57 
test_arm_bilinear_interp_f32(void)58 void test_arm_bilinear_interp_f32(void)
59 {
60 	arm_bilinear_interp_instance_f32 inst;
61 	size_t index;
62 	size_t length = ARRAY_SIZE(ref_bilinear);
63 	const float32_t *input = (const float32_t *)in_bilinear_x;
64 	float32_t *output;
65 
66 	/* Initialise instance */
67 	inst.numRows = in_bilinear_config[1];
68 	inst.numCols = in_bilinear_config[0];
69 	inst.pData = (float32_t *)in_bilinear_y;
70 
71 	/* Allocate output buffer */
72 	output = malloc(length * sizeof(float32_t));
73 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
74 
75 	/* Run test function */
76 	for (index = 0; index < length; index++) {
77 		output[index] = arm_bilinear_interp_f32(
78 					&inst,
79 					input[2 * index],
80 					input[2 * index + 1]
81 					);
82 	}
83 
84 	/* Validate output */
85 	zassert_true(
86 		test_snr_error_f32(length, output, (float32_t *)ref_bilinear,
87 			SNR_ERROR_THRESH),
88 		ASSERT_MSG_SNR_LIMIT_EXCEED);
89 
90 	zassert_true(
91 		test_rel_error_f32(length, output, (float32_t *)ref_bilinear,
92 			REL_ERROR_THRESH),
93 		ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
94 
95 	/* Free output buffer */
96 	free(output);
97 }
98 
test_arm_spline(const uint32_t * input_x,const uint32_t * input_y,const uint32_t * input_xq,const uint32_t * ref,size_t length,uint32_t n,arm_spline_type type)99 static void test_arm_spline(
100 	const uint32_t *input_x, const uint32_t *input_y,
101 	const uint32_t *input_xq, const uint32_t *ref, size_t length,
102 	uint32_t n, arm_spline_type type)
103 {
104 	float32_t *output;
105 	float32_t *scratch;
106 	float32_t *coeff;
107 	arm_spline_instance_f32 inst;
108 
109 	/* Allocate output buffer */
110 	output = malloc(length * sizeof(float32_t));
111 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
112 
113 	/* Allocate scratch buffer */
114 	scratch = malloc(((n * 2) - 1) * sizeof(float32_t));
115 	zassert_not_null(scratch, ASSERT_MSG_BUFFER_ALLOC_FAILED);
116 
117 	/* Allocate coefficient buffer */
118 	coeff = malloc(((n - 1) * 3) * sizeof(float32_t));
119 	zassert_not_null(coeff, ASSERT_MSG_BUFFER_ALLOC_FAILED);
120 
121 	/* Initialise spline */
122 	arm_spline_init_f32(&inst, type,
123 		(float32_t *)input_x, (float32_t *)input_y, n, coeff, scratch);
124 
125 	/* Run test function */
126 	arm_spline_f32(&inst, (float32_t *)input_xq, output, length);
127 
128 	/* Validate output */
129 	zassert_true(
130 		test_snr_error_f32(length, output, (float32_t *)ref,
131 			SNR_ERROR_THRESH),
132 		ASSERT_MSG_SNR_LIMIT_EXCEED);
133 
134 	/* Free output buffer */
135 	free(output);
136 	free(scratch);
137 	free(coeff);
138 }
139 
140 DEFINE_TEST_VARIANT7(interpolation_f32, arm_spline, square_20,
141 	in_spline_squ_x, in_spline_squ_y, in_spline_squ_xq, ref_spline_squ, 20,
142 	4, ARM_SPLINE_PARABOLIC_RUNOUT);
143 
144 DEFINE_TEST_VARIANT7(interpolation_f32, arm_spline, sine_33,
145 	in_spline_sin_x, in_spline_sin_y, in_spline_sin_xq, ref_spline_sin, 33,
146 	9, ARM_SPLINE_NATURAL);
147 
148 DEFINE_TEST_VARIANT7(interpolation_f32, arm_spline, ramp_30,
149 	in_spline_ram_x, in_spline_ram_y, in_spline_ram_xq, ref_spline_ram, 30,
150 	3, ARM_SPLINE_PARABOLIC_RUNOUT);
151 
152 ZTEST_SUITE(interpolation_f32, NULL, NULL, NULL, NULL, NULL);
153