1% Export to C header filer int32_t data
2%
3% Usage:
4% export_c_int32t(fn, vn, ln, x)
5% Inputs:
6%   fn - filename for header file
7%   vn - variable name
8%   ln - name for defined LENGTH
9%   x - matrix of data
10
11% SPDX-License-Identifier: BSD-3-Clause
12%
13% Copyright (c) 2021, Intel Corporation. All rights reserved.
14
15function export_c_int32t(fn, vn, ln, x)
16
17% Write blob
18blob32 = interleave(x);
19count = length(blob32);
20fh = fopen(fn, 'w');
21numbers_in_line = 6;
22full_lines = floor(count/numbers_in_line);
23numbers_remain = count - numbers_in_line * full_lines;
24
25year = datestr(now, 'yyyy');
26fprintf(fh, '/* SPDX-License-Identifier: BSD-3-Clause\n');
27fprintf(fh, ' *\n');
28fprintf(fh, ' * Copyright(c) %s Intel Corporation. All rights reserved.\n', year);
29fprintf(fh, ' */\n\n');
30fprintf(fh, '#define %s %d\n\n', ln, count);
31
32n = 1;
33fprintf(fh, 'int32_t %s[%s] = {\n', vn, ln);
34for i = 1:full_lines
35	fprintf(fh, '\t');
36	for j = 1:numbers_in_line
37		fprintf(fh, '%dLL', blob32(n));
38		if n < count
39			fprintf(fh, ',');
40		end
41		if j < numbers_in_line
42			fprintf(fh, ' ');
43		else
44			fprintf(fh, '\n');
45		end
46		n = n + 1;
47	end
48end
49
50if numbers_remain
51	fprintf(fh, '\t');
52	for j = 1:numbers_remain
53		fprintf(fh, '%dLL', blob32(n));
54		if n < count
55			fprintf(fh, ',');
56		end
57		if j < numbers_remain
58			fprintf(fh, ' ');
59		else
60			fprintf(fh, '\n');
61		end
62		n = n + 1;
63	end
64end
65
66fprintf(fh, '};\n');
67fclose(fh);
68end
69
70function y = interleave(x)
71
72sx = size(x);
73samples = sx(1);
74channels = sx(2);
75if sx(2) > 1
76	y = zeros(samples * channels, 1);
77	for i = 1:channels
78		y(i:channels:end) = x(:, i);
79	end
80else
81	y = x;
82end
83end
84