how to make both graphs x and y axis identical?

Here in graph x axis is 200,400,600 and y axis has been ranging from 0 to 3 while other graph x axis is like 20,40,60.. and y axis range is ranging from 0 to 8.How to make both axis equal?Please help me?

1 Comment

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;
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');ylabel('Amplitude');
title('Original Data')
subplot (2,1,2),
num_sent = length(qam_demodulated_data );
lie_about_the_time = linspace(1, length(qam_demodulated_data), num_sent);
stairs(lie_about_the_time, qam_demodulated_data)
I used thid code,but this is not something I want.

Sign in to comment.

 Accepted Answer

Currently you have something like
stairs(received_data)
Change that to
num_sent = length(original_data);
lie_about_the_time = linspace(1, length(recieved_data), num_sent);
stairs(lie_about_the_time, received_data)

12 Comments

I have code like this
recvd_serial_data = reshape(fft_data, 1,(16*8));
qam_demodulated_data = qamdemod(recvd_serial_data,M);
figure(17),
subplot(2,1,1),
stem(data_source);
grid on;xlabel('Data Points');ylabel('Amplitude');
title('Original Data')
subplot (2,1,2),
stem(qam_demodulated_data,'bo');
grid on;xlabel('Data Points');ylabel('Amplitude');
title('Recieved Data')
Now please suggest something.
num_sent = length(data_source);
lie_about_the_time = linspace(1, length(qam_demodulated_data), num_sent);
stairs(lie_about_the_time, qam_demodulated_data)
on running this code i m getting error like
Error using stairs
(line 53)
X must be same length
as Y.
Error in Untitled2
(line 188)
stairs(lie_about_the_time,qam_demodulated_data);
Please suggest me.
num_sent = length(data_source);
lie_about_the_time = linspace(1, num_sent, length(qam_demodulated_data));
stairs(lie_about_the_time, qam_demodulated_data)
I am getting this kind of graph where still both the axis are not equal and comparing this two graph is very difficult.
If I get this kind of graphs then I can compare both data,Is it possible to get,I have used the last graph for reference,I need this kind of graph should come in my code also.Can u please give me more suggestion.
num_sent = length(data_source);
lie_about_the_time = linspace(1, num_sent, length(qam_demodulated_data));
stem(lie_about_the_time, qam_demodulated_data)
The first graph you show cannot be produced by your posted code. The x axis shows an upper bound of 1.2e-3, but the stem() call you show does not provide any x coordinates so the plot would use integer coordinates and those would not be less than 1 for the upper bound.
clc;
M =8; %here we initialize the number of constellation point of qam
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
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',14);
ylabel('amplitude','Fontsize',14);
title('Transmitted Data','Fontsize',14);%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',14);%here we plot those constellation point
y=ifft(qam_modulated_data);
figure(4);
x=1:nqdata;
stem(x*Fm,abs(y));
grid minor;
xlabel('freq(Mhz)');
ylabel('amplitude of ifft');
title('without hermitian ifft','Fontsize',14);
data1 = udata;
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',14);
ylabel('amplitude','Fontsize',14);
title('Hermitian symmetry','Fontsize',14);
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',14);
ylabel('amplitude','Fontsize',14);
title('even frequency suppressed output','Fontsize',14);
y1c=conj(y1);
real_y1=y1.*y1c ;
figure(7);
x=1:M;
stem(x*Fm,real_y1);
grid on;
xlabel('Freq(Mhz)','Fontsize',14);
ylabel('amplitude of real values of ifft','Fontsize',14);
title('real value of ifft','Fontsize',14);
number_of_subcarriers=M;
assert(number_of_subcarriers <= M);
cp_start=block_size-cp_len;
for i=1:number_of_subcarriers,
S2P = reshape(qam_modulated_data, no_of_data_bits/M,M);
size(S2P)
ifft_Subcarrier(:,i) = ifft((S2P(:,i)),16);% 16 is the ifft point
size(ifft_Subcarrier)
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 sub-carriers')
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')
figure(11),plot((real(A1)),'r'),title('Orthogonality'),hold on ,plot((real(A2)),'c'),hold on ,
plot((real(A3)),'b'),hold on ,plot((real(A4)),'g'),hold on ,plot((real(A5)),'r'),hold on,plot((real(A6)),'b'),hold on,plot((real(A7)),'c'),hold on,plot((real(A8)),'g'),hold on,grid on
%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'); ylabel('Amplitude');
title('OFDM Signal');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 = rayleighchan(Ts, Fd,tau,pdb);
h.StoreHistory = 0;
h.StorePathGains = 1;
h.ResetBeforeFiltering = 1;
%Serial stream passing rayleigh fading through the channel
channel = filter(h,ofdm_signal(:,j).');
a = h.PathGains;
AM = h.channelFilter.alphaMatrix;
g = a*AM;
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'); ylabel('Amplitude');
title('OFDM Signal after rayleigh fading passing through channel');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')
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')
% Fourier Transofrm of the receivied 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')
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')
% 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');ylabel('Amplitude');
title('Original Data')
subplot (2,1,2),
num_sent = length(data_source);
lie_about_the_time = linspace(1, num_sent, length(qam_demodulated_data));
stairs(lie_about_the_time, qam_demodulated_data);
This is entire code sir,you only helped me to write this code.
Now I want to compare transmitted data and received data in the same scale.
Here is the code changed to lie about the time scale the way you asked. I did not repair the underlying problem in the code: I only fixed the graph to do what you think you want it to do. I did this specifically so that you could see that, in reality, your program is broken and that you have been focusing too long on getting the graph to look just right when really you should be rethinking about the logic of the program and fixing the logic.
I also upgraded your use of rayleighchan() to the new comm.RayleighChan, as the old function has been removed from MATLAB as of R2020b; you would have been getting warnings about that (unless you are using a sufficiently old version.) The changed portion of the code should work in R2013b and later.
M =8; %here we initialize the number of constellation point of qam
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
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',14);
ylabel('amplitude','Fontsize',14);
title('Transmitted Data','Fontsize',14);%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',14);%here we plot those constellation point
y=ifft(qam_modulated_data);
figure(4);
x=1:nqdata;
stem(x*Fm,abs(y));
grid minor;
xlabel('freq(Mhz)');
ylabel('amplitude of ifft');
title('without hermitian ifft','Fontsize',14);
data1 = udata;
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',14);
ylabel('amplitude','Fontsize',14);
title('Hermitian symmetry','Fontsize',14);
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',14);
ylabel('amplitude','Fontsize',14);
title('even frequency suppressed output','Fontsize',14);
y1c=conj(y1);
real_y1=y1.*y1c ;
figure(7);
x=1:M;
stem(x*Fm,real_y1);
grid on;
xlabel('Freq(Mhz)','Fontsize',14);
ylabel('amplitude of real values of ifft','Fontsize',14);
title('real value of ifft','Fontsize',14);
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);
%size(S2P)
ifft_Subcarrier(:,i) = ifft((S2P(:,i)),16);% 16 is the ifft point
%size(ifft_Subcarrier)
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 sub-carriers')
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')
figure(11),plot((real(A1)),'r'),title('Orthogonality'),hold on ,plot((real(A2)),'c'),hold on ,
plot((real(A3)),'b'),hold on ,plot((real(A4)),'g'),hold on ,plot((real(A5)),'r'),hold on,plot((real(A6)),'b'),hold on,plot((real(A7)),'c'),hold on,plot((real(A8)),'g'),hold on,grid on
%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'); ylabel('Amplitude');
title('OFDM Signal');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 = rayleighchan(Ts, Fd,tau,pdb);
%h = rayleighchan('InputSamplePeriod', Ts, 'MaxDopplerShift', Fd, 'PathDelays', tau, 'AvgPathGaindB', pdb );
h.StoreHistory = 0;
h.StorePathGains = 1;
h.ResetBeforeFiltering = 1;
%}
%we do not access the path gains, because the previous code
%did not actually USE the information, and it is not easy
%to figure out what the modern equivalent is.
h = comm.RayleighChannel('SampleRate', 1/Ts, ...
'MaximumDopplerShift', Fd, ...
'PathDelays', tau, ...
'AveragePathGains', pdb, ...
'PathGainsOutputPort', false);
%Serial stream passing rayleigh fading through the channel
%{
channel = filter(h,ofdm_signal(:,j).');
a = h.PathGains;
AM = h.channelFilter.alphaMatrix;
g = a*AM;
%}
reset(h); %ResetBeforeFiltering equivalent
channel = step(h, ofdm_signal(:,j).'); %second output is PathGains
%this LOOKS like a call to filter of the rayleighchan object, but does
%not use the rayleichan object. It is a completely different filter()
%function
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'); ylabel('Amplitude');
title('OFDM Signal after rayleigh fading passing through channel');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')
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')
% Fourier Transofrm of the receivied signal
%Similarly to how we took the IFFT of the original signal, as we are back
%tracking we are now taking the FFT
fft_data = zeros(16, number_of_subcarriers);
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')
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')
% 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');ylabel('Amplitude');
title('Original Data')
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);
sir you are simply great,I have no word to show my gratitude.
Sir one help please
S2P = reshape(qam_modulated_data, no_of_data_bits/M,M);
here instead of passing qam_modulated_data I want to pass udata1 .Because I want to add Cyclic prefix only on even frequency suppressed data not every constallation point of qam.So how can I achieve that,instead of qam_modulated_data i want to pass udata1,
How to calculate ber vs snr of ofdm in above code,can you please guide?

Sign in to comment.

More Answers (1)

subplot(211)
t = linspace(0,3,1200)
stem(1:100:1200,3*cos(t(1:100:1200)))
xlim([1 1200])
xticks([0 200 400 600 800 1000 1200])
xticklabels({'0','200','400','600','800','1000','1200'})
subplot(212)
t = linspace(0,8,1200)
stem(1:100:1200,8*sin(t(1:100:1200)))
xlim([1 1200])
xticks([0 200 400 600 800 1000 1200])
xticklabels({'0','200','400','600','800','1000','1200'})
Try some thing like above

Categories

Tags

Community Treasure Hunt

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

Start Hunting!