Click or drag to resize

Weight Mode Example

Below, an example implementation of a RelaxISPlugin_WeightMode is shown.

The function of the plugin is chosen for illustrative purposes.

Requirements
  • Reference to the RelaxIS_SDK.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 the implementation of a WeightMode plugin.

It generates weight values on an empirical basis for illustrative purposes.

Example
C#
// <copyright file="MyWeightMode.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.Plugins
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using RelaxIS_SDK.Common;
    using RelaxIS_SDK.libMath;
    using RelaxIS_SDK.Plugins;

    /***
     * WeightMode plugins calculate a set of weights for the real and imaginary residuals of a
     * model fit of an impedance spectrum in RelaxIS.
     */

    /// <summary>
    /// Defines an example <see cref="RelaxISPlugin_WeightMode"/> class.
    /// </summary>
    public class MyWeightMode
        : RelaxISPlugin_WeightMode
    {
        /***
         * First, implement the default plugin properties Name and Description that describe the plugin.
         * This is mainly used for display purposes.
         */

        /// <inheritdoc/>
        public override string Name
        {
            get { return "MyWeightMode"; }
        }

        /// <inheritdoc/>
        public override string Description
        {
            get { return "Calculates weights based on the values frequency and magnitude."; }
        }

        /// <inheritdoc/>
        public override string Abbreviation
        {
            get
            {
                return "MyWeights";
            }
        }

        /***
         * This function calculates the weights for the given input data. The input data
         * is already transformed into the correct transfer function at this point.
         * RelaxIS expects the weight mode to return one weight value for the real and imaginary
         * residuals per input datapoint. The Complex.Real value of the returned value is used
         * as the weight for the real residual, and the Complex.Imginary value of the returned
         * value is used as the weight for the imaginary residual.
         */

        /// <inheritdoc/>
        public override IList<Complex> CalculateWeights(IList<EISValue> data)
        {
            /*
            Uses completely arbitrary values for illustration.
            Interpolates a weight based on frequency with 1 for the lowest frequency and 100 for the highest frequency.
            In addition divides the weight by magnitude.
            */

            var n = data.Count;
            var fMin = Math.Log10(data.Min(d => d.Frequency));
            var fMax = Math.Log10(data.Max(d => d.Frequency));

            var fWeightMin = 1;
            var fWeightMax = 100;
            var res = new List<Complex>();
            for (var i = 0; i < n; i++)
            {
                var wF = Interpolate(Math.Log10(data[i].Frequency), fMin, fMax, fWeightMin, fWeightMax);
                var mag = new Complex(data[i].Real, data[i].Imaginary);
                var v = wF / mag.Magnitude;
                res.Add(new Complex(v, v));
            }

            return res;
        }

        /***
         * Helper function to calculate a linear interpolation.
         */

        private static double Interpolate(double x, double minOld, double maxOld, double minNew, double maxNew)
        {
            var x1 = minOld;
            var y1 = minNew;
            var x2 = maxOld;
            var y2 = maxNew;

            return y1 + ((x - x1) * (y2 - y1) / (x2 - x1));
        }
    }
}
See Also