Reading Data from LoggerPro

Starting Up

The following two commands prevent annoying warnings about the use of symbols similar to those used by Mathematica because Mathematica will not let us change their definitions.

Off[General::spell];Off[General::spell1];

Use SetDirectory to choose the directory where your data file is located.

SetDirectory["C:\Users\Anlage\Desktop"];

Read LoggerPro Output Data Files

To read a sequential data file into Mathematica use ReadList. The output files generated by LoggerPro consist of 7 header lines followed by data in two- or three-column format. The first column is the time; the second column is the data for channel 1; and the third column is the data for channel 2. The commands  below clear the variables to be used, open the file, skip the header line, and then read the data. The option RecordLists->True puts objects on separate lines into separate sublists.

Clear[in,datafile];
in=OpenRead["sampleLPData.txt"]; (* open the file - insert the name you used when you saved it in LoggerPro *)
Skip[in,String,7]; (* skip the header *)
datafile=ReadList[in,{Number,Number}];  (* This assumes 2-columns of data *)

Plotting your data

You can check your data with a plot:

ListPlot[datafile, PlotRange->All]   (*plots the data *)

Data is fitted to I(θ)=(Subscript[I, 0])[((Sin(α))^2)/(α^2)], where α=((π a)/λ)Sinθ = ((π a)/λ)(x/D) = ((π a v)/(λ D))(t - (Subscript[t, 0])), and (Subscript[t, 0]) is the location (in seconds) of the principal maximum (see plot above). Let (Subscript[t, 1]) be the location (in seconds) of the first minimum (valley) on the right hand side of the principal peak. Then ((π a v)/(λD)) can be estimated as (π/(( (Subscript[t, 1])- (Subscript[t, 0])))). Actually, data is fitted to the form: (Subscript[I, 0])*(((Sin(k*(t-c)))^2)+0.000000001) / ((((k*(t-c)))^2)+0.000000001). (Subscript[I, 0]) is the height of the principal maximum; k = ((π a v)/(λD)) = (π/(( (Subscript[t, 1])- (Subscript[t, 0])))); and c is the location of the principal maximum. The constant 0.000000001 is inserted to prevent the zero divide during the (χ^2) minimization.

Clear[dataFit];
dataFit=Select[datafile,((#[[1]]≥5)&&(#[[1]]≤17))&];  (* Select the part of the data that you want to fit *)

dataPlot = ListPlot[dataFit, PlotRange->All]

Choose the initial parameters for the nonlinearfit. This could be done with the mouse as follows. Move the cursor anywhere inside the graph above and click the mouse left button. A frame will be drawn around the graph. While holding the "Ctrl" key down and moving the cursor anywhere inside the graph, the location (x and y coordinates) of the cursor will be shown at the lower left cornor of the window. To find the location (t0) and the peak height (peak) of the principal maximum, position the cursor at the peak and the x-coordinate would be t0 and y-coordinate would be peak. To find t1, position the cursor at the first minimum on the right and the x-coordinate would then be t1.

Clear[t0,t1,peak];
t0=11.1;
t1=12.36;
peak=6.14;

Needs["Statistics`NonlinearFit`"]

Clear[model,startingValues,fitResult,Subscript,k,c,t];
model=offset+(Subscript[I, 0]) (((Sin[k (t-c)])^2)+0.000000001)/((((k (t-c)))^2)+0.000000001);
startingValues={{(Subscript[I, 0]),peak},{k,Pi/(t1-t0)},{c,t0}, {offset,0.34}};
Timing[fitResult=NonlinearRegress[dataFit,model,t,startingValues,RegressionReport -> {BestFit,ParameterCITable}]]

fitPlot=Plot[(BestFit/.fitResult),{t,5,17},	Frame-> True,PlotStyle-> {RGBColor[1,0,1]} ,
		FrameLabel-> {"Sample Time in Seconds","Fitted Intensity"},
		PlotLabel-> "Fitted Intensity Graph",PlotRange-> All]

Show[fitPlot,dataPlot,PlotRange-> All]

Now Look at a 3-Slit Data set and fit it

Clear[in,datafile];
in=OpenRead["3 slit.txt"]; (* open the file - NOTE THAT THIS FILE HAS 3 COLUMNS! *)
Skip[in,String]; (* skip the header NOTE THAT THIS FILE HAS ONLY 1 HEADER LINE! *)
datafile=ReadList[in,Number,RecordLists->True];

{time,channel0,channel1}=Transpose[datafile];
adata0=Transpose[{time,channel0}];
adata1=Transpose[{time,channel1}];

ListPlot[adata0, PlotRange->All](*plots the data *)

Data is fitted to I(θ)=(Subscript[I, 0])[((Sin(α))^2)/(α^2)][((Sin(Nβ))^2)/((Sin(β))^2)] for multiple-slit cases, where N is the number of slits, α =((π a)/λ)Sinθ = ((π a)/λ)(x/D) = ((π a v)/(λ D))(t - (Subscript[t, 0])) , β = ((π d)/λ)Sinθ = ((π d)/λ)(x/D) = ((π d v)/(λ D))(t - (Subscript[t, 0])), and (Subscript[t, 0]) is the location (in seconds) of the central diffraction maximum (see plot above). Let  (Subscript[t, 1]) be the location (in seconds) of the first diffraction (not interference) minimum on the right hand side of the principal peak; and (Subscript[t, 2]) be the location (in seconds) of the first interference maximum on the right hand side of the principal maximum. Then ((π a v)/(λD)) and ((κ d v)/(λ D)) can be estimated from channel 0 data (see plot above) as (π/(( (Subscript[t, 1])- (Subscript[t, 0])))) and  (π/(( (Subscript[t, 2])- (Subscript[t, 0])))), respectively.

Clear[dataFit];
dataFit=Select[adata0,((#[[1]]≥5.0)&&(#[[1]]≤15.0))&];

dataPlot = ListPlot[dataFit, PlotRange->All]

Clear[t0,t1,t2,peak,n];
t0=9.72;
t1=11.4;
t2=10.2;
peak=0.985;
n=3;

Clear[model,startingValues,fitResult,kα,kβ,c,t];
model=(Subscript[I, 0]) ((((Sin[kα (t-c)])^2)+0.000000001)/((((kα (t-c)))^2)+0.000000001))*((((Sin[n (kβ (t-c))])^2)+(n^2) 0.000000001)/(Sin[kβ (t-c)]^2+0.000000001))+bgd;
startingValues={{(Subscript[I, 0]),peak},{kα,Pi/(t1-t0)},{kβ,Pi/(t2-t0)},{c,t0},{bgd,0.05}};
Timing[fitResult=NonlinearRegress[dataFit,model,t,startingValues,RegressionReport -> {BestFit,ParameterCITable}]]

Clear[fitPlot];
fitPlot=Plot[(BestFit/.fitResult),{t,5,15},	Frame-> True,PlotStyle-> {RGBColor[1,0,1]} ,
		FrameLabel-> {"Sample Time in Seconds","Fitted Intensity"},
		PlotLabel-> "Fitted Intensity Graph",PlotRange-> {-0.02,1.0}]

Show[dataPlot,fitPlot,PlotRange-> {{5,15},{-0.02,1.0}}]