public static double[] FitLorenzianToData(double[] voltages, double[] signal) { double high = getMax(voltages).value; double low = getMin(voltages).value; System.Diagnostics.Debug.WriteLine(" " + low.ToString() + " " + high.ToString()); LorentzianFitter lorentzianFitter = new LorentzianFitter(); // takes the parameters (in this order, in the double[]) // N: background // Q: signal // c: centre // w: width dataPoint max = getMax(signal); dataPoint min = getMin(signal); lorentzianFitter.Fit(voltages, signal, new double[] { min.value, max.value - min.value, voltages[max.index], (high - low) / 10, }); double[] coefficients = lorentzianFitter.Parameters; fitFailSafe(coefficients, low, high); return(new double[] { coefficients[3], coefficients[2], coefficients[1], coefficients[0] }); //to be consistent with old convention. }
private void dispatcherTimer_Tick(object sender, EventArgs e) { Dispatcher.CurrentDispatcher.Invoke(() => { for (var sampleCount = 0; sampleCount < Constants.NUMSAMPLES; sampleCount++) { colData[sampleCount] = acquireData(); //adjust the data colData[sampleCount].normalForce *= normalGain; colData[sampleCount].normalForce += normalOffset; colData[sampleCount].shearForce *= shearGain; colData[sampleCount].shearForce += shearOffset; } //Average the data //This just does a simple linear average, which is what was done in the original labview program. This could be changed. double shearSum = 0; double normalSum = 0; foreach (var datum in colData) { shearSum += datum.shearForce; normalSum += datum.normalForce; } var data = new dataPoint(); data.normalForce = normalSum / Constants.NUMSAMPLES; data.shearForce = shearSum / Constants.NUMSAMPLES; data.time = (DateTime.Now - start).TotalSeconds; dataPoints.Add(data); dataAcquiredEvent(this, new EventArgs()); }); }
/// <summary> /// Constructs a DAQ object. /// </summary> /// <param name="normalChannelId">The channel of the normal force probe</param> /// <param name="shearChannelId">The channel of the shear force probe</param> public DAQ(string normalChannelId, string shearChannelId) { Console.WriteLine("RUN INIT"); //Initialize lists dataPoints = new List <dataPoint>(); start = DateTime.Now; try { //Define channels normalForceChannel = analogInTask.AIChannels.CreateVoltageChannel(normalChannelId, Constants.NORMALCHANNELNAME, AITerminalConfiguration.Differential, 0, 5, AIVoltageUnits.Volts); shearForceChannel = analogInTask.AIChannels.CreateVoltageChannel(shearChannelId, Constants.SHEARCHANNELNAME, AITerminalConfiguration.Differential, 0, 5, AIVoltageUnits.Volts); //Define reader reader = new AnalogMultiChannelReader(analogInTask.Stream); var dataCollection = new Thread(beginTrial); dataCollection.IsBackground = true; dataCollection.Start(); beginTrial(); } catch (DaqException e) { Console.Write("DAQEXCEPTION: " + e.Message); MessageBox.Show( "Failed to connect to force probes. Please check that they are connected properly and that all required drivers are installed."); var data = new dataPoint(); data.time = -1; data.normalForce = 0; data.shearForce = 0; dataPoints.Add(data); } }
public static double[] FitLorenzianToSlaveData(CavityScanData data, double limitLow, double limitHigh) { double[] voltages = data.parameters.CalculateRampVoltages(); LorentzianFitter lorentzianFitter = new LorentzianFitter(); // takes the parameters (in this order, in the double[]) // N: background // Q: signal // c: centre // w: width dataPoint max = getMax(data.SlavePhotodiodeData); dataPoint min = getMin(data.SlavePhotodiodeData); lorentzianFitter.Fit(voltages, data.SlavePhotodiodeData, new double[] { min.value, max.value - min.value, voltages[max.index], (data.parameters.High - data.parameters.Low) / 10, }); double[] coefficients = lorentzianFitter.Parameters; fitFailSafe(coefficients, limitLow, limitHigh); return(new double[] { coefficients[3], coefficients[2], coefficients[1], coefficients[0] }); //to be consistent with old convention. }
private static dataPoint getMax(double[] data) { dataPoint max = new dataPoint(); max.value = 0; for (int i = 0; i < data.Length; i++) { if (data[i] > max.value) { max.value = data[i]; max.index = i; } } return max; }
private static dataPoint getMin(double[] data) { dataPoint min = new dataPoint(); min.value = data[0]; for (int i = 0; i < data.Length; i++) { if (data[i] < min.value) { min.value = data[i]; min.index = i; } } return(min); }
private static dataPoint getMax(double[] data) { dataPoint max = new dataPoint(); max.value = 0; for (int i = 0; i < data.Length; i++) { if (data[i] > max.value) { max.value = data[i]; max.index = i; } } return(max); }
//This function speeds up the fitting process by passing the last fitted parameters as a guess, // and by restricting the fit to only those points near to the peak public static double[] FitLorenzianToData(double[] voltages, double[] signal, double[] parameters, double pointsToConsiderEitherSideOfPeakInFWHMs, int maximumNLMFSteps) { double high = getMax(voltages).value; double low = getMin(voltages).value; double pointsToConsider = 0; System.Diagnostics.Debug.WriteLine(" " + low.ToString() + " " + high.ToString()); LorentzianFitter lorentzianFitter = new LorentzianFitter(); double[][] allxypairs = new double[voltages.Length][]; int j = 0; for (int i = 0; i < voltages.Length; i++) { allxypairs[i] = new double[2]; allxypairs[i][0] = voltages[i]; allxypairs[i][1] = signal[i]; } if (pointsToConsiderEitherSideOfPeakInFWHMs * parameters[0] < 10) { pointsToConsider = 10; } else { pointsToConsider = pointsToConsiderEitherSideOfPeakInFWHMs * parameters[0]; } for (int i = 0; i < voltages.Length; i++) { if ((allxypairs[i][0] > (parameters[1] - pointsToConsider)) && (allxypairs[i][0] < (parameters[1] + pointsToConsider))) { j++; } } double[] selectedvoltages = new double[j]; double[] selectedsignal = new double[j]; for (int i = 0, k = 0; i < voltages.Length; i++) { if ((allxypairs[i][0] > (parameters[1] - pointsToConsider)) && (allxypairs[i][0] < (parameters[1] + pointsToConsider))) { selectedvoltages[k] = allxypairs[i][0]; selectedsignal[k] = allxypairs[i][1]; k++; } } // takes the parameters (in this order, in the double[]) // N: background // Q: signal // c: centre // w: width dataPoint max = getMax(signal); dataPoint min = getMin(signal); lorentzianFitter.Fit(selectedvoltages, selectedsignal, new double[] { parameters[3], parameters[2], parameters[1], parameters[0] }, 0, 0, maximumNLMFSteps); double[] coefficients = lorentzianFitter.Parameters; fitFailSafe(coefficients, low, high); return(new double[] { coefficients[3], coefficients[2], coefficients[1], coefficients[0] }); //to be consistent with old convention. }
private static dataPoint getMin(double[] data) { dataPoint min = new dataPoint(); min.value = data[0]; for (int i = 0; i < data.Length; i++) { if (data[i] < min.value) { min.value = data[i]; min.index = i; } } return min; }