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 /*
9  * NOTE: The following tests are disabled because the current Q15 IFFT
10  *       implementation cannot achieve sufficient accuracy to pass them:
11  *
12  *       test_arm_rifft_q15_noisy_256
13  *       test_arm_rifft_q15_noisy_512
14  *       test_arm_rifft_q15_noisy_1024
15  *       test_arm_rifft_q15_noisy_2048
16  *       test_arm_rifft_q15_noisy_4096
17  *       test_arm_rifft_q15_step_256
18  *       test_arm_rifft_q15_step_512
19  *       test_arm_rifft_q15_step_1024
20  *       test_arm_rifft_q15_step_2048
21  *       test_arm_rifft_q15_step_4096
22  */
23 
24 #include <zephyr/ztest.h>
25 #include <zephyr/kernel.h>
26 #include <stdlib.h>
27 #include <arm_math.h>
28 #include <arm_const_structs.h>
29 #include "../../common/test_common.h"
30 
31 #include "rq15.pat"
32 
33 #define SNR_ERROR_THRESH_FFT	((float32_t)40)
34 #define SNR_ERROR_THRESH_IFFT	((float32_t)25)
35 
test_arm_rfft_q15(const q15_t * input,const q15_t * ref,size_t length)36 static void test_arm_rfft_q15(
37 	const q15_t *input, const q15_t *ref, size_t length)
38 {
39 	arm_rfft_instance_q15 inst;
40 	q15_t *scratch, *output;
41 
42 	/* Initialise instance */
43 	arm_rfft_init_q15(&inst, length, false, true);
44 
45 	/* Allocate buffers */
46 	scratch = malloc(length * sizeof(q15_t));
47 	zassert_not_null(scratch, ASSERT_MSG_BUFFER_ALLOC_FAILED);
48 
49 	output = malloc(2 * length * sizeof(q15_t));
50 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
51 
52 	/* Load input data into the scratch buffer */
53 	memcpy(scratch, input, length * sizeof(q15_t));
54 
55 	/* Run test function */
56 	arm_rfft_q15(&inst, scratch, output);
57 
58 	/* Validate output */
59 	zassert_true(
60 		test_snr_error_q15(length, output, ref, SNR_ERROR_THRESH_FFT),
61 		ASSERT_MSG_SNR_LIMIT_EXCEED);
62 
63 	/* Free output buffer */
64 	free(scratch);
65 	free(output);
66 }
67 
68 DEFINE_TEST_VARIANT3(transform_rq15,
69 	arm_rfft_q15, noisy_32,
70 	in_rfft_noisy_32, ref_rfft_noisy_32, 32);
71 
72 DEFINE_TEST_VARIANT3(transform_rq15,
73 	arm_rfft_q15, noisy_64,
74 	in_rfft_noisy_64, ref_rfft_noisy_64, 64);
75 
76 DEFINE_TEST_VARIANT3(transform_rq15,
77 	arm_rfft_q15, noisy_128,
78 	in_rfft_noisy_128, ref_rfft_noisy_128, 128);
79 
80 DEFINE_TEST_VARIANT3(transform_rq15,
81 	arm_rfft_q15, noisy_256,
82 	in_rfft_noisy_256, ref_rfft_noisy_256, 256);
83 
84 DEFINE_TEST_VARIANT3(transform_rq15,
85 	arm_rfft_q15, noisy_512,
86 	in_rfft_noisy_512, ref_rfft_noisy_512, 512);
87 
88 DEFINE_TEST_VARIANT3(transform_rq15,
89 	arm_rfft_q15, noisy_1024,
90 	in_rfft_noisy_1024, ref_rfft_noisy_1024, 1024);
91 
92 DEFINE_TEST_VARIANT3(transform_rq15,
93 	arm_rfft_q15, noisy_2048,
94 	in_rfft_noisy_2048, ref_rfft_noisy_2048, 2048);
95 
96 DEFINE_TEST_VARIANT3(transform_rq15,
97 	arm_rfft_q15, noisy_4096,
98 	in_rfft_noisy_4096, ref_rfft_noisy_4096, 4096);
99 
100 DEFINE_TEST_VARIANT3(transform_rq15,
101 	arm_rfft_q15, step_32,
102 	in_rfft_step_32, ref_rfft_step_32, 32);
103 
104 DEFINE_TEST_VARIANT3(transform_rq15,
105 	arm_rfft_q15, step_64,
106 	in_rfft_step_64, ref_rfft_step_64, 64);
107 
108 DEFINE_TEST_VARIANT3(transform_rq15,
109 	arm_rfft_q15, step_128,
110 	in_rfft_step_128, ref_rfft_step_128, 128);
111 
112 DEFINE_TEST_VARIANT3(transform_rq15,
113 	arm_rfft_q15, step_256,
114 	in_rfft_step_256, ref_rfft_step_256, 256);
115 
116 DEFINE_TEST_VARIANT3(transform_rq15,
117 	arm_rfft_q15, step_512,
118 	in_rfft_step_512, ref_rfft_step_512, 512);
119 
120 DEFINE_TEST_VARIANT3(transform_rq15,
121 	arm_rfft_q15, step_1024,
122 	in_rfft_step_1024, ref_rfft_step_1024, 1024);
123 
124 DEFINE_TEST_VARIANT3(transform_rq15,
125 	arm_rfft_q15, step_2048,
126 	in_rfft_step_2048, ref_rfft_step_2048, 2048);
127 
128 DEFINE_TEST_VARIANT3(transform_rq15,
129 	arm_rfft_q15, step_4096,
130 	in_rfft_step_4096, ref_rfft_step_4096, 4096);
131 
test_arm_rifft_q15(int scale_factor,const q15_t * input,const q15_t * ref,size_t length)132 static void test_arm_rifft_q15(
133 	int scale_factor, const q15_t *input, const q15_t *ref, size_t length)
134 {
135 	size_t index;
136 	arm_rfft_instance_q15 inst;
137 	q15_t *scratch, *output;
138 
139 	/* Initialise instance */
140 	arm_rfft_init_q15(&inst, length, true, true);
141 
142 	/* Allocate buffers */
143 	scratch = calloc(length + 2, sizeof(q15_t)); /* see #24701 */
144 	zassert_not_null(scratch, ASSERT_MSG_BUFFER_ALLOC_FAILED);
145 
146 	output = malloc(2 * length * sizeof(q15_t));
147 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
148 
149 	/* Load input data into the scratch buffer */
150 	memcpy(scratch, input, length * sizeof(q15_t));
151 
152 	/* Run test function */
153 	arm_rfft_q15(&inst, scratch, output);
154 
155 	/* Scale reference data */
156 	for (index = 0; index < length; index++) {
157 		output[index] = output[index] << scale_factor;
158 	}
159 
160 	/* Validate output */
161 	zassert_true(
162 		test_snr_error_q15(length, output, ref, SNR_ERROR_THRESH_IFFT),
163 		ASSERT_MSG_SNR_LIMIT_EXCEED);
164 
165 	/* Free output buffer */
166 	free(scratch);
167 	free(output);
168 }
169 
170 DEFINE_TEST_VARIANT4(transform_rq15,
171 	arm_rifft_q15, noisy_32, 5,
172 	in_rifft_noisy_32, in_rfft_noisy_32, 32);
173 
174 DEFINE_TEST_VARIANT4(transform_rq15,
175 	arm_rifft_q15, noisy_64, 6,
176 	in_rifft_noisy_64, in_rfft_noisy_64, 64);
177 
178 DEFINE_TEST_VARIANT4(transform_rq15,
179 	arm_rifft_q15, noisy_128, 7,
180 	in_rifft_noisy_128, in_rfft_noisy_128, 128);
181 
182 #if 0
183 DEFINE_TEST_VARIANT4(transform_rq15,
184 	arm_rifft_q15, noisy_256, 8,
185 	in_rifft_noisy_256, in_rfft_noisy_256, 256);
186 
187 DEFINE_TEST_VARIANT4(transform_rq15,
188 	arm_rifft_q15, noisy_512, 9,
189 	in_rifft_noisy_512, in_rfft_noisy_512, 512);
190 
191 DEFINE_TEST_VARIANT4(transform_rq15,
192 	arm_rifft_q15, noisy_1024, 10,
193 	in_rifft_noisy_1024, in_rfft_noisy_1024, 1024);
194 
195 DEFINE_TEST_VARIANT4(transform_rq15,
196 	arm_rifft_q15, noisy_2048, 11,
197 	in_rifft_noisy_2048, in_rfft_noisy_2048, 2048);
198 
199 DEFINE_TEST_VARIANT4(transform_rq15,
200 	arm_rifft_q15, noisy_4096, 12,
201 	in_rifft_noisy_4096, in_rfft_noisy_4096, 4096);
202 #endif
203 
204 DEFINE_TEST_VARIANT4(transform_rq15,
205 	arm_rifft_q15, step_32, 5,
206 	in_rifft_step_32, in_rfft_step_32, 32);
207 
208 DEFINE_TEST_VARIANT4(transform_rq15,
209 	arm_rifft_q15, step_64, 6,
210 	in_rifft_step_64, in_rfft_step_64, 64);
211 
212 DEFINE_TEST_VARIANT4(transform_rq15,
213 	arm_rifft_q15, step_128, 7,
214 	in_rifft_step_128, in_rfft_step_128, 128);
215 
216 #if 0
217 DEFINE_TEST_VARIANT4(transform_rq15,
218 	arm_rifft_q15, step_256, 8,
219 	in_rifft_step_256, in_rfft_step_256, 256);
220 
221 DEFINE_TEST_VARIANT4(transform_rq15,
222 	arm_rifft_q15, step_512, 9,
223 	in_rifft_step_512, in_rfft_step_512, 512);
224 
225 DEFINE_TEST_VARIANT4(transform_rq15,
226 	arm_rifft_q15, step_1024, 10,
227 	in_rifft_step_1024, in_rfft_step_1024, 1024);
228 
229 DEFINE_TEST_VARIANT4(transform_rq15,
230 	arm_rifft_q15, step_2048, 11,
231 	in_rifft_step_2048, in_rfft_step_2048, 2048);
232 
233 DEFINE_TEST_VARIANT4(transform_rq15,
234 	arm_rifft_q15, step_4096, 12,
235 	in_rifft_step_4096, in_rfft_step_4096, 4096);
236 #endif
237 
238 ZTEST_SUITE(transform_rq15, NULL, NULL, NULL, NULL, NULL);
239