打印频域图的最高峰值 [英] Print highest peak value of the frequency domain plot
问题描述
我试图在时域和频域中绘制我的自制四边形的振荡.如何在频域图中打印最高峰值的值?
I've attempted to plot the oscillations of my home made quad in the time and frequency domain. How do I print the value of my highest peak in the frequency domain plot?
代码:
import matplotlib.pyplot as plt
import numpy as np
from scipy import fft, arange
csv = np.genfromtxt ('/Users/shaunbarney/Desktop/Results/quadOscillations.csv', delimiter=",",dtype=float)
x = csv[:,0]
y = csv[:,1]
x = x - 6318 #Remove start offset
av=0
for i in xrange(1097): #Calculate average sampling time in seconds oscillations
if i == 1076:
avSampleTime = av/1097000 #
break
av = av + (x[i+1]-x[i])
Fs = 1/avSampleTime #Average sampling freq.
n = 1079 #no.Samples
k = arange(n)
Ts = n/Fs
frq = k/Ts #Frequency range two sided
frq = frq[range(n/2)] #Frequency range one sided
Y = fft(y)/n #Fast fourier transfors
Y = Y[range(n/2)] #Normalise
# PLOTS
plt.subplot(2,1,1)
plt.plot(frq,abs(Y),'r') # plotting the spectrum
plt.xlabel('Freq (Hz)')
plt.ylabel('|Y(freq)|')
plt.grid('on')
plt.subplot(2,1,2)
plt.plot(x,y)
plt.xlabel('Time (ms)')
plt.ylabel('Angle (degrees)')
plt.grid('on')
plt.show()
结果如下:
谢谢,肖恩
推荐答案
由于您使用的是 numpy
,因此只需使用numpy.max
和 numpy.argmax
确定峰以及峰的位置,以便可以将其打印到屏幕上.找到这个位置后,索引到您的频率数组以获得最终坐标.
Since you're using numpy
, just simply use numpy.max
and numpy.argmax
to determine the peak as well as the location of the peak so you can print this out to your screen. Once you find this location, index into your frequency array to obtain the final coordinate.
假设在您运行代码时已创建所有变量,只需执行以下操作:
Assuming that all of your variables have been created when you run your code, simply do the following:
mY = np.abs(Y) # Find magnitude
peakY = np.max(mY) # Find max peak
locY = np.argmax(mY) # Find its location
frqY = frq[locY] # Get the actual frequency value
peakY
包含图中最大的幅度值,而 frqY
包含该最大值(即峰值)所在的频率.作为奖励,您可以用不同的颜色和更大的标记在图表上绘制它,以将其与主幅度图区分开来.请记住,调用多个 plot
调用只会简单地附加在当前焦点图的顶部.因此,绘制频谱,然后将此点绘制在频谱顶部.我将使点的大小大于绘图的厚度,并用不同的颜色标记该点.您也可以制作一个反映该最大峰值和相应位置的标题.
peakY
contains the magnitude value that is the largest in your graph and frqY
contains the frequency that this largest value (i.e. peak) is located at. As a bonus, you can plot that on your graph in a different colour and with a larger marker to distinguish it from the main magnitude graph. Remember that invoking multiple plot
calls will simply append on top of the current figure of focus. Therefore, plot your spectrum then plot this point on top of the spectrum. I'll make the size of the point larger than the thickness of the plot as well as marking the point with a different colour. You could also perhaps make a title that reflects this largest peak value and the corresponding location.
还要记住,这是对震级进行的,因此在绘制实际震级之前,只需执行以下操作:
Also remember that this is to be done on the magnitude, so before you plot your actual magnitude, simply do this:
# PLOTS
# New - Find peak of spectrum - Code from above
mY = np.abs(Y) # Find magnitude
peakY = np.max(mY) # Find max peak
locY = np.argmax(mY) # Find its location
frqY = frq[locY] # Get the actual frequency value
# Code from before
plt.subplot(2,1,1)
plt.plot(frq,abs(Y),'r') # plotting the spectrum
# New - Plot the max point
plt.plot(frqY, peakY, 'b.', markersize=18)
# New - Make title reflecting peak information
plt.title('Peak value: %f, Location: %f Hz' % (peakY, frqY))
# Rest of the code is the same
plt.xlabel('Freq (Hz)')
plt.ylabel('|Y(freq)|')
plt.grid('on')
plt.subplot(2,1,2)
plt.plot(x,y)
plt.xlabel('Time (ms)')
plt.ylabel('Angle (degrees)')
plt.grid('on')
plt.show()
这篇关于打印频域图的最高峰值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!