{"id":178,"date":"2012-03-02T13:35:21","date_gmt":"2012-03-02T11:35:21","guid":{"rendered":"http:\/\/www.joanillo.org\/?p=178"},"modified":"2020-09-14T13:36:33","modified_gmt":"2020-09-14T11:36:33","slug":"jugant-amb-la-fft","status":"publish","type":"post","link":"https:\/\/www.joanillo.org\/?p=178","title":{"rendered":"Jugant amb la FFT"},"content":{"rendered":"<p>&gt;Fa ja molt de temps (realment molt de temps) que estudiava la <strong>Transformada de Fourier<\/strong> (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Fourier_transform\">http:\/\/en.wikipedia.org\/wiki\/Fourier_transform<\/a>) a la facultat, i fins i tot vaig utilitzar l&#8217;algorisme de la <strong>FFT<\/strong> (Fast Fourier Transform, <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fast_Fourier_transform\">http:\/\/en.wikipedia.org\/wiki\/Fast_Fourier_transform<\/a>) en un aplicatiu escrit amb C per a processament digital d&#8217;imatges. Des d&#8217;aleshores en\u00e7\u00e0 que no m&#8217;havia preocupat de la <strong>FFT<\/strong> i realment ho tenia molt oblidat.<\/p>\n<p>Aquests dies he tornat a la c\u00e0rrega amb la <strong>FFT<\/strong> doncs, amb la idea d&#8217;aprendre les t\u00e8cniques de programaci\u00f3 d&#8217;aplicacions d&#8217;audio, volia fer o un afinador o un analitzador de la pot\u00e8ncia espectral d&#8217;un senyal, que fos compatible amb JACK.<\/p>\n<p>Primer de tot he mirat quin algorisme de FFT es podria utilitzar, amb llic\u00e8ncia GPL, i r\u00e0pidament he vist que la tria seria <strong>FFTW<\/strong> (<a href=\"http:\/\/www.fftw.org\/\">http:\/\/www.fftw.org\/<\/a>):<\/p>\n<blockquote><p>FFTW is a C subroutine library for computing the discrete Fourier transform (DFT) in one or more dimensions, of arbitrary input size, and of both real and complex data (as well as of even\/odd data, i.e. the discrete cosine\/sine transforms or DCT\/DST). We believe that FFTW, which is free software, should become the FFT library of choice for most applications.<\/p><\/blockquote>\n<p>M&#8217;he posat a compilar uns quants exemples i familiaritzar-me amb els c\u00e0lculs. En definitiva jo el que vull \u00e9s calcular la pot\u00e8ncia espectral d&#8217;un senyal d&#8217;audio, per tant el senyal d&#8217;entrada seran unes mostres reals (una sola dimensi\u00f3). La sortida de la FFT seran uns valors complexos, i he de tenir en compte tant la part real com la part imagin\u00e0ria per tal de calcular la pot\u00e8ncia espectral.<\/p>\n<blockquote><p>fftPlan = fftwf_plan_dft_r2c_1d(fftSize, fftIn, fftOut, FFTW_MEASURE); \/\/r2c_1d: real to complex, one dimension<\/p><\/blockquote>\n<p>Mirant si hi ha algun afinador de codi lliure, que treballi per la consola, i compatible amb JACK, m&#8217;he trobat amb aquests dos projectes, que precisament utilitzen la llibreria FFTW:<\/p>\n<ul>\n<li><strong>tuneit<\/strong>: A simple command-line instrument tuner for Linux (<a href=\"http:\/\/delysid.org\/tuneit.html\">http:\/\/delysid.org\/tuneit.html<\/a>, Mario Lang). Pot funcionar com a client de JACK<\/li>\n<li><strong>sndfile-spectrogram<\/strong>: Generate a spectrogram as a PNG file from a given sound file (<a href=\"http:\/\/www.mega-nerd.com\/libsndfile\/tools\/\">http:\/\/www.mega-nerd.com\/libsndfile\/tools\/<\/a>, Erik de Castro Lopo)<\/li>\n<\/ul>\n<p>i tamb\u00e9 m&#8217;ha sigut \u00faltil per fer el meu propi codi el projecte <em>capture_client<\/em>:<\/p>\n<ul>\n<li><strong>capture_client<\/strong>: (<a href=\"https:\/\/github.com\/jackaudio\/example-clients\/blob\/master\/capture_client.c\">https:\/\/github.com\/jackaudio\/example-clients\/blob\/master\/capture_client.c<\/a>, Paul Davis, Jack O&#8217;Quin)<\/li>\n<\/ul>\n<p>He dividit el problema en dues parts:<\/p>\n<ul>\n<li>Primer de tot capturo el senyal que prov\u00e9 del micr\u00f2fon, i com a resultat obtinc dos fitxers: el fitxer de so wav i un fitxer de text on fico un n\u00famero suficient de mostres.<\/li>\n<li>Segon: obro el fitxer de mostres, aplico una finestra de Hanning a les mostres com \u00e9s habitual, calculo la FFT, calculo la pot\u00e8ncia espectral del senyal, i obtinc un fitxer de sortida per a ser processat amb la utilitat de creaci\u00f3 de gr\u00e0fics <strong>gnuplot<\/strong> (<a href=\"http:\/\/www.gnuplot.info\/\">http:\/\/www.gnuplot.info\/<\/a>).<\/li>\n<\/ul>\n<p>No havia treballat mai amb <strong>gnuplot<\/strong>, que seria l&#8217;equivalent a <strong>Matlab<\/strong> per al m\u00f3n Linux. Veient la demo de <strong>gnuplot<\/strong> que es distribueix amb el codi font, i que mostra de forma r\u00e0pida totes les possibilitats de gnuplot, m&#8217;he quedat realment sorpr\u00e8s. En el meu cas dibuixar el gr\u00e0fic \u00e9s molt f\u00e0cil:<\/p>\n<blockquote><p>$ gnuplot<br \/>\ngnuplot&gt; plot &#8220;data_440_trumpet_output.txt&#8221;<\/p><\/blockquote>\n<p>La prova que presento s&#8217;ha gravat amb mini teclat <a href=\"http:\/\/www.google.es\/search?q=CASIO+SK-8&amp;hl=ca&amp;prmd=imvns&amp;tbm=isch&amp;biw=1280&amp;bih=739\">Casio SK-8<\/a>, un mini-teclat dels anys 80, escollint el so de trompeta, i obtinc el seg\u00fcent resultat.<\/p>\n<p><object height=\"81\" width=\"100%\"><param name=\"movie\" value=\"https:\/\/player.soundcloud.com\/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F38439858\"><param name=\"allowscriptaccess\" value=\"always\"><embed allowscriptaccess=\"always\" height=\"81\" src=\"https:\/\/player.soundcloud.com\/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F38439858\" type=\"application\/x-shockwave-flash\" width=\"100%\"><\/object>  <span><a href=\"http:\/\/soundcloud.com\/joanillo\/casio-sk-8-440hz-a-trumpet\">Casio SK-8 440Hz (A) trumpet, 44100 fps<\/a> by <a href=\"http:\/\/soundcloud.com\/joanillo\">joanillo<\/a><\/span><\/p>\n<p>Ara vull comparar el resultat dels meus c\u00e0lculs amb els obtinguts amb <strong>Audacity<\/strong> i <strong>Ardour<\/strong>, per comprovar que els c\u00e0lculs s\u00f3n correctes:<\/p>\n<table>\n<tbody>\n<tr>\n<td>Els meus c\u00e0lculs i Gnuplot<\/td>\n<td>Audacity<\/td>\n<td>Ardour<\/td>\n<\/tr>\n<tr>\n<td><a href=\"http:\/\/www.joanillo.org\/wp-content\/uploads\/2012\/03\/data_440_trumpet_gnuplot.jpg\"><img loading=\"lazy\" src=\"http:\/\/www.joanillo.org\/wp-content\/uploads\/2012\/03\/data_440_trumpet_gnuplot-300x238.jpg\" alt=\"\" title=\"Gnuplot. FFT. Casio SK-8 440_trumpet\" width=\"300\" height=\"238\"><\/a><\/td>\n<td><a href=\"http:\/\/www.joanillo.org\/wp-content\/uploads\/2012\/03\/data_440_trumpet_audacity.jpg\"><img loading=\"lazy\" src=\"http:\/\/www.joanillo.org\/wp-content\/uploads\/2012\/03\/data_440_trumpet_audacity-300x262.jpg\" alt=\"\" title=\"Audacity. FFT. Casio SK-8 440_trumpet\" width=\"300\" height=\"262\"><\/a><\/td>\n<td><a href=\"http:\/\/www.joanillo.org\/wp-content\/uploads\/2012\/03\/data_440_trumpet_ardour.jpg\"><img loading=\"lazy\" src=\"http:\/\/www.joanillo.org\/wp-content\/uploads\/2012\/03\/data_440_trumpet_ardour-300x273.jpg\" alt=\"\" title=\"Ardour. FFT. Casio SK-8 440_trumpet\" width=\"300\" height=\"273\"><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Perfecte! L&#8217;he clavat!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&gt;Fa ja molt de temps (realment molt de temps) que estudiava la Transformada de Fourier (http:\/\/en.wikipedia.org\/wiki\/Fourier_transform) a la facultat, i fins i tot vaig utilitzar l&#8217;algorisme de la FFT (Fast Fourier Transform, http:\/\/en.wikipedia.org\/wiki\/Fast_Fourier_transform) en un aplicatiu escrit amb C per a processament digital d&#8217;imatges. Des d&#8217;aleshores en\u00e7\u00e0 que no m&#8217;havia preocupat de la FFT i [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,18,16,12],"tags":[],"_links":{"self":[{"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/posts\/178"}],"collection":[{"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=178"}],"version-history":[{"count":1,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":179,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions\/179"}],"wp:attachment":[{"href":"https:\/\/www.joanillo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joanillo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}