MATLAB-生成高斯噪声wgn函数

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

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

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

资源描述

functiony=wgn(varargin)%WGNGeneratewhiteGaussiannoise.%Y=WGN(M,N,P)generatesanM-by-NmatrixofwhiteGaussiannoise.%PspecifiesthepoweroftheoutputnoiseindBW.%%Y=WGN(M,N,P,IMP)specifiestheloadimpedanceinOhms.%%Y=WGN(M,N,P,IMP,STATE)resetsthestateofRANDNtoSTATE.%%Additionalflagsthatcanfollowthenumericargumentsare:%%Y=WGN(...,POWERTYPE)specifiestheunitsofP.POWERTYPEcan%be'dBW','dBm'or'linear'.LinearpowerisinWatts.%%Y=WGN(...,OUTPUTTYPE);Specifiestheoutputtype.OUTPUTTYPEcan%be'real'or'complex'.Iftheoutputtypeiscomplex,thenP%isdividedequallybetweentherealandimaginarycomponents.%%Example1:%%Togeneratea1024-by-1vectorofcomplexnoisewithpower%%of5dBmacrossa50Ohmload,use:%Y=wgn(1024,1,5,50,'dBm','complex')%%Example2:%%Togeneratea256-by-5matrixofrealnoisewithpower%%of10dBWacrossa1Ohmload,use:%Y=wgn(256,5,10,'real')%%Example3:%%Togeneratea1-by-10vectorofcomplexnoisewithpower%%of3Wattsacrossa75Ohmload,use:%Y=wgn(1,10,3,75,'linear','complex')%%SeealsoRANDN,AWGN.%Copyright1996-2008TheMathWorks,Inc.%$Revision:1.11.4.5$$Date:2008/08/0112:17:45$%---Initialcheckserror(nargchk(3,7,nargin,'struct'));%---Valuesetindicators(usedforthestrings)pModeSet=0;cplxModeSet=0;%---Setdefaultvaluesp=[];row=[];col=[];pMode='dbw';imp=1;cplxMode='real';seed=[];%---PlaceholdersforthenumericandstringindexvaluesnumArg=[];strArg=[];%---Identifystringandnumericarguments%Anemptyinposition4(Impedance)or5(Seed)areconsiderednumericforn=1:narginif(isempty(varargin{n}))switchncase4if(ischar(varargin{n}))error('comm:wgn:InvalidDefaultImp','Thedefaultimpedanceshouldbemarkedby[].');end;varargin{n}=imp;%Impedancehasadefaultvaluecase5if(ischar(varargin{n}))error('comm:wgn:InvalidNumericInput','Thedefaultseedshouldbemarkedby[].');end;varargin{n}=[];%Seedhasnodefaultotherwisevarargin{n}='';end;end;%---Assignthestringandnumericvectorsif(ischar(varargin{n}))strArg(size(strArg,2)+1)=n;elseif(isnumeric(varargin{n}))numArg(size(numArg,2)+1)=n;elseerror('comm:wgn:InvalidArg','Onlystringandnumericargumentsareallowed.');end;end;%---Buildthenumericargumentsetswitch(length(numArg))case3%---rowisfirst(element1),col(element2),p(element3)if(all(numArg==[123]))row=varargin{numArg(1)};col=varargin{numArg(2)};p=varargin{numArg(3)};elseerror('comm:wgn:InvalidSyntax','Illegalsyntax.')end;case4%---rowisfirst(element1),col(element2),p(element3),imp(element4)%if(all(numArg(1:3)==[123]))row=varargin{numArg(1)};col=varargin{numArg(2)};p=varargin{numArg(3)};imp=varargin{numArg(4)};elseerror('comm:wgn:InvalidSyntax','Illegalsyntax.')end;case5%---rowisfirst(element1),col(element2),p(element3),imp(element4),seed(element5)if(all(numArg(1:3)==[123]))row=varargin{numArg(1)};col=varargin{numArg(2)};p=varargin{numArg(3)};imp=varargin{numArg(4)};seed=varargin{numArg(5)};elseerror('comm:wgn:InvalidSyntax','Illegalsyntax.');end;otherwiseerror('comm:wgn:InvalidSyntax','Illegalsyntax.');end;%---Buildthestringargumentsetforn=1:length(strArg)switchlower(varargin{strArg(n)})case{'dbw''dbm''linear'}if(~pModeSet)pModeSet=1;pMode=lower(varargin{strArg(n)});elseerror('comm:wgn:TooManyPowerTypes','ThePowermodemustonlybesetonce.');end;case{'db'}error('comm:wgn:InvalidPowerType','Incorrectpowermodepassedin.Pleaseuse''dBW'',''dBm'',or''linear.''');case{'real''complex'}if(~cplxModeSet)cplxModeSet=1;cplxMode=lower(varargin{strArg(n)});elseerror('comm:wgn:TooManyOutputTypes','Thecomplexitymodemustonlybesetonce.');end;otherwiseerror('comm:wgn:InvalidArgOption','Unknownoptionpassedin.');end;end;%---Argumentsanddefaultshaveallbeenset,eithertotheirdefaultsorbythevaluespassedin%so,performrangeandtypechecks%---pif(isempty(p))error('comm:wgn:InvalidPowerVal','Thepowervaluemustbearealscalar.');end;if(any([~isreal(p)(length(p)1)(length(p)==0)]))error('comm:wgn:InvalidPowerVal','Thepowervaluemustbearealscalar.');end;if(strcmp(pMode,'linear'))if(p0)error('comm:wgn:NegativePower','Inlinearmode,therequirednoisepowermustbe=0.');end;end;%---Dimensionsif(any([isempty(row)isempty(col)~isscalar(row)~isscalar(col)]))error('comm:wgn:InvalidDims','Therequireddimensionsmustbereal,integerscalars1.');end;if(any([(row=0)(col=0)~isreal(row)~isreal(col)((row-floor(row))~=0)((col-floor(col))~=0)]))error('comm:wgn:InvalidDims','Therequireddimensionsmustbereal,integerscalars1.');end;%---Impedanceif(any([~isreal(imp)(length(imp)1)(length(imp)==0)any(imp=0)]))error('comm:wgn:InvalidImp','TheImpedancevaluemustbearealscalar0.');end;%---Seedif(~isempty(seed))if(any([~isreal(seed)(length(seed)1)(length(seed)==0)any((seed-floor(seed))~=0)]))error('comm:wgn:InvalidState','TheStatemustbeareal,integerscalar.');end;end;%---Allparametersarevalid,sonoextracheckingisrequiredswitchlower(pMode)case'linear'noisePower=p;case'dbw'noisePower=10^(p/10);case'dbm'noisePower=10^((p-30)/10);end;%---Generatethenoiseif(~isempty(seed))randn('state',seed);end;if(strcmp(cplxMode,'complex'))z=randn(2*row,col);y=(sqrt(imp*noisePower/2))*(z(1:row,:)+j*z(row+1:end,:));elsey=(sqrt(imp*noisePower))*randn(row,col);end;

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

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

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

×
保存成功