% fig_alias_sweep_2d.m
% illustrate aliasing by sweeping frequencies from 0 to 1

ir_fontsize reset
ir_fontsize title 16
ir_fontsize label 16

d = 32;
x = linspace(0, d, 2^8-1);
y = linspace(0, d, 2^8);
[xx yy] = ndgrid(x,y);

%g = sin(2*pi*(x/4 + x.^2/4/d));
%plot(x, g)

%f0 = 0;
f1 = 1;

%pf = @(x,y) (f0*x + (f1-f0)/d*x.^2/2) +  (f0*y + (f1-f0)/d*y.^2/2);
%gf = @(x,y) cos(2*pi*pf(x,y));

gr = @(r) cos(2 * pi * f1/d*r.^2/2);
%gr = @(r) cos(2 * pi * r.^2/64);

rr = sqrt(xx.^2 + yy.^2);
%g = gf(xx,yy);
g = gr(rr);

tf = @(x, y, s) text(x*d, y*d, s, ...
	'color', 'magenta', 'fontsize', 17, 'fontweight', 'bold');

im plc 1 2
im(1, x, y, g), axis image
xtick([0 d])
ytick([0 d])
xlabelf('$x$')
ylabelf('$y$')
titlef('$g_a(x,y)$ for sweep in $x$ and $y$')

tf(0.2, 0.8, '2')
tf(0.8, 0.8, '1')
tf(0.2, 0.2, '3')
tf(0.8, 0.2, '4')

% ir_savefig cw fig_alias_sweep_2a

clf
im subplot 2
n = 0:d; m = n;
[mm nn] = ndgrid(m, n);
%gd = gf(mm,nn);
gd = gr(sqrt(mm.^2 + nn.^2));
im(m, n, gd)
xtick([0 d])
ytick([0 d])
xlabelf('$m$')
ylabelf('$n$')
titlef('\gdmn samples')

tf(0.2, 0.8, '2')
tf(0.8, 0.8, '1')
tf(0.2, 0.2, '3')
tf(0.8, 0.2, '4')

% ir_savefig cw fig_alias_sweep_2d
