{
   "code" : "(\r\n// a slightly modified version of the excellent example patch by James Harkins.\r\n// I added some grid lines for the samples, an adjustable sampling rate, and changed some colors.\r\n// Jascha Narveson\r\n\r\nvar screen = Window.screenBounds,\r\n\theight = (screen.height - 120).asInteger,\r\n\twidth = (screen.width - 120).asInteger,\r\nwin = Window(\\aliasing, Rect.aboutPoint(screen.center, width / 2, height / 2 + 60)).background_(Color.white),\r\n\tsinPts = 400, sampPts = 20, sr,\r\n\tfreq = 1, fsl,\r\n\tsinColor = Color.red, sampColor = Color.blue, lineColor = Color.grey;\r\n\r\nwin.drawHook = {\r\n\tvar pt;\r\n\tpt = Point(0, height/2);\r\n\tPen.color_(sampColor)\r\n\t\t.moveTo(pt);\r\n\t(1..sampPts).do { |x|\r\n\t\tPen.moveTo(pt);\r\n\t\tpt = Point(x * (width / sampPts), sin(x * freq / sampPts * 2pi).linlin(-1, 1, height, 0));\r\n\t\tPen.lineTo(pt).stroke\r\n\t\t\t.fillRect(Rect.aboutPoint(pt, 3, 3));\r\n\t};\r\n\r\n\tPen.color_(sinColor)\r\n\t\t.moveTo(Point(0, height/2));\r\n\t(1..sinPts).do { |x|\r\n\t\tPen.lineTo(Point(\r\n\t\t\tx * (width / sinPts),\r\n\t\t\tsin(x * freq / sinPts * 2pi).linlin(-1, 1, height, 0)\r\n\t\t));\r\n\t};\r\n\r\n\tPen.stroke;\r\n\r\n\tPen.color_(lineColor);\r\n\t(1..sinPts).do({|x|\r\n\t\tvar xpos = x * (width / sampPts);\r\n\t\tPen.moveTo(Point(xpos, 0));\r\n\t\tPen.lineTo(Point(xpos, height));\r\n\t\tPen.stroke;\r\n\t});\r\n\r\n};\r\n\r\nfsl = EZSlider(\r\n\twin,\r\n\tRect(5, height + 10, width-10, 20),\r\n\t\"freq of input wave (red):\",\r\n\tControlSpec(1, 40, 'lin', 0, 20),\r\n\t{|slider| freq = slider.value; win.refresh },\r\n\t1,\r\n\tlabelWidth: 200,\r\n\tinitAction: true\r\n);\r\n\r\nsr = EZSlider(\r\n\twin,\r\n\tRect(5, height + 30, width-10, 20),\r\n\t\"samp rate (output wave in blue):\",\r\n\tControlSpec(1, 44, 'lin', 1, 20),\r\n\t{|slider| sampPts = slider.value; win.refresh },\r\n\t1,\r\n\tlabelWidth: 200,\r\n\tinitAction: true\r\n);\r\nwin.front;\r\n)",
   "is_private" : null,
   "id" : "1-4VA",
   "labels" : [
      "informative",
      "educational",
      "nyquist"
   ],
   "ancestor_list" : [],
   "description" : "James Harkins already posted already excellent little visualizer for illustrating the importance of the Nyquist frequency - I just wanted to add a slider for variable sample rate, and some grid lines that makes it easier for me to see the sampling rate.",
   "name" : "Visual demonstration of aliasing/foldover (modified from James Harkins)",
   "author" : "jnarveson"
}
