Python numpy.fft.rfft() Examples

The following are code examples for showing how to use numpy.fft.rfft(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: Bounded-Kalman-filter-method-for-motion-robust-non-contact-heart-rate-estimation   Author: DataLabPSU   File: Proposed Algorithm_Main Program.py    GNU General Public License v3.0 6 votes vote down vote up
def freq_from_fft(sig, fs):
    
    # Compute Fourier transform of windowed signal
    windowed = sig * blackmanharris(len(sig))
    fe = rfft(windowed)

    # Find the peak and interpolate to get a more accurate peak
    i = argmax(abs(fe))  # Just use this for less-accurate, naive version
    true_i = parabolic(log(abs(fe)), i)[0]

    # Convert to equivalent frequency
    return fs * true_i / len(windowed)



#creates a butter pass filter 
Example 2
Project: powerfit   Author: haddocking   File: test_gpyfft.py    Apache License 2.0 6 votes vote down vote up
def test_rfft_1d(self):
        shape = (8,)
        plan = self.G.create_plan(self.ctx, shape)
        plan.inplace = False
        plan.layouts = (self.CLFFT_REAL, self.CLFFT_HERMITIAN_INTERLEAVED)
        plan.batch_size = 2
        plan.distances = (8, 5)
        plan.bake(self.queue)

        np_in = np.random.rand(16).astype(np.float32)
        np_out = np.zeros(10, dtype=np.complex64)

        answer1 = fft.rfft(np_in[:8])
        answer2 = fft.rfft(np_in[8:])

        cl_in = cl_array.to_device(self.queue, np_in)
        cl_out = cl_array.to_device(self.queue, np_out)
        plan.enqueue_transform(self.queue, cl_in.data, cl_out.data)

        self.assertTrue(np.allclose(answer1, cl_out[:5].get()))
        self.assertTrue(np.allclose(answer2, cl_out[5:].get())) 
Example 3
Project: aces   Author: vanceeasleaf   File: vdos.py    GNU General Public License v2.0 6 votes vote down vote up
def dos_atom(self, id):
        node = '/freq'
        if node not in self.db:
            print('getting %s in h5' % node)
            self.db[node] = self.freq

        node, label = self.inith5('dos_atom', self.totalStep / 2 + 1)
        if not self.db[label][id]:
            print('prepare %s:%d' % (node, id))
            vcf = self.correlate_atom(id)
            vcf = np.average(vcf[:, :3], axis=1)
            vcf0 = vcf[0].copy()
            vcf /= vcf0
            dos = np.abs(rfft(vcf, axis=0))
            self.db[node][id] = dos
            self.db[label][id] = 1
        return self.db[node][id] 
Example 4
Project: parSpectral   Author: JoyMonteiro   File: pSpectral.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def ChebY(self,field):

        N = self.yn - 1;
        z = cos(arange(0, N+1)*pi/N) /self.lengthY;
        mf = arange(0, N+1);
        m = mf[:, newaxis];
        ans = zeros((N+1, self.xn));

        newField = vstack((field, field[N-1:0:-1]));

        temp = rfft(newField, axis=0); 

        multiplier = 1j*m;
        mtemp = multiplier* temp;

        out = irfft(mtemp, axis=0);

        ans[1:N,:] = -out[1:N,:]/ (1- self.cby[1:N, np.newaxis]**2)**0.5; 

        ans[0,:] = sum(m**2 * temp[mf,:], axis=0)/N + 0.5*N*temp[N,:];

        ans[N,:] = sum( (-1)**(m+1) * m**2 * temp[mf,:], axis=0)/N + 0.5*(-1**(N+1))* N * temp[N];

        return ans; 
Example 5
Project: pybold   Author: hcherkaoui   File: convolution.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def spectral_convolve(k, x):
    """ Return k.conv(x).

    Parameters:
    -----------
    k : 1d np.ndarray,
        kernel.
    x : 1d np.ndarray,
        signal.

    Results:
    --------
    k_conv_x : 1d np.ndarray,
        the convolved signal.
    """
    x, p = custom_padd(x)
    N = len(x)
    fft_k = rfft(k, n=N, norm=None)
    padded_h_conv_x = irfft(fft_k * rfft(x,  n=N, norm=None), norm=None)
    k_conv_x = unpadd(padded_h_conv_x, p)

    return k_conv_x 
Example 6
Project: pybold   Author: hcherkaoui   File: convolution.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def spectral_retro_convolve(k, x):
    """ Return k_t.conv(x).

    Parameters:
    -----------
    k : 1d np.ndarray,
        kernel.
    x : 1d np.ndarray,
        signal.

    Results:
    --------
    k_conv_x : 1d np.ndarray,
        the convolved signal.
    """
    x, p = custom_padd(x)
    N = len(x)
    fft_k = rfft(k, n=N, norm=None).conj()
    padded_k_conv_x = irfft(fft_k * rfft(x,  n=N, norm=None), norm=None)
    k_conv_x = unpadd(padded_k_conv_x, p)

    return k_conv_x 
Example 7
Project: pybold   Author: hcherkaoui   File: convolution.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def spectral_deconvolve(k, x):
    """ Return k.conv(x).

    Parameters:
    -----------
    k : 1d np.ndarray,
        kernel.
    x : 1d np.ndarray,
        signal.

    Results:
    --------
    k_conv_x : 1d np.ndarray,
        the convolved signal.
    """
    x, p = custom_padd(x)
    N = len(x)
    fft_k = 1.0 / rfft(k, n=N, norm=None)
    padded_h_conv_x = irfft(fft_k * rfft(x,  n=N, norm=None), norm=None)
    k_conv_x = unpadd(padded_h_conv_x, p)

    return k_conv_x 
Example 8
Project: pybold   Author: hcherkaoui   File: convolution.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def spectral_retro_deconvolve(k, x):
    """ Return k.conv(x).

    Parameters:
    -----------
    k : 1d np.ndarray,
        kernel.
    x : 1d np.ndarray,
        signal.

    Results:
    --------
    k_conv_x : 1d np.ndarray,
        the convolved signal.
    """
    x, p = custom_padd(x)
    N = len(x)
    fft_k = (1.0 / rfft(k, n=N, norm=None)).conj()
    padded_h_conv_x = irfft(fft_k * rfft(x,  n=N, norm=None), norm=None)
    k_conv_x = unpadd(padded_h_conv_x, p)

    return k_conv_x 
Example 9
Project: pyMUSIC   Author: amjadsaadeh   File: pyMUSIC.py    MIT License 6 votes vote down vote up
def STFT(data, nfft, noOverlap=0):
    """
    Applies a STFT on given data of a real signal

    @param data sampled data of the real signal (1-D numpy array)
    @param nfft window size of the fft
    @param noOverlap number of samples the windows should overlap
    @return numpy array, lines are the frequency bins, coloumns are the time window
    """

    assert noOverlap < nfft

    # Amount of windows
    noWindows = data.shape[0] // (nfft - noOverlap)
    stft = np.empty((nfft // 2 + 1, noWindows), dtype=np.complex)
    for i in range(noWindows):
        fft = rfft(data[i * (nfft - noOverlap):i * (nfft - noOverlap) + nfft])
        stft[:, i] = fft

    return stft 
Example 10
Project: acoustics-hardware   Author: AppliedAcousticsChalmers   File: generators.py    MIT License 5 votes vote down vote up
def _fft_noise(self):
        normal = np.random.normal(size=self.device.framesize)
        shaped = ifft(self._spectral_coefficients * fft(normal))
        return shaped 
Example 11
Project: LaserTOF   Author: kyleuckert   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_gaussian_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type in [numpy.float32, numpy.float64]:
                a = numpy.zeros(shape, type)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_gaussian(a, [5.0, 2.5],
                                                       shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1) 
Example 12
Project: LaserTOF   Author: kyleuckert   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_uniform_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type in [numpy.float32, numpy.float64]:
                a = numpy.zeros(shape, type)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_uniform(a, [5.0, 2.5],
                                                      shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1.0) 
Example 13
Project: LaserTOF   Author: kyleuckert   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_shift_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for dtype in [numpy.float32, numpy.float64]:
                expected = numpy.arange(shape[0] * shape[1], dtype=dtype)
                expected.shape = shape
                a = fft.rfft(expected, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_shift(a, [1, 1], shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_array_almost_equal(a[1:, 1:], expected[:-1, :-1])
                assert_array_almost_equal(a.imag, numpy.zeros(shape)) 
Example 14
Project: LaserTOF   Author: kyleuckert   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_ellipsoid_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type in [numpy.float32, numpy.float64]:
                a = numpy.zeros(shape, type)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_ellipsoid(a, [5.0, 2.5],
                                              shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1.0) 
Example 15
Project: pyramic-dataset   Author: fakufaku   File: deconvolution.py    MIT License 5 votes vote down vote up
def plot_spectrum(x, fs, *args, **kwargs):
    n = x.shape[0]
    f = np.linspace(0, 0.5, n // 2 + 1) * fs
    X = np.abs(np.fft.rfft(x, axis=0)) / np.sqrt(n)
    plt.plot(f, 20*np.log10(X), *args, **kwargs) 
Example 16
Project: pyramic-dataset   Author: fakufaku   File: deconvolution.py    MIT License 5 votes vote down vote up
def periodogram(data, p_len, n=None):
    '''
    Compute the Periodogram of the input data

    Parameters
    ----------
    data: ndarray
        The input data
    p_len: int
        The number of points in the analyisis periodogram
    n: int, optional
        Optionaly, the periodogram is linearly resampled to this number of point
    '''

    n_frames = (data.shape[0] // p_len)
    if data.ndim == 1:
        frames = data[:n_frames*p_len].reshape((n_frames,p_len))
    elif data.ndim == 2:
        frames = data[:n_frames*p_len].reshape((n_frames,p_len,-1))
    else:
        raise ValueError('Dimension larger than 2 not supported')

    P = np.mean(np.abs(np.fft.rfft(frames, axis=1) / np.sqrt(p_len))**2, axis=0)

    if n is not None:
        f = np.arange(p_len // 2 + 1) / p_len
        interpolator = interp1d(f, P, kind='linear', axis=0)
        f_long = np.arange(n // 2 + 1) / n
        P = interpolator(f_long)

    return P 
Example 17
Project: pyMatrixProfile   Author: ZiyaoWei   File: util.py    MIT License 5 votes vote down vote up
def mass(query, ts):
    """
    >>> np.round(mass(np.array([0.0, 1.0, -1.0, 0.0]), np.array([-1, 1, 0, 0, -1, 1])), 3)
    array([ 2.   ,  2.828,  2.   ])
    """
    query = zNormalize(query)
    m = len(query)
    n = len(ts)

    stdv = movstd(ts, m)
    query = query[::-1]
    query = np.pad(query, (0, n - m), 'constant')
    dots = fft.irfft(fft.rfft(ts) * fft.rfft(query))
    return np.sqrt(2 * (m - (dots[m - 1 :] / stdv))) 
Example 18
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: bench_basic.py    GNU General Public License v3.0 5 votes vote down vote up
def bench_random(self):
        from numpy.fft import rfft as numpy_rfft
        print()
        print('Fast Fourier Transform (real data)')
        print('==================================')
        print(' size |  scipy  |  numpy  ')
        print('----------------------------------')
        for size,repeat in [(100,7000),(1000,2000),
                            (256,10000),
                            (512,10000),
                            (1024,1000),
                            (2048,1000),
                            (2048*2,500),
                            (2048*4,500),
                            ]:
            print('%5s' % size, end=' ')
            sys.stdout.flush()

            x = random([size]).astype(double)
            print('|%8.2f' % measure('rfft(x)',repeat), end=' ')
            sys.stdout.flush()

            print('|%8.2f' % measure('numpy_rfft(x)',repeat), end=' ')
            sys.stdout.flush()

            print(' (secs for %s calls)' % (repeat))
        sys.stdout.flush() 
Example 19
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_ndimage.py    GNU General Public License v3.0 5 votes vote down vote up
def test_fourier_gaussian_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type in [numpy.float32, numpy.float64]:
                a = numpy.zeros(shape, type)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_gaussian(a, [5.0, 2.5],
                                                       shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1) 
Example 20
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_ndimage.py    GNU General Public License v3.0 5 votes vote down vote up
def test_fourier_uniform_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type in [numpy.float32, numpy.float64]:
                a = numpy.zeros(shape, type)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_uniform(a, [5.0, 2.5],
                                                      shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1.0) 
Example 21
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_ndimage.py    GNU General Public License v3.0 5 votes vote down vote up
def test_fourier_shift_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for dtype in [numpy.float32, numpy.float64]:
                expected = numpy.arange(shape[0] * shape[1], dtype=dtype)
                expected.shape = shape
                a = fft.rfft(expected, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_shift(a, [1, 1], shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_array_almost_equal(a[1:, 1:], expected[:-1, :-1])
                assert_array_almost_equal(a.imag, numpy.zeros(shape)) 
Example 22
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_ndimage.py    GNU General Public License v3.0 5 votes vote down vote up
def test_fourier_ellipsoid_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type in [numpy.float32, numpy.float64]:
                a = numpy.zeros(shape, type)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_ellipsoid(a, [5.0, 2.5],
                                              shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1.0) 
Example 23
Project: ble5-nrf52-mac   Author: tomasero   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_gaussian_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type_, dec in zip([numpy.float32, numpy.float64], [6, 14]):
                a = numpy.zeros(shape, type_)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_gaussian(a, [5.0, 2.5], shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1, decimal=dec) 
Example 24
Project: ble5-nrf52-mac   Author: tomasero   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_uniform_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type_, dec in zip([numpy.float32, numpy.float64], [6, 14]):
                a = numpy.zeros(shape, type_)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_uniform(a, [5.0, 2.5], shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1.0, decimal=dec) 
Example 25
Project: ble5-nrf52-mac   Author: tomasero   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_shift_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type_, dec in zip([numpy.float32, numpy.float64], [4, 11]):
                expected = numpy.arange(shape[0] * shape[1], dtype=type_)
                expected.shape = shape
                a = fft.rfft(expected, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_shift(a, [1, 1], shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_array_almost_equal(a[1:, 1:], expected[:-1, :-1],
                                          decimal=dec)
                assert_array_almost_equal(a.imag, numpy.zeros(shape),
                                          decimal=dec) 
Example 26
Project: ble5-nrf52-mac   Author: tomasero   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_ellipsoid_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type_, dec in zip([numpy.float32, numpy.float64], [5, 14]):
                a = numpy.zeros(shape, type_)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_ellipsoid(a, [5.0, 2.5],
                                              shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1.0, decimal=dec) 
Example 27
Project: Computable   Author: ktraunmueller   File: bench_basic.py    MIT License 5 votes vote down vote up
def bench_random(self):
        from numpy.fft import rfft as numpy_rfft
        print()
        print('Fast Fourier Transform (real data)')
        print('==================================')
        print(' size |  scipy  |  numpy  ')
        print('----------------------------------')
        for size,repeat in [(100,7000),(1000,2000),
                            (256,10000),
                            (512,10000),
                            (1024,1000),
                            (2048,1000),
                            (2048*2,500),
                            (2048*4,500),
                            ]:
            print('%5s' % size, end=' ')
            sys.stdout.flush()

            x = random([size]).astype(double)
            print('|%8.2f' % measure('rfft(x)',repeat), end=' ')
            sys.stdout.flush()

            print('|%8.2f' % measure('numpy_rfft(x)',repeat), end=' ')
            sys.stdout.flush()

            print(' (secs for %s calls)' % (repeat))
        sys.stdout.flush() 
Example 28
Project: Computable   Author: ktraunmueller   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_gaussian_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type in [numpy.float32, numpy.float64]:
                a = numpy.zeros(shape, type)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_gaussian(a, [5.0, 2.5],
                                                       shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1) 
Example 29
Project: Computable   Author: ktraunmueller   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_uniform_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type in [numpy.float32, numpy.float64]:
                a = numpy.zeros(shape, type)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_uniform(a, [5.0, 2.5],
                                                      shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1.0) 
Example 30
Project: Computable   Author: ktraunmueller   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_shift_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for dtype in [numpy.float32, numpy.float64]:
                expected = numpy.arange(shape[0] * shape[1], dtype=dtype)
                expected.shape = shape
                a = fft.rfft(expected, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_shift(a, [1, 1], shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_array_almost_equal(a[1:, 1:], expected[:-1, :-1])
                assert_array_almost_equal(a.imag, numpy.zeros(shape)) 
Example 31
Project: Computable   Author: ktraunmueller   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_ellipsoid_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type in [numpy.float32, numpy.float64]:
                a = numpy.zeros(shape, type)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_ellipsoid(a, [5.0, 2.5],
                                              shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1.0) 
Example 32
Project: poker   Author: surgebiswas   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_gaussian_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type in [numpy.float32, numpy.float64]:
                a = numpy.zeros(shape, type)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_gaussian(a, [5.0, 2.5],
                                                       shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1) 
Example 33
Project: poker   Author: surgebiswas   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_uniform_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type in [numpy.float32, numpy.float64]:
                a = numpy.zeros(shape, type)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_uniform(a, [5.0, 2.5],
                                                      shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1.0) 
Example 34
Project: poker   Author: surgebiswas   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_shift_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for dtype in [numpy.float32, numpy.float64]:
                expected = numpy.arange(shape[0] * shape[1], dtype=dtype)
                expected.shape = shape
                a = fft.rfft(expected, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_shift(a, [1, 1], shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_array_almost_equal(a[1:, 1:], expected[:-1, :-1])
                assert_array_almost_equal(a.imag, numpy.zeros(shape)) 
Example 35
Project: poker   Author: surgebiswas   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_ellipsoid_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type in [numpy.float32, numpy.float64]:
                a = numpy.zeros(shape, type)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_ellipsoid(a, [5.0, 2.5],
                                              shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1.0) 
Example 36
Project: P3_image_processing   Author: latedude2   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_gaussian_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type_, dec in zip([numpy.float32, numpy.float64], [6, 14]):
                a = numpy.zeros(shape, type_)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_gaussian(a, [5.0, 2.5], shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1, decimal=dec) 
Example 37
Project: P3_image_processing   Author: latedude2   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_uniform_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type_, dec in zip([numpy.float32, numpy.float64], [6, 14]):
                a = numpy.zeros(shape, type_)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_uniform(a, [5.0, 2.5], shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1.0, decimal=dec) 
Example 38
Project: P3_image_processing   Author: latedude2   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_shift_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type_, dec in zip([numpy.float32, numpy.float64], [4, 11]):
                expected = numpy.arange(shape[0] * shape[1], dtype=type_)
                expected.shape = shape
                a = fft.rfft(expected, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_shift(a, [1, 1], shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_array_almost_equal(a[1:, 1:], expected[:-1, :-1],
                                          decimal=dec)
                assert_array_almost_equal(a.imag, numpy.zeros(shape),
                                          decimal=dec) 
Example 39
Project: P3_image_processing   Author: latedude2   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_ellipsoid_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type_, dec in zip([numpy.float32, numpy.float64], [5, 14]):
                a = numpy.zeros(shape, type_)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_ellipsoid(a, [5.0, 2.5],
                                              shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1.0, decimal=dec) 
Example 40
Project: BabyMonitor   Author: 0x1001   File: analyzer.py    GNU General Public License v2.0 5 votes vote down vote up
def _fft(self, audio_data):
        from numpy import fft

        amp = fft.rfft(audio_data)
        freq = fft.fftfreq(audio_data.shape[-1])[:len(amp)]

        return freq, amp.real 
Example 41
Project: SimpleMC   Author: ja-vazquez   File: cosmich.py    GNU General Public License v2.0 5 votes vote down vote up
def smline(x, y, mnmx):

    # first lets.pad
    y = log(y+1e-30)
    N = len(y)
    y = array([y[0]]*N+list(y)+[y[-1]]*N)
    rft = fft.rfft(y)
    Nx = len(rft)
    k = linspace(0, 1, Nx)
    rft *= exp(-k*k/(2*0.2**2))
    y = fft.irfft(rft)
    y = y[N:2*N]
    y = exp(y)
    return x, y 
Example 42
Project: SimpleMC   Author: ja-vazquez   File: cosmich.py    GNU General Public License v2.0 5 votes vote down vote up
def smline(x, y, mnmx):

    # first lets.pad
    y = log(y+1e-30)
    N = len(y)
    y = array([y[0]]*N+list(y)+[y[-1]]*N)
    rft = fft.rfft(y)
    Nx = len(rft)
    k = linspace(0, 1, Nx)
    rft *= exp(-k*k/(2*0.2**2))
    y = fft.irfft(rft)
    y = y[N:2*N]
    y = exp(y)
    return x, y 
Example 43
Project: SimpleMC   Author: ja-vazquez   File: cosmich.py    GNU General Public License v2.0 5 votes vote down vote up
def smline(x, y, mnmx):

    # first lets.pad
    y = log(y+1e-30)
    N = len(y)
    y = array([y[0]]*N+list(y)+[y[-1]]*N)
    rft = fft.rfft(y)
    Nx = len(rft)
    k = linspace(0, 1, Nx)
    rft *= exp(-k*k/(2*0.2**2))
    y = fft.irfft(rft)
    y = y[N:2*N]
    y = exp(y)
    return x, y 
Example 44
Project: alona   Author: oscar-franzen   File: irlb.py    GNU General Public License v3.0 5 votes vote down vote up
def multS(s, v, L, TP=False):

    N = s.shape[0]
    vp = prepare_v(v, N, L, TP=TP)
    p = irfft(rfft(vp) * rfft(s))
    if not TP:
        return p[:L]
    return p[L - 1:] 
Example 45
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_gaussian_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type_, dec in zip([numpy.float32, numpy.float64], [6, 14]):
                a = numpy.zeros(shape, type_)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_gaussian(a, [5.0, 2.5], shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1, decimal=dec) 
Example 46
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_uniform_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type_, dec in zip([numpy.float32, numpy.float64], [6, 14]):
                a = numpy.zeros(shape, type_)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_uniform(a, [5.0, 2.5], shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1.0, decimal=dec) 
Example 47
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_shift_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type_, dec in zip([numpy.float32, numpy.float64], [4, 11]):
                expected = numpy.arange(shape[0] * shape[1], dtype=type_)
                expected.shape = shape
                a = fft.rfft(expected, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_shift(a, [1, 1], shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_array_almost_equal(a[1:, 1:], expected[:-1, :-1],
                                          decimal=dec)
                assert_array_almost_equal(a.imag, numpy.zeros(shape),
                                          decimal=dec) 
Example 48
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_ndimage.py    MIT License 5 votes vote down vote up
def test_fourier_ellipsoid_real01(self):
        for shape in [(32, 16), (31, 15)]:
            for type_, dec in zip([numpy.float32, numpy.float64], [5, 14]):
                a = numpy.zeros(shape, type_)
                a[0, 0] = 1.0
                a = fft.rfft(a, shape[0], 0)
                a = fft.fft(a, shape[1], 1)
                a = ndimage.fourier_ellipsoid(a, [5.0, 2.5],
                                              shape[0], 0)
                a = fft.ifft(a, shape[1], 1)
                a = fft.irfft(a, shape[0], 0)
                assert_almost_equal(ndimage.sum(a), 1.0, decimal=dec) 
Example 49
Project: ml4ms   Author: uqzzhao   File: sigproc.py    MIT License 5 votes vote down vote up
def peakfreq_from_fft(sig, fs):
    """
    Estimate frequency from peak of FFT
    """
    # Compute Fourier transform of windowed signal
    windowed = sig * blackmanharris(len(sig))
    f = rfft(windowed)

    # Find the peak and interpolate to get a more accurate peak
    i = argmax(abs(f)) - 1 # Just use this for less-accurate, naive version
    true_i = parabolic(log(abs(f)), i)[0]

    # Convert to equivalent frequency
    return fs * true_i / len(windowed) 
Example 50
Project: ComputationalNeurodynamics   Author: pmediano   File: PowerSpectrum.py    GNU General Public License v3.0 5 votes vote down vote up
def PowerSpectrum(X, Fs):
  """
  Calculate the power spectrum of real-valued signal X measured with a
  sampling frequency Fs. Result is in the same units as Fs.
  """

  X = X - X.mean()
  F = rfft(X)[:-1]
  freq = np.arange(0, 0.5*Fs, Fs*1.0/len(X))
  pw = np.abs(F)**2
  return freq, pw/max(pw)