Skip to content

Commit e834b3b

Browse files
committed
Test: Cmocka: Add test vector generator for A-law and mu-law
This patch adds Octave script to generate test data for A-law and mu-law coding. Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
1 parent fe37226 commit e834b3b

File tree

2 files changed

+181
-2
lines changed

2 files changed

+181
-2
lines changed

test/cmocka/m/export_headerfile_open.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
% SPDX-License-Identifier: BSD-3-Clause
1111
%
12-
% Copyright(c) 2022 Intel Corporation. All rights reserved.
12+
% Copyright(c) 2022-2025 Intel Corporation.
1313

1414
function fh = export_headerfile_open(headerfn, corp)
1515

@@ -23,7 +23,7 @@
2323
end
2424
fprintf(fh, '/* SPDX-License-Identifier: BSD-3-Clause\n');
2525
fprintf(fh, ' *\n');
26-
fprintf(fh, ' * Copyright(c) %s %s. All rights reserved.\n', ...
26+
fprintf(fh, ' * Copyright(c) %s %s.\n', ...
2727
datestr(now, 'yyyy'), corp);
2828
fprintf(fh, ' */\n\n');
2929
end
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
% a_law_mu_law_test_vectors() - Create A-law and mu-law test vectors
2+
%
3+
% Running this script creates header files ref_chirp_mono_8k_s16.h,
4+
% a_law_codec.h, and mu_law_codec.h. The chirp waveform for test
5+
% input is created with sox, and then ended and decoded as
6+
% A-law and mu-law.
7+
8+
% SPDX-License-Identifier: BSD-3-Clause
9+
%
10+
% Copyright(c) 2025 Intel Corporation.
11+
12+
function a_law_mu_law_test_vectors()
13+
14+
sox = 0;
15+
alaw = 1;
16+
mulaw = 0;
17+
bits = 16;
18+
ts = 0.5;
19+
fs = 8e3;
20+
t = (0:round(ts*fs - 1))/fs;
21+
x = round(2^(bits - 1) * chirp(t, 100, ts, 3.5e3, 'logarithmic'));
22+
xmax = 2^(bits - 1) - 1;
23+
xmin = -2^(bits - 1);
24+
x = max(min(x, xmax), xmin);
25+
ref_s16_data = int16(x);
26+
max(ref_s16_data)
27+
min(ref_s16_data)
28+
29+
ref_s16_header_fn = 'ref_chirp_mono_8k_s16.h';
30+
ref_alaw_header_fn = 'a_law_codec.h';
31+
ref_mulaw_header_fn = 'mu_law_codec.h';
32+
33+
close all;
34+
path(path(), '../../../m');
35+
36+
if sox
37+
ref_s16 = '/tmp/chirp_mono_8k_s16.raw';
38+
ref_alaw_enc = '/tmp/ref_alaw_enc.raw';
39+
ref_alaw_dec = '/tmp/ref_alaw_dec.raw';
40+
ref_mulaw_enc = '/tmp/ref_mulaw_enc.raw';
41+
ref_mulaw_dec = '/tmp/ref_mulaw_dec.raw';
42+
sox_s16_chirp_gen = sprintf('sox -c 1 -r 8000 -b 16 --encoding signed-integer -n %s synth 0.5 sine 100-3500', ref_s16);
43+
sox_alaw_enc = sprintf('sox -c 1 -r 8000 -b 16 --encoding signed-integer %s --encoding a-law %s', ref_s16, ref_alaw_enc);
44+
sox_alaw_dec = sprintf('sox -c 1 -r 8000 --encoding a-law %s -b 16 --encoding signed-integer %s', ref_alaw_enc, ref_alaw_dec);
45+
sox_mulaw_enc = sprintf('sox -c 1 -r 8000 -b 16 --encoding signed-integer %s --encoding a-law %s', ref_s16, ref_mulaw_enc);
46+
sox_mulaw_dec = sprintf('sox -c 1 -r 8000 --encoding a-law %s -b 16 --encoding signed-integer %s', ref_mulaw_enc, ref_mulaw_dec);
47+
system(sox_s16_chirp_gen);
48+
system(sox_alaw_enc);
49+
system(sox_alaw_dec);
50+
system(sox_mulaw_enc);
51+
system(sox_mulaw_dec);
52+
ref_s16_data = readbin(ref_s16, 'int16'); figure;
53+
ref_alaw_enc_data = readbin(ref_alaw_enc, 'uint8');
54+
ref_alaw_dec_data = readbin(ref_alaw_dec, 'int16');
55+
ref_mulaw_enc_data = readbin(ref_mulaw_enc, 'uint8');
56+
ref_mulaw_dec_data = readbin(ref_mulaw_dec, 'int16');
57+
else
58+
if alaw
59+
ref_alaw_enc_data = alaw_enc(ref_s16_data);
60+
ref_alaw_dec_data = alaw_dec(ref_alaw_enc_data);
61+
end
62+
if mulaw
63+
ref_mulaw_enc_data = mulaw_enc(ref_s16_data);
64+
ref_mulaw_dec_data = mulaw_dec(ref_mulaw_enc_data);
65+
end
66+
end
67+
68+
if alaw
69+
plot(ref_s16_data); grid on; title('Input s16');
70+
figure; plot(ref_alaw_enc_data); grid on; title('A-law data');
71+
figure; plot(ref_alaw_dec_data); grid on; title('A-law decode s16');
72+
end
73+
74+
if mulaw
75+
figure; plot(ref_mulaw_enc_data); grid on; title('mu-law data');
76+
figure; plot(ref_mulaw_dec_data); grid on; title('mu-law decode s16');
77+
end
78+
79+
fh = export_headerfile_open(ref_s16_header_fn);
80+
comment = sprintf('Created %s with script a_law_mu_law_test_vectors.m %s', ...
81+
datestr(now, 0), export_get_git_describe());
82+
export_comment(fh, comment);
83+
fprintf(fh, '#include <stdint.h>\n');
84+
export_ndefine(fh, 'REF_DATA_SAMPLE_COUNT', length(ref_s16_data));
85+
export_vector(fh, 16, 'chirp_mono_8k_s16', ref_s16_data);
86+
fclose(fh);
87+
88+
if alaw
89+
fh = export_headerfile_open(ref_alaw_header_fn);
90+
comment = sprintf('Created %s with script a_law_mu_law_test_vectors.m %s', ...
91+
datestr(now, 0), export_get_git_describe());
92+
export_comment(fh, comment);
93+
fprintf(fh, '#include <stdint.h>\n');
94+
export_vector(fh, 8, 'ref_alaw_enc_data', ref_alaw_enc_data);
95+
export_vector(fh, 16, 'ref_alaw_dec_data', ref_alaw_dec_data);
96+
fclose(fh);
97+
end
98+
99+
if mulaw
100+
fh = export_headerfile_open(ref_mulaw_header_fn);
101+
comment = sprintf('Created %s with script a_law_mu_law_test_vectors.m %s', ...
102+
datestr(now, 0), export_get_git_describe());
103+
export_comment(fh, comment);
104+
fprintf(fh, '#include <stdint.h>\n');
105+
export_vector(fh, 8, 'ref_mulaw_enc_data', ref_,mulaw_enc_data);
106+
export_vector(fh, 16, 'ref_mulaw_dec_data', ref_mulaw_dec_data);
107+
fclose(fh);
108+
end
109+
110+
end
111+
112+
function x = readbin(fn, itype)
113+
fh = fopen(fn, 'r');
114+
if fh == -1
115+
fprintf(1, 'Could not open file %s.\n', fn);
116+
error("Failed.");
117+
end
118+
x = fread(fh, inf, itype);
119+
fclose(fh);
120+
end
121+
122+
% See G.711 alaw compress from
123+
% https://www.itu.int/rec/T-REC-G.191/
124+
function encoded = alaw_enc(input_samples)
125+
num_samples = length(input_samples);
126+
in16 = int16(input_samples);
127+
encoded_samples = uint8(zeros(num_samples, 1));
128+
for n = 1:num_samples
129+
if in16(n) < 0
130+
ix = bitshift(-in16(n) -1, -4); % 1's complement
131+
else
132+
ix = bitshift(in16(n), -4);
133+
end
134+
135+
if ix > 15
136+
iexp = 1;
137+
while (ix > 16 + 15)
138+
ix = bitshift(ix, -1);
139+
iexp = iexp + 1;
140+
end
141+
142+
ix = ix - 16;
143+
ix = ix + bitshift(iexp, 4);
144+
end
145+
146+
if in16(n) >= 0
147+
ix = bitor(ix, 128);
148+
end
149+
150+
encoded(n) = bitxor(ix, 85);
151+
end
152+
end
153+
154+
% See G.711 alaw expand from
155+
% https://www.itu.int/rec/T-REC-G.191/
156+
function samples_s16 = alaw_dec(input_bytes)
157+
num_samples = length(input_bytes);
158+
samples_s16 = int16(zeros(num_samples, 1));
159+
for n = 1:num_samples
160+
ix = bitxor(int16(input_bytes(n)), 85);
161+
ix = bitand(ix, 127);
162+
iexp = bitshift(ix, -4);
163+
mant = bitand(ix, 15);
164+
if iexp > 0
165+
mant = mant + 16;
166+
end
167+
168+
mant = bitshift(mant, 4) + 8;
169+
if iexp > 1
170+
mant = bitshift(mant, iexp - 1);
171+
end
172+
173+
if input_bytes(n) > 127
174+
samples_s16(n) = mant;
175+
else
176+
samples_s16(n) = -mant;
177+
end
178+
end
179+
end

0 commit comments

Comments
 (0)