function z = zernpol(n,m,r,nflag) iB[>uW
%ZERNPOL Radial Zernike polynomials of order N and frequency M. >
Y
<in/
% Z = ZERNPOL(N,M,R) returns the radial Zernike polynomials of V[-4cu,Ph^
% order N and frequency M, evaluated at R. N is a vector of JcsJfTI
% positive integers (including 0), and M is a vector with the Qq;` 9-&j
% same number of elements as N. Each element k of M must be a TRwlUC3hQ
% positive integer, with possible values M(k) = 0,2,4,...,N(k) M17oAVN7D
% for N(k) even, and M(k) = 1,3,5,...,N(k) for N(k) odd. R is Z$R6'EUb1
% a vector of numbers between 0 and 1. The output Z is a matrix NG-Wn+W@b
% with one column for every (N,M) pair, and one row for every ]3tg|?%B
% element in R. .Ap-<FB
% ,1'9l)zP
% Z = ZERNPOL(N,M,R,'norm') returns the normalized Zernike poly- ~F8M_
% nomials. The normalization factor Nnm = sqrt(2*(n+1)) is )Lht}I ]:
% chosen so that the integral of (r * [Znm(r)]^2) from r=0 to Ov1$7 r@
% r=1 is unity. For the non-normalized polynomials, Znm(r=1)=1 ]>fAV(ix
% for all [n,m]. tx}}Kd
% %4#,y(dO
% The radial Zernike polynomials are the radial portion of the NvH9?Ek"
% Zernike functions, which are an orthogonal basis on the unit wjk-$p
% circle. The series representation of the radial Zernike hzIP ?0^E
% polynomials is 7.fpGzUM
% 4`lt 4L
% (n-m)/2 ;K<e]RI;?
% __ 5Hvg%g-c
% m \ s n-2s f}q4~NPn-
% Z(r) = /__ (-1) [(n-s)!/(s!((n-m)/2-s)!((n+m)/2-s)!)] * r |4uH
% n s=0 ( lbF/F>v
% 1@Dp<Q
% The following table shows the first 12 polynomials. !g}?x3
% tydD~a
% n m Zernike polynomial Normalization hS]g^S==2h
% --------------------------------------------- 2XhtK
% 0 0 1 sqrt(2) yidUtSv=,
% 1 1 r 2 Az4+([
% 2 0 2*r^2 - 1 sqrt(6) `ER">@&
% 2 2 r^2 sqrt(6) WAPN,WuW
% 3 1 3*r^3 - 2*r sqrt(8) VXt8y)?a
% 3 3 r^3 sqrt(8) fl| 8#\r
% 4 0 6*r^4 - 6*r^2 + 1 sqrt(10) ;V(- ;O
% 4 2 4*r^4 - 3*r^2 sqrt(10) T^LpoN/T
% 4 4 r^4 sqrt(10) X|+ o4R?
% 5 1 10*r^5 - 12*r^3 + 3*r sqrt(12) n<
UuVu
% 5 3 5*r^5 - 4*r^3 sqrt(12) Wbo{v r[2+
% 5 5 r^5 sqrt(12) cIqk=_]
% --------------------------------------------- _DPWp,k<~
%
4\'1j|nS[
% Example: D=}UKd
% q)%F#g
% % Display three example Zernike radial polynomials utIR\e#:B
% r = 0:0.01:1; /BB(riG
% n = [3 2 5]; :3`6P:^
% m = [1 2 1]; FqQqjA
% z = zernpol(n,m,r); XWH{+c"
% figure #<ppiu$
% plot(r,z) &YQ
% grid on :;[pl|}tM
% legend('Z_3^1(r)','Z_2^2(r)','Z_5^1(r)','Location','NorthWest') Ay7I_"%
% ,ra!O=d~0
% See also ZERNFUN, ZERNFUN2. ,~^0AtLv
`"CIy_m
% A note on the algorithm. ~`'!nzP5H
% ------------------------ x]
[/9e
% The radial Zernike polynomials are computed using the series RlH|G
% representation shown in the Help section above. For many special 0* Ox>O>
% functions, direct evaluation using the series representation can w,hl<=:(FB
% produce poor numerical results (floating point errors), because P:GAJ->;]>
% the summation often involves computing small differences between $OI 6^
% large successive terms in the series. (In such cases, the functions |l\&4/SJ
% are often evaluated using alternative methods such as recurrence 2=Sv#
% relations: see the Legendre functions, for example). For the Zernike eF]`?AeWQ
% polynomials, however, this problem does not arise, because the }SL&Y `Y]
% polynomials are evaluated over the finite domain r = (0,1), and VO#x+u]/
% because the coefficients for a given polynomial are generally all g)7~vm2/,
% of similar magnitude. l,cnMr^.W
% 8U,VpuQ:
% ZERNPOL has been written using a vectorized implementation: multiple Rf *we+
% Zernike polynomials can be computed (i.e., multiple sets of [N,M] y V=Ku
% values can be passed as inputs) for a vector of points R. To achieve gO>XNXN{
% this vectorization most efficiently, the algorithm in ZERNPOL +/u)/ey
% involves pre-determining all the powers p of R that are required to N ] KS\
% compute the outputs, and then compiling the {R^p} into a single \Fd6Q_
% matrix. This avoids any redundant computation of the R^p, and IXE`MLc
% minimizes the sizes of certain intermediate variables. VyQ@. Lm
% :
utY4
% Paul Fricker 11/13/2006 ;pk4Voo$
uSnG= tB
Y+il>.Z
% Check and prepare the inputs: ><<(6
% ----------------------------- ,;3#}OGg
if ( ~any(size(n)==1) ) || ( ~any(size(m)==1) ) y|r+<
error('zernpol:NMvectors','N and M must be vectors.') 4n55{?Z
end i?+ZrAx>
ZL!,s#
if length(n)~=length(m) Z)
nB
error('zernpol:NMlength','N and M must be the same length.') pq8XCOllXx
end 5u/d r9n
5%H(AaG*q
n = n(:); <2b&AF{En
m = m(:); O~3<P3W
length_n = length(n); ?HD(EGdx
6T-h("t
if any(mod(n-m,2)) m|K"I3W$
error('zernpol:NMmultiplesof2','All N and M must differ by multiples of 2 (including 0).') xBba&A]=
end ,1xX`:
JQ5E; 8J>
if any(m<0) i.QS(gM
error('zernpol:Mpositive','All M must be positive.') EPEy60Rx5
end X`-7: !+
R]dN-'U
if any(m>n) Ck`-<)uN
error('zernpol:MlessthanN','Each M must be less than or equal to its corresponding N.') 2o8:[3C5
end 9;W2zcN
@zu IR0Gr)
if any( r>1 | r<0 ) L7kNQ/
error('zernpol:Rlessthan1','All R must be between 0 and 1.') .h({ P#QT
end VU8EjuOetb
"LwLTPC2
if ~any(size(r)==1) irjOGn
error('zernpol:Rvector','R must be a vector.') 6JrwPZB
end ALcin))+B
UCu0Xqf
r = r(:); ++9?LH4S4
length_r = length(r); W=E+/ZvPt
Q#k Sp8
if nargin==4 Iax-~{B3AY
isnorm = ischar(nflag) & strcmpi(nflag,'norm'); *R'r=C`
if ~isnorm F747K);_
error('zernpol:normalization','Unrecognized normalization flag.') d_v]mfUF
end 6XP>qI,AJ
else w\V1pu^6@
isnorm = false; 0o2*X|i(
end s<z`<^hRe
+ 6noQYe
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% WD/\f$4
% Compute the Zernike Polynomials ]izrr
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% }Z="}Dg|T
;s*
% Determine the required powers of r: 9f(0
qa
% ----------------------------------- HZASIsl
rpowers = []; <QuIX A
for j = 1:length(n) _+sb~
rpowers = [rpowers m(j):2:n(j)]; }l>\D~:M
end rCK
rpowers = unique(rpowers); N F$k~r
64LX[8Ax#
% Pre-compute the values of r raised to the required powers, W)X" G3
% and compile them in a matrix: &9+]{jXF
% ----------------------------- H^Mfj!S
if rpowers(1)==0 xNrPj8V<Y
rpowern = arrayfun(@(p)r.^p,rpowers(2:end),'UniformOutput',false); lQS(\}N
rpowern = cat(2,rpowern{:}); ;\]&k
rpowern = [ones(length_r,1) rpowern]; bUzo> fm_
else @:dn\{Zsea
rpowern = arrayfun(@(p)r.^p,rpowers,'UniformOutput',false); Gye84C2E=
rpowern = cat(2,rpowern{:}); aM7e?.rU
end SD/=e3
]8n*f o2#
% Compute the values of the polynomials: @=7[ KM b
% -------------------------------------- f};RtRo2
z = zeros(length_r,length_n); (U{,D1?
for j = 1:length_n R7o'V* d
s = 0:(n(j)-m(j))/2; ]/9@^D}&
pows = n(j):-2:m(j); YujR}=B!/
for k = length(s):-1:1 "[QQ(]={
p = (1-2*mod(s(k),2))* ... -lY,lC>{
prod(2:(n(j)-s(k)))/ ... -Qy@-s $
prod(2:s(k))/ ... a
Xn:hn~O
prod(2:((n(j)-m(j))/2-s(k)))/ ... !+k);;.+
prod(2:((n(j)+m(j))/2-s(k))); QO/nUl0E
idx = (pows(k)==rpowers); :'
=le*h
z(:,j) = z(:,j) + p*rpowern(:,idx); }6'%p Bd
end {e+}jZ[L
_v#Vf*#
if isnorm 9bQD"%ha=d
z(:,j) = z(:,j)*sqrt(2*(n(j)+1)); &wX568o
end %A3ci[$g
end ynZp|'b?<
p
uZY4}b_
% EOF zernpol