[英]Calculating the intensity of a FFT peak
for the physical laboratory our professor gave us a task to make analysis of a frequency spectrum of a plucked string.对于物理实验室,我们的教授给了我们一项任务,即对弹拨的频谱进行分析。 After the sound acquisition we were given a script to perform a FFT.
在声音采集之后,我们得到了一个执行 FFT 的脚本。
After the FFT we now have several frequency peaks.在 FFT 之后,我们现在有几个频率峰值。
He then told us that we have to calculate the intensity of each peak separately.然后他告诉我们,我们必须分别计算每个峰的强度。 I am new to this topic, so I am asking for your help how to alter the given code to get as an output peak intensity let's say from 760 to 765 Hz.
我是这个话题的新手,所以我想请教如何更改给定的代码以获得输出峰值强度,比如从 760 到 765 Hz。
The code is here:代码在这里:
from scipy.fftpack import fft,ifft
import matplotlib.pyplot as plt
from scipy.signal import blackman
data = np.loadtxt("mic.txt")
x = data[:,0]
y = data[:,1]
fy = fft(y)
print np.sum(y),"==",fy[0]
n = len(x)
t = x[-1]
fx = np.linspace(0,n/t,n)
plt.plot(fx[0:n/2],np.abs(fy[0:n/2]))
plt.xlabel("frequency (Hz)")
plt.show()
I would appreciate your help, Matthew我会很感激你的帮助,马修
By peak intensity I assume you mean the magnitude of each peak (of course it can be normalized).我认为峰值强度是指每个峰值的大小(当然可以归一化)。 I used
scipy.signal.find_peaks
to get the peaks in the spectrum.我使用
scipy.signal.find_peaks
来获取频谱中的峰值。 Because FFT returns complex values, I used it on the absolute value of y.因为 FFT 返回复数值,所以我在 y 的绝对值上使用它。 This is a generic example, so finding the maximums is not that trivial in most cases.
这是一个通用示例,因此在大多数情况下找到最大值并不是那么简单。 There are other peak detection tools, such as
scipy.signal.find_peaks_cwt
, or peakutils
package.还有其他峰值检测工具,例如
scipy.signal.find_peaks_cwt
或peakutils
包。 From the scipy tutorials here is a minimal working example:从scipy 教程这里是一个最小的工作示例:
import numpy as np
from scipy.fft import fft
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
N = 600
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)
# finding the peaks in the absolute value of y
y_abs = 2.0 / N * np.abs(yf[0:N//2])
peakind, _ = find_peaks(y_abs)
plt.plot(xf, y_abs)
# plotting the peaks
plt.plot(xf[peakind], y_abs[peakind], 'k.')
plt.grid()
plt.show()
The magnitue of the peaks is just峰的大小只是
>>> y_abs[peakind]
[0.70947072 0.4914645 ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.