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