private void Form1_Load(object sender, EventArgs e) { // Строим сплайн Spline.BuildSpline(x, y, n); // Формируем массив для вывода // Шаг 0.01, интервал [0; 2*PI], 6.28 / 0.01 = 628 точек для вывода tmpX = 0; for (int i = 0; i <= 628; i++) { X[i] = tmpX; Y[i] = Spline.Func(tmpX); tmpX += 0.01; } }
private float spline2(float x, List <float> vs, int scale) { if (cb == null) { cb = new CubicSpline(); var xs = new double[vs.Count]; var ys = new double[vs.Count]; for (int i = 0; i < vs.Count; i++) { xs[i] = i * scale; ys[i] = vs[i]; } cb.BuildSpline(xs, ys, xs.Length); } return((float)cb.Func(x)); }
static double[][] emd(double[] signal, double[][] mods, int calls, int countOfMods) { if (originalSignal == null) { originalSignal = signal; } //int calls = 0; double[] mod = new double[signal.Length]; // while (calls<=21) { double[] localMaximums; double[] localMinimums; double[] indexesOfLocalMaximums; double[] indexesOfLocalMinimums; FindExtremums(signal, out localMinimums, out localMaximums, out indexesOfLocalMinimums, out indexesOfLocalMaximums); if (calls == 100) { GC.Collect(); } if (calls > 150 || indexesOfLocalMaximums.Length <= 1 || indexesOfLocalMinimums.Length <= 1) { mods[countOfMods] = originalSignal; return(mods); } double[] interpMinimums = new double[signal.Length]; double[] interpMaximums = new double[signal.Length]; Stopwatch sss = new Stopwatch(); sss.Start(); // ToFile(localMinimums); // ToFile(localMaximums); CubicSpline interp = new CubicSpline(); interp.BuildSpline(indexesOfLocalMinimums, localMinimums, indexesOfLocalMinimums.Length); int min = (int)(indexesOfLocalMinimums[0] < indexesOfLocalMaximums[0] ? indexesOfLocalMaximums[0] : indexesOfLocalMinimums[0]); int max = (int)(indexesOfLocalMinimums[indexesOfLocalMinimums.Length - 1] > indexesOfLocalMaximums[indexesOfLocalMaximums.Length - 1] ? indexesOfLocalMaximums[indexesOfLocalMaximums.Length - 1] : indexesOfLocalMinimums[indexesOfLocalMinimums.Length - 1]); double[] difference = new double[max - min]; for (int interpolatedX = min; interpolatedX < max; interpolatedX++) { interpMinimums[interpolatedX] = interp.Interpolate(interpolatedX); } // ToFile(interpMinimums); interp.BuildSpline(indexesOfLocalMaximums, localMaximums, indexesOfLocalMaximums.Length); for (int interpolatedX = min; interpolatedX < max; interpolatedX++) { interpMaximums[interpolatedX] = interp.Interpolate(interpolatedX); } // ToFile(interpMaximums); sss.Stop(); for (int i = min; i < max; i++) { difference[i - min] = signal[i] - (interpMaximums[i] + interpMinimums[i]) / 2; } int countOfNulls = 0; double mean = 0; for (int i = 0; i < difference.Length - 1; i++) { if (difference[i] * difference[i + 1] < 0) { countOfNulls++; } mean += difference[i]; } mean /= difference.Length; FindExtremums(difference, out localMinimums, out localMaximums, out indexesOfLocalMinimums, out indexesOfLocalMaximums); if (Math.Abs(mean) <= 0.009 && Math.Abs(indexesOfLocalMaximums.Length + indexesOfLocalMinimums.Length - countOfNulls) <= 1) { mods[countOfMods] = difference; countOfMods++; var backupLink = originalSignal; originalSignal = new double[max - min]; for (int i = min; i < max; i++) { originalSignal[i - min] = backupLink[i] - difference[i - min]; } calls++; /* difference = null; * localMinimums = null; * localMaximums = null; * indexesOfLocalMinimums = null; * indexesOfLocalMaximums = null; * interpMaximums = null; * interpMinimums = null;*/ return(emd(originalSignal, mods, calls, countOfMods)); } calls++; return(emd(difference, mods, calls, countOfMods)); } }
static void Main(string[] args) { CubicSpline CS = new CubicSpline(); double[] x = new double[21]; double[] y = { -1.24, -1.17, -1.08, -0.96, -0.84, -0.79, -0.8, -0.9, -1.1, -1.21, -1.02, -1.28, -1.32, -1.34, -1.36, -1.37, -1.37, -1.36, -1.35, -1.33, -1.30 }; x[0] = (-2 * Math.PI) / 21; Console.WriteLine(); Console.WriteLine("\tx[0] = {0}", x[0]); for (int k = 1; k < y.Length; k++) { x[k] = (2 * Math.PI * (k - 1)) / 21; Console.WriteLine("\tx[{0}] = {1}", k, x[k]); } CS.BuildSpline(x, y, 21); Console.WriteLine(); Console.Write("\t\tx"); Console.Write("\t\t a"); Console.Write("\t b"); Console.Write("\t c"); Console.Write("\t d\n"); int i = 0; foreach (SplineTuple v in CS.splines) { if (i > 0 && i < 20) { Console.Write("\t{0:f3} < x <{1,6:f3}\t", (x[i] - (x[i + 1] - x[i]) / 2.0), (x[i] + (x[i + 1] - x[i]) / 2.0)); //Console.WriteLine(); //double x1 = (x[i] - (x[i + 1] - x[i]) / 2.0); //double x2 = (x[i] + (x[i + 1] - x[i]) / 2.0); //double h = ((x2) - (x1))/10.0; //for (int m = 0; m < 10; m++) //{ // x1 += Math.Abs(h); // Console.WriteLine("{0}", x1); //} } if (i == 0) { Console.Write("\t{0:f3} < x < {1,6:f3}\t", x[i], (x[i] + (x[i + 1] - x[i]) / 2.0)); //Console.WriteLine(); //double x1 = x[i]; //double x2 = (x[i] + (x[i + 1] - x[i]) / 2.0); //double h = (x2 - x1) / 10.0; //for (int j = 0; j < 10; j++) //{ // x1 += Math.Abs(h); // Console.WriteLine("{0}", x1); //} } if (i == 20) { Console.Write("\t{0:f3} < t < {1,6:f3}\t", (x[i - 1] + (x[i] - x[i - 1]) / 2.0), x[i]); } Console.Write("{0,7:f3}\t", v.a); Console.Write("{0,7:f3}\t", v.b); Console.Write("{0,7:f3}\t", v.c); Console.Write("{0,7:f3}\t\n", v.d); Console.WriteLine(); i++; } }