Aneps
Aneps

Reputation: 133

How to accumulate histogram?

I have data ranging from 0 to 1e5 (in microseconds). I want to make histogram for successive 2.68 microseconds binning for 0.05 microseconds and finally accumulate all the histograms. I have following attempt:

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.

This is the program I made. But it seems the histogram is not getting accumulated fore each run of the loop. Can anyone help me to accumulate the histogram? Or any other better ways to accumulate histogram for the successive ranges?

Upvotes: 0

Views: 748

Answers (1)

Ander Biguri
Ander Biguri

Reputation: 35525

You are not doing it rigth. There are a couple of things you are not doing correctly:

First, the number of pieces of a is not properly computed. To know the amount of pieces youneed to do you need the frequenzy of the data.

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

This will give you the number of pieces to calculate. However, note that this will not be an integer in general. We need to count for that also.

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

Therefore lets make n an integer

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

Now you want to get only a piece of the data, adn compute the histogram for it for the same amount of values. Lets say that you want an histogram with 10 bins;

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

Now we have defined the values of a of wich we want the histogram to count on. Lets loop

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

So what are we doing there? we are taking just a piece of a each iteration, and always getting the histogram of the same bins (else accumulating it wont make sense). So how do we acces data? a(1 + (ii-1)*nindexRange : nindexRange*ii) try to sort it out, it should be very difficult ;).

but oh! we forget that we left some data out! (because the n is not integer!)

therefore

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

This should do the trick

Upvotes: 2

Related Questions