Example #1
0
        /// <summary>
        /// Real to complex transform.
        /// </summary>
        static void Example2(int length)
        {
            Console.Write("Test 2: real to complex transform ... ");

            int n = length;

            // Create two managed arrays, possibly misalinged.
            var data = Util.GenerateSignal(n);

            // Copy to native memory.
            var input  = new RealArray(data);
            var output = new ComplexArray(n / 2 + 1);

            // Create a managed plan.
            var plan1 = Plan.Create1(n, input, output, Options.Estimate);

            plan1.Execute();

            var plan2 = Plan.Create1(n, output, input, Options.Estimate);

            plan2.Execute();

            Array.Clear(data, 0, n);

            // Copy unmanaged output of back-tranform to managed array.
            input.CopyTo(data);

            // Check and see how we did.
            Util.CheckResults(n, n, data);
        }
Example #2
0
        public double[] Spectrum(double[] input, bool scale)
        {
            int length = input.Length;

            using (var data1 = new RealArray(length))
                using (var data2 = new ComplexArray(length / 2 + 1))
                    using (var plan1 = Plan.Create1(length, data1, data2, Options.Estimate))
                        using (var plan2 = Plan.Create1(length, data2, data1, Options.Estimate))
                        {
                            data1.Set(input);

                            plan1.Execute();

                            var temp     = data2.ToArray();
                            var spectrum = Helper.ComputeSpectrum(temp);

                            plan2.Execute();

                            data1.CopyTo(input);

                            if (scale)
                            {
                                for (int i = 0; i < length; i++)
                                {
                                    input[i] /= length;
                                }
                            }

                            return(spectrum);
                        }
        }
Example #3
0
 protected RealArray CreateArray(string name, int len, ComputeMemoryFlags?memoryFlag = null)
 {
     arrays[name] = new RealArray(len);
     if (memoryFlag != null)
     {
         arrays[name].GpuMemoryFlag = (ComputeMemoryFlags)memoryFlag;
     }
     return(arrays[name]);
 }
Example #4
0
        public void Initialize(double[] data)
        {
            int length = Size = data.Length;

            this.data = (double[])data.Clone();

            input  = new RealArray(data);
            output = new ComplexArray(length / 2 + 1);

            plan = Plan.Create1(length, input, output, Options.Estimate);
        }
Example #5
0
 void CalcBiasGrad(RealArray gy, int batchCount)
 {
     using (gy.Switch(Common.ComputeDeviceTypes.Cpu))
         using (Bias.Grad.Switch(Common.ComputeDeviceTypes.Cpu))
         {
             for (int batchCounter = 0; batchCounter < batchCount; batchCounter++)
             {
                 for (int i = 0; i < OutputCount; i++)
                 {
                     Bias.Grad[i] += gy[batchCounter * OutputCount + i];
                 }
             }
         }
 }
Example #6
0
        public void Initialize(double[] data)
        {
            int length = Size = data.Length;

            this.data = new float[length];

            for (int i = 0; i < length; i++)
            {
                this.data[i] = (float)data[i];
            }

            input  = new RealArray(this.data);
            output = new ComplexArray(length / 2 + 1);

            plan = Plan.Create1(length, input, output, Options.Estimate);
        }
Example #7
0
        protected override void NeedPreviousBackwardCpu(NdArray y, NdArray x)
        {
            RealArray activatedgy = Activator != null?GetActivatedgy(y) : y.Grad;

            if (!NoBias)
            {
                CalcBiasGrad(activatedgy, y.BatchCount);
            }

            for (int batchCount = 0; batchCount < y.BatchCount; batchCount++)
            {
                for (int i = 0; i < this.OutputCount; i++)
                {
                    Real gyData = activatedgy[i + batchCount * this.OutputCount];

                    for (int j = 0; j < this.InputCount; j++)
                    {
                        this.Weight.Grad[i * this.InputCount + j] += x.Data[j + batchCount * this.InputCount] * gyData;
                        x.Grad[j + batchCount * this.InputCount]  += this.Weight.Data[i * this.InputCount + j] * gyData;
                    }
                }
            }
        }
Example #8
0
 protected RealArray SetArray(string name, RealArray arr)
 {
     arrays[name] = arr;
     return(arr);
 }