Title

Names

Clear["Global`*"]; DateString[]
SetDirectory[NotebookDirectory[]];

files = FileNames["TO-driven*.csv"]

files[[2]]

osc[V_, f_, ϕ_, Voff_, t_]:= V Sin[2 π f t - ϕ] + Voff

osc[V, f, ϕ, Voff, t]

files

fset = {0.20, 0.40, 0.78, 0.82, 1.00}

FilePrint[files[[2]], 3]

Select[Import[files[[2]], "CSV"], Length[#] == 3 && VectorQ[#, NumberQ] &]

getVin[filename_]:= Select[Import[filename, "CSV"], Length[#] == 3 && VectorQ[#, NumberQ] &][[All, {1, 2}]]

getVin[files[[2]]]

getVout[filename_]:= Select[Import[filename, "CSV"], Length[#] == 3 && VectorQ[#, NumberQ] &][[All, {1, 3}]]

showData[data_]:= ListPlot[data, PlotStyle->Red, LabelStyle->Larger, AxesLabel -> {"t (s)", "V (V)"}, ImageSize -> Scaled[0.5]]

showData[getVin[files[[5]]]]

Clear[fitV]
fitV[data_, ffg_, ϕguess_] := NonlinearModelFit[data, 
{osc[V, f, ϕ, Voff, t], V > 0, ffg - 0.01 <= f <= ffg + 0.01},
{V, {f, ffg}, {ϕ,ϕguess}, Voff},
t]

i = 1;
vdata = getVin[files[[i]]];
fset[[i]]
fit = fitV[vdata, fset[[i]], ϕinguess[[i]]];
fit["BestFitParameters"]
Show[{
showData[vdata],
Plot[fit[t], {t, 0, 30}]}, PlotRange -> {{0, 10}, All}]

ϕinguess = {π, 0, 0, 0, 0};
ϕoutguess = {π,0, π/2, π/2, π }

fset

Table[
{vdata = getVout[files[[i]]];
fset[[i]],
fit = fitV[vdata, fset[[i]], ϕoutguess[[i]]];
fit["BestFitParameters"],
Show[{
showData[vdata],
Plot[fit[t], {t, 0, 30}]}, PlotRange -> {{0, 10}, All}]}, {i, 1, Length[files]}]

infits = Table[fitV[getVin[files[[i]]], fset[[i]], ϕinguess[[i]]], {i, 1, Length[files]}]

Plot[infits[[2]][t], {t, 0, 30}]

infitparameters = Table[infits[[i]]["BestFitParameters"], {i, 1, Length[files]}]

Vins = V /. infitparameters

ϕins = ϕ /. infitparameters