Showing posts with label MATLAB. Show all posts
Showing posts with label MATLAB. Show all posts

Monday, February 10, 2014

MATLAB Code to Generate a Chessboard



In the last 2 years, the writer attended a lecture on Linear Filters in Image Processing. Among the various assignments was to write a function which generates a chessboard/checkerboard, which then can be visualized as a conventional 2D image. This is a tutorial on how to apply a function called 'schachbrett' (the German word for chessboard) in MATLAB [1].



Why chessboard?
Figure 1: An artificially generated image of chessboard (256 x 256 pixel)

The chessboard (Figure 1) was deemed an interesting object to investigate frequencies after applying Fourier Transform. The test would looked like Figure 2 and Figure 3 below.

Figure 2: The real part of the Fourier Transform of lohSchahmat

Figure 3: The real part of the Fourier Transform of lohSchahmat (as a 3D-mesh plot)
The results would look finer if the writer had applied:
  1. a higher number of pixel and/or
  2. a higher number of fields. 

The function schachbrett()

This function takes 2 arguments - namely the no. of pixel and and the no. of fields to be displayed.

function [chessBoard] = schachbrett(noPxl,noField)

And the output is - chessBoard.

For security, the writer set default values as follows:


% Setting the default no. of pixels
if noPxl <= 0
    noPxl = 10;
end

% Dimension of the board => noField x noField
if noField <= 0
    noField = 2;
end

The here is where the magic happens:

boardTemp = 255*[ones(noPxl) zeros(noPxl); zeros(noPxl) ones(noPxl)];
chessBoard = repmat(boardTemp,noField/2,noField/2);

Firstly a 2x2 board of ones and zeros are generated, which resembles an identity matrix, except that this matrix is NOT an identity matrix. Then it is multiplied by the value of 255, so that all values of 1 become 255 (the color white in 8-bit grayscale).

Then the matrix is replicated with the help of a built-in MATLAB function called repmat() - replicated as half as the number of fields given as the argument noField in schachbrett() above.

How to apply the function schachbrett()

This step is very easy just like below, which has generated the image in Figure 1 above:


noPixel = 2^5;
noLineColumn = 2^3;
lohSchahmat = schachbrett(noPixel, noLineColumn);
figure('Name', 'BetHaMikdash'),
imshow(lohSchahmat)
title('Rosfah Schlomo')

That is all. Have fun with coding in MATLAB!

Remarks:

[1]. MATLAB is a registered trademark of The Mathworks, Inc. The code above was strictly used for academical and research purpose.

Friday, December 14, 2012

Plotting a pentagram with MATLAB.


Figure: As seen after running the code

This is a MATLAB code to plot the symbol of pentagram on a Cartesian coordinate system. The symbol consists of 1 circle and 1 pentagram - a 5-pointed star drawn with 5 straight strokes.

Basically this algorithm focuses on the getting the coordinates to draw:

(1) the circle and
(2) the horizontal line.

Later the horizonzal line is manipulated to generate the other 4 lines to form the pentagram.



Contents

The variables

n = 2^8; % Number of sampling points
t = linspace(0,2*pi,n); % Interval
noSides = 5; % Number of sides of the polygon
innerAngle = 2*pi/noSides; % Interior angle of a pentagon.

% Variables for the cirle
r = 2; % Radius
h = 0; % Transition distance
k = 0; % Transition distance

xsfact = 1.3; % Factor to extend the display range of the xy-axes.

Equations for the circle

x = r*cos(t)+h;
y = r*sin(t)+k;

Equation(s) for the horizontal line

% The length of the horizontal line
lH = r*sin(innerAngle)*2;
% Distance to the horizontal line from the center
dist_lH = r*cos(innerAngle);
% The definition interval of the horizontal line
xH = linspace(-lH/2, lH/2, n);

Rotation

Here the horizontal line from the previous will be simply duplicated for 4 times. Every point (x,y) will be multiplied with a 2D-rotation matrix.

side = zeros(2,n);
side(1,:) = xH;
side(2,:) = -dist_lH;

side1rot = rot2d(innerAngle)*side;
side2rot = rot2d(innerAngle*2)*side;
side3rot = rot2d(innerAngle*3)*side;
side4rot = rot2d(innerAngle*4)*side;

Plotting

figure,
hold on
plot(x,y, 'r')
plot(xH, -dist_lH, 'r')
plot(side1rot(1,:), side1rot(2,:), 'r')
plot(side2rot(1,:), side2rot(2,:), 'r')
plot(side3rot(1,:), side3rot(2,:), 'r')
plot(side4rot(1,:), side4rot(2,:), 'r')
hold off
axis(xsfact.*[min(x) max(x) min(y) max(y)])
title('Das Pentagramm')


Thursday, November 15, 2012

Easy Curve Fitting with MATLAB



Basically this exercise is about an easy and common curve fitting, which is used on a set of raw data points. The book 'Mathematische Formelsammlung' by Lothar Papula was referred for this exercise and its particular chapter is 'Ausgleichs- oder Regressionsparabel'.

Your mission, should you choose to accept it, is to fit a parabolic curve of the form y(x) = a*x^2 + b*x + c, to the given set of raw data points.

At the end of this post, I've included the link to download the M-script file.

Raw data
x = [-3:1:5]';
y = [-3.94 -1.47 -4.66 -3.21 0.84 7.24 15.58 27.94 42.59]';
[n,m] = size(x);
Calculation
The curve parameter a, b and c can be determined from the method of least squares (German- Methode der kleinsten Quadrate), which uses linear equation system with 3 equations and 3 unknowns.

x4 = x.^4;
x3 = x.^3;
x2 = x.^2;

sx4 = sum(x4);
sx3 = sum(x3);
sx2 = sum(x2);

sx = sum(x);
sy = sum(y);

x2y = x2.*y;
xy = x.*y;
sx2y = sum(x2y);
sxy = sum(xy);
Formation of the known matrices
The targetted matrix equation is as follows: A*K = vekb. Out of those 3 matrices, A and vekb are known.


A = [sx4 sx3 sx2;...
    sx3 sx2 sx;...
    sx2 sx n];

vekb = [sx2y; sxy; sy];
Determination of the curve parameter
The matrix K consists of the parameters - a, b and c, that we sought after. Simply multiply the inverse of A and vekb, both gained from the previous step.
K = inv(A)*vekb;
Generation of new data points
By using the gained parameters in K, new points can now be plotted.


yneu = K(1)*x.*x + K(2)*x + K(3);

Plotting

The displaying of the result.

M-Script code:
MV_Blatt_1_Aufgb5.m
p/s: just in case that you're not using MATLAB or Octave, that file can also be viewed in normal text editor =P