Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

OFDM Simulaton over AWGN _ Problem in Code

Status
Not open for further replies.

w_bwr

Member level 3
Joined
Feb 4, 2010
Messages
66
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Karachi, Pakistan
Activity points
1,810
Hi , i wrote a code and facing problems in it. it gives error in the line written in red color.
Plz help

??? Error using ==> xor
Operands must be real.

Error in ==> TestRun at 74
numErrors=sum(xor(RecSymbols,data)); % Count Number of Errors



Here is the code:

clear all;
clc;

% OFDM Parameters

N=256;
ofdmBW=2.5e6;
deltaf=ofdmBW/256; %data on each subcarriers
Tfft=1/deltaf; % ifft period
Tgi=Tfft/4; %guard interval duration duration of cyclic prefix 1/4rth portion of ofdm symbols
Tsignal=Tgi+Tfft; %total duration of BPSK-OFDM symbols=guard time+ifft period
Ncp=N*Tgi/Tfft; %number of symbols allocated to cyclic prefix
nbitspersymbol=N; % For BPSK, the number of Bits/Symbol is the same as N

% Simulation Parameters

samples=100; %Number of the OFDM Symbols to transmit
EbNOdB= -10:2:10; % Bit to noise ratio
EsNOdB= EbNOdB + 10*log10(N/N) + 10*log10(N/(Ncp+N)); % Symbol to Noise Ratio

SimulatedBER = zeros(1,length(EsNOdB));
TheoreticalBER = zeros(1,length(EsNOdB));

% Monte Carlo Simulation
for i=1:length(EsNOdB),

%Transmitter

data=randint(1,N*samples);
A=reshape(data,samples,N);
s=2*A-1; % BPSK Modulation
d_ifft=ifft(s); % Apply IFFT
ofdm_signal=[d_ifft:),N-Ncp+1:N) d_ifft]; % Adding Cyclic Prefix

[rows cols]=size(ofdm_signal);

%Parallel to serial conversion for transmission

ofdm_signal=reshape(ofdm_signal.',1,rows*cols);

%Zero mean unit variance AWGN channel

Tdata=awgn(ofdm_signal,i); % AWGN added

%Receiver

RecDataP=reshape(Tdata.',(N+Ncp),samples).'; % Serial to Parallel Conversion

% Removing Cyclic Prefix
RecDataP=RecDataP:),Ncp+1:(N+Ncp));

% FFT

R_fft=fft(RecDataP);

%BPSK Demodulation

for m=1:samples
for n=1:N
if R_fft(m,n)==1
R_fft(m,n)=1;
end

if R_fft(m,n)==-1
R_fft(m,n)=0;
end
end
end
RecSymbols=reshape(R_fft,1,N*samples);
numErrors=sum(xor(RecSymbols,data)); % Count Number of Errors


simulatedBER(i)=numErrors/(100);
theoreticalBER(i)=(1/2)*erfc(sqrt(10.^(EbNOdB(i)/10)))
end
% Results

plot(EbNOdB,log10(simulatedBER),'r-o');
hold on;
plot(EbNOdB,log10(theoreticalBER),'k*');
grid on; tiltle('BER Vs SNR(db) for OFDM with BPSK Modulation Over AWGN Channel');
xlabel ('Eb/No (dB)');ylabel ('BER');legend ('simulated','theoretical');
 

Kindly replace
Code:
numErrors=sum(xor(RecSymbols,data)); % Count Number of Errors
with
Code:
numErrors=sum(RecSymbols~=data); % Count Number of Errors
 

Kindly replace
Code:
numErrors=sum(xor(RecSymbols,data)); % Count Number of Errors
with
Code:
numErrors=sum(RecSymbols~=data); % Count Number of Errors


The original "numErrors=sum(xor(RecSymbols,data));" is already right. I mean no problem with the function. Let me find the fault


Added:

I don't understand this line of code
Tdata=awgn(ofdm_signal,i); % AWGN added
and this is the error of the code. The xor function you used is already OK. But you got an error because
RecSymbols and data MUST BE real number o,1. But your RecSymbols are not.

If you wanna add noise you should do Y=Xt+noise(after scaling factor)
 
Last edited:

Thanx for your reply...

Problem was in BPSK Demodulation. I changed "Nested IF" statement with the "IF - ELSE" then code runs fine. But the result is not as expected.
I have attached the result figure. Take a look.



Tdata=awgn(ofdm_signal,i) % AWGN Added.

awgn is a built in command in matlab that adds AWGN to the input signal (ofdm_signal) for the SNR value ( i ).

But
i do want add noise as you say bcoz i guess it is the better way to do it. Can you please elaborate it a bit more.

---------- Post added at 14:27 ---------- Previous post was at 14:25 ----------
 

I tried using this:

noise=1/sqrt(2)*(randn(1,rows*cols)+1i*randn(1,rows*cols));
Tdata= sqrt((N+Ncp)/N)*ofdm_signal + 10^(-EsN0dB(i)/20)*noise;

Instead of this:
Tdata=awgn(ofdm_signal,i) % AWGN Added

but it gives the following error:

??? Undefined function or method 'EsN0dB' for input arguments of
type 'double'.

Error in ==> TestRun at 48
r= sqrt((N+Ncp)/N)*ofdm_signal + 10^(-EsN0dB(i)/20)*noise;

Plz help!!!
 

from this line

EsNOdB= EbNOdB + 10*log10(N/N) + 10*log10(N/(Ncp+N));

why second term is 10*log10(N/N)??
N/N =1 so this term is 0



And what do you use between Tdata or "r" as the transmitted data? I am confused.
 

from this line

EsNOdB= EbNOdB + 10*log10(N/N) + 10*log10(N/(Ncp+N));

why second term is 10*log10(N/N)??
N/N =1 so this term is 0

The actual relation is:
EsN0dB = EbN0dB + 10*log10(Nst/N) + 10*log10(N/(Ncp+N));

where Nst is the total number of used sub-carriers. i.e. if one is also using Null Sub-carriers (Zero Padding) then Nst is:
Nst = Total number of Sub-Carriers - Null Sub-carriers.

i haven't used any Null Subcarriers, so this is why this term is equal to zero.


And what do you use between Tdata or "r" as the transmitted data? I am confused.

I use "Tdata" for transmitted data. "r" is a mistake.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top