简体   繁体   English

从信号的ftf寻找局部最大值

[英]finding local maximum from fft of a signal

I'm trying to find a peak of an fft of a signal to be used for a further analysis of the signal. 我正在尝试找到信号英尺峰值,以用于信号的进一步分析。 I'm using a SpanSelect of data and doing an fft, represented as a frequency spectrum. 我正在使用SpanSelect数据并进行fft表示为频谱。 I really wanted to have the plot be interactive and the user click a point to be further analyzed, but I don't see a way to do that so would like a way to find local frequency peaks. 我确实想让绘图具有交互性,并且用户单击要进一步分析的点,但是我看不到这样做的方法,因此希望找到局部频率峰值的方法。 The frequency spectrum may look like this: 频谱可能看起来像这样:

频谱图示例

So I would want a way to return the frequency that has a peak at 38 hz for example. 因此,我想要一种方法来返回例如在38 hz处具有峰值的频率。 Is there a way to do this? 有没有办法做到这一点?

use argrelextrema for finding local maxima: 使用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() 

在此处输入图片说明

You can do something like that using matplotlib widgets, for example check out the lasso method of selecting points. 您可以使用matplotlib小部件执行类似的操作,例如, 检查选择点的套索方法。

You can then use the selected point in any form of analysis you need. 然后,您可以在所需的任何分析形式中使用选定的点。

EDIT: Combined lasso and SpanSelect widget from matplotlib examples 编辑: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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM