SetSystemOptions["LibraryLinkOptions"->"TestFloatingPointExceptions"->False];
matData=Import["/Users/andreydanilin/Desktop/LIGO/Scatterimg measurement/bDarmOut_dataset.mat","Data","IndeterminateValue"->0];
RINZM5=Flatten[matData[[1]][[All,1]]];
RINZM2=Flatten[matData[[1]][[All,2]]];
RINref=Flatten[matData[[1]][[All,3]]];
freq=Flatten[matData[[2]]];

a=1;
E0=1*10^-3;
λ=1064.0*10^-9;
A=1.2*10^-6;
Ω=1*2 π;
samplingRate=1200;
timeRange=200;
timeStep=1/samplingRate;
timeValues=Table[t,{t,0,timeRange,timeStep}];

calculateRIN[r_]:=Module[{x,RIN},x[t_]:=2 r Sin[(4*π/λ)*A Sin[Ω t]];
RIN=x/@timeValues;
RIN]

r1 = 0.04; (*% Reflection coefficient 1*)
r2 = 0.001 ; (*% Reflection coefficient 2*)
RIN1=calculateRIN[r1/100];
RIN2=calculateRIN[r2/100];


result1=ResourceFunction["WelchSpectralEstimate"][RIN1,samplingRate, "Window"-> HannWindow, "OverlapOffset" -> Automatic];
result2=ResourceFunction["WelchSpectralEstimate"][RIN2,samplingRate, "Window"-> HannWindow, "OverlapOffset" -> Automatic];

result1[[All,2]]=Sqrt[result1[[All,2]]];
result2[[All,2]]=Sqrt[result2[[All,2]]];

fontSetting=Directive[FontFamily->"TeX Gyre Termes",Black,FontSize->16];
tickFontSetting=Directive[FontFamily->"TeX Gyre Termes",Black,FontSize->14];
color1 = RGBColor[{255, 105,97}/255];
color2 = RGBColor[{119, 158,203}/255];
plotColors={Black,color2,color1,Directive[color1, Dashed],Directive[color2, Dashed]};

g = ListLogLogPlot[{Transpose[{freq,RINref}],Transpose[{freq,RINZM2}],Transpose[{freq,RINZM5}],result1,result2},PlotRange->{{7,100},{2*10^-8,0.001}},Joined->True,Frame->True,FrameStyle->Directive[Black,Thick],FrameLabel->{Style["Frequency, Hz",fontSetting],Style["RIN, 1/(Sqrt[Hz])",fontSetting]},FrameTicksStyle->tickFontSetting,PlotLegends->Placed[LineLegend[{"No excitation","ZM2 excitation SFI2, 25.2°","ZM5 excitation, SFI2, 25.2°","Fit (r = "<>ToString[r1]<>"%, Ω = "<>ToString[Ω/2/π]<>" Hz)", "Fit (r = "<>ToString[r2]<>"%, Ω = "<>ToString[Ω/2/π]<>" Hz)"},LabelStyle->fontSetting,LegendMarkers->None,LegendFunction->(Framed[#,RoundingRadius->5,FrameStyle->Black,Background->White]&)],{0.8,0.8}],PlotStyle->Map[Directive[#,Thickness[0.004]]&,plotColors],ImageSize->700]
Export["/Users/andreydanilin/Desktop/LIGO/Scatterimg measurement/Untitled.png",g,ImageResolution->300]