// title: Visual demonstration of aliasing/foldover // author: jamshark70 // description: // 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. // code: ( 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; )