Exemplo n.º 1
0
def rfft(a, af=None, inplace=0, nthreads=1):
    #CHECK b type size

    if inplace:
        inplace = _sfftw.FFTW_IN_PLACE  # == 8
        shape = a.shape[:-1] + (a.shape[-1] - 2, )
    else:
        shape = a.shape
        inplace = 0
    dir = _sfftw.FFTW_FORWARD
    if a.dtype == _N.float32:
        if af is not None and (not af.flags.carray
                               or af.dtype != _N.complex64):
            raise RuntimeError("af needs to be well behaved Complex64 array")
        key = ("sr%d" % inplace, shape)

        try:
            p = _splans[key]
        except:
            ashape = _N.array(shape, dtype=_N.int32)
            p = _sfftw.rfftwnd_create_plan(len(shape), ashape, dir,
                                           _measure | inplace)
            if p is None:
                raise RuntimeError("could not create plan")
            _splans[key] = p

        if inplace:
            #debug print 11111111111111
            _sfftw.rfftwnd_one_real_to_complex(p, a, None)
            if af is None:
                s2 = shape[:-1] + (shape[-1] / 2. + 1, )
                af = _N.ndarray(buffer=a, shape=s2, dtype=_N.complex64)
                return af
        else:
            #debug print "plan", repr(p)
            #debuf print 22222222222222, a.shape
            #debuf print a.flags, a.dtype.isnative
            if af is None:
                s2 = shape[:-1] + (shape[-1] / 2. + 1, )
                af = _N.empty(shape=s2, dtype=_N.complex64)
                #debuf print 33333333322222, af.shape
                #debuf print af.flags, af.dtype.isnative
                _sfftw.rfftwnd_one_real_to_complex(p, a, af)
                return af
            else:
                #debuf print 22222222222222, af.shape
                #debuf print af.flags, af.dtype.isnative
                _sfftw.rfftwnd_one_real_to_complex(p, a, af)

    elif a.dtype == _N.float64:
        if af is not None and (not af.flags.carray
                               or af.dtype != _N.complex128):
            raise RuntimeError("af needs to be well behaved Complex64 array")
        key = ("dr%d" % inplace, shape)

        try:
            p = _dplans[key]
        except:
            p = _dfftw.rfftwnd_create_plan(len(shape),
                                           _N.array(shape, dtype=_N.int32),
                                           dir, _measure | inplace)
            if p is None:
                raise RuntimeError("could not create plan")
            _dplans[key] = p

        if inplace:
            _dfftw.rfftwnd_one_real_to_complex(p, a, None)
            if af is None:
                s2 = shape[:-1] + (shape[-1] / 2. + 1, )
                af = _N.ndarray(buffer=a, shape=s2, dtype=_N.complex128)
                return af
        else:
            if af is None:
                s2 = shape[:-1] + (shape[-1] / 2. + 1, )
                af = _N.empty(shape=s2, dtype=_N.complex128)
                _dfftw.rfftwnd_one_real_to_complex(p, a, af)
                return af
            else:
                _dfftw.rfftwnd_one_real_to_complex(p, a, af)

    else:
        raise TypeError("(c)float32 and (c)float64 must be used consistently (%s %s)"%\
              ((a is None and "a is None" or "a.dtype=%s"%a.dtype),
               (af is None and "af is None" or "af.dtype=%s"%af.dtype)))
Exemplo n.º 2
0
Arquivo: fftw.py Projeto: LLNL/WVL
def rfft(a,af=None, inplace=0):
    #CHECK b type size

    if inplace:
        inplace = _sfftw.FFTW_IN_PLACE     # == 8
        shape = a.shape[:-1]+(a.shape[-1]-2,)
    else:
        shape = a.shape
        inplace = 0
    dir = _sfftw.FFTW_FORWARD
    if a.dtype == _N.float32:
        if af is not None and (not af.flags.carray or af.dtype != _N.complex64):
            raise RuntimeError, "af needs to be well behaved Complex64 array"
        key = ("sr%d"%inplace, shape )

        try:
            p = _splans[ key ]
        except:
            ashape = _N.array(shape, dtype=_N.int32)
            p = _sfftw.rfftwnd_create_plan(len(shape), ashape, dir,
                    _measure | inplace)
            if p is None:
                raise RuntimeError, "could not create plan"
            _splans[ key ] = p

        if inplace:
            #debug print 11111111111111
            _sfftw.rfftwnd_one_real_to_complex(p,a,None)
            if af is None:
                s2 = shape[:-1]+(shape[-1]/2+1,)
                af = _N.ndarray(buffer=a, shape=s2,dtype=_N.complex64)
                return af
        else:
            #debug print "plan", repr(p)
            #debuf print 22222222222222, a.shape
            #debuf print a.flags, a.dtype.isnative
            if af is None:
                s2 = shape[:-1]+(shape[-1]/2+1,)
                af = _N.empty(shape=s2, dtype=_N.complex64)
                #debuf print 33333333322222, af.shape
                #debuf print af.flags, af.dtype.isnative
                _sfftw.rfftwnd_one_real_to_complex(p,a,af)
                return af
            else:
                #debuf print 22222222222222, af.shape
                #debuf print af.flags, af.dtype.isnative
                _sfftw.rfftwnd_one_real_to_complex(p,a,af)  


    elif a.dtype == _N.float64:
        if af is not None and (not af.flags.carray or af.dtype != _N.complex128):
            raise RuntimeError, "af needs to be well behaved Complex64 array"
        key = ("dr%d"%inplace, shape )

        try:
            p = _dplans[ key ]
        except:
            p = _dfftw.rfftwnd_create_plan(len(shape), _N.array(shape, dtype=_N.int32), dir,
                    _measure | inplace)
            if p is None:
                raise RuntimeError, "could not create plan"
            _dplans[ key ] = p

        if inplace:
            _dfftw.rfftwnd_one_real_to_complex(p,a,None)
            if af is None:
                s2 = shape[:-1]+(shape[-1]/2+1,)
                af = _N.ndarray(buffer=a, shape=s2, dtype=_N.complex128)
                return af
        else:
            if af is None:
                s2 = shape[:-1]+(shape[-1]/2+1,)
                af = _N.empty(shape=s2, dtype=_N.complex128)
                _dfftw.rfftwnd_one_real_to_complex(p,a,af)
                return af
            else:
                _dfftw.rfftwnd_one_real_to_complex(p,a,af)  

    else:
        raise TypeError, "(c)float32 and (c)float64 must be used consistently (%s %s)"%\
              ((a is None and "a is None" or "a.dtype=%s"%a.dtype),
               (af is None and "af is None" or "af.dtype=%s"%af.dtype))
Exemplo n.º 3
0
def irfft(af, a=None, inplace=0, copy=1, nthreads=1):
    """if copy==1 (and inplace==0 !!) fftw uses a copy of af to prevent overwriting the original
       (fftw always messes up the input array when inv-fft complex_to_real)
       """
    #CHECK b type size
    global shape, s2

    if copy and not inplace:
        af = af.copy()

    if inplace:
        inplace = _dfftw.FFTW_IN_PLACE  # == 8
        shape = af.shape[:-1] + ((af.shape[-1] - 1) * 2, )
    else:
        shape = af.shape[:-1] + ((af.shape[-1] - 1) * 2, )
        inplace = 0
    dir = _sfftw.FFTW_BACKWARD
    if af.dtype == _N.complex64:
        if a is not None and (not a.flags.carray or a.dtype != _N.float32):
            raise RuntimeError("a needs to be well behaved float32 array")
        key = ("sir%d" % inplace, shape)

        try:
            p = _splans[key]
        except:
            p = _sfftw.rfftwnd_create_plan(len(shape),
                                           _N.array(shape, dtype=_N.int32),
                                           dir, _measure | inplace)
            if p is None:
                raise RuntimeError("could not create plan")
            _splans[key] = p

        if inplace:
            _sfftw.rfftwnd_one_complex_to_real(p, af, None)
            if a is None:
                s2 = shape[:-1] + (shape[-1] + 2, )
                a = _N.ndarray(buffer=af, shape=s2, dtype=_N.float32)
                return a
        else:
            if a is None:
                s2 = shape
                a = _N.empty(shape=s2, dtype=_N.float32)
                _sfftw.rfftwnd_one_complex_to_real(p, af, a)
                return a
            else:
                _sfftw.rfftwnd_one_complex_to_real(p, af, a)

    elif af.dtype == _N.complex128:
        if a is not None and (not a.flags.carray or a.dtype != _N.float64):
            raise RuntimeError("a needs to be well behaved float64 array")
        key = ("dir%d" % inplace, shape)

        try:
            p = _dplans[key]
        except:
            p = _dfftw.rfftwnd_create_plan(len(shape),
                                           _N.array(shape, dtype=_N.int32),
                                           dir, _measure | inplace)
            if p is None:
                raise RuntimeError("could not create plan")
            _dplans[key] = p

        if inplace:
            _dfftw.rfftwnd_one_complex_to_real(p, af, None)
            if a is None:
                s2 = shape[:-1] + (shape[-1] + 2, )
                a = _N.ndarray(buffer=af, shape=s2, dtype=_N.float64)
                return a
        else:
            if a is None:
                s2 = shape
                a = _N.empty(shape=s2, dtype=_N.float64)
                _dfftw.rfftwnd_one_complex_to_real(p, af, a)
                return a
            else:
                _dfftw.rfftwnd_one_complex_to_real(p, af, a)

    else:
        raise TypeError("(c)float32 and (c)float64 must be used consistently (%s %s)"%\
              ((a is None and "a is None" or "a.dtype=%s"%a.dtype),
               (af is None and "af is None" or "af.dtype=%s"%af.dtype)))
Exemplo n.º 4
0
Arquivo: fftw.py Projeto: LLNL/WVL
def irfft(af, a=None, inplace=0, copy=1):
    """if copy==1 (and inplace==0 !!) fftw uses a copy of af to prevent overwriting the original
       (fftw always messes up the input array when inv-fft complex_to_real)
       """
    #CHECK b type size
    global shape,s2

    if copy and not inplace:
        af = af.copy()

    if inplace:
        inplace = _dfftw.FFTW_IN_PLACE     # == 8
        shape = af.shape[:-1] + ((af.shape[-1]-1)*2,)
    else:
        shape = af.shape[:-1] + ((af.shape[-1]-1)*2,)
        inplace = 0
    dir = _sfftw.FFTW_BACKWARD
    if af.dtype == _N.complex64:
        if a is not None and (not a.flags.carray or a.dtype != _N.float32):
            raise RuntimeError, "a needs to be well behaved float32 array"
        key = ("sir%d"%inplace, shape )

        try:
            p = _splans[ key ]
        except:
            p = _sfftw.rfftwnd_create_plan(len(shape), _N.array(shape, dtype=_N.int32), dir,
                    _measure | inplace)
            if p is None:
                raise RuntimeError, "could not create plan"
            _splans[ key ] = p
            

        if inplace:
            _sfftw.rfftwnd_one_complex_to_real(p,af,None)
            if a is None:
                s2 = shape[:-1]+(shape[-1]+2,)
                a = _N.ndarray(buffer=af, shape=s2, dtype=_N.float32)
                return a
        else:
            if a is None:
                s2 = shape
                a = _N.empty(shape=s2, dtype=_N.float32)
                _sfftw.rfftwnd_one_complex_to_real(p,af,a)
                return a
            else:
                _sfftw.rfftwnd_one_complex_to_real(p,af,a)  


    elif af.dtype == _N.complex128:
        if a is not None and (not a.flags.carray or a.dtype != _N.float64):
            raise RuntimeError, "a needs to be well behaved float64 array"
        key = ("dir%d"%inplace, shape )

        try:
            p = _dplans[ key ]
        except:
            p = _dfftw.rfftwnd_create_plan(len(shape), _N.array(shape, dtype=_N.int32), dir,
                    _measure | inplace)
            if p is None:
                raise RuntimeError, "could not create plan"
            _dplans[ key ] = p

        if inplace:
            _dfftw.rfftwnd_one_complex_to_real(p,af,None)
            if a is None:
                s2 = shape[:-1]+(shape[-1]+2,)
                a = _N.ndarray(buffer=af, shape=s2,dtype=_N.float64)
                return a
        else:
            if a is None:
                s2 = shape
                a = _N.empty(shape=s2, dtype=_N.float64)
                _dfftw.rfftwnd_one_complex_to_real(p,af,a)
                return a
            else:
                _dfftw.rfftwnd_one_complex_to_real(p,af,a)  

    else:
        raise TypeError, "(c)float32 and (c)float64 must be used consistently (%s %s)"%\
              ((a is None and "a is None" or "a.dtype=%s"%a.dtype),
               (af is None and "af is None" or "af.dtype=%s"%af.dtype))