can i use two functions in a same program? when i used like that not getting output for second function

3 views (last 30 days)
My main program is here
function mrner()
global M Mt hmodem bit_T
mode = 1;
M = 16;
Mt = 4;
Mr = 4;
bit_SMsym = log2(M*Mt); % number of bit per sysmbol
Nbits = bit_SMsym*1e4; % Number of bits to be simulated.
hmodem = modem.qammod('M',M, 'SymbolOrder', 'Gray','InputType', 'bit');
hdemodem = modem.qamdemod('M', M,'SymbolOrder','Gray','OutputType','bit');
Eac = (mean(hmodem.Constellation .* conj(hmodem.Constellation)));
SNR = 0 : 2 :24; % signal-to-noise ratio in dB
No= (Eac)*10.^(-SNR/10); % noise variance
L_SNR=length(SNR);
ber= zeros (L_SNR,1);
bit_R=zeros(Nbits, 1);
bit_T = randi([0 1],Nbits,1);
[mod_T ante]= SpatialMod();
mod_T = mod_T.';
for ii=1:L_SNR
for j = 1 : size(mod_T ,2)
channel = sqrt(.5)*( randn(Mr,Mt,1) + 1i*randn(Mr,Mt,1));
noise = sqrt(.5)*(randn(Mr , 1) + 1i*randn(Mr , 1))* sqrt(No(ii));
p = diag(channel'*channel);
switch mode
case 1
y = channel(:,ante(j))*(mod_T(ante(j) ,j)./sqrt(p(ante(j)))) + noise;
z = (channel'*y)./p.^.5;
case 2
y = channel(:,ante(j)) * mod_T(ante(j) ,j) + noise ;
z = (channel'*y)./p;
end
[UnUsEdVaRiAbLe_To_IgNoRe, ant_est] = max(abs(z));
bi_ant = de2bi(ant_est - 1 , log2(Mt) ,'left-msb');
bi_mod = demodulate(hdemodem, z(ant_est,1) );
bit_R( (j-1)*bit_SMsym+1 : (j-1)*bit_SMsym+bit_SMsym,1) = [bi_ant.' ; bi_mod];
end
[UnUsEdVaRiAbLe_To_IgNoRe,ber(ii,1)] = biterr(bit_T(:,1),bit_R(:));
end
figure(1);
semilogy(SNR,ber(:,1),'color',[0,0.75,0.75],'linestyle','--','LineWidth',2.4);
xlabel('SNR');
ylabel('BER');
grid on;
hold on;
end
function mrner1()
global M Mt hmodem1 bit_T
mode1 = 1;
M = 16;
Mt = 4;
Mr1 = 4;
bit_SMsym1 = log2(M*Mt); % number of bit per sysmbol
Nbits1 = bit_SMsym1*1e4; % Number of bits to be simulated.
m1=10;
P_avg1=2;
hmodem1 = modem.qammod('M',M, 'SymbolOrder', 'Gray','InputType', 'bit');
hdemodem1 = modem.qamdemod('M', M,'SymbolOrder','Gray','OutputType','bit');
Eac1 = (mean(hmodem1.Constellation .* conj(hmodem1.Constellation)));
SNR1 = 0 : 2 :24; % signal-to-noise ratio in dB
No1= (Eac1)*10.^(-SNR1/10); % noise variance
L_SNR1=length(SNR1);
ber1= zeros (L_SNR1,1);
bit_R1=zeros(Nbits1, 1);
bit_T = randi([0 1],Nbits1,1);
[mod_T ante]= SpatialMod1();
mod_T = mod_T.';
for pp=1:L_SNR1
for f = 1 : size(mod_T ,2)
nak1 = random('gam',m1,P_avg1/m1,[Mr1,Mt ,1]); % generation of RV
nakagami1=sqrt(nak1); % generation of gamma RV
phase1=random('unif',-pi,pi,[Mr1,Mt ,1]); % uniform distributed phase
channel1=nakagami1.*exp(1i*phase1);% nakagami channel
noise1 = sqrt(.5)*(randn(Mr1 , 1) + 1i*randn(Mr1 , 1))* sqrt(No1(pp));
p1 = diag(channel'*channel);
switch mode
case 1
*bold* y1 = channel(:,ante(f))*(mod_T(ante(f) ,f)./sqrt(p1(ante(f)))) + noise1;
z1 = (channel1'*y1)./p1.^.5;
case 2
y1 = channel1(:,ante(f)) * mod_T(ante(f) ,f) + noise1 ;
z1 = (channel1'*y1)./p1;
end
[UnUsEdVaRiAbLe_To_IgNoRe,ant_est] = max(abs(z1));
bi_ant = de2bi(ant_est - 1 , log2(Mt) ,'left-msb');
bi_mod = demodulate(hdemodem1, z1(ant_est,1) );
bit_R1( (f-1)*bit_SMsym1+1 : (f-1)*bit_SMsym1+bit_SMsym1,1) = [bi_ant.' ; bi_mod];
end
[UnUsEdVaRiAbLe_To_IgNoRe,ber1(pp,1)] = biterr(bit_T(:,1),bit_R(:));
end
semilogy(SNR1,ber1(:,1),'color',[0,0.75,0.75],'linestyle','--','LineWidth',2);
xlabel('SNR');
ylabel('BER');
title('BER performance of MRT over Rayleigh and Nakagami fading channel')
legend('MRT over Rayleigh','MRT over Nakagami');
end
*Function spatialmod used in main program is here*
function [signal ant_no]= SpatialMod()
global M Mt hmodem bit_T
Nt = log2(Mt);
Nobit = log2(M);
x1 = reshape(bit_T,Nobit+Nt,[]);
ant_no = bi2de([x1(1:Nt,:)].' , 'left-msb') + 1;
digMod = modulate(hmodem,x1(Nt+1:end,:));
signal = zeros(Mt , length(digMod));
for i = 1 : length(digMod)
signal( ant_no(i) , i) = digMod(i);
signal = signal.';
end
  2 Comments
Jan
Jan on 28 Jan 2017
Edited: Jan on 28 Jan 2017
Please use the "{} Code" button to format the code. Currently it is painful to read.
While we see a big block of code, it does not get clear to me, which lines concern your question. I assume removing the computational details by editing the question would help to understand, what your question is. Of course you can use multiple functions in a code, to be exact: you are calling multiple functions already.
dpb
dpb on 28 Jan 2017
function mrner()
global M Mt hmodem bit_T
...
This is a verybadidea; use an argument list or a structure containing the parameters need for the function instead of global
function mrner1()
global M Mt hmodem1 bit_T
....
And this is also...looks like you've duplicated a complete function and changed only data parameters inside the two copies. That also is what the parameter list is for; write (and debug and maintain) only one copy of the function and pass it the necessary data to invoke multiple cases.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!