|
回复 7 # sososwim 的帖子
我是想通过FFT的结果判断序列数据的周期性……不过对于非简谐数据也不是很明显……还是用常规方法吧……
- #include "stdafx.h"
- #include <stdlib.h>
- #include <iostream>
- #include <time.h>
- #include <math.h>
- using namespace std;
- #define NN 10
- #define NEARZERO 1e-5
- void Sample_Random(double *arrSource,int arrlen)
- {
- srand((int)time(0));
- for(int i=0;i<arrlen;++i)
- cout << (*(arrSource+i) = rand()%100)<<endl;
- }
- void Sample_Cycle(double *arrSouce,int slen,double *arrCycle,int clen)
- {
- for(int i=0;i<slen;++i)
- cout << (*(arrSouce+i) = *(arrCycle+i%clen)) <<endl;
- }
- int SearchForCycle(double *arrSouce,int slen)
- {
- for (int clen =1;clen<=slen/2;++clen)
- {
- double abssum = 0.0;
- //按照位移量clen作差并求绝对值和
- for(int i=0;i<slen/2;++i)
- {
- abssum += abs( *(arrSouce+i) - *(arrSouce+i+clen) );
- if(abssum >= NEARZERO)
- break;
- }
- if(abssum < NEARZERO)
- return clen;
- }
- return -1;
- }
- int main(int argc, char* argv[])
- {
- double arrData[NN];
- double arrC3[] = {1,2,3};
- double arrC2[] = {1,2};
- int choice;
- cout<<"0]随机序列"<<"1]循环序列A"<<"2]循环序列B"<<endl;
- cin>>choice;
- system("cls");
- switch (choice)
- {
- case 0:
- Sample_Random(arrData,NN);
- break;
- case 1:
- Sample_Cycle(arrData,NN,arrC2,2);
- break;
- case 2:
- Sample_Cycle(arrData,NN,arrC3,3);
- break;
- }
- cout<<"****"<<endl;
- int clen = SearchForCycle(arrData,NN);
- if(clen >0)
- {
- cout<<"最小循环:"<<clen<<endl;
- cout<<"循环体:"<<endl;
- for(int i=0;i<clen;++i)
- {
- cout<<arrData[i]<<endl;
- }
- }
- else
- {
- cout<<"无循环体"<<endl;
- }
- system("pause");
- return 0;
- }
复制代码
|
|