Advertisement
Golden_Rus

Fck math

Jun 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1.  private  int calcLenth(int length)//округление длинны массива до ближайшей степени 2ки.
  2.         {
  3.             int bit = 2;
  4.             for (int i = 0; i < 30; i++)
  5.             {
  6.                 bit *= 2;
  7.                 if ((bit == length) || (bit > length)) return bit;
  8.             }
  9.             return 0;
  10.         }
  11.  
  12.         private  double midleOfFrame(int pos, int framesize)
  13.         {
  14.             double res = 0;
  15.             for (int i = pos; i <= pos + framesize; i++)
  16.             {
  17.                 res += Math.Abs(Math.Sqrt(output[i].Real * output[i].Real + output[i].Imaginary * output[i].Imaginary));
  18.             }
  19.             res /= framesize;
  20.             return res;
  21.         }
  22.         public  double midleOfFrame(Complex[] input, int pos, int framesize)
  23.         {
  24.             double res = 0;
  25.             for (int i = pos; i <= pos + framesize; i++)
  26.             {
  27.                 res += Math.Sqrt(input[i].Real * input[i].Real + input[i].Imaginary * input[i].Imaginary);
  28.             }
  29.             res /= framesize;
  30.             return res;
  31.         }
  32.  
  33.         public  double maxOfFrame(Complex[] input, int pos, int framesize)
  34.         {
  35.             double res = 0;
  36.             for (int i = pos; i <= pos + framesize; i++)
  37.             {
  38.                 res = Math.Sqrt(input[i].Real * input[i].Real + input[i].Imaginary * input[i].Imaginary) > res ? Math.Sqrt(input[i].Real * input[i].Real + input[i].Imaginary * input[i].Imaginary) : res;
  39.             }
  40.             return res;
  41.         }
  42.  
  43.         private double maxOfArray(Double[] input)
  44.         {
  45.             double max = input[0];
  46.             for (int i = 1; i < input.Count(); i++)
  47.                 max = input[i] > max ? input[i] : max;
  48.             return max;
  49.         }
  50.  
  51.         public double[] normalize(double[] input)
  52.         {
  53.             Double[] tmp = new Double[input.Count()];
  54.             double k = 100 / maxOfArray(input);
  55.             for (int i = 0; i < input.Count(); i++)
  56.                 tmp[i] = input[i] * k;
  57.             return tmp;
  58.         }
  59.  
  60.         private  void AddDelimed(int start, int end)
  61.         {
  62.             int size = end - start;
  63.             Complex[] tmp = new Complex[calcLenth(size)];
  64.             for(int i = 0; i < size; i++)
  65.             {
  66.                 tmp[i] = output[i + start];
  67.             }
  68.             delimed.Add(tmp, start);
  69.         }
  70.  
  71.         private  bool isSil(int pos)
  72.         {
  73.             const int framesize = 100;
  74.             const double level = 0.01;
  75.             return midleOfFrame(pos, framesize) < level;
  76.         }
  77.  
  78.         private void delimBySil()
  79.         {
  80.             const int framesize = 500;
  81.             int last = 0;
  82.             bool mark = false;
  83.  
  84.             for (int i = 0; i < output.Length - framesize; i += framesize)
  85.             {
  86.                 if(isSil(i))
  87.                 {
  88.                     if(mark)
  89.                     {
  90.                         AddDelimed(last, i);
  91.                         mark = false;
  92.                     }
  93.                 }
  94.                 else
  95.                 {
  96.                     if(!mark)
  97.                     {
  98.                         last = i;
  99.                     }
  100.                     mark = true;
  101.                 }
  102.             }
  103.             if(mark)
  104.             {
  105.                 AddDelimed(last, output.Length - framesize);
  106.             }
  107.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement