예제 #1
0
    public static double[] f_alpha(int n, double q_d, double alpha, ref typeMethods.r8NormalData data, ref int seed)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    F_ALPHA generates a 1/F^ALPHA noise sequence.
    //
    //  Discussion:
    //
    //    Thanks to Miro Stoyanov for pointing out that the second half of
    //    the data returned by the inverse Fourier transform should be
    //    discarded, 24 August 2010.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    24 August 2010
    //
    //  Author:
    //
    //    Original C version by Todd Walter.
    //    C++ version by John Burkardt.
    //
    //  Reference:
    //
    //    Jeremy Kasdin,
    //    Discrete Simulation of Colored Noise and Stochastic Processes
    //    and 1/f^a Power Law Noise Generation,
    //    Proceedings of the IEEE,
    //    Volume 83, Number 5, 1995, pages 802-827.
    //
    //  Parameters:
    //
    //    Input, int N, the number of samples of the noise to generate.
    //
    //    Input, double Q_D, the variance of the noise.
    //
    //    Input, double ALPHA, the exponent for the noise.
    //
    //    Input/output, int *SEED, a seed for the random number generator.
    //
    //    Output, double F_ALPHA[N], a sequence sampled with the given
    //    power law.
    //
    {
        double h_azero = 0;
        int    i;
        double w_azero = 0;

        //
        //  Set the deviation of the noise.
        //
        q_d = Math.Sqrt(q_d);
        //
        //  Generate the coefficients Hk.
        //
        double[] hfa = new double[2 * n];
        hfa[0] = 1.0;
        for (i = 1; i < n; i++)
        {
            hfa[i] = hfa[i - 1]
                     * (0.5 * alpha + (i - 1)) / i;
        }

        for (i = n; i < 2 * n; i++)
        {
            hfa[i] = 0.0;
        }

        //
        //  Fill Wk with white noise.
        //
        double[] wfa = new double[2 * n];

        for (i = 0; i < n; i++)
        {
            wfa[i] = q_d * typeMethods.r8_normal_01(ref data, ref seed);
        }

        for (i = n; i < 2 * n; i++)
        {
            wfa[i] = 0.0;
        }

        //
        //  Perform the discrete Fourier transforms of Hk and Wk.
        //
        double[] h_a = new double[n];
        double[] h_b = new double[n];

        Slow.r8vec_sftf(2 * n, hfa, ref h_azero, ref h_a, ref h_b);

        double[] w_a = new double[n];
        double[] w_b = new double[n];

        Slow.r8vec_sftf(2 * n, wfa, ref w_azero, ref w_a, ref w_b);
        //
        //  Multiply the two complex vectors.
        //
        w_azero *= h_azero;

        for (i = 0; i < n; i++)
        {
            double wr = w_a[i];
            double wi = w_b[i];
            w_a[i] = wr * h_a[i] - wi * h_b[i];
            w_b[i] = wi * h_a[i] + wr * h_b[i];
        }

        //
        //  This scaling is introduced only to match the behavior
        //  of the Numerical Recipes code...
        //
        w_azero = w_azero * 2 * n;
        for (i = 0; i < n - 1; i++)
        {
            w_a[i] *= n;
            w_b[i] *= n;
        }

        i      = n - 1;
        w_a[i] = w_a[i] * 2 * n;
        w_b[i] = w_b[i] * 2 * n;
        //
        //  Take the inverse Fourier transform of the result.
        //
        double[] x2 = Slow.r8vec_sftb(2 * n, w_azero, w_a, w_b);
        //
        //  Only return the first N inverse Fourier transform values.
        //
        double[] x = new double[n];
        for (i = 0; i < n; i++)
        {
            x[i] = x2[i];
        }

        return(x);
    }
예제 #2
0
    private static void r8vec_sft_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    R8VEC_SFT_TEST tests R8VEC_SFTB and R8VEC_SFTF.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    27 February 2010
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const double ahi   = 5.0;
        const double alo   = 0.0;
        double       azero = 0;
        int          i;
        const int    n = 36;

        Console.WriteLine("");
        Console.WriteLine("R8VEC_SFT_TEST");
        Console.WriteLine("  R8VEC_SFTF computes the forward slow Fourier transform.");
        Console.WriteLine("  R8VEC_SFTB computes the backward slow Fourier transform.");
        Console.WriteLine("");
        Console.WriteLine("  The number of data values, N = " + n + "");

        int seed = 123456789;

        double[] x = UniformRNG.r8vec_uniform_ab_new(n, alo, ahi, ref seed);

        typeMethods.r8vec_print_part(n, x, 10, "  The original data:");
        //
        //  Compute the slow Fourier transform of the data.
        //
        double[] a = new double[n / 2];
        double[] b = new double[n / 2];

        Slow.r8vec_sftf(n, x, ref azero, ref a, ref b);

        Console.WriteLine("");
        Console.WriteLine("  A (cosine) coefficients:");
        Console.WriteLine("");

        Console.WriteLine("  " + 0.ToString(CultureInfo.InvariantCulture).PadLeft(4)
                          + "  " + azero.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");

        for (i = 0; i < n / 2; i++)
        {
            Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(4)
                              + "  " + a[i].ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
        }

        Console.WriteLine("");
        Console.WriteLine("  B (sine) coefficients:");
        Console.WriteLine("");

        for (i = 0; i < n / 2; i++)
        {
            Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(4)
                              + "  " + b[i].ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
        }

        //
        //  Now try to retrieve the data from the coefficients.
        //
        double[] z = Slow.r8vec_sftb(n, azero, a, b);

        typeMethods.r8vec_print_part(n, z, 10, "  The retrieved data:");
    }