% MICROCRE teaching simulation: Synthetic profile and linear detrending % 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: z=A₁sin(2πx/λ₁)+A₂sin(2πx/λ₂+0.7)+bx;z_res=z−(a_fit+b_fit x) % Assumption: 2001等距点,最短波长至少10样点;仅作最小二乘直线去趋势。 % Limitation: 未应用S/L滤波、截止波长、探针卷积和标准评价规则。 % Limitation: 这里不能把数值直接用于标准Ra/Rq验收。 % Source: https://www.nist.gov/system/files/documents/calibrations/asm-hb18.pdf % This is not product performance, calibration, or compliance evidence. clear; close all; clc; p.length = 4; % 评价长度 [mm] p.amplitude1 = 0.8; % 长波振幅 [µm] p.wavelength1 = 0.4; % 长波波长 [mm] p.amplitude2 = 0.2; % 短波振幅 [µm] p.wavelength2 = 0.06; % 短波波长 [mm] p.slope = 0.4; % 线性倾斜 [µm/mm] assert(isfinite(p.length) && p.length>=0.01 && p.length<=100, 'Invalid length'); assert(isfinite(p.amplitude1) && p.amplitude1>=0 && p.amplitude1<=100, 'Invalid amplitude1'); assert(isfinite(p.wavelength1) && p.wavelength1>=0.001 && p.wavelength1<=100, 'Invalid wavelength1'); assert(isfinite(p.amplitude2) && p.amplitude2>=0 && p.amplitude2<=100, 'Invalid amplitude2'); assert(isfinite(p.wavelength2) && p.wavelength2>=0.001 && p.wavelength2<=100, 'Invalid wavelength2'); assert(isfinite(p.slope) && p.slope>=-100 && p.slope<=100, 'Invalid slope'); x=linspace(0,p.length,2001); assert(p.length/2000<=min(p.wavelength1,p.wavelength2)/10,'Insufficient spatial sampling.'); z=p.amplitude1*sin(2*pi*x/p.wavelength1)+p.amplitude2*sin(2*pi*x/p.wavelength2+0.7)+p.slope*x; b=sum((x-mean(x)).*(z-mean(z)))/sum((x-mean(x)).^2); a=mean(z)-b*mean(x); residual=z-(a+b*x); lines=[s('Synthetic raw profile',x,z,'mm','um'),s('Linear detrend only',x,residual,'mm','um')]; out=struct('mean_absolute',mean(abs(residual)),'rms',sqrt(mean(residual.^2)),'peak_to_valley',max(residual)-min(residual),'fitted_slope',b); outdir=fullfile(pwd,'roughness-profile_results'); if ~exist(outdir,'dir'),mkdir(outdir);end fid=fopen(fullfile(outdir,'roughness-profile.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('Synthetic profile and linear detrending','Interpreter','none'); print(fig,fullfile(outdir,'roughness-profile.png'),'-dpng','-r160'); fid=fopen(fullfile(outdir,'roughness-profile-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