%% CFHigham University of Glasgow February 2026

% Timing Diffusion

addpath('Diffusion')

% We use net0508.mat (Experiment2) to time the generation of data

addpath('Experiment2')

load('net0508.mat')
load('data0508.mat')

imageSize=[4 4];
numChannels=1;
numImages=16;%100;

numNoiseSteps = 100;
betaMin = 1e-4;
betaMax = 0.02;
varianceSchedule = linspace(betaMin,betaMax,numNoiseSteps);


% Compute variance schedule parameters.
alphaBar = cumprod(1 - varianceSchedule);
alphaBarPrev = [1 alphaBar(1:end-1)];
posteriorVariance = varianceSchedule.*(1 - alphaBarPrev)./(1 - alphaBar);


images = randn([imageSize numChannels numImages]);
for j=1:100
tic
% Reverse the diffusion process.
numNoiseSteps = length(varianceSchedule);

for noiseStep = numNoiseSteps:-1:1
    if noiseStep ~= 1
        z = randn([imageSize,numChannels,numImages]);
    else
        z = zeros([imageSize,numChannels,numImages]);
    end

    % Predict the noise using the network.
    predictedNoise = predict(net,images,noiseStep);

    sqrtOneMinusBeta = sqrt(1 - varianceSchedule(noiseStep));
    addedNoise = sqrt(posteriorVariance(noiseStep))*z;
    predNoise = varianceSchedule(noiseStep)*predictedNoise/sqrt(1 - alphaBar(noiseStep));

    images = 1/sqrtOneMinusBeta*(images - predNoise) + addedNoise;


end
tocj(j)=toc;
end


  

  %% saved results 
   
  addpath(genpath('Experiment3'))
   
  % NumImages=16;
  load('tocj_100ns_16img_0915.mat')
  % mean time cost (s)
  mtocj = mean(tocj(6:end));
  
  % NumImages=100;
  load('tocj_100ns_100img_0915.mat')
  % mean time cost (s)
  mtocj = mean(tocj(6:end));


 














