% MICROCRE teaching simulation: Gaussian point pair and pixel sampling % NOT EXECUTED IN MATLAB/Octave in the authoring environment. % Python/SciPy counterpart has been run; see validation-report.json. % Standalone MATLAB R2016b+ script (local functions); no extra toolbox required. % Edit p below, run this file, inspect outputs and assumptions before use. % Formula: I(x)=Σexp[−(x−x_j)²/(2σ²)];σ=FWHM/(2√(2ln2));pixel_i=(1/p)∫pixel I(x)dx % Assumption: 等强非相干点源、移不变高斯PSF、方形像素一维截面。 % Assumption: 连续峰值通过驻点方程x=a·tanh(ax/σ²)计算,a为半间距;不使用绘图网格最大值归一化。绘图合并全域1001点与每个峰附近401点。 % Assumption: SciPy erf计算像素孔径积分;浏览器采用erf数值近似,默认像素孔径曲线对参考的绝对差已验证小于2×10⁻⁶。 % Limitation: 高斯PSF是教学假设,不是完整Airy衍射或具体物镜实测PSF。 % Limitation: 不自动判定Rayleigh分辨率;无噪声、MTF标定、去马赛克与重建算法。 % Source: https://www.edmundoptics.com/knowledge-center/application-notes/imaging/resolution/ % Source: https://svi.nl/Nyquist-Calculator % This is not product performance, calibration, or compliance evidence. clear; close all; clc; p.fwhm = 0.5; % 假设PSF半高全宽 [µm] p.separation = 0.8; % 两点间距 [µm] p.pixel = 0.2; % 物方像素间距/孔径 [µm] p.phase = 0; % 像素相位 [pixel] assert(isfinite(p.fwhm) && p.fwhm>=0.01 && p.fwhm<=100, 'Invalid fwhm'); assert(isfinite(p.separation) && p.separation>=0 && p.separation<=100, 'Invalid separation'); assert(isfinite(p.pixel) && p.pixel>=0.001 && p.pixel<=100, 'Invalid pixel'); assert(isfinite(p.phase) && p.phase>=0 && p.phase<=0.999, 'Invalid phase'); sigma=p.fwhm/(2*sqrt(2*log(2))); extent=p.separation/2+3*p.fwhm; a=p.separation/2; if a<=sigma,peak_x=0;else lo=0;hi=a;for j=1:80,mid=(lo+hi)/2;if a*tanh(a*mid/sigma^2)>mid,lo=mid;else,hi=mid;end,end;peak_x=(lo+hi)/2; end x=sort([linspace(-extent,extent,1001),linspace(-a-3*p.fwhm,-a+3*p.fwhm,401),linspace(a-3*p.fwhm,a+3*p.fwhm,401),-peak_x,0,peak_x]); continuous=@(z)exp(-0.5*((z-p.separation/2)/sigma).^2)+exp(-0.5*((z+p.separation/2)/sigma).^2); y=continuous(x); peak=continuous(peak_x); k=ceil(extent/p.pixel)+1; assert(2*k+1<=4001,'Pixel grid too large.'); sample=((-k:k)+p.phase)*p.pixel; sample=sample(abs(sample)<=extent+p.pixel/2); sampled=zeros(size(sample)); for c=[-p.separation/2,p.separation/2] sampled=sampled+sigma*sqrt(pi/2)/p.pixel*(erf((sample+p.pixel/2-c)/(sqrt(2)*sigma))-erf((sample-p.pixel/2-c)/(sqrt(2)*sigma))); end lines=[s('Continuous Gaussian pair',x,y/peak,'um','relative intensity'),s('Pixel aperture average',sample,sampled/peak,'um','relative intensity')]; out=struct('pixels_per_fwhm',p.fwhm/p.pixel,'midpoint_to_peak',continuous(0)/peak,'nyquist',1000/(2*p.pixel)); outdir=fullfile(pwd,'resolution-sampling_results'); if ~exist(outdir,'dir'),mkdir(outdir);end fid=fopen(fullfile(outdir,'resolution-sampling.csv'),'w'); assert(fid>=0,'Cannot open CSV'); fprintf(fid,'series,x,y,x_unit,y_unit\n'); for j=1:numel(lines) for k=1:numel(lines(j).x) fprintf(fid,'"%s",%.17g,%.17g,"%s","%s"\n',lines(j).name,lines(j).x(k),lines(j).y(k),lines(j).xu,lines(j).yu); end end fclose(fid); fig=figure('Color','w');hold on; for j=1:numel(lines) if lines(j).plot,plot(lines(j).x,lines(j).y,'LineWidth',1.5,'DisplayName',lines(j).name);end end grid on;legend('show','Location','best');xlabel(lines(1).xu);ylabel(lines(1).yu); title('Gaussian point pair and pixel sampling','Interpreter','none'); print(fig,fullfile(outdir,'resolution-sampling.png'),'-dpng','-r160'); fid=fopen(fullfile(outdir,'resolution-sampling-summary.json'),'w');fprintf(fid,'%s',jsonencode(struct('inputs',p,'outputs',out,'notice','Teaching model; not product validation.')));fclose(fid); disp(out); function a=s(name,x,y,xu,yu) a=struct('name',name,'x',x,'y',y,'xu',xu,'yu',yu,'plot',true); end function q=qlinear(values,p) v=sort(values); pos=p*(numel(v)-1); k=floor(pos)+1; f=pos-floor(pos); if k==numel(v),q=v(k);else,q=v(k)*(1-f)+v(k+1)*f;end end