-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsmoothHistogram.m
58 lines (47 loc) · 1.64 KB
/
smoothHistogram.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function smoothHistogram(data, numBins,color)
% Create a histogram
[counts, edges] = histcounts(data, numBins, 'Normalization', 'pdf');
% Calculate bin centers
binCenters = (edges(1:end-1) + edges(2:end)) / 2;
% Use spline to create a smooth curve
pp = spline(binCenters, counts);
% Generate a finer set of x-values
xFine = linspace(min(binCenters), max(binCenters), 100);
% Evaluate the spline at the finer x-values
ySmooth = ppval(pp, xFine);
delta = 0.01;
xFine = [min(xFine)-delta xFine max(xFine)+delta];
ySmooth = [0 ySmooth 0];
ySmooth(ySmooth < 0) = 0;
% Plot the smooth histogram
plot(xFine, ySmooth, 'LineWidth', 2,Color= color);
%plot(binExtended,countsExtended,'LineWidth', 2);
end
% function smoothHistogram(data, numBins)
% % Create a histogram
% [counts, edges] = histcounts(data, numBins, 'Normalization', 'pdf');
%
% % Calculate bin centers
% binCenters = (edges(1:end-1) + edges(2:end)) / 2;
%
% delta = 0.01;
% % Extend the data and counts with control points to force the spline to zero
% binExtended = [min(binCenters)-delta binCenters max(binCenters)+delta];
% countsExtended = [0 counts 0];
%
% % Use spline to create a smooth curve
% pp = spline(binExtended, countsExtended);
%
% % Generate a finer set of x-values
% xFine = linspace(min(binCenters), max(binCenters), 100);
%
% % Evaluate the spline at the finer x-values
% ySmooth = ppval(pp, xFine);
%
%
%
% % Plot the smooth histogram
% %plot(xFine, ySmooth, 'LineWidth', 2);
% plot(binExtended,countsExtended,'LineWidth', 2);
% end
%