从信号的fft中找到局部最大值

时间:2015-05-26 11:02:21

标签: python matplotlib widget signal-processing fft

我正在尝试找到信号fft的峰值,用于进一步分析信号。我正在使用SpanSelect数据并执行fft,表示为频谱。我真的希望让绘图是交互式的,并且用户点击要进一步分析的点,但是我没有看到这样做的方法,因此想要找到局部频率峰值的方法。频谱可能如下所示:

Example of frequency spectrum plot

所以我想要一种方法来返回具有38赫兹峰值的频率。有没有办法做到这一点?

2 个答案:

答案 0 :(得分:2)

使用argrelextrema查找本地最大值:

import numpy as np
from scipy.signal import argrelextrema
from matplotlib.pyplot import *
np.random.seed()
x = np.random.random(50)
m = argrelextrema(x, np.greater) #array of indexes of the locals maxima
y = [x[m] for i in m]
plot(x)
plot(m, y, 'rs')
show() 

enter image description here

答案 1 :(得分:1)

您可以使用matplotlib小部件执行类似的操作,例如check out选择点的套索方法。

然后,您可以在所需的任何形式的分析中使用所选点。

编辑:来自matplotlib示例的组合套索和SpanSelect小部件

#!/usr/bin/env python

from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import SpanSelector, LassoSelector
from matplotlib.path import Path
import matplotlib.pyplot as plt

try:
    raw_input
except NameError:
    # Python 3
    raw_input = input


class SelectFromCollection(object):
    """Select indices from a matplotlib collection using `LassoSelector`.

    Selected indices are saved in the `ind` attribute. This tool highlights
    selected points by fading them out (i.e., reducing their alpha values).
    If your collection has alpha < 1, this tool will permanently alter them.

    Note that this tool selects collection objects based on their *origins*
    (i.e., `offsets`).

    Parameters
    ----------
    ax : :class:`~matplotlib.axes.Axes`
        Axes to interact with.

    collection : :class:`matplotlib.collections.Collection` subclass
        Collection you want to select from.

    alpha_other : 0 <= float <= 1
        To highlight a selection, this tool sets all selected points to an
        alpha value of 1 and non-selected points to `alpha_other`.
    """
    def __init__(self, ax, collection, alpha_other=0.3):
        self.canvas = ax.figure.canvas
        self.collection = collection
        self.alpha_other = alpha_other

        self.xys = collection.get_offsets()
        self.Npts = len(self.xys)

        # Ensure that we have separate colors for each object
        self.fc = collection.get_facecolors()
        if len(self.fc) == 0:
            raise ValueError('Collection must have a facecolor')
        elif len(self.fc) == 1:
            self.fc = np.tile(self.fc, self.Npts).reshape(self.Npts, -1)

        self.lasso = LassoSelector(ax, onselect=self.onselect)
        self.ind = []

    def onselect(self, verts):
        path = Path(verts)
        self.ind = np.nonzero([path.contains_point(xy) for xy in self.xys])[0]
        self.fc[:, -1] = self.alpha_other
        self.fc[self.ind, -1] = 1
        self.collection.set_facecolors(self.fc)
        self.canvas.draw_idle()

    def disconnect(self):
        self.lasso.disconnect_events()
        self.fc[:, -1] = 1
        self.collection.set_facecolors(self.fc)
        self.canvas.draw_idle()

def onselect(xmin, xmax):
    indmin, indmax = np.searchsorted(x, (xmin, xmax))
    indmax = min(len(x)-1, indmax)

    thisx = x[indmin:indmax]
    thisy = y[indmin:indmax]
    line2.set_data(thisx, thisy)
    ax2.set_xlim(thisx[0], thisx[-1])
    ax2.set_ylim(thisy.min(), thisy.max())
    fig.canvas.draw()





if __name__ == '__main__':


    plt.ion()

    fig = plt.figure(figsize=(8,6))
    ax = fig.add_subplot(211, axisbg='#FFFFCC')

    x = np.arange(0.0, 5.0, 0.01)
    y = np.sin(2*np.pi*x) + 0.5*np.random.randn(len(x))

    ax.plot(x, y, '-')
    ax.set_ylim(-2,2)
    ax.set_title('Press left mouse button and drag to test')

    ax2 = fig.add_subplot(212, axisbg='#FFFFCC')
    line2, = ax2.plot(x, y, '-')
    pts = ax2.scatter(x, y)

    # set useblit True on gtkagg for enhanced performance
    span = SpanSelector(ax, onselect, 'horizontal', useblit=True,
                        rectprops=dict(alpha=0.5, facecolor='red') )

    selector = SelectFromCollection(ax2, pts)

    plt.draw()
    raw_input('Press any key to accept selected points')
    print("Selected points:")
    print(selector.xys[selector.ind])
    selector.disconnect()

    # Block end of script so you can check that the lasso is disconnected.
    raw_input('Press any key to quit')