How to plot two graph using different colour?

2 views (last 30 days)
EbNo = 4:2:20;
ber = zeros(length(EbNo),20);
for L = 1:2
ber(:,L) = berfading(EbNo,'qam',8,L);
end
figure(18);
semilogy(EbNo,ber,'b')
text(18.5, 0.02, sprintf('L=%d',1))
text(18.5, 1e-11, sprintf('L=%d',2))
title('ofdm and optical ofdm ber curve')
xlabel('E_b/N_0 (dB)')
ylabel('BER')
legend({'ofdm','optical ofdm'},'Location','southwest')
grid on
But as per the graph both graphs are having same colour,I want that both graphs must be plotted with different colour.How can be done?

Answers (1)

Walter Roberson
Walter Roberson on 9 May 2021
semilogy(EbNo,ber,'b')
The 'b' specifically says to use blue for all 20 of the lines. (Only 2 of the lines are obvious because ber = zeros(length(EbNo),20); initialized the 20 lines to all zeros so the other 18 are all in the same place along the axes.)
If you want different colors for each line, then do not use 'b' in the semilogy() call.
  4 Comments
Dipsikha Roy
Dipsikha Roy on 14 May 2021
clc;
M =8; %here we initialize the number of constellation point of qam
k = log2(M); % Bits per symbol
EbNoVec = (5:15)'; % Eb/No values (dB)
numSymPerFrame = 100; % Number of QAM symbols per frame
no_of_data_bits=1024;
Fm=10^6;%Max freq
block_size = 16; %Size of each OFDM block to add cyclic prefix
cp_len = floor(0.1 * block_size); %Length of the cyclic prefix
EbNoVec = (5:15)'; % Eb/No values (dB)
numSymPerFrame = 100; % Number of QAM symbols per frame
berEst = zeros(size(EbNoVec));
data_source= abs(round(randn(1,no_of_data_bits)));%here we take random normal function
figure(1);
x=1:no_of_data_bits;
stem (x*(1/Fm),data_source);
grid minor;
xlabel('time(Microsecond)','Fontsize',16);
ylabel('amplitude','Fontsize',16);
title('Transmitted Data','Fontsize',16);%here we plot that transmitted data
qam_modulated_data = qammod(data_source, M);%here we perform 8bit qam on the random normal function
nqdata = length(qam_modulated_data);
data = 0:M-1;
scatterplot(qam_modulated_data,1,0,'r*');
[udata, uidx] = unique(qam_modulated_data);
nudata = length(udata);
grid minor
for k=1:nudata
text(real(udata(k))-0.4,imag(udata(k))+0.4,num2str(data_source(uidx(k))));
end
axis([-4 4 -2 2])
qm = abs(qam_modulated_data);
figure(3);
stem(qm)
title('MODULATED TRANSMITTED DATA','Fontsize',16);%here we plot those constellation point
y=ifft(qam_modulated_data);
figure(4);
x=1:nqdata;
stem(x(2:end)*Fm,abs(y(2:end)));
grid minor;
xlabel('freq(Mhz)','Fontsize',16);
ylabel('amplitude of ifft','Fontsize',16);
title('without hermitian ifft','Fontsize',16);
udata1(1:(M/2))=qam_modulated_data(1:(M/2)); %here we introduce another array named qam_modulated_data1 where first four element is first four constellation point
udata1(((M/2)+1):M)=qam_modulated_data(1:(M/2));% in my qam_modulated_data1 array last four point is Hermitian of first four point
figure(5);
x=1:M;
stem (x*Fm,abs(udata1));
grid minor;
xlabel('frequency(MHZ)','Fontsize',16);
ylabel('amplitude','Fontsize',16);
title('Hermitian symmetry','Fontsize',16);
y1=ifft(udata1); %here we apply fast Fourier transform on the data of udata1
figure(6);
x=1:M;
stem(x*Fm,abs(y1)); %here we plot that fft result
grid minor;
xlabel('Freq(Mhz)','Fontsize',16);
ylabel('amplitude','Fontsize',16);
title('even frequency suppressed output','Fontsize',16);
y1c=conj(y1);
real_y1=y1.*y1c ;
figure(7);
x=1:M;
stem(x*Fm,real_y1);
grid on;
xlabel('Freq(Mhz)','Fontsize',16);
ylabel('amplitude of real values of ifft','Fontsize',16);
title('real value of ifft','Fontsize',16);
number_of_subcarriers=M;
assert(number_of_subcarriers <= M);
cp_start=block_size-cp_len;
ifft_Subcarrier = zeros(16, number_of_subcarriers);
cyclic_prefix = zeros(cp_len, number_of_subcarriers);
Append_prefix = zeros(16+cp_len, number_of_subcarriers);
for i=1:number_of_subcarriers
S2P = reshape(qam_modulated_data, no_of_data_bits/M,M);
ifft_Subcarrier(:,i) = ifft((S2P(:,i)),16);% 16 is the ifft point
for j=1:cp_len
cyclic_prefix(j,i) = ifft_Subcarrier(j+cp_start,i);
end
Append_prefix(:,i) = vertcat( cyclic_prefix(:,i), ifft_Subcarrier(:,i));
% Appends prefix to each subcarriers
end
A1=Append_prefix(:,1);
A2=Append_prefix(:,2);
A3=Append_prefix(:,3);
A4=Append_prefix(:,4);
A5=Append_prefix(:,5);
A6=Append_prefix(:,6);
A7=Append_prefix(:,7);
A8=Append_prefix(:,8);
figure(12), subplot(8,1,1),plot(real(A1),'r'),title('Cyclic prefix added to all the odd sub-carriers','Fontsize',16)
subplot(8,1,2),plot(real(A2),'y')
subplot(8,1,3),plot(real(A3),'b')
subplot(8,1,4),plot(real(A4),'g')
subplot(8,1,5),plot(real(A5),'m')
subplot(8,1,6),plot(real(A6),'k')
subplot(8,1,7),plot(real(A7),'c')
subplot(8,1,8),plot(real(A8),'g')
xlabel('frequency(MHZ)','Fontsize',16);
ylabel('amplitude','Fontsize',16);
%Convert to serial from parallel
[rows_Append_prefix cols_Append_prefix]=size(Append_prefix)
len_ofdm_data = rows_Append_prefix*cols_Append_prefix;
% OFDM signal to be transmitted
ofdm_signal = reshape(Append_prefix, 1, len_ofdm_data);
figure(13),
plot(real(ofdm_signal)); xlabel('Time','Fontsize',16); ylabel('Amplitude','Fontsize',16);
title('OFDM Signal','Fontsize',16);grid on;
% Frequency selective channel with 4 taps
Ts = 1e-3; % Sampling period of channel
Fd = 0; % Max Doppler frequency shift
tau = [0 .2e-9 .5e-9 1.6e-9 2.3e-9 5e-9]; % Path delays
pdb = [0.189 0.379 0.239 0.095 0.061 0.037]; % Avg path power gains
h = comm.RayleighChannel('SampleRate', 1/Ts, ...
'MaximumDopplerShift', Fd, ...
'PathDelays', tau, ...
'AveragePathGains', pdb, ...
'PathGainsOutputPort', false);
reset(h); %ResetBeforeFiltering equivalent
channel = step(h, ofdm_signal(:,j).'); %second output is PathGains
after_channel = filter(channel, 1, ofdm_signal);
awgn_noise = awgn(zeros(1,length(after_channel)),0);
recvd_signal = awgn_noise+after_channel; % With AWGN noise
%Converts from serial back to parallel
figure(14),
plot(real(recvd_signal)),xlabel('Time','Fontsize',16); ylabel('Amplitude','Fontsize',16);
title('OFDM Signal after rayleigh fading passing through channel','Fontsize',16);grid on;
recvd_signal_paralleled = reshape(recvd_signal,rows_Append_prefix, cols_Append_prefix);
%now that the signal has passed through the channel we begin to work
%backawards, but we are first going to remove the CP
% Remove cyclic Prefix
%Now that the signal has already passed through the channel, we can get rid
%of the CP
recvd_signal_paralleled(1:cp_len,:)=[];
R1=recvd_signal_paralleled(:,1);
R2=recvd_signal_paralleled(:,2);
R3=recvd_signal_paralleled(:,3);
R4=recvd_signal_paralleled(:,4);
R5=recvd_signal_paralleled(:,5);
R6=recvd_signal_paralleled(:,6);
R7=recvd_signal_paralleled(:,7);
R8=recvd_signal_paralleled(:,8);
figure(15),plot((imag(R1)),'r'),subplot(8,1,1),plot(real(R1),'r'),
title('Cyclic prefix removed from the eight sub-carriers','Fontsize',16)
subplot(8,1,2),plot(real(R2),'y')
subplot(8,1,3),plot(real(R3),'g')
subplot(8,1,4),plot(real(R4),'b')
subplot(8,1,5),plot(real(R5),'k')
subplot(8,1,6),plot(real(R6),'c')
subplot(8,1,7),plot(real(R7),'m')
subplot(8,1,8),plot(real(R8),'b')
xlabel('frequency(MHZ)','Fontsize',16);
ylabel('amplitude','Fontsize',16);
% Fourier Transofrm of the received signal
%Similarly to how we took the IFFT of the original signal, as we are back
%tracking we are now taking the FFT
for i=1:number_of_subcarriers
fft_data(:,i) = fft(recvd_signal_paralleled(:,i),16);
end
F1=fft_data(:,1);
F2=fft_data(:,2);
F3=fft_data(:,3);
F4=fft_data(:,4);
F5=fft_data(:,5);
F6=fft_data(:,6);
F7=fft_data(:,7);
F8=fft_data(:,8);
figure(16), subplot(8,1,1),plot(real(F1),'r'),title('Fourier Transform of all the eight sub-carriers','Fontsize',16)
subplot(8,1,2),plot(real(F2),'y')
subplot(8,1,3),plot(real(F3),'g')
subplot(8,1,4),plot(real(F4),'b')
subplot(8,1,5),plot(real(F5),'K')
subplot(8,1,6),plot(real(F6),'c')
subplot(8,1,7),plot(real(F7),'m')
subplot(8,1,8),plot(real(F8),'g')
xlabel('Time(microsec)','Fontsize',16);
ylabel('amplitude','Fontsize',16);
% Conversion to serial and demodulation
%The orignal data was in series, so we are taking the demodulated data
%which is still in parallel form and we are converting back into series to
%hopefully show the same data
recvd_serial_data = reshape(fft_data, 1,(16*8));
qam_demodulated_data = qamdemod(recvd_serial_data,M);
figure(17),
subplot(2,1,1),
x=1:no_of_data_bits;
stem(x*(1/Fm),data_source);
grid on;xlabel('Data Points(time in microsec)','Fontsize',16);ylabel('Amplitude','Fontsize',16);
title('Original Data','Fontsize',16)
subplot (2,1,2),
num_sent = length(data_source);
lie_about_the_time = linspace(x(1)/Fm, x(end)/Fm, length(qam_demodulated_data));
stem(lie_about_the_time, qam_demodulated_data);
grid on;xlabel('Data Points(time in microsec)','Fontsize',16);ylabel('Amplitude','Fontsize',16);
title('Received Data','Fontsize',16);
dataOut = de2bi(qam_demodulated_data,k);
for n = 1:length(EbNoVec)
% Convert Eb/No to SNR
snrdB = EbNoVec(n) + 10*log10(k);
% Reset the error and bit counters
numErrs = 0;
numBits = 0;
while numErrs < 200 && numBits < 1e7
% Calculate the number of bit errors
nErrors = biterr(y,y1);
% Increment the error and bit counters
numErrs = numErrs + nErrors;
numBits = numBits + numSymPerFrame*k;
end
% Estimate the BER
berEst(n) = numErrs/numBits;
end
berTheory = berawgn(EbNoVec,'qam',8);
figure(18)
semilogy(EbNoVec,berEst,'*')
hold on
semilogy(EbNoVec,berTheory)
grid
legend('Estimated BER','Theoretical BER')
xlabel('Eb/No (dB)')
ylabel('Bit Error Rate')
got error message like Error in biterr (line 134)
validateattributes(a, {'numeric', 'logical'}, ...
Error in final_code (line 207)
nErrors = biterr(y,y1);
what to do now?please help
Walter Roberson
Walter Roberson on 14 May 2021
data_source= abs(round(randn(1,no_of_data_bits)));%here we take random normal function
So data_source will be a 1 x no_data_bits vector of non-negative integers, unlikely to be symmetric.
qam_modulated_data = qammod(data_source, M);%here we perform 8bit qam on the random normal function
You qammod that stream.
y=ifft(qam_modulated_data);
you ifft() the qam modulated data.
nErrors = biterr(y,y1);
and try to calculate the bit error rate using the ifft() values.
However, qam_modulated_data is quite unlikely to just happen to be a skew-symmetric complex-conjugate array, so the ifft of it is almost certain to be complex valued, and so not bit values.
qammod() takes a sequence of M consecutive bits and map them into a single complex value, with the value being deterministic according to the input bits. 01001101 on input will always produce the same complex number on output. Time-domain data remains time domain data, just encoded into a complex number. qammod() does not produce frequency domain data, so you should never expect that ifft() of qammod() will happen to produce real-valued time-domain data.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!