Showing posts with label matlab tutorial. Show all posts
Showing posts with label matlab tutorial. Show all posts
Friday, 15 June 2012
On 07:41 by Unknown in matlab tutorial No comments
Here in this post I discuss
about how to connect MATLAB? And taking images from Webcam?
So first of all we
need a videobject of the webcam video. Here
“obj =
videoinput('winvideo',1)” create the videoobject. The description of this line is discussed
next.
The winvideo is a video
adapter for webcam. And videoinput creates an object of webcam through winvideo
adapter, & save it in obj variable. One another thing is the definition of
webcam. It is possible that one have more than one webcams. Since the sequence
number of webcam defines which webcam you want to connect. 1 is for internal system webcam or 1st
webcam. If someone has two webcam & want to connect the 2nd
webcam to MATLAB then it will be 2.
After creation the
videoobject now I call this object and run the live video through “preview (obj)” command. We have
live video, now Images can take easily from this video, “getsapshot(obj)” function is used for this purpose. We can
easily convert this image frame to grayscale image through function “rgb2gray(frame)”. At almost all cases the same size images
needed for further procession, since the images are resized to 64 by 64 size.
64 by 64 is so small size, you can resize it to 128 by 128, 320 by 440 etc,
this process is done through “imresize(file_name,[128,
128])”. We can show this image
by “imshow(frame)” function.
But only the first image will show.
We can also save this image
to the system directory through “imwrite(file-name,
directory, extention-type-of-image)” function. In the code “int2str(k)” is used to convert
the integer value of k to string type, to save it in a directory.
Loop while defines the number
of images one can want to take from video. it will be decide by environment and
the purposes where the program is used. Here I take 5 images from this video
which is done through the highest value of k, “if (k==4)”. Now I pause the first image to the screen to
show first image, close the image, & then delete the videoobject, to save
memory.
%% Create a video object to the webcame of system.
obj = videoinput('winvideo',1);
preview(obj);
k = 0;
while(true)
frame =
getsnapshot(obj);
frame=rgb2gray(frame);
% Resize Image Size to desired size. I resized it to 64 by 64
frame=imresize(frame,
[64 64]);
imshow(frame);
k = k+1;
% Save the
Images to a directory
imwrite(frame,'F:\ImageProcessing\studymaterial\PROJECT\OurProjet\Detection_Queue\image' , int2str(k),'.jpeg']);
% k defines number of Images take from video,I take
5 Images.
if(k==4)
break;
end
end
%% This will remove the video object permanently
% pause will still the image on the screen
pause(1);
% This will remove the showing image from memory
close;
% This will permanently delete the video object from memory
delete(obj);
![]() |
Live Video Stream |
![]() |
First Image Captured from video |
![]() |
All 4 Images Taking from Video |
Monday, 4 June 2012
On 14:13 by Unknown in matlab tutorial 1 comment
Signals, FFT & IFFT
In Matlab we can draw different types of signal. Also do different operations on that signal in time, frequency, and Z domains. This is the beauty of DSP that we work o signals in frequency domain and then go back to Time domain easily. Here in this post I will discuss to draw different signals in Time Domain and then go to Frequency domain, do some changes in Frequency domain, then go back to Time domain and see changes which was done in Time domain.
n=0:1:1024; % Define the time interval
w1 = 0.1 * pi; % Define the Omega (w) values for cosine signals
w2 = 0.2 * pi;
w3 = 0.4 * pi;
xn = sin(w1*n) + sin(w2*n) + sin (w3*n); % Signal is a Composite signal
figure (1)
subplot(3,1,1);
plot(n,xn);
title('Original Signal X[n]');
xf = fft(xn); % fft Command is used to convert to Frequency Domain
subplot(3,1,2);
plot(n,xf);
title('X(e^jw))');
xmag = abs(xf); % Magnitude of fft Signal
subplot(3,1,3);
plot(n,xmag);
title('Magnitude of X(e^jw))');
x = sin(w3*n); % 2nd Signal in Time Domain
x_fft = fft(x); % fft of 2nd Signal
xf = xf - x_fft; % Subtract 2nd Signal from Original Signal in Frequency Domain
xnt=ifft(real(xf)); % Convert Back Modified F_domain Signal to Time domain
figure (2)
plot (n,xnt);
title('Back to Time Domain & see the changes');
![]() |
a) Time Domain, b) Frequency Domain, c) Magnitude in Frequency Domain |
![]() |
Reverse Signal in Time Domain From Frequency Domain |
Wednesday, 16 May 2012
On 10:57 by Unknown in matlab tutorial No comments
Convolution Methods & Coding
Convolution is the process of multiplying two or more signals in time domain. Theoretically it is difficult to don convolution in time domain. for that we goes to frequency domain. because in frequency domain just multiply both convoled signals
y[n]= summation (with upper and lower limits of infinity){x[k]h[n-k]}
>> XMIN = -10; % Define the Initial Point of X-axis
>> XMAX = 25; % Define the Ending Point of X-axis
>> YMIN = 0; % Define the initial Point of Y-axis
>> YMAX = 6; % Define the Ending Point of X-axis
>> axis_size = [XMIN, XMAX, YMIN, YMAX];
>> % generate two input signals
>> xn1 = [0 0 0 1 1 0 1 0 1 0 1 0 0 0 0 0];
>> xn2 = [0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 0];
>> % generate indices for the input and output signals
>> n = -4:11; % Limits of First Signal X1[n]
>> m = -8:22; % Limits of Second Signal X2[n]
>> % convolve two signals
>> yn = conv(xn1,xn1);
>> grid on % grid on meanz the grid view in the figure
>> subplot(3, 1, 1);
>> stem(n, xn1,'filled', 'r'), axis(axis_size), ylabel ('xn1'), grid;
>> title ('First Signal')
>> subplot (3, 1, 2);
>> stem(n, xn2, 'filled', 'r'),axis(axis_size), ylabel ('xn2'), grid;
>> title ('Second Signal')
>> subplot (3, 1, 3);
>>stem(m,yn,'filled','b'),axis(axis_size),xlabel('yn=xn1*xn2'), grid;
>> title ('Result of Convolution');
The Result of this Program is Shown below. try to run it in matlab.
Tuesday, 15 May 2012
On 15:13 by Unknown in matlab tutorial 1 comment
Matlab Basic Part 2
In the first Part We See Some Basic of Matlab. now in the Part 2we will see some advanced functions and methods of Matlab. then we go forward to our work on the Telecom And Networking side. In this section we will see about the loop statements and how to draw plots figure etc.
if, if else, else Statement in Matlab
>> A=109;
>> B=104;
>> If (A > B)
>> fprintf('A is Greater than B');
>> elseif (A < B)
>> fprintf('B is greater than A');
>> elseif (A = = B)
>> fprintf('B is equal with A');
>> else
>> fprintf('Unexpected situation');
>> end
In Matlab, loops can be implemented with a for ...end construct or a while ...end construct. In terms of their ability to repeat a series of calculations, for loops and while loops are equivalent.
for Loops
for loops are often used when a sequence of operations is to be performed a predetermined number of times. For example computing the Maximum or Minimum of a list of numbers.
Syntax
Loop counter incremented by one:
for i = startingValue : endingValue
x = Some Value
y = Some value
Do somthing
...
...
...
end
Exp1: Compute the sum of the fi rst n integers
>> n = 10;
>> sum = 0;
>> for i = 1 : n
>> sum = sum + i;
>> end
Exp2: Compute the average of a list of numbers
>> n = 500;
>> x = rand(1,n); % generate a row vector of pseudo random number having length n
>> sum = 0;
>> for i=1:n
>> sum = sum + x(i); % Add ith element of x to sum
>> end
>> avg = sum/n;
while loop
While Loop is same as for loop, only the difference is it first check the condition then do action. syntax is below
while(condition)
...
...
do something
...
...
end
Switch Statement
In Matlab switch statement is same as other languages like C/C++. but in Matlab no parenthesis are used. the syntax is below:
switch(y)
case(1)
do something
break
case (2)
do something
break
...
...
...
case(n)
do something
break
default
display default message/ do default work
end
Graphics
Graphics are used to see the graphical representation of a sequence/ signal etc. In Matlab two major types of plot are used. one is "plot" command used for continuous signal, second is "stem" command for discrete type signal.
"subplot" command is used if one want to display multipull plots in one figure at different location. if one wants to display one plot together with another plot the after first plot "hold on" command is used. Remember that "figure" command is used to create one figure.
Exp 3: sine generation: A*sin(omega*n+theta)
>> n = 0: 1: 50;
>> A = 0.87; % Amplitude of sin wave
>> theta = 0.4; % Phase of sin wave
>> omega = 2*pi / 20; % frequency of sin wave
>> xn1 = A*sin(omega*n+theta); % sin generation
>> figure (1); % create first figure
>> subplot(2,1,1); % there are two locations for plot in figure 1
>> plot(xn1);
>> title('first signal');
>> xn2 = A.^n; % exp generation
>> subplot(2,1,2); % xn2 will be plot on the 2nd row 1st column
>> plot(xn2);
>> title('Exponential signal');
>> figure (2);
>> plot(xn1);
>> hold on;
>> plot(xn2);
>> hold off;
>> figure (3);
>> stem (n, xn1);
On 00:11 by Unknown in matlab tutorial 1 comment
Basic to MATLAB
Matlab is a commercial "Matrix Laboratory" package which operates as an interactive programming environment. It is available for PC's, Macintosh and UNIX systems. Matlab is well adapted to numerical experiments. Programming becomes more easy through Matlab. We can write Programs as a Script (m-files). Program and Script files (m-files) always have file names ending with ".m". Almost every data object is assumed to be an array. Also note that Matlab is a Case sensitive language, it means that 'A' and 'a' are two different variables.
The beauty of Matlab is that it is more Programming easy. There is allot of built in functions and some toolboxes to easy work. Graphical output (figure) is available to supplement numerical results. We can also Online help is available from the Matlab prompt by typing help.
Now lets start some basic functions and m-files
>> help
help gives us information about the built in function there description. for exp
>> help imwrite()
Matrix: matrix have rows and columns. we can specify rows and columns of any dimentions.
>> A=[1 2 3 4 , 4 3 2 1]
Here are 2 rows and 4 columns. A Scalar is 1*1 matrix in Matlab. A=1. A Vector of n elements can be represent by n*1 matrix. there is a row vector n*1 and a column vector 1*n.
>> colvec=[2, 5, 3, 1, 6] % colvec is column vector with 5*1
>> rowvec=[2 5 3 1 6] % rowvec is a row vector with 1*5
Colon operator: The colon operator ' : ' is understood by Matlab to perform special and useful operations.
for exp if two integer numbers are separated by a colon, Matlab will generate all of the integers between these two integers.
>>a = 1:8 % generate a sequence through colon operator
>>a = [ 1 2 3 4 5 6 7 8 ] % this method also generate a seq in a matrix form. both are same
If three numbers, integer or non-integer, are separated by two colons, the middle number is interpreted to be a ”step" and the first and third are interpreted to be "limits”:
>>b = 0.0 : .5 : 5.0
it will generates the row vector b = [ 0.0 .5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0]
Colon Operator also used to create a vector from a matrix. lets a
>> B=[1 2 3; 3 4 7, 7 9 3] % B is 3*3 matrix, now see next line
>>rowB=B(1 , :) % it will generate a row vector like
rowB=[1 2 3]
>> colB=B(: , 1) % it will generate a column vector like
colB=[1
3
7]
Some Basic Commands of Matlab
>>pwd % prints working directory
>>demo % demonstrates what is possible in Matlab
>>who % lists all of the variables in your matlab workspace
>>whos % list the variables and describes their matrix size
>>clear % rases variables and functions from memory
>>clear x %erases the matrix 'x' from your workspace
>>close % by itself, closes the current figure window
>>figure % creates an empty figure window
>>hold on % holds the current plot and all axis properties so that
>>subsequent % graphing commands add to the existing graph
>>hold off % sets the next plot property of the current axes to "replace"
>>save % saves all the matrices defined in the current session into the file, matlab.mat, located in the current working directory
>>load % loads contents of matlab.mat into current workspace
>>save filename x y z % saves the matrices x, y and z into the file titled filename.mat
>>save filename x y z /ascii % save the matrices x, y and z into the file titled filename.dat
>> load filename % loads the contents of filename into current workspace; the file can be a binary (.mat)
>>load filename.dat % loads the contents of filename.dat into the variable filename
>> grid % creates a grid on the graphics plot
>>title('text') % places a title at top of graphics plot
>>xlabel('text') % writes 'text' beneath the x-axis of a plot
>>ylabel('text') % writes 'text' beside the y axis of a plot
>>text(x,y,'text') % writes 'text' at the location (x,y)
>> text(x,y,'text','sc') % writes 'text' at point x,y assuming lower left corner is (0,0) and upper right corner is (1,1)
>> axis([xmin xmax ymin ymax]) %sets scaling for the x- and y-axes on the current plot
Matrix Operation
>> A=[1 2 3; 2 3 4; 7 8 1]
>> B=[3 5 1; 2 7 0; 8 3 6]
>> A+B; % is an addition Operation
>> A-B; % is an subtraction Operation
>> A*B; % is an Multiplication Operation
>> A/B; % is an Division Operation
>> A' % is a Transpose Matrix of A
>> A^2 % is a Power Operation. here Power is Square
>> C = A .* B % is point by point Multiplication used when dimension mismatched
>> A(1:3 , 2) % is the first to the third elements of the second column of A.
>> C = zeros(1,3) % generate Matrix C of all zeros like C=[0 0 0]
>> C = ones(1,4) % generates Matrix C of all Ones like C=[1 1 1 1]
>> C = rand(3,5) % uniformly distributed random elements
>> C = randn(2,5) % normally distributed random elements
>> B = [A A-2; B*2 C/4] % Construction of Matrix Z from Small matrices.
Functions:
MATLAB provides a large number of standard elementary mathematical functions, including abs, sqrt, exp, sin, cos, magic etc.
Mathematical Functions
>> help elfun % this will give list of all Elementary Maths Functions
Advanced Mathematical and Matrix Functions
>>help specfun
>> help elmat
Data Analysis Functions
>>help datafun
Programming with Matlab
Matlab Programming codes are save into m-files. To start a new program code, go to files, select new files, and then select text editor. a new window will be opened. now write your program in this file and save it as M-file. M-files are of two types. script and functions. functions are like methods they can accept arguments and return outputs, but the function name should be same as the M-file name. the script is same as normal program contains a bunch of code.
Flow Controls
Matlab is a Programming language which support all flow controls types- If statement
- Switch statement
- For loops
- While loops
- Continue statement
- Break statement
All these flow control and some other important graphs will be discussed in the next Post "Matlab Tutorial Part2".
Subscribe to:
Posts (Atom)
Search
Popular Posts
-
Here in this post I discuss about how to connect MATLAB? And taking images from Webcam? So first of all we need a videobject of the web...
-
Erlang C table is attached in this post with up to 45 number of channels, and more GOS probability values. This will help you to solve Erla...
-
Erlang B table is attached in this post with up to 115 number of channels, and more GOS probability values. This will help you to solve Erl...
-
Example Mini-AES Encryption The application of the four components NibbleSub , ShiftRow , MixColumn and KeyAddition in sequence con...
-
Mini Advanced Encryption Standard (Mini-AES): A Testbed for Cryptanalysis Students Raphael Chung-Wei Phan ADDRESS: Swin...
-
Matlab Basic Part 2 In the first Part We See Some Basic of Matlab. now in the Part 2we will see some advanced functions and methods of...
-
Telecommunication & Networking is the emerging technologies now a days. In fast these both fields are distinguish from each other. Tel...
-
Data and Network Security book by William Stalling is in attachment. Please follow the below link to find the book. This book provides yo...
-
Programming Methodologies Two popular approaches to programming design are the structured approach and the object-oriented approach, whi...
-
Basic to MATLAB Matlab is a commercial "Matrix Laboratory" package which operates as an interactive programming environmen...
Categories
- Advanced Wireless Networks (26)
- Wireless Networks (21)
- Data and Network Security (20)
- Digital Logic Design (7)
- matlab tutorial (5)
- C Programing (3)
- Research Papers (2)
Editorial
- Javed Chaudhry (21)
- PANAMA Leaks (18)
- Wasat Ullah Khan (11)
- Abdul Qadir Hassan (10)
- PAK-America Relationship (7)
- Ali Ahmad Dhalo (6)
- Muqtada Mansoor (6)
- Asghar Abdullah (4)
- Dr. Abdul Qadeer Khan (4)
- PAK-India Relationship (4)
- Aftab Iqbal (2)
- Ayaz Ameer (2)
- Doctor Atta Ur Rehman (2)
- PAK-Afghan Relationship (2)
- PAK-Chaina Relationship (2)
- PAK-Iran Relationship (2)
- Anees Baqir (1)
Sample Text
Blog Archive
My Traffic
Powered by Blogger.