/////////////////////////////////////////////////////////////////////////////////////////////
        // Original Simple Fluid Solver Code is by Jos Stam
        // 	  http://www.dgp.utoronto.ca/people/stam/reality/Research/pub.html
        //
        //    Using AForge.Math.FourierTransform for FFT
        //
        void SolveSFSW(int n, float[] wu, float[] wv, float[] fu, float[] fv, int rsize, float visc, float dt)
        {
            for (int i = 0; i < n * n; i++)
            {
                wu [i] += dt * fu [i];
                wv [i] += dt * fv [i];
                m_work_u [i] = wu [i] / rsize;
                m_work_v [i] = wv [i] / rsize;
            }

            for (int j = 0; j < n; j++)
            {
                int jj = j * n;
                for (int i = 0; i < n; i++)
                {
                    float x = i - dt * m_work_u [i + jj] * n;
                    float y = j - dt * m_work_v [i + jj] * n;
                    //
                    int xi = (int)Math.Floor (x);
                    int yi = (int)Math.Floor (y);
                    float s = (x - xi);
                    float t = (y - yi);

                    int i0 = (n + (xi % n)) % n;
                    int i1 = (i0 + 1) % n;
                    int j0 = (n + (yi % n)) % n;
                    int j1 = (j0 + 1) % n;

                    wu [i + jj] = (1.0f - s) * ((1.0f - t) * m_work_u [i0 + n * j0] + t * m_work_u [i0 + n * j1])
                                    + s * ((1.0f - t) * m_work_u [i1 + n * j0] + t * m_work_u [i1 + n * j1]);
                    wv [i + jj] = (1.0f - s) * ((1.0f - t) * m_work_v [i0 + n * j0] + t * m_work_v [i0 + n * j1])
                                    + s * ((1.0f - t) * m_work_v [i1 + n * j0] + t * m_work_v [i1 + n * j1]);
                }
            }

            for (int j = 0; j < n; j++)
            {
                int jj = j * n;
                for (int i = 0; i < n; i++)
                {
                    m_comp_u [i, j] = new AForge.Math.Complex (wu [i + jj], 0.0);
                    m_comp_v [i, j] = new AForge.Math.Complex (wv [i + jj], 0.0);
                }
            }

            AForge.Math.FourierTransform.FFT2 (m_comp_u, AForge.Math.FourierTransform.Direction.Forward);
            AForge.Math.FourierTransform.FFT2 (m_comp_v, AForge.Math.FourierTransform.Direction.Forward);

            visc *= dt;
            for (int j = 0; j < n; j++)
            {
                float y = (float)(j <= n / 2 ? j : j - n);
                float yy = y * y;
                //
                for (int i = 0; i < n / 2 + 1; i++)
                {
                    float xx = i * i;
                    float r_sq = xx + yy;
                    if (r_sq != 0.0f)
                    {
                        float fac = (float)Math.Exp (-r_sq * visc);
                        float xy = i * y;

                        AForge.Math.Complex ux = m_comp_u [i, j];
                        AForge.Math.Complex vx = m_comp_v [i, j];
                        m_comp_u [i, j] = fac * ((1.0f - xx / r_sq) * ux - xy / r_sq * vx);
                        m_comp_v [i, j] = fac * (-xy / r_sq * ux + (1.0f - yy / r_sq) * vx);
                    }
                }
            }

            AForge.Math.FourierTransform.FFT2 (m_comp_u, AForge.Math.FourierTransform.Direction.Backward);
            AForge.Math.FourierTransform.FFT2 (m_comp_v, AForge.Math.FourierTransform.Direction.Backward);

            float nrml = 1.0f;//(n*n);
            for (int j = 0; j < n; j++)
            {
                int jj = j * n;
                for (int i = 0; i < n; i++)
                {
                    wu [i + jj] = (float)(nrml * m_comp_u [i, j].Re) * rsize;
                    wv [i + jj] = (float)(nrml * m_comp_v [i, j].Re) * rsize;
                }
            }

            return;
        }
Ejemplo n.º 2
0
        public static int Main(string[] args)
        {
            //IList<double> R = getRandomVector(500, 100);
            //Читаем файлик OHLCV
            //string path = "z:\\YandexDisk\\Data\\GAZP_test_1h.txt";
            //TS.DataSource.BarList BarList = new TS.DataSource.BarList(path);

            HsaClassLibrary.Helpers.HhtCreator emdcr = new HsaClassLibrary.Helpers.HhtCreator();
            //EmdClassLibrary.EmdAbstractClass emd = emdcr.FactoryMethod(EmdClassLibrary.InterpolationEnum.ЛинейнаяИнтерполяция, EmdClassLibrary.StopCriterionEnum.КоличествоИтераций, EmdClassLibrary.StopSiftCriterionEnum.КоличествоИтераций);
            //EmdClassLibrary.EmdAbstractClass emd = emdcr.FactoryMethod(EmdClassLibrary.InterpolationEnum.ЛинейнаяИнтерполяция, EmdClassLibrary.StopCriterionEnum.КоличествоИтераций, EmdClassLibrary.StopSiftCriterionEnum.ДостигнутаТочностьОтсеивания);
            HsaClassLibrary.Transform.HilbertSpectrum Hsa = emdcr.HsaFactoryMethod(HsaClassLibrary.Transform.EnumHilbertTransform.HilbertTransform);

            //Выбираем для анализа
            //IList<double> R = HsaClassLibrary.Helpers.MathHelper.getCosAM(256, 1, 2.0 * Math.PI / 32.0, 2.0 * Math.PI / 256.0);
            //y <- sin(2*pi*n*Tn/(T*N))
            IList<double> R = HsaClassLibrary.Helpers.MathHelper.getSin(256, 1, 2.0 * Math.PI / 256.0);

            //Данные для вывода в файл
            IList<IList<double>> data = new List<IList<double>>();
            data.Add(R);
            Hsa.Source = R;
            /*
            //заменяем коэффичиента преобразования
            //Hsa.HilbertTransform = new EmdClassLibrary.HilbertTransform1();
            Hsa.HS(true, false, false, false);
            //сохраняем для вывода
            data.Add(Hsa.Xi);
            //Hsa.Data = Hsa.Xi;
            //Hsa.HS(true, false, false, false);
            //data.Add(Hsa.Xi);

            //заменяем коэффичиента преобразования
            Hsa.HilbertTransform = new EmdClassLibrary.Transform.HilbertTransform1();
            Hsa.HS(true, false, false, false);
            //сохраняем для вывода
            data.Add(Hsa.Xi);

            //заменяем коэффичиента преобразования
            Hsa.HilbertTransform = new EmdClassLibrary.Transform.HilbertTransform2();
            Hsa.HS(true, false, false, false);
            //сохраняем для вывода
            data.Add(Hsa.Xi);

            //заменяем коэффичиента преобразования
            Hsa.HilbertTransform = new EmdClassLibrary.Transform.HilbertTransform3();
            Hsa.HS(true, false, false, false);
            //сохраняем для вывода
            data.Add(Hsa.Xi);

            //заменяем коэффичиента преобразования
            Hsa.HilbertTransform = new EmdClassLibrary.Transform.HilbertTransform4();
            Hsa.HS(true, false, false, false);
            //сохраняем для вывода
            data.Add(Hsa.Xi);

            //заменяем коэффичиента преобразования
            Hsa.HilbertTransform = new EmdClassLibrary.Transform.HilbertTransform5();
            Hsa.HS(true, false, false, false);
            //сохраняем для вывода
            data.Add(Hsa.Xi);
            /*
            //заменяем коэффичиента преобразования
            Hsa.HilbertTransform = new EmdClassLibrary.Transform.HilbertTransform6();
            Hsa.HS(true, false, false, false);
            //сохраняем для вывода
            data.Add(Hsa.Xi);

            //заменяем коэффичиента преобразования
            Hsa.HilbertTransform = new EmdClassLibrary.Transform.HilbertTransform7();
            Hsa.HS(true, false, false, false);
            //сохраняем для вывода
            data.Add(Hsa.Xi);
            */

            //0
            double[] HT0 = new double[R.Count];
            R.CopyTo(HT0, 0);

            Accord.Math.HilbertTransform.FHT(HT0, AForge.Math.FourierTransform.Direction.Forward);
            data.Add(HT0);

            //1
            //double[] dHT = new double[R.Count];
            double[] HT1 = new double[R.Count];
            R.CopyTo(HT1, 0);

            Accord.Math.HilbertTransform.FHT2(HT1, AForge.Math.FourierTransform.Direction.Forward);
            data.Add(HT1);

            // 2
            // Правильно но странно
            // инвертирован вызов преобразований фурье
            double[] HT2r = new double[R.Count];
            double[] HT2i = new double[R.Count];

            AForge.Math.Complex[] HT2 = new AForge.Math.Complex[R.Count];
            for (int i = 0; i < R.Count; i++)
            {
                HT2[i].Re = R[i];
                HT2[i].Im = 0;
            }
            Accord.Math.HilbertTransform.FHT(HT2, AForge.Math.FourierTransform.Direction.Forward);

            for (int i = 0; i < R.Count; i++)
            {
                HT2r[i] = HT2[i].Re;
                HT2i[i] = HT2[i].Im;
            }
            data.Add(HT2r);
            data.Add(HT2i);

            //3
            /*
            ///  QW модифицировал Accord.Math.HilbertTransform.FHT2
            ///  неправильно - инвертирован знак мнимой части
            double[] HT3r = new double[R.Count];
            double[] HT3i = new double[R.Count];

            //AForge.Math.Complex[] cHT = new AForge.Math.Complex[R.Count];
            AForge.Math.Complex[] HT3 = new AForge.Math.Complex[R.Count];
            for (int i = 0; i < R.Count; i++)
            {
                HT3[i].Re = R[i];
                HT3[i].Im = 0;
            }
            Accord.Math.HilbertTransform.FHT2(HT3, AForge.Math.FourierTransform.Direction.Forward);

            //double[] HTr = new double[R.Count];
            //double[] HTi = new double[R.Count];
            HT3r = new double[R.Count];
            HT3i = new double[R.Count];
            for (int i = 0; i < R.Count; i++)
            {
                HT3r[i] = HT3[i].Re;
                HT3i[i] = HT3[i].Im;
            }
            data.Add(HT3r);
            data.Add(HT3i);
            */
            //4 не совсем Правильно - постоянная
            double[] X4 = new double[R.Count];
            IList<double> HT4r = new double[R.Count];
            IList<double> HT4i = new double[R.Count];
            R.CopyTo(X4, 0);

            //HsaClassLibrary.Transform.HilbertTransformFFT FHT = new HsaClassLibrary.Transform.HilbertTransformFFT();
            //HT4i = FHT.(X4, out HT4r);
            HsaClassLibrary.Transform.HilbertTransform.HTFFT_alglib(X4, out HT4r, out HT4i);
            //сохраняем для вывода
            data.Add(HT4r);
            data.Add(HT4i);

            //--
            //4 Правильно
            double[] X5 = new double[R.Count];
            IList<double> HT5r = new double[R.Count];
            IList<double> HT5i = new double[R.Count];
            R.CopyTo(X5, 0);

            IList<Complex> HT5 = HsaClassLibrary.Transform.HilbertTransform.HTFFT(X5);
            HsaClassLibrary.Transform.TransformHelper.convert(HT5, out HT5r, out HT5i);
            //сохраняем для вывода
            data.Add(HT5r);
            data.Add(HT5i);
            //--

            //--
            //4 Правильно
            double[] X6 = new double[R.Count];
            IList<double> HT6r = new double[R.Count];
            IList<double> HT6i = new double[R.Count];
            R.CopyTo(X6, 0);

            IList<Complex> HT6 = HsaClassLibrary.Transform.HilbertTransform.HTFFT_alglib(X6);
            HsaClassLibrary.Transform.TransformHelper.convert(HT6, out HT6r, out HT6i);
            //сохраняем для вывода
            data.Add(HT6r);
            data.Add(HT6i);
            //--

            System.Console.WriteLine("HilbertTransform выполнена.");
            System.Console.WriteLine("Console.ReadKey();");
            Console.ReadKey();
            HsaClassLibrary.Helpers.ReadWriteHelper emdWriter = new HsaClassLibrary.Helpers.ReadWriteHelper();
            emdWriter.WriteCSV(data, "D:\\hsa7.csv");
            System.Console.WriteLine("Файл csv создан.\nD:\\hsa7.csv");
            System.Console.WriteLine("Console.ReadKey();");
            Console.ReadKey();
            return 0;
        }
Ejemplo n.º 3
0
        public static int Main(string[] args)
        {
            //IList<double> R = getRandomVector(500, 100);
            //Читаем файлик OHLCV
            //string path = "z:\\YandexDisk\\Data\\GAZP_test_1h.txt";
            //TS.DataSource.BarList BarList = new TS.DataSource.BarList(path);

            HsaClassLibrary.Helpers.HhtCreator emdcr = new HsaClassLibrary.Helpers.HhtCreator();
            //EmdClassLibrary.EmdAbstractClass emd = emdcr.FactoryMethod(EmdClassLibrary.InterpolationEnum.ЛинейнаяИнтерполяция, EmdClassLibrary.StopCriterionEnum.КоличествоИтераций, EmdClassLibrary.StopSiftCriterionEnum.КоличествоИтераций);
            //EmdClassLibrary.EmdAbstractClass emd = emdcr.FactoryMethod(EmdClassLibrary.InterpolationEnum.ЛинейнаяИнтерполяция, EmdClassLibrary.StopCriterionEnum.КоличествоИтераций, EmdClassLibrary.StopSiftCriterionEnum.ДостигнутаТочностьОтсеивания);
            HsaClassLibrary.Transform.HilbertSpectrum Hsa = emdcr.HsaFactoryMethod(HsaClassLibrary.Transform.EnumHilbertTransform.HilbertTransform);

            //Выбираем для анализа
            IList<double> R = HsaClassLibrary.Helpers.MathHelper.getCos(256, 1, 2.0 * Math.PI / 32.0);
            //IList<double> R = HsaClassLibrary.Helpers.MathHelper.getCosAM(256, 1, 2.0 * Math.PI / 32.0, 2.0 * Math.PI / 256.0);
            //Данные для вывода в файл
            IList<IList<double>> data = new List<IList<double>>();
            data.Add(R);
            Hsa.Source = R;

            //1 ft
            double[] FT1r = new double[R.Count];
            double[] FT1i = new double[R.Count];

            AForge.Math.Complex[] FT1f = new AForge.Math.Complex[R.Count];
            for (int i = 0; i < R.Count; i++)
            {
                FT1f[i].Re = R[i];
                FT1f[i].Im = 0;
            }
            AForge.Math.FourierTransform.DFT(FT1f, AForge.Math.FourierTransform.Direction.Forward);
            for (int i = 0; i < R.Count; i++)
            {
                FT1r[i] = FT1f[i].Re;
                FT1i[i] = FT1f[i].Im;
            }
            data.Add(FT1r);
            data.Add(FT1i);
            //2 ft
            double[] FT2r = new double[R.Count];
            double[] FT2i = new double[R.Count];

            AForge.Math.Complex[] FT2f = new AForge.Math.Complex[R.Count];
            for (int i = 0; i < R.Count; i++)
            {
                FT2f[i].Re = R[i];
                FT2f[i].Im = 0;
            }
            AForge.Math.FourierTransform.DFT(FT2f, AForge.Math.FourierTransform.Direction.Backward);
            for (int i = 0; i < R.Count; i++)
            {
                FT2r[i] = FT2f[i].Re;
                FT2i[i] = FT2f[i].Im;
            }
            data.Add(FT2r);
            data.Add(FT2i);

            //3 ft
            double[] FT3r = new double[R.Count];
            double[] FT3i = new double[R.Count];

            AForge.Math.Complex[] FT3f = new AForge.Math.Complex[R.Count];
            for (int i = 0; i < R.Count; i++)
            {
                FT3f[i].Re = R[i];
                FT3f[i].Im = 0;
            }
            AForge.Math.FourierTransform.FFT(FT3f, AForge.Math.FourierTransform.Direction.Forward);
            for (int i = 0; i < R.Count; i++)
            {
                FT3r[i] = FT3f[i].Re;
                FT3i[i] = FT3f[i].Im;
            }
            data.Add(FT1r);
            data.Add(FT1i);

            //4 ft
            double[] FT4r = new double[R.Count];
            double[] FT4i = new double[R.Count];

            AForge.Math.Complex[] FT4f = new AForge.Math.Complex[R.Count];
            for (int i = 0; i < R.Count; i++)
            {
                FT4f[i].Re = R[i];
                FT4f[i].Im = 0;
            }
            AForge.Math.FourierTransform.FFT(FT4f, AForge.Math.FourierTransform.Direction.Backward);
            for (int i = 0; i < R.Count; i++)
            {
                FT4r[i] = FT4f[i].Re;
                FT4i[i] = FT4f[i].Im;
            }
            data.Add(FT4r);
            data.Add(FT4i);

            //5
            double[] FT5r = new double[R.Count];
            double[] FT5i = new double[R.Count];

            double[] FT5 = new double[R.Count];
            R.CopyTo(FT5, 0);

            alglib.complex[] FT5f;
            alglib.fftr1d(FT5, out FT5f);
            for (int i = 0; i < R.Count; i++)
            {
                FT5r[i] = FT5f[i].x;
                FT5i[i] = FT5f[i].y;
            }
            data.Add(FT5r);
            data.Add(FT5i);

            //6
            double[] FT6f = new double[R.Count];

            alglib.complex[] FT6 = new alglib.complex[R.Count];
            for (int i = 0; i < R.Count; i++)
            {
                FT6[i].x = R[i];
                FT6[i].y = 0;
            }

            alglib.fftr1dinv(FT6, out FT6f);
            data.Add(FT6f);

            //7
            /*
            double[] FT7r = new double[R.Count];
            double[] FT7i = new double[R.Count];

            List<double> FT7 = new List<double>(R);
            List<Complex> FT7f;
            FT7f = HsaClassLibrary.Transform.FourierTransform.ft(FT7);
            for (int i = 0; i < R.Count; i++)
            {
                FT7r[i] = FT7f[i].Real;
                FT7i[i] = FT7f[i].Imaginary;
            }
            data.Add(FT7r);
            data.Add(FT7i);

            data.Add(R);
             */
            //8
            List<double> FT8 = new List<double>(R);
            List<Complex> FT8f;

            FT8f = HsaClassLibrary.Transform.FourierTransform.fft(FT8).ToList();

            IList<double> FT8r = new double[R.Count];
            IList<double> FT8i = new double[R.Count];
            HsaClassLibrary.Transform.TransformHelper.convert(FT8f, out FT8r, out FT8i);
            data.Add(FT8r);
            data.Add(FT8i);

            //9
            /*
            double[] FT9r = new double[R.Count];
            double[] FT9i = new double[R.Count];

            List<Complex> FT9 = new List<Complex>(R.Count);
            for (int i = 0; i < R.Count; i++)
            {
                FT9.Add(new Complex(R[i],0));
            }
            List<Complex> FT9f;

            FT9f = HsaClassLibrary.Transform.FourierTransform.ifft(FT9);
            for (int i = 0; i < R.Count; i++)
            {
                FT9r[i] = FT9f[i].Real;
                FT9i[i] = FT9f[i].Imaginary;
            }
            data.Add(FT9r);
            data.Add(FT9i);
            */
            //10
            List<Complex> FT10 = new List<Complex>(R.Count);
            for (int i = 0; i < R.Count; i++)
            {
                FT10.Add(new Complex(R[i], 0));
            }

            IList<Complex> FT10f;
            FT10f = HsaClassLibrary.Transform.FourierTransform.ifft(FT10);

            IList<double> FT10r = new double[R.Count];
            IList<double> FT10i = new double[R.Count];
            HsaClassLibrary.Transform.TransformHelper.convert(FT10f, out FT10r, out FT10i);
            data.Add(FT10r);
            data.Add(FT10i);

            //AForge.Math.FourierTransform.FFT

            //System.Console.WriteLine("FourierTransform.FFT выполнена.");
            //System.Console.WriteLine("Console.ReadKey();");
            //Console.ReadKey();
            HsaClassLibrary.Helpers.ReadWriteHelper emdWriter = new HsaClassLibrary.Helpers.ReadWriteHelper();
            emdWriter.WriteCSV(data, "D:\\hsa8.csv");
            System.Console.WriteLine("Файл csv создан.\nD:\\hsa8.csv");
            //System.Console.WriteLine("Console.ReadKey();");
            Console.ReadKey();
            return 0;
        }