supernova: fixes for boost-1.49 and gcc-4.7
[supercollider.git] / Help / UGens / FFT / UnpackFFT.html
blob2eb51000fe2c3016b29a20d8d64ce0b6be159fb5
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="949.43">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #ad140d}
16 span.s1 {font: 15.0px Helvetica}
17 span.s2 {color: #001bb9}
18 span.s3 {color: #000000}
19 span.s4 {color: #606060}
20 span.Apple-tab-span {white-space:pre}
21 </style>
22 </head>
23 <body>
24 <p class="p1"><span class="s1"><b>UnpackFFT<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b></span><b>Unpack an FFT chain into separate demand-rate FFT bin streams</b></p>
25 <p class="p2"><br></p>
26 <p class="p1"><b>#magsphases = UnpackFFT(chain, bufsize)</b></p>
27 <p class="p2"><br></p>
28 <p class="p1">Takes an <a href="FFT.html"><span class="s2">FFT</span></a> chain and separates the magnitude and phase data into separate demand-rate streams, for arithmetic manipulation etc. Note that you do have to decide your FFT buffer size in advance, since this determines how many values the UGen will output. Output is a list from DC up to Nyquist of [mag[0], phase[0], mag[1], phase[1], ... mag[nyquist], phase[nyquist]].</p>
29 <p class="p2"><br></p>
30 <p class="p1">This is technically a demand-rate UGen. The actual "demand" is usually created by PackFFT later on in the graph, which requests the values in order to re-pack the data. This allows for processing to occur imbetween...</p>
31 <p class="p2"><br></p>
32 <p class="p1">See also: <a href="PackFFT.html"><span class="s2">PackFFT</span></a>, <a href="Unpack1FFT.html"><span class="s2">Unpack1FFT</span></a>, and <a href="pvcollect.html"><span class="s2">pvcollect</span></a> <a href="pvcalc.html"><span class="s2">pvcalc</span></a> <a href="pvcalc2.html"><span class="s2">pvcalc2</span></a> which provide convenient ways to process audio in the frequency domain.</p>
33 <p class="p2"><br></p>
34 <p class="p1">The helpfile for <a href="pvcollect.html"><span class="s2">pvcollect</span></a> includes notes on efficiency considerations.</p>
35 <p class="p2"><br></p>
36 <p class="p3">(</p>
37 <p class="p3">s.boot.doWhenBooted {</p>
38 <p class="p3"><span class="s2"><span class="Apple-tab-span"> </span>var </span>fftsize = 1024;</p>
39 <p class="p3"><span class="Apple-tab-span"> </span>b = <span class="s2">Buffer</span>.alloc(s, fftsize, 1);</p>
40 <p class="p4"><span class="s3"><span class="Apple-tab-span"> </span>c = </span><span class="s2">Buffer</span><span class="s3">.read(s,</span>"sounds/a11wlk01.wav"<span class="s3">);</span></p>
41 <p class="p3">}</p>
42 <p class="p3">)</p>
43 <p class="p5"><br></p>
44 <p class="p6">// This one just drags out various the values and posts them - a little bit pointless!</p>
45 <p class="p3">(</p>
46 <p class="p3">x = {</p>
47 <p class="p3"><span class="Apple-tab-span"> </span><span class="s2">var</span> sig, chain, unp;</p>
48 <p class="p3"><span class="Apple-tab-span"> </span>sig = <span class="s2">SinOsc</span>.ar;</p>
49 <p class="p3"><span class="Apple-tab-span"> </span>sig = <span class="s2">PlayBuf</span>.ar(1, c, <span class="s2">BufRateScale</span>.kr(c), loop: 1);</p>
50 <p class="p3"><span class="Apple-tab-span"> </span>chain = <span class="s2">FFT</span>(b, sig);</p>
51 <p class="p5"><span class="Apple-tab-span"> </span></p>
52 <p class="p6"><span class="s3"><span class="Apple-tab-span"> </span></span>// Using the frombin &amp; tobin args makes it much more efficient, limiting analysis to the bins of interest</p>
53 <p class="p3"><span class="Apple-tab-span"> </span>unp = <span class="s2">UnpackFFT</span>(chain, b.numFrames, frombin: 0, tobin: 4);</p>
54 <p class="p5"><span class="Apple-tab-span"> </span></p>
55 <p class="p6"><span class="s3"><span class="Apple-tab-span"> </span></span>// Demand some data from the unpacker.</p>
56 <p class="p6"><span class="s3"><span class="Apple-tab-span"> </span></span>// NOTE: At present, Demand.kr is unable to handle more than 32 inputs,<span class="Apple-converted-space"> </span></p>
57 <p class="p6"><span class="s3"><span class="Apple-tab-span"> </span></span>// so using frombin &amp; tobin to limit the number of bins is compulsory.</p>
58 <p class="p3"><span class="Apple-tab-span"> </span><span class="s2">Demand</span>.kr(chain&gt;=0, 0, unp).collect{<span class="s2">|anunp, index|</span><span class="Apple-converted-space"> </span></p>
59 <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>anunp.poll(chain&gt;=0, if(index % 2 == 0,<span class="Apple-converted-space">  </span><span class="s4">"Magnitude"</span>, <span class="s4">"Phase"</span>)+(index/2).floor);</p>
60 <p class="p3"><span class="Apple-tab-span"> </span>};</p>
61 <p class="p5"><br></p>
62 <p class="p3"><span class="Apple-tab-span"> </span>(sig*0.1).dup;</p>
63 <p class="p3">}.play(s);</p>
64 <p class="p3">)</p>
65 <p class="p3">x.free;</p>
66 <p class="p5"><br></p>
67 <p class="p6">// Now a simple frequency-domain manipulation, square-rooting the magnitudes AND phases.</p>
68 <p class="p3">(</p>
69 <p class="p3">x = {</p>
70 <p class="p3"><span class="Apple-tab-span"> </span><span class="s2">var</span> in, chain, magsphases;</p>
71 <p class="p3"><span class="Apple-tab-span"> </span>in = <span class="s2">PlayBuf</span>.ar(1, c, <span class="s2">BufRateScale</span>.kr(c), loop: 1);</p>
72 <p class="p3"><span class="Apple-tab-span"> </span>chain = <span class="s2">FFT</span>(b, in);</p>
73 <p class="p3"><span class="Apple-tab-span"> </span>magsphases = <span class="s2">UnpackFFT</span>(chain, b.numFrames);</p>
74 <p class="p3"><span class="Apple-tab-span"> </span>magsphases = magsphases.collect(<span class="s2">_</span>.sqrt);</p>
75 <p class="p3"><span class="Apple-tab-span"> </span><span class="s2">PackFFT</span>(chain, b.numFrames, magsphases);</p>
76 <p class="p3"><span class="Apple-tab-span"> </span><span class="s2">Out</span>.ar(0, 0.25 * <span class="s2">IFFT</span>(chain).dup);</p>
77 <p class="p3">}.play(s);</p>
78 <p class="p3">)</p>
79 <p class="p3">x.free;</p>
80 <p class="p5"><br></p>
81 <p class="p5"><br></p>
82 <p class="p1">See <a href="pvcollect.html"><span class="s2">pvcollect</span></a> <a href="pvcalc.html"><span class="s2">pvcalc</span></a> <a href="pvcalc2.html"><span class="s2">pvcalc2</span></a> for more interesting examples.</p>
83 </body>
84 </html>