【问题标题】:pyQTGraph set axis values (FFT)pyQTGraph 设置轴值(FFT)
【发布时间】:2014-05-17 01:23:25
【问题描述】:

类 FFT_Plot():

def __init__(self,
             win,
             nSamples,
             aData,
             sRate,
             wFunction,
             zStart = 0):
    self.nSamples = nSamples    # Number of Sample must be a 2^n power
    self.aData = aData          # Amplitude data array
    self.sRate = sRate          # Sample Rate
    self.wFunction = wFunction  # Windowing Function
    self.zStart = zStart        # Start of Zoom Window if Used
    self.zStop = nSamples/2     # End of Zoom Window if Used
    # Instantiate a plot window within an existing pyQtGraph window.
    self.plot = win.addPlot(title="FFT")
    self.update(aData)
    self.grid_state()

    self.plot.setLabel('left', 'Amplitude', 'Volts')
    self.plot.setLabel('bottom', 'Frequency', 'Hz')

def update(self, aData):
    x = np.fft.fft(aData,)
    amplitude = np.absolute(x)
    fScale = np.linspace(0 , 50000, self.nSamples)
    self.plot.plot(amplitude)
    # Calculate and set-up X axis
    self.plot.setXRange(SampleSize/2, 0)

def grid_state(self, x = True, y = True):
    self.plot.showGrid(x, y)

我的问题很简单。如何更改沿 x 和 y 轴显示的值?

当使用 2048 个样本并显示一半样本(0 到样本/2)时,我会显示 0 到 1。如果无法显示频率或幅度,计算它们对我没有好处。

如果我改变范围,我会有效地放大光谱​​...我看过一些例子,但我很快就迷失了,因为没有任何关于发生了什么的解释。

任何帮助将不胜感激......

正如 Luke 所分享的......我错过了我可以使用“X”数组的事实。 :) 更正的初学者课程如下:

类 FFT_Plot():

def __init__(self,
             win,
             nSamples,
             aData,
             sRate,
             wFunction,
             zStart = 0):
    self.nSamples = nSamples    # Number of Sample must be a 2^n power
    self.aData = aData          # Amplitude data array
    self.sRate = sRate          # Sample Rate as Frequency
    self.wFunction = wFunction  # Windowing Function
    self.zStart = zStart        # Start of Zoom Window if Used
    self.zStop = nSamples/2     # End of Zoom Window if Used
    # Instantiate a plot window within an existing pyQtGraph window.
    self.plot = win.addPlot(title="FFT")
    self.update(aData)
    self.grid_state()
    self.plot.setLabel('left', 'Amplitude', 'Volts')
    self.plot.setLabel('bottom', 'Frequency', 'Hz')

def update(self, aData):
    x = np.fft.fft(aData,)
    amplitude = np.absolute(x)
    # Create a linear scale based on the Sample Rate and Number of Samples.
    fScale = np.linspace(0 , self.sRate, self.nSamples)
    self.plot.plot(x = fScale, y = amplitude, pen={'color': (0, 0, 0), 'width': 2})
    # Because the X-axis is now tied to the fScale, which os based on sRate,
    # to set any range limits you must use the sRate.
    self.plot.setXRange(self.sRate/2, 0)

def grid_state(self, x = True, y = True):
    self.plot.showGrid(x, y)

任何 DSP 类型请随意添加非数学 cmets。

此外,要让 Y 轴正确读取,幅度数组似乎必须相应地进行预缩放。

【问题讨论】:

    标签: python pyqt pyqtgraph


    【解决方案1】:

    在pyqtgraph中,轴值是根据显示数据的坐标系自动确定的。当您只指定 y 值调用 plot() 时,它假定您需要整数 x 值,例如 range(len(yValues))。因此,如果您希望样本的 x 值介于 0 到 50k 之间,则需要在调用 plot 时提供这些值:self.plot.plot(x=fScale, y=amplitude)。您应该会发现轴值会做出相应的反应。

    【讨论】:

      猜你喜欢
      • 2017-04-26
      • 2019-11-15
      • 2018-07-19
      • 2020-12-16
      • 1970-01-01
      • 2019-01-30
      • 2015-01-17
      • 2018-10-05
      • 2017-12-19
      相关资源
      最近更新 更多