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 <arm_const_structs.h>
13 #include "../../common/test_common.h"
14 
15 #include "cq31.pat"
16 
17 #define SNR_ERROR_THRESH	((float32_t)90)
18 
test_arm_cfft_q31(const q31_t * input,const q31_t * ref,size_t length)19 static void test_arm_cfft_q31(
20 	const q31_t *input, const q31_t *ref, size_t length)
21 {
22 	arm_cfft_instance_q31 inst;
23 	q31_t *output;
24 	arm_status status;
25 
26 	/* Initialise instance */
27 	status = arm_cfft_init_q31(&inst, length / 2);
28 
29 	zassert_equal(status, ARM_MATH_SUCCESS,
30 		      ASSERT_MSG_INCORRECT_COMP_RESULT);
31 
32 	/* Allocate output buffer */
33 	output = malloc(length * sizeof(q31_t));
34 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
35 
36 	/* Load data in place */
37 	memcpy(output, input, length * sizeof(q31_t));
38 
39 	/* Run test function */
40 	arm_cfft_q31(&inst, output, false, true);
41 
42 	/* Validate output */
43 	zassert_true(
44 		test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
45 		ASSERT_MSG_SNR_LIMIT_EXCEED);
46 
47 	/* Free output buffer */
48 	free(output);
49 }
50 
51 DEFINE_TEST_VARIANT3(transform_cq31,
52 	arm_cfft_q31, noisy_16,
53 	in_cfft_noisy_16, ref_cfft_noisy_16, 32);
54 
55 DEFINE_TEST_VARIANT3(transform_cq31,
56 	arm_cfft_q31, noisy_32,
57 	in_cfft_noisy_32, ref_cfft_noisy_32, 64);
58 
59 DEFINE_TEST_VARIANT3(transform_cq31,
60 	arm_cfft_q31, noisy_64,
61 	in_cfft_noisy_64, ref_cfft_noisy_64, 128);
62 
63 DEFINE_TEST_VARIANT3(transform_cq31,
64 	arm_cfft_q31, noisy_128,
65 	in_cfft_noisy_128, ref_cfft_noisy_128, 256);
66 
67 DEFINE_TEST_VARIANT3(transform_cq31,
68 	arm_cfft_q31, noisy_256,
69 	in_cfft_noisy_256, ref_cfft_noisy_256, 512);
70 
71 DEFINE_TEST_VARIANT3(transform_cq31,
72 	arm_cfft_q31, noisy_512,
73 	in_cfft_noisy_512, ref_cfft_noisy_512, 1024);
74 
75 DEFINE_TEST_VARIANT3(transform_cq31,
76 	arm_cfft_q31, noisy_1024,
77 	in_cfft_noisy_1024, ref_cfft_noisy_1024, 2048);
78 
79 DEFINE_TEST_VARIANT3(transform_cq31,
80 	arm_cfft_q31, noisy_2048,
81 	in_cfft_noisy_2048, ref_cfft_noisy_2048, 4096);
82 
83 DEFINE_TEST_VARIANT3(transform_cq31,
84 	arm_cfft_q31, noisy_4096,
85 	in_cfft_noisy_4096, ref_cfft_noisy_4096, 8192);
86 
87 DEFINE_TEST_VARIANT3(transform_cq31,
88 	arm_cfft_q31, step_16,
89 	in_cfft_step_16, ref_cfft_step_16, 32);
90 
91 DEFINE_TEST_VARIANT3(transform_cq31,
92 	arm_cfft_q31, step_32,
93 	in_cfft_step_32, ref_cfft_step_32, 64);
94 
95 DEFINE_TEST_VARIANT3(transform_cq31,
96 	arm_cfft_q31, step_64,
97 	in_cfft_step_64, ref_cfft_step_64, 128);
98 
99 DEFINE_TEST_VARIANT3(transform_cq31,
100 	arm_cfft_q31, step_128,
101 	in_cfft_step_128, ref_cfft_step_128, 256);
102 
103 DEFINE_TEST_VARIANT3(transform_cq31,
104 	arm_cfft_q31, step_256,
105 	in_cfft_step_256, ref_cfft_step_256, 512);
106 
107 DEFINE_TEST_VARIANT3(transform_cq31,
108 	arm_cfft_q31, step_512,
109 	in_cfft_step_512, ref_cfft_step_512, 1024);
110 
111 DEFINE_TEST_VARIANT3(transform_cq31,
112 	arm_cfft_q31, step_1024,
113 	in_cfft_step_1024, ref_cfft_step_1024, 2048);
114 
115 DEFINE_TEST_VARIANT3(transform_cq31,
116 	arm_cfft_q31, step_2048,
117 	in_cfft_step_2048, ref_cfft_step_2048, 4096);
118 
119 DEFINE_TEST_VARIANT3(transform_cq31,
120 	arm_cfft_q31, step_4096,
121 	in_cfft_step_4096, ref_cfft_step_4096, 8192);
122 
test_arm_cifft_q31(int scale_factor,const q31_t * input,const q31_t * ref,size_t length)123 static void test_arm_cifft_q31(
124 	int scale_factor, const q31_t *input, const q31_t *ref, size_t length)
125 {
126 	arm_cfft_instance_q31 inst;
127 	size_t index;
128 	q31_t *output, *scaled_ref;
129 	arm_status status;
130 
131 	/* Initialise instance */
132 	status = arm_cfft_init_q31(&inst, length / 2);
133 
134 	zassert_equal(status, ARM_MATH_SUCCESS,
135 		      ASSERT_MSG_INCORRECT_COMP_RESULT);
136 
137 	/* Allocate buffers */
138 	output = malloc(length * sizeof(q31_t));
139 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
140 
141 	scaled_ref = malloc(length * sizeof(q31_t));
142 	zassert_not_null(scaled_ref, ASSERT_MSG_BUFFER_ALLOC_FAILED);
143 
144 	/* Load data in place */
145 	memcpy(output, input, length * sizeof(q31_t));
146 
147 	/* Run test function */
148 	arm_cfft_q31(&inst, output, true, true);
149 
150 	/* Scale reference data */
151 	for (index = 0; index < length; index++) {
152 		scaled_ref[index] = ref[index] >> scale_factor;
153 	}
154 
155 	/* Validate output */
156 	zassert_true(
157 		test_snr_error_q31(length, output, scaled_ref,
158 			SNR_ERROR_THRESH),
159 		ASSERT_MSG_SNR_LIMIT_EXCEED);
160 
161 	/* Free output buffer */
162 	free(output);
163 	free(scaled_ref);
164 }
165 
166 DEFINE_TEST_VARIANT4(transform_cq31,
167 	arm_cifft_q31, noisy_16, 4,
168 	in_cifft_noisy_16, in_cfft_noisy_16, 32);
169 
170 DEFINE_TEST_VARIANT4(transform_cq31,
171 	arm_cifft_q31, noisy_32, 5,
172 	in_cifft_noisy_32, in_cfft_noisy_32, 64);
173 
174 DEFINE_TEST_VARIANT4(transform_cq31,
175 	arm_cifft_q31, noisy_64, 6,
176 	in_cifft_noisy_64, in_cfft_noisy_64, 128);
177 
178 DEFINE_TEST_VARIANT4(transform_cq31,
179 	arm_cifft_q31, noisy_128, 7,
180 	in_cifft_noisy_128, in_cfft_noisy_128, 256);
181 
182 DEFINE_TEST_VARIANT4(transform_cq31,
183 	arm_cifft_q31, noisy_256, 8,
184 	in_cifft_noisy_256, in_cfft_noisy_256, 512);
185 
186 DEFINE_TEST_VARIANT4(transform_cq31,
187 	arm_cifft_q31, noisy_512, 9,
188 	in_cifft_noisy_512, in_cfft_noisy_512, 1024);
189 
190 DEFINE_TEST_VARIANT4(transform_cq31,
191 	arm_cifft_q31, noisy_1024, 10,
192 	in_cifft_noisy_1024, in_cfft_noisy_1024, 2048);
193 
194 DEFINE_TEST_VARIANT4(transform_cq31,
195 	arm_cifft_q31, noisy_2048, 11,
196 	in_cifft_noisy_2048, in_cfft_noisy_2048, 4096);
197 
198 DEFINE_TEST_VARIANT4(transform_cq31,
199 	arm_cifft_q31, noisy_4096, 12,
200 	in_cifft_noisy_4096, in_cfft_noisy_4096, 8192);
201 
202 DEFINE_TEST_VARIANT4(transform_cq31,
203 	arm_cifft_q31, step_16, 4,
204 	in_cifft_step_16, in_cfft_step_16, 32);
205 
206 DEFINE_TEST_VARIANT4(transform_cq31,
207 	arm_cifft_q31, step_32, 5,
208 	in_cifft_step_32, in_cfft_step_32, 64);
209 
210 DEFINE_TEST_VARIANT4(transform_cq31,
211 	arm_cifft_q31, step_64, 6,
212 	in_cifft_step_64, in_cfft_step_64, 128);
213 
214 DEFINE_TEST_VARIANT4(transform_cq31,
215 	arm_cifft_q31, step_128, 7,
216 	in_cifft_step_128, in_cfft_step_128, 256);
217 
218 DEFINE_TEST_VARIANT4(transform_cq31,
219 	arm_cifft_q31, step_256, 8,
220 	in_cifft_step_256, in_cfft_step_256, 512);
221 
222 DEFINE_TEST_VARIANT4(transform_cq31,
223 	arm_cifft_q31, step_512, 9,
224 	in_cifft_step_512, in_cfft_step_512, 1024);
225 
226 DEFINE_TEST_VARIANT4(transform_cq31,
227 	arm_cifft_q31, step_1024, 10,
228 	in_cifft_step_1024, in_cfft_step_1024, 2048);
229 
230 DEFINE_TEST_VARIANT4(transform_cq31,
231 	arm_cifft_q31, step_2048, 11,
232 	in_cifft_step_2048, in_cfft_step_2048, 4096);
233 
234 DEFINE_TEST_VARIANT4(transform_cq31,
235 	arm_cifft_q31, step_4096, 12,
236 	in_cifft_step_4096, in_cfft_step_4096, 8192);
237 
238 ZTEST_SUITE(transform_cq31, NULL, NULL, NULL, NULL, NULL);
239