i have code for BPSK,QPSK and 16-QAM as given below and i want to code it for 8-psk, what are changes i have to make to below code for 8-psk .
1 view (last 30 days)
Show older comments
*THE CODE IS *
function done = Space_Time_Coding_BER_Alamouti_split(nRx)
done=0; nTx = 2; % Number of Tx antennas filename = ['Alamouti_data_' num2str(nTx) 'x' num2str(nRx) ]; filevar = ['Eb_N0_dB'; 'maxEb '; 'Ber '; 'Ber4 '; 'Ber16 ']; BPSK = bin2dec(['0'; '1']); QPSK = bin2dec(['01'; '11'; '00'; '10']);
QAM16 = bin2dec(['1011'; '1001'; '0001'; '0011'; '1010'; '1000'; '0000'; '0010'; '1110'; '1100'; '0100'; '0110'; '1111'; '1101'; '0101'; '0111']); N=10^4; % Number of symbols to tx Eb_N0_dB = 0:30; % Multiple Eb/N0 values THRESHOLD = 100; % Minimum number of errors before proceeding to next SNR MAXITER=100; % Maximum number of iterrations before quiting
% Initialize some variables qam = 3; %The qam in use logM=0; % Bits per symbol k_QAM=0; % QAM factor for alphaRe=[]; % Real axis of constellation alphaIm=[]; % Real axis of constellation sqrtM = 0; % For calculating constellation k_qam = -1; % Unity transmit power scaling factor. BitErr=zeros(1,length(Eb_N0_dB)); % Array for counting biterrors SymbolErr=zeros(1,length(Eb_N0_dB)); % Array for counting biterrors maxEb = length(Eb_N0_dB); % Max SNR length indx =[]; % The index of the QAM constellation in increasing order modsetqam = []; % The current QAM constellation QAM_Matrix = []; % Matrix to help quickly calculate decision statistic blockTx = 2; % Number of transmissions per code block blockSym = 2; % Number of symbols sent per code block Rate = blockSym/blockTx; % The rate of the code block
% Convert N to even number of transmissions % For Alamouti, 2 transmissions per 2 symbols while(mod(N,blockSym)) N=N+1; end
for kk=1:3 %For each different QAM
if(kk==1)
qam = 2;
elseif(kk==2)
qam = 4;
elseif(kk==3)
qam = 16;
end
if(kk>1)
% Calculate constellation
QAMIQ = zeros(qam,1);
sqrtM = sqrt(qam);
logM = log2(qam); % bits per symbol
xmin = -sqrtM + 1;
xmax = sqrtM - 1;
ymin = -sqrtM + 1;
ymax = sqrtM - 1;
alphaRe = xmin:2:xmax
alphaIm = ymin:2:ymax;
% Put the real and complex axis together
ii=1;
for y = alphaIm
for x = alphaRe
QAMIQ(ii)= x+y*1i;
ii = ii+1;
end
end
% For faster receiver due to Matlab
QAM_Matrix = kron(QAMIQ,ones(1,N/nTx));
% To normalize transmit power to unity
% Average power for 16 qam is sqrt(10)
if(qam==16)
k_QAM = 1/sqrt(10);
[tt indx] = sort(QAM16);
mod_set = QAM16;
elseif(qam==4)
k_QAM = 1;
[tt indx] = sort(QPSK);
mod_set = QPSK;
end
else
logM = log2(qam); % bits per symbol
k_QAM = 1;
[tt indx] = sort(BPSK);
mod_set = BPSK;
QAMIQ = [-1; 1];
QAM_Matrix = kron(QAMIQ,ones(1,N/nTx));
end
% Begin simulation
for ii = 1:length(Eb_N0_dB)
countiter = 0; % Number of iterations
fprintf('\n At SNR = %d of %s \n',Eb_N0_dB(ii),strtrim(filevar(kk+2,:)));
while(THRESHOLD>BitErr(ii) && countiter<MAXITER)
countiter = countiter+1;
% TRANSMITTER SIDE
NNew=N*nTx/Rate; % New size of transmitted signal;
s = zeros(1,NNew); % space-time coded modulated transmitted sequence
% generate random signal
transmitted = randintt(N,1,[0,qam-1]); % transmitted sequence in decimal
% modulate random signal
bitmod = QAMIQ(indx(transmitted+1)) ;
% normalize transmit power to one
s_precoded = bitmod *k_QAM; % uncoded modulated transmitted sequence
% Perform Space-Time Coding
d_btwc = blockTx*nTx; % Distance between blocks
s(1:d_btwc:NNew)=s_precoded(1:blockSym:N); % s1
s(2:d_btwc:NNew)=s_precoded(2:blockSym:N); % s2
s(3:d_btwc:NNew)= -(conj(s_precoded(2:blockSym:N))); % -s2*
s(4:d_btwc:NNew)= conj(s_precoded(1:blockSym:N)); % s1*
%CHANNEL
% Expand for easy multiplication with channel matrix h
% (much faster than using matlab's matrix multiplication)
sMod = kron(s,ones(nRx,1));
% Add a third dimension for easy product multiplication
sMod = reshape(sMod,[nRx,nTx,NNew/nTx]);
% Rayleigh channel, generate one every two transmissions
h_orig = 1/sqrt(2)*(randn(nRx,nTx,N/nTx) + 1i*randn(nRx,nTx,N/nTx));
% Repeat the channel every two transmissions:
dummyarray=ones(blockTx,1);
hprime = reshape(h_orig,nRx*nTx,[]);
h=reshape(kron(dummyarray,hprime),nRx,nTx,[]);
% white gaussian noise, 0dB variance
n = 1/sqrt(2)*(randn(nRx,NNew/nTx) + 1i*randn(nRx,NNew/nTx));
% Channel and noise Noise addition
% Here we keep the signal power the same and add variance
% to the noise, which is equivalent to decreasing the signal
% and having a 0dB variance noise at the RX, then amplifying
% the signal, and therefore amplifying the noise (more
% variance).
SNR = 10^(Eb_N0_dB(ii)/10); % SNR in linear scale
variance = nTx/(2*SNR); % Variance
sigma = sqrt(variance); % Standard Deviation
y = reshape(sum(h.*sMod,2),nRx,[]) + sigma*n; % received signal
% sMod = [s1 s2 s3 s4 ... ; s1 s2 s3 s4 ...];
% sum(h.*sMod,2) multiplies each element of h with sMod and
% adds each row
% RECEIVER SIDE
% Amplify for detection
y= y./k_QAM;
% Maximum-likelihood detection
h_orig = reshape(h_orig,nRx,[]);
% Decision Statistic for s1
% r1^j = y(:,1:2:end)
% r2^j = y(:,2:2:end)
% alpha1,j = h_orig(:,1:2:end)
% alpha2,j = h_orig(:,2:2:end)
firstterm = sum(y(:,1:blockTx:end).*conj(h_orig(:,1:nTx:end))...
+conj(y(:,2:nTx:end)).*h_orig(:,2:nTx:end),1);
secondtermTemp = sum(abs(h_orig).^2,1);
secondterm = secondtermTemp(1:nTx:end);
for TxAnt = 2:nTx
secondterm = secondterm + secondtermTemp(TxAnt:nTx:end);
end
secondterm = -1 + secondterm;
% For ease of computing due to Matlab
firstterm_Matrix = kron(firstterm,ones(qam,1));
secondterm_Matrix = kron(secondterm,ones(qam,1));
%Now check for every possible s1, and get min
s1_decision = abs((firstterm_Matrix - QAM_Matrix)).^2 ...
+ secondterm_Matrix.*abs(QAM_Matrix).^2;
[s1_min s1_index] = min(s1_decision);
received_s1 = mod_set(s1_index);
% Decision Statistic for s2
% r1^j = y(:,1:2:end)
% r2^j = y(:,2:2:end)
% alpha1,j = h_orig(:,1:2:end)
% alpha2,j = h_orig(:,2:2:end)
firstterm = sum(y(:,1:blockTx:end).*conj(h_orig(:,2:blockTx:end))...
-conj(y(:,2:blockTx:end)).*h_orig(:,1:blockTx:end),1);
% For ease of computing due to Matlab
firstterm_Matrix = kron(firstterm,ones(qam,1));
%Now check for every possible s1, and get min
s2_decision = abs((firstterm_Matrix - QAM_Matrix)).^2 ...
+ secondterm_Matrix.*abs(QAM_Matrix).^2;
[s2_min s2_index] = min(s2_decision);
received_s2 = mod_set(s2_index);
received = zeros(N,1);
received(1:blockSym:end)=received_s1;
received(2:blockSym:end)=received_s2;
errors = biterrr(received,transmitted);
if(errors)
fprintf('x');
else
fprintf('.');
end
BitErr(ii) = BitErr(ii) + errors;
end
if(BitErr(ii)==0)
maxEb = ii;
break;
end
BitErr(ii) = BitErr(ii)./(N*logM*countiter);
end
if(kk==1)
Ber = BitErr;
tempname = [filename '-' strtrim(filevar(kk+2,:)) '.mat'];
save(tempname, strtrim(filevar(1,:)), ...
strtrim(filevar(2,:)), strtrim(filevar(kk+2,:)));
elseif(kk==2)
Ber4 = BitErr;
tempname = [filename '-' strtrim(filevar(kk+2,:)) '.mat'];
save(tempname, strtrim(filevar(1,:)), ...
strtrim(filevar(2,:)), strtrim(filevar(kk+2,:)));
else
Ber16 = BitErr;
tempname = [filename '-' strtrim(filevar(kk+2,:)) '.mat'];
save(tempname, strtrim(filevar(1,:)), ...
strtrim(filevar(2,:)), strtrim(filevar(kk+2,:)));
end
BitErr=zeros(1,length(Eb_N0_dB));
end
fprintf('\n');
done = 1;
PLEASE HELP ME TO SOLVE THIS,
0 Comments
Answers (1)
Eda Pejtamalli
on 4 May 2017
I can get error when I try to run it: Error: File: Untitled.m Line: 173 Column: 5 Illegal use of reserved keyword "end".
I can't fix it, any suggestions?
0 Comments
See Also
Categories
Find more on QPSK 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!