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.

help in correcting error in following matlab code

Status
Not open for further replies.

Shruti01

Member level 3
Joined
Apr 14, 2010
Messages
67
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
Mumbai, India
Activity points
1,941
hello all,
I have written a code for qpsk modulation in matlab. My code is as follows:

clc;
close all;
d = [0 1 1 1 0 1 0 0 1 0] % Data sequence
b = 2*d-1 % Convert unipolar to bipolar
Tb = 1 % Bit duration
fc = 3/Tb % Carrier frequency
Es = Tb/2 % This will result into unit amplitude waveforms
t = linspace(0, 10, 1000); % Discrete time samples between 0 and 10*Tb(1000 samples)
N = length(t) % Number of samples
Nsb = N/length(d) % Number of samples per bit
dd = repmat(d', 1, Nsb); % Replicate each bit Nsb times
bb = repmat(b', 1, Nsb);
dw = dd'; % Represent dw into a column vector(column by column)
dw = dw:))'; % Represent dw into a row vector(row by row)
bw = bb'; % Represent bw into a column vector(column by column)
bw = bw:))'; % Data sequence samples

o = b(1:2:end) %separating odd bits
e = b(2:2:end) %separating even bits
oo = repmat(o', 1, Nsb)
ee = repmat(e', 1, Nsb)
ow = oo'
ow = ow:))'
ew = ee'
ew = ew:))'

inPhaseOsc = 1/sqrt(2*Es/Tb)*cos(2*pi*fc*t)
quadPhaseOsc = 1/sqrt(2*Es/Tb)*sin(2*pi*fc*t)

qpskModulated = ow.*quadPhaseOsc + ew.*inPhaseOsc;

% Plotting the waveforms

subplot(4,1,1);
plot(t,dw);axis([0 10 -1.5 1.5])

subplot(4,1,2);
plot(t,bw);axis([0 10 -1.5 1.5])

subplot(4,1,3);
plot(t,inPhaseOsc);axis([0 10 -1.5 1.5])

subplot(4,1,4);
plot(t,quadPhaseOsc);axis([0 10 -1.5 1.5])

subplot(4,1,5);
plot(t,qpskModulated);axis([0 10 -1.5 1.5])


When I run the program, it gives me following error.
Error using ==> times
Matrix dimensions must agree.

Error in ==> Untitled5 at 30
qpskModulated = ow.*quadPhaseOsc + ew.*inPhaseOsc;

Kindly help.
 

hello all,
I have written a code for qpsk modulation in matlab. My code is as follows:

clc;
close all;
d = [0 1 1 1 0 1 0 0 1 0] % Data sequence
b = 2*d-1 % Convert unipolar to bipolar
Tb = 1 % Bit duration
fc = 3/Tb % Carrier frequency
Es = Tb/2 % This will result into unit amplitude waveforms
t = linspace(0, 10, 1000); % Discrete time samples between 0 and 10*Tb(1000 samples)
N = length(t) % Number of samples
Nsb = N/length(d) % Number of samples per bit
dd = repmat(d', 1, Nsb); % Replicate each bit Nsb times
bb = repmat(b', 1, Nsb);
dw = dd'; % Represent dw into a column vector(column by column)
dw = dw:))'; % Represent dw into a row vector(row by row)
bw = bb'; % Represent bw into a column vector(column by column)
bw = bw:))'; % Data sequence samples

o = b(1:2:end) %separating odd bits
e = b(2:2:end) %separating even bits
oo = repmat(o', 1, Nsb)
ee = repmat(e', 1, Nsb)
ow = oo'
ow = ow:))'
ew = ee'
ew = ew:))'

inPhaseOsc = 1/sqrt(2*Es/Tb)*cos(2*pi*fc*t)
quadPhaseOsc = 1/sqrt(2*Es/Tb)*sin(2*pi*fc*t)

qpskModulated = ow.*quadPhaseOsc + ew.*inPhaseOsc;

% Plotting the waveforms

subplot(4,1,1);
plot(t,dw);axis([0 10 -1.5 1.5])

subplot(4,1,2);
plot(t,bw);axis([0 10 -1.5 1.5])

subplot(4,1,3);
plot(t,inPhaseOsc);axis([0 10 -1.5 1.5])

subplot(4,1,4);
plot(t,quadPhaseOsc);axis([0 10 -1.5 1.5])

subplot(4,1,5);
plot(t,qpskModulated);axis([0 10 -1.5 1.5])


When I run the program, it gives me following error.
Error using ==> times
Matrix dimensions must agree.

Error in ==> Untitled5 at 30
qpskModulated = ow.*quadPhaseOsc + ew.*inPhaseOsc;

Kindly help.

Matlab reports error because there is a mismatch in matrix size

qpskModulated = ow.*quadPhaseOsc + ew.*inPhaseOsc;

Here
Code:
>> size(ow)

ans =

     1   500

>> size(ew)

ans =

     1   500

>> size(quadPhaseOsc)

ans =

           1        1000

>> size(inPhaseOsc)

ans =

           1        1000

for a.*b size(a) must be equal to size(b)

Here size one matrix is 1,500 where other 1,000
so that means there is a mismatch


that means if you try some thing like this

Code:
>> a=[ 1 2 4 5];
>> b=[3 4 6];
>> a.*b;
??? Error using ==> times
Matrix dimensions must agree.

it's an error :!:

so matrix dimensions must agree like
this

Code:
>> a=[ 1 2 4 5];
>> b=[3 4 5 6];
>> a.*b

ans =

     3     8    20    30

and it Works :idea:

---------- Post added at 14:50 ---------- Previous post was at 14:47 ----------

Inorder to get a correct result

qpskModulated = ow.*quadPhaseOsc + ew.*inPhaseOsc;

the size of 0w, quadPhaseOsc have to be same
similarly size of ew. and nPhaseOsc
 

i am separating odd bits and even bits from a binary sequence and then I am adding the respective bits to carrier oscillator. Could you plz correct my matlab code. It would be very helpful to me.

Thanx.

---------- Post added at 11:09 ---------- Previous post was at 10:32 ----------

Hello I made a change in my program.

Just have a look at it and tell me whether it is correct...
The program is as follows:

clc;
close all;
d = [0 1 1 1 0 1 0 0 1 0] % Data sequence
b = 2*d-1 % Convert unipolar to bipolar
Tb = 1 % Bit duration
fc = 3/Tb % Carrier frequency
Es = Tb/2 % This will result into unit amplitude waveforms
t = linspace(0, 10, 1000); % Discrete time samples between 0 and 10*Tb(1000 samples)
N = length(t) % Number of samples
Nsb = N/length(d) % Number of samples per bit
dd = repmat(d', 1, Nsb); % Replicate each bit Nsb times
bb = repmat(b', 1, Nsb);
dw = dd'; % Represent dw into a column vector(column by column)
dw = dw:))'; % Represent dw into a row vector(row by row)
bw = bb'; % Represent bw into a column vector(column by column)
bw = bw:))'; % Data sequence samples

o = b(1:2:end) %separating odd bits
e = b(2:2:end) %separating even bits
oo = repmat(o', 1, Nsb)
ee = repmat(e', 1, Nsb)
ow = oo'
ow = ow:))'
ew = ee'
ew = ew:))'

tc = linspace(0, 10, 500);

inPhaseOsc = 1/sqrt(2*Es/Tb)*cos(2*pi*fc*tc)
quadPhaseOsc = 1/sqrt(2*Es/Tb)*sin(2*pi*fc*tc)

qpskModulated = ow.*inPhaseOsc + ew.*quadPhaseOsc

% Plotting the waveforms

subplot(4,1,1);
plot(t,dw);axis([0 10 -1.5 1.5])

subplot(4,1,2);
plot(t,bw);axis([0 10 -1.5 1.5])

subplot(4,1,3);
plot(tc,qpskModulated);axis([0 10 -1.5 1.5])

Now I am getting plots..
 

your matrix order is not same mxn and nxm conditions must be satisfied in order for correct multiplication have a check into it.
and then try
if you encounter any other problem then let me know..

R
 

i am separating odd bits and even bits from a binary sequence and then I am adding the respective bits to carrier oscillator. Could you plz correct my matlab code. It would be very helpful to me.

Thanx.

---------- Post added at 11:09 ---------- Previous post was at 10:32 ----------

Hello I made a change in my program.

Just have a look at it and tell me whether it is correct...
The program is as follows:

clc;
close all;
d = [0 1 1 1 0 1 0 0 1 0] % Data sequence
b = 2*d-1 % Convert unipolar to bipolar
Tb = 1 % Bit duration
fc = 3/Tb % Carrier frequency
Es = Tb/2 % This will result into unit amplitude waveforms
t = linspace(0, 10, 1000); % Discrete time samples between 0 and 10*Tb(1000 samples)
N = length(t) % Number of samples
Nsb = N/length(d) % Number of samples per bit
dd = repmat(d', 1, Nsb); % Replicate each bit Nsb times
bb = repmat(b', 1, Nsb);
dw = dd'; % Represent dw into a column vector(column by column)
dw = dw:))'; % Represent dw into a row vector(row by row)
bw = bb'; % Represent bw into a column vector(column by column)
bw = bw:))'; % Data sequence samples

o = b(1:2:end) %separating odd bits
e = b(2:2:end) %separating even bits
oo = repmat(o', 1, Nsb)
ee = repmat(e', 1, Nsb)
ow = oo'
ow = ow:))'
ew = ee'
ew = ew:))'

tc = linspace(0, 10, 500);

inPhaseOsc = 1/sqrt(2*Es/Tb)*cos(2*pi*fc*tc)
quadPhaseOsc = 1/sqrt(2*Es/Tb)*sin(2*pi*fc*tc)

qpskModulated = ow.*inPhaseOsc + ew.*quadPhaseOsc

% Plotting the waveforms

subplot(4,1,1);
plot(t,dw);axis([0 10 -1.5 1.5])

subplot(4,1,2);
plot(t,bw);axis([0 10 -1.5 1.5])

subplot(4,1,3);
plot(tc,qpskModulated);axis([0 10 -1.5 1.5])

Now I am getting plots..

Hi
Shruthi ,
I have examined your plot and usually it's a good practice to give
name to your plot and label the axes so that it's clear .Also it would be ideal to plot the odd ans even
Sequences along with the resultant QPSK Signal .

To verify the QPSK visually ,plot the Components in two graphs and then the QPSK
 

How to generate delay in the following bit sequence using matlab.

My bit sequence is d(n) = [1 0 1 1 0 1 0 1 1 0]
and my delay has to be d(n-1)...

What is the matlab code for generating delay d(n-1).


Thanx.
 

How to generate delay in the following bit sequence using matlab.

My bit sequence is d(n) = [1 0 1 1 0 1 0 1 1 0]
and my delay has to be d(n-1)...




What is the matlab code for generating delay d(n-1).


Thanx.

sample code

Code:
x=[ 1 2 3 4 5 6 7 8];%Sample Data 
 subplot(2,1,1)
 stem(x)
 grid on;
 xlabel('n');
 ylabel('X(n)');
 Title('Sequence X(n)')

 subplot(2,1,2)
 stem([0,x(1:end-1)])%Delayed Data
 grid on;
 xlabel('n');
 ylabel('X(n)');
 title('Delayed Sequence X(n)');




delayed_sequence.jpg

regards
Blooz
 

I have to a generate a random binary sequence and then after generating the binary sequence I have to apply threshold of 0.5. Suppose for example - the random value generated is 0.112 ten since the value is greater than threshold then it will binary 1 and if the random values generated are less than 0.5 then it will binary 0.
I have written a matlab code for it as follows:
clc;
close all;
A = randn(1,1000) % Generate a random binary sequence
threshold = 0.5;
B = (A > threshold);
if any(B)
j = 1
else
j = 0
end
M not getting output.
Kindly help....

---------- Post added at 07:08 ---------- Previous post was at 06:51 ----------

I have modified my above matlab code as follows but still I am not getting the output.

clc;
close all;
A = randn(1,1000) % Generate a random binary sequence
threshold = 0.5;
for i=1:1000
if i>=threshold
A(i)=1;
else
A(i)=0;
end
end
 

I have to a generate a random binary sequence and then after generating the binary sequence I have to apply threshold of 0.5. Suppose for example - the random value generated is 0.112 ten since the value is greater than threshold then it will binary 1 and if the random values generated are less than 0.5 then it will binary 0.
I have written a matlab code for it as follows:
clc;
close all;
A = randn(1,1000) % Generate a random binary sequence
threshold = 0.5;
B = (A > threshold);
if any(B)
j = 1
else
j = 0
end
M not getting output.
Kindly help....

---------- Post added at 07:08 ---------- Previous post was at 06:51 ----------

I have modified my above matlab code as follows but still I am not getting the output.

clc;
close all;
A = randn(1,1000) % Generate a random binary sequence
threshold = 0.5;
for i=1:1000
if i>=threshold
A(i)=1;
else
A(i)=0;
end
end

if i>=threshold Error!



Correct code
Code:
clc;
close all;
A = randn(1,1000) ;% Generate a random binary sequence
threshold = 0.5;
for i=1:1000
if [B][COLOR="#0000FF"]A(i)>=threshold[/COLOR][/B]
A(i)=1;
else
A(i)=0;
end
end

output First 20 Values
Code:
>> A(1:20)

ans =

  Columns 1 through 13 

     0     0     1     1     1     1     0     1     0     0     0     0     1

  Columns 14 through 20 

     0     0     0     1     0     0     0
 

I think you need to modify it in this form ....

clc;
close all;
A = randn(1,1000) % Generate a random binary sequence
threshold = 0.5;
for i=1:1000
if (A(i)>=threshold)
A(i)=1;
else
A(i)=0;
end
end

Then the code make some sense.....in your code you will get always A =1,

But the code which I had modified will have binary values of A based on the random number values.....

Good Luck
 

In the demodulator the received signal is multiplied by a reference frequency generator (assuming the PLL/Costas loop be present). The multiplied output is integrated over one bit period using an integrator. A threshold detector makes a decision on each integrated bit based on a threshold. Since an NRZ signaling format is used with equal amplitudes in positive and negative direction, the threshold for this case would be '0'. I have attached my code till the multiplied output.... Next part is integration part. How to do that...




clc;
close all;
A = randn(1,1000) % Generate a random sequence
threshold = 0.5;
Tb = 1 % Bit duration
fc = 3/Tb; % Carrier frequency
Es = Tb/2; % This will result into unit amplitude waveforms
t = linspace(0, 10, 1000); % Discrete time samples between 0 and 10*Tb(1000 samples)
Nsb = 10; % Number of samples per bit
t1 = 0.1;
noiseVariance = 0.5;
Fs = 400; % Sampling frequency
for i=1:1000
if (A(i)>=threshold)
A(i)=1;
else
A(i)=0;
end
end
A(1:1000);
d = A(1:1000) % Binary sequence
subplot(4,1,1)
plot(t,d);axis([0 10 -1.5 1.5])
b = 2*d-1 % Bipolar sequence
subplot(4,1,2)
plot(t,b);axis([0 10 -1.5 1.5])

w = sqrt(2*Es/Tb)*cos(2*pi*fc*t) % Carrier waveform
for i=1:10
bpsk_w = b.*w
end
subplot(4,1,3)
plot(t,bpsk_w);axis([0 10 -1.5 1.5])

noise = sqrt(noiseVariance)*randn(1,length(bpsk_w))
received = bpsk_w + noise
subplot(4,1,4);
plot(t,received);axis([0 10 -1.5 1.5])

v = received.*w;

next part is integration...

Thanx for your help....

---------- Post added at 08:21 ---------- Previous post was at 08:18 ----------

Sorry there is no loop in the program.. The correct program is as follows:

clc;
close all;
A = randn(1,1000) % Generate a random sequence
threshold = 0.5;
Tb = 1 % Bit duration
fc = 3/Tb; % Carrier frequency
Es = Tb/2; % This will result into unit amplitude waveforms
t = linspace(0, 10, 1000); % Discrete time samples between 0 and 10*Tb(1000 samples)
Nsb = 10; % Number of samples per bit
t1 = 0.1;
noiseVariance = 0.5;
Fs = 400; % Sampling frequency
for i=1:1000
if (A(i)>=threshold)
A(i)=1;
else
A(i)=0;
end
end
A(1:1000);
d = A(1:1000) % Binary sequence
subplot(4,1,1)
plot(t,d);axis([0 10 -1.5 1.5])
b = 2*d-1 % Bipolar sequence
subplot(4,1,2)
plot(t,b);axis([0 10 -1.5 1.5])

w = sqrt(2*Es/Tb)*cos(2*pi*fc*t) % Carrier waveform
bpsk_w = b.*w

subplot(4,1,3)
plot(t,bpsk_w);axis([0 10 -1.5 1.5])

noise = sqrt(noiseVariance)*randn(1,length(bpsk_w))
received = bpsk_w + noise
subplot(4,1,4);
plot(t,received);axis([0 10 -1.5 1.5])

v = received.*w
 

can anybody plz explain me the demodulation part of BPSK of the following matlab code.....

clc;
clear all;
close all;
N = 10^6 % number of bits or symbols
rand('state',100); % initializing the rand() function
randn('state',200); % initializing the randn() function

% Transmitter
ip = rand(1,N)>0.5; % generating 0,1 with equal probability
s = 2*ip-1; % BPSK modulation 0 -> -1; 1 -> 1
n = 1/sqrt(2)*[randn(1,N) + j*randn(1,N)]; % white gaussian noise, 0dB variance
Eb_N0_dB = [-3:10]; % multiple Eb/N0 values

for ii = 1:length(Eb_N0_dB)
% Noise addition
y = s + 10^(-Eb_N0_dB(ii)/20)*n; % additive white gaussian noise

% receiver - hard decision decoding
ipHat = real(y)>0;
end

Thanx a lot....
 

hello... I have a binary data sequence given as d =[1 0 1 1 0]. In each bit there are 10 samples. These samples are to multiplied with cos(2*pi*3*t1*i) where i varies from 1 to 2 and t1 = 0.1. The matlab code i have written for this is as follows:
clc;
close all;
clear all;
d = [1 0 1 1 0]; % Generate binary sequence
for a = d(1)
for i =1:2
j = cos(2*pi*3*0.1*i)
end
end
for a = d(2)
for i =1:2
j = cos(2*pi*3*0.1*i)
end
end
for a = d(3)
for i =1:2
j = cos(2*pi*3*0.1*i)
end
end
for a = d(4)
for i =1:2
j = cos(2*pi*3*0.1*i)
end
end
for a = d(5)
for i =1:2
j = cos(2*pi*3*0.1*i)
end
end
I have to repeat this for all the bits present in the bit sequence...
Kindly help...
 

As per I undestand you need this mostly

clc;
close all;
clear all;
d = [1 0 1 1 0]; % Generate binary sequence

for a = d(1)
for i =1:10
j = cos(2*pi*3*0.1*(i-1))
end
end
for a = d(2)
for i =1:10
j = cos(2*pi*3*0.1*(i-1))

end
end
for a = d(3)
for i =1:10
j = cos(2*pi*3*0.1*(i-1))
end
end
for a = d(4)
for i =1:10
j = cos(2*pi*3*0.1*(i-1))
end
end
for a = d(5)
for i =1:10
j = cos(2*pi*3*0.1*(i-1))
end
end


Good Luck
 

what I need is this...
I have written the matlab code for it as follows.
clc;
close all;
clear all;
d = [1 0 1 1 0]; % Generate binary sequence

for d=1:5
for d=d(1)
for i =1:10
j1 = cos(2*pi*3*0.1*(i-1))
end
end
for d=d(2)
for i =1:10
j2 = cos(2*pi*3*0.1*(i-1))
end
end
for d=d(3)
for i =1:10
j3 = cos(2*pi*3*0.1*(i-1))
end
end
for d=d(4)
for i =1:10
j4 = cos(2*pi*3*0.1*(i-1))
end
end
for d=d(5)
for i =1:10
j5 = cos(2*pi*3*0.1*(i-1))
end
end
end
Each bit in the binary sequence is divided into 10 samples and each sample for that bit is multiplied with carrier waveform. This has to be repeated for every bit in the binary sequence. Hope u understood what I am trying to say. Thanx.
 

need help in following code... i am getting a error in following code of bpsk demodulation

clc;
close all;
clear all;
A = randn(1,1000); % Generate a random sequence
threshold = 0.5;
noiseVariance=0.5
Tb = 1; % Bit duration
fc = 3/Tb; % Carrier frequency
Es = Tb/2; % This will result into unit amplitude waveforms
t = linspace(0, 10, 1000); % Discrete time samples between 0 and 10*Tb(1000 samples)
for i=1:1000
if (A(i)>=threshold)
A(i)=1;
else
A(i)=0;
end
end
A(1:1000);
d = A(1:1000)% Binary sequence
subplot(4,1,1);
plot(t,d);axis([0 10 -1.5 1.5])
b = 2*d-1; % Bipolar sequence
subplot(4,1,2);
plot(t,b);axis([0 10 -1.5 1.5])
t1=0:1/100:9.99
t2=t1/fc
w=cos(2*pi*fc*t2)
bpskmod=b.*w
subplot(4,1,3);
plot(t,bpskmod);axis([0 10 -1.5 1.5])
noise = sqrt(noiseVariance)*randn(1,length(bpskmod))
received = bpskmod + noise
v = received.*w
x=0:1/100:9.99
y=v
z=trapz(x,y)
if z<0
z=-1
else
z=1
end
bpskdmod=reshape([z],1,[])
t=linspace(0, 10, 1000);
subplot(4,1,4);
plot(t,bpskdmod);axis([0 10 -1.5 1.5])
I am not getting the input data sequence at the receiver. Kindly help..
 

I have demodulated the data at the receiver side but I am not getting the correct data sequence... Kindly help me out. My matlab code for MSK is as follows:

clc;
close all;
A = randn(1,1000); % Generate a random sequence
threshold = 0.5;
Tb = 1; % Bit duration
fc = 3/Tb; % Carrier frequency
Es = Tb/2; % This will result into unit amplitude waveforms
t = linspace(0, 10, 1000); % Discrete time samples between 0 and 10*Tb(1000 samples)
noiseVariance=0.5
for i=1:1000
if (A(i)>=threshold)
A(i)=1;
else
A(i)=0;
end
end
A(1:1000);
d = A(1:1000)% Binary sequence
subplot(4,1,1);
plot(t,d);axis([0 10 -1.5 1.5])
b = 2*d-1; % Bipolar sequence
o = b(1:2:end) %separating odd bits
e = b(2:2:end) %separating even bits
t = linspace(0, 10, 500);
t1=0:1/100:4.99;
t2=t1/fc;
inPhaseOsc = cos((pi*t2)/2*Tb).*cos(2*pi*fc*t2)
quadPhaseOsc = sin((pi*t2)/2*Tb).*sin(2*pi*fc*t2)
mskmod = o.*quadPhaseOsc + e.*inPhaseOsc
subplot(4,1,2);
plot(t,mskmod);axis([0 10 -1.5 1.5])
noise =sqrt(noiseVariance)*randn(1,length(mskmod))
received = mskmod + noise
subplot(4,1,3);
plot(t,received);axis([0 10 -1.5 1.5])
isignal = received.*inPhaseOsc
qsignal = received.*quadPhaseOsc
x1=0:1/100:4.99
y1=isignal
z1=trapz(x1,y1)
if z1<0
z1=-1
else
z1=1
end
x2=0:1/100:4.99
y2=qsignal
z2=trapz(x2,y2)
if z2<0
z2=-1
else
z2=1
end
finaloutput1=(o-z2)/2
finaloutput2=(e-z1)/2
finaloutput=reshape([finaloutput1;finaloutput2],1,[])
t = linspace(0, 10, 1000);
subplot(4,1,4);
plot(t,finalOutput);axis([0 10 -1.5 1.5])
 

I have written a simple matlab code for bpsk modulation and demodulation... Kindly tell me whether it is correct or not. The code is as follows:

clc;
close all;
A = randn(1,1000)% Generate a random sequence
threshold = 0.5;
Tb = 1; % Bit duration
noiseVariance = 0.5
fc = 3/Tb; % Carrier frequency
t = linspace(0, 10, 1000); % Discrete time samples between 0 and 10*Tb(1000 samples)
for i=1:1000
if (A(i)>=threshold)
A(i)=1;
else
A(i)=0;
end
end
A(1:1000)
d = A(1:1000)% Binary sequence
subplot(4,1,1);
plot(t,d);axis([0 10 -1.5 1.5])
b = 2*d-1; % Bipolar sequence
subplot(4,1,2);
plot(t,b);axis([0 10 -1.5 1.5])
t1=0:1/100:9.99
t2=t1/fc
w=cos(2*pi*fc*t2)
bpskmod = b.*w % Modulated waveform
subplot(4,1,3);
plot(t,bpskmod);axis([0 10 -1.5 1.5])
noise = sqrt(noiseVariance)*randn(1,length(bpskmod))
received = bpskmod + noise
v = received.*w
x=0:1/100:9.99
y=v
z=trapz(x,y)
if z<0
z=-1
else
z=1
end
finaloutput=(b-z)/2;
subplot(4,1,4);
plot(t,finaloutput);axis([0 10 -1.5 1.5])
 

Keep Number of bits, Number of samples, Bit duration and fc ( carrier frequency) as variables.

Add a bit error calculation part where you compare transmitted bits and received bits and find the number of errorrs. Then calcualte bit error rate (BER) = No. of errors/ Total number of bits

Tabulate BER for different noise variance. A curve with BER on the Y-axis and SNR on the X-axis is called the BER curve.

SNR = 10 log( Signal Variance/ Noise Variance)

Take Signal variance as 1. The matlab code for bpsk is as follows. I have made above changes in my code but still I am not getting required output. Kindly help.

clc;
close all;
no_of_bits=10
no_of_samples=20
A1 = rand(1,no_of_bits); % Generate a random sequence
A2=max(A1);
A3=min(A1);
A=A1/A2;
threshold = 0.5;
Tb = 1; % Bit duration
noiseVariance = [0.1:0.1:0.5]
fc = 3/Tb; % Carrier frequency
t = linspace(0, 1, no_of_samples);
for i=1:no_of_bits
if (A(i)>=threshold)
A(i)=1;
else
A(i)=0;
end
end
d = A(1:no_of_bits)% Binary sequence
b = 2*d-1 % Bipolar sequence
b(1)
t1=linspace(0, 1, no_of_samples);
t2=t1/fc;
w=cos(2*pi*fc*t2)
for i=1:no_of_bits
for j=1:no_of_samples
b1(j)=b(i);
end
bpskmod=b1.*w; % Modulated waveform
%transmitter end
%Noise addition
for k=1:length(noiseVariance)
noise = sqrt(noiseVariance(k))*randn(1,no_of_samples)
received = bpskmod + noise
% Multipying the received signal with carrier waveform
v = received.*w
x=linspace(0, 1, no_of_samples)
y=v
z=trapz(x,y)
if z<0
z=0
else
z=1
end
bpsk(i)=z;
bpskout(i)=bpsk(i)
(d-bpskout(i))
a=find(bpskout(i)-d)
te=size(a,2)
ber=te/length(d)
signalVariance=1;
snr=10*log(signalVariance/noiseVariance)
end
end
 

I have written the following code for ber vs snr in bpsk but for low snr i m getting ber value as 0 which is not correct bcoz as snr increases, ber decreses. Kindly help. My code is as follows:

clc;
close all;
no_of_bits=10000
no_of_samples=20
A1 = rand(1,no_of_bits); % Generate a random sequence
A2=max(A1);
A3=min(A1);
A=A1/A2;
threshold = 0.5;
noiseVariance=[2:0.1:2.1];
Tb = 1; % Bit duration
signalVariance=1;
fc = 3/Tb; % Carrier frequency
t = linspace(0, 1, no_of_samples);
for i=1:no_of_bits
if (A(i)>=threshold)
A(i)=1;
else
A(i)=0;
end
end
d = A(1:no_of_bits);% Binary sequence
b = 2*d-1; % Bipolar sequence
b(1);
t1=linspace(0, 1, no_of_samples);
t2=t1/fc;
w=cos(2*pi*fc*t2);

for k=1:1
err(k)=0;
for i=1:no_of_bits
for j=1:no_of_samples
b1(j)=b(i);
end
bpskmod=b1.*w; % Modulated waveform
%Noise addition
noise = sqrt(noiseVariance(k)*randn(1,no_of_samples));
received = bpskmod + noise;
% Multipying the received signal with carrier waveform
v = received.*w;
x=linspace(0, 1, no_of_samples);
y=v;
z=trapz(x,y);
if z<0
z=0;
else
z=1;
end
bpsk(i)=z;
bpskout(i)=bpsk(i);
if (d(i)~=bpskout(i))
err(k)=err(k)+1
end
end
err(k)
ber(k)=err(k)/no_of_bits
snr(k)=10*log(signalVariance/noiseVariance(k))
end
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top