假设我有一个示例信号,该信号由三个余弦组成,每个余弦分别代表4、6和8频段。现在,我使用FFT将信号放入频域,在频域中,我切断了不想要的6 Hz频带。最后,我想将信号从频域逆转回时域。但是当我只使用numpy.fft.ifft时,我得到的是复数数组,这不是进一步分析信号的最佳结果。在执行带通后如何对FFT求逆,这样我就可以将实部和虚部所携带的全部信息作为一个数字传输?我调查了z = sqrt(real^2 + imaginary^2)事物,但这不是“事物”。

下面,我提供一个工作示例。感谢您的帮助。

import numpy as np
from scipy.fftpack import fftfreq

# Define signal.
Fs = 128  # Sampling rate.
Ts = 1 / Fs  # Sampling interval.
Time = np.arange(0, 10, Ts)  # Time vector.
signal = np.cos(4*np.pi*Time) + np.cos(6*np.pi*Time) + np.cos(8*np.pi*Time)


def spectrum(sig, t):
    """
    Represent given signal in frequency domain.
    :param sig: signal.
    :param t: time scale.
    :return:
    """
    f = fftfreq(sig.size, d=t[1]-t[0])
    y = np.fft.fft(sig)
    return f, np.abs(y)


def bandpass(f, sig, min_freq, max_freq):
    """
    Bandpass signal in a specified by min_freq and max_freq frequency range.
    :param f: frequency.
    :param sig: signal.
    :param min_freq: minimum frequency.
    :param max_freq: maximum frequency.
    :return:
    """
    return np.where(np.logical_or(f < min_freq, f > max_freq), 0, sig)

freq, spec = spectrum(signal, Time)
signal_filtered = np.fft.ifft(bandpass(freq, spec, 5, 7))

print(signal_filtered)

"""
print(signal_filtered) result:

[  2.22833798e-15 +0.00000000e+00j   2.13212081e-15 +6.44480810e-16j
   1.85209996e-15 +1.23225456e-15j ...,   1.41336488e-15 -1.71179288e-15j
   1.85209996e-15 -1.23225456e-15j   2.13212081e-15 -6.44480810e-16j]
"""

最佳答案

如果要切掉5到7之间的频率
那么您需要将频率保持在

(f < min_freq) | (f > max_freq)

相当于
np.logical_or(f < min_freq, f > max_freq)

因此,使用
return np.where(np.logical_or(f < min_freq, f > max_freq), sig, 0)

代替
return np.where(np.logical_or(f < min_freq, f > max_freq), 0, sig)

因为当条件为True时,np.where的第二个参数包含np.where返回的值。

有了这一更改,您的代码就会产生
[ 3.00000000 +0.00000000e+00j  2.96514652 +1.24442385e-15j
  2.86160515 +2.08976636e-15j ...,  2.69239924 +4.71763845e-15j
  2.86160515 +5.88163496e-15j  2.96514652 +6.82134642e-15j]

请注意,如果您的信号是真实的,则可以使用rfft进行实数序列的离散傅立叶变换,使用irfft进行逆变换,使用rfftfreq生成频率。

例如,
from __future__ import division
import numpy as np
import scipy.fftpack as fftpack

# Define signal.
Fs = 128  # Sampling rate.
Ts = 1 / Fs  # Sampling interval.
Time = np.arange(0, 10, Ts)  # Time vector.
signal = np.cos(4*np.pi*Time) + np.cos(6*np.pi*Time) + np.cos(8*np.pi*Time)


def spectrum(sig, t):
    """
    Represent given signal in frequency domain.
    :param sig: signal.
    :param t: time scale.
    :return:
    """
    f = fftpack.rfftfreq(sig.size, d=t[1]-t[0])
    y = fftpack.rfft(sig)
    return f, np.abs(y)


def bandpass(f, sig, min_freq, max_freq):
    """
    Bandpass signal in a specified by min_freq and max_freq frequency range.
    :param f: frequency.
    :param sig: signal.
    :param min_freq: minimum frequency.
    :param max_freq: maximum frequency.
    :return:
    """
    return np.where(np.logical_or(f < min_freq, f > max_freq), sig, 0)

freq, spec = spectrum(signal, Time)
signal_filtered = fftpack.irfft(bandpass(freq, spec, 5, 7))

print(signal_filtered)

产量
[ 3.          2.96514652  2.86160515 ...,  2.69239924  2.86160515
  2.96514652]

注意,您必须在此处使用scipyfftpack;不要将SciPy的实现与NumPy的实现混在一起。

10-05 18:37