# 阈值过滤求波峰和波谷

#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)

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120