1function test = chirp_test_analyze(test)
2
3% SPDX-License-Identifier: BSD-3-Clause
4% Copyright(c) 2017-2020 Intel Corporation. All rights reserved.
5% Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
6
7test.ph = [];
8test.fh = [];
9
10%% Load output file
11[x, nx] = load_test_output(test);
12if nx < 1
13	test.fail = -1; % No data
14	return;
15end
16
17%% Find sync
18[d, nt, nt_use, nt_skip] = find_test_signal(x(:,test.ch(1)), test);
19if isempty(d)
20	t = (((1:size(x, 1)) - 1)/test.fs)';
21	figure;
22	plot(t, x)
23	grid on;
24	xlabel('Time (s)');
25	ylabel('PCM sample values');
26	test.fail = -1;
27	return;
28end
29
30%% Trim sample
31i1 = d+nt_skip;
32i2 = i1+nt_use-1;
33z = x(i1:i2, :);
34y = z(:,test.ch);
35s = sum(z,2);
36
37%% Quick check length, RMS, offset, and channels sum
38sz = size(z);
39tz = sz(1)/test.fs;
40et = abs(test.cl-tz)/test.cl;
41rms_db = 10*log10(mean(z.^2)) + 20*log10(sqrt(2));
42offs_db = 20*log10(abs(mean(z)));
43sum_rms_db = 10*log10(mean(s.^2));
44% Check for proper ratio of out/in samples, minimum level, maximum offset
45% and maximum of sum of channels. The input is such that the channels should
46% sum to zero. A phase difference in channels would cause non-zero output
47% for channels sum. Dithered input causes a small non-zero sum value.
48
49if et > 0.05
50	fail = 1;
51	fprintf('Failed output chirp length, err=%f, t=%f.\n', et, tz);
52else if (min(rms_db) < -6) && (test.fs2 + 1 > test.fs1)
53	     fail = 1;
54	     fprintf('Failed output chirp level.\n');
55     else if max(offs_db) > -40
56		  fail = 1;
57		  fprintf('Failed output chirp DC offset.\n');
58	  else if (sum_rms_db > -80) && (mod(test.nch, 2) == 0)
59		       fail = 1;
60		       fprintf('Failed output chirp channels phase.\n');
61	       else
62		       fail = 0;
63	       end
64	  end
65     end
66end
67
68test.fh = figure('visible', test.plot_visible);
69test.ph = subplot(1, 1, 1);
70ns = 1024;
71no = round(0.9*ns);
72specgram(y(:,1), ns, test.fs, kaiser(ns,27), no);
73colormap('jet');
74caxis([-150 0]);
75colorbar('EastOutside');
76
77test.fail = fail;
78
79end
80