%matlab short notes 25 March 2009
Getting Started
Expressions
Like most other programming languages, MATLAB provides mathematical expressions, but unlike most programming languages, these expressions involve entire matrices. The building blocks of expressions are Variables
Numbers
Operators
Functions
See also Examples of Expressions.
Variables
MATLAB does not require any type declarations or dimension statements. When MATLAB encounters a new variable name, it automatically creates the variable and allocates the appropriate amount of storage. If the variable already exists, MATLAB changes its contents and, if necessary, allocates new storage. For example,
num_students = 25
creates a 1-by-1 matrix named num_students and stores the value 25 in its single element.
Variable names consist of a letter, followed by any number of letters, digits, or underscores. MATLAB uses only the first 31 characters of a variable name. MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. A and a are not the same variable. To view the matrix assigned to any variable, simply enter the variable name.
Wednesday, March 25, 2009
Numbers
MATLAB uses conventional decimal notation, with an optional decimal point and leading plus or minus sign, for numbers. Scientific notation uses the letter e to specify a power-of-ten scale factor. Imaginary numbers use either i or j as a suffix. Some examples of legal numbers are
3 -99 0.0001
9.6397238 1.60210e-20 6.02252e23
1i -3.14159j 3e5i
All numbers are stored internally using the long format specified by the IEEE floating-point standard. Floating-point numbers have a finite precision of roughly 16 significant decimal digits and a finite range of roughly 10-308 to 10+308.
Operators
Expressions use familiar arithmetic operators and precedence rules.
+ Addition-Subtraction
*Multiplication/Division
\Left division (described in "Matrices and Linear Algebra" in the MATLAB documentation)
^Power'Complex conjugate transpose
( )Specify evaluation order
ting Started
Functions
MATLAB provides a large number of standard elementary mathematical functions, including abs, sqrt, exp, and sin. Taking the square root or logarithm of a negative number is not an error; the appropriate complex result is produced automatically. MATLAB also provides many more advanced mathematical functions, including Bessel and gamma functions. Most of these functions accept complex arguments. For a list of the elementary mathematical functions, type
help elfun
For a list of more advanced mathematical and matrix functions, type
help specfun
help elmat
Some of the functions, like sqrt and sin, are built in. Built-in functions are part of the MATLAB core so they are very efficient, but the computational details are not readily accessible. Other functions, like gamma and sinh, are implemented in M-files. There are some differences between built-in functions and other functions. For example, for built-in functions, you cannot see the code. For other functions, you can see the code and even modify it if you want.
Several special functions provide values of useful constants.
pi3.14159265...iImaginary unit, jSame as iepsFloating-point relative precision, realmin Smallest floating-point number, realmaxLargest floating-point number, InfInfinityNaNNot-a-number
Infinity is generated by dividing a nonzero value by zero, or by evaluating well defined mathematical expressions that overflow, i.e., exceed realmax. Not-a-number is generated by trying to evaluate expressions like 0/0 or Inf-Inf that do not have well defined mathematical values.
The function names are not reserved. It is possible to overwrite any of them with a new variable, such as
eps = 1.e-6
and then use that value in subsequent calculations. The original function can be restored with
clear eps
Wednesday, March 25, 2009
Examples of Expressions
You have already seen several examples of MATLAB expressions. Here are a few more examples, and the resulting values:
rho = (1+sqrt(5))/2
rho =
1.6180
a = abs(3+4i)
a =
5
z = sqrt(besselk(4/3,rho-i))
z =
0.3730+ 0.3214i
huge = exp(log(realmax))
huge =
1.7977e+308
toobig = pi*huge
toobig =
Inf
Wednesday, March 25, 2009
Starting MATLAB
On Windows platforms, start MATLAB by double-clicking the MATLAB shortcut icon on your Windows desktop.
On UNIX platforms, start MATLAB by typing matlab at the operating system prompt.
You can customize MATLAB startup. For example, you can change the directory in which MATLAB starts or automatically execute MATLAB statements in a script file named startup.m.
Thursday, March 26, 2009
5:59 AM
Noise
Noise is considered to be any measurement that is not part of the phenomena of interest. Noise can be generated within the electrical components of the input amplifier (internal noise), or it can be added to the signal as it travels down the input wires to the amplifier (external noise). Techniques that you can use to reduce the effects of noise are described below.
Configure the input channels in differential mode. Refer to Channel Configuration for more information about channel configuration. Use signal wires that are twisted together rather than separate. Keep the signal wires as short as possible. Keep the signal wires as far away as possible from environmental electrical activity.
Filtering
Filtering also reduces signal noise. For many data acquisition applications, a low-pass filter is beneficial. As the name suggests, a low-pass filter passes the lower frequency components but attenuates the higher frequency components. The cut-off frequency of the filter must be compatible with the frequencies present in the signal of interest and the sampling rate used for the A/D conversion.
A low-pass filter that's used to prevent higher frequencies from introducing distortion into the digitized signal is known as an antialiasing filter if the cut-off occurs at the Nyquist frequency. That is, the filter removes frequencies greater than one-half the sampling frequency. These filters generally have a sharper cut-off than the normal low-pass filter used to condition a signal. Antialiasing filters are specified according to the sampling rate of the system and there must be one filter per input signal.
Communications Toolbox
White Gaussian Noise
The wgn function generates random matrices using a white Gaussian noise distribution. You specify the power of the noise in either dBW (decibels relative to a watt), dBm, or linear units. You can generate either real or complex noise.
For example, the command below generates a column vector of length 50 containing real white Gaussian noise whose power is 2 dBW. The function assumes that the load impedance is 1 ohm.
y1 = wgn(50,1,2);
To generate complex white Gaussian noise whose power is 2 Watts, across a load of 60 ohms, use either of the commands below. Notice that the ordering of the string inputs does not matter.
y2 = wgn(50,1,2,60,'complex','linear');
y3 = wgn(50,1,2,60,'linear','complex');
To send a signal through an additive white Gaussian noise channel, use the awgn function. See AWGN Channel for more information.
Imnoise
Add noise to an image
Syntax
J = imnoise(I,type)
J = imnoise(I,type,parameters)
Description
J = imnoise(I,type) adds noise of a given type to the intensity image I. type is a string that can have one of these values.
Value Description
'gaussian' Gaussian white noise with constant mean and variance
'localvar' Zero-mean Gaussian white noise with an intensity-dependent variance
'poisson' Poisson noise
'salt & pepper' On and off pixels
'speckle' Multiplicative noise.
Depending on which type of noise you specify, imnoise accepts additional parameters. If you omit these arguments, imnoise uses default values for the parameters. All numerical parameters are normalized; they correspond to operations with images with intensities ranging from 0 to 1.
J = imnoise(I,'gaussian',m,v) adds Gaussian white noise of mean m and variance v to the image I. The default is zero mean noise with 0.01 variance.
J = imnoise(I,'localvar',V) adds zero-mean, Gaussian white noise of local variance V to the image I. V is an array of the same size as I.
J = imnoise(I,'localvar',image_intensity,var) adds zero-mean, Gaussian noise to an image I, where the local variance of the noise, var, is a function of the image intensity values in I. The image_intensity and var arguments are vectors of the same size, and plot(image_intensity,var) plots the functional relationship between noise variance and image intensity. The image_intensity vector must contain normalized intensity values ranging from 0 to 1.
J = imnoise(I,'poisson') generates Poisson noise from the data instead of adding artificial noise to the data. In order to respect Poisson statistics, the intensities of unit8 and uint16 images must correspond to the number of photons (or any other quanta of information). Double-precision images are used when the number of photons per pixel can be much larger than 65535 (but less than 10^12); the intensity values vary between 0 and 1 and correspond to the number of photons divided by 10^12.
J = imnoise(I,'salt & pepper',d) adds salt and pepper noise to the image I, where d is the noise density. This affects approximately d*prod(size(I)) pixels. The default is 0.05 noise density.
J = imnoise(I,'speckle',v) adds multiplicative noise to the image I, using the equation J = I+n*I, where n is uniformly distributed random noise with mean 0 and variance v. The default for v is 0.04.
Note The mean and variance parameters for 'gaussian', 'localvar', and 'speckle' noise types are always specified as if the image were of class double in the range [0, 1]. If the input image is of class uint8 or uint16, the imnoise function converts the image to double, adds noise according to the specified type and parameters, and then converts the noisy image back to the same class as the input.
Class Support
I can be of class uint8, uint16, int16, single, or double. The output image J is of the same class as I. If I has more than two dimensions it is treated as a multidimensional intensity image and not as an RGB image.
Example
I = imread('eight.tif');
J = imnoise(I,'salt & pepper',0.02);
imshow(I)
figure, imshow(J)
%Example
I = imread('eight.tif');
J = imnoise(I,'salt & pepper',0.02);
imshow(I)
figure, imshow(J)
%is this creation
%Example
I = imread('eight.tif');
J = imnoise(I,'salt & pepper',0.02);
imshow(I)
figure, imshow(J)
%'gaussian' %Gaussian white noise with constant mean and variance
%'localvar' %Zero-mean Gaussian white noise with an intensity-dependent variance
%'poisson' %Poisson noise
%'salt & pepper %On and off pixels
%'speckle' %Multiplicative noise.
I = imread('eight.tif');
>> plot(I) %image is observed;
%is this creation on noise 27 march 09.
%I %1 original numerical data
%plot(I) %2 graph form of original data
%imshow(I) %3 picture form of original data
%figure, imshow(I) %4 ******
%J %1n noised numerical data
%plot(J) %2n graph form of noised data
%imshow(J) %3n picture form of noised data
%figure, imshow(J) %4n ******
%Example
I = imread('eight.tif'); %original image,coin
J = imnoise(I,'salt & pepper',0.02); %adding noise
w=imread(‘YellowHlaMyintWin0012’) %Hla myint win
??? w=imread(‘YellowHlaMyintWin0012’) %Hla myint win
|
Error: The input character is not valid in MATLAB statements or expressions.
%I %1 original numerical data
%plot(I) %2 graph form of original data
%imshow(I) %3 picture form of original data
%figure, imshow(I) %4 ******
%J %1n noised numerical data
%plot(J) %2n graph form of noised data
%imshow(J) %3n picture form of noised data
%figure, imshow(J) %4n ******
Friday, March 27, 2009
%colon operator creation 27 march 09.
A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1];
1:10;
%is a row vector containing the integers from 1 to 10:
%1 2 3 4 5 6 7 8 9 10
%To obtain nonunit spacing, specify an increment. For example,
100:-7:50
%is
%100 93 86 79 72 65 58 51
%and
%0:pi/4:pi
%is
%0 0.7854 1.5708 2.3562 3.1416
%Subscript expressions involving colons refer to portions of a matrix:
%A(1:k,j)
%is the first k elements of the jth column of A. So
%sum(A(1:4,4))
%computes the sum of the fourth column. But there is a better way. The colon by itself refers to all the elements in a row or column of a matrix and the keyword end refers to the last row or column. So
sum(A(:,end))
%computes the sum of the elements in the last column of A
To make this B into Dürer's A, swap the two middle columns:
A = B(:,[1 3 2 4]) %swapping colon 2 & 3 of B
3/27/2009 1:50 PM
N = fix(10*rand(1,10))
N =
9 2 6 4 8 7 4 0 8 4
Generating Matrices
MATLAB provides four functions that generate basic matrices.
Zeros All zeros
Ones All ones
Rand Uniformly distributed random elements
randn Normally distributed random elements.
Here are some examples:
Z = zeros(2,4)
Z =
0 0 0 0
0 0 0 0
F = 5*ones(3,3)
F =
5 5 5
5 5 5
5 5 5
N = fix(10*rand(1,10))
N =
9 2 6 4 8 7 4 0 8 4
R = randn(4,4)
R =
0.6353 0.0860 -0.3210 -1.2316
-0.6014 -2.0046 1.2366 1.0556
0.5512 -0.4931 -0.6313 -0.1132
-1.0998 0.4620 -2.3252 0.3792
3/27/2009 2:26 PM
Getting Started
The load Function (NOT successful)
The load function reads binary files containing matrices generated by earlier MATLAB sessions, or reads text files containing numeric data. The text file should be organized as a rectangular table of numbers, separated by blanks, with one row per line, and an equal number of elements in each row. For example, outside of MATLAB, create a text file containing these four lines:
16.0 3.0 2.0 13.0
5.0 10.0 11.0 8.0
9.0 6.0 7.0 12.0
4.0 15.0 14.0 1.0
Store the file under the name magik.dat. Then the statement
load magik.dat
reads the file and creates a variable, magik, containing our example matrix.
An easy way to read data into MATLAB in many text or binary formats is to use Import Wizard.
Load function not successful
3/27/2009 3:42 PM
Getting Started
More About Matrices and Arrays
This section shows you more about working with matrices and arrays, focusing on Linear Algebra Arrays Multivariate Data Scalar Expansion Logical Subscripting The find Function
Linear Algebra
Informally, the terms matrix and array are often used interchangeably. More precisely, a matrix is a two-dimensional numeric array that represents a linear transformation. The mathematical operations defined on matrices are the subject of linear algebra. Dürer's magic square
A = [16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1 ]
provides several examples that give a taste of MATLAB matrix operations. You have already seen the matrix transpose, A'. Adding a matrix to its transpose produces a symmetric matrix:
A + A'
ans =
32 8 11 17
8 20 17 23
11 17 14 26
17 23 26 2
The multiplication symbol, *, denotes the matrix multiplication involving inner products between rows and columns. Multiplying the transpose of a matrix by the original matrix also produces a symmetric matrix:
A'*A
ans =
378 212 206 360
212 370 368 206
206 368 370 212
360 206 212 378
The determinant of this particular matrix happens to be zero, indicating that the matrix is singular:
d = det(A)
d =
0
The reduced row echelon form of A is not the identity:
R = rref(A)
R =
1 0 0 1
0 1 0 -3
0 0 1 3
0 0 0 0
Since the matrix is singular, it does not have an inverse. If you try to compute the inverse with
X = inv(A)
you will get a warning message:
Warning: Matrix is close to singular or badly scaled.
Results may be inaccurate. RCOND = 9.796086e-018.
Roundoff error has prevented the matrix inversion algorithm from detecting exact singularity. But the value of rcond, which stands for reciprocal condition estimate, is on the order of eps, the floating-point relative precision, so the computed inverse is unlikely to be of much use.
The eigenvalues of the magic square are interesting:
e = eig(A)
e =
34.0000
8.0000
0.0000
-8.0000
One of the eigenvalues is zero, which is another consequence of singularity. The largest eigenvalue is 34, the magic sum. That is because the vector of all ones is an eigenvector:
v = ones(4,1)
v =
1
1
1
1
A*v
ans =
34
34
34
34
When a magic square is scaled by its magic sum,
P = A/34
the result is a doubly stochastic matrix whose row and column sums are all 1:
P =
0.4706 0.0882 0.0588 0.3824
0.1471 0.2941 0.3235 0.2353
0.2647 0.1765 0.2059 0.3529
0.1176 0.4412 0.4118 0.0294
Such matrices represent the transition probabilities in a Markov process. Repeated powers of the matrix represent repeated steps of the process. For our example, the fifth power
P^5
is
0.2507 0.2495 0.2494 0.2504
0.2497 0.2501 0.2502 0.2500
0.2500 0.2498 0.2499 0.2503
0.2496 0.2506 0.2505 0.2493
This shows that as k approaches infinity, all the elements in the kth power,p^k , approach 1/4.
Finally, the coefficients in the characteristic polynomial
poly(A)
are
1 -34 -64 2176 0
This indicates that the characteristic polynomial
*******************************************
The constant term is zero, because the matrix is singular, and the coefficient of the cubic term is -34, because the matrix is magic!.
Arrays
When they are taken away from the world of linear algebra, matrices become two-dimensional numeric arrays. Arithmetic operations on arrays are done element by element. This means that addition and subtraction are the same for arrays and matrices, but that multiplicative operations are different. MATLAB uses a dot, or decimal point, as part of the notation for multiplicative array operations.
The list of operators includes
+ Addition
- Subtraction
.* Element-by-element multiplication
./ Element-by-element division.
\ Element-by-element left division
.^ Element-by-element power
.' Unconjugated array transpose
A = [16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1 ] % Dürer magic square
If the Dürer magic square is multiplied by itself with array multiplication
A.*A
the result is an array containing the squares of the integers from 1 to 16, in an unusual order:
ans =
256 9 4 169
25 100 121 64
81 36 49 144
16 225 196 1
Building Tables
Array operations are useful for building tables. Suppose n is the column vector
n = (0:9)';
Then
pows = [n n.^2 2.^n]
builds a table of squares and powers of 2:
pows =
0 0 1
1 1 2
2 4 4
3 9 8
4 16 16
5 25 32
6 36 64
7 49 128
8 64 256
9 81 512
he elementary math functions operate on arrays element by element. So
format short g
x = (1:0.1:2)';
logs = [x log10(x)]
builds a table of logarithms.
logs =
1.0 0
1.1 0.04139
1.2 0.07918
1.3 0.11394
1.4 0.14613
1.5 0.17609
1.6 0.20412
1.7 0.23045
1.8 0.25527
1.9 0.27875
2.0 0.30103
3/27/2009 9:29 PM
To contd. Getting started-matrices and arrays-more about matrices-arrays
6/12/2009 3:42 AM
Multivariate Data
MATLAB uses column-oriented analysis for multivariate statistical data. Each column in a data set represents a variable and each row an observation. The (i,j)th element is the ith observation of the jth variable.
As an example, consider a data set with three variables:
Heart rate
Weight
Hours of exercise per week
For five observations, the resulting array might look like
D = [ 72 134 3.2
81 201 3.5
69 156 7.1
82 148 2.4
75 170 1.2 ]
The first row contains the heart rate, weight, and exercise hours for patient 1, the second row contains the data for patient 2, and so on.
Now you can apply many MATLAB data analysis functions to this data set. For example, to obtain the mean and standard deviation of each column, use
mu = mean(D), sigma = std(D)
mu =
75.8 161.8 3.48
sigma =
5.6303 25.499 2.2107
For a list of the data analysis functions available in MATLAB, type
help datafun
If you have access to the Statistics Toolbox, type
help stats
6/12/2009 3:42 AM
Scalar Expansion
Matrices and scalars can be combined in several different ways. For example, a scalar is subtracted from a matrix by subtracting it from each element. The average value of the elements in our magic square is 8.5, so
B = A - 8.5
forms a matrix whose column sums are zero:
B =
7.5 -5.5 -6.5 4.5
-3.5 1.5 2.5 -0.5
0.5 -2.5 -1.5 3.5
-4.5 6.5 5.5 -7.5
sum(B) % column sums
ans =
0 0 0 0
With scalar expansion, MATLAB assigns a specified scalar to all indices in a range. For example,
B(1:2,2:3) = 0 % elements in 1st row. & 2nd row. AND 2nd column & 3rd column
zeroes out a portion of B:
B =
7.5 0 0 4.5
-3.5 0 0 -0.5
0.5 -2.5 -1.5 3.5
-4.5 6.5 5.5 -7.5
B(1:2,2:3)=16; % elements in 1st row. & 2nd row. AND 2nd column
& 3rd column
>> B
B =
7.5000 16.0000 16.0000 4.5000
-3.5000 16.0000 16.0000 -0.5000
0.5000 -2.5000 -1.5000 3.5000
-4.5000 6.5000 5.5000 -7.5000
Amen amen amen6/12/2009 3:42 AM
Controlling Command Window Input and Output
So far, you have been using the MATLAB command line, typing functions and expressions, and seeing the results printed in the Command Window. This section describes The format Function Suppressing Output Entering Long Statements Command Line Editing
The format Function
The format function controls the numeric format of the values displayed by MATLAB. The function affects only how numbers are displayed, not how MATLAB computes or saves them. Here are the different formats, together with the resulting output produced from a vector x with components of different magnitudes.
Note To ensure proper spacing, use a fixed-width font, such as Courier.
x = [4/3 1.2345e-6]
format short
1.3333 0.0000
format short e
1.3333e+000 1.2345e-006
format short g
1.3333 1.2345e-006
format long
1.33333333333333 0.00000123450000
format long e
1.333333333333333e+000 1.234500000000000e-006
format long g
1.33333333333333 1.2345e-006
format bank
1.33 0.00
format rat
4/3 1/810045
format hex
3ff5555555555555 3eb4b6231abfd271
If the largest element of a matrix is larger than 103 or smaller than 10-3, MATLAB applies a common scale factor for the short and long formats.
In addition to the format functions shown above
format compact
suppresses many of the blank lines that appear in the output. This lets you view more information on a screen or window. If you want more control over the output format, use the sprintf and fprintf functions.
Amen amen amen3/28/2009 8:35 AM
Next---suppressing output
Sunday, March 29, 2009
double-click
vb. To press and release a mouse button twice without moving the mouse. Double-clicking is a means of rapidly selecting and activating a program or program feature. Compare click, drag.
Suppressing Output
If you simply type a statement and press Return or Enter, MATLAB automatically displays the results on screen. However, if you end the line with a semicolon, MATLAB performs the computation but does not display any output. This is particularly useful when you generate large matrices. For example,
A = magic(100);
Entering Long Statements
If a statement does not fit on one line, use an ellipsis (three periods), ..., followed by Return or Enter to indicate that the statement continues on the next line. For example,
s = 1 -1/2 + 1/3 -1/4 + 1/5 - 1/6 + 1/7 ...
- 1/8 + 1/9 - 1/10 + 1/11 - 1/12;
Blank spaces around the =, +, and - signs are optional, but they improve readability.
Sunday, March 29, 2009
Examples -- Using MATLAB Plotting Tools
Suppose you want to graph the function y = x3 over the x domain -1 to 1. The first step is to generate the data to plot.
It is simple to evaluate a function because MATLAB can distribute arithmetic operations over all elements of a multivalued variable.
For example, the following statement creates a variable x that contains values ranging from -1 to 1 in increments of 0.1 (you could also use the linspace function to generate data for x). The second statement raises each value in x to the third power and stores these values in y:
x = -1:.1:1; % Define the range of x
y = x.^3; % Raise each element in x to the third power
Now that you have generated some data, you can plot it using the MATLAB plotting tools. To start the plotting tools, type
plottools
MATLAB displays a figure with plotting tools attached
Plotting Two Variables
A simple line graph is a suitable way to display x as the independent variable and y as the dependent variable. To do this, select both variables (click to select, then Shift-click to select again), then right-click to display the context menu.
Select plot(x,y) from the menu. MATLAB creates the line graph in the figure area. The black squares indicate that the line is selected and you can edit its properties with the Property Editor.
To contd.-Getting started-GRAPHICS
3/30/2009 3:12 AM
Case Study: Curve Fitting
This section provides an overview of some of the MATLAB basic data analysis capabilities in the form of a case study. The examples that follow work with a collection of census data, using MATLAB functions to experiment with fitting curves to the data: Polynomial fit
Analyzing residuals
Exponential fit Error bounds
This section also tells you how to use the Basic Fitting interface to perform curve fitting tasks.
Loading the Data
The file census.mat contains U.S. population data for the years 1790 through 1990. Load it into MATLAB:
load census
Your workspace now contains two new variables, cdate and pop: cdate is a column vector containing the years from 1790 to 1990 in increments of 10. pop is a column vector with the U.S. population figures that correspond to the years in cdate.
Polynomial Fit
A first try in fitting the census data might be a simple polynomial fit. Two MATLAB functions help with this process.
Curve Fitting Function Summary
Function Description
Polyfit Polynomial curve fit.
Polyval Evaluation of polynomial fit.
The MATLAB polyfit function generates a "best fit" polynomial (in the least squares sense) of a specified order for a given set of data. For a polynomial fit of the fourth-order
p = polyfit(cdate,pop,4)
Warning: Polynomial is badly conditioned. Remove repeated data
points or try centering and scaling as described in HELP POLYFIT.
p =
1.0e+05 *
0.0000 -0.0000 0.0000 -0.0126 6.0020
The warning arises because the polyfit function uses the cdate values as the basis for a matrix with very large values (it creates a Vandermonde matrix in its calculations - see the polyfit M-file for details). The spread of the cdate values results in scaling problems. One way to deal with this is to normalize the cdate data.
Preprocessing: Normalizing the Data
Normalization is a process of scaling the numbers in a data set to improve the accuracy of the subsequent numeric computations. A way to normalize cdate is to center it at zero mean and scale it to unit standard deviation:
sdate = (cdate - mean(cdate))./std(cdate)
Now try the fourth-degree polynomial model using the normalized data:
p = polyfit(sdate,pop,4)
p =
0.7047 0.9210 23.4706 73.8598 62.2285
Evaluate the fitted polynomial at the normalized year values, and plot the fit against the observed data points:
pop4 = polyval(p,sdate);
plot(cdate,pop4,'-',cdate,pop,'+'), grid on
amen amen amen3/30/2009 4:25 AM
Analyzing Residuals
A measure of the "goodness" of fit is the residual, the difference between the observed and predicted data. Compare the residuals for the various fits, using normalized cdate values. It's evident from studying the fit plots and residuals that it should be possible to do better than a simple polynomial fit with this data set.
%fit
p1 = polyfit(sdate,pop,1);
pop1 = polyval(p1,sdate);
plot(cdate,pop1,'b-',cdate,pop,'g+')
%residuals
res1 = pop - pop1;
figure, plot(cdate,res1,'g+')
%fit
p = polyfit(sdate,pop,4);
pop4 = polyval(p,sdate);
plot(cdate,pop4,'b-',cdate,pop,'g+');
%residual
res4 = pop - pop4;
figure, plot(cdate,res4,'g+');
amen amen amen 3/30/2009 4:48 AM
Sunday, April 05, 2009 2:46 PM
ommunications Toolbox
Convolution
This toolbox offers two equivalent ways to convolve a pair of Galois vectors:
Use the conv function, as described in Multiplication and Division of Polynomials. This works because convolving two vectors is equivalent to multiplying the two polynomials whose coefficients are the entries of the vectors.
Use the convmtx function to compute the convolution matrix of one of the vectors, and then multiply that matrix by the other vector. This works because convolving two vectors is equivalent to filtering one of the vectors by the other. The equivalence permits the representation of a digital filter as a convolution matrix, which you can then multiply by any Galois vector of appropriate length.
Tip If you need to convolve large Galois vectors, then multiplying by the convolution matrix might be faster than using conv.
Example
The example below computes the convolution matrix for a vector b in GF(4), representing the numerator coefficients for a digital filter. It then illustrates the two equivalent ways to convolve b with x over the Galois field.
m = 2; b = gf([1 2 3]',m);
n = 3; x = gf(randint(n,1,2^m),m);
C = convmtx(b,n); % Compute convolution matrix.
v1 = conv(b,x); % Use conv to convolve b with x
v2 = C*x; % Use C to convolve b with x.
%outputs
>> v1
v1 = GF(2^2) array. Primitive polynomial = D^2+D+1 (7 decimal)
Array elements =
3
1
0
3
1
>> v2
v2 = GF(2^2) array. Primitive polynomial = D^2+D+1 (7 decimal)
Array elements =
3
1
0
3
1
%very good v1=v2 QED
Amen amen amen Sunday, April 05, 2009
ATLAB Function Reference
Polynomials
Conv Convolution and polynomial multiplication
Deconv Deconvolution and polynomial division
Poly Polynomial with specified roots
Polyder Polynomial derivative
Polyeig Polynomial eigenvalue problem
Polyfit Polynomial curve fitting
Polyint Analytic polynomial integration
Polyval Polynomial evaluation
Polyvalm Matrix polynomial evaluation
Residue Convert between partial fraction expansion and polynomial coefficients
Roots Polynomial roots
Data Analysis and Fourier Transforms Interpolation and Computational Geometry
%polyder
%Polynomial derivative
%5 april 09
Syntax
k = polyder(p)
k = polyder(a,b)
[q,d] = polyder(b,a)
Description
%The polyder function calculates the derivative of polynomials,
%polynomial products, and polynomial quotients.
%The operands a, b, and p are vectors whose elements are the coefficients
%of a polynomial in descending powers.
k = polyder(p) returns the derivative of the polynomial p.
k = polyder(a,b) returns the derivative of the product of the polynomials a and b.
[q,d] = polyder(b,a) returns the numerator q and denominator d of the derivative
of the polynomial quotient b/a.
Examples
%The derivative of the product is obtained with
a = [3 6 9];
b = [1 2 0];
k = polyder(a,b)
k =
12 36 42 18
products
>> %The derivative of the product is obtained with
a = [3 6 9];
b = [1 2 0];
k = polyder(a,b)
k =
12 36 42 18
>>
Monday, April 06, 2009
mage Processing Toolbox User's Guide
Convolution
Linear filtering of an image is accomplished through an operation called convolution. Convolution is a neighborhood operation in which each output pixel is the weighted sum of neighboring input pixels. The matrix of weights is called the convolution kernel, also known as the filter. A convolution kernel is a correlation kernel that has been rotated 180 degrees.
For example, suppose the image is
A = [17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9]
and the convolution kernel is
h = [8 1 6
3 5 7
4 9 2]
Amen
%Mathematics
%Convolution and Deconvolution
%3 april 09
%Polynomial multiplication and division correspond to
%the operations convolution and deconvolution.
%The functions conv and deconv implement these operations.
%Consider the polynomials -- and--- . To compute their product,
a = [1 2 3]; b = [4 5 6];
c = conv(a,b)
%c =
% 4 13 28 27 18
%Use deconvolution to divide back out of the product.
%[q,r] = deconv(c,a)
%q =
% 4 5 6
%r =
% 0 0 0 0 0
% outputs
To get started, select MATLAB Help or Demos from the Help menu.
>> %Consider the polynomials -- and--- . To compute their product,
a = [1 2 3]; b = [4 5 6];
c = conv(a,b)
%c =
% 4 13 28 27 18
%Use deconvolution to divide back out of the product.
%[q,r] = deconv(c,a)
c =
4 13 28 27 18
>> [q,r] = deconv(c,a)
q =
4 5 6
r =
0 0 0 0 0
Monday, April 06, 2009 3:40 PM
Image Processing Toolbox User's Guide
Linear Filtering and Filter Design
The Image Processing Toolbox provides a number of functions for designing and implementing two-dimensional linear filters for image data. This chapter describes these functions and how to use them effectively. Linear FilteringProvides an explanation of linear filtering and how it is implemented in the toolbox. This topic describes filtering in terms of the spatial domain, and is accessible to anyone doing image processing.Filter DesignDiscusses designing two-dimensional finite impulse response (FIR) filters. This section assumes you are familiar with working in the frequency domain.
Image Processing Toolbox User's Guide
Linear Filtering
Filtering is a technique for modifying or enhancing an image. For example, you can filter an image to emphasize certain features or remove other features. Image processing operations implemented with filtering include smoothing, sharpening, and edge enhancement.
Filtering is a neighborhood operation, in which the value of any given pixel in the output image is determined by applying some algorithm to the values of the pixels in the neighborhood of the corresponding input pixel. A pixel's neighborhood is some set of pixels, defined by their locations relative to that pixel. (See Neighborhood and Block Operations, for a general discussion of neighborhood operations.)
Linear filtering is filtering in which the value of an output pixel is a linear combination of the values of the pixels in the input pixel's neighborhood.
This section discusses linear filtering in MATLAB and the Image Processing Toolbox. It includes
A description of filtering, using convolution and correlation
A description of how to perform filtering using the imfilter function
A discussion about using predefined filter types
See Filter Design for information about how to design filters.
Amen amen amen
Correlation
The operation called correlation is closely related to convolution. In correlation, the value of an output pixel is also computed as a weighted sum of neighboring pixels. The difference is that the matrix of weights, in this case called the correlation kernel, is not rotated during the computation. The filter design functions in the Image Processing Toolbox return correlation kernels.
The following figure shows how to compute the (2,4) output pixel of the correlation of A, assuming h is a correlation kernel instead of a convolution kernel, using these steps: Slide the center element of the correlation kernel so that lies on top of the (2,4) element of A. Multiply each weight in the correlation kernel by the pixel of A underneath. Sum the individual products from step 3.
Fig $$$$$$$$$$$$$$$$$$$$$$$$$$$$$
amen
Wednesday, April 08, 2009
Wednesday, April 15, 2009
Multiplication and Division of Polynomials
To multiply and divide polynomials, use conv and deconv on Galois vectors that represent the polynomials. Multiplication and division of polynomials is equivalent to convolution and deconvolution of vectors. The deconv function returns the quotient of the two polynomials as well as the remainder polynomial. Examples are below.
m = 4;
apoly = gf([4 5 3],m); % A^2 x^2 + (A^2 + 1) x + (A + 1)
bpoly = gf([1 1],m); % x + 1
xpoly = gf([1 0],m); % x
% Product is A^2 x^3 + x^2 + (A^2 + A) x + (A + 1).
cpoly = conv(apoly,bpoly);
[a2,remd] = deconv(cpoly,bpoly); % a2==apoly. remd is zero.
[otherpol,remd2] = deconv(cpoly,xpoly); % remd is nonzero.
The multiplication and division operators in Arithmetic in Galois Fields multiply elements or matrices, not polynomial.
%Multiplication and Division of Polynomials
%15 APRIL 2009
%eureka eureka
%To multiply and divide polynomials, use conv and deconv on Galois vectors that represent
%the polynomials.
%Multiplication and division of polynomials is equivalent to convolution and deconvolution
% of vectors.
% The deconv function returns the quotient of the two polynomials as well as
%the remainder polynomial. Examples are below.
m = 4;
apoly = gf([4 5 3],m); % A^2 x^2 + (A^2 + 1) x + (A + 1)
bpoly = gf([1 1],m); % x + 1
xpoly = gf([1 0],m); % x
% Product is A^2 x^3 + x^2 + (A^2 + A) x + (A + 1).
cpoly = conv(apoly,bpoly);
[a2,remd] = deconv(cpoly,bpoly); % a2==apoly. remd is zero.
[otherpol,remd2] = deconv(cpoly,xpoly); % remd is nonzero.
%outputs
m = 4;
apoly = gf([4 5 3],m); % A^2 x^2 + (A^2 + 1) x + (A + 1)
bpoly = gf([1 1],m); % x + 1
xpoly = gf([1 0],m); % x
% Product is A^2 x^3 + x^2 + (A^2 + A) x + (A + 1).
cpoly = conv(apoly,bpoly);
[a2,remd] = deconv(cpoly,bpoly); % a2==apoly. remd is zero.
[otherpol,remd2] = deconv(cpoly,xpoly); % remd is n
apoly
apoly = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
4 5 3
bpoly
bpoly = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
1 1
cpoly
cpoly = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
4 1 6 3
otherpol
otherpol = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
4 1 6
apoly = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
4 5 3
bpoly
bpoly = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
1 1
otherpol
otherpol = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
4 1 6
conv(otherpol,bpoly)
ans = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
4 5 7 6
cpoly
cpoly = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
4 1 6 3
conv(apoly,bpoly)
ans = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
4 1 6 3
Wednesday, April 15, 2009
%computes the convolution matrix for a vector b in GF(4),
%representing the numerator coefficients for a digital filter.
%15 april 2009
%Example
%The example below computes the convolution matrix for a vector b in GF(4),
%representing the numerator coefficients for a digital filter.
%It then illustrates the two equivalent ways to convolve b with x over the Galois field.
m = 2; b = gf([1 2 3]',m);
n = 3; x = gf(randint(n,1,2^m),m);
C = convmtx(b,n); % Compute convolution matrix.
v1 = conv(b,x); % Use conv to convolve b with x
v2 = C*x; % Use C to convolve b with x.
%outputs
To get started, select MATLAB Help or Demos from the Help menu.
>> m = 2; b = gf([1 2 3]',m);
n = 3; x = gf(randint(n,1,2^m),m);
C = convmtx(b,n); % Compute convolution matrix.
v1 = conv(b,x); % Use conv to convolve b with x
v2 = C*x; % Use C to convolve b with x.
>> b
b = GF(2^2) array. Primitive polynomial = D^2+D+1 (7 decimal)
Array elements =
1
2
3
>> v1
v1 = GF(2^2) array. Primitive polynomial = D^2+D+1 (7 decimal)
Array elements =
3
1
0
3
1
>> v2
v2 = GF(2^2) array. Primitive polynomial = D^2+D+1 (7 decimal)
Array elements =
3
1
0
3
1
>> b
b = GF(2^2) array. Primitive polynomial = D^2+D+1 (7 decimal)
Array elements =
1
2
3
>>
Wednesday, April 15, 2009
Discrete Fourier Transform
The discrete Fourier transform is an important tool in digital signal processing. This toolbox offers these tools to help you process discrete Fourier transforms:
%fft, which transforms a Galois vector
%ifft, which inverts the discrete Fourier transform on a Galois vector
%dftmtx, which returns a Galois array that you can use to perform or invert the discrete Fourier transform on a Galois vector
%In all cases, the vector being transformed must be a Galois vector of length 2m-1 in the field %GF(2m). The examples below illustrate the use of these functions. You can check, using the %isequal function, that y equals y1, z equals z1, and z equals x.
m = 4;
x = gf(randint(2^m-1,1,2^m),m); % A vector to transform
alph = gf(2,m);
dm = dftmtx(alph);
idm = dftmtx(1/alph);
y = dm*x; % Transform x using the result of dftmtx.
y1 = fft(x); % Transform x using fft.
z = idm*y; % Recover x using the result of dftmtx(1/alph).
z1 = ifft(y1); % Recover x using ifft.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %outputs
>> x
x = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal) %signal
Array elements =
7
14
12
7
0
13
7
9
12
14
11
2
6
14
14
>> length(x) %length of signal
ans =
15
>> dm
m = 4;
x = gf(randint(2^m-1,1,2^m),m); % A vector to transform
alph = gf(2,m);
dm = dftmtx(alph);
idm = dftmtx(1/alph);
y = dm*x; % Transform x using the result of dftmtx.
y1 = fft(x); % Transform x using fft.
z = idm*y; % Recover x using the result of dftmtx(1/alph).
z1 = ifft(y1); % Recover x using ifft.
dm = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
Columns 1 through 8
1 1 1 1 1 1 1 1
1 2 4 8 3 6 12 11
1 4 3 12 5 7 15 9
1 8 12 10 15 1 8 12
1 3 5 15 2 6 10 13
1 6 7 1 6 7 1 6
1 12 15 8 10 1 12 15
1 11 9 12 13 6 15 3
1 5 2 10 4 7 8 14
1 10 8 15 12 1 10 8
1 7 6 1 7 6 1 7
1 14 11 8 9 7 12 4
1 15 10 12 8 1 15 10
1 13 14 10 11 6 8 2
1 9 13 15 14 7 10 5
Columns 9 through 15
1 1 1 1 1 1 1
5 10 7 14 15 13 9
2 8 6 11 10 14 13
10 15 1 8 12 10 15
4 12 7 9 8 11 14
7 1 6 7 1 6 7
8 10 1 12 15 8 10
14 8 7 4 10 2 5
3 15 6 13 12 9 11
15 12 1 10 8 15 12
6 1 7 6 1 7 6
13 10 6 2 15 5 3
12 8 1 15 10 12 8
9 15 7 5 12 3 4
11 12 6 3 8 4 2
>> idm = dftmtx(1/alph);
>> idm = dftmtx(1/alph)
idm = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
Columns 1 through 8
1 1 1 1 1 1 1 1
1 9 13 15 14 7 10 5
1 13 14 10 11 6 8 2
1 15 10 12 8 1 15 10
1 14 11 8 9 7 12 4
1 7 6 1 7 6 1 7
1 10 8 15 12 1 10 8
1 5 2 10 4 7 8 14
1 11 9 12 13 6 15 3
1 12 15 8 10 1 12 15
1 6 7 1 6 7 1 6
1 3 5 15 2 6 10 13
1 8 12 10 15 1 8 12
1 4 3 12 5 7 15 9
1 2 4 8 3 6 12 11
Columns 9 through 15
1 1 1 1 1 1 1
11 12 6 3 8 4 2
9 15 7 5 12 3 4
12 8 1 15 10 12 8
13 10 6 2 15 5 3
6 1 7 6 1 7 6
15 12 1 10 8 15 12
3 15 6 13 12 9 11
14 8 7 4 10 2 5
8 10 1 12 15 8 10
7 1 6 7 1 6 7
4 12 7 9 8 11 14
10 15 1 8 12 10 15
2 8 6 11 10 14 13
5 10 7 14 15 13 9
>> y
y = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
12
11
12
5
2
4
0
4
6
14
7
1
6
1
2
>> y1
y1 = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
12
11
12
5
2
4
0
4
6
14
7
1
6
1
2
>> y-y1 %difference between "F. transforms by 2 different methods.
ans = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
>> z
z = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
7
14
12
7
0
13
7
9
12
14
11
2
6
14
14
>> z1
z1 = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
7
14
12
7
0
13
7
9
12
14
11
2
6
14
14
>> z-z1 %difference between original signals by 2 diff. methods.
ans = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
>> amen amen amen
Wednesday, April 15, 2009
Polynomials over Galois Fields
You can use Galois vectors to represent polynomials in an indeterminate quantity x, with coefficients in a Galois field. Form the representation by listing the coefficients of the polynomial in a vector in order of descending powers of x. For example, the vector
gf([2 1 0 3],4)
represents the polynomial Ax3 + 1x2 + 0x + (A+1), where
A is a primitive element in the field GF(24).
x is the indeterminate quantity in the polynomial.
You can then use such a Galois vector to perform arithmetic with, evaluate, and find roots of polynomials. You can also find minimal polynomials of elements of a Galois field.
Wednesday, April 15, 2009
Communications Toolbox
Addition and Subtraction of Polynomials
To add and subtract polynomials, use + and - on equal-length Galois vectors that represent the polynomials. If one polynomial has lower degree than the other, then you must pad the shorter vector with zeros at the beginning so that the two vectors have the same length.
The example below shows how to add a degree-one and a degree-two polynomial.
lin = gf([4 2],3); % A^2 x + A, which is linear in x
linpadded = gf([0 4 2],3); % The same polynomial, zero-padded
quadr = gf([1 4 2],3); % x^2 + A^2 x + A, which is quadratic in x
% Can't do lin + quadr because they have different vector lengths.
sumpoly = [0, lin] + quadr; % Sum of the two polynomials
sumpoly2 = linpadded + quadr; % The same sum.
%outputs
>> lin
lin = GF(2^3) array. Primitive polynomial = D^3+D+1 (11 decimal)
Array elements =
4 2
>> linpadded
linpadded = GF(2^3) array. Primitive polynomial = D^3+D+1 (11 decimal)
Array elements =
0 4 2
>> quadr
quadr = GF(2^3) array. Primitive polynomial = D^3+D+1 (11 decimal)
Array elements =
1 4 2
>> sumpoly
sumpoly = GF(2^3) array. Primitive polynomial = D^3+D+1 (11 decimal)
Array elements =
1 0 0
>> sumpoly2
sumpoly2 = GF(2^3) array. Primitive polynomial = D^3+D+1 (11 decimal)
Array elements =
1 0 0
Wednesday, April 15, 2009
Simulink
Polynomial
Perform evaluation of polynomial coefficients on input values
Library
Math Operations
Description
block
The Polynomial block uses a coefficients parameter to evaluate a real polynomial for the input value.
You define a set of polynomial coefficients in the form accepted by the MATLAB polyval command. The block then calculates P(u) at each time step for the input u. Inputs and coefficients must be real.
Data Type Support
The Polynomial block accepts real signals of types double or single. The Polynomial coefficients parameter must be of the same type as the inputs. The output data type is set to the input data type.
polynomial coefficients
Values are in coefficients of a polynomial in MATLAB polyval form, with the first coefficient representing x^N, then decreasing in order until the last coefficient, which represents the constant for the polynomial. See polyval in the MATLAB documentation for more information.
Wednesday, April 15, 2009
%Communications Toolbox
%Evaluating Polynomials
%15 april 2009
%sayanyein
%To evaluate a polynomial at an element of a Galois field, use polyval.
%It behaves like the ordinary MATLAB polyval function when given exactly two input arguments. The example below evaluates a polynomial at several elements in a field and checks the results using .^ and .* in the field.
m = 4;
apoly = gf([4 5 3],m); % A^2 x^2 + (A^2 + 1) x + (A + 1)
x0 = gf([0 1 2],m); % Points at which to evaluate the polynomial
y = polyval(apoly,x0);
a = gf(2,m); % Primitive element of the field, corresponding to A.
y2 = a.^2.*x0.^2 + (a.^2+1).*x0 + (a+1) ;% Check the result.
%The output is below.
%y = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
%Array elements =
% 3 2 10
%y2 = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
%Array elements =
% 3 2 10
%The first element of y evaluates the polynomial at 0 and, therefore,
%returns the polynomial's constant term of 3
%outputs
To get started, select MATLAB Help or Demos from the Help menu.
a = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)
Array elements =
2
y
y = GF(2^4) array. Primitive polynomial = D
%1 nov. 2009
No comments:
Post a Comment