private void FormReal_Load(object sender, EventArgs e)
        {
            N = 5;
            P = 1000000;
            L = 1024;

            double[] result;

            ILeastSquare ls = LeastSquare.Create(LSType.GeneralReal);

            for (int n = 2; n < 8; n++)
            {
                N = n;

                ls.Ad = A;
                ls.Dd = D;

                ls.Solve(N, P, out result);

                for (int i = 0; i < result.Length; i++)
                {
                    Console.Write("{0,15:0.0000000000}", result[i]);
                }
                Console.WriteLine();
            }
            this.Close();
        }
Beispiel #2
0
        private void FormComplex_Load(object sender, EventArgs e)
        {
            N = 3;
            P = 100;
            double[] result;

            ILeastSquare ls = LeastSquare.Create(LSType.GeneralComplex);

            ls.Ac = A;
            ls.Dc = D;

            ls.Solve(N, P, out result);

            this.Close();
        }
        private void FirTest()
        {
            double fs     = 48000;
            int    N      = 1000;
            int    length = 51;

            double[]  result1, result2;
            double[]  om  = new double[N];
            Complex[] Exp = new Complex[N];
            Complex[] D   = new Complex[N];
            double[]  W   = new double[N];
            Complex[] H   = new Complex[N];

            double f1 = fs / 2.56 / 2;
            double f2 = 1.28 * f1;

            for (int i = 0; i < N; i++)
            {
                if (i < N / 2)
                {
                    om[i]  = f1 / (N / 2) * i * 2 * Math.PI / fs;
                    Exp[i] = new Complex(Math.Cos(om[i]), Math.Sin(-om[i]));
                    D[i]   = new Complex(1, 0) * Exp[i] * Exp[i];// *Exp[i];// *Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i];
                    W[i]   = 0.000001;
                }
                else
                {
                    om[i]  = ((fs / 2 - f2) / (N / 2) * (i - N / 2) + f2) * 2 * Math.PI / fs;
                    Exp[i] = new Complex(Math.Cos(om[i]), Math.Sin(-om[i]));
                    D[i]   = new Complex(0, 0);
                    W[i]   = 1;
                }
            }

            LeastSquare.Create(LSType.FirComplex).Solve(length, Exp, D, W, out result1);
            H = FirResponse(om, result1);
            DisplayFirResult(result1, f1, fs);

            double sum = 0;

            for (int j = 0; j < N; j++)
            {
                sum += W[j] * Complex.AbsSqr(H[j] - D[j]);
            }
            Console.WriteLine("Error1  {0}", Math.Sqrt(sum / N));

            for (int j = 0; j < N; j++)
            {
                W[j] *= W[j];
            }

            for (int k1 = 0; k1 < 100; k1++)
            {
                LeastSquare.Create(LSType.FirPowerApprox).Solve(length, Exp, D, W, H, out result2);
                for (int i = 0; i < length; i++)
                {
                    result1[i] += 0.5 * result2[i];
                }

                H = FirResponse(om, result1);

                sum = 0;
                for (int j = 0; j < N; j++)
                {
                    sum += W[j] * Math.Pow((Complex.AbsSqr(H[j]) - Complex.AbsSqr(D[j])), 2);
                }
                Console.WriteLine("Error2  {0}", Math.Sqrt(sum / N));

                DisplayFirResult(result1, f1, fs);
            }
        }
        private void IirTest()
        {
            double fs               = 4000;
            int    N                = 1000;
            int    numeratorOrder   = 4;
            int    denominatorOrder = 4;

            double[]  result;
            double[]  om  = new double[N];
            Complex[] Exp = new Complex[N];
            Complex[] D   = new Complex[N];
            double[]  W   = new double[N];
            Complex[] A   = new Complex[N];
            Complex[] B   = new Complex[N];
            Complex[] H   = new Complex[N];
            double[]  a   = new double[denominatorOrder + 1];
            double[]  b   = new double[numeratorOrder + 1];

            double f1 = fs / 8;
            double f2 = 3 * f1;

            for (int i = 0; i < N; i++)
            {
                if (i < N / 2)
                {
                    om[i]  = f1 / (N / 2) * i * 2 * Math.PI / fs;
                    Exp[i] = new Complex(Math.Cos(om[i]), Math.Sin(-om[i]));
                    D[i]   = new Complex(1, 0) * Exp[i] * Exp[i] * Exp[i];// *Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i] * Exp[i];
                    W[i]   = 1;
                }
                else
                {
                    om[i]  = ((fs / 2 - f2) / (N / 2) * (i - N / 2) + f2) * 2 * Math.PI / fs;
                    Exp[i] = new Complex(Math.Cos(om[i]), Math.Sin(-om[i]));
                    D[i]   = new Complex(0, 0);
                    W[i]   = 1;
                }
            }

            a[0] = 1;
            A    = FirResponse(om, a);
            LeastSquare.Create(LSType.FirComplex).Solve(numeratorOrder + 1, Exp, Vector.Mul(D, A), W, out b);
            DisplayFirResult(b, f1, fs);

            H = FirResponse(om, b);
            double sum = 0;

            for (int j = 0; j < N; j++)
            {
                sum += W[j] * Math.Pow((Complex.AbsSqr(H[j]) - Complex.AbsSqr(D[j])), 2);
            }
            Console.WriteLine("Error1  {0}", Math.Sqrt(sum / N));

            for (int j = 0; j < N; j++)
            {
                W[j] *= 1;// W[j];
            }
            for (int k1 = 0; k1 < 5000; k1++)
            {
                LeastSquare.Create(LSType.IirComplexApprox).Solve(numeratorOrder + 1 + denominatorOrder, numeratorOrder + 1, Exp, D, W, H, A, out result);
                for (int i = 0; i < numeratorOrder + 1; i++)
                {
                    b[i] += 0.5 * result[i];
                }
                for (int i = 0; i < denominatorOrder; i++)
                {
                    a[i + 1] += 0.5 * result[i + numeratorOrder + 1];
                }

                PolynomialRootFinder.PolynomialRootFinder poly = new PolynomialRootFinder.PolynomialRootFinder();
                Complex[] roots = new Complex[denominatorOrder];
                int       numberRoots;
                poly.FindRootsReverse(a, roots, out numberRoots);
                for (int i = 0; i < numberRoots; i++)
                {
                    if (Complex.AbsSqr(roots[i]) > 1)
                    {
                        roots[i] = 1 / roots[i];
                    }
                    if (Complex.AbsSqr(roots[i]) > 0.81)
                    {
                        roots[i] /= Math.Sqrt(Complex.AbsSqr(roots[i])) * 1.1;
                    }
                    Console.WriteLine(Math.Sqrt(Complex.AbsSqr(roots[i])));
                }

                a = poly.BuildPolynomial(roots);
                double suma = 0;
                for (int i = 0; i < numeratorOrder + 1; i++)
                {
                    suma += a[i];
                }

                double sumb = 0;
                for (int i = 0; i < numeratorOrder + 1; i++)
                {
                    sumb += b[i];
                }

                for (int i = 0; i < numeratorOrder + 1; i++)
                {
                    b[i] *= suma / sumb;
                }

                A = FirResponse(om, a);
                for (int j = 0; j < N; j++)
                {
                    if (Complex.AbsSqr(A[j]) < 0.1)
                    {
                        A[j] /= Math.Sqrt(Complex.AbsSqr(A[j])) * 10;
                    }
                }

                H = Vector.Div(FirResponse(om, b), A);
                //for (int j = 0; j < N; j++)
                //    if (Complex.AbsSqr(H[j]) > 1.1)
                //        H[j] /= Math.Sqrt(Complex.AbsSqr(H[j]));

                //for (int j = 0; j < N; j++)
                //       D[j] = Math.Sqrt(Complex.AbsSqr(D[j]/H[j])) * H[j];
                sum = 0;
                for (int j = 0; j < N; j++)
                {
                    sum += W[j] * Math.Pow((Complex.AbsSqr(H[j]) - Complex.AbsSqr(D[j])), 2);
                }
                Console.WriteLine("Error2  {0}", Math.Sqrt(sum / N));

                DisplayIirResult(a, b, f1, fs);
            }
        }