huanying
MATLAB中的help文檔,是一個(gè)神奇的存在,檢索你想找的關(guān)鍵詞,會(huì)自動(dòng)檢索出與關(guān)鍵詞相關(guān)的內(nèi)容。例如:檢索一下“awgn”,我們可以得到如下圖所示的界面。
可以非常方便地查閱函數(shù)的語法定義,信號(hào)定義,以及給出example。
大部分非核心函數(shù),可以打開看到源碼,例如awgn的源碼:
function y = awgn(varargin)%AWGN Add white Gaussian noise to a signal.% Y = AWGN(X,SNR) adds white Gaussian noise to X. The SNR is in dB.% The power of X is assumed to be 0 dBW. If X is complex, then % AWGN adds complex noise.%% Y = AWGN(X,SNR,SIGPOWER) when SIGPOWER is numeric, it represents % the signal power in dBW. When SIGPOWER is 'measured', AWGN measures% the signal power before adding noise.%% Y = AWGN(X,SNR,SIGPOWER,S) uses S to generate random noise samples with% the RANDN function. S can be a random number stream specified by% RandStream. S can also be an integer, which seeds a random number% stream inside the AWGN function. If you want to generate repeatable% noise samples, then either reset the random stream input before calling% AWGN or use the same seed input.%% Y = AWGN(..., POWERTYPE) specifies the units of SNR and SIGPOWER.% POWERTYPE can be 'db' or 'linear'. If POWERTYPE is 'db', then SNR% is measured in dB and SIGPOWER is measured in dBW. If POWERTYPE is% 'linear', then SNR is measured as a ratio and SIGPOWER is measured% in Watts.%% Example 1: % % To specify the power of X to be 0 dBW and add noise to produce% % an SNR of 10dB, use:% X = sqrt(2)*sin(0:pi/8:6*pi);% Y = awgn(X,10,0);%% Example 2: % % To specify the power of X to be 3 Watts and add noise to% % produce a linear SNR of 4, use:% X = sqrt(2)*sin(0:pi/8:6*pi);% Y = awgn(X,4,3,'linear');%% Example 3: % % To cause AWGN to measure the power of X and add noise to% % produce a linear SNR of 4, use:% X = sqrt(2)*sin(0:pi/8:6*pi);% Y = awgn(X,4,'measured','linear');%% Example 4: % % To specify the power of X to be 0 dBW, add noise to produce% % an SNR of 10dB, and utilize a local random stream, use:% S = RandStream('mt19937ar','Seed',5489);% X = sqrt(2)*sin(0:pi/8:6*pi);% Y = awgn(X,10,0,S);%% Example 5: % % To specify the power of X to be 0 dBW, add noise to produce% % an SNR of 10dB, and produce reproducible results, use:% reset(RandStream.getGlobalStream)% X = sqrt(2)*sin(0:pi/8:6*pi);% Y = awgn(X,10,0);%%% See also comm.AWGNChannel, WGN, RANDN, RandStream/RANDN, and BSC. % Copyright 1996-2018 The MathWorks, Inc. %#codegen narginchk(2,5); % Validate signal inputsig = varargin{1};validateattributes(sig, {'numeric'}, ... {'nonempty'}, 'awgn', 'signal input'); % Validate SNR inputreqSNR = varargin{2};validateattributes(reqSNR, {'numeric'}, ... {'real','scalar','nonempty'}, 'awgn', 'SNR input'); % Validate signal powerif nargin >= 3 if strcmpi(varargin{3}, 'measured') sigPower = sum(abs(sig(:)).^2)/numel(sig); % linear else validateattributes(varargin{3}, {'numeric'}, ... {'real','scalar','nonempty'}, 'awgn', 'signal power input'); sigPower = varargin{3}; % linear or dB endelse sigPower = 1; % linear, defaultend % Validate state or power typeif nargin >= 4 coder.internal.errorIf(comm.internal.utilities.isCharOrStringScalar(varargin{4}) && ... all(~strcmpi(varargin{4}, {'db','linear'})), ... 'commInvalidPowerType'); isStream = ~isempty(varargin{4}) && ~comm.internal.utilities.isCharOrStringScalar(varargin{4}); if isStream && ~isa(varargin{4}, 'RandStream') % Random stream seed validateattributes(varargin{4}, {'double'}, ... {'real','scalar','nonnegative','integer','<',2^32}, ... 'awgn', 'seed input'); endelse % Default isStream = false;end % Validate power typeif nargin == 5 coder.internal.errorIf(comm.internal.utilities.isCharOrStringScalar(varargin{4}), ... % Type has been specified as the 4th input 'commInputAfterPowerType'); coder.internal.errorIf(all(~strcmpi(varargin{5}, {'db','linear'})), ... 'commInvalidPowerType'); end isLinearScale = ((nargin == 4) && ~isStream && strcmpi(varargin{4}, 'linear')) || ... ((nargin == 5) && strcmpi(varargin{5}, 'linear')); % Cross-validationcoder.internal.errorIf(isLinearScale && (sigPower < 0), ... 'commInvalidSigPowerForLinearMode'); coder.internal.errorIf(isLinearScale && (reqSNR < 0), ... 'commInvalidSNRForLinearMode'); if ~isLinearScale % Convert signal power and SNR to linear scale if (nargin >= 3) && ~comm.internal.utilities.isCharOrStringScalar(varargin{3}) % User-specified signal power sigPower = 10^(sigPower/10); end reqSNR = 10^(reqSNR/10);end noisePower = sigPower/reqSNR; if isStream if isa(varargin{4}, 'RandStream') stream = varargin{4}; elseif isempty(coder.target) stream = RandStream('shr3cong', 'Seed', varargin{4}); else stream = coder.internal.RandStream('shr3cong', 'Seed', varargin{4}); end if ~isreal(sig) noise = sqrt(noisePower/2)* (randn(stream, size(sig)) + ... 1i*randn(stream, size(sig))); else noise = sqrt(noisePower)* randn(stream, size(sig)); endelse if ~isreal(sig) noise = sqrt(noisePower/2)* (randn(size(sig)) + 1i*randn(size(sig))); else noise = sqrt(noisePower)* randn(size(sig)); endend y = sig + noise; % [EOF]
但是,如果不用MATLAB內(nèi)置的awgn函數(shù),如何用MATLAB生成AWGN噪聲?
可以自己寫一個(gè),可以找找別人寫的,站在別人的肩膀上,可以看得更遠(yuǎn)。(如果是學(xué)習(xí),建議理解awgn背后的原理和定義,然后自己寫;如果只是為了應(yīng)用,提高效率,直接用,浮躁的沉不下來的心,已經(jīng)蔓延~)
下面這份文檔,借花獻(xiàn)佛,在不用內(nèi)置的函數(shù)下,用MATLAB生成AWGN噪聲,可以參考一下。
文檔來源于https://www.gaussianwaves.com,國外的網(wǎng)站,總是這么專業(yè)!
高斯波形,信號(hào)處理,通信系統(tǒng),簡潔明了。
MATLAB 源碼:
%author - Mathuranathan Viswanathan (gaussianwaves.com%This code is part of the books: Wireless communication systems using Matlab & Digital modulations using Matlab. function [r,n,N0] = add_awgn_noise(s,SNRdB,L)%Function to add AWGN to the given signal%[r,n,N0]= add_awgn_noise(s,SNRdB) adds AWGN noise vector to signal%'s' to generate a %resulting signal vector 'r' of specified SNR%in dB. It also returns the noise vector 'n' that is added to the%signal 's' and the spectral density N0 of noise added%%[r,n,N0]= add_awgn_noise(s,SNRdB,L) adds AWGN noise vector to%signal 's' to generate a resulting signal vector 'r' of specified%SNR in dB. The parameter 'L' specifies the oversampling ratio used%in the system (for waveform simulation). It also returns the noise%vector 'n' that is added to the signal 's' and the spectral%density N0 of noise added s_temp=s; if iscolumn(s), s=s.'; end; %to return the result in same dim as 's' gamma = 10?(SNRdB/10); %SNR to linear scale if nargin==2, L=1; end %if third argument is not given, set it to 1 if isvector(s), P=L*sum(abs(s).?2)/length(s);%Actual power in the vector else %for multi-dimensional signals like MFSK P=L*sum(sum(abs(s).?2))/length(s); %if s is a matrix [MxN] end N0=P/gamma; %Find the noise spectral density if(isreal(s)), n = sqrt(N0/2)*randn(size(s));%computed noise else n = sqrt(N0/2)*(randn(size(s))+1i*randn(size(s)));%computed noise end r = s + n; %received signal if iscolumn(s_temp), r=r.'; end;%return r in original format as send
原文標(biāo)題:基礎(chǔ):如何用MATLAB生成AWGN噪聲?(附源代碼)
文章出處:【微信公眾號(hào):FPGA技術(shù)江湖】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
-
matlab
+關(guān)注
關(guān)注
185文章
2976瀏覽量
230483 -
函數(shù)
+關(guān)注
關(guān)注
3文章
4331瀏覽量
62622 -
AWGN
+關(guān)注
關(guān)注
0文章
7瀏覽量
6735
原文標(biāo)題:基礎(chǔ):如何用MATLAB生成AWGN噪聲?(附源代碼)
文章出處:【微信號(hào):HXSLH1010101010,微信公眾號(hào):FPGA技術(shù)江湖】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論