Find local maxima using scipy.signal












1















I have a 1D array (A) of 64 integer values. How can I find the 2 local maxima corresponding to the values 56 and 50 (indices 10 and 45, respectively) using the scipy.signal module?



At first I tried importing



from scipy.signal import find_peaks

A
array([ 0., 1., 3., 8., 6., 16., 29., 29., 47., 42., 56.,
44., 49., 40., 34., 34., 26., 24., 25., 21., 22., 24.,
34., 17., 17., 29., 24., 26., 13., 25., 16., 19., 19.,
26., 24., 26., 41., 34., 24., 37., 37., 39., 34., 40.,
45., 50., 28., 45., 43., 46., 47., 41., 30., 23., 19.,
15., 9., 10., 3., 6., 4., 1., 1., 0.])


but before I got any further I got the error message that




"AttributeError: 'module' object has no attribute 'find_peaks'",




so then instead I tried importing



from scipy import signal
peakind = signal.find_peaks_cwt(A, widths=32)


but then I get the error message that




"TypeError: 'int' object is not subscriptable".




I still get this error message even if I first do A.astype(np.int64).



Can't I use scipy.signal_find_peaks on an array of integer values?










share|improve this question

























  • @Cleb Thank you for your response! Not quite I'm afraid. I found the potential solution to use argrelextrema, but it would return all local maxima and I am only interested in a function/module who finds the two values I want (56 & 49). Actually, the documentation I found about scipy.signal.find_peaks seemed very promising so it was disappointing that it could not be loaded.

    – kirerik
    Nov 19 '18 at 16:11






  • 2





    As the docs state, find_peaks is new in version 1.1.0. Updating scipy should work

    – Brenlla
    Nov 19 '18 at 16:14


















1















I have a 1D array (A) of 64 integer values. How can I find the 2 local maxima corresponding to the values 56 and 50 (indices 10 and 45, respectively) using the scipy.signal module?



At first I tried importing



from scipy.signal import find_peaks

A
array([ 0., 1., 3., 8., 6., 16., 29., 29., 47., 42., 56.,
44., 49., 40., 34., 34., 26., 24., 25., 21., 22., 24.,
34., 17., 17., 29., 24., 26., 13., 25., 16., 19., 19.,
26., 24., 26., 41., 34., 24., 37., 37., 39., 34., 40.,
45., 50., 28., 45., 43., 46., 47., 41., 30., 23., 19.,
15., 9., 10., 3., 6., 4., 1., 1., 0.])


but before I got any further I got the error message that




"AttributeError: 'module' object has no attribute 'find_peaks'",




so then instead I tried importing



from scipy import signal
peakind = signal.find_peaks_cwt(A, widths=32)


but then I get the error message that




"TypeError: 'int' object is not subscriptable".




I still get this error message even if I first do A.astype(np.int64).



Can't I use scipy.signal_find_peaks on an array of integer values?










share|improve this question

























  • @Cleb Thank you for your response! Not quite I'm afraid. I found the potential solution to use argrelextrema, but it would return all local maxima and I am only interested in a function/module who finds the two values I want (56 & 49). Actually, the documentation I found about scipy.signal.find_peaks seemed very promising so it was disappointing that it could not be loaded.

    – kirerik
    Nov 19 '18 at 16:11






  • 2





    As the docs state, find_peaks is new in version 1.1.0. Updating scipy should work

    – Brenlla
    Nov 19 '18 at 16:14
















1












1








1








I have a 1D array (A) of 64 integer values. How can I find the 2 local maxima corresponding to the values 56 and 50 (indices 10 and 45, respectively) using the scipy.signal module?



At first I tried importing



from scipy.signal import find_peaks

A
array([ 0., 1., 3., 8., 6., 16., 29., 29., 47., 42., 56.,
44., 49., 40., 34., 34., 26., 24., 25., 21., 22., 24.,
34., 17., 17., 29., 24., 26., 13., 25., 16., 19., 19.,
26., 24., 26., 41., 34., 24., 37., 37., 39., 34., 40.,
45., 50., 28., 45., 43., 46., 47., 41., 30., 23., 19.,
15., 9., 10., 3., 6., 4., 1., 1., 0.])


but before I got any further I got the error message that




"AttributeError: 'module' object has no attribute 'find_peaks'",




so then instead I tried importing



from scipy import signal
peakind = signal.find_peaks_cwt(A, widths=32)


but then I get the error message that




"TypeError: 'int' object is not subscriptable".




I still get this error message even if I first do A.astype(np.int64).



Can't I use scipy.signal_find_peaks on an array of integer values?










share|improve this question
















I have a 1D array (A) of 64 integer values. How can I find the 2 local maxima corresponding to the values 56 and 50 (indices 10 and 45, respectively) using the scipy.signal module?



At first I tried importing



from scipy.signal import find_peaks

A
array([ 0., 1., 3., 8., 6., 16., 29., 29., 47., 42., 56.,
44., 49., 40., 34., 34., 26., 24., 25., 21., 22., 24.,
34., 17., 17., 29., 24., 26., 13., 25., 16., 19., 19.,
26., 24., 26., 41., 34., 24., 37., 37., 39., 34., 40.,
45., 50., 28., 45., 43., 46., 47., 41., 30., 23., 19.,
15., 9., 10., 3., 6., 4., 1., 1., 0.])


but before I got any further I got the error message that




"AttributeError: 'module' object has no attribute 'find_peaks'",




so then instead I tried importing



from scipy import signal
peakind = signal.find_peaks_cwt(A, widths=32)


but then I get the error message that




"TypeError: 'int' object is not subscriptable".




I still get this error message even if I first do A.astype(np.int64).



Can't I use scipy.signal_find_peaks on an array of integer values?







python python-3.x scipy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 9:30









Cleb

10.8k125482




10.8k125482










asked Nov 19 '18 at 15:37









kirerikkirerik

668




668













  • @Cleb Thank you for your response! Not quite I'm afraid. I found the potential solution to use argrelextrema, but it would return all local maxima and I am only interested in a function/module who finds the two values I want (56 & 49). Actually, the documentation I found about scipy.signal.find_peaks seemed very promising so it was disappointing that it could not be loaded.

    – kirerik
    Nov 19 '18 at 16:11






  • 2





    As the docs state, find_peaks is new in version 1.1.0. Updating scipy should work

    – Brenlla
    Nov 19 '18 at 16:14





















  • @Cleb Thank you for your response! Not quite I'm afraid. I found the potential solution to use argrelextrema, but it would return all local maxima and I am only interested in a function/module who finds the two values I want (56 & 49). Actually, the documentation I found about scipy.signal.find_peaks seemed very promising so it was disappointing that it could not be loaded.

    – kirerik
    Nov 19 '18 at 16:11






  • 2





    As the docs state, find_peaks is new in version 1.1.0. Updating scipy should work

    – Brenlla
    Nov 19 '18 at 16:14



















@Cleb Thank you for your response! Not quite I'm afraid. I found the potential solution to use argrelextrema, but it would return all local maxima and I am only interested in a function/module who finds the two values I want (56 & 49). Actually, the documentation I found about scipy.signal.find_peaks seemed very promising so it was disappointing that it could not be loaded.

– kirerik
Nov 19 '18 at 16:11





@Cleb Thank you for your response! Not quite I'm afraid. I found the potential solution to use argrelextrema, but it would return all local maxima and I am only interested in a function/module who finds the two values I want (56 & 49). Actually, the documentation I found about scipy.signal.find_peaks seemed very promising so it was disappointing that it could not be loaded.

– kirerik
Nov 19 '18 at 16:11




2




2





As the docs state, find_peaks is new in version 1.1.0. Updating scipy should work

– Brenlla
Nov 19 '18 at 16:14







As the docs state, find_peaks is new in version 1.1.0. Updating scipy should work

– Brenlla
Nov 19 '18 at 16:14














1 Answer
1






active

oldest

votes


















1














You can use find_peaks as follows (make sure that you work with SciPy >= 1.1):



from scipy.signal import find_peaks
import numpy as np
import matplotlib.pyplot as plt

A = np.array([0., 1., 3., 8., 6., 16., 29., 29., 47., 42., 56.,
44., 49., 40., 34., 34., 26., 24., 25., 21., 22., 24.,
34., 17., 17., 29., 24., 26., 13., 25., 16., 19., 19.,
26., 24., 26., 41., 34., 24., 37., 37., 39., 34., 40.,
45., 50., 28., 45., 43., 46., 47., 41., 30., 23., 19.,
15., 9., 10., 3., 6., 4., 1., 1., 0.])

peaks, _ = find_peaks(A, distance=32)
print(peaks)
# This prints the desired indices [10 45]

plt.plot(A)
plt.plot(peaks, A[peaks], "x")
plt.show()


So, instead of using widths, you use distance which defines the minimum distance between two peaks.



This will plot:



enter image description here



As you can see, only the two desired peaks are marked by a x and all remaining local maxima are ignored.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378016%2ffind-local-maxima-using-scipy-signal%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can use find_peaks as follows (make sure that you work with SciPy >= 1.1):



    from scipy.signal import find_peaks
    import numpy as np
    import matplotlib.pyplot as plt

    A = np.array([0., 1., 3., 8., 6., 16., 29., 29., 47., 42., 56.,
    44., 49., 40., 34., 34., 26., 24., 25., 21., 22., 24.,
    34., 17., 17., 29., 24., 26., 13., 25., 16., 19., 19.,
    26., 24., 26., 41., 34., 24., 37., 37., 39., 34., 40.,
    45., 50., 28., 45., 43., 46., 47., 41., 30., 23., 19.,
    15., 9., 10., 3., 6., 4., 1., 1., 0.])

    peaks, _ = find_peaks(A, distance=32)
    print(peaks)
    # This prints the desired indices [10 45]

    plt.plot(A)
    plt.plot(peaks, A[peaks], "x")
    plt.show()


    So, instead of using widths, you use distance which defines the minimum distance between two peaks.



    This will plot:



    enter image description here



    As you can see, only the two desired peaks are marked by a x and all remaining local maxima are ignored.






    share|improve this answer






























      1














      You can use find_peaks as follows (make sure that you work with SciPy >= 1.1):



      from scipy.signal import find_peaks
      import numpy as np
      import matplotlib.pyplot as plt

      A = np.array([0., 1., 3., 8., 6., 16., 29., 29., 47., 42., 56.,
      44., 49., 40., 34., 34., 26., 24., 25., 21., 22., 24.,
      34., 17., 17., 29., 24., 26., 13., 25., 16., 19., 19.,
      26., 24., 26., 41., 34., 24., 37., 37., 39., 34., 40.,
      45., 50., 28., 45., 43., 46., 47., 41., 30., 23., 19.,
      15., 9., 10., 3., 6., 4., 1., 1., 0.])

      peaks, _ = find_peaks(A, distance=32)
      print(peaks)
      # This prints the desired indices [10 45]

      plt.plot(A)
      plt.plot(peaks, A[peaks], "x")
      plt.show()


      So, instead of using widths, you use distance which defines the minimum distance between two peaks.



      This will plot:



      enter image description here



      As you can see, only the two desired peaks are marked by a x and all remaining local maxima are ignored.






      share|improve this answer




























        1












        1








        1







        You can use find_peaks as follows (make sure that you work with SciPy >= 1.1):



        from scipy.signal import find_peaks
        import numpy as np
        import matplotlib.pyplot as plt

        A = np.array([0., 1., 3., 8., 6., 16., 29., 29., 47., 42., 56.,
        44., 49., 40., 34., 34., 26., 24., 25., 21., 22., 24.,
        34., 17., 17., 29., 24., 26., 13., 25., 16., 19., 19.,
        26., 24., 26., 41., 34., 24., 37., 37., 39., 34., 40.,
        45., 50., 28., 45., 43., 46., 47., 41., 30., 23., 19.,
        15., 9., 10., 3., 6., 4., 1., 1., 0.])

        peaks, _ = find_peaks(A, distance=32)
        print(peaks)
        # This prints the desired indices [10 45]

        plt.plot(A)
        plt.plot(peaks, A[peaks], "x")
        plt.show()


        So, instead of using widths, you use distance which defines the minimum distance between two peaks.



        This will plot:



        enter image description here



        As you can see, only the two desired peaks are marked by a x and all remaining local maxima are ignored.






        share|improve this answer















        You can use find_peaks as follows (make sure that you work with SciPy >= 1.1):



        from scipy.signal import find_peaks
        import numpy as np
        import matplotlib.pyplot as plt

        A = np.array([0., 1., 3., 8., 6., 16., 29., 29., 47., 42., 56.,
        44., 49., 40., 34., 34., 26., 24., 25., 21., 22., 24.,
        34., 17., 17., 29., 24., 26., 13., 25., 16., 19., 19.,
        26., 24., 26., 41., 34., 24., 37., 37., 39., 34., 40.,
        45., 50., 28., 45., 43., 46., 47., 41., 30., 23., 19.,
        15., 9., 10., 3., 6., 4., 1., 1., 0.])

        peaks, _ = find_peaks(A, distance=32)
        print(peaks)
        # This prints the desired indices [10 45]

        plt.plot(A)
        plt.plot(peaks, A[peaks], "x")
        plt.show()


        So, instead of using widths, you use distance which defines the minimum distance between two peaks.



        This will plot:



        enter image description here



        As you can see, only the two desired peaks are marked by a x and all remaining local maxima are ignored.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 27 '18 at 15:58

























        answered Nov 23 '18 at 8:27









        ClebCleb

        10.8k125482




        10.8k125482






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378016%2ffind-local-maxima-using-scipy-signal%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)