对 1024 个样本的每个块应用 FFT
Apply FFT to each chunk of 1024 samples
我有 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()
你的语法有点错误
realFFT = [np.fft.fft(chunk) for chunk in chunks(amplitude, 32)]
列表理解的语法与 for 循环略有不同。
我有 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()
你的语法有点错误
realFFT = [np.fft.fft(chunk) for chunk in chunks(amplitude, 32)]
列表理解的语法与 for 循环略有不同。