예제 #1
0
def test_split_combine_complex(some_thr):

    i1 = get_test_array((1000, ), numpy.float32)
    i2 = get_test_array((1000, ), numpy.float32)
    i1_dev = some_thr.to_device(i1)
    i2_dev = some_thr.to_device(i2)
    o1_dev = some_thr.empty_like(i1)
    o2_dev = some_thr.empty_like(i2)

    base_t = Type(numpy.complex64, shape=1000)
    test = get_test_computation(base_t)
    combine = tr.combine_complex(base_t)
    split = tr.split_complex(base_t)

    test.parameter.input.connect(combine,
                                 combine.output,
                                 i_real=combine.real,
                                 i_imag=combine.imag)
    test.parameter.output.connect(split,
                                  split.input,
                                  o_real=split.real,
                                  o_imag=split.imag)
    testc = test.compile(some_thr)

    testc(o1_dev, o2_dev, i1_dev, i2_dev)
    assert diff_is_negligible(o1_dev.get(), i1)
    assert diff_is_negligible(o2_dev.get(), i2)
예제 #2
0
파일: gpu.py 프로젝트: ioxuy/Jamais-Vu
    def __init__(self,
                 x,
                 NFFT=256,
                 noverlap=128,
                 pad_to=None,
                 window=hanning_window):

        # print("x Data type = %s" % x.dtype)
        # print("Is Real = %s" % dtypes.is_real(x.dtype))
        # print("dim = %s" % x.ndim)
        assert dtypes.is_real(x.dtype)
        assert x.ndim == 1

        rolling_frame_trf = rolling_frame(x, NFFT, noverlap, pad_to)

        complex_dtype = dtypes.complex_for(x.dtype)
        fft_arr = Type(complex_dtype, rolling_frame_trf.output.shape)
        real_fft_arr = Type(x.dtype, rolling_frame_trf.output.shape)

        window_trf = window(real_fft_arr, NFFT)
        broadcast_zero_trf = transformations.broadcast_const(real_fft_arr, 0)
        to_complex_trf = transformations.combine_complex(fft_arr)
        amplitude_trf = transformations.norm_const(fft_arr, 1)
        crop_trf = crop_frequencies(amplitude_trf.output)

        fft = FFT(fft_arr, axes=(1, ))
        fft.parameter.input.connect(to_complex_trf,
                                    to_complex_trf.output,
                                    input_real=to_complex_trf.real,
                                    input_imag=to_complex_trf.imag)
        fft.parameter.input_imag.connect(broadcast_zero_trf,
                                         broadcast_zero_trf.output)
        fft.parameter.input_real.connect(window_trf,
                                         window_trf.output,
                                         unwindowed_input=window_trf.input)
        fft.parameter.unwindowed_input.connect(
            rolling_frame_trf,
            rolling_frame_trf.output,
            flat_input=rolling_frame_trf.input)
        fft.parameter.output.connect(amplitude_trf,
                                     amplitude_trf.input,
                                     amplitude=amplitude_trf.output)
        fft.parameter.amplitude.connect(crop_trf,
                                        crop_trf.input,
                                        cropped_amplitude=crop_trf.output)

        self._fft = fft

        self._transpose = Transpose(fft.parameter.cropped_amplitude)

        Computation.__init__(self, [
            Parameter('output',
                      Annotation(self._transpose.parameter.output, 'o')),
            Parameter('input', Annotation(fft.parameter.flat_input, 'i'))
        ])
예제 #3
0
    def rfft(self, a, nthreads=ncpu):
        a = self.check_array(a, RTYPES, RTYPE)
        
        if SCIK and self.is_gpu_memory_enough(a):
            shape = [s for s in a.shape]
            shape[-1] = shape[-1]//2 + 1
            dtype = G_RTYPES[a.dtype.type]
            func = fft.fft
            af = self._fft_scik(a, func, shape, dtype)
            
        elif REIK and self.is_gpu_memory_enough(a):
            thr = self.api.Thread(self.dev)

            plan  = FFT(Type(complex_for(a.dtype), a.shape))

            # combines two real-valued inputs into a complex-valued input of the same shape
            cc = combine_complex(plan.parameter.input)
            # supplies a constant output
            bc = broadcast_const(cc.imag, 0)

            plan.parameter.input.connect(cc, cc.output, real_input=cc.real, imag_input=cc.imag)
            plan.parameter.imag_input.connect(bc, bc.output)

            fftc = plan.compile(thr, fast_math=True)

            a_dev   = thr.to_device(a)
            a_out_dev = thr.empty_like(plan.parameter.output)

            fftc(a_out_dev, a_dev)

            af = a_out_dev.get()
            af = N.fft.fftshift(af)

        elif FFTW:
            func = pyfftw.builders.rfftn

            af = self._fftw(a, func, nthreads)
        else:
            af = N.fft.rfftn(a)

        return af
예제 #4
0
    def rfft(self, a, nthreads=ncpu):
        a = self.check_array(a, RTYPES, RTYPE)
        
        if SCIK and self.is_gpu_memory_enough(a):
            shape = [s for s in a.shape]
            shape[-1] = shape[-1]//2 + 1
            dtype = G_RTYPES[a.dtype.type]
            func = fft.fft
            af = self._fft_scik(a, func, shape, dtype)
            
        elif REIK and self.is_gpu_memory_enough(a):
            thr = self.api.Thread(self.dev)

            plan  = FFT(Type(complex_for(a.dtype), a.shape))

            # combines two real-valued inputs into a complex-valued input of the same shape
            cc = combine_complex(plan.parameter.input)
            # supplies a constant output
            bc = broadcast_const(cc.imag, 0)

            plan.parameter.input.connect(cc, cc.output, real_input=cc.real, imag_input=cc.imag)
            plan.parameter.imag_input.connect(bc, bc.output)

            fftc = plan.compile(thr, fast_math=True)

            a_dev   = thr.to_device(a)
            a_out_dev = thr.empty_like(plan.parameter.output)

            fftc(a_out_dev, a_dev)

            af = a_out_dev.get()
            af = N.fft.fftshift(af)

        elif FFTW:
            func = pyfftw.builders.rfftn

            af = self._fftw(a, func, nthreads)
        else:
            af = N.fft.rfftn(a)

        return af
예제 #5
0
def test_split_combine_complex(some_thr):

    i1 = get_test_array((1000,), numpy.float32)
    i2 = get_test_array((1000,), numpy.float32)
    i1_dev = some_thr.to_device(i1)
    i2_dev = some_thr.to_device(i2)
    o1_dev = some_thr.empty_like(i1)
    o2_dev = some_thr.empty_like(i2)

    base_t = Type(numpy.complex64, shape=1000)
    test = get_test_computation(base_t)
    combine = tr.combine_complex(base_t)
    split = tr.split_complex(base_t)

    test.parameter.input.connect(combine, combine.output, i_real=combine.real, i_imag=combine.imag)
    test.parameter.output.connect(split, split.input, o_real=split.real, o_imag=split.imag)
    testc = test.compile(some_thr)

    testc(o1_dev, o2_dev, i1_dev, i2_dev)
    assert diff_is_negligible(o1_dev.get(), i1)
    assert diff_is_negligible(o2_dev.get(), i2)
예제 #6
0
    def __init__(self, x, NFFT=256, noverlap=128, pad_to=None, window=hanning_window):

        assert dtypes.is_real(x.dtype)
        assert x.ndim == 1

        rolling_frame_trf = rolling_frame(x, NFFT, noverlap, pad_to)

        complex_dtype = dtypes.complex_for(x.dtype)
        fft_arr = Type(complex_dtype, rolling_frame_trf.output.shape)
        real_fft_arr = Type(x.dtype, rolling_frame_trf.output.shape)

        window_trf = window(real_fft_arr, NFFT)
        broadcast_zero_trf = transformations.broadcast_const(real_fft_arr, 0)
        to_complex_trf = transformations.combine_complex(fft_arr)
        amplitude_trf = transformations.norm_const(fft_arr, 1)
        crop_trf = crop_frequencies(amplitude_trf.output)

        fft = FFT(fft_arr, axes=(1,))
        fft.parameter.input.connect(
            to_complex_trf, to_complex_trf.output,
            input_real=to_complex_trf.real, input_imag=to_complex_trf.imag)
        fft.parameter.input_imag.connect(
            broadcast_zero_trf, broadcast_zero_trf.output)
        fft.parameter.input_real.connect(
            window_trf, window_trf.output, unwindowed_input=window_trf.input)
        fft.parameter.unwindowed_input.connect(
            rolling_frame_trf, rolling_frame_trf.output, flat_input=rolling_frame_trf.input)
        fft.parameter.output.connect(
            amplitude_trf, amplitude_trf.input, amplitude=amplitude_trf.output)
        fft.parameter.amplitude.connect(
            crop_trf, crop_trf.input, cropped_amplitude=crop_trf.output)

        self._fft = fft

        self._transpose = Transpose(fft.parameter.cropped_amplitude)

        Computation.__init__(self,
            [Parameter('output', Annotation(self._transpose.parameter.output, 'o')),
            Parameter('input', Annotation(fft.parameter.flat_input, 'i'))])