简体   繁体   English

使用python的FFT系数

[英]FFT coefficients using python

I am a newbie in Signal Processing. 我是信号处理领域的新手。 In here, I want to ask how to get FFT coeffients from FFT from in python. 在这里,我想问一下如何从python中的FFT获取FFT系数。 This is the example of my code: 这是我的代码示例:

from scipy.fftpack import fft
# Number of samplepoints
N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
import matplotlib.pyplot as plt
plt.plot(xf, 2.0/N * np.abs(yf[0:N/2]))
plt.grid()
plt.show()

在此处输入图片说明

Hmm I don't really know about signal processing either but maybe this works: 嗯,我也不是很了解信号处理,但是也许可以用:

from scipy.signal import argrelmax
f = xf[scipy.signal.argrelmax(yf[0:N/2])]
Af = np.abs(yf[argrelmax(yf[0:N/2])])

Quoting @hotpaw, in this similar answer: 引用@hotpaw,在这个类似的答案:

"The real and imaginary arrays, when put together, can represent a complex array. Every complex element of the complex array in the frequency domain can be considered a frequency coefficient, and has a magnitude sqrt(R R + I I))". “将实数和虚数数组放在一起时,可以表示一个复数数组。该复数数组在频域中的每个复数元素都可以视为频率系数,并且幅度为sqrt(R R + I I))”。

So, the coefficients are the complex elements in the array returned by the fft function. 因此,系数是fft函数返回的数组中的复数元素。 Also, it is important to play with the size (the number) of the bins for the FFT function. 同样,重要的是要使用FFT功能的存储区大小(数量)。 It would make sense to test a bunch of values and pick the one that makes more sense to your application. 测试一堆值并选择对您的应用程序更有意义的值将是有意义的。 Often, it is in the same magnitude of the number of samples. 通常,它与样本数量的大小相同。 This was as assumed by most of the answers given, and produces great and reasonable results. 给出的大多数答案都假定了这一点,并且产生了很好且合理的结果。 In case one wants to explore that, here is my code version: 如果有人想探索一下,这是我的代码版本:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack

fig = plt.figure(figsize=[14,4])
N = 600           # Number of samplepoints
Fs = 800.0
T = 1.0 / Fs      # N_samps*T (#samples x sample period) is the sample spacing.
N_fft = 80        # Number of bins (chooses granularity)
x = np.linspace(0, N*T, N)     # the interval
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)   # the signal

# removing the mean of the signal
mean_removed = np.ones_like(y)*np.mean(y)
y = y - mean_removed

# Compute the fft.
yf = scipy.fftpack.fft(y,n=N_fft)
xf = np.arange(0,Fs,Fs/N_fft)

##### Plot the fft #####
ax = plt.subplot(121)
pt, = ax.plot(xf,np.abs(yf), lw=2.0, c='b')
p = plt.Rectangle((Fs/2, 0), Fs/2, ax.get_ylim()[1], facecolor="grey", fill=True, alpha=0.75, hatch="/", zorder=3)
ax.add_patch(p)
ax.set_xlim((ax.get_xlim()[0],Fs))
ax.set_title('FFT', fontsize= 16, fontweight="bold")
ax.set_ylabel('FFT magnitude (power)')
ax.set_xlabel('Frequency (Hz)')
plt.legend((p,), ('mirrowed',))
ax.grid()

##### Close up on the graph of fft#######
# This is the same histogram above, but truncated at the max frequence + an offset. 
offset = 1    # just to help the visualization. Nothing important.
ax2 = fig.add_subplot(122)
ax2.plot(xf,np.abs(yf), lw=2.0, c='b')
ax2.set_xticks(xf)
ax2.set_xlim(-1,int(Fs/6)+offset)
ax2.set_title('FFT close-up', fontsize= 16, fontweight="bold")
ax2.set_ylabel('FFT magnitude (power) - log')
ax2.set_xlabel('Frequency (Hz)')
ax2.hold(True)
ax2.grid()

plt.yscale('log')

Output: 输出: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM