Esempio n. 1
0
        public static Complex[,] Forward(double[,] image, double norm = 1.0)
        {
            Complex[,] output = new Complex[image.GetLength(0), image.GetLength(1)];
            using (var imageSpace = new AlignedArrayComplex(16, image.GetLength(0), image.GetLength(1)))
                using (var fourierSpace = new AlignedArrayComplex(16, imageSpace.GetSize()))
                {
                    for (int y = 0; y < image.GetLength(0); y++)
                    {
                        for (int x = 0; x < image.GetLength(1); x++)
                        {
                            imageSpace[y, x] = image[y, x];
                        }
                    }

                    DFT.FFT(imageSpace, fourierSpace, PlannerFlags.Default, Environment.ProcessorCount);

                    for (int y = 0; y < image.GetLength(0); y++)
                    {
                        for (int x = 0; x < image.GetLength(1); x++)
                        {
                            output[y, x] = fourierSpace[y, x] / norm;
                        }
                    }
                }

            return(output);
        }
Esempio n. 2
0
        static void Example2D()
        {
            using (var input = new AlignedArrayComplex(16, 64, 16))
                using (var output = new AlignedArrayComplex(16, input.GetSize()))
                {
                    for (int row = 0; row < input.GetLength(0); row++)
                    {
                        for (int col = 0; col < input.GetLength(1); col++)
                        {
                            input[row, col] = (double)row * col / input.Length;
                        }
                    }

                    DFT.FFT(input, output);
                    DFT.IFFT(output, output);

                    for (int row = 0; row < input.GetLength(0); row++)
                    {
                        for (int col = 0; col < input.GetLength(1); col++)
                        {
                            Console.Write((output[row, col].Real / input[row, col].Real / input.Length).ToString("F2").PadLeft(6));
                        }
                        Console.WriteLine();
                    }
                }
        }
Esempio n. 3
0
        public static double[,] Backward(Complex[,] grid, long visibilityCount)
        {
            double[,] output = new double[grid.GetLength(0), grid.GetLength(1)];
            using (var imageSpace = new AlignedArrayComplex(16, grid.GetLength(0), grid.GetLength(1)))
                using (var fourierSpace = new AlignedArrayComplex(16, imageSpace.GetSize()))
                {
                    for (int y = 0; y < grid.GetLength(0); y++)
                    {
                        for (int x = 0; x < grid.GetLength(1); x++)
                        {
                            fourierSpace[y, x] = grid[y, x];
                        }
                    }

                    DFT.IFFT(fourierSpace, imageSpace, PlannerFlags.Default, Environment.ProcessorCount);

                    for (int y = 0; y < grid.GetLength(0); y++)
                    {
                        for (int x = 0; x < grid.GetLength(1); x++)
                        {
                            output[y, x] = imageSpace[y, x].Real / visibilityCount;
                        }
                    }
                }

            return(output);
        }
Esempio n. 4
0
        public static float[,] WStackIFFTFloat(List <Complex[, ]> grid, long visibilityCount)
        {
            var output = new float[grid[0].GetLength(0), grid[0].GetLength(1)];

            using (var imageSpace = new AlignedArrayComplex(16, grid[0].GetLength(0), grid[0].GetLength(1)))
                using (var fourierSpace = new AlignedArrayComplex(16, imageSpace.GetSize()))
                {
                    for (int k = 0; k < grid.Count; k++)
                    {
                        Parallel.For(0, grid[0].GetLength(0), (y) =>
                        {
                            for (int x = 0; x < grid[0].GetLength(1); x++)
                            {
                                fourierSpace[y, x] = grid[k][y, x];
                            }
                        });

                        DFT.IFFT(fourierSpace, imageSpace, PlannerFlags.Default, Environment.ProcessorCount);

                        Parallel.For(0, grid[0].GetLength(0), (y) =>
                        {
                            for (int x = 0; x < grid[0].GetLength(1); x++)
                            {
                                output[y, x] += (float)(imageSpace[y, x].Real / visibilityCount);
                            }
                        });
                    }
                }

            return(output);
        }
Esempio n. 5
0
        public static float[,] BackwardFloat(Complex[,] image, double norm)
        {
            var output = new float[image.GetLength(0), image.GetLength(1)];

            using (var imageSpace = new AlignedArrayComplex(16, image.GetLength(0), image.GetLength(1)))
                using (var fourierSpace = new AlignedArrayComplex(16, imageSpace.GetSize()))
                {
                    for (int y = 0; y < image.GetLength(0); y++)
                    {
                        for (int x = 0; x < image.GetLength(1); x++)
                        {
                            imageSpace[y, x] = image[y, x];
                        }
                    }

                    DFT.IFFT(imageSpace, fourierSpace, PlannerFlags.Default, Environment.ProcessorCount);

                    for (int y = 0; y < image.GetLength(0); y++)
                    {
                        for (int x = 0; x < image.GetLength(1); x++)
                        {
                            output[y, x] = (float)(fourierSpace[y, x].Real / norm);
                        }
                    }
                }

            return(output);
        }
Esempio n. 6
0
        public static Complex[,] Forward(float[,] image, double norm = 1.0)
        {
            Complex[,] output = new Complex[image.GetLength(0), image.GetLength(1)];
            using (var imageSpace = new AlignedArrayComplex(16, image.GetLength(0), image.GetLength(1)))
                using (var fourierSpace = new AlignedArrayComplex(16, imageSpace.GetSize()))
                {
                    for (int y = 0; y < image.GetLength(0); y++)
                    {
                        for (int x = 0; x < image.GetLength(1); x++)
                        {
                            imageSpace[y, x] = image[y, x];
                        }
                    }

                    DFT.FFT(imageSpace, fourierSpace);

                    for (int y = 0; y < image.GetLength(0); y++)
                    {
                        for (int x = 0; x < image.GetLength(1); x++)
                        {
                            output[y, x] = fourierSpace[y, x] / norm;
                        }
                    }
                }

            return(output);
        }
Esempio n. 7
0
        public static List <List <Complex[, ]> > Forward(GriddingConstants c, List <List <Complex[, ]> > subgrids)
        {
            var output = new List <List <Complex[, ]> >(subgrids.Count);

            for (int baseline = 0; baseline < subgrids.Count; baseline++)
            {
                var blSubgrids = subgrids[baseline];
                var blOutput   = new List <Complex[, ]>(blSubgrids.Count);
                for (int subgrid = 0; subgrid < blSubgrids.Count; subgrid++)
                {
                    var sub        = blSubgrids[subgrid];
                    var outFourier = new Complex[c.SubgridSize, c.SubgridSize];
                    using (var imageSpace = new AlignedArrayComplex(16, c.SubgridSize, c.SubgridSize))
                        using (var fourierSpace = new AlignedArrayComplex(16, imageSpace.GetSize()))
                        {
                            //copy
                            for (int i = 0; i < c.SubgridSize; i++)
                            {
                                for (int j = 0; j < c.SubgridSize; j++)
                                {
                                    imageSpace[i, j] = sub[i, j];
                                }
                            }

                            /*
                             * This is not a bug
                             * The original IDG implementation uses the inverse Fourier transform here, even though the
                             * Subgrids are already in image space.
                             */
                            DFT.IFFT(imageSpace, fourierSpace);
                            var norm = 1.0 / (c.SubgridSize * c.SubgridSize);

                            for (int i = 0; i < c.SubgridSize; i++)
                            {
                                for (int j = 0; j < c.SubgridSize; j++)
                                {
                                    outFourier[i, j] = fourierSpace[i, j] * norm;
                                }
                            }
                        }
                    blOutput.Add(outFourier);
                }
                output.Add(blOutput);
            }

            return(output);
        }
Esempio n. 8
0
        public static List <List <Complex[, ]> > Backward(GriddingConstants c, List <List <Complex[, ]> > subgrids)
        {
            var output = new List <List <Complex[, ]> >(subgrids.Count);

            for (int baseline = 0; baseline < subgrids.Count; baseline++)
            {
                var blSubgrids = subgrids[baseline];
                var blOutput   = new List <Complex[, ]>(blSubgrids.Count);
                for (int subgrid = 0; subgrid < blSubgrids.Count; subgrid++)
                {
                    var sub        = blSubgrids[subgrid];
                    var outFourier = new Complex[c.SubgridSize, c.SubgridSize];
                    using (var imageSpace = new AlignedArrayComplex(16, c.SubgridSize, c.SubgridSize))
                        using (var fourierSpace = new AlignedArrayComplex(16, imageSpace.GetSize()))
                        {
                            //copy
                            for (int i = 0; i < c.SubgridSize; i++)
                            {
                                for (int j = 0; j < c.SubgridSize; j++)
                                {
                                    imageSpace[i, j] = sub[i, j];
                                }
                            }

                            DFT.FFT(imageSpace, fourierSpace);
                            //normalization is done in the Gridder

                            for (int i = 0; i < c.SubgridSize; i++)
                            {
                                for (int j = 0; j < c.SubgridSize; j++)
                                {
                                    outFourier[i, j] = fourierSpace[i, j];
                                }
                            }
                        }
                    blOutput.Add(outFourier);
                }
                output.Add(blOutput);
            }

            return(output);
        }
Esempio n. 9
0
 public static cuDoubleComplex[] PerformFft(cuDoubleComplex[] data, int size, TransformDirection direction)
 {
     if (cudaAvailable)
     {
         return(cuHelper.PerformFFT(data, size, direction));
     }
     else
     {
         cuDoubleComplex[] result = new cuDoubleComplex[size * size];
         using (var input = new AlignedArrayComplex(16, size, size))
             using (var output = new AlignedArrayComplex(16, input.GetSize()))
             {
                 for (int i = 0; i < input.GetLength(0); i++)
                 {
                     for (int j = 0; j < input.GetLength(1); j++)
                     {
                         input[i, j] = new Complex(data[i * size + j].real, data[i * size + j].imag);
                     }
                 }
                 if (direction == TransformDirection.Forward)
                 {
                     DFT.FFT(input, output);
                 }
                 else
                 {
                     DFT.IFFT(input, output);
                 }
                 for (int i = 0; i < input.GetLength(0); i++)
                 {
                     for (int j = 0; j < input.GetLength(1); j++)
                     {
                         result[i * size + j].real = output[i, j].Real;
                         result[i * size + j].imag = output[i, j].Imaginary;
                     }
                 }
             }
         return(result);
     }
 }