{
   "labels" : [
      "gui",
      "theory",
      "fourier",
      "transform",
      "sampling theorem",
      "oversampling",
      "userview"
   ],
   "is_private" : null,
   "id" : "1-57j",
   "code" : "// Set 'x' to the input waveform\r\n(\r\nvar stream = Pstutter(64, Pseq([1, -1], inf))/*.drop(16)*/.asStream;\r\nx = Signal.fill(128, stream);\r\nx.plot;\r\n)\r\n\r\n// Then run this whole block\r\n(\r\nvar factor = 8,\r\nwaitTime = 0.3,\r\nscale = 0.3,\r\n\r\nsigSize = x.size * factor,\r\nsig,\r\nintermediateSig = nil, interp = 0,\r\nw = Window(\"inverse Fourier\", Rect(400, 200, 800, 600)).front,\r\nm, uv, r, fftSize, halfSize;\r\nvar indexRange, indexLNum, indexRNum, runButton, stepButton,\r\nrangeSpec,\r\nrangeFunc = { |view| [rangeSpec.map(view.lo).asInteger, rangeSpec.map(view.hi).asInteger] };\r\n\r\nvar allCosines;\r\n\r\ny = x.fft(Signal.newClear(x.size), Signal.fftCosTable(x.size));\r\ny = y.asPolar;\r\n\r\nfftSize = y.rho.size;\r\nhalfSize = fftSize div: 2;\r\nrangeSpec = [0, halfSize, \\lin, 1, 0].asSpec;\r\n\r\nallCosines = Array.fill(fftSize div: 2 + 1, { |j|\r\n\tvar mag = y.rho[j], phase = y.theta[j],\r\n\tmagScale = mag / fftSize, phaseScale = 2pi * j / sigSize;\r\n\tif(j > 0 and: { j < (fftSize div: 2) } ) { magScale = magScale * 2 };\r\n\tSignal.fill(sigSize, { |i|\r\n\t\tcos((i * phaseScale) + phase) * magScale;\r\n\t});\r\n});\r\n\r\nsig = allCosines[0];\r\n\r\nw.layout = VLayout(\r\n\tuv = UserView(),\r\n\tView().maxHeight_(60).layout_(\r\n\t\tVLayout(\r\n\t\t\tHLayout(\r\n\t\t\t\tindexLNum = NumberBox().fixedWidth_(80), // .fixedSize_(Size(80, 20)),\r\n\t\t\t\tindexRNum = NumberBox().fixedWidth_(80), // .fixedSize_(Size(80, 20)),\r\n\t\t\t\tindexRange = RangeSlider().orientation_(\\horizontal)\r\n\t\t\t).margins_(2),\r\n\t\t\t// indexView = LayoutValueSlider(initValue: 0, spec: [0, fftSize div: 2, \\lin, 1, 0]),\r\n\t\t\tHLayout(\r\n\t\t\t\tnil,\r\n\t\t\t\trunButton = Button().fixedWidth_(80),\r\n\t\t\t\tstepButton = Button().fixedWidth_(80),\r\n\t\t\t\tnil\r\n\t\t\t).margins_(2)\r\n\t\t).margins_(2)\r\n\t)\r\n);  // display all\r\n\r\nrunButton.states_([[\"stopped\"], [\"running\", Color.black, Color(0.7, 1, 0.7)]])\r\n.action_({ |view|\r\n\tif(view.value > 0) {\r\n\t\t// indexView.enabled = false;\r\n\t\t[indexRange, indexLNum, indexRNum].do(_.enabled = false);\r\n\t\tr.play;\r\n\t} {\r\n\t\t// indexView.enabled = true;\r\n\t\t[indexRange, indexLNum, indexRNum].do(_.enabled = true);\r\n\t\tr.stop;\r\n\t};\r\n});\r\n\r\nstepButton.states_([[\"step\"]])\r\n.action_({ |view|\r\n\tvar lo, hi;\r\n\t#lo, hi = rangeFunc.(indexRange);\r\n\tif(hi < (fftSize div: 2)) {\r\n\t\thi = hi + 1;\r\n\t\t// j = indexView.hi.asInteger;\r\n\t\tif(y.rho[hi] < 0.0001) {\r\n\t\t\tsig = allCosines[lo .. hi].sum;\r\n\t\t\tindexRange.hi = rangeSpec.unmap(hi);\r\n\t\t\tindexRNum.value = hi;\r\n\t\t\tuv.refresh;\r\n\t\t} {\r\n\t\t\t{\r\n\t\t\t\tindexRange.hi = rangeSpec.unmap(hi);\r\n\t\t\t\tindexRNum.value = hi;\r\n\t\t\t\t[indexRange, indexLNum, indexRNum].do(_.enabled = false);\r\n\t\t\t\t// indexRange.enabled = false;\r\n\t\t\t\tview.enabled = false;\r\n\t\t\t\tintermediateSig = allCosines[hi];\r\n\t\t\t\tforBy(0.0, 1.0, 0.01, { |frac|\r\n\t\t\t\t\tinterp = frac;\r\n\t\t\t\t\tuv.refresh;\r\n\t\t\t\t\tif(frac == 0.0) { 0.5.wait } { 0.02.wait };\r\n\t\t\t\t});\r\n\t\t\t\tintermediateSig = nil;\r\n\t\t\t\tsig = allCosines[lo .. hi].sum;\r\n\t\t\t\tuv.refresh;\r\n\t\t\t\tview.enabled_(true).focus(true);\r\n\t\t\t\t[indexRange, indexLNum, indexRNum].do(_.enabled = true);\r\n\t\t\t\t// indexView.enabled = true;\r\n\t\t\t}.fork(AppClock);\r\n\t\t};\r\n\t};\r\n});\r\n\r\nindexRange.action_({ |view|\r\n\tvar lo, hi;\r\n\t#lo, hi = rangeFunc.(view);\r\n\tsig = allCosines[lo .. hi].sum;\r\n\tuv.refresh;\r\n\tindexLNum.value = lo;\r\n\tindexRNum.value = hi;\r\n}).setSpan(0, 0);\r\n\r\nindexLNum.action = { |view|\r\n\tvar v = view.value;\r\n\tif(v.inclusivelyBetween(0, indexRNum.value)) {\r\n\t\tindexRange.activeLo_(rangeSpec.unmap(v));\r\n\t};\r\n};\r\n\r\nindexRNum.action = { |view|\r\n\tvar v = view.value;\r\n\tif(v.inclusivelyBetween(indexLNum.value, halfSize)) {\r\n\t\tindexRange.activeHi_(rangeSpec.unmap(v));\r\n\t};\r\n};\r\n\r\n\r\nuv.drawFunc = { |view|\r\n\tvar bounds = view.bounds,\r\n\theight = bounds.height, width = bounds.width,\r\n\tscaleY = height * (0.5 - scale), scaleX;\r\n\tPen.color_(Color.gray(0.7))\r\n\t.moveTo(Point(0, scaleY)).lineTo(Point(width, scaleY))\r\n\t.moveTo(Point(0, height - scaleY)).lineTo(Point(width, height - scaleY))\r\n\t.moveTo(Point(0, height * 0.5)).lineTo(Point(width, height * 0.5))\r\n\t.stroke;\r\n\tif(factor > 1) {\r\n\t\tscaleX = width / x.size;\r\n\t\tx.size.do { |i|\r\n\t\t\ti = i * scaleX;\r\n\t\t\tPen.moveTo(Point(i, scaleY)).lineTo(Point(i, scaleY - 6))\r\n\t\t\t.moveTo(Point(i, height - scaleY)).lineTo(Point(i, height - scaleY + 6))\r\n\t\t\t.moveTo(Point(i, height * 0.5 - 4)).lineTo(Point(i, height * 0.5 + 4))\r\n\t\t\t.stroke;\r\n\t\t};\r\n\t};\r\n\tPen.color_(Color.white);\r\n\tscaleX = width / sigSize;\r\n\tsig.do { |y, i|\r\n\t\tif(intermediateSig.notNil) {\r\n\t\t\ty = y + (intermediateSig[i] * interp);\r\n\t\t};\r\n\t\ty = height * (0.5 - (y * scale));\r\n\t\ti = i * scaleX;\r\n\t\tif(i == 0) { Pen.moveTo(Point(i, y)) } { Pen.lineTo(Point(i, y)) };\r\n\t};\r\n\tPen.stroke;\r\n\tif(intermediateSig.notNil) {\r\n\t\tPen.color_(Color.gray(0.7));\r\n\t\tintermediateSig.do { |y, i|\r\n\t\t\ty = blend(y, sig[i] + y, interp);\r\n\t\t\ty = height * (0.5 - (y * scale));\r\n\t\t\ti = i * scaleX;\r\n\t\t\tif(i == 0) { Pen.moveTo(Point(i, y)) } { Pen.lineTo(Point(i, y)) };\r\n\t\t};\r\n\t\tPen.stroke;\r\n\t};\r\n};\r\nuv.refresh;\r\n\r\nr = Task({\r\n\tvar halfSize = y.rho.size div: 2, lo, hi;\r\n\t#lo, hi = rangeFunc.(indexRange);\r\n\twhile { hi < halfSize } {\r\n\t\thi = hi + 1;\r\n\t\tindexRange.hi = rangeSpec.unmap(hi);\r\n\t\tindexRNum.value = hi;\r\n\t\tsig = allCosines[lo .. hi].sum;\r\n\t\tuv.refresh;\r\n\t\twaitTime.wait;\r\n\t};\r\n\trunButton.value = 0;\r\n}, AppClock);\r\n\r\nw.onClose = { r.stop };\r\n)",
   "name" : "FFT additive oversampling (graphical demo of the sampling theorem)",
   "author" : "jamshark70",
   "ancestor_list" : [],
   "description" : "I originally wrote this to demonstrate what sampled audio really represents -- that is, if a series of samples represents the one and only band-limited function that passes through the sampled values, we could obtain the band-limited function by adding up the cosines given by a Fourier transform. Further, doing it additively, we could select ranges of frequencies and see, interactively, each frequency band's influence on the final waveform.\r\n\r\n- The Gibbs effect is obviously visible for any sequences of samples that have discontinuities in the value or slope (e.g. non-bandlimited sawtooth or pulse waves).\r\n\r\nx = Env([0, 0.75, -1, 1, 0], [0.1, 0.01, 0.4, 0.2]).discretize(128);\r\n\r\n- Inter-sample distortion is clearly visible for 0 dBFS pulse waves.\r\n\r\n(\r\nvar stream = Pstutter(Pseq([24, 8], inf), Pseq([1, -1], inf)).asStream;\r\nx = Signal.fill(128, stream);\r\nx.plot;\r\n)\r\n\r\n- If you use a rectangular window and the window can't play continuously as a cycle, there will be a discontinuity from the end of the window to the beginning. The Gibbs effect is obvious here, too. This is good to demonstrate to students why phase vocoders should pretty much always use a windowing function (e.g. Hanning).\r\n\r\nx = Signal.fill(128, { |i| sin(i / 128 * 2pi * 1.1) });\r\n\r\n- Try lots of input signals. It's quite dramatic how the partials reinforce each other in the right places, and cancel in the right places, and always add up.\r\n\r\nBTW this example has a lot of UserView tricks. Note, for example, that to do the animation, I had to set a state variable *outside* the scope of the drawFunc, and 'refresh' the UserView to update the frame.\r\n\r\nUsage:\r\n\r\n1. Set 'x' to a Signal containing 128 or 256 values.\r\n\r\n2. Run the long code block.\r\n\r\n3. The range slider chooses a band of frequencies to include -- the audio equivalent is a pair of PV_BrickWall filters.\r\n\r\n4. The left-hand button will add partials at timed intervals.\r\n\r\n5. The right-hand button will add one partial, and animate the way that the new partial \"bends\" the waveform. This is *really* instructive!"
}
