Showing posts with label 3D plots. Show all posts
Showing posts with label 3D plots. Show all posts

Tuesday, 11 March 2014

How to put legends only to some of the lines in a Matlab Plot?

1. Use the 'plot' handles.

x1 = 1:10;
y1blue = 2 .* x1;


y2green = 2 .* y1;

y3red = y2 + 5;

h1 = plot(x1,y1blue ,'b');
hold on;

h2 = plot(y2green,'g');
h3 = plot(y3red,'r');

%Legend all the line plots
legend('Y1blue','Y2green', 'Y3red');


%Legend only Y1 and Y2
legend([h1 h2],{'Y1','Y2'});

2. Another method to control the 'legend content' is given HERE


3. How to use numbers in the legend?

legend({['PRN' num2str(1)],['PRN' num2str(2)]});

Monday, 17 February 2014

Plot the spectrum of the real sinuoids and complex sinuoids.

Magnitude spectrum:
Magnitude spectrum of the sinusoids

Phase spectrum:

Phase spectrum (-fs/2 to +fs/2)

3D-Plots of Sinusoids: [note that Sin(-wt) should be read as Sin(wt)]
Sinusoids on Real and Imaginary axes

Complex Sinusoids 


%Code used to generate above plots

%% Clear the workspace
clear all;
close all;
clc;
home;

%% Parameters
fSamp = 1e3; % Hz
ts = 1/fSamp;
T = 1; % seconds - Data duration
t = 0:ts:T-ts;

fSig = 10; % Hz - Signal frequency
%% Real - sinusoids
% 1. Sine
s = sin(2*pi*fSig*t);

% 2. Co-sine
c = cos(2*pi*fSig*t);

% 3. Complex exponential
compExpoPositive = exp(1i*2*pi*fSig*t);
compExpoNegative = exp(-1i*2*pi*fSig*t);

%% Their Magnitude spectrum
df = fSamp / length(s);
freqIndex = -fSamp/2:df:fSamp/2 - df;

sFft = fftshift(fft(s)) ./ length(s);
% phaseSine = unwrap(angle((sFft))); %output is in radians but continuos (unwrap)
phaseSine = phase(sFft); %equal to unwrap and angle

cFft = fftshift(fft(c))./ length(s);
phaseCosine = unwrap(angle((cFft)));

fftCompPositive = fftshift(fft(compExpoPositive))./ length(s);
phaseCompPositive = phase(fftCompPositive);
fftCompNegative = fftshift(fft(compExpoNegative))./ length(s);
phaseCompNegative = phase(fftCompNegative);
% Sine
figure;
subplot(3,1,1);
stem(freqIndex,real(sFft),'b*'); hold on;
stem(freqIndex,imag(sFft),'ro');


xlim([-15 15]);
title('Sinusoid''s magnitude spectrum','color','b');
xlabel('Frequency (Hz)');
ylabel('Mag.');
legend('Real','Imag','location','best');

% Cosine
subplot(3,1,2);
stem(freqIndex,real(cFft),'g-'); hold on;
stem(freqIndex,imag(cFft),'m^');
xlim([-15 15]);
title('Co-sinusoid''s magnitude spectrum','color','b');
xlabel('Frequency (Hz)');
ylabel('Mag.');
legend('Real','Imag','location','best');

% Complex sinusoids
subplot(3,1,3);
stem(freqIndex,real(fftCompPositive),'b*'); hold on;
stem(freqIndex,imag(fftCompPositive),'ro');

stem(freqIndex,real(fftCompNegative),'g*');%,'markersize',5);
stem(freqIndex,imag(fftCompNegative),'mo');%,'markersize',5);
xlim([-15 15]);
title('Complex exponentials / Complex sinuoids','color','b');
xlabel('Frequency (Hz)');
ylabel('Mag. (arbitrary unit)');
legend('Real(exp(jwt))','Imag(exp(jwt))','Real(exp(-jwt))','Imag(exp(-jwt))','location','best');

% Setting plot properties
set(findall(gcf,'Type','line'),'linewidth',2);
set(findall(gcf,'type','axes'),'fontsize',12);
set(findall(gcf,'Type','text'),'FontSize',12);
saveas(gcf,'MagSpectrumFreqDomain.jpg');




%% Their Phase spectrum
figure;
plot(freqIndex,phaseSine*180/pi,'b');
hold on;
plot(freqIndex,phaseCosine*180/pi,'r');

plot(freqIndex,phaseCompPositive*180/pi,'g');
plot(freqIndex,phaseCompNegative*180/pi,'m');


xlim([-15 15]);
% Setting plot properties
title('Phase plot');
xlabel('Frequency (Hz)');
ylabel('Phase (degrees)');



legend('Sine','Co-sine','exp(+jwt)','exp(-jwt)','location','best');
set(findall(gcf,'Type','line'),'linewidth',2);
set(findall(gcf,'type','axes'),'fontsize',12);
set(findall(gcf,'Type','text'),'FontSize',12);

saveas(gcf,'PhaseSpecFreqDomain.jpg');




***************************************************************** 







%% 3D plot of the real and complex sinusoids

% Real sinusoids

% Complex sinusoids

% Code used to generate 3-D plots

%% Clear the workspace

clc;
clear all;
close all;

figure;

%% Plotting components of Co-Sinusoids

x=ones(1,6) .* 0.5;

y=0:0.1:0.5;

z=zeros(1,length(x));

plot3(x,y,z,'g');
hold on;

x=ones(1,6) .* -0.5;
y=0:0.1:0.5;
z=zeros(1,length(x));

plot3(x,y,z,'g+');

%% Plotting components of Sinusoids

x=ones(1,6) .* 0.5;
y=0:0.1:0.5;
z=zeros(1,length(x));

plot3(x,z,-y,'m');

x=ones(1,6) .* -0.5;
y=0:0.1:0.5;
z=zeros(1,length(x));
plot3(x,z,y,'m+');

%% Plotting axis lines

xL = [-1 1];%xlim;
yL = xL;
zL = yL;
line([0 0], yL, [0 0],'linewidth',3,'color','y'); %x-axis
line(xL, [0 0], [0 0],'linewidth',3,'color','k'); %y-axis
line([0 0], [0 0],zL,'linewidth',3,'color','b'); %y-axis
hold on;
grid on;

%%
axis equal;
set(findall(gcf,'Type','line'),'linewidth',2);
legend('Cos(wt) @ w', 'Cos(wt) @ -w', 'Sin(wt) @ w','Sin(-wt) @ -w');
ylabel('Real');
xlabel('Frequency (rad)');
zlabel('Imaginary')
view(36,25);
saveas(gcf,'RealSinusoids.jpg');

%% Plotting complex exponentials

figure;

x=ones(1,6) .* +0.5;
y=0:0.1:0.5;
z=zeros(1,length(x));
plot3(x,y,z,'r');
hold on;
x=ones(1,6) .* -0.5;
y=0:0.1:0.5;
z=zeros(1,length(x));
plot3(x,y,z,'b');

%
x=ones(1,6) .* 0.5;
y=0:0.1:0.5;
z=zeros(1,length(x));
plot3(x,z,-y,'r+');

x=ones(1,6) .* -0.5;
y=0:0.1:0.5;
z=zeros(1,length(x));
plot3(x,z,y,'b+');

%% Plotting lines
xL = [-1 1];%xlim;
yL = xL;
zL = yL;
line([0 0], yL, [0 0],'linewidth',3,'color','y'); %x-axis
line(xL, [0 0], [0 0],'linewidth',3,'color','k'); %y-axis
line([0 0], [0 0],zL,'linewidth',3,'color','b'); %y-axis
hold on;
grid on;
axis equal;
set(findall(gcf,'Type','line'),'linewidth',2);

legend('exp(+jwt) ','exp(-jwt)','-j*exp(+jwt)','j*exp(-jwt)');
ylabel('Real');
xlabel('Frequency (rad)');
zlabel('Imaginary')
whitebg('white');
% whitebg('blue')
view(36,25);
saveas(gcf,'ComplexExpo.jpg');    






Wednesday, 23 October 2013

How to rotate (or align) the labels in 3D plots?

1. The 'rot' attribute gives the access to rotate the labels. We need to know the angle by which we need to rotate.

xlabel('xMagic','rot', 14,'fontweight','bold','fontsize',12); 
ylabel('yMagic','rot', -20,'fontweight','bold','fontsize',12);

How to plot the shadow in the 'surf' plot in Matlab?

1. Follow these hints: Thanks to the 'shadowplot' function from Mathworks.
z = magic(100);
surf(z);
shading interp;
shadowplot x;
shadowplot y;

xlabel('xMagic','rot', 14,'fontweight','bold','fontsize',12); 
ylabel('yMagic','rot', -20,'fontweight','bold','fontsize',12);
zlabel('zMagic','fontweight','bold','fontsize',14);

Example of Shadow plot:


2. The shadowplot x and shadowplot y demand 'x' and 'y' to be monotonically increasing. Due to Matlab's floating point representation problem (clearly explained HERE), we will not get monotonically increasing number even when we use: dx = 0: 0.01:1; This can be checked by:

dx = 0:0.01:1;
figure;
plot(diff(dx));

This will become a problem to use 'shadowplot'. One method that could be used is:

a. Plot the 3-D space
surf(corrValues); % instead of surf(x,y, corrValues);
shading interp;
shadowplot y;
shadowplot x;

%Now the plot x and y indices will be integers starting from 1 to size(corrValues) respectively. Off course, there will be (0,0) point but corrValues are not pointed at this point.


b. Now to have the x- and y-axis labelling as desired, instead of integers, following method could be used:

% Get the string from the number array
for ii = 1:2:length(xx)
   xTickLabel1(ii).a = num2str(xx(ii), 4);
end

xlbl = {xTickLabel1.a};

for ii = 1:1:length(yy)
 yTickLabel1(ii).a = num2str(yy(ii), 4);
end

ylbl = {yTickLabel1.a};

% Set the grid-lines -- x and y indices where grid lines to be shown
set(gca, 'XTick',[1:1:length(xx)]);
set(gca, 'YTick',[1:1:length(yy)]);
% Label the grid-lines that are selected above.
set(gca,'XTickLabel',xlbl)
set(gca,'YTickLabel',ylbl)

Thursday, 3 October 2013

How to generate 3D plots in Matlab ?

How to generate 3D plots in Matlab ?

Use 'surf' command in Matlab

Example:

figure;
x = 1:100;
y = 1:100;
z = magic(100);
surf(x,y,z);

Another example to plot the correlation values of the GPS signal:

figure;
% fSamp = sampling frequency
% fCode = 1.023e6;
% fIf = Intermediate frequency, carFreqs -- dopplers
% x - chip axis
% y - frequency axis
% z - correlation values

x = (1:1:fSamp*1e-3)*fCode./fSamp;
y = carFreqs - fIf;
surf(x,y,abs(corrVal));