-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathZernike.m
66 lines (63 loc) · 2.05 KB
/
Zernike.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function zern = Zernike(r,t,n,m)
% Returns a matrix corresponding to the Zernike polynomial of radial
% degree n and azimuthal degree m.
% Matrices r and t correspond to the radii and angles of points on an
% x-y grid satisfying 1>x>-1 and 1>y>-1; they are obtained via
% cart2pol. See example for clarification.
% Must satisfy n - m >= 0, n - m even, and n >= 0. n and m are
% both integers.
% Function elliptical_crop should be used in this context to ignore all
% data outside of the unit circle.
%
% Example 1:
%
% % Display the 'power' Zernike polynomial (n = 2, m = 0) over a
% % 100x100 grid.
%
% x=linspace(-1,1,100);
% y=linspace(1,-1,100);
% [x,y] = meshgrid(x,y);
% [t,r] = cart2pol(x,y);
% z_power = zernike(r,t,2,0);
% figure, imagesc(elliptical_crop(z_power,1));
% colormap jet;
%
% Example 2:
%
% % Display all Zernike polynomials up to radial degree 4 over a
% % 100x100 grid.
%
% for n = 0:4
% for m = -n:2:n
% x=linspace(-1,1,100);
% y=linspace(1,-1,100);
% [x,y] = meshgrid(x,y);
% [t,r] = cart2pol(x,y);
% zern_mat = zernike(r,t,n,m);
% figure, imagesc(elliptical_crop(zern_mat,1));
% title(strcat('n = ',{' '},string(n),', m = ',{' '},string(m)));
% colormap jet;
% end
% end
%
%
% Functions required for use: elliptical_crop, zernike_radial
% See also: zernike_moments, zernike_recreation
%
% Evan Czako, 8.14.2019
% -------------------------------------------
if mod(n-m,2) == 1
error('n-m must be even');
end
if n < 0
error('n must both be positive')
end
if floor(n) ~= n || floor(m) ~= m
error('n and m must both be integers')
end
if m < 0
zern = -ZernikeRadial(r,n,-m).*sin(m*t);
else
zern = ZernikeRadial(r,n,m).*cos(m*t);
end
end