6
6
#include "pimp.h"
7
7
#include "tests.h"
8
8
9
- #define LEN 8
10
- #define BLOCKLEN 2
9
+ void setUp (void ) {}
11
10
12
- BlockLMSFilter * blms ;
11
+ void tearDown ( void ) {}
13
12
14
- void setUp (void ) {
15
- blms = blms_init (LEN , BLOCKLEN , 0.1 , 1 );
16
- }
17
13
18
- void tearDown (void ) {
19
- blms_destroy (blms );
20
- }
14
+ #define LEN2 2
15
+ #define BLOCKLEN2 2
21
16
22
17
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 ;
28
23
24
+ BlockLMSFilter * blms = blms_init (LEN2 , BLOCKLEN2 , 0.1 , 1 );
29
25
blms_set_w (blms , w );
30
26
31
27
// 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 );
33
29
34
30
blms_get_w (blms , _w );
35
31
// first half is w
36
- TEST_ARRAY_EQUAL (w , _w , LEN );
32
+ TEST_ARRAY_EQUAL (w , _w , LEN2 );
37
33
// second is zero
38
- TEST_ARRAY_EQUAL (zeros , & _w [LEN ], LEN );
34
+ TEST_ARRAY_EQUAL (zeros , & _w [LEN2 ], LEN2 );
35
+
36
+ blms_destroy (blms );
39
37
}
40
38
41
39
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 );
43
43
blms_set_w (blms , w );
44
44
45
45
AudioBuf * in = audiobuf_from_wav ("../tests/data/x.wav" );
46
46
AudioBuf * out = audiobuf_from_wav ("../tests/data/y_1.wav" );
47
47
pfloat * x = in -> data ;
48
48
pfloat * y = out -> data ;
49
49
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 };
53
53
54
54
// printf("W:\t");
55
- // PRINT_ARRAY(blms->W, LEN +1);
55
+ // PRINT_ARRAY(blms->W, LEN2 +1);
56
56
57
- for (size_t i = 0 ; i < in -> len / BLOCKLEN ; i ++ ) {
57
+ for (size_t i = 0 ; i < in -> len / BLOCKLEN2 ; i ++ ) {
58
58
// prepare Xbuf
59
- block_right_extend (2 * LEN , BLOCKLEN , xbuf , x );
59
+ block_right_extend (2 * LEN2 , BLOCKLEN2 , xbuf , x );
60
60
rfft (blms -> plan , xbuf , Xbuf );
61
61
62
62
blms_predict (blms , Xbuf , y_hat );
63
63
64
64
// printf("x:\t");
65
- // PRINT_ARRAY(x, BLOCKLEN );
65
+ // PRINT_ARRAY(x, BLOCKLEN2 );
66
66
// printf("xbuf:\t");
67
- // PRINT_ARRAY(xbuf, 2*LEN );
67
+ // PRINT_ARRAY(xbuf, 2*LEN2 );
68
68
// printf("y:\t");
69
- // PRINT_ARRAY(y, BLOCKLEN );
69
+ // PRINT_ARRAY(y, BLOCKLEN2 );
70
70
// printf("yhat:\t");
71
- // PRINT_ARRAY(y_hat, BLOCKLEN );
71
+ // PRINT_ARRAY(y_hat, BLOCKLEN2 );
72
72
// 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
74
74
// printf("\n");
75
75
76
- TEST_ARRAY_WITHIN (1e-5 , y , y_hat , BLOCKLEN );
76
+ TEST_ARRAY_WITHIN (1e-5 , y , y_hat , BLOCKLEN2 );
77
77
78
- x += BLOCKLEN ;
79
- y += BLOCKLEN ;
78
+ x += BLOCKLEN2 ;
79
+ y += BLOCKLEN2 ;
80
80
}
81
81
82
82
audiobuf_destroy (in );
83
83
audiobuf_destroy (out );
84
+ blms_destroy (blms );
84
85
}
85
86
86
87
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
+
88
91
blms_set_w (blms , w );
89
92
90
93
AudioBuf * in = audiobuf_from_wav ("../tests/data/x.wav" );
91
94
AudioBuf * out = audiobuf_from_wav ("../tests/data/y_1.wav" );
92
95
pfloat * x = in -> data ;
93
96
pfloat * y = out -> data ;
94
97
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 };
100
103
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 );
103
106
rfft (blms -> plan , xbuf , Xbuf );
104
107
blms_predict (blms , Xbuf , y_hat );
105
- for (size_t i = 0 ; i < BLOCKLEN ; i ++ )
108
+ for (size_t i = 0 ; i < BLOCKLEN2 ; i ++ )
106
109
e [i ] = y [i ] - y_hat [i ];
107
- block_right_extend (LEN , BLOCKLEN , ebuf , e );
110
+ block_right_extend (LEN2 , BLOCKLEN2 , ebuf , e );
108
111
blms_update (blms , Xbuf , ebuf );
109
112
110
- x += BLOCKLEN ;
111
- y += BLOCKLEN ;
113
+ x += BLOCKLEN2 ;
114
+ y += BLOCKLEN2 ;
112
115
}
113
116
114
- pfloat _w [2 * LEN ] = {0 };
115
- pfloat zeros [LEN ] = {0 };
117
+ pfloat _w [2 * LEN2 ] = {0 };
118
+ pfloat zeros [LEN2 ] = {0 };
116
119
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 );
119
122
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 );
122
125
123
126
audiobuf_destroy (in );
124
127
audiobuf_destroy (out );
128
+ blms_destroy (blms );
125
129
}
126
130
131
+
132
+ #define LEN 4
133
+ #define BLOCKLEN 2
134
+
127
135
void test_BlockLMSFilter_train (void ) {
128
136
pfloat w0 [LEN ] = {0.5 , 0 };
129
137
pfloat w1 [LEN ] = {0 , 0.5 };
130
138
pfloat w3 [LEN ] = {0 , 0.5 , -0.5 };
131
139
pfloat w4 [LEN ] = {0.1 , 0.5 , -0.5 };
140
+ pfloat w5 [LEN ] = {0.1 , 0.5 , -0.5 , 1 };
132
141
133
- pfloat * wtrue [4 ] = {w0 , w1 , w3 , w4 };
142
+ pfloat * wtrue [] = {w0 , w1 , w3 , w4 , w5 };
134
143
135
144
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 );
140
147
141
- printf ("test_BlockLMSFilter_train: %ld \n" , i );
148
+ printf ("test_BlockLMSFilter_train: %d \n" , ( int ) i );
142
149
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 );
144
151
AudioBuf * y = audiobuf_from_wav (path );
145
152
146
153
blms_train (blms , x -> len , x -> data , y -> data );
147
154
148
155
pfloat w [2 * LEN ];
149
156
blms_get_w (blms , w );
150
157
158
+ PRINT_ARRAY (wtrue [i ], LEN );
159
+ PRINT_ARRAY (w , LEN );
151
160
TEST_ARRAY_WITHIN (1e-5 , wtrue [i ], w , LEN );
152
161
153
162
audiobuf_destroy (y );
163
+ blms_destroy (blms );
154
164
}
155
165
audiobuf_destroy (x );
156
166
}
@@ -163,4 +173,4 @@ int main(void) {
163
173
RUN_TEST (test_BlockLMSFilter_update_predict );
164
174
RUN_TEST (test_BlockLMSFilter_train );
165
175
return UNITY_END ();
166
- }
176
+ }
0 commit comments