Click or drag to resize

WCF Link - Fitting

The code below shows some examples of using fitting functions exposed by the IRelaxISWCFLink WCF interface.

The functions generally first construct a RelaxISWCFLinkProxy object by calling GetDefaultProxy. This proxy object is then used to call the functions. This will then execute the functions in the RelaxIS instance that has an active WCF Link.

Requirements
  • Reference to the RelaxIS_SDK.dll

  • Reference to System.ServiceModel.dll

  • .NET Framework 4.7.2 target (class library)

  • Recommended: Development environment with compiler e.g. RelaxIS SDK Code Editor, Microsoft Visual Studio

Demonstrates

This examples illustrates how to get the results of a Single and an Autofit of an impedance spectrum.

The spectrum data is generated by a GetExampleData() function that outputs spectrum data as an IListEISDatapoint.

The example then generates parameters for a model using the GetDefaultParameters function, sets up initial values if needed and runs the fit.

The results are written to the console.

Example
C#
// <copyright file="WCFFitting.cs" company="rhd instruments GmbH and Co. KG">
// Copyright (c) rhd instruments GmbH and Co. KG. All rights reserved.
// Licensed under the MIT No Attribution (MIT-0) license. See section 'License' in the 'SDK Examples / Tutorials' topic for full license information.
// </copyright>

namespace RelaxIS_SDK_Examples.WCFInterface
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using RelaxIS_SDK.Common;
    using RelaxIS_SDK.RelaxISWCFLink;

    /// <summary>
    /// Implements examples of using the WCF fit functions.
    /// </summary>
    public static class WCFFitting
    {
        /// <summary>
        /// An example of performing a single spectrum fit via the WCF Link.
        /// </summary>
        public static void PerformSingleFit()
        {
            // Get the interface object
            using (var wcf = RelaxISWCFLinkProxy.GetDefaultProxy())
            {
                // Create a spectrum that contains the data
                var spectrum = new ImpedanceSpectrum(CommonFunctions.GetExampleData());

                // Define fit model
                spectrum.Analysis.Model = "R-(R)(C)";

                // Get parameters for this model.
                var parameters = wcf.GetDefaultParameters(spectrum.Analysis.Model);

                // Set initial parameter values
                parameters[0].Value = 40;
                parameters[1].Value = 70;
                parameters[2].Value = 2e-6;

                spectrum.Analysis.Parameters.AddRange(parameters);

                // Empty list to signal that all parameters are active. This overrides the fixed indizes in the parameter list.
                // If null is used as the argument in the GetFit function, the spectrum parameter's fixed property is checked instead.
                var fixedIndices = new List<int>();

                // Check the Internal Name of the available transfer functions and weight modes in the RelaxIS Setup -> Plugins list.
                var transferFunction = "Impedance";
                var weightMode = "Proportional Weighting";

                var fitResult = wcf.GetFit(spectrum, transferFunction, weightMode, fixedIndices);
                OutputFitResult(wcf, fitResult, spectrum, parameters, transferFunction);
            }
        }

        /// <summary>
        ///  An example of performing an Auto Fit using the WCF interface.
        /// </summary>
        public static void PerformAutoFit()
        {
            // Get the interface object
            using (var wcf = RelaxISWCFLinkProxy.GetDefaultProxy())
            {
                // Create a spectrum that contains the data
                var spectrum = new ImpedanceSpectrum(CommonFunctions.GetExampleData());

                // Define fit model
                spectrum.Analysis.Model = "R-(R)(C)";

                // Get parameters for this model.
                var parameters = wcf.GetDefaultParameters(spectrum.Analysis.Model);

                // Set search ranges. Leave default to allow RelaxIS to find optimized limits automatically.
                parameters[0].Limits.SetLimits(1, 1000);

                spectrum.Analysis.Parameters.AddRange(parameters);

                // Check the Internal Name of the available transfer functions and weight modes in the RelaxIS Setup -> Plugins list.
                var transferFunction = "Impedance";
                var weightMode = "Proportional Weighting";

                var fitResult = wcf.GetAutoFit(spectrum, transferFunction, weightMode, 20, 50);
                OutputFitResult(wcf, fitResult, spectrum, parameters, transferFunction);
            }
        }

        private static void OutputFitResult(IRelaxISWCFLink wcf, RelaxISFitResult fitResult, ImpedanceSpectrum spectrum, IList<Fitparameter> parameters, string transferFunction)
        {
            if (fitResult.Status == RelaxISFitResult.FitStatus.FitOK)
            {
                // Output the SSR
                Console.WriteLine(string.Format("SSR: {0}", fitResult.ESS));

                // Output the parameter values
                for (var i = 0; i < fitResult.ParameterValues.Length; i++)
                {
                    Console.WriteLine(string.Format("{0} => {1}", parameters[i].Name, fitResult.ParameterValues[i]));
                }

                // Get plot data for the input frequencies and the result parameters
                var freqs = spectrum.Data.Select(d => d.Impedance.Frequency).ToArray();
                var plotData = wcf.GetPlotDataFixedFreqs(spectrum.Analysis.Model, fitResult.ParameterValues, transferFunction, freqs);
                Console.WriteLine("Plot Data:");
                for (int i = 0; i < freqs.Length; i++)
                {
                    Console.WriteLine(string.Format("{0}\t{1}\t{2}", plotData[0][i], plotData[1][i], plotData[2][i]));
                }
            }
            else
            {
                Console.WriteLine(string.Format("Fit not successful: {0}", fitResult.Status));
            }
        }
    }
}
See Also

Reference

IRelaxISWCFLink

Other Resources

List of examples