Click here to Skip to main content
15,895,709 members
Articles / Programming Languages / C#

Grandiose Projects 3. Compatibility with Simulink

Rate me:
Please Sign up or sign in to vote.
4.27/5 (11 votes)
8 Feb 2010CPOL23 min read 47.8K   5.9K   38  
Import of Simulink files
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Runtime.Serialization;

using CategoryTheory;
using DiagramUI;
using DiagramUI.Labels;
using DataPerformer.Interfaces;

namespace DataPerformer
{
    /// <summary>
    /// Vector formula data transformer
    /// </summary>
    [Serializable()]
    public class FourierSeries : DataConsumer, IMeasurements, ISerializable,
        IPostSetArrow, IAssociatedObject
    {
        /// <summary>
        /// Output measurements
        /// </summary>
        private IMeasure[] output = new IMeasure[2];

        /// <summary>
        /// Results of calculation
        /// </summary>
        private double[,] result = new double[2, 2];

        /// <summary>
        /// Arguments
        /// </summary>
        private string argument;

        /// <summary>
        /// Input measurements
        /// </summary>
        private IMeasure input;

        /// <summary>
        /// List of series
        /// </summary>
        private ArrayList seriesList = new ArrayList();

        /// <summary>
        /// Series
        /// </summary>
        private Series[] series = new Series[2];

        /// <summary>
        /// Names of unary
        /// </summary>
        private Hashtable seriesTable = new Hashtable();


        /// <summary>
        /// Constructor
        /// </summary>
        public FourierSeries()
            : base(65)
        {
            argument = "";
            for (int i = 0; i < 2; i++)
            {
                seriesTable[i] = "";
            }
            Double a = 0;
            output[0] = new MeasureDerivation(a, new Func<object>(getRe), new Measure(getReDerivation, ""), "Real");
            output[1] = new MeasureDerivation(a, new Func<object>(getIm), new Measure(getImDerivation, ""), "Image");
        }

        /// <summary>
        /// Deserialization constructor
        /// </summary>
        /// <param name="info">Serialization info</param>
        /// <param name="context">Streaming context</param>
        public FourierSeries(SerializationInfo info, StreamingContext context)
            :
            base(65)
        {
            try
            {
                argument = (string)info.GetValue("Argument", typeof(string));
                seriesTable = info.GetValue("SeriesTable", typeof(Hashtable)) as Hashtable;
            }
            catch (Exception)
            {
                seriesTable = new Hashtable();
                seriesTable[0] = "";
                seriesTable[1] = "";
            }
            if (seriesTable == null)
            {
            }
            Double a = 0;
            output[0] = new MeasureDerivation(a, new Func<object>(getRe), new Measure(getReDerivation, ""), "Real");
            output[1] = new MeasureDerivation(a, new Func<object>(getIm), new Measure(getImDerivation, ""), "Image");
        }

        /// <summary>
        /// ISerializable interface implementation
        /// </summary>
        /// <param name="info">Serialization info</param>
        /// <param name="context">Streaming context</param>
        new public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Argument", argument);
            info.AddValue("SeriesTable", seriesTable);
        }

        /// <summary>
        /// Creates correspond xml
        /// </summary>
        /// <param name="doc">document to create element</param>
        /// <returns>The created element</returns>
        /*new public XmlElement CreateXml(XmlDocument doc)
        {
			
            XmlElement el = doc.CreateElement("FourierSeries");
            return el;
        }*/

        /// <summary>
        /// Series table
        /// </summary>
        /*	public Hashtable SeriesTable
            {
                get
                {
                    return seriesTable;
                }
            }

            /// <summary>
            /// Table of series names
            /// </summary>
            /*	public Hashtable SeriesNames
                {
                    get
                    {
                        return seriesNames;
                    }
                }*/

        /// <summary>
        /// Adds series to list
        /// </summary>
        /// <param name="series">Series to remove</param>
        /*	public void AddSeries(Series series)
            {
                if (seriesList.Count == 2)
                {
                    throw new Exception("Too many series");
                }
                seriesList.Add(series);
            }


            /// <summary>
            /// Removes series
            /// </summary>
            /// <param name="series">Series to remove</param>
            public void RemoveSeries(Series series)
            {
                seriesList.Remove(series);
            }*/


        /// <summary>
        /// The count of measurements
        /// </summary>
        int IMeasurements.Count
        {
            get
            {
                return 2;
            }
        }

        /// <summary>
        /// Access to n - th measurement
        /// </summary>
        IMeasure IMeasurements.this[int n]
        {
            get
            {
                return output[n];
            }
        }

        /// <summary>
        /// Updates measurements data
        /// </summary>
        public void UpdateMeasurements()
        {
            if (IsUpdated)
            {
                return;
            }
            try
            {
                UpdateChildrenData();
                result[0, 0] = 0;
                result[1, 0] = 0;
                double x = (double)input.Parameter();
                IDerivation der = input as IDerivation;
                double d = Measure.GetDouble(der.Derivation);
                for (int i = 0; i < series[0].Count; i++)
                {
                    double om = series[0][i, 0];
                    d *= om;
                    double a = x * om;
                    double s = Math.Sin(a);
                    double c = Math.Cos(a);
                    double re = series[0][i, 1] * c + series[1][i, 1] * s;
                    double im = -series[0][i, 1] * s + series[1][i, 1] * c;
                    result[0, 0] += re;
                    result[1, 0] += im;
                    result[0, 1] += d * im;
                    result[1, 1] -= d * re;
                }
                isUpdated = true;
            }
            catch (Exception e)
            {
                PureDesktop.Throw(this, e);
            }
        }

        /// <summary>
        /// The operation that performs after arrows setting
        /// </summary>
        public void PostSetArrow()
        {
            acceptArgument();
            AcceptSeries();
        }

        /// <summary>
        /// The name of measurements source
        /// </summary>
        public string SourceName
        {
            get
            {
                INamedComponent comp = Object as INamedComponent;
                return comp.Name;
            }
        }

        /// <summary>
        /// Argument
        /// </summary>
        public string Argument
        {
            get
            {
                return argument;
            }
            set
            {
                argument = value;
                acceptArgument();
            }
        }


        public DynamicalParameter Parameter
        {
            set
            {
                if (value.Variables.Length != 1)
                {
                    throw new Exception("Fourier transform should have one parameter");
                }
                input = value['x'];
                argument = input.Name;
            }
        }

        /// <summary>
        /// Gets i - th unary
        /// </summary>
        /// <param name="i">Unary number</param>
        /// <returns>The i - th unary</returns>
        /*public IUnary GetUnary(int i)
        {
            return seriesList[i] as IUnary;
        }*/

        /// <summary>
        /// Count of unaries
        /// </summary>
        public int UnaryCount
        {
            get
            {
                return seriesList.Count;
            }
        }


        public void AcceptSeries()
        {
            if (seriesTable.Count != 2)
            {
                return;
            }
            for (int i = 0; i < 2; i++)
            {
                string sn = seriesTable[i] as string;
                foreach (Series s in seriesList)
                {
                    //INamedComponent nc = s.Object as INamedComponent;
                    if (sn.Equals(PureDesktop.GetRelativeName(this, s)))//nc.Name))
                    {
                        series[i] = s;
                        continue;
                    }
                }
            }
        }


        /// <summary>
        /// Accepts arguments
        /// </summary>
        private void acceptArgument()
        {
            foreach (IMeasurements measurements in measurementsData)
            {
                /*IAssociatedObject cont = measurements as IAssociatedObject;
                INamedComponent c = cont.Object as INamedComponent;*/
                /*			string name = DataConsumer.GetName(this, measurements);//c.Name;
                            for (int i = 0; i < measurements.Count; i++)
                            {
                                IMeasure measure = measurements[i];
                                string p = name + "." + measure.Name;
                                if (argument.Substring(4).Equals(p))
                                {
                                    input = measure;
                                    break;
                                }
                            }
                        }
                        if (argument.Equals("Time"))
                        {
                            input = DataConsumer.TimeMeasure;
                        }
                    }*/
            }
        }







        /// <summary>
        /// Gets function value
        /// </summary>
        /// <returns>The value</returns>
        private object getRe()
        {
            return result[0, 0];
        }

        /// <summary>
        /// Gets function value
        /// </summary>
        /// <returns>The value</returns>
        private object getIm()
        {
            return result[1, 0];
        }

        /// <summary>
        /// Gets function derivation
        /// </summary>
        /// <returns>The derivation</returns>
        private object getReDerivation()
        {
            return result[0, 1];
        }

        /// <summary>
        /// Gets function derivation
        /// </summary>
        /// <returns>The derivation</returns>
        private object getImDerivation()
        {
            return result[1, 1];
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Russian Federation Russian Federation
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:

1) Noncommutative geometry

http://front.math.ucdavis.edu/author/P.Ivankov

2) Literary work (Russian only)

http://zhurnal.lib.ru/editors/3/3d_m/

3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1

Comments and Discussions