代码:
%% ------------------------------------------------------------------------ %% Output Info about this m-file fprintf(' *********************************************************** '); fprintf(' <DSP using MATLAB> Exameple 10.2 '); time_stamp = datestr(now, 31); [wkd1, wkd2] = weekday(today, 'long'); fprintf(' Now is %20s, and it is %7s ', time_stamp, wkd2); %% ------------------------------------------------------------------------ clear; close all; % Example parameters B = 2; N = 500000; xn = (2*rand(1,N)-1); % Quantization error analysis [H1, H2, Q, estat] = StatModelR(xn, B, N); % Compute histograms H1max = max(H1); H1min = min(H1); % Max and Min of H1 H2max = max(H2); H2min = min(H2); % Max and Min of H2 Hf1 = figure('units', 'inches', 'position', [1, 1, 8, 6], ... 'paperunits', 'inches', 'paperposition', [0, 0, 6, 4], ... 'NumberTitle', 'off', 'Name', 'Exameple 10.2a B=2'); set(gcf,'Color','white'); TF = 10; subplot(2, 1, 1); bar(Q, H1); axis([-0.5, 0.5, -0.001, 4/128]); grid on; title('Normalized error e1 and e2 Histograms, B = 2'); xlabel('Normalized error e1'); ylabel('Distribution of e1 ', 'vertical', 'baseline'); set(gca, 'YTickMode', 'manual', 'YTick', [0, [1:1:4]/128] ); text(-0.45, 0.030, sprintf('SAMPLE SIZE N = %d', N)); text(-0.45, 0.025, sprintf(' ROUNDED TO B = %d BITS', B)); text(-0.45, 0.020, sprintf(' MEAN = %.4e', estat(1))); text(0.10, 0.030, sprintf('MIN PROB BAR HEIGHT = %f', H1min)) ; text(0.10, 0.025, sprintf('MAX PROB BAR HEIGHT = %f', H1max)) ; text(0.10, 0.020, sprintf(' SIGMA = %f', estat(2))) ; subplot(2, 1, 2); bar(Q, H2); axis([-0.5, 0.5, -0.001, 4/128]); grid on; %title('Normalized error e2'); xlabel('Normalized error e2'); ylabel('Distribution of e2', 'vertical', 'baseline'); set(gca, 'YTickMode', 'manual', 'YTick', [0, 1:1:4]/128 ); text(-0.45, 0.030, sprintf('SAMPLE SIZE N = %d', N)); text(-0.45, 0.025, sprintf(' ROUNDED TO B = %d BITS', B)); text(-0.45, 0.020, sprintf(' MEAN = %.4e', estat(3))); text(0.10, 0.030, sprintf('MIN PROB BAR HEIGHT = %f', H2min)) ; text(0.10, 0.025, sprintf('MAX PROB BAR HEIGHT = %f', H2max)) ; text(0.10, 0.020, sprintf(' SIGMA = %f', estat(4))) ;
这里只写了B=2 的情况,B=6的类似。
运行结果:
上面给出了B=2和B=6两种情况下误差分布图。从第1张图看出,即使B=2的情况,量化误差采样序列也是独立的和均匀分布的。