波峰波谷的判断

版权声明:版权归Swordboris所有,转载必须注明出处 https://blog.csdn.net/boris_wang2008/article/details/81209849

给定一个数组h, 从左往右扫描。 

用S记录当前的状态(未知0, 下坡1,上坡2) 

当S=0, 如果 h[i] >  h[i+1]  修改状态为 下坡1, 否则为上坡 2 

当S=1, 如果 h[i]  <  h[i+1], 则判断为由下坡变为上坡, 此处为一个波谷。 如果该波谷比上一个rangesize范围内的波谷更低,则修改上一个波谷的值。 否则就将该波谷加入波谷列表。 

当S=2, 如果 h[i]  > h[i+1], 则判断为由上坡变为下坡, 此处为一个波峰。 如果该波峰比上一个rangesize范围内的波峰更高,则修改上一个波谷的值。 否则就将该波峰加入波峰列表。 

最后返回波峰和波谷列表。 

 

 

import numpy as np

def get_peaks_troughs(h,rangesize):
    peaks = list()
    troughs = list()
    S = 0
    for x in range(1,len(h)-1):
        if S == 0:
            if h[x] > h[x+1]:
                S = 1 ## down
            else:
                S = 2 ## up
        elif S == 1:
            if h[x] < h[x+1]:
                S = 2
                ## from down to up
                if len(troughs):
                    ## check if need merge
                    (prev_x,prev_trough) =  troughs[-1]
                    if x - prev_x < rangesize:
                        if prev_trough > h[x]:
                            troughs[-1] = (x,h[x])
                    else:
                        troughs.append((x,h[x])) 
                else:
                    troughs.append((x,h[x]))                           
 
          
        elif S == 2:
            if h[x] > h[x+1]:
                S = 1
                ## from up to down
                if len(peaks):
                    prev_x,prev_peak =  peaks[-1]
                    if x - prev_x < rangesize:
                        if prev_peak < h[x]:
                            peaks[-1] = (x,h[x])
                    else:
                        peaks.append((x,h[x]))
                else:
                    peaks.append((x,h[x]))
                
    return peaks,troughs
                
        
if __name__ == "__main__":
    import cv2 as cv
    import numpy as np
    from matplotlib import pyplot as plt


    img = cv.imread("peak.png",0)
    img =  255 - img

    Y,X = img.shape
    print Y,X

    h = np.zeros((X,1))
    for i in range(X):
        for j in range(Y):
            if img[j,i] > 0:
                h[i] = Y - j  

    peaks,troughs = get_peaks_troughs(h,10)

    plt.subplot(1,2,1)
    plt.imshow(img)
    plt.subplot(1,2,2)
    plt.plot(np.arange(len(h)),h)
    for x,y in peaks:
        plt.text(x,y,y,fontsize=10,verticalalignment="bottom",horizontalalignment="center")
    for x,y in troughs:
        plt.text(x,y,y,fontsize=10,verticalalignment="top",horizontalalignment="center")

    plt.show()
 

peaks,troughs = get_peaks_troughs(h,50)  ##  跨度修改成50后

跨度为50

 

波峰波谷

06-24

[img=http://img.bbs.csdn.net/upload/201306/24/1372071142_126818.jpg][/img]rnrn问题描述:rn对于任意给出的一组随机数列,求出离给出点和指定区间(即给出点往前多少个单位)最近的上波浪或下波浪,结果表示为两个点的坐标。rn如给出的点在B与C之间,给出的指定区间为30,则答案为(3,5.15)和(24,3.3),即A、B两个点的坐标。rn给出的点在E、F之间,给定的区间为30,则答案为(54,3.3)和(62,4.55),即D、E两点的坐标rnrn大家能帮忙想一下吗?rnrn数据:rn(1,4.6)(2,4.7)(3,5.15)(4,5.05)(5,5.1)(6,4.8)(7,4.7)(8,4.65)(9,4.7)(10,4.4)(11,4.0)(12,3.85)(13,3.65)(14,3.7)(15,3.85)(16,4.05)(17,3.8)(18,3.95)(19,4.0)(20,3.9)(21,3.65)(22,3.85)(23,3.5)(24,3.3)(25,3.3)(26,3.35)(27,3.35)(28,3.55)(29,3.45)(30,3.75)(31,4.15)(32,4.0)(33,4.05)(34,4.25)(35,4.1)(36,3.85)(37,3.95)(38,4.15)(39,4.0)(40,4.0)(41,3.75)(42,3.35)(43,3.25)(44,3.9)(45,4.0)(46,4.0)(47,4.05)(48,3.85)(49,3.85)(50,3.45)(51,3.4)(52,3.45)(53,3.4)(54,3.3)(55,3.7)(56,4.0)(57,4.15)(58,3.95)(59,4.2)(60,4.3)(61,4.45)(62,4.55)(63,4.5)(64,3.95)(65,3.8)(66,3.75)(67,3.7)(68,3.75)(69,3.75)(70,3.8)(71,3.75)(72,3.7)(73,3.75)(74,3.6)(75,3.2)(76,3.25)(77,3.2)(78,3.05)(79,2.8)(80,2.9)(81,2.75)(82,2.85)(83,2.85)(84,2.85)(85,2.85)(86,3.15)(87,3.6)(88,3.9)(89,3.95)(90,4.05)(91,3.75)(92,3.75)(93,4.15)(94,4.15)(95,4.35)(96,4.1)(97,4.0)(98,4.1)(99,4.4)(100,4.25)(101,4.35)(102,4.15)(103,4.15)(104,4.1)(105,4.2)(106,4.0)(107,3.9)(108,3.65)(109,3.85)(110,3.7)(111,3.75)(112,3.8)(113,3.35)(114,3.55)(115,3.35)(116,3.3)(117,3.5)(118,3.6)(119,3.35)(120,3.3)(121,3.55)(122,3.75)(123,3.75)(124,4.0)(125,4.0)(126,3.95)(127,3.7)(128,3.6)(129,3.4)(130,3.65)(131,3.6)(132,3.55)(133,3.6)(134,3.3)(135,3.55)(136,3.55)(137,3.2)(138,3.1)(139,3.1)(140,3.2)(141,3.35)(142,3.2)(143,3.45)(144,3.2)(145,3.15)(146,3.25)(147,3.2)(148,3.5)(149,3.5)(150,3.4)(151,3.5)(152,4.1)(153,4.3)(154,4.35)(155,4.1)(156,4.0)(157,4.0)(158,4.15)(159,4.2)(160,4.2)(161,3.75)(162,3.8)(163,3.55)(164,3.7)(165,3.75)(166,3.6)(167,3.55)(168,3.35)(169,3.35)(170,3.3)(171,3.3)(172,2.7)(173,2.6)(174,2.6)(175,2.85)(176,3.05)(177,3.35)(178,3.1)(179,3.0)(180,3.05)(181,3.55)(182,3.7)(183,3.7)(184,4.2)(185,4.1)(186,4.2)(187,4.2)(188,4.1)(189,4.3)(190,4.25)(191,4.35)(192,4.45)(193,4.6)(194,4.6)(195,4.35)(196,4.4)(197,4.45)(198,4.45)(199,4.7)(200,4.75)(201,4.8)(202,4.6)(203,4.65)(204,4.05)(205,4.1)(206,4.05)(207,4.25)(208,4.3)(209,4.45)(210,4.55)(211,4.5)(212,4.65)(213,4.3)(214,4.75)(215,4.9)(216,4.6)(217,4.25)(218,4.5)(219,4.25)(220,4.35)(221,3.9)(222,4.15)(223,4.45)(224,4.5)(225,4.45)(226,4.35)(227,4.4)(228,4.35)(229,4.0)(230,3.9)(231,3.75)(232,3.7)(233,3.75)(234,3.45)(235,3.5)(236,3.5)(237,4.2)(238,4.0)(239,4.1)(240,3.85)(241,3.7)(242,3.4)(243,3.15)(244,3.1)(245,3.1)(246,3.4)(247,3.6)(248,3.7)(249,3.8)(250,3.85)(251,3.85)(252,3.7)(253,3.7)(254,3.65)(255,3.75)(256,4.0)(257,3.45)(258,3.55)(259,3.8)(260,3.95)(261,3.95)(262,4.0)(263,4.15)(264,4.2)(265,4.25)(266,3.95)(267,3.6)(268,3.5)(269,3.55)(270,3.45)(271,3.65)(272,3.9)(273,3.9)(274,4.2)(275,3.95)(1,4.6)(2,4.7)(3,5.15)(4,5.05)(5,5.1)(6,4.8)(7,4.7)(8,4.65)(9,4.7)(10,4.4)(11,4.0)(12,3.85)(13,3.65)(14,3.7)(15,3.85)(16,4.05)(17,3.8)(18,3.95)(19,4.0)(20,3.9)(21,3.65)(22,3.85)(23,3.5)(24,3.3)(25,3.3)(26,3.35)(27,3.35)(28,3.55)(29,3.45)(30,3.75)(31,4.15)(32,4.0)(33,4.05)(34,4.25)(35,4.1)(36,3.85)(37,3.95)(38,4.15)(39,4.0)(40,4.0)(41,3.75)(42,3.35)(43,3.25)(44,3.9)(45,4.0)(46,4.0)(47,4.05)(48,3.85)(49,3.85)(50,3.45)(51,3.4)(52,3.45)(53,3.4)(54,3.3)(55,3.7)(56,4.0)(57,4.15)(58,3.95)(59,4.2)(60,4.3)(61,4.45)(62,4.55)(63,4.5)(64,3.95)(65,3.8)(66,3.75)(67,3.7)(68,3.75)(69,3.75)(70,3.8)(71,3.75)(72,3.7)(73,3.75)(74,3.6)(75,3.2)(76,3.25)(77,3.2)(78,3.05)(79,2.8)(80,2.9)(81,2.75)(82,2.85)(83,2.85)(84,2.85)(85,2.85)(86,3.15)(87,3.6)(88,3.9)(89,3.95)(90,4.05)(91,3.75)(92,3.75)(93,4.15)(94,4.15)(95,4.35)(96,4.1)(97,4.0)(98,4.1)(99,4.4)(100,4.25)(101,4.35)(102,4.15)(103,4.15)(104,4.1)(105,4.2)(106,4.0)(107,3.9)(108,3.65)(109,3.85)(110,3.7)(111,3.75)(112,3.8)(113,3.35)(114,3.55)(115,3.35)(116,3.3)(117,3.5)(118,3.6)(119,3.35)(120,3.3)(121,3.55)(122,3.75)(123,3.75)(124,4.0)(125,4.0)(126,3.95)(127,3.7)(128,3.6)(129,3.4)(130,3.65)(131,3.6)(132,3.55)(133,3.6)(134,3.3)(135,3.55)(136,3.55)(137,3.2)(138,3.1)(139,3.1)(140,3.2)(141,3.35)(142,3.2)(143,3.45)(144,3.2)(145,3.15)(146,3.25)(147,3.2)(148,3.5)(149,3.5)(150,3.4)(151,3.5)(152,4.1)(153,4.3)(154,4.35)(155,4.1)(156,4.0)(157,4.0)(158,4.15)(159,4.2)(160,4.2)(161,3.75)(162,3.8)(163,3.55)(164,3.7)(165,3.75)(166,3.6)(167,3.55)(168,3.35)(169,3.35)(170,3.3)(171,3.3)(172,2.7)(173,2.6)(174,2.6)(175,2.85)(176,3.05)(177,3.35)(178,3.1)(179,3.0)(180,3.05)(181,3.55)(182,3.7)(183,3.7)(184,4.2)(185,4.1)(186,4.2)(187,4.2)(188,4.1)(189,4.3)(190,4.25)(191,4.35)(192,4.45)(193,4.6)(194,4.6)(195,4.35)(196,4.4)(197,4.45)(198,4.45)(199,4.7)(200,4.75)(201,4.8)(202,4.6)(203,4.65)(204,4.05)(205,4.1)(206,4.05)(207,4.25)(208,4.3)(209,4.45)(210,4.55)(211,4.5)(212,4.65)(213,4.3)(214,4.75)(215,4.9)(216,4.6)(217,4.25)(218,4.5)(219,4.25)(220,4.35)(221,3.9)(222,4.15)(223,4.45)(224,4.5)(225,4.45)(226,4.35)(227,4.4)(228,4.35)(229,4.0)(230,3.9)(231,3.75)(232,3.7)(233,3.75)(234,3.45)(235,3.5)(236,3.5)(237,4.2)(238,4.0)(239,4.1)(240,3.85)(241,3.7)(242,3.4)(243,3.15)(244,3.1)(245,3.1)(246,3.4)(247,3.6)(248,3.7)(249,3.8)(250,3.85)(251,3.85)(252,3.7)(253,3.7)(254,3.65)(255,3.75)(256,4.0)(257,3.45)(258,3.55)(259,3.8)(260,3.95)(261,3.95)(262,4.0)(263,4.15)(264,4.2)(265,4.25)(266,3.95)(267,3.6)(268,3.5)(269,3.55)(270,3.45)(271,3.65)(272,3.9)(273,3.9)(274,4.2)(275,3.95)

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试