pyQTGraph设置轴值(FFT)

时间:2014-04-04 16:05:53

标签: python pyqt pyqtgraph

类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。如果我无法显示它们,那么计算频率或幅度对我没用。

如果我改变范围我有效地缩放光谱......我已经看到了一些例子,但由于对正在发生的事情缺乏任何解释,我很快就迷失了。

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

卢克分享了......我错过了我可以使用'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类型请随意添加非数学注释。

此外,为了使Y轴正确读取,幅度阵列似乎必须相应地进行预缩放。

1 个答案:

答案 0 :(得分:2)

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