tag:blogger.com,1999:blog-52773347940323229512013-11-29T07:01:49.230-08:00Summer Of Blogpoljarhttp://www.blogger.com/profile/09036553254635002178noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-5277334794032322951.post-62880605531501323842013-10-10T15:49:00.000-07:002013-10-10T15:49:15.879-07:00Epilogue: Fourier analysis and testingHi.<br /><br />It's time for the final update of my GSoC project. The last part of my GSoC was all about improving the resampler test cases for PulseAudio. Since a lot of DSP testing depends on the Fourier transformation, I'll try to explain it briefly here.<br /><br />The Fourier transform converts a periodic signal into a sum of sines and cosines of varying frequencies and phases. This way we can see the amplitude and frequency of the fundamental sine waves that make out our signal.<br /><br />Lets take a look at a 440Hz sine wave like the one below.<br /><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody><tr><td style="text-align: center;"><img border="0" height="400" src="https://lh3.ggpht.com/-fWUvf9MCTHU/Ulb7iyYg4AI/AAAAAAAAAD4/JS3RmtGU2po/s1600/440hz_sine.png" width="340" /></td></tr><tr><td class="tr-caption" style="text-align: center;">~100 samples of a 440Hz sine wave</td></tr></tbody></table><br />This is a simple sine wave without any higher harmonics so we don't expect to see much in our transformed signal. The sample format of our sine wave is 16-bit PCM. We can see that our amplitude is at about 50% (32768 would be loudest for 16-bit PCM and we are at about 16000) or -6dB.<br /><br />Below we can see the Fourier transform of our sine wave.<br /><br /><div class="separator" style="clear: both; text-align: center;"><img border="0" height="400" src="https://lh3.ggpht.com/-4c7XE2GlMy8/UlcPAwaVv7I/AAAAAAAAAEQ/xRBmoJ1vgCo/s1600/fft_440hz_sine.png" width="340" /></div><div class="separator" style="clear: both; text-align: center;"></div><br />We can clearly see a nice spike at 440Hz with an amplitude of -6dB. If our sine wave would consist of a fundamental sine wave at 440Hz and a higher harmonic at 880Hz we would see two spikes.<br /><br />Now there are some other spikes, albeit rather small ones, which means that our original sine wave did not only contain a fundamental wave at 440Hz. The other spikes are considered to be noise. This way we can measure the signal to noise ratio (SNR). We take a look at the amplitude of our signal and the highest amplitude of an unwanted spike and divide them. Easy, isn't it?<br /><br />Lets take a final look at a transformed signal. Below is the Fourier transform of a logarithmic chirp (logarithmically swept sine wave) from 440Hz to 48kHz which was run through a resampler.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-Ga_DB8yw8NY/UlcP5s7mL-I/AAAAAAAAAEc/bm0q8iznN1A/s1600/resampled_chirp.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://lh3.ggpht.com/-Ga_DB8yw8NY/UlcP5s7mL-I/AAAAAAAAAEc/bm0q8iznN1A/s1600/resampled_chirp.png" width="340" /></a></div><br />We can see that the start frequency is indeed around 440Hz but at about 20kHz our amplitude starts to fall off. This happened because our resampler was smart enough to filter out frequencies above the audible level (or frequencies that are higher than half of the sampling frequency) to avoid aliasing.<br /><br />These test and graphs are now easily reproducible using the new and shiny 'resampler-quality-test' which generates arbitrary sine or chirp signals and runs them through the resampler (the result can be saved) and a fft.py script which plots the Fourier transform of a WAVE file. <br /><br />Some more improvements were made to the already existing resampler test case but these are not so interesting.<br /><br />That's all for this year's GSoC. It was a fun and productive experience. I want to take a moment and thank my mentor for this year, Peter Meerwald, for all the help and friendly exchanges during the summer, and my last year's mentor Tanu Kaskinen for the same nice treatment last year.<br /><br />Goodbye.poljarhttp://www.blogger.com/profile/09036553254635002178noreply@blogger.com1tag:blogger.com,1999:blog-5277334794032322951.post-78128265813182706122013-08-12T04:29:00.001-07:002013-08-12T04:29:17.926-07:00Vol 2: Resampling methodsHi.<br />Time for an update. This time I will talk a little bit about the different resampling methods and resampling in general. So lets start with a quick introduction to resampling.<br /><br />Below is a figure of a discrete time representation of a 440Hz sine wave. The sine wave is sampled at a rate (or sampling frequency) of 48kHz. This means that roughly every 0.02ms a sample of our sine wave is taken so for one second of sound 48000 samples are needed.<br /><br /><table cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody><tr><td style="text-align: center;"><br /><a href="http://2.bp.blogspot.com/-ly5B0mCZuyE/UgerjyTJ9qI/AAAAAAAAAB4/HMIBTs7NTAU/s1600/original.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="206" src="https://lh3.ggpht.com/-ly5B0mCZuyE/UgerjyTJ9qI/AAAAAAAAAB4/HMIBTs7NTAU/s1600/original.png" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">440Hz sine wave sampled at 48kHz</td></tr></tbody></table><div class="separator" style="clear: both; text-align: center;"></div><br />Now if the clock in our sound card supports this frequency, we can just feed it our sine wave and it will play it just fine. If this is not the case, we would get pitch shifted output just like when the playback speed on an old tape recorder or turntable is increased or decreased.<br /><br />Conceptually, we could reconstruct our analog signal and sample it at our new sample rate to obtain our desired discrete time representation. But this is not a really practical solution and we use pure mathematical solutions instead. One of them is linear interpolation. Shown below is our original sine wave resampled via linear interpolation to 96kHz which means that we now have twice as many samples than in our original sampled sine wave.<br /><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto; text-align: center;"><tbody><tr><td style="text-align: center;"><a href="http://1.bp.blogspot.com/-yR4HHv894as/Ugerj8QXI7I/AAAAAAAAAB8/rbpJRxS7so8/s1600/resampled.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="206" src="https://lh3.ggpht.com/-yR4HHv894as/Ugerj8QXI7I/AAAAAAAAAB8/rbpJRxS7so8/s1600/resampled.png" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">440Hz sine wave resampled to 96kHz</td></tr></tbody></table>There are many different resampling methods and implementations. PulseAudio already supports quite a few. I added support for some more and tested their performance.<br /><br />Here are the newly added methods:<br /><ul><li>libswresample (lswr below)</li><li>libavresample (lavr below)</li><li>sox resampler (soxr below)</li></ul>And here are the test results:<br /><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody><tr><td style="text-align: center;"><a href="http://1.bp.blogspot.com/-7g9cxLQ3AMg/UgexewMH49I/AAAAAAAAACM/AwynFJ57oa8/s1600/s16le_ivy.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="https://lh3.ggpht.com/-7g9cxLQ3AMg/UgexewMH49I/AAAAAAAAACM/AwynFJ57oa8/s1600/s16le_ivy.png" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">Performance using signed 16-bit integers as the sample format</td></tr></tbody></table><br />These results should be taken with a grain of salt because the different resampling methods do not carry the same quality. The most interesting resampler here seems to be soxr using cubic interpolation.<br /><br />Below is the same test but this time using floating point numbers as the sample format:<br /><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody><tr><td style="text-align: center;"><a href="http://1.bp.blogspot.com/-2PbTny-ki1g/Ugi1V5H1q5I/AAAAAAAAACY/C_gTwu8YxlI/s1600/float32le_ivy.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="https://lh3.ggpht.com/-2PbTny-ki1g/Ugi1V5H1q5I/AAAAAAAAACY/C_gTwu8YxlI/s1600/float32le_ivy.png" width="400" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">Performance using floating point numbers as the sample format</td></tr></tbody></table><br />Again soxr here seems to be the most promising.<br /><br />Which of these new resampling methods will find their way into the master tree of PulseAudio remains to be seen.<br /><br />This ended up somewhat longer than anticipated, but I hope it was interesting.<br /><br />Thanks for your attention!poljarhttp://www.blogger.com/profile/09036553254635002178noreply@blogger.com0tag:blogger.com,1999:blog-5277334794032322951.post-36272582761948976542013-07-14T17:09:00.000-07:002013-07-14T17:09:45.243-07:00Vol 1. Refactoring<p>Hi. Time for a quick update about my Summer of Code project. <br>I'm going to talk a little about my first checklist item, refactoring the resampling code. While refactoring isn't quite exciting as implementing new features it is a necessity to assure code maintainability. </p><p>So let's take a look at the resampler interface and see what we can do. The interface consists of a pa_resampler structure which we create if we want to do some resampling. This structure holds all of our settings the resampler cares about, (sample specifications, different buffers) but also some specific data for different resampling implementations. <br>The interesting bits are shown bellow. <pre class="brush: c" name="code"><br />struct pa_resampler {<br />...<br /> void (*impl_free)(pa_resampler *r);<br /> void (*impl_update_rates)(pa_resampler *r);<br /> void (*impl_resample)(pa_resampler *r, const pa_memchunk *in, <br /> unsigned in_samples, pa_memchunk *out, <br /> unsigned *out_samples);<br /> void (*impl_reset)(pa_resampler *r);<br /><br /> struct { /* data specific to the trivial resampler */<br /> unsigned o_counter;<br /> unsigned i_counter;<br /> } trivial;<br /><br /> struct { /* data specific to the peak finder pseudo resampler */<br /> unsigned o_counter;<br /> unsigned i_counter;<br /><br /> float max_f[PA_CHANNELS_MAX];<br /> int16_t max_i[PA_CHANNELS_MAX];<br /><br /> } peaks;<br />...<br />};<br /></pre>After the implementation specific function pointers we can see multiple structures holding implementation specific data. Since the resampler can't switch implementations on the fly (without destroying and recreating a resampler) only one of these structures is used at a time. <br>There are six of those structures contained inside of pa_resampler and some of them have only a single member which is quite pointless. </p><p>Further bellow of the file we see a big init_table containing the mapping between a resampling method (an enumeration) and its initialization function. <pre class="brush: c" name="code"><br />static int (* const init_table[])(pa_resampler*r) = {<br /> [PA_RESAMPLER_SRC_SINC_BEST_QUALITY] = libsamplerate_init,<br /> [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = libsamplerate_init,<br /> [PA_RESAMPLER_SRC_SINC_FASTEST] = libsamplerate_init,<br /> [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD] = libsamplerate_init,<br /> [PA_RESAMPLER_SRC_LINEAR] = libsamplerate_init,<br /> [PA_RESAMPLER_TRIVIAL] = trivial_init,<br /> [PA_RESAMPLER_SPEEX_FLOAT_BASE+0] = speex_init,<br /> [PA_RESAMPLER_SPEEX_FLOAT_BASE+1] = speex_init,<br /> ...<br /> [PA_RESAMPLER_SPEEX_FLOAT_BASE+10] = speex_init,<br /> [PA_RESAMPLER_SPEEX_FIXED_BASE+0] = speex_init,<br /> ...<br /> [PA_RESAMPLER_SPEEX_FIXED_BASE+10] = speex_init,<br /> ...<br /></pre>As we can see there are quite some duplicates here. There are a total of 32 entries while only having 6 distinctive init functions. There is another big table like this containing the implementation names, this table doesn't contain any duplicates but it would be nice if we could group the implementation names into smaller implementation specific tables. </p>So without further ado here is the first code snippet with the appropriate changes. <p><pre class="brush: c" name="code"><br />struct pa_resampler {<br /> ...<br /> pa_resampler_implementation implementation;<br /> ...<br />};<br /></pre>All the implementation specific data is now contained inside a single structure. <br>And here is how the equivalent second code snippet looks like. <pre class="brush: c" name="code"><br />static pa_resampler_implementation *impl_table[] = {<br /> [PA_RESAMPLER_SRC_SINC_FASTEST] = &libsamplerate_impl,<br /> [PA_RESAMPLER_TRIVIAL] = &trivial_impl,<br /> [PA_RESAMPLER_SPEEX_FLOAT_BASE] = &speex_impl,<br /> [PA_RESAMPLER_FFMPEG] = &ffmpeg_impl,<br /> [PA_RESAMPLER_AUTO] = &auto_impl,<br /> [PA_RESAMPLER_COPY] = ©_impl,<br /> [PA_RESAMPLER_PEAKS] = &peaks_impl,<br />};<br /></pre>No more duplicate entries here. <br>And at last here is how the pa_resampler_implementation structure is defined. <pre class="brush: c" name="code"><br />struct pa_resampler_implementation {<br /> int (*init)(pa_resampler *r);<br /> void (*free)(pa_resampler *r);<br /> void (*update_rates)(pa_resampler *r);<br /> void (*resample)(pa_resampler *r, const pa_memchunk *in,<br /> unsigned in_samples, pa_memchunk *out,<br /> unsigned *out_samples);<br /> void (*reset)(pa_resampler *r);<br /> void *data;<br /> const char *names[PA_RESAMPLER_MAX_VARIANTS];<br />};<br /></pre>The implementation specific structures are replaced by a simple opaque data entry and the implementations init function takes care of the allocating. The big names table is also now split up and contained inside this structure. </p><p>These changes aren't yet merged upstream and some of it may change if needed. Further details are on my <a href="https://github.com/poljar/pulseaudio/">github </a> page. <br>Thats it for now, next time there should be a more interesting topic. Thanks for your attention and bye. </p>poljarhttp://www.blogger.com/profile/09036553254635002178noreply@blogger.com0tag:blogger.com,1999:blog-5277334794032322951.post-28268529664160008662013-06-02T12:46:00.002-07:002013-06-02T12:46:53.001-07:00PreludeHello and welcome to my new blog.<br /> <br />This blog should mainly serve as a small developer log over the duration of this year's Google Summer of Code. I'll try to keep it short and technical.<br /> <br />This year, like last year, I'm participating as a student for PulseAudio. My main task for this year is to update the resampling code.<br /> <br />So here's a list of things I have the privilege to work on over the summer:<br /> <ul><li>refactor and cleanup the resampling code</li><li>enable resampling with libavresample</li><li>enable resampling with libswresample</li><li>deprecate the ffmpeg resampling method</li><li>replace libspeex with opus tools</li><li>improve resampling tests</li></ul><br />What all of this exactly means will be explained some other time.<br /> <br />That's all for now. Bye!<br /> poljarhttp://www.blogger.com/profile/09036553254635002178noreply@blogger.com0