# 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
```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
```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)

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)

Example 3
 Project: aces   Author: vanceeasleaf   File: vdos.py    GNU General Public License v2.0 6 votes
```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
```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
```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.
"""
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)

return k_conv_x ```
Example 6
 Project: pybold   Author: hcherkaoui   File: convolution.py    BSD 3-Clause "New" or "Revised" License 6 votes
```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.
"""
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)

return k_conv_x ```
Example 7
 Project: pybold   Author: hcherkaoui   File: convolution.py    BSD 3-Clause "New" or "Revised" License 6 votes
```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.
"""
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)

return k_conv_x ```
Example 8
 Project: pybold   Author: hcherkaoui   File: convolution.py    BSD 3-Clause "New" or "Revised" License 6 votes
```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.
"""
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)

return k_conv_x ```
Example 9
```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
```def _fft_noise(self):
normal = np.random.normal(size=self.device.framesize)
shaped = ifft(self._spectral_coefficients * fft(normal))
return shaped ```
Example 11
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```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
```def smline(x, y, mnmx):

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
```def smline(x, y, mnmx):

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
```def smline(x, y, mnmx):

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
```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
```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
```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
```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
```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
```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
```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) ```