===========================Octave-Templates==================
3D:Noise GDSII:Read Print:Formats
AIFF:Read Get:Set:Clear Read:Write:files
Arithmetic:Operators Global:variables Read:Write:Matrixes
AudioPict:Read:Write INPUT:Prompt STATE:SPACE:FORM
Complex:Functions Integral:Evaluating STEP:IMPULSE
Control:Loops MATH:Functions STRING:Functions
Enter:Values Matrix:Solve Statistic:Functions
Factoring:Polynomials Matrixes:How2 Structure:Format
FFT:Functions MISC:Functions TF:to:PZ
File:Read Number:Translate Transfer:Function
FORMAT:TRANSLATE PLOT:Functions Trigonometry:Functions
FSOLVE:Functions Pole:Zero Unit:Conversion
Functions:of:functions Poly:Deriv:integrate Vector:Functions
===========================Enter:Values===============================
z = 1
thing = "food"
predicate = "good"
message = [ "this " , thing , " is " , predicate ]
% message = this food is good
foo = 1 % foo = 1
foo = "bar" % foo = bar
A = rand(4,5)
% 0.128403 0.691702 0.327433 0.315387 0.501449
% 0.858987 0.881478 0.599393 0.746367 0.715950
% 0.412631 0.688264 0.012783 0.266997 0.572204
% 0.650368 0.463052 0.655021 0.105027 0.850679
A (3, :) = [] % delete 3rd row
% 0.12840 0.69170 0.32743 0.31539 0.50145
% 0.85899 0.88148 0.59939 0.74637 0.71595
% 0.65037 0.46305 0.65502 0.10503 0.85068
A (:, 1:2:5) = [] % deletes the first, third, and fifth columns.
% 0.69170 0.31539
% 0.88148 0.74637
% 0.46305 0.10503
A = [ 1, 1, 2;
3, 5, 8;
13, 21, 34]
% 1 1 2
% 3 5 8
% 13 21 34
x0 = [1; 2];
% 1
% 2
A = [ 1:3; 4:6; 7:9 ; 7:9 ]
% 1 2 3
% 4 5 6
% 7 8 9
a = ["hello"; "world"];
% hello
% world
t = linspace (0, 50, 4)' # set of output times as column vector
% 0.00000
% 16.66667
% 33.33333
% 50.00000
C = 1 : 0.25 : 2 % C = 1.0000 1.2500 1.5000 1.7500 2.0000
D = 0 : 4 % D = 0 1 2 3 4
E = linspace(0,5,3) # E = 0.00000 2.50000 5.00000
F = logspace(1,3,5) # F = 10.000 31.623 100.000 316.228 1000.000
rang = 1 : 5 # rang = 1 2 3 4 5
rang = 1 : 3 : 5 # rang = 1 4
[1, 2; 3, 4] == [1, 3; 2, 4]
% 1 0
% 0 1
===========================Matrixes:How2===============================
A = [ 1, 1, 2;
3, 5, 8;
13, 21, 34]
% 1 1 2
% 3 5 8
% 13 21 34
B = rand (3, 2); # semicolon to not print
B # to print B
% 0.172471 0.056614
% 0.604975 0.671378
% 0.904422 0.987858
2 * A # matrix multiply constant
% 2 2 4
% 6 10 16
% 26 42 68
A * B # matrix multiply row by colum
% 2.5863 2.7037
% 10.7777 11.4296
% 45.6970 48.4221
A' # transpose matrix
% 1 3 13
% 1 5 21
% 2 8 34
A' * A # form the matrix product AT*A
% 179 289 468
% 289 467 756
% 468 756 1224
C = rand (3, 3)
% 0.0021958 0.9701577 0.7403410
% 0.8118483 0.3679684 0.5225063
% 0.7443481 0.0918563 0.5943353
C^-1 # inverse of matrix = 1/C
% -0.71728 2.13710 -0.98533
% 0.39324 2.31011 -2.52076
% 0.83755 -3.03355 3.30617
C^-1*C # 1/C * C = 1
% 1.00000 0.00000 -0.00000
% 0.00000 1.00000 -0.00000
% 0.00000 -0.00000 1.00000
C\C # equivalent to C^-1*C,
% 1.00000 0.00000 0.00000
% 0.00000 1.00000 0.00000
% -0.00000 0.00000 1.00000
A = [1 2 3; 4,5,6; 7 8 9]
% 1 2 3
% 4 5 6
% 7 8 9
size(A) % row cols = 3 3
A(4,2) = 13 % add to vector
% 1 2 3
% 4 5 6
% 7 8 9
% 0 13 0
A = [ 1, 1, 2; 3, 5, 8; 13, 21, 34 ]
% 1 1 2
% 3 5 8
% 13 21 34
ndims (A) % ans = 2
columns (A) % ans = 3
rows (A) % ans = 3
numel (A) % ans = 9 Returns the number of elements in the object a.
length (A) % ans = 3 number rows or columns, whichever is greater
size (A) % ans = 3 3
size ([1, 2; 3, 4; 5, 6]) % 3 2
[nr, nc] = size ([1, 2; 3, 4; 5, 6])
nr % 3
nc % 2
H = [1:4; linspace(10,40,4); 400:-100:100]
% 1 2 3 4
% 10 20 30 40
% 400 300 200 100
I = H( [1 2], : ) # Colon by itself means, “all the elements”
% 1 2 3 4
% 10 20 30 40
J = H( : ) # Colon by itself means, “all the elements”
k = zeros(3)
% 0 0 0
% 0 0 0
% 0 0 0
l = ones(4)
% 1 1 1 1
% 1 1 1 1
% 1 1 1 1
% 1 1 1 1
m = eye(3)
% 1 0 0
% 0 1 0
% 0 0 1
x = [1, 2, 3; 4, 5, 6; 7, 8, 9]
% 1 2 3
% 4 5 6
% 7 8 9
circshift (x, 1)
% 7 8 9
% 1 2 3
% 4 5 6
circshift (x, -2)
% 7 8 9
% 1 2 3
% 4 5 6
circshift (x, [0,1])
% 3 1 2
% 6 4 5
% 9 7 8
reshape ([1, 2, 3, 4], 2, 2)
% 1 3
% 2 4
sort ([1, 2; 2, 3; 3, 1])
% 1 1
% 2 2
% 3 3
a = [1, 2; 2, 3; 3, 1]
% 1 2
% 2 3
% 3 1
[s, i] = sort (a (:, 2));
% 1
% 2
% 3
% i =
% 3
% 1
% 2
x = ones (1, 2, 3)
% ans(:,:,1) =
% 1 1
% ans(:,:,2) =
% 1 1
% ans(:,:,3) =
% 1 1
size (shiftdim (x, -1))
% 1 1 2 3
size (shiftdim (x, 1))
% 2 3
[b, ns] = shiftdim (x);
% b =
% 1 1 1
% 1 1 1
% ns = 1
cross ([1,1,0], [0,1,1])
% 1 -1 1
G = rand (3)
% 0.38974 0.17352 0.77572
% 0.91265 0.59789 0.55643
% 0.77587 0.18040 0.79859
det(G) % -0.13672
==========================Matrix:Solve===============================
R1 = 10e3 # 10k
R2 = R3 = 5e3 # 5k
V = 10 #10V
R = [1 -1 -1;
0 0 R1;
0 (R2+R3) 0]
% R = 1 -1 -1
% 0 0 10000
% 0 10000 0
V = [0 10 10]'
% V =
% 0
% 10
% 10
I = R \ V # solve for I
% I =
% 0.0020000
% 0.0010000
% 0.0010000
V2 = R*I
% V2 =
% 0
% 10
% 10
A = [0,2,0,1;2,2,3,2;4,-3,0,1.;6,1,-6,-5] #Sample Input 4X4 Matrix
% A =
% 0 2 0 1
% 2 2 3 2
% 4 -3 0 1
% 6 1 -6 -5
B = [0;-2;-7;6] # Sample Input of Column Vector
% B =
% 0
% -2
% -7
% 6
# Matrix Solve A*ANS=B is A\B in Octave for A^(-1)*B in Math:
A\B # (Back Slash (\) ALERT: "\" for "divided into")
% -0.50000
% 1.00000
% 0.33333
% -2.00000
C = rand(5,5)
% 0.42784 0.51277 0.37575 0.96164 0.87992
% 0.46826 0.18080 0.10152 0.79470 0.69229
% 0.97067 0.26213 0.49988 0.67370 0.10226
% 0.67739 0.46039 0.12238 0.44211 0.24419
% 0.93623 0.53211 0.97488 0.97723 0.75156
D = rand(5,1)
% 0.27630
% 0.16780
% 0.81785
% 0.54391
% 0.20706
C\D # Solving C*X=D for the Vector X:
% -0.14489
% 0.81166
% -0.43989
% 1.70028
% -1.75889
=============================Read:Write:Matrixes========================
B = rand (5, 5)
# B =
# 0.333239 0.902413 0.132628 0.868415 0.037117
# 0.761534 0.692484 0.610604 0.662929 0.509673
# 0.022151 0.795431 0.221990 0.074476 0.879351
# 0.072556 0.555682 0.875474 0.944015 0.957373
# 0.953469 0.397975 0.859487 0.984755 0.980154
size (B)
# 5 5
save -ascii /Users/donsauer/Downloads/myfile3myfile.mat B
# requires a LF and CR to be in myfile3myfile.mat
load -ascii /Users/donsauer/Downloads/myfile3myfile.mat
size (myfile3myfile)
# 5 5
myfile3myfile
# myfile3myfile =
# 0.333239 0.902413 0.132628 0.868415 0.037117
# 0.761534 0.692484 0.610604 0.662929 0.509673
# 0.022151 0.795431 0.221990 0.074476 0.879351
# 0.072556 0.555682 0.875474 0.944015 0.957373
# 0.953469 0.397975 0.859487 0.984755 0.980154
mesh(myfile3myfile)
===============File:Read======================================
[val, count] = fread (fid, size, precision, skip, arch)
"native" The format of the current machine.
"ieee-be" IEEE big endian.
"ieee-le" IEEE little endian.
"vaxd" VAX D oating format.
"vaxg" VAX G oating format.
"cray" Cray oating format.
count = fwrite (fid, data, precision, skip, arch)
fgets (fid, len)
==============Read:Write:files===============================
fid = fopen ("myfile.txt", "w");
fdisp (fid, "3/8 is ");
fdisp (fid, 3/8);
fclose (fid);
filename = "myfile.txt";
fid = fopen (filename, "r");
while (! feof (fid) )
text_line = fgetl (fid)
endwhile
fclose (fid);
text_line = 3/8 is
text_line = 0.37500
filename = "free.txt";
fid = fopen (filename, "w");
fputs (fid, "Free Software is needed for Free Science \n");
fputs (fid, "Free Hardware is needed for Free Science");
fclose (fid);
fid = fopen ("free.txt");
txt = fgetl (fid)
%txt = Free Software is needed for Free Science
fclose (fid);
filename = "free.txt";
fid = fopen (filename, "r");
while (! feof (fid) )
text_line = fgetl (fid)
endwhile
fclose (fid);
text_line = Free Software is needed for Free Science
text_line = Free Hardware is needed for Free Science
filename = "free.txt";
fid = fopen (filename, "r");
i=1;
while (! feof (fid) )
text_line = fgetl (fid) # fgetl (fid, len) stopps after newline, or EOF, or len char
i=i+1;
endwhile
fclose (fid);
text_line = Free Software is needed for Free Science
text_line = Free Hardware is needed for Free Science
filename = "free.txt";
fid = fopen (filename, "r");
text_line = fgets (fid,4) # text_line = Free
asc = toascii (text_line) # 70 114 101 101
hx = dec2hex (asc) # 46 72 65 65
fclose (fid);
filename = "free.txt";
fid = fopen (filename, "w");
fputs (fid, "3 \n");
fputs (fid, "4");
fclose (fid);
filename = "free.txt";
fid = fopen (filename, "r");
i=1;
while (! feof (fid) )
text_line = fgetl (fid) # fgetl (fid, len) stopps after newline, or EOF, or len char
i=i+1;
endwhile
fclose (fid);
text_line = 3
text_line = 4
=============================Get:Set:Clear=============================
who #(information about what is in memory)
whos #(more detailed information about what is in memory)
clc #(clears the command window)
clear #(clears all user defined variables)
help # name
clf # command will clear the screen.
=============================FSOLVE:Functions=======================
fsolve' solves the set of equations such that `f(X) == 0'
function y = f(x) #Define Function:
y = 3*x +5;
endfunction
[x,info] = fsolve ("f",0) # x = -1.6667
f(-1.6667) # ans = -1.0000e-04
function y = f (x) # Define Function:
y = x^3+x^2-3*x-3;
endfunction
[x, info] = fsolve ("f", 1.) # Solve for Root Starting at x0=+1.0:
# x = 1.7321
# info = 1
[x, info] = fsolve ("f", 0.) # Solve for Root Starting at x0=0.0:
# x = -1
# info = 1
[x,info] = fsolve("f",-2.) #Solve for Root Starting at x0=-2.0:
# x = -1.7321
# info = 1
function y = f (x) # Define Function:2 Unknowns:
y(1) = -2*x(1)^2 + 3*x(1)*x(2) + 4*sin(x(2)) - 6;
y(2) = 3*x(1)^2 - 2*x(1)*x(2)^2 + 3*cos(x(1)) + 4;
endfunction
[x, info] = fsolve ("f", [1; 2]) #Solve for Vector Root Starting at [1; 2]:
# x =
# 0.57983
# 2.54621
# info = 1 A value of info = 1 indicates that the solution has converged.
x(1) %ans = 0.57983
-2*x(1)^2 + 3*x(1)*x(2) + 4*sin(x(2)) - 6 %ans = -5.7184e-10
3*x(1)^2 - 2*x(1)*x(2)^2 + 3*cos(x(1)) + 4 %ans = 5.5461e-10
=============================Poly:Deriv:integrate========================
c = [1, 0, 1]; % definite integral of p(x) = x2 + 1 from 0 to 3.
% [1, 0, 1]
% x^2 x^1 x^0
polyderiv (c) % differebtial a polygon
% 2 0
% x^1 x^0
integral = polyint(c) % intergrates a polygon
% integral =
% 0.33333 0.00000 1.00000 0.00000
% x^3 x^2 x^1 x^0
area = polyval(integral, 3) - polyval(integral, 0)
% 12
=============================Integral:Evaluating======================
Integral of exp(-x^2) on Both (0,1) and (0,Infinity) Intervals:
function y = f (x) # Comment: Define Integrand:
y = exp(-x*x);
endfunction
[area,ierror,nfneval] = quad("f",0,1) # Integral for x on (0,1):
# area = 0.74682
# ierror = 0 "ierror" is the error code
# nfneval = 21 nfneval" number of function evaluations.
[area,ierror,nfneval] = quad("f",0,Inf) # Comment: Integral for x on (0,Inf):
# area = 0.88623
# ierror = 0
# nfneval = 135
Trapezoidal rule integrate an f(x) from a to b using the trapezoidal rule with : h = (b-a)/N
function S = trapez(fun,a,b,N)
h = (b-a)/N;
fy = feval(fun,linspace(a,b,N+1)); % fy = feval(fun,[a:h:b]) better:
fy(1) = fy(1)/2;
fy(N+1) = fy(N+1)/2;
S = h*sum(fy);
end
function y = f(x)
y = exp(x);
end
for k = 1:15;
err(k) = abs(exp(1)-1-trapez('f',0,1,2^k));
end
loglog(1./2.^[1:15],err);
hold on;
title('Trapezoidal rule,f(x) = exp(x)');
xlabel('Increment');
ylabel('Error');
loglog(1./2.^[1:15],err,'x');
===========================Global:variables=================================
global N % makes N a global variable; may be set in main file
function out = foo(arg1,arg2)
global N % makes local N refer to the global N
end
global a
global b = 2
global c = 3, d, e = 5
global x % declare variable as global within function body to access it.
function f ()
x = 1; % does not set the value of the global variable x to 1.
endfunction
function f ()
global x; % must declare it to be global within function body
x = 1;
endfunction
============================Control:Loops========================
++x % x = x + 1.
--x % x = x - 1.
x++ % x incremented. value is the old value of x.
x-- % x decremented. value is old value of x.
[1, 2; 3, 4] == [1, 3; 2, 4]
ans =
1 0
0 1
x < y % True if x is less than y.
x <= y % True if x is less than or equal to y.
x == y % True if x is equal to y.
x >= y % True if x is greater than or equal to y.
x > y % True if x is greater than y.
x != y
x ~= y
x <> y % True if x is not equal to y.
x || y % OR
x && y % And
[1, 0; 0, 1]
1 0
0 1
[1, 0; 2, 3]
1 0
2 3
[1, 0; 0, 1] && [1, 0; 2, 3]
ans = 0
x = 7;
if (rem (x, 2) == 0)
printf ("x is even\n");
else
printf ("x is odd\n");
endif % x is odd
if (rem (x, 2) == 0)
printf ("x is even\n");
elseif (rem (x, 3) == 0)
printf ("x is odd and divisible by 3\n");
else
printf ("x is odd\n");
endif % x is odd
fib = ones (1, 10); % 1 1 1 1 1 1 1 1 1 1
i = 3;
while (i <= 10)
fib (i) = fib (i-1) + fib (i-2);
i++;
endwhile
fib % 1 1 2 3 5 8 13 21 34 55
fib = ones (1, 10); % 1 1 1 1 1 1 1 1 1 1
for i = 3:10
fib (i) = fib (i-1) + fib (i-2);
endfor
fib % 1 1 2 3 5 8 13 21 34 55
num = 103;
div = 2;
while (div*div <= num)
if (rem (num, div) == 0)
break;
endif
div++;
endwhile
if (rem (num, div) == 0)
printf ("Smallest divisor of %d is %d\n", num, div)
else
printf ("%d is prime\n", num);
endif % 103 is prime
num = 103;
div = 2;
while (1)
if (rem (num, div) == 0)
printf ("Smallest divisor of %d is %d\n", num, div);
break;
endif
if (div*div > num)
printf ("%d is prime\n", num);
break;
endif
div++;
endwhile % 103 is prime
vec = round (rand (1, 10) * 100) % 90 4 57 90 82 42 48 88 61 7
for x = vec
if (rem (x, 2) != 0)
continue;
endif
printf ("%d\n", x);
endfor
90
4
90
82
42
48
88
if x==0
error('x is 0!');
else
y = 1/x;
end
switch pnorm
case 1
sum(abs(v))
case inf
max(abs(v))
otherwise
sqrt(v'*v)
end
============================Functions:of:functions===========================
3.5 Functions of functions
eval(string) # evaluates string as Octave code.
feval(funcname,arg1, ) # equivalent to calling funcname with arguments arg1, .
Approximate an integral by the midpoint rule:
function y = gauss(x) # define functions gauss.m
y = exp(-x.^2/2);
end
function S = mpr(fun,a,b,N) # define functions mpr.m
h = (b-a)/N;
S = h*sum(feval(fun,[a+h/2:h:b]));
end
mpr('gauss',0,5,500) # function gauss can be integrated by calling:
============================INPUT:Prompt==================================
input (prompt, "s")
% ---- Press a key to continue ---
% s
% ans = [](0x0)
numb = input ("Pick a number, any number! ")
% Pick a number, any number! 3
% numb = 3
=============================MATH:Functions===============================
x = -2:.7:3
# x = -2.0000 -1.3000 -0.6000 0.1000 0.8000 1.5000 2.2000 2.9000
ceil (x)
# -2 -1 0 1 1 2 3 3
floor (x)
# -2 -2 -1 0 0 1 2 2
fix (x) % Truncate x toward zero.
# -2 -1 0 0 0 1 2 2
round (x) % integer nearest to x.
# -2 -1 -1 0 1 2 2 3
abs (x)
# 2.000000 1.300000 0.600000 0.100000 0.800000 1.500000 2.200000 2.900000
sign (x)
# -1 -1 -1 1 1 1 1 1
exp (x)
# 0.13534 0.27253 0.54881 1.10517 2.22554 4.48169 9.02501 18.17415
y = .1:.7:5
# 0.10000 0.80000 1.50000 2.20000 2.90000 3.60000 4.30000 5.00000
log (y)
# -2.30259 -0.22314 0.40547 0.78846 1.06471 1.28093 1.45862 1.60944
log10 (y)
# -1.000000 -0.096910 0.176091 0.342423 0.462398 0.556303 0.633468 0.698970
log2 (y)
# -3.32193 -0.32193 0.58496 1.13750 1.53605 1.84800 2.10434 2.32193
sqrt (y)
# 0.31623 0.89443 1.22474 1.48324 1.70294 1.89737 2.07364 2.23607
mod (x, y) % x - y .* floor (x ./ y)
# 0.00000 0.30000 0.90000 0.10000 0.80000 1.50000 2.20000 2.90000
pow2 (x)
# 0.25000 0.40613 0.65975 1.07177 1.74110 2.82843 4.59479 7.46426
pow2 (x, y) % x ^2
# -2.14355 -2.26343 -1.69706 0.45948 5.97141 18.18860 43.33628 92.80000
rem (x, y) % Return the remainder of x / y, computed x - y .* fix (x ./ y)
# 0.00000 -0.50000 -0.60000 0.10000 0.80000 1.50000 2.20000 2.90000
dim = 1;
max (x, y, dim)
# 0.10000 0.80000 1.50000 2.20000 2.90000 3.60000 4.30000 5.00000
min (x, y, dim)
# -2.000000 -1.300000 -0.600000 0.100000 0.800000 1.500000 2.200000 2.900000
factorial (3) % 1*2*3 ans = 6
factor (6)
# 2 3
primes (34) %Return all primes up to n.
# 2 3 5 7 11 13 17 19 23 29 31
x = -2:.7:3
# -2.0000 -1.3000 -0.6000 0.1000 0.8000 1.5000 2.2000 2.9000
y = .1:.7:4.5
# 0.10000 0.80000 1.50000 2.20000 2.90000 3.60000 4.30000 5.00000
x = [1:5] % 1 2 3 4 5
sum (x) % 15
prod (x) % 120
cumsum (x) % 1 3 6 10 15
cumprod(x) % 1 2 6 24 120
y = [1, 2; 3, 4]
# 1 2
# 3 4
sum (y)
# 4 6
prod (y)
# 3 8
cumsum (y)
# 1 2
# 4 6
cumprod(y)
# 1 2
# 3 8
r = rat(pi) % 3 + 1/(7 + 1/16) = 355/113 Find rational approx as string
r = rat(e) % 3 + 1/(-4 + 1/(2 + 1/(5 + 1/(-2 + 1/(-7)))))
x = str2num(r(2,:)) % convert the string back into a matrix as follows:
======================Arithmetic:Operators=====================
x =
0.077681 0.287496 0.556786
0.237022 0.046526 0.620466
0.773810 0.878441 0.915570
y =
0.082974 0.055266 0.109225
0.803756 0.171530 0.973141
0.105620 0.093594 0.876131
x .+ y # Element by element addition.
0.16066 0.34276 0.66601
1.04078 0.21806 1.59361
0.87943 0.97204 1.79170
x + y # Matrix addition.
0.16066 0.34276 0.66601
1.04078 0.21806 1.59361
0.87943 0.97204 1.79170
x * y # Matrix multiplication.
0.296330 0.105719 0.776077
0.122595 0.079152 0.614774
0.866960 0.279137 1.741525
x .* y # Element by element multiplication
0.0064455 0.0158888 0.0608147
0.1905082 0.0079806 0.6038007
0.0817295 0.0822170 0.8021586
x / y # Right division. equivalent to (inverse (y') * x')'
6.18025 -0.61311 0.54603
-0.97276 0.33525 0.45709
19.47723 -1.01422 -0.25663
x \ y # Left division. equivalent to inverse (x) * y
1.17715 0.19056 2.25307
-1.95063 -0.29678 -1.87121
0.99200 0.22591 0.84803
x .\ y # Element by element left division.
1.06813 0.19223 0.19617
3.39106 3.68679 1.56840
0.13649 0.10655 0.95692
x .^ y # Element by element power operator.
0.80896 0.93343 0.93804
0.31440 0.59084 0.62847
0.97328 0.98794 0.92563
x ^ 2 # Matrix multiplication x*x
0.50502 0.52481 0.73141
0.50956 0.61535 0.72892
0.97680 1.06761 1.81416
x .** y # Element by element element power operator
0.80896 0.93343 0.93804
0.31440 0.59084 0.62847
0.97328 0.98794 0.92563
-x
-0.077681 -0.287496 -0.556786
-0.237022 -0.046526 -0.620466
-0.773810 -0.878441 -0.915570
x' # Complex conjugate transpose
0.077681 0.237022 0.773810
0.287496 0.046526 0.878441
0.556786 0.620466 0.915570
x.' # Transpose.
0.077681 0.237022 0.773810
0.287496 0.046526 0.878441
0.556786 0.620466 0.915570
======================Complex:Functions==========================
j % 0 + 1i
i % 0 + 1i
sqrt(-1) % 0 + 1i
abs (x)
arg(3 + 4i) % 0.92730
angle(0 + 4i) % 1.5708
conj(3 + 4i) % 3 - 4i
imag(3 + 4i) % 4
real(3 + 4i) % 3
x = 1;
y = 2;
z = x + j * y # z = 1 + 2i
1/z # ans = 0.2 - 0.4i
z^2 # ans = -3 + 4i
conj(z) # ans = 1 - 2i
z*conj(z) # ans = 5
abs(z)^2 # ans = 5
norm(z)^2 # ans = 5
angle(z) # ans = 1.1071
# Now let's do polar form:
r = abs(z) # r = 2.2361
theta = angle(z) # theta = 1.1071
r * exp(j * theta) # ans = 1 + 2i
z # z = 1 + 2i
z/abs(z) # ans = 0.4472 + 0.8944i
exp(j*theta) # ans = 0.4472 + 0.8944i
z/conj(z) # ans = -0.6 + 0.8i
exp(2*j*theta) # ans = -0.6 + 0.8i
imag(log(z/abs(z))) # ans = 1.1071
theta # theta = 1.1071
x = [1:4]*j # x = 0 + 1i 0 + 2i 0 + 3i 0 + 4i
x = 1;
y = 2;
z = x + j * y % z = 1 + 2i
1/z % 0.2 - 0.4i
z^2 % -3 + 4i
conj(z) % 1 - 2i
z*conj(z) % 5
abs(z)^2 % 5
norm(z)^2 % 5
angle(z) % 1.1071
r = abs(z) % 2.2361 Now let's do polar form:
theta = angle(z) % 1.1071
r * exp(j * theta) % 1 + 2i
z % 1 + 2i
z/abs(z) % 0.4472 + 0.8944i
exp(j*theta) % 0.4472 + 0.8944i
z/conj(z) %-0.6 + 0.8i
exp(2*j*theta) %-0.6 + 0.8i
imag(log(z/abs(z))) % 1.1071
theta % 1.1071
x1 = 1;
x2 = 2;
y1 = 3;
y2 = 4;
z1 = x1 + j * y1;
z2 = x2 + j * y2;
z1 % z1 = 1 + 3i
z2 % z2 = 2 + 4i
z1*z2 % -10 +10i
z1/z2 % 0.7 + 0.1i
transpose operator ' (for vectors and matrices) conjugates as well as transposes.
Use .' to transpose without conjugation:
x = [1:4]*j % 0 + 1i 0 + 2i 0 + 3i 0 + 4i
x'
0 - 1i
0 - 2i
0 - 3i
0 - 4i
x.'
0 + 1i
0 + 2i
0 + 3i
0 + 4i
==============================Statistic:Functions==================
erf (1) % ans = 0.84270
erfinv (0.84270) % ans = 1.00000
Inside = (1 -2*(1-erf (1))) % Inside = 0.68540
Inside = (1 -2*(1-erf (3))) % Inside = 0.99996
function rmsout = rms(v)
vs = v.^2; % what does this line do? Also note semicolon.
s = size(v); % what does this line do?
rmsout=sqrt(sum(vs,1)/s(1)); % what is s(1)?
endfunction
v=sin([0: 0.0001*pi: 2*pi]')
# 0.00000
# 0.00031
# 0.00063
# 0.00094
# 0.00126
rms(v) # ans = 0.70709
v1 # v1 = 3 2 -5
rms(v1) # ans = 3 2 5
A
# 1 2 3
# 4 5 6
# 7 8 9
rms(A) # ans = 4.6904 5.5678 6.4807
% Comments Matlab:
% Comments or # Octave:
y = rand(3)
# 0.53094 0.30991 0.94716
# 0.48315 0.67147 0.63213
# 0.42188 0.10610 0.65008
std (y) # 0.054667 0.286329 0.176924
mean (y) # 0.47866 0.36249 0.74312
median (y) # 0.48315 0.30991 0.65008
meansq (y) # 0.23111 0.18606 0.57310
mu = sum(sum(y))/c # mu = 1.5843
mu = mean(mean(y)) # mu = 0.52809
y(:,1) # 0.53094
# 0.48315
# 0.42188
y(1,:) # 0.53094 0.30991 0.94716
mean (y(1,:) ) # 0.59600
mean (y(:,1) ) # 0.47866
Signal Energy and Power
x = rand(1,3) # 0.086410 0.042049 0.631235
std (x) # 0.32811
mean (x) # 0.25323
median (x) # 0.086410
meansq (x) # 0.13590
[r,c] = size(x) # r = 1 c = 3
mu = sum(x)/c # 0.25323
x = # 0.086410 0.042049 0.631235
Ex = x(:)' * x(:) # Ex = 0.40769
Ex = sum(conj(x(:)) .* x(:)) # Ex = 0.40769
Ex = sum(abs(x(:)).^2) # Ex = 0.40769
Px = Ex/c. # Px = 0.13590 average power (energy per sample) is similarly
xL2 = sqrt(Ex) # xL2 = 0.63851
norm(x) # xL2 = 0.63851 norm is similar
xL1 = sum(abs(x)) # xL1 = 0.75969 The norm is given by
xL1 = norm(x,1) # xL1 = 0.75969
xLInf = max(abs(x)) # xLInf = 0.63124 infinity-norm (Chebyshev norm) is computed as
xLInf = norm(x,Inf) # xLInf = 0.63124
==============================Factoring:Polynomials================
Let's find all roots of the polynomial
% polynomial = array of coefficients in matlab:
p = [1 0 0 0 5 7]; % p(x) = x^5 + 5*x + 7
format long; % print double-precision
roots(p) % print out the roots of p(x)
% 1.30051917307206 + 1.10944723819596i
% 1.30051917307206 - 1.10944723819596i
% -0.75504792501755 + 1.27501061923774i
% -0.75504792501755 - 1.27501061923774i
% -1.09094249610903
==============================Vector:Functions===============================
cross (x, y, dim) % vector cross product of two 3-dimensional vectors x and y.
cross ([1,1,0], [0,1,1]) % 1 -1 1
dot (x, y, dim)
[theta, r] = cart2pol (1, 1)
% theta = 0.78540
% r = 1.4142
[theta, r, z] = cart2pol (1, 1, 1)
% theta = 0.78540
% r = 1.4142
% z = 1
a = [1, 1, 1]
1 1 1
b = a'
b =
1
1
1
x= 1;y= 1;z= 1 ;
[theta, phi, r] = cart2sph (x, y, z)
[theta, phi, r] = cart2sph (x, y, z)
theta = 0.78540
phi = 0.61548
r = 1.7321
[x, y, z] = sph2cart (theta, phi, r)
x = 1
y = 1.00000
z =
1.0000
v1 =[3 2 -5] # v1-> = 3i +2j -5k v1 = 3 2 -5
v2 =[2 -4 10] # v2-> = 2i -4j +10k v2 = 2 -4 10
sum(v1.*v2) # ans = -52 v1->.v2-> = mag(v1)*mag(v2)*cos(theta)
vs = v1.^2 # vs = 9 4 25 dot means each and every
s =size(v1) # s = 1 3
v1 # v1 = 3 2 -5
pi (2, 3)
3.1416 3.1416 3.1416
3.1416 3.1416 3.1416
==============================Trigonometry:Functions=================
sin asin sinh asinh
cos acos cosh acosh
tan atan tanh atanh
sin ([1, 2; 3, 4])
0.84147 0.90930
0.14112 -0.75680
============================Transfer:Function======================
sys = tf([1 2],[3 4 5]);
[num,den] = sys2tf(sys)
sysout(sys)
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1
% transfer function form:
% 1*s^1 + 2
% -----------------
% 3*s^2 + 4*s^1 + 5
sysout(sys, "tf")
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1
% transfer function form:
% 1*s^1 + 2
% -----------------
% 3*s^2 + 4*s^1 + 5
sysout(sys, "zp")
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1
% zero-pole form:
% 0.33333 (s + 2)
% -------------------------------------------------
% (s + 0.66667 - 1.1055i) (s + 0.66667 + 1.1055i)
bode(sys);
clf
rlocus(sys)
num = [5, -1]
denom = [1, -2, 6]
sys = tf(num,denom);
sys = tf([5, -1], [1, -2, 6]); %alternatively
sysout(sys)
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1
% transfer function form:
% 5*s^1 - 1
% -----------------
% 1*s^2 - 2*s^1 + 6
sys1 = tf([0.00100502, -0.00099502], [1, -2, 1], 0.001);
sysout(sys1)
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1 (discrete)
% Sampling interval: 0.001
% transfer function form:
% 0.001005*z^1 - 0.00099502
% -------------------------
% 1*z^2 - 2*z^1 + 1
sysout(sys1,"zp"); % examine open loop zeros and poles of the system,
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1 (discrete)
% Sampling interval: 0.001
% zero-pole form:
% 0.001005 (z - 0.99005)
% ----------------------
% (z - 1) (z - 1)
bode(sys1); % o view the system's bode plots, execute the following
sys1 = tf([1.5, 18.5, 6],[1, 4, 155, 302, 5050]);
sysout(sys1);
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1
% transfer function form:
% 1.5*s^2 + 18.5*s^1 + 6
% ----------------------------------------
% 1*s^4 + 4*s^3 + 155*s^2 + 302*s^1 + 5050
sysout(sys1,"zp"); %Pole-zero form can be obtained as follows:
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1
% 1.5 (s + 0.33333) (s + 12)
% -------------------------------------------------------
% (s + 1 - 7i) (s + 1 + 7i) (s + 1 - 10i) (s + 1 + 10i)
rlocus(sys1);
sys = tf([2 1],[1 2 1],0.1);
sysout(sys)
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1 (discrete)
% Sampling interval: 0.1
% transfer function form:
% 2*z^1 + 1
% -----------------
% 1*z^2 + 2*z^1 + 1
[num,den] = sys2tf(sys) % extract transfer function
% num = 2 1
% den = 1 2 1
sys = tf2sys([1 2],[3 4 5]);
% [a,b,c,d] = sys2ss(sys)
% a =
% 0.00000 1.00000
% -1.66667 -1.33333
% b =
% 0
% 1
% c = 0.66667 0.33333
% d = 0
sys = tf2sys([2 1],[1 2 1],0.1);
% sysout(sys)
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1 (discrete)
% Sampling interval: 0.1
% transfer function form:
% 2*z^1 + 1
% -----------------
% 1*z^2 + 2*z^1 + 1
============================Pole:Zero==========================
sys2 = zp([0.99258;0.99745],[0.99961;0.99242],1,0.001);
sysout(sys2)
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1 (discrete)
% Sampling interval: 0.001
% zero-pole form:
% 1 (z - 0.99745) (z - 0.99258)
% -----------------------------
% (z - 0.99961) (z - 0.99242)
bode(sys2);
sys2 = zp([],[0, -20, -2, -0.1],5);
sysout(sys2);
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1
% zero-pole form:
% 5
% ----------------------------
% s (s + 0.1) (s + 2) (s + 20)
rlocus(sys2);
sys = zp2sys([1 -1],[-2 -2 0],1);
sysout(sys)
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1
% zero-pole form:
% 1 (s - 1) (s + 1)
% -----------------
% s (s + 2) (s + 2)
============================TF:to:PZ==========================
num = [2 3];
den = [1 4 0 5];
[z,p,k] = tf2zp(num,den) % H(s) = k(s- z )(s- z ) (s- z )/( (s- p )(s- p ) (s- p ))
% z = -1.5000
% p =
% -4.27375 + 0.00000i
% 0.13687 + 1.07294i
% 0.13687 - 1.07294i
% k = 2
[num,den] = zp2tf(z,p,k)
% num =
% 2 3
% den =
% 1.0000e+00 4.0000e+00 6.6613e-16 5.0000e+00
============================STEP:IMPULSE=========================
y(s)/F(s) = 1/(ms2+Ds+K)
m = 20;
K = 2;
D = 4;
num = [1];
denum = [m,D,K];
mfdsys = tf(num,denum) % Creates system data structure mfdsys.
sysout(mfdsys,'tf') % Displays the transfer function.
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1
% transfer function form:
% 1
% ------------------
% 20*s^2 + 4*s^1 + 2
step(mfdsys)
impulse(mfdsys) .
============================STATE:SPACE:FORM=========================
sys3 = ss([.857, .0011; 0, .99930],[1;1],[-.6318, .0057096],5.2, .001);
sysout(sys3);
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1 (discrete)
% Sampling interval: 0.001
% state-space form:
% 0 continuous states, 2 discrete states
% State(s):
% 1: xd_1 (discrete)
% 2: xd_2 (discrete)
% A matrix: 2 x 2
% 0.85700 0.00110
% 0.00000 0.99930
% B matrix: 2 x 1
% 1
% 1
% C matrix: 1 x 2
% -0.6318000 0.0057096
% D matrix: 1 x 1
% 5.2000
bode(sys3); % To view the system's bode plots
wrange
= logspace(log10(1),log10(1000),100);
bode(sys3,wrange);
sys3 = ss([0, 1; -10, -11], [0; 1], [0, -2], 0);
sysout(sys3);
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1
% state-space form:
% 2 continuous states, 0 discrete states
% State(s):
% 1: x_1
% 2: x_2
% A matrix: 2 x 2
% 0 1
% -10 -11
% B matrix: 2 x 1
% 0
% 1
% C matrix: 1 x 2
% 0 -2
% D matrix: 1 x 1
% 0
sysout(sys3,"zp")
% Input(s)
% 1: u_1
% Output(s):
% 1: y_1
% zero-pole form:
% -2s
% ------------------
% (s + 1) (s + 10)
bode(sys3);
wrange = logspace(log10(1),log10(1000),100);
bode(sys3,wrange);
============================FORMAT:TRANSLATE=================================
x = uint16 ([1, 65535]);
typecast ( x, 'uint8') # 1 0 255 255
uint16 (1:4) # 1 2 3 4
swapbytes (uint16 (1:4)) # 256 512 768 1024 swapbytes little endian to big endian
double (x) # Convert x to double precision type.
single (val) # Convert the numeric value val to single precision.
complex (val)
complex (re, im) # Convert x to a complex value.
float = rand (2, 2)
% float = 0.37569 0.92982
% 0.11962 0.50876
integer = int32 (float)
% integer = 0 1
% 0 1
int8 (x) # Convert x to 8-bit integer type.
uint8 (x) # Convert x to unsigned 8-bit integer type.
int16 (x) # Convert x to 16-bit integer type.
uint16 (x) # Convert x to unsigned 16-bit integer type.
int32 (x) # Convert x to 32-bit integer type.
uint32 (x) # Convert x to unsigned 32-bit integer type.
int64 (x) # Convert x to 64-bit integer type.
uint64 (x) # Convert x to unsigned 64-bit integer type.
intmax (type)
char ([97, 98, 99]) # abc
dec2bin (14) # 1110
dec2hex (2748) # ABC
hex2dec ("12B") # 299
hex2dec ("12b") # 299
dec2base (123, 3) # 11120
dec2base (123, "aei") # eeeia
base2dec ("11120", 3) # 123
base2dec ("yyyzx", "xyz") #
str2double ("-.1e-5") # -1.0000e-06
str2double (".314e1, 44.44e-1, .7; -1e+1") #
% 3.14000 4.44400 0.70000
% -10.00000 NaN NaN
toascii ("ASCII") # 65 83 67 73 73
tolower ("MiXeD cAsE 123") # mixed case 123
toupper ("MiXeD cAsE 123") # MIXED CASE 123
isalpha ("!Q@WERT^Y&") # [ 0, 1, 0, 1, 1, 1, 1, 0, 1, 0 ]
isalnum (s) #[Mapping Function]
x = uint16 ([1, 65535]);
y = typecast (x, 'uint8') # y = 1 0 255 255
y(1) # ans = 1
y(2) # ans = 0
y(3) # ans = 255
b = uint16 (1:4) # b = 1 2 3 4
d = typecast( uint16 (1:4), 'uint8') # d = 1 0 2 0 3 0 4 0
e = typecast( swapbytes (uint16 (1:4)), 'uint8') # e = 0 1 0 2 0 3 0 4
c = swapbytes (uint16 (1:4)) # c = 256 512 768 1024
===========================Unit:Conversion===========================
x = uint16 ([1, 65535]);
typecast ( x, 'uint8') # 1 0 255 255
uint16 (1:4)
1 2 3 4
swapbytes (uint16 (1:4)) # swapbytes (x) converting between little endian to big endian
# 256 512 768 1024
double (x) # Convert x to double precision type.
single (val) # Convert the numeric value val to single precision.
complex (val)
complex (re, im) # Convert x to a complex value.
float = rand (2, 2)
float = 0.37569 0.92982
0.11962 0.50876
integer = int32 (float)
integer = 0 1
0 1
bitset (10, 1) # ans = 11
dec2bin (bitset (10, 1)) # 1011
int8 (x) # Convert x to 8-bit integer type.
uint8 (x) # Convert x to unsigned 8-bit integer type.
int16 (x) # Convert x to 16-bit integer type.
uint16 (x) # Convert x to unsigned 16-bit integer type.
int32 (x) # Convert x to 32-bit integer type.
uint32 (x) # Convert x to unsigned 32-bit integer type.
int64 (x) # Convert x to 64-bit integer type.
uint64 (x) # Convert x to unsigned 64-bit integer type.
intmax (type) # Return largest integer can represented in an integer type
intmin (type) # Return smallest integer be represented in an integer type
int8 signed 8-bit integer.
int16 signed 16-bit integer.
int32 signed 32-bit integer.
int64 signed 64-bit integer.
uint8 unsigned 8-bit integer.
uint16 unsigned 16-bit integer.
uint32 unsigned 32-bit integer.
uint64 unsigned 64-bit integer.
default uint32.
=================================Number:Translate===========================
8:-1:1 # 8 7 6 5 4 3 2 1
bitget (100, 8:-1:1) # 0 1 1 0 0 1 0 0
bitget (100, 8:-1:1)
# is the same as
# bitget (100 * ones (1, 8), 8:-1:1)
bitand (x, y) # bitwise AND of nonnegative integers.
bitor (x, y)
bitxor (x, y)
bitcmp (a, y) # Return k-bit complement of integers in a
x = 7 ; y = 4;
dec2bin(x) # ans = 111
dec2bin(y) # 100
bitcmp(x,y) # 8
bitand (x, y) # 4
bitor (x, y) # 7
bitxor (x, y) # 3
dec2bin(6) # 110
dec2bin(11) # 1011
dec2bin(bitcmp(11, 6)) # 110100
bitshift (eye (3), 1)
# 2 0 0
# 0 2 0
# 0 0 2
bitshift (10, [-2, -1, 0, 1, 2]) # 2 5 10 20 40
bitshift (-10, -1) # -5
bitshift (-10, 1) # -20
bitshift (int8 (-1), -1) # -1
s = mat2str (A) # s = [1,2,3;4,5,6;7,8,9]
t = str2mat(s) # t = [1,2,3;4,5,6;7,8,9]
str = num2str (44) # str = 44
str = "44"
n = num2str (44) # n = 44
int2str (n)
strcmp (s1, s2)
findstr ("ababab", "a") # 1 3 5
findstr ("abababa", "aba", 0) # 1 5
index ("Teststring", "t") # 4
rindex ("Teststring", "t") # 6
dec2bin (14) # 1110
dec2hex (2748) # ABC
hex2dec ("12B") # 299
hex2dec ("12b") # 299
dec2base (123, 3) # 11120
dec2base (123, "aei") # eeeia
base2dec ("11120", 3) # 123
base2dec ("yyyzx", "xyz") #
str2double ("-.1e-5") # -1.0000e-06
str2double ( ".314e1, 44.44e-1, .7; -1e+1") #
# 3.14000 4.44400 0.70000
# -10.00000 NaN NaN
toascii ("ASCII") # 65 83 67 73 73
tolower ("MiXeD cAsE 123") # mixed case 123
toupper ("MiXeD cAsE 123") # MIXED CASE 123
isalpha ("!Q@WERT^Y&") # [ 0, 1, 0, 1, 1, 1, 1, 0, 1, 0 ]
isalnum (s) #[Mapping Function]
=================================Print:Formats========================
pct = 37;
filename = "foo.txt";
printf ("Processed %d%% of `%s'.\nPlease be patient.\n", pct, filename);
# Processed 37% of `foo.txt'.
# Please be patient.
printf ("Processed %d%% of `%s'. Please be patient.\n",pct, filename);
# Processed 37% of `foo.txt'. Please be patient.
printf ("%4.2f %10.2e %8.4g\n", hilb (3));
# 1.00 5.00e-01 0.3333
# 0.50 3.33e-01 0.25
# 0.33 2.50e-01 0.2
printf ("%4.2f %10.2e %8.4g\n", [1, 2], [3, 4]);
# 1.00 2.00e+00 3
# 4.00
fdisp (stdout, "The value of pi is:"), fdisp (stdout, pi)
# a the value of pi is:
# a 3.1416
\\ Represents a literal backslash, `\'.
\" Represents a literal double-quote character, `"'.
\' Represents a literal single-quote character, `''.
\0 Represents the \nul" character, control-@, ASCII code 0.
\a Represents the \alert" character, control-g, ASCII code 7.
\b Represents a backspace, control-h, ASCII code 8.
\f Represents a formfeed, control-l, ASCII code 12.
\n Represents a newline, control-j, ASCII code 10.
\r Represents a carriage return, control-m, ASCII code 13.
\t Represents a horizontal tab, control-i, ASCII code 9.
\v Represents a vertical tab, control-k, ASCII code 11.
pi
# pi = 3.1416
disp ("The value of pi is:"), disp (pi)
# The value of pi is:
# 3.1416
format long
disp ("The value of pi is:"), disp (pi)
# The value of pi is:
# 3.14159265358979
=================================STRING:Functions==================
split ("Test string", "t")
# Tes
# s
# ring
split ("Test string", "t", 2)
# Tes
# string
s = "Test string"
offset = 2
len = 7
substr (s, offset, len) % est str
hex2dec ("FF") % 255
bin2dec ("1110") % 14
dec2bin (14) %"1110"
dec2hex (2748) %"ABC"
hex2dec ("12B") % 299
hex2dec ("12b") % 299
char ([97, 98, 99]) % abc
s = [ "ab"; "cde" ];
strcat (s, s, s)
# ab ab ab
# cdecdecde
s = "abcde";
strtrunc (s, 3) % abc
string_fill_char ("X");
[ "these"; "are"; "strings" ]
# theseXX
# areXXXX
# strings
aquote = ...
"First things first, but not necessarily in that order";
quote( aquote == " " ) = "_"
# quote = First_things_first,_but_not_necessarily_in_that_order
s = "1 12 3"
t = str2mat(s) % 1 12 3
size(t) % 1 6
t(:)
# 1
#
# 1
# 2
#
# 3
char ([97, 98, 99]) % abc
s = [ "ab"; "cde" ];
strcat (s, s, s)
# ab ab ab
# cdecdecde
deblank(" 1 12 3 ") % 1 12 3
substr ("This is a test string", 6, 9) % is a test
split ("Test string", "t")
# Tes
# s
# ring
split ("Test string", "t", 2)
# Tes
# string
strrep ("This is a test string", "is", "&%$") % Th&%$ &%$ a test string
s =" 1.01 -12 3 66 ";
s = strrep (s, " ", " ") ;
s = strrep (s, " ", " ") ;
s = strrep (s, " ", " ") ;
s = strrep (s, " ", " ") ;
s = strrep (s, " ", " ") ;
s = strrep (s, " ", " ") ;
s = strrep (s, " ", ":") # s = :1.01:-12:3:66:
len = length(s); # 15
offset = 2;
s2 = substr (s, offset, len-2); # s2 = 1.01:-12:3:66
t = str2num(split (s2, ":"));
# 1.0100
# -12.0000
# 3.0000
# 66.0000
t(2) # -12
size(t) # 4 1
===========================FFT:Functions=====================================
t = 0:.1:10; % x from 0 to 10 in .1 steps
x = cos(2*pi*.5*t); % Functions are applied element by element. For example,
plot (t, x);
y = fft(x);
subplot(2,1,1);
plot (real(y));
subplot(2,1,2);
plot (imag(y));
z = ifft(y);
clf
plot (t, z);
t = 0:.1:10;
x = cos(2*t);
N = 2^10;
X = fft(x,N);
subplot(2,1,1);
plot (real(X));
subplot(2,1,2);
plot (imag(X));
y = ifft(X);
plot (0:N-1,y);
=========================
fft (a, n, dim) n number of elements of a to use, dim specifying dimension
ifft (a, n, dim)
fft2 (a, n, m) two dimensional FFT of a using subroutines from Fftw.
ifftn (a, size) inverse N dimensional FFT of a using subroutines from Fftw.
================================Structure:Format=======================
c = {"a string", rand(2, 2)}; # a cell array indexed with { and }
c{1} # ans = a string
c{1:2}
# ans =
# (,[1] = a string
# [2] =
# 0.35260 0.77503
# 0.92208 0.45996
# ,)
c{3} = 3 # add a cell
# c =
# { [1,1] = a string
# [1,2] =
# 0.35260 0.77503
# 0.92208 0.45996
# [1,3] = 3
# }
c = cell(2,2) # creates a 2-by-2 cell array empty matrices
# c =
# { [1,1] = [](0x0)
# [2,1] = [](0x0)
# [1,2] = [](0x0)
# [2,2] = [](0x0)
# }
c1 = cell(3, 4, 5);
size(c1) # 3 4 5
reshape(1:16,4,4)
# 1 5 9 13
# 2 6 10 14
# 3 7 11 15
# 4 8 12 16
mat2cell (reshape(1:16,4,4),[3,1],[3,1])
# { [1,1] =
# 1 5 9
# 2 6 10
# 3 7 11
# [2,1] =
# 4 8 12
# [1,2] =
# 13
# 14
# 15
# [2,2] = 16
# }
x = {"1", "2"; "3", "4"}
# x =
# { [1,1] = 1
# [2,1] = 3
# [1,2] = 2
# [2,2] = 4
# }
x{1, :} = []
# x =
# {
# [1,1] = [](0x0)
# [2,1] = 3
# [1,2] = [](0x0)
# [2,2] = 4
# }
x(1, :) = []
# x =
# {
# [1,1] = 3
# [1,2] = 4
# }
a = ["hello"; "world"]
# a =
# hello
# world
c = cellstr (a)
# c =
# {
# [1,1] = hello
# [2,1] = world
# }
A = cell2struct ({'Peter', 'Hannah', 'Robert';
185, 170, 168},
{'Name','Height'}, 1)
# A =
# {
# Name =
# (,
# [1] = Peter
# [2] = Hannah
# [3] = Robert
# ,)
# Height =
# (,
# [1] = 185
# [2] = 170
# [3] = 168
# ,)
# }
A(1)
# ans =
# {
# Name = Peter
# Height = 185
# }
A
# A =
# 1 2 3
# 4 5 6
# 7 8 9
A(2,2) # ans = 5
===================MISC:Functions===========================================
clock # ans = 1993 8 20 4 56 1
# month (1-12), day (1-31), hour (0-23), minute (0-59) and second (0-60)
date % 20-Aug-93
users = # shell_cmd ("finger", 1)
shell_cmd ("pwd") % /Users/donsauer
getenv ("PATH") % returns a string containing the value of your path.
time () % ans = 1.3064e+09
% sec sinc epoch 00:00:00 CUT (Coordinated Universal Time) 1 Jan 1970.
t = now () % t = 7.3465e+05
% number of days since Jan 1, 0000. Jan 1, 1970 is day number 719529.
% The integral part, floor (now) corresponds to 00:00:00 today.
ctime (t) % ans = Fri Jan 09 04:04:09 1970
ctime (time ()) % ans = Thu May 26 11:16:29 2011
gmtime (t) % return a time structure corresponding to CUT. For example,
gmtime (time ())
{
usec = 510869
sec = 52
min = 17
hour = 18
mday = 26
mon = 4
year = 111
wday = 4
yday = 145
isdst = 0
zone = UTC
}
localtim(time ())
{
usec = 271974
sec = 22
min = 18
hour = 11
mday = 26
mon = 4
year = 111
wday = 4
yday = 145
isdst = 1
zone = PDT
}
=============================PLOT:Functions================================
x = [2 3]; % coordinates of x
origin = [0 0]; % coordinates of the origin
xcoords = [origin(1) x(1)]; % plot() expects coordinates
ycoords = [origin(2) x(2)];
plot(xcoords,ycoords); % Draw a line from origin to x
v = 0:.5:3 # 0.00000 0.50000 1.00000 1.50000 2.00000 2.50000 3.00000
plot(v) # plot to index
M = rand(3,3) # random
0.731163 0.926955 0.202031
0.711750 0.519944 0.046730
0.206870 0.484734 0.772079
plot(M(2,:)) # plot second row
plot(M(:,3)) # plot third column
function y = f(x) # Define Function:
y = -sin(x);
endfunction
x = -6:.1:6; # Define range
plot(x,f(x)) # plot function to x
x = -10:.1:10; # Define range
y = sin(x).*exp(-abs(x)); # Define y
plot(x,y) # X Y Plot
grid # add grid
x = .001:.3:7; # Define range
y = sin(x); # Define y
plot (x, y) # X Y Plot
title ("title here") # Add title
xlabel ("xlabel here") # Add xlabel
ylabel ("ylabel here") # Add ylabel
x = -10:0.1:10;
plot (x, sin (x));
title ("sin(x) for x = -10:0.1:10");
xlabel ("x"); # xlabel
ylabel ("sin (x)"); # ylabel
text (pi, 0.7, "arbitrary text"); # text
legend ("sin (x)"); # legend
xmin = 0;
xmax = 6;
ymin = -.5;
ymax = .5;
axis([xmin, xmax, ymin, ymax]); # axis
x = 0:0.01:3;
plot (x,erf(x));
hold on; # hold on
plot(x,x,"r");
axis([0, 3, 0, 1]);
text(0.65, 0.6175, strcat('\leftarrow x = {2/\surd\pi',
' {\fontsize{16}\int_{\fontsize{8}0}^{\fontsize{8}x}}',
' e^{-t^2} dt} = 0.6175'))
xmin = 0;
xmax = 6;
ymin = 0;
ymax = 0;
axisV = [xmin, xmax, ymin, ymax];
axis([xmin, xmax, ymin, ymax]);
loglog (x, y) # warning: axis: omitting nonpositive data in log plot
semilogx (x, y)
semilogy (x, y) # warning: axis: omitting nonpositive data in log plot
clear # clear memory
start = 0;
stop = 10*pi;
numbpts = 100;
x = linspace(start,stop,numbpts);
size(x) # 1 100
y = sin(x);
clf # clear plot
plot(x,y)
hold on # stop autoclear
plot(x,y,'ro') # r=red o=points
hold off # autoclear on
clf
hold ("on");
x = -10:0.1:10;
plot (x, sin (x),'c:');
plot (x, cos (x),'gs');
hold ("off");
o (circle) , x (x-marks), +, *, s (square), d (diamond), ^ (up triangle),
- (solid line, the default), : (dotted line), -- (dashed line), -. (dash-dotted line).
y(ellow) , m(agenta) , c(yan) , r(ed), g(reen), b(lue), w(hite), k(black).
subplot (2, 1, 1) # subplot
fplot (@sin, [-10, 10]);
subplot (2, 1, 2)
fplot (@cos, [-10, 10]);
figure (1);
fplot (@sin, [-10, 10]);
figure (2); # figure (2)
fplot (@cos, [-10, 10]);
t = 0:0.1:6.3;
plot (t, cos(t), "-;cos(t);", t, sin(t), "+3;sin(t);");
polar (0:0.1:10*pi, 0:0.1:10*pi);
fplot ("cos", [0, 2*pi])
fplot ("[cos(x), sin(x)]", [0, 2*pi])
subplot (2, 1, 1);
set (0, "defaultlinecolor", "red");
set (1, "defaultlinecolor", "green");
set (gca (), "defaultlinecolor", "blue");
line (1:10, rand (1, 10));
subplot (2, 1, 2);
line (1:10, rand (1, 10));
figure (2)
line (1:10, rand (1, 10));
x = 0:.3:3
y = sin(x)
bar (x, y);
[xb, yb] = bar (x, y);
plot (xb, yb);
stairs (x, y)
sombrero (10)
randn (3, 1)
-0.44565
1.25612
-1.74002
hist (randn (10000, 1), 30);
x = 0:2;
y = x;
z = x' * y;
contour (x, y, z, 2:3)
2.0000 1.0000 1.0000 2.0000 2.0000 3.0000 1.5000 2.0000
4.0000 2.0000 2.0000 1.0000 1.0000 2.0000 2.0000 1.5000
polar (0:0.1:10*pi, 0:0.1:10*pi);
t = 0:0.1:10*pi;
r = linspace (0, 1, numel (t));
z = linspace (0, 1, numel (t));
plot3 (r.*sin(t), r.*cos(t), z);
x = 0:0.3:6.3;
y = sin(x)
barh (x, y)
bar (x, y)
stairs (x, y);
x = 1:10;
y = ones (size (x))*2.*x;
h = stem (x, y, "-.k");
x = 0:0.1:10;
y = sin (x);
yp = 0.1 .* randn (size (x));
ym = -0.1 .* randn (size (x));
errorbar (x, sin (x), ym, yp);
[x, y] = meshgrid (1:2:20);
quiver (x, y, sin (2*pi*x/10), sin (2*pi*y/10));
tx = ty = linspace (-8, 8, 41)';
[xx, yy] = meshgrid (tx, ty);
r = sqrt (xx .^ 2 + yy .^ 2) + eps; % eps' is approximately 2.2204e-16.
tz = sin (r) ./ r;
mesh (tx, ty, tz);
tx = ty = linspace (-8, 8, 41)';
[xx, yy] = meshgrid (tx, ty);
r = sqrt (xx .^ 2 + yy .^ 2) + eps; % eps' is approximately 2.2204e-16.
tz = sin (r) ./ r;
surfc (tx, ty, tz);
x = rand (1, 7);
% 0.205076 0.143246 0.132569 0.170692 0.025636 0.021859 0.476844
y = rand (size (x));
% 0.629666 0.257001 0.118183 0.404563 0.413007 0.085909 0.469295
T = delaunay (x, y);
4 1 7
5 4 1
2 4 7
2 3 7
2 5 4
2 5 6
2 3 6
X = [x(T(:,1)); x(T(:,2)); x(T(:,3)); x(T(:,1))];
Y = [y(T(:,1)); y(T(:,2)); y(T(:,3)); y(T(:,1))];
axis ([0,1,0,1]);
plot (X, Y, "b", x, y, "r*");
rand ("state", 2);
x = rand (10, 1);
y = rand (10, 1);
T = delaunay (x, y);
X = [ x(T(:,1)); x(T(:,2)); x(T(:,3)); x(T(:,1)) ];
Y = [ y(T(:,1)); y(T(:,2)); y(T(:,3)); y(T(:,1)) ];
axis ([0, 1, 0, 1]);
plot(X, Y, "b", x, y, "r*");
rand ("state", 2)
x = rand (20, 1);
y = rand (20, 1);
tri = delaunay (x, y);
triplot (tri, x, y);
rand("state",9);
x = rand(5,1)
0.15716
0.23801
0.11095
0.50627
0.92383
y = rand(5,1)
0.59043
0.77421
0.38366
0.74610
0.10167
tri = delaunay (x, y)
4 5 3
1 4 3
1 4 2
[vx, vy] = voronoi (x, y, tri)
vx =
-6.6440e+04 -5.6452e+04 -2.0936e+04 4.4334e-01 4.4334e-01 5.4117e-01 7.7116e+03
4.4334e-01 3.5665e-01 5.4117e-01 5.4117e-01 3.5665e-01 4.9328e+04 3.5665e-01
vy =
1.4849e+04 2.4837e+04 -6.0352e+04 4.1792e-01 4.1792e-01 3.1121e-01 7.3578e+04
4.1792e-01 6.1234e-01 3.1121e-01 3.1121e-01 6.1234e-01 3.1962e+04 6.1234e-01
triplot (tri, x, y, "b");
hold on;
plot (vx, vy, "r");
hold off;
randn ("state", 2);
x = randn (100, 1);
y = randn (100, 1);
vx = cos (pi * [-1 : 0.1: 1]);
vy = sin (pi * [-1 : 0.1 : 1]);
in = inpolygon (x, y, vx, vy);
0
1
0
0
1
plot(vx, vy, x(in), y(in), "r+", x(!in), y(!in), "bo");
axis ([-2, 2, -2, 2]);
x = -3:0.05:3;
y = abs (sin (x));
k = convhull (x, y);
plot (x(k), y(k), "r-", x, y, "b+");
axis ([-3.05, 3.05, -0.05, 1.05]);
rand("state",1);
x=2*rand(1000,1)-1;
y=2*rand(size(x))-1;
z=sin(2*(x.^2+y.^2));
[xx,yy]=meshgrid(linspace(-1,1,32));
griddata(x,y,z,xx,yy);
[x,y] = meshgrid(-1:.5:1) # create x and y grid arrays
x =
-1.00000 -0.50000 0.00000 0.50000 1.00000
-1.00000 -0.50000 0.00000 0.50000 1.00000
-1.00000 -0.50000 0.00000 0.50000 1.00000
-1.00000 -0.50000 0.00000 0.50000 1.00000
-1.00000 -0.50000 0.00000 0.50000 1.00000
y =
-1.00000 -1.00000 -1.00000 -1.00000 -1.00000
-0.50000 -0.50000 -0.50000 -0.50000 -0.50000
0.00000 0.00000 0.00000 0.00000 0.00000
0.50000 0.50000 0.50000 0.50000 0.50000
1.00000 1.00000 1.00000 1.00000 1.00000
Plot the graph of the function
f(x,y) = exp(-x^2 -y^2)
x = -3:0.1:3;
[xx,yy] = meshgrid(x,x); % create 2D grid data arrays
z = exp(-xx.^2-yy.^2); % calc Z for mesh grid
figure, mesh(x,x,z); % Set current plot window , mesh 3D plot
title('exp(-x^2-y^2)');
tx = ty = linspace (-8, 8, 41)';
[xx, yy] = meshgrid (tx, ty);
r = sqrt (xx .^ 2 + yy .^ 2) + eps; % eps = 2.2204e-16
tz = sin (r) ./ r;
mesh ( tx, ty, tz);
tx = ty = linspace (-8, 8, 5)';
tx =
-8
-4
0
4
8
[xx, yy] = meshgrid (tx, ty);
xx =
-8 -4 0 4 8
-8 -4 0 4 8
-8 -4 0 4 8
-8 -4 0 4 8
-8 -4 0 4 8
yy =
-8 -8 -8 -8 -8
-4 -4 -4 -4 -4
0 0 0 0 0
4 4 4 4 4
8 8 8 8 8
r = sqrt (xx .^ 2 + yy .^ 2) + eps; % eps = 2.2204e-16
r =
1.1314e+01 8.9443e+00 8.0000e+00 8.9443e+00 1.1314e+01
8.9443e+00 5.6569e+00 4.0000e+00 5.6569e+00 8.9443e+00
8.0000e+00 4.0000e+00 2.2204e-16 4.0000e+00 8.0000e+00
8.9443e+00 5.6569e+00 4.0000e+00 5.6569e+00 8.9443e+00
1.1314e+01 8.9443e+00 8.0000e+00 8.9443e+00 1.1314e+01
tz = sin (r) ./ r;
tz =
-0.083953 0.051679 0.123670 0.051679 -0.083953
0.051679 -0.103622 -0.189201 -0.103622 0.051679
0.123670 -0.189201 1.000000 -0.189201 0.123670
0.051679 -0.103622 -0.189201 -0.103622 0.051679
-0.083953 0.051679 0.123670 0.051679 -0.083953
mesh ( tx, ty, tz);
tz = sin (r) / r;
warning: matrix singular to machine precision, rcond = 0
warning: attempting to find minimum norm solution
warning: dgelsd: rank deficient 5x5 matrix, rank = 3
xx
-8 -4 0 4 8
-8 -4 0 4 8
-8 -4 0 4 8
-8 -4 0 4 8
-8 -4 0 4 8
xx .^ 2 % element by element multipy
64 16 0 16 64
64 16 0 16 64
64 16 0 16 64
64 16 0 16 64
64 16 0 16 64
xx ^ 2 % matrix multipy
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
xx*xx % matrix multipy
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
x = -2:0.1:2;
[xx,yy] = meshgrid(x,x);
z = sin(xx.^2-yy.^2);
mesh( x,x,z);
t = 0:0.1:10*pi;
numel (t) % ans = 315
r = linspace (0, 1, numel (t));
z = linspace (0, 1, numel (t));
plot3 ( r.*sin(t), r.*cos(t), z);
x = -2:0.1:2;
[xx,yy] = meshgrid(x,x);
z = sin(xx.^2-yy.^2);
mesh( x,x,z);
clf
surfc ( xx, yy, z);
clf
contour (xx, yy, z )
clf
contour (xx, yy, z , -.5:.5)
x = -7:0.5:7;
[xx,yy] = meshgrid(x,x);
mesh( x,x,sin (2*pi*xx/10));
mesh( x,x,sin (2*pi*yy/10));
quiver (x, x, sin (2*pi*xx/10), sin (2*pi*yy/10));
====================================3D:Noise===================================
i = 4
c = 0.15
m =[] # [](0x0)
if isempty (m)
m = zeros (2);
# 0 0
# 0 0
end
k = 1
m = interp2 (m, 1)
# 0 0 0
# 0 0 0
# 0 0 0
gridmap = ones (size (m));
# 1 1 1
# 1 1 1
# 1 1 1
gridmap(1:2:end, 1:2:end) = 0;
# 0 1 0
# 1 1 1
# 0 1 0
gridmap = find(gridmap)
# 2
# 4
# 5
# 6
# 8
randmap = c * randn (size (m))
# 0.038388 -0.356312 0.098282
# -0.097543 -0.066107 0.116256
# -0.047285 -0.090405 0.143750
m(gridmap) += randmap(gridmap)
# 0.00000 -0.35631 0.00000
# -0.09754 -0.06611 0.11626
# 0.00000 -0.09040 0.00000
mesh(m)
c = c / 2 # c = 0.075000
k = 2
m = interp2 (m, 1)
# 0.00000 -0.17816 -0.35631 -0.17816 0.00000
# -0.04877 -0.12999 -0.21121 -0.07654 0.05813
# -0.09754 -0.08182 -0.06611 0.02507 0.11626
# -0.04877 -0.06351 -0.07826 -0.01006 0.05813
# 0.00000 -0.04520 -0.09040 -0.04520 0.00000
gridmap = ones (size (m));
gridmap(1:2:end, 1:2:end) = 0;
# 0 1 0 1 0
# 1 1 1 1 1
# 0 1 0 1 0
# 1 1 1 1 1
# 0 1 0 1 0
gridmap = find(gridmap) # 2 4 6 7 8 9 10 12 14 16 17 18 19 20 22 24
randmap = c * randn (size (m))
# 0.1062401 -0.2729403 0.0242680 -0.0576757 -0.0025187
# -0.0339185 0.0162471 -0.0027516 -0.0466645 0.0545292
# 0.0618160 -0.0334473 0.0243991 -0.0395248 -0.0690449
# 0.0501923 0.0508080 -0.0666404 -0.0101137 0.0403578
# -0.0205719 -0.0702958 -0.1222184 -0.1376008 -0.0987856
m(gridmap) += randmap(gridmap)
mesh(m)
function m = diamond_square (m, i, c)
if isempty (m)
m = zeros (2); # m = [ 0,0; 0,0 ]
end
for k = 1:i
m = interp2 (m, 1); # m = [ 0,0,0;0,0,0;0,0,0 ]
# Define points we want to randomize
gridmap = ones (size (m)); # [ 1,1,1;1,1,1;1,1,1 ]
gridmap(1:2:end, 1:2:end) = 0; # [ 0,1,0;1,1,1;0,1,0 ]
gridmap = find(gridmap); # Makes Octave happy gridmap= 2 4 5 6 8
randmap = c * randn (size (m)); # randmap = random 3 X 3 matrix
m(gridmap) += randmap(gridmap); # m = randmap which has zeros at the end like gridmap
c = c / 2;
end
end
mesh(m=diamond_square([], 6, 0.1)) # mesh (x, y, z) plot 3D surface
imagesc(m);
colormap(gray);
colormap ("default")
w = m;
w(w < -0.01) = -0.01;
mesh(w)
function s = perlin (m)
s = zeros(m); # output image
w = m; # width of current layer
i = 0; # iterations
while w > 3
i = i + 1;
d = interp2(randn(w), i-1, "spline");
s = s + i * d(1:m, 1:m);
w -= ceil(w/2 - 1);
end
end
n = perlin (200);
mesh(n)
imagesc(n);
function a = get_clouds (a)
## Scale a between 0 and 1
a = a - min(a(:));
a = a / max(a(:));
## Parameters
density = 0.5;
sharpness = 0.1;
a = 1 - e.^(-(a - density) * sharpness);
a(a < 0) = 0;
## Scale between 0 to 255 and quantize
a = a / max(a(:));
a = round(a * 255);
end
c = get_clouds(n);
mesh(c)
imagesc(c)
contour (c)
function c = cloud_cmap ()
c = [0.25 0.25 1];
for i = 2:256
c(i, :) = (i-2)/256 * (1 - c(2, :)) + c(2, :);
end
end
function i = pers_clouds (n)
w = size(n, 1);
c = get_clouds (n);
t = -pi/32;
P = [cos(t) sin(t) 0; -sin(t) cos(t) 0; 0.001 0.002 1];
## Perspective transformation
pc = imperspectivewarp (c/255, P, "cubic");
pw = size(pc, 1);
ph = size(pc, 2);
## Create and combine background gradient
[dump back] = meshgrid(1:ph, 1:pw);
i = pc * 4 * pw + back;
## Fit between 0 to 255 for image
i = i - min (i(:));
i = round (i / max (i(:)) * 255);
i(isnan(i)) = 0;
end
=====================AIFF:Read============================================
filename = "/Users/donsauer/Downloads/REF_SOURCE/octave/Doc/0.1uf.aiff";
fid = fopen (filename, "r");
Iff_Header = fgets (fid,4)
[File_Size, count] = fread (fid,1, "uint32", 0, "ieee-be");
File_Type = fgets (fid,4);
CommonChunk = fgets (fid,4);
[Chunk_Size, count] = fread (fid,1, "uint32", 0, "ieee-be");
[NumbChan, count] = fread (fid,1, "uint16", 0, "ieee-be");
[NumbFrame, count] = fread (fid,1, "uint32", 0, "ieee-be");
[SampSize, count] = fread (fid,1, "uint16", 0, "ieee-be");
[SampRate, count] = fread (fid,10, "uint8", 0, "ieee-be");
SSND_Chunk = fgets (fid,4);
[Chunk_Size2, count] = fread (fid,1, "uint32", 0, "ieee-be");
[Offset, count] = fread (fid,1, "uint32", 0, "ieee-be");
[Block_Size, count] = fread (fid,1, "uint32", 0, "ieee-be");
fclose (fid);
s3 ="";
s3 = strcat (s3,"HEADER ","VALUE "," \n");
s3 = strcat (s3,"IFF_HEADER ", Iff_Header," \n");
s3 = strcat (s3,"File_Size ",mat2str(File_Size)," \n");
s3 = strcat (s3,"File_Type ", File_Type," \n");
s3 = strcat (s3,"CommonChunk ", CommonChunk," \n");
s3 = strcat (s3,"Chunk_Size ",mat2str(Chunk_Size)," \n");
s3 = strcat (s3,"NumbChan ",mat2str(NumbChan)," \n");
s3 = strcat (s3,"NumbFrame ",mat2str(NumbFrame)," \n");
s3 = strcat (s3,"SampSize ",mat2str(SampSize)," \n");
s3 = strcat (s3,"SSND_Chunk ", SSND_Chunk," \n");
s3 = strcat (s3,"Chunk_Size2 ",mat2str(Chunk_Size2),"\n");
s3 = strcat (s3,"Offset ",mat2str(Offset)," \n");
s3 = strcat (s3,"Block_Size ",mat2str(Block_Size)," \n");
disp (s3);
filename = 0.1uf.aiff
file size = 910
ADDR TYPE VALUE
0 IFF_HEADER FORM
4 FILE_SIZE 902
8 TYPE_AIFF AIFF
12 COMMON_CHUNK COMM
16 CHUNK_SIZE 18
20 NUMB_CHAN 2
22 NUMB_FRAM 214
26 SAMP_SIZE 16
28 SAMP_RATE 96000
38 SSND_CHUNK SSND
42 CHUNK_SIZE 864
46 OFFSET 0
50 BLOCK_SIZE 0
0 -7348 372
1.04167e-05 -7263 303
2.08333e-05 -7196 247
3.125e-05 -7218 187
================GDSII:Read==========================================
=====================================
CODE_INFO = [
2 , 1 ; % HEADER
258, -1 ; % BGNLIB
518, 2 ; % LIBNAME
8198, 2 ; % FONTS
8706, 1 ; % GENERATIONS
773, 5 ; % UNITS
1282,-1 ; % BGNSTR
1542, 2 ; % STRNAME
2048, -1; % BOUNDARY
3330 ,1 ; % LAYER
3586, 1 ; % DATATYPE
4099, 4 ; % XY
4352, -1; % ENDEL
2304, -1; % PATH
8450, 1 ; % PATHTYPE
3843, 4 ; % WIDTH
3072, -1; % TEXT
5634, 1; % TEXTTYPE
5889, 1; % PRESENTATION
6657, 1; % STRANS
6917, 5; % MAG
6406, 2; % STRING
7173, 5; % ANGLE
2560, -1; % SREF
4614, 2; % SNAME
1792, -1; % ENDSTR
1024, -1; % ENDLIB
];
=====================================
CODE_NAME = [
"HEADER Release #" ;
"BGNLIB";
"LIBNAME = ";
"FONTS = ";
"GENERATIONS = ";
"UNITS = ";
"BGNSTR ";
"STRNAME = ";
"BOUNDARY ";
"LAYER = ";
"DATATYPE = ";
"XY = ";
"ENDEL" ;
"PATH" ;
"PATHTYPE = ";
"WIDTH = ";
"TEXT" ;
"TEXTTYPE = ";
"PRESENTATION = ";
"STRANS = ";
"MAG = ";
"STRING = ";
"ANGLE = ";
"SREF ";
"SNAME = ";
"ENDSTR" ;
"ENDLIB" ;
];
=====================================
function index = findIndex( Block_ID, CODE_INFO, CODE_NAME)
% Block_ID
[N ,LEN] = size(CODE_INFO);
i = 1;
while (i <= N)
code = CODE_INFO(i,1);
if (code == Block_ID)
index = i;
endif
i++;
endwhile
endfunction
=====================================
function Block_Value = ReadValue(fid ,Read_Type , Block_Bytes)
switch Read_Type
case 1
[Block_Value, count] = fread (fid,(Block_Bytes-4)/2, "uint16", 0, "ieee-be");
Block_Value = mat2str(Block_Value);
case 2
Block_Value = fgets (fid,Block_Bytes-4);
case 3
[Block_Value, count] = fread (fid,(Block_Bytes-4)/8, "float64", 0, "ieee-be");
Block_Value = mat2str(Block_Value);
case 4
[Block_Value, count] = fread (fid,(Block_Bytes-4)/4, "integer*4", 0, "ieee-be");
Block_Value = mat2str(Block_Value);
case 5
[Block_Value, count] = fread (fid,(Block_Bytes-4), "uint8", 0);
Fval = Real(Block_Value )
Block_Value = mat2str(Fval);
case 6
[Block_Value, count] = fread (fid,(Block_Bytes-4)/2, "uint32", 0, "ieee-be");
Block_Value = mat2str(Block_Value);
otherwise
Block_Value = fgets (fid,Block_Bytes-4);
Block_Name = " " ;
Block_Value = " ";
end
endfunction
=====================================
function Fval = Real(RealNumb)
Fval = RealNumb;
[nr, nc] = size( RealNumb);
Fval = 1:0 ;
Offset = 0;
sign = 1-2*bitand(RealNumb(1+ Offset),128);
expo = bitand(RealNumb(1 + Offset),127) -65;
Mat=0;
i = 0;
while (i <= 7)
bit = 2^(7-i);
bitscale = 2^i;
val = 8*bitand(RealNumb(2 + Offset),bit)/(bit*bitscale);
val2= 8*bitand(RealNumb(3 + Offset),2^(7-i))/(2^i*2^(7-i));
val3= 8*bitand(RealNumb(4 + Offset),2^(7-i))/(2^i*2^(7-i));
val4= 8*bitand(RealNumb(5 + Offset),2^(7-i))/(2^i*2^(7-i));
val5= 8*bitand(RealNumb(6 + Offset),2^(7-i))/(2^i*2^(7-i));
val6= 8*bitand(RealNumb(7 + Offset),2^(7-i))/(2^i*2^(7-i));
val7= 8*bitand(RealNumb(8 + Offset),2^(7-i))/(2^i*2^(7-i));
Mat = Mat +val +val2/256 +val3/256^2 +val4/256^3 +val5/256^4 +val6/256^5 +val7/256^6;
i++;
endwhile
Mat;
Fval(1) = sign*Mat*16^expo;
if (nr > 9)
Offset = 8;
sign = 1-2*bitand(RealNumb(1+ Offset),128);
expo = bitand(RealNumb(1 + Offset),127) -65;
Mat=0;
i = 0;
while (i <= 7)
bit = 2^(7-i);
bitscale = 2^i;
val = 8*bitand(RealNumb(2 + Offset),bit)/(bit*bitscale);
val2= 8*bitand(RealNumb(3 + Offset),2^(7-i))/(2^i*2^(7-i));
val3= 8*bitand(RealNumb(4 + Offset),2^(7-i))/(2^i*2^(7-i));
val4= 8*bitand(RealNumb(5 + Offset),2^(7-i))/(2^i*2^(7-i));
val5= 8*bitand(RealNumb(6 + Offset),2^(7-i))/(2^i*2^(7-i));
val6= 8*bitand(RealNumb(7 + Offset),2^(7-i))/(2^i*2^(7-i));
val7= 8*bitand(RealNumb(8 + Offset),2^(7-i))/(2^i*2^(7-i));
Mat = Mat +val +val2/256 +val3/256^2 +val4/256^3 +val5/256^4 +val6/256^5 +val7/256^6;
i++;
endwhile
Fval(2) = sign*Mat*16^expo;
endif
endfunction
=====================================
filename = "/Users/donsauer/Downloads/REF_SOURCE/octave/Doc/M1TEST.SF";
s3 = "";
fid = fopen (filename, "r");
i = 0;
while ( i <66 )
place = ftell (fid);
[Block_Bytes, count] = fread (fid,1, "uint16", 0, "ieee-be");
[Block_ID, count] = fread (fid,1, "uint16", 0, "ieee-be");
index = findIndex( Block_ID, CODE_INFO, CODE_NAME);
Block_Name = CODE_NAME(index,:);
Read_Type = CODE_INFO(index,2);
Block_Value = ReadValue(fid ,Read_Type , Block_Bytes);
s3 = strcat (s3,mat2str(place)," ",mat2str(Block_Bytes)," ",mat2str(Block_ID)," ",Block_Name,Block_Value,"\n");
i = i+1;
endwhile
fclose (fid);
disp (s3);
=====================================
0 6 2 HEADER Release #3
6 28 258 BGNLIB
34 14 518 LIBNAME = M1TEST.DB^@
48 180 8198 FONTS = GDSII:CALMAFONT.TX
228 6 8706 GENERATIONS = 3
234 20 773 UNITS = [0.001,9.9999999999999986e-10]
254 28 1282 BGNSTR
282 10 1542 STRNAME = M1TEST
292 4 2048 BOUNDARY
296 6 3330 LAYER = 8
302 6 3586 DATATYPE = 0
308 44 4099 XY = [-23100;32500;-23100;12500;-6400;12500;-6400;32500;-23100;32500]
352 4 4352 ENDEL
356 4 2304 PATH
360 6 3330 LAYER = 8
366 6 3586 DATATYPE = 0
372 6 8450 PATHTYPE = 2
378 8 3843 WIDTH = 1400
386 36 4099 XY = [-12100;23000;25600;23000;25600;11700;40400;11700]
422 4 4352 ENDEL
426 4 3072 TEXT
430 6 3330 LAYER = 8
436 6 5634 TEXTTYPE = 0
442 6 5889 PRESENTATION = 8
448 6 6657 STRANS = 0
454 12 6917 MAG = 3
466 12 4099 XY = [-17000;25400]
478 8 6406 STRING = VCC^@
486 4 4352 ENDEL
490 4 3072 TEXT
494 6 3330 LAYER = 8
500 6 5634 TEXTTYPE = 0
506 6 5889 PRESENTATION = 8
512 6 6657 STRANS = 0
518 12 6917 MAG = 2
530 12 7173 ANGLE = 90
542 12 4099 XY = [-18700;14400]
554 8 6406 STRING = VCC2
562 4 4352 ENDEL
566 4 2560 SREF
570 12 4614 SNAME = AT_ZERO^@
582 6 6657 STRANS = 0
588 12 4099 XY = [0;0]
600 4 4352 ENDEL
604 4 2304 PATH
608 6 3330 LAYER = 10
614 6 3586 DATATYPE = 0
620 6 8450 PATHTYPE = 2
626 8 3843 WIDTH = 1800
634 28 4099 XY = [-700;5900;6200;5900;6200;-5300]
662 4 4352 ENDEL
666 4 1792 ENDSTR
670 28 1282 BGNSTR
698 12 1542 STRNAME = AT_ZERO
710 4 2048 BOUNDARY
714 6 3330 LAYER = 50
720 6 3586 DATATYPE = 1
726 44 4099 XY = [-1000;0;-1000;-1000;0;-1000;0;0;-1000;0]
770 4 4352 ENDEL
774 4 2048 BOUNDARY
778 6 3330 LAYER = 50
784 6 3586 DATATYPE = 1
790 44 4099 XY = [0;1000;0;0;1000;0;1000;1000;0;1000]
834 4 4352 ENDEL
838 4 1792 ENDSTR
842 4 1024 ENDLIB
HEADER 0002
BGNLIB 0102
LIBNAME 0206
REFLIBS 1F06 2 45-character ASCII strings
FONTS 2006 4 44-character ASCII strings
ATTRTABLE 2306 44-character ASCII string
GENERATIONS 2202 2-byte integer
FORMAT 3602 2-byte integer
MASK 3706 ASCII string
ENDMASKS 3800 No data
UNITS 0305 2 8-byte floats
ENDLIB 0400 No data
BGNSTR 0502 12 2-byte integers
STRNAME 0606 Up to 32-characters ASCII string
ENDSTR 0700 No data
BOUNDARY 0800 No data
PATH 0900 No data
SREF 0A00 No data
AREF 0B00 No data
TEXT 0C00 No data
NODE 1500 No data
BOX 2D00 No data
ELFLAGS 2601 2-byte integer
PLEX 2F03 4-byte integer
LAYER 0D02 2-byte integers
DATATYPE 0E02 2-byte integer
XY 1003 Up to 200 4-byte integer pairs
PATHTYPE 2102 2-byte integer
WIDTH 0F03 4-byte integer
SNAME 1206 Up to 32-character ASCII string
STRANS 1A01 2-byte integer
MAG 1B05 8-byte float
ANGLE 1C05 8-byte float
COLROW 1302 2 2-byte integers
TEXTTYPE 1602 2-byte integer
PRESENTATION 1701 2-byte integer
ASCII STRING 1906 Up to 512-character string
NODETYPE 2A02 2-byte integer
BOXTYPE 2E02 2-byte integer
=================== AudioPict:Read:Write ===========================
filename = "/Users/donsauer/Downloads/REF_SOURCE/octave/Doc/test.wav";
[y, Fs, bits] = wavread (filename);
Fs % Fs = 44100
bits % bits = 16
plot (y)
playaudio (y) sh: /dev/dsp: Permission denied
xmin = 0;
xmax = 10000;
ymin = -.6;
ymax = .6;
axis([xmin, xmax, ymin, ymax]);
step = 1/22050
z = .5*sin(2*pi*90*[0:step:1]);
plot (z)
fs = 22050 % Fs = 44100/2
bits = 8 % bits = 16/2
filename2 = "/Users/donsauer/Downloads/REF_SOURCE/octave/Doc/test2.wav";
wavwrite (z, fs, bits, filename2)
filename = "/Users/donsauer/Downloads/T2.ppm";
gmap
=
gray (256);
[x, y ]
= meshgrid
(0:1:128);
z
= x + y;
saveimage
(filename , z, "ppm", gmap
);
% can view on gimp
mesh
(z);