Array

#region [ Static ] GetFrequency, GetChanPower, PeakSearch, SortTrace ... To Verify
        public static decimal GetFrequency(decimal center, decimal span, int nop, int index)
        {
            decimal start = center + span / 2;
            return start + span * index / (nop - 1);
        }

        public static decimal GetFrequencyIndex(decimal center, decimal span, int nop, decimal frequency)
        {
            decimal start = center + span / 2;
            return (frequency - start) * (nop - 1) / span;
        }

        public static double GetChanPower(decimal[] trace, int startindex, int stopindex)
        {
            double p = 0D;
            for (int i = startindex; i <= stopindex; i++)
            {
                p += Math.Pow(10D, (double)trace[i] / 10D);
            }

            return p < Math.Pow(10D, -17) ? -175 : 10 * Math.Log10(p);
        }

        public static double GetChanPower(decimal[] trace, decimal span, decimal bandwidth)
        {
            decimal N = bandwidth * (trace.Length - 1) / span;
            N = N / 2;
            decimal istart = (trace.Length - 1) / 2 - N;
            decimal istop = (trace.Length - 1) / 2 + N;

            return GetChanPower(trace, (int)istart, istop - (int)istop > 0 ? (int)istop + 1 : (int)istop);
        }

        public static int[] SortTrace(decimal[] trace)
        {
            int[] index = new int[trace.Length];
            for (int i=0;i<index.Length;i++)
                index[i] = i;

            decimal[] sort = (decimal[]) trace.Clone();
            Array.Sort(sort, index, 0, index.Length);//from 0 -> N
            //Array.Reverse(index);
            return index;
        }

        public static int[] GetPeakIndex(decimal[] trace, int npeaks)
        {          
            int[] peakindex = new int[npeaks];

            int[] sortindex = SortTrace(trace);
            int n=0;

            
            int candidateindex ;
            for (int i = sortindex.Length - 1; i >= 0 && n < npeaks; i--)
            {
                candidateindex = sortindex[i];//peak index, n starts from 0
                if (candidateindex == 0 && trace[candidateindex] > trace[candidateindex + 1])
                {
                    peakindex[n] = candidateindex;
                    n++;
                    continue;
                }
                else if (candidateindex == sortindex.Length - 1 && trace[candidateindex] > trace[candidateindex - 1])
                {
                    peakindex[n] = candidateindex;
                    n++;
                    continue;
                }
                else if (trace[candidateindex] > trace[candidateindex - 1] && trace[candidateindex] > trace[candidateindex + 1])
                {
                    peakindex[n] = candidateindex;
                    n++;
                    continue;
                }
            }
            return peakindex;
        }
        #endregion


#region [ Static ] GetValue (from array) 
        public static decimal GetValue(decimal[,] cal, decimal f, int col)
        {
            if (cal == null)
                return decimal.MinValue;

            int N = cal.GetLength(0);
            if (f <= cal[0, 0] || N == 1)   //最小 or N = 1
                return cal[0, col];
            
            if (f >= cal[N - 1, 0])
                return cal[N - 1, col];     //最大

            int m = 0;
            while (f >= cal[m, 0])
                m++;                //整数部分

            decimal delta = f - cal[m, 0];
            if (delta == 0)
                return cal[m, col]; //在整点上

            return cal[m, col] + (cal[m + 1, col] - cal[m, col]) * delta / (cal[m + 1, 0] - cal[m, 0]);
            
        }
        #endregion


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值