«Visual demonstration of aliasing/foldover» by jamshark70

on 31 Aug'11 10:49 in educationalguitheorynyquistaliasingpengraphic

I needed to demonstrate the importance of the Nyquist frequency to a class, so I wrote this visual, interactive demonstration. Use the slider at the bottom to set the frequency -- 1.0 corresponds to one cycle shown in the window.

Tested with Qt and Swing GUIs. Should work in cocoa, but not tested.

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
(
var screen = Window.screenBounds,
	height = (screen.height * 0.8).asInteger,
	width = (screen.width * 0.8).asInteger,
	win = Window(\aliasing, Rect.aboutPoint(screen.center, width / 2, height / 2 + 20)),
	sinPts = 400, sampPts = 20,
	freq = 1, fsl,
	sinColor = Color.red, sampColor = Color.black;

win.drawHook = {
	var pt;
	pt = Point(0, height/2);
	Pen.color_(sampColor)
		.moveTo(pt);
	(1..sampPts).do { |x|
		Pen.moveTo(pt);
		pt = Point(x * (width / sampPts), sin(x * freq / sampPts * 2pi).linlin(-1, 1, height, 0));
		Pen.lineTo(pt).stroke
			.fillRect(Rect.aboutPoint(pt, 3, 3));		
	};

	Pen.color_(sinColor)
		.moveTo(Point(0, height/2));
	(1..sinPts).do { |x|
		Pen.lineTo(Point(
			x * (width / sinPts),
			sin(x * freq / sinPts * 2pi).linlin(-1, 1, height, 0)
		));
	};
	Pen.stroke;
};

fsl = EZSlider(win, Rect(5, height+10, width-10, 20), "freq:", [1, 20], { |view| freq = view.value; win.refresh }, 1, initAction: true);

win.front;
)
raw 985 chars (focus & ctrl+a+c to copy)
comments
wraaaawr user 01 Sep'11 01:23

thanx a lot!

jnarveson user 15 Jan'14 18:30

Elegant.