Clear Filters
Clear Filters

How can I use SNR vector instead of scalar in AWGN function ? Can I use arrayfun , cellfun , spfun, splitapply, bsxfun, structfun , or other ?

2 views (last 30 days)
Hi there
(I have updates in the comments)
is there a way to use SNR vector instead of scalar in AWGN ?
The reason is to avoid for loop for SNR range as it requires long computation time for large number of input samplse
N = 2^20;
EbN0_dB = (0:18);
X = randi(2,1,N)-1;
Y = awgn(X,EbN0_dB,'measured');
The above code produces the following error
Error using awgn
Expected SNR input to be a scalar.
So I have to use for loop as below
clear
N = 2^20;
EbN0_dB = (0:18);
Erate = zeros(size(EbN0_dB));
Enum = Erate ;
X = randi(2,1,N)-1;
for n = 1:numel(EbN0_dB)
Y = awgn(X,EbN0_dB(n),'measured');
Y = Y>0.5 ;
[Enum(n),Erate(n)]=biterr(X,Y);
end
semilogy(EbN0_dB,Erate)
grid
This is just a sample code. I have several hundred lines of code for different modulation scheme.
Can I use arrayfun , cellfun , spfun, splitapply, bsxfun, structfun , or other ? is it useful and faster than for loop ?
% Thanks in advance
  1 Comment
Mustafa qays
Mustafa qays on 15 Nov 2017
This is an update ... I have implemented it using bsxfun but looks like it is slower than for loop
Below is the first code using for loop for both awgn and biterr functions
tic()
clear
N = 2^21;
EbN0_dB = (0:18);
Erate = zeros(size(EbN0_dB));
Enum = Erate ;
X = randi(2,N,1)-1;
for n = 1:numel(EbN0_dB)
Y = awgn(X,EbN0_dB(n),'measured');
Y = Y>0.5 ;
[Enum(n),Erate(n)]=biterr(X,Y);
end
toc()
semilogy(EbN0_dB,Erate)
grid
Elapsed time
Elapsed time is 3.504173 seconds.
The second using for loop for awgn only
tic()
clear
N = 2^21;
EbN0_dB = (0:18);
Erate = zeros(size(EbN0_dB));
Enum = Erate ;
X = randi(2,N,1)-1;
Y = zeros(size(X).*size(EbN0_dB));
for n = 1:numel(EbN0_dB)
Y(:,n) = awgn(X,EbN0_dB(n),'measured');
end
Y = (Y>0.5)+0 ;
[Enum,Erate]=biterr(X,Y,[],'column-wise');
toc()
semilogy(EbN0_dB,Erate)
grid
Elapsed time
Elapsed time is 3.894274 seconds.
And finally , using bsxfun for awgn function
tic()
clear
N = 2^21;
EbN0_dB = (0:18);
Erate = zeros(size(EbN0_dB));
Enum = Erate ;
X = randi(2,N,1)-1;
YY = bsxfun(@(a,b) awgn(a,b,'measured'),X,EbN0_dB);
YY = (YY > 0.5)+0 ;
[Enum,Erate]=biterr(X,YY,[],'column-wise');
toc()
semilogy(EbN0_dB,Erate)
grid
Elapsed time
Elapsed time is 3.999013 seconds.
Waiting you comments and advises .

Sign in to comment.

Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!