Weighted Mean of multiple experiments.
This function is for finding the weighted mean (by error) of multiple experiments. It requires that input files are of equal length and scaling. There should be two sets of 1Dwaves already open (perhaps in a table): waves containing actual data points, and waves containing the experimental errors for those points. Each set of waves should have an original base name followed by some alphabetic or numeric iteration, for example 3 experiments with data files data0, data1, and data2, and corresponding error files st_dev0, st_dev1, and st_dev3.
The base names are used as input: function("data","st_dev"), and the program will return the number of experiments it finds (it searches all currently open waves!), as well as printing the files in case errant waves are included. Final output is in the form of the weighted means of each row of points, and the error on those means.
#pragma rtGlobals=1 // Use modern global access method. function weighted(data_name,error_name) string data_name,error_name // names of the base files to be averaged string data_handles = WaveList(data_name+"*",";","") // finds and lists names of data waves string error_handles = WaveList(error_name+"*",";","") // finds and lists names of error waves variable i=0// iteration variable variable n = ItemsInLIst(data_handles) // number of experiments to be averaged print n, "experiments will be averaged." variable j = numpnts($stringfromlist(0,data_handles)) // length of input,output waves. input waves must be of equal length. make/o/d/n=(j) numerator=0, denominator=0 // for building the final waves (see loop math) make/o/d/n=(j) weighted_mean, weighted_error, h_data, h_error for(i=0;i<=(n-1);i+=1) // Initialize variables;continue test wave h_data = $stringfromlist(i,data_handles) wave h_error = $stringfromlist(i,error_handles) print h_data print h_error numerator = numerator + (h_data / ((h_error)^2)) denominator = denominator + 1/((h_error)^2) endfor // Execute body code until continue test is FALSE weighted_mean=numerator/denominator appendtotable weighted_mean weighted_error=sqrt(1/denominator) appendtotable weighted_error end
beginner programmer here, so feel free to patch things up or point out improvements..