1

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 轴正确读取,幅度数组似乎必须相应地进行预缩放。

4

1 回答 1

2

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

于 2014-04-04T19:09:23.647 回答