vibudh is talking

Posts tagged ‘MATLAB’

Dividing an image into smaller parts using MATLAB

Here we are going to learn how to divide an image and save each part into different files. As we have already seen earlier that we can insert a image/profile picture in Facebook chat.
Inserting Profile Picture:
But the size of those images inserted were too small, now if we want to insert larger images in the Facebook chat then we are simply going to insert multiple images alligned properly so that it appears to be a big image, I call it as the ‘Jumbo Image’.

Now we can divide the image manually in the paint and taking equal part at a time, but this method is too tedious and is neither too accurate. So, the simpler way out is to use MATLAB to do the job for us. So here we will be dividing the image into multiple parts(nXm) and then uploading all the images, so that when we insert them in the chat we get a Jumbo Image in the chat box comprising of various small images that we had uploaded earlier.

Here is the MATLAB code to divide a simgle image into multiple parts and saving each part into a different file:

clear; % removes all variables from the workspace, releasing them from system memory
Image = imread(‘’);
%Taking the image from the given URL, it could have been the name of the file with extension if the root folder
n =3; %defining the number of rows
m =3; %defining the number of columns
a=’a’; %defining a random file name
NewImage = ‘newimage.gif’; %granting permission to create a file and write in it
Image = imresize(Image, [512 512]); %resizing the image, this is a mandatory step

rf = floor(512/n); %generating the number of row pixels in the new file
cf = floor(512/m); %generating the number of row pixels in the new file

for v = 1:n
for s = 1:m %nXm files need to be made
startr = (v-1)*rf;
startc = (s-1)*cf;
for i = 1 : rf
for j = 1 : cf
for k = 1:3 %loop for RGB values

Img1(i,j,k) = Image(startr+i, startc+j,k);

a = strcat(a,’a’); %generating a new file name
NewImage = strcat(a,’.jpg’);
imwrite( Img1, NewImage);

It is generally adviced to make a seperate folder in your PC for each Jumbo Image, it simple makes the handing of the files simpler.


Control System: State Space using MATLAB

For a system defined by its state space variables, it is possible to determine the controllability, observability and hence the stability of the system using MATLAB. We will be discuss the same here with the help of an example.

A system is said to be controllable if we can transform the state of a system from xo to x(t) with the help of a control function u(t) over a finite period of time. If from measurements of output y(t) taken over a finite period of time, state of  a system x(t) can be determined, then the system is observable.

If a system is both controllable and observable then we can say that the system is stable.

Consider a linear time invariant(LTI) system described by the state equations:

x = A x + B u

y = C x + D u

Here x, y are the state variables, u represents the unit step response and A, B, C, D are the constants which depend on the system.

For a model with Nx states, Ny outputs, and Nu inputs:

  • a is an Nx-by-Nx real- or complex-valued matrix.
  • b is an Nx-by-Nu real- or complex-valued matrix.
  • c is an Ny-by-Nx real- or complex-valued matrix.
  • d is an Ny-by-Nu real- or complex-valued matrix.

sys = ss(a,b,c,d,Tscreates the discrete-time model

with sample time Ts (in seconds). Set Ts = -1 or Ts = [] to leave the sample time unspecified.

Matlab Code:-

a=[0 1 0; 0 0 1; -6 -11 -6];

b=[0; 0; 2];

c=[1 0 0];


sys = ss(a,b,c,d);                                %Creating the state space model

xo=[0 0 0];                                           %Setting initial conditions


step(sys);                                               %for step response

ob = obsv(sys);                                   %for calculating observability

unob = length(a)-rank(ob)             %for calculating unobservability

ct = ctrb(sys);                                      %for calculating controllability

unct = length(a)-rank(ct)                %for calculating uncontrollability

eigen = eig(a)                                        %for obtaining the eigen values


unob = 0

unct =0

eigen = -1.0000



As the system is controllable and observable, hence we can say that the system is stable.

Control System: Block Diagrams Reduction using MATLAB

Most of the circuits in Control System today are represented by simple blocks that help us understand the function of each block in a better way. Is also helps the designers to easily make amendments in the circuit for better functionality and testing purpose. But the problem with Block Diagrams is that having blocks and their feedbacks makes the transfer function on the system to tedious to calculate.

Here we are going to study block reduction using MATLAB. The blocks connected in series, parallel and as feedbacks are at times very tedious to compute. MATLAB allows solving of such blocks directly using some functions that is being discussed below with the help of the example. Here we have to calculate C(s)/R(s), that is taken as T(s).

The MATLAB code for the above problem is:

num1 = [1 2];
den1 = [3 1 0];
G1 = tf(num1, den1)                      %Making G1 as the tranfer function
G2 = tf( [2], [1 7] )
G3 = tf( [1 5], [1 6 3 ] )
G4 = tf( [1], [1 0] )

T1 = parallel(G1, G2)                   %as G1 and G2 are in parallel
T2 = series(T1, G3)                       %as T1 and G3 are in series
T = feedback(T2, G4, -1)            %as G4 is the negative feedback

Here we use the tf() function to get the transfer function
parallel() and series() functions according to the requirement
and the feedback() function for feedback.

The output for the above code is as follows:

s + 2
3 s^2 + s

Transfer function:
s + 7

Transfer function:
s + 5
s^2 + 6 s + 3

Transfer function:


Transfer function:
7 s^2 + 11 s + 14
3 s^3 + 22 s^2 + 7 s

Transfer function:
7 s^3 + 46 s^2 + 69 s + 70
3 s^5 + 40 s^4 + 148 s^3 + 108 s^2 + 21 s

Transfer function:
7 s^4 + 46 s^3 + 69 s^2 + 70 s
3 s^6 + 40 s^5 + 148 s^4 + 115 s^3 + 67 s^2 + 69 s + 70

Here we can see that the transfer function for the block diagram is very complex and tedious to deduce. Which can be obtained by using MATLAB very easily.