⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tools.cs

📁 心电信号处理库文件
💻 CS
📖 第 1 页 / 共 3 页
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
using System.Globalization;

namespace DSProcessing
{
    /// <summary>
    /// Low level functions, a part of DSProcessing library.
    /// author: Jan Sova
    /// mailto: twardowski@email.cz
    ///
    /// -------------------------------------------------------------------------
    ///
    /// DSProcessing - C#/C++ library of signal processing, speech processing,
    ///              and communications classes and functions
    ///
    /// Copyright (C) 2007-2008
    ///
    /// This program is free software; you can redistribute it and/or modify
    /// it under the terms of the GNU General Public License as published by
    /// the Free Software Foundation; either version 2 of the License, or
    /// (at your option) any later version.
    ///
    /// This program is distributed in the hope that it will be useful,
    /// but WITHOUT ANY WARRANTY; without even the implied warranty of
    /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    /// GNU General Public License for more details.
    ///
    /// You should have received a copy of the GNU General Public License
    /// along with this program; if not, write to the Free Software
    /// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    ///
    ///-------------------------------------------------------------------------
    /// </summary>
    public class Tools
    {
        /// <summary>
        /// From 2D filed return 1D filed -- specificed column from 2D field.
        /// </summary>
        /// <param name="data">2D field</param>
        /// <param name="column">column, data from this column will be copied into return 1D field</param>
        /// <returns></returns>
        public static double[] ToDoubleVector(double[,] data, int column)
        {
            double[] rdata = new double[data.GetUpperBound(1)];
            for (int i = 0; i < rdata.Length; i++)
                rdata[i] = data[column, i];

            return rdata;
        }

        /// <summary>
        /// Mean value of double 1D field.
        /// </summary>
        /// <param name="data">intput 1D field</param>
        /// <returns>mean value</returns>
        public static double mean(double[] data)
        {
            double returnValue = 0;
            
            if(data == null || data.Length == 0)
                return 0;

            for (int i = 0; i < data.Length; i++)
                returnValue += data[i];

            returnValue /= data.Length;
            
            return returnValue;
        }

        /// <summary>
        /// Mean value of double 1D field.
        /// </summary>
        /// <param name="data">intput 1D field</param>
        /// <returns>mean value</returns>
        public static int mean(int[] data)
        {
            int returnValue = 0;

            if (data == null || data.Length == 0)
                return 0;

            for (int i = 0; i < data.Length; i++)
                returnValue += data[i];

            returnValue = (int)(returnValue/data.Length);

            return returnValue;
        }

        /// <summary>
        /// Convolution function. 
        /// </summary>
        /// <param name="x">signal</param>
        /// <param name="h">system</param>
        /// <returns></returns>
        /// pozn. x a h must be the same length, chech this if need be h fill-up with zeros
        public static double[] convolution(double[] x, double[] h)
        {
            double[] returnData = new double[x.Length];
            
            for (int i = 0; i < returnData.Length; i++)
                returnData[i] = 0;

            double[] newh = new double[x.Length];
            
            //fill-up h with zeros 
            for (int i = 0; i < newh.Length; i++)
                newh[i] = 0;

            for (int i = 0; i < h.Length; i++)
                newh[i] = h[i];


            for (int n = 0; n < x.Length; n++)
                for (int m = 0; m <= n; m++)
                    returnData[n] += x[m] * newh[n - m];
            
            return returnData; 
        }

        /// <summary>
        /// Finish or let out? Not ready!
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static double[] timereverse(double[] data)
        {
            double swap;
            double[] returnValue = new double[data.Length];

            for (int i = 0; i < data.Length; i++)
            {
                swap = data[i];
                returnValue[i] = data[data.Length - 1 - i];
                //returnValue[data.Length - 1] = data[i]; 
            }

            return returnValue;
        }

        /// <summary>
        /// Finish or let out? Not ready!
        /// </summary>
        /// <param name="data"></param>
        public static void reverse(double[] data)
        {
            //double swap;
            for (int i = 0; i < data.Length; i++)
            {
                data[i] *= 1;
            }
        }

        /// <summary>
        /// Print double filed into console.
        /// </summary>
        /// <param name="data">Data to write.</param>
        public static void print(double[] data)
        {
            for (int i = 0; i < data.Length; i++)
                Console.WriteLine(i + " :" + data[i]);
        }


        /// <summary>
        /// Return sum of each values form input field. 
        /// Function known from Matlab.
        /// </summary>
        /// <param name="data">input field</param>
        /// <returns>cumsum value</returns>
        public static double cumsum(double[] data)
        {
            double returnSum = 0;

            for (int i = 0; i < data.Length; i++)
                returnSum += data[i];

            return returnSum;

        }

        /// <summary>
        /// Return double field with lenght given by len filled with zeros.
        /// </summary>
        /// <param name="len">length of output field</param>
        /// <returns></returns>
        public static double[] ones(int len)
        {
            double[] returnValue = new double[len];
            for(int i = 0; i < len; i++)
                returnValue[i] = 1;

            return returnValue;
        }

        /// <summary>
        /// Return double field with lenght given by len with values
        /// 1, 2, 3, 4, 5, 6... trough len
        /// </summary>
        /// <param name="len">length of output field</param>
        /// <returns></returns>
        public static double[] linspace(int len)
        {
            double[] linspace = new double[len];
            for (int i = 0; i < linspace.Length; i++)
                linspace[i] = i;

            return linspace;
        }

        /// <summary>
        /// Get 1D double filed and return 1D field with two values: [max value, location of max value].
        /// </summary>
        /// <param name="data">input double field</param>
        /// <returns>[max value, location of max value]</returns>
        public static double[] max(double[] data)
        {
            double[] returnValue = new double[2];
            double maximum = data[0];
            int poloha = 0;

            for(int i = 0; i < data.Length; i++)
            {
                if(maximum < data[i])
                {
                    maximum = data[i];
                    poloha = i;
                }
            }

            returnValue[0] = maximum;
            returnValue[1] = poloha;

            return returnValue;
        }

        /// <summary>
        /// Get 1D int filed and return 1D field with two values: [max value, location of max value].
        /// </summary>
        /// <param name="data">input int field</param>
        /// <returns>[max value, location of max value]</returns>
        public static int[] max(int[] data)
        {
            int[] returnValue = new int[2];
            int maximum = data[0];
            int poloha = 0;

            for (int i = 0; i < data.Length; i++)
            {
                if (maximum < data[i])
                {
                    maximum = data[i];
                    poloha = i;
                }
            }

            returnValue[0] = maximum;
            returnValue[1] = poloha;

            return returnValue;
        }

        /// <summary>
        /// Get 1D double filed and return 1D field with two values: [min value, location of min value].
        /// </summary>
        /// <param name="data">input double field</param>
        /// <returns>[min value, location of min value]</returns>
        public static double[] min(double[] data)
        {
            double[] returnValue = new double[2];
            double minimum = data[0];
            int poloha = 0;

            for (int i = 0; i < data.Length; i++)
            {
                if (minimum > data[i])
                {
                    minimum = data[i];
                    poloha = i;
                }
            }

            returnValue[0] = minimum;
            returnValue[1] = poloha;

            return returnValue;
        }

        /// <summary>
        /// Get 1D int filed and return 1D field with two values: [min value, location of min value].
        /// </summary>
        /// <param name="data">input int field</param>
        /// <returns>[min value, location of min value]</returns>
        public static int[] min(int[] data)
        {
            int[] returnValue = new int[2];
            int minimum = data[0];
            int poloha = 0;

            for (int i = 0; i < data.Length; i++)
            {
                if (minimum > data[i])
                {
                    minimum = data[i];
                    poloha = i;
                }
            }

            returnValue[0] = minimum;
            returnValue[1] = poloha;

            return returnValue;
        }

        /// <summary>
        /// Return min value of double field.
        /// </summary>
        /// <param name="data">input double data field</param>
        /// <returns>min value</returns>
        public static double minValue(double[] data)
        {
            double[] returnValue = min(data);
            return returnValue[0];
        }

        /// <summary>
        /// Return max value of double field.
        /// </summary>
        /// <param name="data">input double data field</param>
        /// <returns>max value</returns>
        public static double maxValue(double[] data)
        {
            double[] returnValue = max(data);
            return returnValue[0];
        }

        /// <summary>
        /// Return min value of int field.
        /// </summary>
        /// <param name="data">input int data field</param>
        /// <returns>min value</returns>
        public static int minValue(int[] data)
        {
            int[] returnValue = min(data);
            return returnValue[0];
        }

        /// <summary>
        /// Return max value of int field.
        /// </summary>
        /// <param name="data">input int data field</param>
        /// <returns>max value</returns>
        public static int maxValue(int[] data)
        {
            int[] returnValue = max(data);
            return returnValue[0];
        }


        /// <summary>
        /// Function known from Matlab
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        /// <remarks>Input filed is at the end fill-up with zeros, to have the same length as
        /// intput filed.</remarks>
        public static double[] diff(double[] data)
        {
            double[] returnData = new double[data.Length];
            for (int i = 0; i < data.Length-1; i++)
                returnData[i] = data[i + 1] - data[i];
            
            returnData[data.Length - 1] = 0;

            return returnData;
        }

        /// <summary>
        /// Function known from Matlab.
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        /// <remarks>Input filed is at the end fill-up with zeros, to have the same length as
        /// intput filed.</remarks>
        public static double[] diff(int[] data)
        {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -