Showing posts with label Plots. Show all posts
Showing posts with label Plots. Show all posts

Thursday, 6 February 2014

Euler's formula and How to plot the XY axis at the center of the plot?


%% Clearing workspace
clc;
clear all;
close all;
home;

%% Just a circle -- Euler's circle
f = 1; tm = 1/f;
numberOfSamplesPerCycle = 6; % define the sides of the polygon, if large the polygon appears as circle. If = 6, we get hexagon.

dt = tm / numberOfSamplesPerCycle;

t = 0:dt:(1/f); % the number of sides in the circle depend on the 'dt' step - this define the sample time interval

s = exp(1i*2*pi*f*t);


%% Another way to plot a circle -- Trigonometric relation with complex numbers

% We can get the same polygon using this below code:
r = abs(s);
theta = phase(s);
x = r .* cos(theta);
y = r .* sin(theta);
figure; plot(s,'g'); hold on; plot(x,y,'r*');% hold on;
legend('Euler''s style', 'polar style','Location','best');
xlabel('Real part');
ylabel('Imaginary part');
set(findall(gcf, '-property', 'FontSize'), 'FontSize', 12, 'fontWeight', 'normal')
set(findall(gcf,'Type','line'),'linewidth',2);
saveas(gcf,'fig1.jpg');
Circle plot using Euler's formula and polar form formula

%% To draw the XY axes at the centre of the plot
xL = xlim;
yL = ylim;
line([0 0], yL); %x-axis
line(xL, [0 0]); %y-axis


xlabel('Real part');
ylabel('Imaginary part');
set(findall(gcf, '-property', 'FontSize'), 'FontSize', 12, 'fontWeight', 'normal')
set(findall(gcf,'Type','line'),'linewidth',2);
saveas(gcf,'fig2.jpg');

Plotting Center XY coordinate lines

%% To draw a circle
f = 1; tm = 1/f;
numberOfSamplesPerCycle = 60; % define the sides of the polygon, if large the polygon appears as circle. If = 6, we get hexagon.

dt = tm / numberOfSamplesPerCycle;

t = 0:dt:(1/f); % the number of sides in the circle depend on the 'dt' step - this define the sample time interval
s = exp(1i*2*pi*f*t);

figure; plot(s,'r');

xL = xlim;
yL = ylim;
line([0 0], yL); %x-axis
line(xL, [0 0]); %y-axis

xlabel('Real part');
ylabel('Imaginary part');
set(findall(gcf, '-property', 'FontSize'), 'FontSize', 12, 'fontWeight', 'normal')
set(findall(gcf,'Type','line'),'linewidth',2);

saveas(gcf,'fig3.jpg');
Smooth Cirle



%% Concentric circles
r1 = 0:0.1:50;
theta1 = r1;
x1 = r1 .* cos(theta1);
y1 = r1 .* sin(theta1);

figure;plot(x1,y1,'g')
xL = xlim;
yL = ylim;
line([0 0], yL); %x-axis
line(xL, [0 0]); %y-axis

xlabel('Real part');
ylabel('Imaginary part');
set(findall(gcf, '-property', 'FontSize'), 'FontSize', 12, 'fontWeight', 'normal')
set(findall(gcf,'Type','line'),'linewidth',2);
saveas(gcf,'fig4.jpg');
Spiral plot using Euler's formula


%% Complex plane: s-plane
sigma = 0:0.1:1;
jOmega = sigma;
s = sigma + 1i * jOmega;
figure;plot(s)
xlabel('Real part');
ylabel('Imaginary part');
set(findall(gcf, '-property', 'FontSize'), 'FontSize', 12, 'fontWeight', 'normal')
set(findall(gcf,'Type','line'),'linewidth',2);

saveas(gcf,'fig5.jpg');
S-plane plot of a complex number

Wednesday, 23 October 2013

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)

Tuesday, 22 October 2013

How to give names to each indices of an axis in Matlab?

Taken from Matlab help:

x = -pi:.1:pi;
y = sin(x);
p = plot(x,y)

set(gca,'XTick',-pi:pi/2:pi)

set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'})

xlabel('-\pi \leq \Theta \leq \pi')

ylabel('sin(\Theta)')

title('Plot of sin(\Theta)')

How to change the GRID size and Grid labels (of x, y, and z axis indices) in Matlab Plots?

1 . Simplest command is:

>> grid minor


2. If we need to customize the grid-line locations, then

a. Equal spacing:

set(gca,'xtick',[0:13:100])
set(gca,'ytick',linspace(-50,50,0))


b. Unequal spacing

xGrid = [-5 -2 -0 4 6 9]; %This need not cover the entire range of x. Where ever %grid-lines are needed we can have


set(gca,'ytick',[-50:2:0])

set(gca,'xtick',xGrid)

c. Example of playing with grid size and grid labels
%Code search space (x-axis) indices
xx = codeChipInd-1;
for ii = 1:2:length(xx)
  xTickLabel1(ii).a = num2str(xx(ii).*(fCode/fSamp), 4);
end
xlbl = {xTickLabel1.a};

%Frequency search space (y-axis) indices
yy = freqSearchRange(1:1:end);
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
%Define the grid-line locations
set(gca, 'XTick',[1:1200:length(xx)]); % selecting every 1200 samples (100 chips) (12.276 MHz Fs=> 12 samples per code)

set(gca, 'YTick',[1:10:length(yy)]); %selecting every 10 frequency bin

% Label the grid-lines that are selected above.
set(gca,'XTickLabel',xlbl(1:1200:end))
set(gca,'YTickLabel',ylbl(1:10:end))


GPS Acquisition: Delay and Frequency search space

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));