阈值过滤求波峰和波谷

如果要求一个数据列的波峰和波谷,我们只需要将数据遍历,找出极值点即可。然而有时候我们希望过滤掉一些小的变化,比如我们要过滤掉10%内的变化,就要复杂一点了。

一开始我们并不知道第一个极值点是波峰还波谷,就要在遍历数组的时候把最大值和最小值及其位置保存下来,如果发现在当前元素的值与极值相比达到一个阈值,则这个极值点就被确认是一个波峰或者波谷。如果确认已经找到一个波峰,就用一个变量记录下一个要找的极值点是波谷,反之亦然,直到把整个数组完全遍历。然后把最后一个没有保存的极值点保存下来。

#include <stdio.h>

#define PERCENTAGE_THRESHOLD 10
typedef enum enum_extrema_type {
    EXTREMA_TYPE_DUMMY,
    EXTREMA_TYPE_TROUGH,
    EXTREMA_TYPE_CREST,
    EXTREMA_TYPE_BOTH
} ENUM_EXTREMA_TYPE;

typedef struct _extrema_item {
    int idx;
    int val;
} extrema_item_t;

int kl[] = { 5558, 5484, 5293, 5417, 5238, 5215, 5281, 5592, 5398, 5449, 5209, 5111, 5134, 5097, 5012, 4895, 4407, 4459, 4539, 4455, 4549, 4557, 4498, 4324 };
#define KLSIZ (sizeof(kl)/sizeof(int))
#define DELTA_PERCENTAGE(vbig, vsmall) ((1000 *(vbig - vsmall) / vsmall + 5)/10)
extrema_item_t extrema[16];

int main(void)
{
    int trough, crest;
    int tidx, cidx, kidx, eidx;
    ENUM_EXTREMA_TYPE expect = EXTREMA_TYPE_BOTH;
    int closeprice;

    tidx = cidx = eidx = kidx = 0;
    trough = crest = kl[kidx];
    for(kidx=1; kidx < KLSIZ; kidx++) {
        closeprice = kl[kidx];
        if(EXTREMA_TYPE_TROUGH & expect) {
            if(trough > closeprice) {
                trough = closeprice;
                tidx = kidx;
            } else if(DELTA_PERCENTAGE(closeprice, trough) > 10) {
                printf("trough[%d]=(%d,%d),kl[%d]=%d\n", eidx, tidx, trough, kidx, closeprice);
                extrema[eidx].idx = tidx;
                extrema[eidx].val = trough;
                eidx++;
                expect = EXTREMA_TYPE_CREST;
                crest = closeprice;
                cidx = kidx;
            }
        }
        if(EXTREMA_TYPE_CREST & expect) {
            if(crest < closeprice) {
                crest = closeprice;
                cidx =  kidx;
            } else if(DELTA_PERCENTAGE(crest, closeprice) > 10) {
                printf("crest[%d]=(%d,%d),kl[%d]=%d\n", eidx, cidx, crest, kidx, closeprice);
                extrema[eidx].idx = cidx;
                extrema[eidx].val = crest;
                eidx++;
                expect = EXTREMA_TYPE_TROUGH;
                trough = closeprice;
                tidx = kidx;
            }
        }
    }
    switch(expect) {
    case EXTREMA_TYPE_TROUGH:
        printf("trough[%d]=(%d,%d)\n", eidx, tidx, trough);
        extrema[eidx].idx = tidx;
        extrema[eidx].val = trough;
        eidx++;
        break;
    case EXTREMA_TYPE_CREST:
        printf("crest[%d]=(%d,%d)\n", eidx, cidx, crest);
        extrema[eidx].idx = cidx;
        extrema[eidx].val = crest;
        eidx++;
        break;
    default:
        break;
    }
}

最后计算结果如下:

crest[0]=(7,5592),kl[14]=5012

trough[1]=(23,4324)

波峰波谷

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
出错啦
系统繁忙,请稍后再试