特征提取(Feature Extraction)常见频域特征笔记(四)

发布于:2024-05-01 ⋅ 阅读:(29) ⋅ 点赞:(0)

        频域特征是指将数据转换到频率域进行分析的特征。在频域分析中,我们可以看到信号在不同频率下的成分,这对于理解信号的周期性、周期性强度以及频率分布非常有用。常见的频域特征包括傅里叶变换、功率谱密度等。下面我会详细解释每个频域特征,并给出相应的Python代码。

  1. 傅里叶变换(Fourier Transform):将信号从时域(时间域)转换到频域(频率域),它将信号分解成一系列正弦和余弦函数的组合。傅里叶变换可以帮助我们了解信号中各种频率成分的强度和相位信息。
    import numpy as np
    import matplotlib.pyplot as plt
    
    def fourier_transform(signal, sampling_rate):
        n = len(signal)
        frequencies = np.fft.fftfreq(n, d=1/sampling_rate)
        fft_values = np.fft.fft(signal)
        return frequencies, fft_values
    
    # Example
    t = np.linspace(0, 1, 1000)  # Time array from 0 to 1 with 1000 points
    signal = np.sin(2 * np.pi * 5 * t) + np.sin(2 * np.pi * 10 * t)  # Signal with two frequencies
    sampling_rate = 1000  # Sampling rate in Hz
    
    frequencies, fft_values = fourier_transform(signal, sampling_rate)
    plt.plot(frequencies, np.abs(fft_values))
    plt.xlabel('Frequency (Hz)')
    plt.ylabel('Amplitude')
    plt.title('Fourier Transform')
    plt.show()
    
  2. 功率谱密度(Power Spectral Density):表示信号在不同频率上的能量分布。功率谱密度可以帮助我们确定信号中哪些频率成分具有更高的能量。
    from scipy.signal import welch
    
    def power_spectral_density(signal, sampling_rate):
        frequencies, psd = welch(signal, fs=sampling_rate)
        return frequencies, psd
    
    # Example
    t = np.linspace(0, 1, 1000)  # Time array from 0 to 1 with 1000 points
    signal = np.sin(2 * np.pi * 5 * t) + np.sin(2 * np.pi * 10 * t)  # Signal with two frequencies
    sampling_rate = 1000  # Sampling rate in Hz
    
    frequencies, psd = power_spectral_density(signal, sampling_rate)
    plt.semilogy(frequencies, psd)
    plt.xlabel('Frequency (Hz)')
    plt.ylabel('Power Spectral Density')
    plt.title('Power Spectral Density')
    plt.show()
    

    这些是常见的频域特征及其相应的Python实现。在信号处理和频域分析中,这些特征对于理解信号的频率成分、周期性以及能量分布非常重要。


网站公告

今日签到

点亮在社区的每一天
去签到