Skip to content

Commit bb1a70b

Browse files
committed
tests and cross-compile
1 parent d3164f2 commit bb1a70b

File tree

7 files changed

+98
-73
lines changed

7 files changed

+98
-73
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
3+
# Cross compile for arm but test on x86
4+
5+
As described here: https://azeria-labs.com/arm-on-x86-qemu-user/
6+
7+
qemu-arm -L /usr/arm-linux-gnueabihf ./test_fft
8+

cmake/arm-linux-toolchain.cmake

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
message("using arm-linux-toolchain")
22

3+
# use qemu to run tests when cross-compiling
4+
set(CMAKE_CROSSCOMPILING_EMULATOR qemu-arm -L/usr/arm-linux-gnueabihf)
5+
36
set(CMAKE_SYSTEM_NAME "Linux")
47
set(CMAKE_SYSTEM_PROCESSOR "arm")
58

6-
set(NE10_LINUX_TARGET_ARCH "armv7")
7-
8-
set(CMAKE_C_COMPILER /usr/local/linaro/arm-bela-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc)
9-
set(CMAKE_CXX_COMPILER /usr/local/linaro/arm-bela-linux-gnueabihf/bin/arm-linux-gnueabihf-g++)
9+
set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
10+
set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
1011

1112
set(triple arm-linux-gnueabihf)
1213
set(CMAKE_C_COMPILER_TARGET ${triple})
1314
set(CMAKE_CXX_COMPILER_TARGET ${triple})
1415

1516
# for Ne10
17+
set(NE10_LINUX_TARGET_ARCH "armv7")
1618
set(GNULINUX_PLATFORM ON)

tests/make_wavs.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
# %%
22
import numpy as np
3-
import matplotlib.pyplot as pl
43
import soundfile as sf
54
import adafilt
65
import measuretf.signals
76
# %%
87
np.random.seed(123)
98

10-
samplerate = 1000 # Hertz
11-
rectime = 1 # second
9+
samplerate = 1000 # Hertz
10+
rectime = 1 # second
1211
gain = 0.01
1312
x = gain * np.random.randn(rectime * int(samplerate))
1413
x[x > 1] = 1
1514
x[x < -1] = -1
1615

1716

18-
ws = [[0.5], [0, 0.5], [0, 0.5, -0.5], [0.1, 0.5, -0.5]]
17+
ws = [[0.5], [0, 0.5], [0, 0.5, -0.5], [0.1, 0.5, -0.5], [0.1, 0.5, -0.5, 1]]
1918

2019
y = []
2120
for i, w in enumerate(ws):
2221
y = adafilt.olafilt(w, x)
23-
sf.write(f'data/y_{i}.wav', y, samplerate, subtype='FLOAT');
22+
sf.write(f'data/y_{i}.wav', y, samplerate, subtype='FLOAT')
2423

25-
sf.write('data/x.wav', x, samplerate, subtype='FLOAT');
24+
sf.write('data/x.wav', x, samplerate, subtype='FLOAT')
2625

2726
# %%
2827
sr = 1000

tests/test_BlockLMSFilter.c

Lines changed: 68 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,151 +6,161 @@
66
#include "pimp.h"
77
#include "tests.h"
88

9-
#define LEN 8
10-
#define BLOCKLEN 2
9+
void setUp(void) {}
1110

12-
BlockLMSFilter* blms;
11+
void tearDown(void) {}
1312

14-
void setUp(void) {
15-
blms = blms_init(LEN, BLOCKLEN, 0.1, 1);
16-
}
1713

18-
void tearDown(void) {
19-
blms_destroy(blms);
20-
}
14+
#define LEN2 2
15+
#define BLOCKLEN2 2
2116

2217
void test_set_get_w(void) {
23-
pfloat w[LEN] = {1, 0};
24-
pfloat _w[2 * LEN] = {0};
25-
pfloat zeros[LEN] = {0};
26-
pcomplex ones[LEN + 1];
27-
for (size_t i = 0; i < LEN+1; i++) ones[i] = 1;
18+
pfloat w[LEN2] = {1, 0};
19+
pfloat _w[2 * LEN2] = {0};
20+
pfloat zeros[LEN2] = {0};
21+
pcomplex ones[LEN2 + 1];
22+
for (size_t i = 0; i < LEN2+1; i++) ones[i] = 1;
2823

24+
BlockLMSFilter* blms = blms_init(LEN2, BLOCKLEN2, 0.1, 1);
2925
blms_set_w(blms, w);
3026

3127
// w is allpass
32-
TEST_ARRAY_WITHIN(1e-5, ones, blms->W, LEN + 1);
28+
TEST_ARRAY_WITHIN(1e-5, ones, blms->W, LEN2 + 1);
3329

3430
blms_get_w(blms, _w);
3531
// first half is w
36-
TEST_ARRAY_EQUAL(w, _w, LEN);
32+
TEST_ARRAY_EQUAL(w, _w, LEN2);
3733
// second is zero
38-
TEST_ARRAY_EQUAL(zeros, &_w[LEN], LEN);
34+
TEST_ARRAY_EQUAL(zeros, &_w[LEN2], LEN2);
35+
36+
blms_destroy(blms);
3937
}
4038

4139
void test_BlockLMSFilter_predict(void) {
42-
pfloat w[LEN] = {0, 0.5};
40+
pfloat w[LEN2] = {0, 0.5};
41+
42+
BlockLMSFilter* blms = blms_init(LEN2, BLOCKLEN2, 0.1, 1);
4343
blms_set_w(blms, w);
4444

4545
AudioBuf* in = audiobuf_from_wav("../tests/data/x.wav");
4646
AudioBuf* out = audiobuf_from_wav("../tests/data/y_1.wav");
4747
pfloat* x = in->data;
4848
pfloat* y = out->data;
4949

50-
pfloat xbuf[2 * LEN] = {0};
51-
pcomplex Xbuf[LEN + 1] = {0};
52-
pfloat y_hat[BLOCKLEN] = {0};
50+
pfloat xbuf[2 * LEN2] = {0};
51+
pcomplex Xbuf[LEN2 + 1] = {0};
52+
pfloat y_hat[BLOCKLEN2] = {0};
5353

5454
// printf("W:\t");
55-
// PRINT_ARRAY(blms->W, LEN+1);
55+
// PRINT_ARRAY(blms->W, LEN2+1);
5656

57-
for (size_t i = 0; i < in->len / BLOCKLEN; i++) {
57+
for (size_t i = 0; i < in->len / BLOCKLEN2; i++) {
5858
// prepare Xbuf
59-
block_right_extend(2 * LEN, BLOCKLEN, xbuf, x);
59+
block_right_extend(2 * LEN2, BLOCKLEN2, xbuf, x);
6060
rfft(blms->plan, xbuf, Xbuf);
6161

6262
blms_predict(blms, Xbuf, y_hat);
6363

6464
// printf("x:\t");
65-
// PRINT_ARRAY(x, BLOCKLEN);
65+
// PRINT_ARRAY(x, BLOCKLEN2);
6666
// printf("xbuf:\t");
67-
// PRINT_ARRAY(xbuf, 2*LEN);
67+
// PRINT_ARRAY(xbuf, 2*LEN2);
6868
// printf("y:\t");
69-
// PRINT_ARRAY(y, BLOCKLEN);
69+
// PRINT_ARRAY(y, BLOCKLEN2);
7070
// printf("yhat:\t");
71-
// PRINT_ARRAY(y_hat, BLOCKLEN);
71+
// PRINT_ARRAY(y_hat, BLOCKLEN2);
7272
// printf("_Y:\t");
73-
// PRINT_ARRAY((pfloat *)(blms->_Y), 2*LEN); // somehow interleaved samples are copied into yhat
73+
// PRINT_ARRAY((pfloat *)(blms->_Y), 2*LEN2); // somehow interleaved samples are copied into yhat
7474
// printf("\n");
7575

76-
TEST_ARRAY_WITHIN(1e-5, y, y_hat, BLOCKLEN);
76+
TEST_ARRAY_WITHIN(1e-5, y, y_hat, BLOCKLEN2);
7777

78-
x += BLOCKLEN;
79-
y += BLOCKLEN;
78+
x += BLOCKLEN2;
79+
y += BLOCKLEN2;
8080
}
8181

8282
audiobuf_destroy(in);
8383
audiobuf_destroy(out);
84+
blms_destroy(blms);
8485
}
8586

8687
void test_BlockLMSFilter_update_predict(void) {
87-
pfloat w[LEN] = {0, 0.5};
88+
pfloat w[LEN2] = {0, 0.5};
89+
BlockLMSFilter* blms = blms_init(LEN2, BLOCKLEN2, 0.1, 1);
90+
8891
blms_set_w(blms, w);
8992

9093
AudioBuf* in = audiobuf_from_wav("../tests/data/x.wav");
9194
AudioBuf* out = audiobuf_from_wav("../tests/data/y_1.wav");
9295
pfloat* x = in->data;
9396
pfloat* y = out->data;
9497

95-
pfloat xbuf[2 * LEN] = {0};
96-
pcomplex Xbuf[LEN + 1] = {0};
97-
pfloat y_hat[BLOCKLEN] = {0};
98-
pfloat e[BLOCKLEN] = {0};
99-
pfloat ebuf[LEN] = {0};
98+
pfloat xbuf[2 * LEN2] = {0};
99+
pcomplex Xbuf[LEN2 + 1] = {0};
100+
pfloat y_hat[BLOCKLEN2] = {0};
101+
pfloat e[BLOCKLEN2] = {0};
102+
pfloat ebuf[LEN2] = {0};
100103

101-
for (size_t i = 0; i < in->len / BLOCKLEN; i++) {
102-
block_right_extend(2 * LEN, BLOCKLEN, xbuf, x);
104+
for (size_t i = 0; i < in->len / BLOCKLEN2; i++) {
105+
block_right_extend(2 * LEN2, BLOCKLEN2, xbuf, x);
103106
rfft(blms->plan, xbuf, Xbuf);
104107
blms_predict(blms, Xbuf, y_hat);
105-
for (size_t i = 0; i < BLOCKLEN; i++)
108+
for (size_t i = 0; i < BLOCKLEN2; i++)
106109
e[i] = y[i] - y_hat[i];
107-
block_right_extend(LEN, BLOCKLEN, ebuf, e);
110+
block_right_extend(LEN2, BLOCKLEN2, ebuf, e);
108111
blms_update(blms, Xbuf, ebuf);
109112

110-
x += BLOCKLEN;
111-
y += BLOCKLEN;
113+
x += BLOCKLEN2;
114+
y += BLOCKLEN2;
112115
}
113116

114-
pfloat _w[2 * LEN] = {0};
115-
pfloat zeros[LEN] = {0};
117+
pfloat _w[2 * LEN2] = {0};
118+
pfloat zeros[LEN2] = {0};
116119
blms_get_w(blms, _w);
117-
TEST_ARRAY_WITHIN(1e-5, w, _w, LEN);
118-
TEST_ARRAY_WITHIN(1e-5, zeros, &_w[LEN], LEN);
120+
TEST_ARRAY_WITHIN(1e-5, w, _w, LEN2);
121+
TEST_ARRAY_WITHIN(1e-5, zeros, &_w[LEN2], LEN2);
119122

120-
TEST_ARRAY_WITHIN(1e-5, y - BLOCKLEN, y_hat, BLOCKLEN);
121-
TEST_ARRAY_WITHIN(1e-5, zeros, e, BLOCKLEN);
123+
TEST_ARRAY_WITHIN(1e-5, y - BLOCKLEN2, y_hat, BLOCKLEN2);
124+
TEST_ARRAY_WITHIN(1e-5, zeros, e, BLOCKLEN2);
122125

123126
audiobuf_destroy(in);
124127
audiobuf_destroy(out);
128+
blms_destroy(blms);
125129
}
126130

131+
132+
#define LEN 4
133+
#define BLOCKLEN 2
134+
127135
void test_BlockLMSFilter_train(void) {
128136
pfloat w0[LEN] = {0.5, 0};
129137
pfloat w1[LEN] = {0, 0.5};
130138
pfloat w3[LEN] = {0, 0.5, -0.5};
131139
pfloat w4[LEN] = {0.1, 0.5, -0.5};
140+
pfloat w5[LEN] = {0.1, 0.5, -0.5, 1};
132141

133-
pfloat* wtrue[4] = {w0, w1, w3, w4};
142+
pfloat* wtrue[] = {w0, w1, w3, w4, w5};
134143

135144
AudioBuf* x = audiobuf_from_wav("../tests/data/x.wav");
136-
for (size_t i = 0; i < sizeof(wtrue) / sizeof(*wtrue); i++)
137-
{
138-
blms_destroy(blms);
139-
blms = blms_init(LEN, BLOCKLEN, 0.1, 1);
145+
for (size_t i = 0; i < sizeof(wtrue) / sizeof(*wtrue); i++) {
146+
BlockLMSFilter* blms = blms_init(LEN, BLOCKLEN, 0.1, 1);
140147

141-
printf("test_BlockLMSFilter_train: %ld \n", i);
148+
printf("test_BlockLMSFilter_train: %d \n", (int)i);
142149
char path[256];
143-
snprintf(path, sizeof(path), "../tests/data/y_%ld.wav", i);
150+
snprintf(path, sizeof(path), "../tests/data/y_%d.wav", (int)i);
144151
AudioBuf* y = audiobuf_from_wav(path);
145152

146153
blms_train(blms, x->len, x->data, y->data);
147154

148155
pfloat w[2 * LEN];
149156
blms_get_w(blms, w);
150157

158+
PRINT_ARRAY(wtrue[i], LEN);
159+
PRINT_ARRAY(w, LEN);
151160
TEST_ARRAY_WITHIN(1e-5, wtrue[i], w, LEN);
152161

153162
audiobuf_destroy(y);
163+
blms_destroy(blms);
154164
}
155165
audiobuf_destroy(x);
156166
}
@@ -163,4 +173,4 @@ int main(void) {
163173
RUN_TEST(test_BlockLMSFilter_update_predict);
164174
RUN_TEST(test_BlockLMSFilter_train);
165175
return UNITY_END();
166-
}
176+
}

tests/test_LMSFilter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ void test_LMSFilter_train(void) {
8585
pfloat w1[LEN] = {0, 0.5};
8686
pfloat w3[LEN] = {0, 0.5, -0.5};
8787
pfloat w4[LEN] = {0.1, 0.5, -0.5};
88+
pfloat w5[LEN] = {0.1, 0.5, -0.5, 1};
8889

89-
pfloat* wtrue[4] = {w0, w1, w3, w4};
90+
pfloat* wtrue[] = {w0, w1, w3, w4, w5};
9091

9192
AudioBuf* x = audiobuf_from_wav("../tests/data/x.wav");
9293
for (size_t i = 0; i < sizeof(wtrue) / sizeof(*wtrue); i++)

tests/test_fft.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ void run(struct data* tests, size_t n) {
2929
rfft(plan, t.in, out);
3030
PRINT_ARRAY(t.out, t.len/2+1);
3131
PRINT_ARRAY(out, t.len/2+1);
32-
TEST_ARRAY_WITHIN(1e-8, t.out, out, t.len/2 + 1);
32+
TEST_ARRAY_WITHIN(1e-7, t.out, out, t.len/2 + 1);
3333

3434
irfft(plan, out, in);
3535
PRINT_ARRAY(t.in, t.len);
3636
PRINT_ARRAY(in, t.len);
37-
TEST_ARRAY_WITHIN(1e-8, t.in, in, t.len);
37+
TEST_ARRAY_WITHIN(1e-7, t.in, in, t.len);
3838
}
3939
}
4040

@@ -186,9 +186,14 @@ void test_fft_odd(void) {
186186
int main(void) {
187187
UNITY_BEGIN();
188188
RUN_TEST(test_even_length);
189-
RUN_TEST(test_odd_length);
189+
RUN_TEST(test_fft_even_dynamic);
190190
RUN_TEST(test_fft_even);
191+
192+
#if PIMP_WITH_NE10 == 0
193+
// ne10 fft only supports even fft sizes
194+
RUN_TEST(test_odd_length);
191195
RUN_TEST(test_fft_odd);
192-
RUN_TEST(test_fft_even_dynamic);
196+
#endif
197+
193198
return UNITY_END();
194199
}

tests/test_on_bela.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)