如何累积直方图?

时间:2014-11-25 09:20:16

标签: matlab histogram matlab-figure

我的数据范围从0到1e5(以微秒为单位)。我想对连续2.68微秒的分组进行直方图0.05微秒,最后累积所有直方图。我有以下尝试:

a=load('Data.dat')
Range=2.68; % For every successive 2.68micro seconds, I want to plot histogram
n= max(a)/Range; % This tells how many such ranges will be in the data
low=min(a);
high=min(a)+Range;
x=low:0.05:high;
y=hist(a,x);% This is the first histogram for the first 2.68 microsecond
figure
semilogy(x,y,'-ob')

for num=1:1:n-1  % This loop is to make histogram for successive 2.68 microseconds
low=low+Range;
high=high+Range;
x=low:0.05:high;
y=y+hist(a,x-low); % This (I AM NOT SURE) is intended to accumulate the histogram for each  loop. 
end

figure
semilogy(x-low,y,'-or'); % Final accumulated histogram.

这是我制作的节目。但似乎直方图在循环的每次运行之前都没有积累。任何人都可以帮我积累直方图吗?或者任何其他更好的方法来累积连续范围的直方图?

1 个答案:

答案 0 :(得分:2)

你没有做到这一点。有几件事你做得不对:

首先,未正确计算a的个数。要知道你需要做的事情,你需要数据的频率。

freq=0.05; %seeing your comments
n=numel(a)*freq/Range;
nindexRange=Range/freq;   %how many data is in each Range

这将为您提供要计算的件数。但请注意,这通常不是整数。我们也需要考虑到这一点。

lastpiece=numel(a)-floor(n)*nindexRange; 
% This gives us how many elements are we leaving for the last calculation

因此,让n为整数

n=floor(n); %round it to the lowest

现在您只想获取一部分数据,并为相同数量的值计算直方图。让我们说你想要一个有10个箱子的直方图;

nbins=10;
bins=linspace(min(a),max(a),nbins);

现在我们已经定义了a的值,我们希望直方图能够依赖它们。让循环

for ii=1:n
   y=y+hist( a(1 + (ii-1)*nindexRange : nindexRange*ii) , bins);  
end

那我们在那做什么? 我们每次迭代只需要a个,并且总是得到相同分档的直方图(否则累积它就没有意义)。那么我们如何访问数据呢? (1 + (ii-1)*nindexRange : nindexRange*ii)尝试对其进行排序,这应该非常困难;)。

但是哦!我们忘了我们留下了一些数据! (因为n不是整数!)

因此

if laspiece~=0
  y=y+hist(a(end-laspiece:end),bins); % take the last "lastpeice" amount of points
end

这应该可以解决问题

相关问题