資源簡介
AR模型的c++程序,AR模型是最常用的線性模型之一。
代碼片段和文件信息
//using?namespace?std;
int?AutoRegression(
???double???*inputseries
???int??????length
???int??????degree
???double???*coefficients
???int??????method)
{
???double?mean;
???int?i?t;????????????
???double?*w=NULL;??????/*?Input?series?-?mean????????????????????????????*/
???double?*h=NULL;?
???double?*g=NULL;??????/*?Used?by?mempar()??????????????????????????????*/
???double?*per=NULL;?
???double?*pef=NULL;??????/*?Used?by?mempar()??????????????????????????????*/
???double?**ar=NULL;??????/*?AR?coefficients?all?degrees??????????????????*/
???/*?Allocate?space?for?working?variables?*/
???if?((w?=?(double?*)malloc(length*sizeof(double)))?==?NULL)?{
fprintf(stderr“Unable?to?malloc?memory?-?fatal!\n“);
exit(-1);
???}
???if?((h?=?(double?*)malloc((degree+1)*sizeof(double)))?==?NULL)?{
??????fprintf(stderr“Unable?to?malloc?memory?-?fatal!\n“);
??????exit(-1);
???}
???if?((g?=?(double?*)malloc((degree+2)*sizeof(double)))?==?NULL)?{
??????fprintf(stderr“Unable?to?malloc?memory?-?fatal!\n“);
??????exit(-1);
???}
???if?((per?=?(double?*)malloc((length+1)*sizeof(double)))?==?NULL)?{
??????fprintf(stderr“Unable?to?malloc?memory?-?fatal!\n“);
??????exit(-1);
???}
???if?((pef?=?(double?*)malloc((length+1)*sizeof(double)))?==?NULL)?{
??????fprintf(stderr“Unable?to?malloc?memory?-?fatal!\n“);
??????exit(-1);
???}
???if?((ar?=?(double?**)malloc((degree+1)*sizeof(double*)))?==?NULL)?{
??????fprintf(stderr“Unable?to?malloc?memory?-?fatal!\n“);
??????exit(-1);
???}
???for?(i=0;i ??????if?((ar[i]?=?(double?*)malloc((degree+1)*sizeof(double)))?==?NULL)?{
?????? fprintf(stderr“Unable?to?malloc?memory?-?fatal!\n“);
?????? exit(-1);
??????}
???}
???/*?Determine?and?subtract?the?mean?from?the?input?series?*/
???mean?=?0.0;
???for?(t=0;t ??????mean?+=?inputseries[t];
???mean?/=?(double)length;
???for?(t=0;t ??????w[t]?=?inputseries[t]?-?mean;
???/*?Perform?the?appropriate?AR?calculation?*/
???if?(method?==?MAXENTROPY)?{
??????if?(!ARMaxEntropy(wlengthdegreearperpefhg))?{
?????? fprintf(stderr“Max?entropy?failed?-?fatal!\n“);
?????? exit(-1);
}
??????for?(i=1;i<=degree;i++)
?????????coefficients[i-1]?=?-ar[degree][i];
???}?else?if?(method?==?LEASTSQUARES)?{
??????if?(!ARLeastSquare(wlengthdegreecoefficients))?{
?????? fprintf(stderr“Least?squares?failed?-?fatal!\n“);
?????? exit(-1);
}
???}?else?{
??????fprintf(stderr“Unknown?method\n“);
exit(-1);
???}
???if?(w?!=?NULL)
??????free(w);
???if?(h?!=?NULL)
??????free(h);
???if?(g?!=?NULL)
??????free(g);
???if?(per?!=?NULL)
??????free(per);
???if?(pef?!=?NULL)
??????free(pef);
???if?(ar?!=?NULL)?{
??????for?(i=0;i ?????????if?(ar[i]?!=?NULL)
????????????free(ar[i]);
??????free(ar);
???}
??????
???return(TRUE);
}
/*???
???Previously?called?mempar()
???Originally?in?FORTRAN?hence?the?array?offsets?of?1?Yuk.
???Original?code?from?Kay?1988?appendix?8D.
???
???Perform?Burg‘s?Maximum?Entropy?AR?parameter?estimation
???outputting?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????7100??2009-04-28?09:32??AR\ar.c
?????文件????????279??2009-04-25?16:36??AR\ar.h
?????文件???????1728??2009-04-25?16:36??AR\artest.c
?????文件???????1579??2009-04-25?16:36??AR\arview.c
?????文件???????1970??2009-04-25?16:38??AR\brug\brug.c
?????目錄??????????0??2009-04-25?16:38??AR\brug
?????目錄??????????0??2009-04-28?09:32??AR
-----------?---------??----------?-----??----
????????????????12656????????????????????7
評論
共有 條評論