% Normal Distributuion Equalization Script

L = 256;
B = (0:1:L-1)';
mn = 128; %mean of synthetic data
stdv = 10; %standard deviation of synthetic data
g = uint8(mn + stdv.*randn(25,25)); %generate synthetic data via randn()
% randn() generates pseudorandom numbers from a normal distribution
 

[m,n] = size(g);
hcg = my_histcounts(g,B); %get histcounts
pdfg = hcg/(m*n); %pdf for g
cdfg = cumsum(pdfg); %cdf for g
ncdfg = (L-1)*cdfg; %normalized cdf

figure(1);
subplot(2,1,1); bar(B,pdfg,'b'); 
title('Normalized histogram of f (PDF)'); xlabel('bin centers'); ylabel('counts');
subplot(2,1,2); plot(B,ncdfg,'b-',B,B,'m-'); 
legend('Gaussian Distribution CDF','line y = x (Uniform Dist.)','Location','northwest')
title('Normalized CDF for Synthetic Image');

gp = my_histeq(g,L); %histogram equalization
hcgp = my_histcounts(gp,B);
pdfgp = hcgp/(m*n);
cdfgp = cumsum(pdfgp);
ncdfgp = (L-1)*cdfgp;

figure(2);
subplot(1,2,1); image(g); colormap(gray(256)); title('Original Image'); 
subplot(1,2,2); image(gp); colormap(gray(256)); title('Processed Image');

figure(3);
subplot(3,1,1); bar(B,pdfg,'b'); 
title('Normalized Histogram for Synthetic Image (PDF)'); xlabel('bin centers'); ylabel('counts');
subplot(3,1,2); bar(B,pdfgp,'b'); 
title('Normalized Histogram for Equalized Synthetic Image (PDF)'); xlabel('bin centers'); ylabel('counts');
subplot(3,1,3); plot(B,ncdfg,'b-',B,ncdfgp,'r-',B,B,'m-'); 
legend('Gaussian Distrib. CDF','Processed Gaussian Dist. CDF',...
    'line y = x (Uniform Dist.)','Location','northwest');
title('Normalized CDF for Processed Gaussian Distribution');

