【问题标题】:Apply FFT to each chunk of 1024 samples将 FFT 应用于 1024 个样本的每个块
【发布时间】:2021-05-25 00:18:18
【问题描述】:

我有 1024 个样本,我想将它们分成 32 个块,每块 32 个,并在每个块上运行 FFT 并通过频率-幅度谱绘制它,我的大部分代码只是将 FFT 应用于每个块都不起作用,但是我可以将 FFT 应用于整个样本数组。

我尝试过这样做:

realFFT = [for chunk in chunks(amplitude,32): np.fft.fft(chunk)]

但这是错误的语法

我还尝试通过将块数组转换为列表然后将其全部保存到另一个列表来遍历块数组,但这也不起作用。

这是我的代码:

# Python example - Fourier transform using numpy.fft method
import numpy as np
import matplotlib.pyplot as plotter
from os import times
from PIL import Image
import numpy as np
from numpy.lib.type_check import real

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

# How many time points are needed i,e., Sampling Frequency
samplingFrequency = 100

# At what intervals time points are sampled
samplingInterval = 1 / samplingFrequency

# Begin time period of the signals
beginTime = 0

# End time period of the signals
endTime = 10.24

# Frequency of the signals
signal1Frequency = 4
signal2Frequency = 7

# Time points
time = np.arange(beginTime, endTime, samplingInterval)
# Create two sine waves
amplitude1 = 0.7* np.sin(2*np.pi*signal1Frequency*time)
amplitude2 = np.sin(2*np.pi*signal2Frequency*time)
# Create subplot

figure, axis = plotter.subplots(2, 1)
plotter.subplots_adjust(hspace=2)
# Time domain representation for sine wave 1

amplitude = amplitude1

axis[0].set_title('Sine wave with a frequency of 4 Hz')
axis[0].plot(time, amplitude)
axis[0].set_xlabel('Time')
axis[0].set_ylabel('Amplitude')
# Frequency domain representation

realFFT = [for chunk in chunks(amplitude,32): np.fft.fft(chunk)]
#fourierTransform = np.fft.fft(amplitude) # Normalize amplitude
fourierTransform = realFFT[range(int(len(amplitude)/2))] # Exclude sampling frequency
tpCount = len(amplitude)
values = np.arange(int(tpCount/2))
timePeriod = tpCount/samplingFrequency
frequencies = values/timePeriod

# Frequency domain representation

axis[1].set_title('Fourier transform depicting the frequency components')
#dBm = 30 + (20 * np.log10(abs(fourierTransform)))
axis[1].plot(frequencies, abs(fourierTransform))
axis[1].set_xlabel('Frequency')
axis[1].set_ylabel('Amplitude')

plotter.show()

【问题讨论】:

    标签: python numpy audio fft


    【解决方案1】:

    你的语法有点错误

    realFFT = [np.fft.fft(chunk) for chunk in chunks(amplitude, 32)]
    

    列表解析的语法与 for 循环有点不同。

    【讨论】:

      猜你喜欢
      • 2021-11-14
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 2013-02-19
      • 2019-12-17
      • 1970-01-01
      相关资源
      最近更新 更多