You are on page 1of 9

Change carrier frequency at the receiver side

MATLAB CODE
clc;
clear all;
close all;
%Nb is the number of bits to be transmitted
T=1;%Bit rate is assumed to be 1 bit/s;
%bits to be transmitted
b=[1 0 1 0 1]
%Rb is the bit rate in bits/second

NRZ_out=[];
RZ_out=[];
Manchester_out=[];

%Vp is the peak voltage +v of the NRZ waveform


Vp=1;
%Here we encode input bitstream as Bipolar NRZ-L waveform
for index=1:size(b,2)
if b(index)==1
NRZ_out=[NRZ_out ones(1,200)*Vp];
elseif b(index)==0
NRZ_out=[NRZ_out ones(1,200)*(-Vp)];
end
end

%Generated bit stream impulses


figure(1);
stem(b);
xlabel('Time (seconds)-->')
ylabel('Amplitude (volts)-->')
title('Impulses of bits to be transmitted');
figure(2);
plot(NRZ_out);
xlabel('Time (seconds)-->');
ylabel('Amplitude (volts)-->');
title('Generated NRZ signal');

t=0.005:0.005:5;
%Frequency of the carrier
f=5;
%Here we generate the modulated signal by multiplying it with
%carrier (basis function)
Modulated=NRZ_out.*(sqrt(2/T)*cos(2*pi*f*t));
figure(3);
plot(Modulated);
xlabel('Time (seconds)-->');
ylabel('Amplitude (volts)-->');
title('BPSK Modulated signal');

y=[];
num_bit=10101; %Signal length
max_run=20; %Maximum number of iterations for a single
SNR
Eb=1; %Bit energy
SNRdB=0:1:9; %Signal to Noise Ratio (in dB)
SNR=10.^(SNRdB/10);

hand=waitbar(0,'Please Wait....');
for count=1:length(SNR) %Beginning of loop for different SNR
avgError=0;
No=Eb/SNR(count); %Calculate noise power from SNR

for run_time=1:max_run %Beginning of loop for different runs


waitbar((((count-1)*max_run)+run_time-1)/(length(SNRdB)*max_run));
Error=0;

data=randint(1,num_bit); %Generate binary data source


s=2*data-1; %Baseband BPSK modulation

N=sqrt(No/2)*randn(1,num_bit); %Generate AWGN

Y=s+N; %Received Signal


for k=1:num_bit %Decision device taking hard decision and
deciding error
if ((Y(k)>0 && data(k)==0)||(Y(k)<0 && data(k)==1))
Error=Error+1;
end
end

Error=Error/num_bit; %Calculate error/bit


avgError=avgError+Error; %Calculate error/bit for different runs
end %Termination of loop for different runs
BER_sim(count)=avgError/max_run; %Calculate BER for a particular SNR
end %Termination of loop for different SNR
BER_th=(1/2)*erfc(sqrt(SNR)); %Calculate analytical BER
close(hand);
figure(4)
semilogy(SNRdB,BER_th,'k'); %Plot BER
hold on
semilogy(SNRdB,BER_sim,'k*');
legend('Theoretical','Simulation',3);
axis([min(SNRdB) max(SNRdB) 10^(-5) 1]);
hold off
%We begin demodulation by multiplying the received signal again with
%the carrier fequency
f=4;
demodulated=Modulated.*(sqrt(2/T)*cos(2*pi*f*t));
%Here we perform the integration over time period T using trapz
%Integrator is an important part of correlator receiver used here
for i=1:200:size(demodulated,2)
y=[y trapz(t(i:i+199),demodulated(i:i+199))];
end
received=y>0;
figure(5);
stem(received)
title('Impulses of Received bits');
xlabel('Time (seconds)-->');
ylabel('Amplitude (volts)');

You might also like