Digital Signal Processing Reference
In-Depth Information
% Estimation of the probability density function, the linear
% and quadratic mean and the variance of a random sequence
% using MATLAB command hist
% function [m1,m2] = histogram(x,M,MIN,MAX,TXT)
% x : random sequence (sequence under test)
% M : number of histogram bins
% MIN : minimum value for histogram bin centers (class centers)
% MAX : maximum value for histogram bin centers (class centers)
% TXT : string ('gauss') - optional
% histogram.m * mw * 06/10/2008
function [c,f,m1,m2] = histogram(x,M,MIN,MAX,TXT)
if nargin==4
TXT = 'bar';
end
N = length(x); % number of samples
span = max(x)-min(x); % span
m1 = mean(x); % estimate of linear mean
s2 = var(x,1); % variance (normalized by 1/N)
stdev = std(x,1); % standard deviation (normalized by 1/N)
m2 = m1^2 + s2; % estimate of quadratic mean
% 1-dim. probability density function (pdf)
d = (MAX-MIN)/(M-1); % bin width
c = MIN + d*(0:M-1); % bin centers
h = hist(x,c); % histogram
% Relative frequency (estimate of probability density function (pdf))
f = h/(d*N);
% Gaussian bell function
if strcmp(TXT,'gauss')
x = MIN:.1:MAX;
% abscissa
g = (1/sqrt(2*pi*s2))*exp(-((x-m1).^2)/(2*s2));
end
% Numerical and graphical output
FIG = figure('Name','histogram : empirical data',...
'NumberTitle','off','Units','normal','Position',[.4 .4 .45 .45]);
subplot(2,1,1)
axis([1 100 0 100]);
axis('off')
text(0,70,'# samples'); text(20,70,num2str(N))
text(0,50,'# bins'); text(20,50,num2str(M))
text(40,70,'span'); text(70,70,num2str(span))
text(40,55,'linear mean'); text(70,55,num2str(m1))
text(40,40,'standard deviation'); text(70,40,num2str(stdev))
text(40,25,'variance'); text(70,25,num2str(s2))
text(40,10,'quadratic mean'); text(70,10,num2str(m2))
% Graphical output of estimated pdf and gaussian bell function
if strcmp(TXT,'gauss')
subplot(2,1,2),plot(x,g,':',c,f,'o'), grid
else
subplot(2,1,2),bar(c,f), grid
end
axis([MIN MAX 0 1.2/sqrt(2*pi*s2)])
xlabel('{\itx} \rightarrow'), ylabel('rel. frequency \rightarrow')
Search WWH ::




Custom Search