{"id":43,"date":"2010-02-22T07:29:52","date_gmt":"2010-02-22T07:29:52","guid":{"rendered":"http:\/\/mitat.tuu.fi\/?p=43"},"modified":"2010-02-22T07:29:52","modified_gmt":"2010-02-22T07:29:52","slug":"fft-aka-fast-fourier-transform-explained-well","status":"publish","type":"post","link":"http:\/\/mitat.tuu.fi\/?p=43","title":{"rendered":"FFT aka Fast Fourier Transform explained well"},"content":{"rendered":"<p>From here: http:\/\/www.kvraudio.com\/forum\/viewtopic.php?p=989000&#038;sid=6e7e5d21145a3efd03b8ebaa5a15f830#989000<\/p>\n<blockquote><p>The fourier transform converts a time representation ( samples ) into a frequency representation ( i.e. the frequency spectrum of these samples ) and vice versa.<\/p>\n<p>Commonly it splits a number of audio samples, say 2048, into a corresponding number of sine waves of different frequencies, so that you know the volumes and the phase for each sine.<\/p>\n<p>The principle is very simple:<\/p>\n<p>If you multiply a set of samples with another set of reference samples and add each result, like this:<\/p>\n<p>result = sample[ 0 ] * reference[ 0 ] + sample[ 1 ] * reference[ 1 ] + &#8230; + sample[ n ] * reference[ n ]<\/p>\n<p>&#8230; the result is an indicator for the similarity of both sets of samples.<\/p>\n<p>And now with sines<\/p>\n<p>Now, if you want to know the volume and the phase of a sine at a certain frequency inside an audio sample, you have to calculate 2 of these similarity values:<\/p>\n<p>FC = 2 * PI * frequency \/ samplerate; \/\/ &#8220;frequency constant&#8221;<\/p>\n<p>result_i = sample[ 0 ] * sin( FC * 0.f ) + sample[ 1 ] * sin( FC * 1.f )+ &#8230; + sample[ n ] * sin( FC * n.f )<\/p>\n<p>result_r = sample[ 0 ] * cos( FC * 0.f ) + sample[ 1 ] * cos( FC * 1.f )+ &#8230; + sample[ n ] * cos( FC * n.f )<\/p>\n<p>To get the actual phase and magnitude, you use simple pythagoras to get length and trigonometry to get the angle:<\/p>\n<p>magnitude = sqrt( result_i * result_i + result_r * result_r);<\/p>\n<p>phase = atan2( result_i, result_r) \/\/ I think&#8230;<\/p>\n<p>This way you can determine how much of a frequency is present in an audio file etc., and which phase it has relative to the beginning.<\/p>\n<p>Complex Numbers?<\/p>\n<p>In Fourier Transform lingo, the sine part is oftenly called &#8220;imaginary&#8221; while the cosine part is called &#8220;rational&#8221;. They pretend to be complex numbers, but that&#8217;s more or less only a convention. The phase\/magnitude representation IMHO feels more like complex numbers.<\/p>\n<p>Getting the whole spectrum<\/p>\n<p>Now, the idea behind fourier transforms is, if you start with a &#8220;fundamental&#8221; frequency of exactly 1 cycle over the whole length of the analysed set of samples (call it n samples) and do this with every multiple up to n\/2 of the fundamental, you get a representation that thoroughly reflects the spectrum of the original sample. &#8211; If you transform it back (create a new bunch of n samples out of adding sines and cosines), it will perfectly fit the original samples!<\/p>\n<p>Each &#8220;frequency slot&#8221; or multiple of the fundamental (1, 2, 3, &#8230;, n\/2) is called a &#8220;bin&#8221;. (Note, there&#8217;s also a zero-bin, I&#8217;ll explain that later)<\/p>\n<p>Fast Fourier Transform &#8211; FFT<\/p>\n<p>This would be a very time consuming technique, if there wasn&#8217;t the Fast Fourier Transform, or FFT. The FFT is a simple, yet highly difficult to understand algorithm. It takes advantage of the fact, that if you draw all those sines in a graph, you have many lines that cross each other at certain points. And because they cross each other pretty often, you need only 1 multiply to gather information for many results needed. Hence, what FFT does is recursively reordering (&#8220;prepacking&#8221;, &#8220;butterfly&#8221;) the samples in a tricky way so that only a fraction of calculations are needed to create the spectrum for a given range of audio samples. The drawback is, FFT requires certain lengths (or &#8220;ns&#8221;) of samples, which are a power of 2: like 16, 32, 1024, 4096 samples etc.<\/p>\n<p>DC offset and Nyquist in FFTs<\/p>\n<p>Typically, a FFT outputs the spectrum in sines and cosines, but it also output 2 special values: A cosine representing the DC (offset of powers above zero and below zero), which is bin 0, or the fundamental frequency * 0 and a sine representing Nyquist (because at Nyquist, there can&#8217;t be a cosine!). Hence some algorithms require N\/2+1 bins, others &#8220;pack&#8221; the Nyquist bin into the sine field of the DC bin.<\/p>\n<p>Hope this helps, or maybe it&#8217;s a start&#8230;<\/p>\n<p>Cheers,<\/p>\n<p>Wink Urs<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>From here: http:\/\/www.kvraudio.com\/forum\/viewtopic.php?p=989000&#038;sid=6e7e5d21145a3efd03b8ebaa5a15f830#989000 The fourier transform converts a time representation ( samples ) into a frequency representation ( i.e. the frequency spectrum of these samples ) and vice versa. Commonly it splits a number of audio samples, say 2048, into a corresponding number of sine waves of different frequencies, so that you know the volumes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=43"}],"version-history":[{"count":1,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":44,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions\/44"}],"wp:attachment":[{"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}