西南交通大学研究生通信建模作业程序集锦

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

11.//生成高斯分布随机数#includestdio.h#includestdlib.h#includemath.h#definePI3.141592654doublegaussrand(){staticdoubleU1,U2;staticintphase=0;doubleZ,X;doublem=0,sd=1;//m为均值,sd是方差if(phase==0){U1=rand()/(RAND_MAX+1.0);U2=rand()/(RAND_MAX+1.0);Z=sqrt(-2.0*log(U1))*sin(2.0*PI*U2);}else{Z=sqrt(-2.0*log(U1))*cos(2.0*PI*U2);}phase=1-phase;X=m+(Z*sd);returnX;}intmain(){inti=0;for(i=0;i1000;i++){printf(%f\n,gaussrand());}}2.//生成瑞利分布随机数#includestdio.h#includestdlib.h#includemath.hdoubleRayleigh(){staticdoubleU1;doublesigma,R;U1=rand()/(RAND_MAX+1.0);sigma=1;R=sigma*sqrt(-2*log(U1));//瑞利分布的公式returnR;}intmain(){inti=0;for(i=0;i1000;i++){printf(%f\t,Rayleigh());}}3.泊松分布:#includestdio.h#includestdlib.h#includectime#includemath.h//GeneratetherandomnumberslongPossion(doubleu){doubleP,U;longN;u=exp(-u);P=1;N=0;while(1){U=(double)rand()/RAND_MAX;P=P*U;if(P=u)break;N++;}returnN;}//Generate10000distributedrandomoutputnumberswhichareputintoatextcalled”312”voidmain(){intk;doublelamda;scanf(%lf,&lamda);2FILE*fp1;fp1=fopen(p1.txt,w);srand((unsigned)time(0));for(k=1;k10000;k++){fprintf(fp1,%6ld,Possion(lamda));}fclose(fp1);}3.//莱斯分布随机变量#define_CRT_SECURE_NO_WARNINGS#includestdio.h#includestdlib.h#includemath.h#definePI3.141592654doubleGaussrand(doublem,doublesd){doubleU1,U2;doubler1,r2,z,r;U1=rand()/(RAND_MAX+1.0);U2=rand()/(RAND_MAX+1.0);r1=sqrt(-2*log(U1))*cos(U2*PI*2);r2=sqrt(-2*log(U1))*sin(U2*PI*2);z=m+r1*sd;returnz;}intmain(){doubleRicianrand;for(inti=0;i10000;i++){doubley1=Gaussrand(0,1.0);doubley2=Gaussrand(0,1.0);//生成N(0,1)的高斯分布变量,考试时可以用awgn()代替Ricianrand=sqrt(y2*y2+(1+y1)*(1+y1));//1代表A,可以修改printf(%f\t,Ricianrand);}system(pause);}4.//卡方分布随机变量#define_CRT_SECURE_NO_WARNINGS#includestdio.h#includestdlib.h#includemath.h#definePI3.1415926doublechispuare(intk){doubleU1,U2,U3;doubler1,r2;doublechis=0.0;for(inti=0;ik;i++){U1=rand()/(RAND_MAX+1.0);U2=rand()/(RAND_MAX+1.0);r1=sqrt(-2*log(U1))*cos(U2*PI*2);r2=sqrt(-2*log(U1))*sin(U2*PI*2);chis+=r1*r1;}returnchis;}voidmain4(){doublernd;charpath[100]=C:\\Users\\mtf\\Desktop\\1.txt;FILE*fpw=fopen(path,w);for(inti=0;i10000;i++){fprintf(fpw,%f\t,chispuare(5));}fclose(fpw);system(pause);}5./*指数分布*/ExponentialDistribution#includestdio.h#includestdlib.h#includetime.h#includemath.hdoublee(doublelambda){doublepV=0.0;while(true){pV=(double)rand()/(double)RAND_MAX;if(pV!=1){3break;}}pV=(-1.0/lambda)*log(pV);returnpV;}voidmain(){FILE*fp;fp=fopen(e.txt,w);inti;doubleb[10000];srand((unsigned)time(NULL));for(i=0;i10000;i++)//产生10000个指数分布随机数{b[i]=e(0.5);printf(%8.2f,b[i]);fprintf(fp,%6.2f,b[i]);}fclose(fp);}6/*Beita分布*/doublecls_random::randomBeta(doublealpha,doublebeta){/*Johnk'sbetagenerator*/doubleu,v;doublex,y;do{u=cls_random::randomUniform();v=cls_random::randomUniform();x=pow(u,1/alpha);y=pow(v,1/beta);}while(x+y1);returnx/(x+y);}(八)Lognormal分布#includestdio.h#includestdlib.h#includetime.h#includemath.hdoublenormal(doublem,doublev)//产生均值为mean,方差为variance的高斯分布的函数{doubleu1,u2,v1,v2,w,n,y,z,mean,variance;do{u1=rand()/32767.0;u2=rand()/32767.0;mean=log((m*m)/sqrt(m*m+v*v));variance=log(1+(v*v)/(m*m));v1=2.0*u1-1;v2=2.0*u2-1;w=pow(v1,2)+pow(v2,2);}while(w1);n=v1*(sqrt((-2*log(w))/w));y=sqrt(variance)*n+mean;returnz=exp(y);//}voidmain(){FILE*fp;inti;srand((unsigned)time(NULL));fp=fopen(n(-2.5,1).txt,w);for(i=0;i5000;i++){printf(%8.2f,normal(0,1));//fprintf(fp,%6.2f,normal(0,1));}fclose(fp);}//几何分布Geometricdistribution#includeiostream#includestring#includecstdlib#includecmathusingnamespacestd;doublegeom(doublep)//几何分布{doublernd=0;doubleX=0.0;doubleU=(double)rand()/(double)RAND_MAX;rnd=1+ln(U)/ln(1-p);returnrnd;}intmain(){doublerandomGemo=0.0;for(inti=0;i10000;i++)//生成10000组几何分布的随机数4{randomGemo=geom(p);//coutrandomGemo,;}return0;}//二项分布binomial#define_CRT_SECURE_NO_WARNINGS#includestdio.h#includestdlib.h#includemath.hdoublebinomial(doublen,doublep){doublernd=0;for(inti=0;in;i++){doubleU=rand()/(RAND_MAX+1.0);if(Up){rnd++;}}returnrnd;}voidmain3(){charpath[100]=C:\\Users\\mtf\\Desktop\\1.txt;FILE*fpw=fopen(path,w);for(inti=0;i10000;i++){fprintf(fpw,%.2f\t,binomial(50.0,0.35));}fclose(fpw);system(pause);}4.Designa128thorderFIRband-passdigitalfilterwithcut-offfrequencies10MHz&50MHzApplyittoasignal,anddisplayrelatedfilterandsignalwaveforms.fc=1000;%载波频率Êfc=1000HZqifs=280;%采样频率fs=280HZt=1:2:1000;x=sin(2*pi*t*fc);%generatesignalwaveforms.a=fir1(128,[1050]/(qifs/2));%Bandpassbutt.freqz(a,1);%plotgain&phase(thebarrayis1forFIRfilter)y=filter(a,1,x);%FIRband-passfilterdesign.//Betadistributionvariatebeta(a1,a2)#includeiostream#includestring#includecstdlib#includecmathusingnamespacestd;doubleBeta_distribution(doublea1,doublea2)//Betadistributionvariatebeta(a1,a2),{doubledv=0.0;doubleY1=0.0,Y2=0.0,X=0.0;Y1=gamma(a1,1);Y2=gamma(a2,1);X=Y1/(Y1+Y2);returnX;}intmain(){doublerandoms=0.0;for(inti=0;i10000;i++)//生成10000组Beta_distribution的随机数{randoms=Beta_distribution(a1,a2);//coutrandomBin,;}return0;}5.Designa4thorderIIRlow-passfilterwith

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功