本文整理匯總了Python中scipy.fft方法的典型用法代碼示例。如果您正苦於以下問題:Python scipy.fft方法的具體用法?Python scipy.fft怎麽用?Python scipy.fft使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scipy
的用法示例。
在下文中一共展示了scipy.fft方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ihfft
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import fft [as 別名]
def ihfft(x, n=None, axis=-1, norm=None, overwrite_x=False, *, plan=None):
"""Compute the FFT of a signal that has Hermitian symmetry.
Args:
a (cupy.ndarray): Array to be transform.
n (None or int): Number of points along transformation axis in the
input to use. If ``n`` is not given, the length of the input along
the axis specified by ``axis`` is used.
axis (int): Axis over which to compute the FFT.
norm (None or ``"ortho"``): Keyword to specify the normalization mode.
overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
plan (None): This argument is currently not supported.
Returns:
cupy.ndarray:
The transformed array which shape is specified by ``n`` and type
will convert to complex if the input is other. The length of the
transformed axis is ``n//2+1``.
.. seealso:: :func:`scipy.fft.ihfft`
"""
# TODO(leofang): support R2C & C2R plans
if plan is not None:
raise NotImplementedError('ihfft plan is currently not yet supported')
return _ihfft(x, n, axis, norm)
示例2: _stft
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import fft [as 別名]
def _stft(self, stim):
x = stim.data
framesamp = int(self.frame_size * stim.sampling_rate)
hopsamp = int(self.hop_size * stim.sampling_rate)
w = np.hanning(framesamp)
X = np.array([fft(w * x[i:(i + framesamp)])
for i in range(0, len(x) - framesamp, hopsamp)])
nyquist_lim = int(X.shape[1] // 2)
X = np.log(X[:, :nyquist_lim])
X = np.absolute(X)
if self.spectrogram:
import matplotlib.pyplot as plt
bins = np.fft.fftfreq(framesamp, d=1. / stim.sampling_rate)
bins = bins[:nyquist_lim]
plt.imshow(X.T, origin='lower', aspect='auto',
interpolation='nearest', cmap='RdYlBu_r',
extent=[0, stim.duration, bins.min(), bins.max()])
plt.xlabel('Time')
plt.ylabel('Frequency')
plt.colorbar()
plt.show()
return X
示例3: write_fft
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import fft [as 別名]
def write_fft(fft_features, fn):
"""
Write the FFT features to separate files to speed up processing.
"""
base_fn, ext = os.path.splitext(fn)
data_fn = base_fn + ".fft"
np.save(data_fn, fft_features)
print("Written "%data_fn)
開發者ID:PacktPublishing,項目名稱:Building-Machine-Learning-Systems-With-Python-Second-Edition,代碼行數:11,代碼來源:fft.py
示例4: create_fft
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import fft [as 別名]
def create_fft(fn):
sample_rate, X = scipy.io.wavfile.read(fn)
fft_features = abs(scipy.fft(X)[:1000])
write_fft(fft_features, fn)
開發者ID:PacktPublishing,項目名稱:Building-Machine-Learning-Systems-With-Python-Second-Edition,代碼行數:7,代碼來源:fft.py
示例5: read_fft
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import fft [as 別名]
def read_fft(genre_list, base_dir=GENRE_DIR):
X = []
y = []
for label, genre in enumerate(genre_list):
genre_dir = os.path.join(base_dir, genre, "*.fft.npy")
file_list = glob.glob(genre_dir)
assert(file_list), genre_dir
for fn in file_list:
fft_features = np.load(fn)
X.append(fft_features[:2000])
y.append(label)
return np.array(X), np.array(y)
開發者ID:PacktPublishing,項目名稱:Building-Machine-Learning-Systems-With-Python-Second-Edition,代碼行數:16,代碼來源:fft.py
示例6: plot_wav_fft
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import fft [as 別名]
def plot_wav_fft(wav_filename, desc=None):
plt.clf()
plt.figure(num=None, figsize=(6, 4))
sample_rate, X = scipy.io.wavfile.read(wav_filename)
spectrum = np.fft.fft(X)
freq = np.fft.fftfreq(len(X), 1.0 / sample_rate)
plt.subplot(211)
num_samples = 200.0
plt.xlim(0, num_samples / sample_rate)
plt.xlabel("time [s]")
plt.title(desc or wav_filename)
plt.plot(np.arange(num_samples) / sample_rate, X[:num_samples])
plt.grid(True)
plt.subplot(212)
plt.xlim(0, 5000)
plt.xlabel("frequency [Hz]")
plt.xticks(np.arange(5) * 1000)
if desc:
desc = desc.strip()
fft_desc = desc[0].lower() + desc[1:]
else:
fft_desc = wav_filename
plt.title("FFT of %s" % fft_desc)
plt.plot(freq, abs(spectrum), linewidth=5)
plt.grid(True)
plt.tight_layout()
rel_filename = os.path.split(wav_filename)[1]
plt.savefig("%s_wav_fft.png" % os.path.splitext(rel_filename)[0],
bbox_inches='tight')
plt.show()
開發者ID:PacktPublishing,項目名稱:Building-Machine-Learning-Systems-With-Python-Second-Edition,代碼行數:37,代碼來源:fft.py
示例7: aggregate_raw_batch
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import fft [as 別名]
def aggregate_raw_batch(self, features, output):
"""Aggregate batch.
All post processing goes here.
Parameters:
-----------
features : 3D float tensor
Input tensor
output : 2D integer tensor
Output classes
"""
channels = 2 if self.complex_ else 1
features_out = numpy.zeros(
[features.shape[0], self.window_size, channels])
if self.fourier:
if self.complex_:
data = fft(features, axis=1)
features_out[:, :, 0] = numpy.real(data[:, :, 0])
features_out[:, :, 1] = numpy.imag(data[:, :, 0])
else:
data = numpy.abs(fft(features, axis=1))
features_out = data
elif self.stft:
_, _, data = stft(features, nperseg=120, noverlap=60, axis=1)
length = data.shape[1]
n_feats = data.shape[3]
if self.complex_:
features_out = numpy.zeros(
[len(self.train_data), length, n_feats * 2])
features_out[:, :, :n_feats] = numpy.real(data)
features_out[:, :, n_feats:] = numpy.imag(data)
else:
features_out = numpy.abs(data[:, :, 0, :])
else:
features_out = features
return features_out, output
示例8: fft
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import fft [as 別名]
def fft(x, n=None, axis=-1, norm=None, overwrite_x=False, *, plan=None):
"""Compute the one-dimensional FFT.
Args:
x (cupy.ndarray): Array to be transformed.
n (None or int): Length of the transformed axis of the output. If ``n``
is not given, the length of the input along the axis specified by
``axis`` is used.
axis (int): Axis over which to compute the FFT.
norm (None or ``'ortho'``): Normalization mode.
overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
plan (:class:`cupy.cuda.cufft.Plan1d` or ``None``): a cuFFT plan for
transforming ``x`` over ``axis``, which can be obtained using::
plan = cupyx.scipy.fftpack.get_fft_plan(x, n, axis)
Note that ``plan`` is defaulted to ``None``, meaning CuPy will use
an auto-generated plan behind the scene.
Returns:
cupy.ndarray:
The transformed array which shape is specified by ``n`` and type
will convert to complex if that of the input is another.
.. seealso:: :func:`scipy.fft.fft`
"""
return _fft(x, (n,), (axis,), norm, cufft.CUFFT_FORWARD,
overwrite_x=overwrite_x, plan=plan)
示例9: ifft
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import fft [as 別名]
def ifft(x, n=None, axis=-1, norm=None, overwrite_x=False, *, plan=None):
"""Compute the one-dimensional inverse FFT.
Args:
x (cupy.ndarray): Array to be transformed.
n (None or int): Length of the transformed axis of the output. If ``n``
is not given, the length of the input along the axis specified by
``axis`` is used.
axis (int): Axis over which to compute the FFT.
norm (None or ``'ortho'``): Normalization mode.
overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
plan (:class:`cupy.cuda.cufft.Plan1d` or ``None``): a cuFFT plan for
transforming ``x`` over ``axis``, which can be obtained using::
plan = cupyx.scipy.fftpack.get_fft_plan(x, n, axis)
Note that ``plan`` is defaulted to ``None``, meaning CuPy will use
an auto-generated plan behind the scene.
Returns:
cupy.ndarray:
The transformed array which shape is specified by ``n`` and type
will convert to complex if that of the input is another.
.. seealso:: :func:`scipy.fft.ifft`
"""
return _fft(x, (n,), (axis,), norm, cufft.CUFFT_INVERSE,
overwrite_x=overwrite_x, plan=plan)
示例10: fft2
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import fft [as 別名]
def fft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, *, plan=None):
"""Compute the two-dimensional FFT.
Args:
x (cupy.ndarray): Array to be transformed.
s (None or tuple of ints): Shape of the transformed axes of the
output. If ``s`` is not given, the lengths of the input along
the axes specified by ``axes`` are used.
axes (tuple of ints): Axes over which to compute the FFT.
norm (None or ``'ortho'``): Normalization mode.
overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
plan (:class:`cupy.cuda.cufft.PlanNd` or ``None``): a cuFFT plan for
transforming ``x`` over ``axes``, which can be obtained using::
plan = cupyx.scipy.fftpack.get_fft_plan(x, s, axes)
Note that ``plan`` is defaulted to ``None``, meaning CuPy will use
an auto-generated plan behind the scene.
Returns:
cupy.ndarray:
The transformed array which shape is specified by ``s`` and
type will convert to complex if that of the input is another.
.. seealso:: :func:`scipy.fft.fft2`
"""
return fftn(x, s, axes, norm, overwrite_x, plan=plan)
示例11: ifft2
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import fft [as 別名]
def ifft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, *,
plan=None):
"""Compute the two-dimensional inverse FFT.
Args:
x (cupy.ndarray): Array to be transformed.
s (None or tuple of ints): Shape of the transformed axes of the
output. If ``s`` is not given, the lengths of the input along
the axes specified by ``axes`` are used.
axes (tuple of ints): Axes over which to compute the FFT.
norm (None or ``'ortho'``): Normalization mode.
overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
plan (:class:`cupy.cuda.cufft.PlanNd` or ``None``): a cuFFT plan for
transforming ``x`` over ``axes``, which can be obtained using::
plan = cupyx.scipy.fftpack.get_fft_plan(x, s, axes)
Note that ``plan`` is defaulted to ``None``, meaning CuPy will use
an auto-generated plan behind the scene.
Returns:
cupy.ndarray:
The transformed array which shape is specified by ``s`` and
type will convert to complex if that of the input is another.
.. seealso:: :func:`scipy.fft.ifft2`
"""
return ifftn(x, s, axes, norm, overwrite_x, plan=plan)
示例12: fftn
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import fft [as 別名]
def fftn(x, s=None, axes=None, norm=None, overwrite_x=False, *, plan=None):
"""Compute the N-dimensional FFT.
Args:
x (cupy.ndarray): Array to be transformed.
s (None or tuple of ints): Shape of the transformed axes of the
output. If ``s`` is not given, the lengths of the input along
the axes specified by ``axes`` are used.
axes (tuple of ints): Axes over which to compute the FFT.
norm (None or ``'ortho'``): Normalization mode.
overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
plan (:class:`cupy.cuda.cufft.PlanNd` or ``None``): a cuFFT plan for
transforming ``x`` over ``axes``, which can be obtained using::
plan = cupyx.scipy.fftpack.get_fft_plan(x, s, axes)
Note that ``plan`` is defaulted to ``None``, meaning CuPy will use
an auto-generated plan behind the scene.
Returns:
cupy.ndarray:
The transformed array which shape is specified by ``s`` and
type will convert to complex if that of the input is another.
.. seealso:: :func:`scipy.fft.fftn`
"""
s = _assequence(s)
axes = _assequence(axes)
func = _default_fft_func(x, s, axes)
return func(x, s, axes, norm, cufft.CUFFT_FORWARD, overwrite_x=overwrite_x,
plan=plan)
示例13: rfft
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import fft [as 別名]
def rfft(x, n=None, axis=-1, norm=None, overwrite_x=False, *, plan=None):
"""Compute the one-dimensional FFT for real input.
The returned array contains the positive frequency components of the
corresponding :func:`fft`, up to and including the Nyquist frequency.
Args:
x (cupy.ndarray): Array to be transformed.
n (None or int): Length of the transformed axis of the output. If ``n``
is not given, the length of the input along the axis specified by
``axis`` is used.
axis (int): Axis over which to compute the FFT.
norm (None or ``'ortho'``): Normalization mode.
overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
plan (:class:`cupy.cuda.cufft.Plan1d` or ``None``): a cuFFT plan for
transforming ``x`` over ``axis``, which can be obtained using::
plan = cupyx.scipy.fftpack.get_fft_plan(x, n, axis,
value_type='R2C')
Note that ``plan`` is defaulted to ``None``, meaning CuPy will use
an auto-generated plan behind the scene.
Returns:
cupy.ndarray:
The transformed array.
.. seealso:: :func:`scipy.fft.rfft`
"""
return _fft(x, (n,), (axis,), norm, cufft.CUFFT_FORWARD, 'R2C',
overwrite_x=overwrite_x, plan=plan)
示例14: irfft
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import fft [as 別名]
def irfft(x, n=None, axis=-1, norm=None, overwrite_x=False, *, plan=None):
"""Compute the one-dimensional inverse FFT for real input.
Args:
x (cupy.ndarray): Array to be transformed.
n (None or int): Length of the transformed axis of the output. If ``n``
is not given, the length of the input along the axis specified by
``axis`` is used.
axis (int): Axis over which to compute the FFT.
norm (None or ``'ortho'``): Normalization mode.
overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
plan (:class:`cupy.cuda.cufft.Plan1d` or ``None``): a cuFFT plan for
transforming ``x`` over ``axis``, which can be obtained using::
plan = cupyx.scipy.fftpack.get_fft_plan(x, n, axis,
value_type='C2R')
Note that ``plan`` is defaulted to ``None``, meaning CuPy will use
an auto-generated plan behind the scene.
Returns:
cupy.ndarray:
The transformed array.
.. seealso:: :func:`scipy.fft.irfft`
"""
return _fft(x, (n,), (axis,), norm, cufft.CUFFT_INVERSE, 'C2R',
overwrite_x=overwrite_x, plan=plan)
示例15: rfft2
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import fft [as 別名]
def rfft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, *,
plan=None):
"""Compute the two-dimensional FFT for real input.
Args:
a (cupy.ndarray): Array to be transform.
s (None or tuple of ints): Shape to use from the input. If ``s`` is not
given, the lengths of the input along the axes specified by
``axes`` are used.
axes (tuple of ints): Axes over which to compute the FFT.
norm (None or ``"ortho"``): Keyword to specify the normalization mode.
overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
plan (:class:`cupy.cuda.cufft.PlanNd` or ``None``): a cuFFT plan for
transforming ``x`` over ``axes``, which can be obtained using::
plan = cupyx.scipy.fftpack.get_fft_plan(x, s, axes,
value_type='R2C')
Note that ``plan`` is defaulted to ``None``, meaning CuPy will use
an auto-generated plan behind the scene.
Returns:
cupy.ndarray:
The transformed array which shape is specified by ``s`` and type
will convert to complex if the input is other. The length of the
last axis transformed will be ``s[-1]//2+1``.
.. seealso:: :func:`scipy.fft.rfft2`
"""
return rfftn(x, s, axes, norm, overwrite_x, plan=plan)