Exemplo n.º 1
0
    def fft2(self, data, dir=0, zp=1, out_shape=[], tx_ON=True):
        # data: np.complex64
        # dir: int (0 or 1)
        # zp: float (>1)

        # simplify the fftw wrapper
        import numpy as np
        import core.math.fft as corefft

        # generate output dim size array
        # fortran dimension ordering
        outdims = list(data.shape)
        if len(out_shape):
            outdims = out_shape
        else:
            for i in range(len(outdims)):
                outdims[i] = int(outdims[i] * zp)
        outdims.reverse()
        outdims = np.array(outdims, dtype=np.int64)

        # load fft arguments
        kwargs = {}
        kwargs['dir'] = dir

        # transform or just zeropad
        if tx_ON:
            kwargs['dim1'] = 1
            kwargs['dim2'] = 1
        else:
            kwargs['dim1'] = 0
            kwargs['dim2'] = 0

        return corefft.fftw(data, outdims, **kwargs)
Exemplo n.º 2
0
def fft2D(data, dir=0, out_dims_fft=[]):
    # data: np.complex64
    # dir: int (0 or 1)
    # outdims = [nr_coils, extra_dim2, extra_dim1, mtx, mtx]

    import core.math.fft as corefft

    # generate output dim size array
    # fortran dimension ordering
    if len(out_dims_fft):
        outdims = out_dims_fft.copy()
    else:
        outdims = list(data.shape)
    
    outdims.reverse()
    outdims = np.array(outdims, dtype=np.int64)

    # load fft arguments
    kwargs = {}
    kwargs['dir'] = dir

    # transform
    kwargs['dim1'] = 1
    kwargs['dim2'] = 1
    kwargs['dim3'] = 0
    kwargs['dim4'] = 0
    kwargs['dim5'] = 0

    return corefft.fftw(data, outdims, **kwargs)
Exemplo n.º 3
0
    def fft2(self, data, dir=0, zp=1, out_shape=[], tx_ON=True):
        # data: np.complex64
        # dir: int (0 or 1)
        # zp: float (>1)

        # simplify the fftw wrapper
        import numpy as np
        import core.math.fft as corefft

        # generate output dim size array
        # fortran dimension ordering
        outdims = list(data.shape)
        if len(out_shape):
            outdims = out_shape
        else:
            for i in range(len(outdims)):
                outdims[i] = int(outdims[i]*zp)
        outdims.reverse()
        outdims = np.array(outdims, dtype=np.int64)

        # load fft arguments
        kwargs = {}
        kwargs['dir'] = dir

        # transform or just zeropad
        if tx_ON:
            kwargs['dim1'] = 1
            kwargs['dim2'] = 1
        else:
            kwargs['dim1'] = 0
            kwargs['dim2'] = 0

        return corefft.fftw(data, outdims, **kwargs)
Exemplo n.º 4
0
    def compute(self):

        import numpy as np

        data = self.getData('in')
        if data.dtype != 'complex128':
            data = data.astype('complex64')

        if self.getVal('compute'):

            # build the fft.fft argument list
            kwargs = {}

            # Direction | 0:FWD, 1:BKWD
            if self.getVal('inverse'):
                kwargs['dir'] = 1
            else:
                kwargs['dir'] = 0

            out_dims = np.array([], np.int64)

            # load up the dimension args
            for i in range(self.ndim):
                kwargs['dim'+str(i+1)] = 0
                val = self.getVal(self.dim_base_name+str(-i-1)+']')
                if i < len(data.shape):
                    out_dims = np.append(out_dims, val['length'])
                    if val['compute']:
                        kwargs['dim'+str(i+1)] = 1


            # import in thread to save namespace
            import core.math.fft as ft

            out = ft.fftw(data.astype(np.complex64), out_dims, **kwargs)

            if data.dtype == np.complex128:
                out = out.astype(np.complex128)

            self.setData('out', out)

        return(0)
Exemplo n.º 5
0
    def compute(self):

        import numpy as np

        data = self.getData('in')
        data = np.require(data, dtype=np.complex64, requirements='C')

        if self.getVal('compute'):

            # build the fft.fft argument list
            kwargs = {}

            # Direction | 0:FWD, 1:BKWD
            if self.getVal('inverse'):
                kwargs['dir'] = 1
            else:
                kwargs['dir'] = 0

            out_dims = np.array([], np.int64)

            # load up the dimension args
            for i in range(self.ndim):
                kwargs['dim' + str(i + 1)] = 0
                val = self.getVal(self.dim_base_name + str(-i - 1) + ']')
                if i < len(data.shape):
                    out_dims = np.append(out_dims, val['length'])
                    if val['compute']:
                        kwargs['dim' + str(i + 1)] = 1

            # import in thread to save namespace
            import core.math.fft as ft

            out = ft.fftw(data, out_dims, **kwargs)

            self.setData('out', out)

        return (0)
Exemplo n.º 6
0
    def compute(self):
        data_in = self.getData('in')
        kind = self.getVal('interpolation-mode')

        self.dimm = data_in.shape
        self.ndim = data_in.ndim

        data_out = data_in.copy()
        if self.getVal('compute'):
            if kind in ('linear', 'nearest'):
                for i in range(self.ndim):
                    val = self.getVal(self.dim_base_name + str(i) + ']')
                    interpnew = val['length']
                    axisnew = self.dimm[i]
                    x = np.linspace(0, axisnew - 1, axisnew)
                    xnew = np.linspace(0, axisnew - 1, interpnew)
                    if axisnew == 1:
                        reps = np.ones((self.ndim, ))
                        reps[i] = interpnew
                        ynew = np.tile(data_out, reps)
                    elif axisnew == interpnew:
                        continue
                    else:
                        yinterp = interpolate.interp1d(x,
                                                       data_out,
                                                       kind=kind,
                                                       axis=i)
                        ynew = yinterp(xnew)

                    data_out = ynew
            elif kind in ('zero', 'slinear', 'quadratic', 'cubic'):
                from scipy.ndimage.interpolation import map_coordinates
                orders = {'zero': 0, 'slinear': 1, 'quadratic': 2, 'cubic': 3}
                o = orders[kind]

                # if the data is complex, interp real and imaginary separately
                if data_in.dtype in (np.complex64, np.complex128):
                    data_real = np.real(data_in)
                    data_imag = np.imag(data_in)
                else:
                    data_real = data_in
                    data_imag = None

                new_dims = []
                for i in range(self.ndim):
                    val = self.getVal(self.dim_base_name + str(i) + ']')
                    new_dims.append(
                        np.linspace(0, val['in_len'] - 1, val['length']))

                coords = np.meshgrid(*new_dims, indexing='ij')
                data_out = map_coordinates(data_real, coords, order=o)

                if data_imag is not None:
                    data_out = (
                        data_out +
                        1j * map_coordinates(data_imag, coords, order=o))

            else:
                # use zero-padding and FFTW to sinc-interpolate
                import core.math.fft as ft
                data_in_c64 = np.require(data_in,
                                         dtype=np.complex64,
                                         requirements='C')

                old_dims = np.asarray(self.dimm[::-1], dtype=np.int64)
                new_dims = old_dims.copy()
                fftargs = {}
                win = np.ones(data_in.shape)
                scale = 1
                for i in range(self.ndim):
                    val = self.getVal(self.dim_base_name + str(i) + ']')
                    new_dims[self.ndim - i - 1] = np.int64(val['length'])
                    if val['length'] == val['in_len']:
                        fftargs['dim{}'.format(self.ndim - i)] = 0
                    else:
                        win *= self.window(i)
                        fftargs['dim{}'.format(self.ndim - i)] = 1
                        scale *= val['length'] / val['in_len']

                # forward FFT with original dimensions
                fftargs['dir'] = 0
                data_out = ft.fftw(data_in_c64, old_dims, **fftargs)

                data_out *= win.astype(np.complex64)
                # inverse FFT with new dimensions (zero-pad kspace)
                fftargs['dir'] = 1
                data_out = ft.fftw(data_out, new_dims, **fftargs)

                if data_in.dtype in (np.float32, np.float64):
                    data_out = np.real(data_out)

                # data needs to be scaled since we changed the size between the
                # forward and inverse FT
                data_out *= scale

            self.setData('out', data_out)
        else:
            pass

        return (0)