bspeice.github.io/audio-compression-using-pca.html

710 lines
6.8 MiB
HTML
Raw Normal View History

2016-11-01 16:19:21 -04:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
2018-01-16 20:28:29 -05:00
<meta name="description" content="Towards a new (and pretty poor) compression scheme¶I&#39;m going to be working with some audio data for a while as I get prepared for a term project this semester. I&#39;ll be working (with a partner) to ...">
2016-11-01 16:19:21 -04:00
<meta name="keywords" content="Digital Signal Processing, Machine Learning, PCA">
2018-01-16 20:28:29 -05:00
<link rel="icon" href="https://bspeice.github.io/favicon.ico">
2016-11-01 16:19:21 -04:00
<title>Audio Compression using PCA - Bradlee Speice</title>
<!-- Stylesheets -->
2018-01-16 20:28:29 -05:00
<link href="https://bspeice.github.io/theme/css/bootstrap.min.css" rel="stylesheet">
<link href="https://bspeice.github.io/theme/css/fonts.css" rel="stylesheet">
<link href="https://bspeice.github.io/theme/css/nest.css" rel="stylesheet">
<link href="https://bspeice.github.io/theme/css/pygment.css" rel="stylesheet">
2016-11-01 16:19:21 -04:00
<!-- /Stylesheets -->
<!-- RSS Feeds -->
2018-01-16 20:28:29 -05:00
<link href="https://bspeice.github.io/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Bradlee Speice Full Atom Feed" />
<link href="https://bspeice.github.io/feeds/blog.atom.xml" type="application/atom+xml" rel="alternate" title="Bradlee Speice Categories Atom Feed" />
2016-11-01 16:19:21 -04:00
<!-- /RSS Feeds -->
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
2018-01-16 20:28:29 -05:00
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-74711362-1', 'auto');
ga('send', 'pageview');
</script>
<!-- /Google Analytics -->
2016-11-01 16:19:21 -04:00
</head>
<body>
<!-- Header -->
<div class="header-container gradient">
<!-- Static navbar -->
<div class="container">
<div class="header-nav">
<div class="header-logo">
2018-01-16 20:28:29 -05:00
<a class="pull-left" href="https://bspeice.github.io/"><img class="mr20" src="https://bspeice.github.io/images/logo.svg" alt="logo">Bradlee Speice</a>
2016-11-01 16:19:21 -04:00
</div>
<div class="nav pull-right">
</div>
</div>
</div>
<!-- /Static navbar -->
<!-- Header -->
<!-- Header -->
<div class="container header-wrapper">
<div class="row">
<div class="col-lg-12">
<div class="header-content">
<h1 class="header-title">Audio Compression using PCA</h1>
2018-01-16 20:28:29 -05:00
<p class="header-date"> <a href="https://bspeice.github.io/author/bradlee-speice.html">Bradlee Speice</a>, Tue 01 November 2016, <a href="https://bspeice.github.io/category/blog.html">Blog</a></p>
2016-11-01 16:19:21 -04:00
<div class="header-underline"></div>
<div class="clearfix"></div>
<p class="pull-right header-tags">
<span class="glyphicon glyphicon-tags mr5" aria-hidden="true"></span>
2018-01-16 20:28:29 -05:00
<a href="https://bspeice.github.io/tag/digital-signal-processing.html">Digital Signal Processing</a>, <a href="https://bspeice.github.io/tag/machine-learning.html">Machine Learning</a>, <a href="https://bspeice.github.io/tag/pca.html">PCA</a> </p>
2016-11-01 16:19:21 -04:00
</div>
</div>
</div>
</div>
<!-- /Header -->
<!-- /Header -->
</div>
<!-- /Header -->
<!-- Content -->
<div class="container content">
2018-01-16 20:28:29 -05:00
<p>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Towards-a-new-(and-pretty-poor)-compression-scheme">Towards a new (and pretty poor) compression scheme<a class="anchor-link" href="#Towards-a-new-(and-pretty-poor)-compression-scheme">&#182;</a></h2><p>I'm going to be working with some audio data for a while as I get prepared for a term project this semester. I'll be working (with a partner) to design a system for separating voices from music. Given my total lack of experience with <a href="https://en.wikipedia.org/wiki/Digital_signal_processing">Digital Signal Processing</a> I figured that now was as good a time as ever to work on a couple of fun projects that would get me back up to speed.</p>
<p>The first project I want to work on: Designing a new compression scheme for audio data.</p>
<h2 id="A-Brief-Introduction-to-Audio-Compression">A Brief Introduction to Audio Compression<a class="anchor-link" href="#A-Brief-Introduction-to-Audio-Compression">&#182;</a></h2><p>Audio files when uncompressed (files ending with <code>.wav</code>) are huge. Like, 10.5 Megabytes per minute huge. Storage is cheap these days, but that's still an incredible amount of data that we don't really need. Instead, we'd like to compress that data so that it's not taking up so much space. There are broadly two ways to accomplish this:</p>
<ol>
<li><p>Lossless compression - Formats like <a href="https://en.wikipedia.org/wiki/FLAC">FLAC</a>, <a href="https://en.wikipedia.org/wiki/Apple_Lossless">ALAC</a>, and <a href="https://en.wikipedia.org/wiki/Monkey%27s_Audio">Monkey's Audio (.ape)</a> all go down this route. The idea is that when you compress and uncompress a file, you get exactly the same as what you started with.</p>
</li>
<li><p>Lossy compression - Formats like <a href="https://en.wikipedia.org/wiki/MP3">MP3</a>, <a href="https://en.wikipedia.org/wiki/Vorbis">Ogg</a>, and <a href="https://en.wikipedia.org/wiki/Advanced_Audio_Coding">AAC (<code>.m4a</code>)</a> are far more popular, but make a crucial tradeoff: We can reduce the file size even more during compression, but the decompressed file won't be the same.</p>
</li>
</ol>
<p>There is a fundamental tradeoff at stake: Using lossy compression sacrifices some of the integrity of the resulting file to save on storage space. Most people (I personally believe it's everybody) can't hear the difference, so this is an acceptable tradeoff. You have files that take up a 10<sup>th</sup> of the space, and nobody can tell there's a difference in audio quality.</p>
<h2 id="A-PCA-based-Compression-Scheme">A PCA-based Compression Scheme<a class="anchor-link" href="#A-PCA-based-Compression-Scheme">&#182;</a></h2><p>What I want to try out is a <a href="https://en.wikipedia.org/wiki/Principal_component_analysis">PCA</a> approach to encoding audio. The PCA technique comes from Machine Learning, where it is used for a process called <a href="https://en.wikipedia.org/wiki/Dimensionality_reduction">Dimensionality Reduction</a>. Put simply, the idea is the same as lossy compression: if we can find a way that represents the data well enough, we can save on space. There are a lot of theoretical concerns that lead me to believe this compression style will not end well, but I'm interested to try it nonetheless.</p>
<p>PCA works as follows: Given a dataset with a number of features, I find a way to approximate those original features using some "new features" that are statistically as close as possible to the original ones. This is comparable to a scheme like MP3: Given an original signal, I want to find a way of representing it that gets approximately close to what the original was. The difference is that PCA is designed for statistical data, and not signal data. But we won't let that stop us.</p>
<p>The idea is as follows: Given a signal, reshape it into 1024 columns by however many rows are needed (zero-padded if necessary). Run the PCA algorithm, and do dimensionality reduction with a couple different settings. The number of components I choose determines the quality: If I use 1024 components, I will essentially be using the original signal. If I use a smaller number of components, I start losing some of the data that was in the original file. This will give me an idea of whether it's possible to actually build an encoding scheme off of this, or whether I'm wasting my time.</p>
<h2 id="Running-the-Algorithm">Running the Algorithm<a class="anchor-link" href="#Running-the-Algorithm">&#182;</a></h2><p>The audio I will be using comes from the song <a href="https://brokeforfree.bandcamp.com/track/tabulasa">Tabulasa</a>, by <a href="https://brokeforfree.bandcamp.com/album/xxvii">Broke for Free</a>. I'll be loading in the audio signal to Python and using <a href="http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html#sklearn.decomposition.PCA">Scikit-Learn</a> to actually run the PCA algorithm.</p>
<p>We first need to convert the FLAC file I have to a WAV:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span class="o">!</span>ffmpeg -hide_banner -loglevel panic -i <span class="s2">&quot;Broke For Free/XXVII/01 Tabulasa.flac&quot;</span> <span class="s2">&quot;Tabulasa.wav&quot;</span> -c wav
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Then, let's go ahead and load a small sample so you can hear what is going on.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span class="kn">from</span> <span class="nn">IPython.display</span> <span class="k">import</span> <span class="n">Audio</span>
<span class="kn">from</span> <span class="nn">scipy.io</span> <span class="k">import</span> <span class="n">wavfile</span>
<span class="n">samplerate</span><span class="p">,</span> <span class="n">tabulasa</span> <span class="o">=</span> <span class="n">wavfile</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="s1">&#39;Tabulasa.wav&#39;</span><span class="p">)</span>
<span class="n">start</span> <span class="o">=</span> <span class="n">samplerate</span> <span class="o">*</span> <span class="mi">14</span> <span class="c1"># 10 seconds in</span>
<span class="n">end</span> <span class="o">=</span> <span class="n">start</span> <span class="o">+</span> <span class="n">samplerate</span> <span class="o">*</span> <span class="mi">10</span> <span class="c1"># 5 second duration</span>
<span class="n">Audio</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">tabulasa</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">end</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">rate</span><span class="o">=</span><span class="n">samplerate</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt output_prompt">Out[2]:</div>
<div class="output_html rendered_html output_subarea output_execute_result">
<audio controls="controls" >
<source src="data:audio/wav;base64,UklGRnR1DQBXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YVB1DQBRFj8TuxABD8ANtgzoC6ELWwx6DvMRDxbVGaAcGx5kHtkd2RyF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ug8SEOkP0w6+DPYJHQfCBN8ClwEZASEBmAFEAgYDQQTXBXsHOAnLCvILwQweDRMN4gySDLsLGQoCCJQFrAKs/+r8qPoq+WX4l/j8+Uv83v7cAMgBsAGeAN/+Vv1o/Nn7cPv2+lj67vk++nz7rf3SAIgEUggMDGUPmxEhEvMQUg7CChoHCwQJAlsBtwFMAlICfAH1/1z+pf10/qUAVQOhBdAGXQagBO0Bxv5q/IP7/fvJ/UQAfAL4AzkE+gJIAKf8KPmv9rH1P/bP96v5mvuZ/Wb/0QCcAVAB/f8P/un7PvpV+Tv5Tvpx/PH+gAHFA38F2AanBx8IxQiICSsKUwqJCZsHgwSzAED94/q++Z35M/og+/H7mfwg/S39ifzj+uv3F/Ts79HrYujx5arkmuRa5eDmM+k17PDvQvSh+Cv8mf7U/zL/5Py1+dD16vEu7//tK+4272rwc/FJ8vjywfOz9Iv1DfYQ9mf1QfQg8z3y4PFr8uHz/PVb+NH6y/z8/Z7+E/+y/xwAvv9o/qv8Cfvn+cT5rPoS/Fv9T/7O/r7+Bv7C/Fj7+vm9+OL3cvdA9/H2Mvb49H/z6PEi8Fnu9+yA7F3tge9X8iP1NfdO+LX4uviZ+Dv4TPcF9uL0AvQt81zyq/HA8ILvU+6f7ffth++c8Y3zDfXe9S/2fvYd9x/4O/kZ+qb6wfps+uX59vgv93H0s/Ab7CnnUeI/3t3b
Your browser does not support the audio element.
</audio>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Next, we'll define the code we will be using to do PCA. It's very short, as the PCA algorithm is very simple.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span class="kn">from</span> <span class="nn">sklearn.decomposition</span> <span class="k">import</span> <span class="n">PCA</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="k">def</span> <span class="nf">pca_reduce</span><span class="p">(</span><span class="n">signal</span><span class="p">,</span> <span class="n">n_components</span><span class="p">,</span> <span class="n">block_size</span><span class="o">=</span><span class="mi">1024</span><span class="p">):</span>
<span class="c1"># First, zero-pad the signal so that it is divisible by the block_size</span>
<span class="n">samples</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">signal</span><span class="p">)</span>
<span class="n">hanging</span> <span class="o">=</span> <span class="n">block_size</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">mod</span><span class="p">(</span><span class="n">samples</span><span class="p">,</span> <span class="n">block_size</span><span class="p">)</span>
<span class="n">padded</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">lib</span><span class="o">.</span><span class="n">pad</span><span class="p">(</span><span class="n">signal</span><span class="p">,</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">hanging</span><span class="p">),</span> <span class="s1">&#39;constant&#39;</span><span class="p">,</span> <span class="n">constant_values</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="c1"># Reshape the signal to have 1024 dimensions</span>
<span class="n">reshaped</span> <span class="o">=</span> <span class="n">padded</span><span class="o">.</span><span class="n">reshape</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">padded</span><span class="p">)</span> <span class="o">//</span> <span class="n">block_size</span><span class="p">,</span> <span class="n">block_size</span><span class="p">))</span>
<span class="c1"># Second, do the actual PCA process</span>
<span class="n">pca</span> <span class="o">=</span> <span class="n">PCA</span><span class="p">(</span><span class="n">n_components</span><span class="o">=</span><span class="n">n_components</span><span class="p">)</span>
<span class="n">pca</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">reshaped</span><span class="p">)</span>
<span class="n">transformed</span> <span class="o">=</span> <span class="n">pca</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">reshaped</span><span class="p">)</span>
<span class="n">reconstructed</span> <span class="o">=</span> <span class="n">pca</span><span class="o">.</span><span class="n">inverse_transform</span><span class="p">(</span><span class="n">transformed</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">padded</span><span class="p">)))</span>
<span class="k">return</span> <span class="n">pca</span><span class="p">,</span> <span class="n">transformed</span><span class="p">,</span> <span class="n">reconstructed</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Now that we've got our functions set up, let's try actually running something. First, we'll use <code>n_components == block_size</code>, which implies that we should end up with the same signal we started with.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span class="n">tabulasa_left</span> <span class="o">=</span> <span class="n">tabulasa</span><span class="p">[:,</span><span class="mi">0</span><span class="p">]</span>
<span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">reconstructed</span> <span class="o">=</span> <span class="n">pca_reduce</span><span class="p">(</span><span class="n">tabulasa_left</span><span class="p">,</span> <span class="mi">1024</span><span class="p">,</span> <span class="mi">1024</span><span class="p">)</span>
<span class="n">Audio</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">reconstructed</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">end</span><span class="p">],</span> <span class="n">rate</span><span class="o">=</span><span class="n">samplerate</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt output_prompt">Out[4]:</div>
<div class="output_html rendered_html output_subarea output_execute_result">
<audio controls="controls" >
<source src="data:audio/wav;base64,UklGRnR1DQBXQVZFZm10IBAAAAABAAEARKwAAIhYAQACAB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/QT8Vftl+/n7Nv0O/zsBvQNrBvIINwsFDS8O2A4bDwwPzQ5pDi8OGA5ODgsPug8SEOkP0w6+DPYJHQfCBN8ClwEZASEBmAFEAgYDQQTXBXsHOAnLCvILwQweDRMN4gySDLsLGQoCCJQFrAKs/+r8qPoq+WX4l/j8+Uv83v7cAMgBsAGeAN/+Vv1o/Nn7cPv2+lj67vk++nz7rf3SAIgEUggMDGUPmxEhEvMQUg7CChoHCwQJAlsBtwFMAlICfAH1/1z+pf10/qUAVQOhBdAGXQagBO0Bxv5q/IP7/fvJ/UQAfAL4AzkE+gJIAKf8KPmv9rH1P/bP96v5mvuZ/Wb/0QCcAVAB/f8P/un7PvpV+Tv5Tvpx/PH+gAHFA38F2AanBx8IxQiICSsKUwqJCZsHgwSzAED94/q++Z35M/og+/H7mfwg/S39ifzj+uv3F/Ts79HrYujx5arkmuRa5eDmM+k17PDvQvSh+Cv8mf7U/zL/5Py1+dD16vEu7//tK+4272rwc/FJ8vjywfOz9Iv1DfYQ9mf1QfQg8z3y4PFr8uHz/PVb+NH6y/z8/Z7+E/+y/xwAvv9o/qv8Cfvn+cT5rPoS/Fv9T/7O/r7+Bv7C/Fj7+vm9+OL3cvdA9/H2Mvb49H/z6PEi8Fnu9+yA7F3tge9X8iP1NfdO+LX4uviZ+Dv4TPcF9uL0AvQt81zyq/HA8ILvU+6f7ffth++c8Y3zDfXe9S/2fvYd9x/4O/kZ+qb6wfps+uX59vgv93H0s/Ab7CnnUeI/3t3b
Your browser does not support the audio element.
</audio>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>OK, that does indeed sound like what we originally had. Let's drastically cut down the number of components we're doing this with as a sanity check: the audio quality should become incredibly poor.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">reconstructed</span> <span class="o">=</span> <span class="n">pca_reduce</span><span class="p">(</span><span class="n">tabulasa_left</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">1024</span><span class="p">)</span>
<span class="n">Audio</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">reconstructed</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">end</span><span class="p">],</span> <span class="n">rate</span><span class="o">=</span><span class="n">samplerate</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt output_prompt">Out[5]:</div>
<div class="output_html rendered_html output_subarea output_execute_result">
<audio controls="controls" >
<source src="data:audio/wav;base64,UklGRnR1DQBXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YVB1DQCwHj0fqB8OIG8griDcIAQh7SDPIKYgeyDRH9weQh7KHfEcsxupGt8ZFBkCGMQWfhWZFNoT7xIGEgIRIhBuD+AOZA4TDn4NCg3+DBsNQw0HDeYM+Qx4DdwN6A0ODlwOJw7+DfUN2A3ODdsN4Q34DRcO6A2gDTUN0wybDFIMGAzIC0wLzgp6CioKAwoeCukJrQmgHF8cERyIG8IawxkjGZsY3hcYF0IWohVGFRIVABX7FOgUlxRRFAsUixQ4FcMVbxbyFncXAhiAGLAYzhjQGAwZEBkBGasYYhhVGD0Y6xcoF2sW4BVtFesUKRQbEwkSEBFREJUPqQ7PDcgMrwvjChYKQwlrCM0HRwemBoAGZwYbBrAFUgXRBKUEYQQJBAwEEARjBL4EygQBBUcFlAWvBdQFcQbZBgwHcQc+CNYIGglyCdQJBgrpCXcKDAtgC2MLhQvZC7cLQQvWCm4KCgoRCv4JtQmHCREJagjaBycHygZGBtEFfAULBXIEmQMDA9QCpQKhAt0CvAJ0AlsCWQKGAmICgQLoAhcDIwMgA3ED8QNk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+jX7mfvP+5v7Vfsc++j6lfrv+Zf5tfiR96v21/Vq9RP1a/RX8yLyFPFy8L/vRO8N78nuPu7G7SrtpuzM7OHs3eyM7CTs+us/7HHsVexb7JTsOe3M7RjuB+417o3uG+9873DvWu9+75fvi+/N7wPw9O/d797v2O//7/Hv+e/z7yHwMPD87wvw2O9P7+3u8e7j7tHuZO4b7g3urO0V7ZXsdOxN7BfssOto6xHr
Your browser does not support the audio element.
</audio>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>As expected, our reconstructed audio does sound incredibly poor! But there's something else very interesting going on here under the hood. Did you notice that the bassline comes across very well, but that there's no midrange or treble? The drums are almost entirely gone.</p>
<h2 id="Drop-the-(Treble)"><a href="https://youtu.be/Ua0KpfJsxKo?t=1m17s">Drop the (Treble)</a><a class="anchor-link" href="#Drop-the-(Treble)">&#182;</a></h2><p>It will help to understand PCA more fully when trying to read this part, but I'll do my best to break it down. PCA tries to find a way to best represent the dataset using "components." Think of each "component" as containing some of the information you need in order to reconstruct the full audio. For example, you might have a "low frequency" component that contains all the information you need in order to hear the bassline. There might be other components that explain the high frequency things like singers, or melodies, that you also need.</p>
<p>What makes PCA interesting is that it attempts to find the "most important" components in explaining the signal. In a signal processing world, this means that PCA is trying to find the signal amongst the noise in your data. In our case, this means that PCA, when forced to work with small numbers of components, will chuck out the noisy components first. It's doing it's best job to reconstruct the signal, but it has to make sacrifices somewhere.</p>
<p>So I've mentioned that PCA identifies the "noisy" components in our dataset. This is equivalent to saying that PCA removes the "high frequency" components in this case: it's very easy to represent a low-frequency signal like a bassline. It's far more difficult to represent a high-frequency signal because it's changing all the time. When you force PCA to make a tradeoff by using a small number of components, the best it can hope to do is replicate the low-frequency sections and skip the high-frequency things.</p>
<p>This is a very interesting insight, and it also has echos (pardon the pun) of how humans understand music in general. Other encoding schemes (like MP3, etc.) typically chop off a lot of the high-frequency range as well. There is typically a lot of high-frequency noise in audio that is nearly impossible to hear, so it's easy to remove it without anyone noticing. PCA ends up doing something similar, and while that certainly wasn't the intention, it is an interesting effect.</p>
<h2 id="A-More-Realistic-Example">A More Realistic Example<a class="anchor-link" href="#A-More-Realistic-Example">&#182;</a></h2><p>So we've seen the edge cases so far: Using a large number of components results in audio very close to the original, and using a small number of components acts as a low-pass filter. How about we develop something that sounds "good enough" in practice, that we can use as a benchmark for size? We'll use ourselves as judges of audio quality, and build another function to help us estimate how much space we need to store everything in.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span class="kn">from</span> <span class="nn">bz2</span> <span class="k">import</span> <span class="n">compress</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="k">def</span> <span class="nf">raw_estimate</span><span class="p">(</span><span class="n">transformed</span><span class="p">,</span> <span class="n">pca</span><span class="p">):</span>
<span class="c1"># We assume that we&#39;ll be storing things as 16-bit WAV,</span>
<span class="c1"># meaning two bytes per sample</span>
<span class="n">signal_bytes</span> <span class="o">=</span> <span class="n">transformed</span><span class="o">.</span><span class="n">tobytes</span><span class="p">()</span>
<span class="c1"># PCA stores the components as floating point, we&#39;ll assume</span>
<span class="c1"># that means 32-bit floats, so 4 bytes per element</span>
<span class="n">component_bytes</span> <span class="o">=</span> <span class="n">transformed</span><span class="o">.</span><span class="n">tobytes</span><span class="p">()</span>
<span class="c1"># Return a result in megabytes</span>
<span class="k">return</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">signal_bytes</span><span class="p">)</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">component_bytes</span><span class="p">))</span> <span class="o">/</span> <span class="p">(</span><span class="mi">2</span><span class="o">**</span><span class="mi">20</span><span class="p">)</span>
<span class="c1"># Do an estimate for lossless compression applied on top of our</span>
<span class="c1"># PCA reduction</span>
<span class="k">def</span> <span class="nf">bz2_estimate</span><span class="p">(</span><span class="n">transformed</span><span class="p">,</span> <span class="n">pca</span><span class="p">):</span>
<span class="n">bytestring</span> <span class="o">=</span> <span class="n">transformed</span><span class="o">.</span><span class="n">tobytes</span><span class="p">()</span> <span class="o">+</span> <span class="n">b</span><span class="s1">&#39;;&#39;</span> <span class="o">+</span> <span class="n">pca</span><span class="o">.</span><span class="n">components_</span><span class="o">.</span><span class="n">tobytes</span><span class="p">()</span>
<span class="n">compressed</span> <span class="o">=</span> <span class="n">compress</span><span class="p">(</span><span class="n">bytestring</span><span class="p">)</span>
<span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">compressed</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="mi">2</span><span class="o">**</span><span class="mi">20</span><span class="p">)</span>
2016-11-01 16:19:21 -04:00
2018-01-16 20:28:29 -05:00
<span class="n">compression_attempts</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span>
<span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">32</span><span class="p">),</span>
<span class="p">(</span><span class="mi">16</span><span class="p">,</span> <span class="mi">256</span><span class="p">),</span>
<span class="p">(</span><span class="mi">32</span><span class="p">,</span> <span class="mi">256</span><span class="p">),</span>
<span class="p">(</span><span class="mi">64</span><span class="p">,</span> <span class="mi">256</span><span class="p">),</span>
<span class="p">(</span><span class="mi">128</span><span class="p">,</span> <span class="mi">1024</span><span class="p">),</span>
<span class="p">(</span><span class="mi">256</span><span class="p">,</span> <span class="mi">1024</span><span class="p">),</span>
<span class="p">(</span><span class="mi">512</span><span class="p">,</span> <span class="mi">1024</span><span class="p">),</span>
<span class="p">(</span><span class="mi">128</span><span class="p">,</span> <span class="mi">2048</span><span class="p">),</span>
<span class="p">(</span><span class="mi">256</span><span class="p">,</span> <span class="mi">2048</span><span class="p">),</span>
<span class="p">(</span><span class="mi">512</span><span class="p">,</span> <span class="mi">2048</span><span class="p">),</span>
<span class="p">(</span><span class="mi">1024</span><span class="p">,</span> <span class="mi">2048</span><span class="p">)</span>
<span class="p">]</span>
2016-11-01 16:19:21 -04:00
2018-01-16 20:28:29 -05:00
<span class="k">def</span> <span class="nf">build_estimates</span><span class="p">(</span><span class="n">signal</span><span class="p">,</span> <span class="n">n_components</span><span class="p">,</span> <span class="n">block_size</span><span class="p">):</span>
<span class="n">pca</span><span class="p">,</span> <span class="n">transformed</span><span class="p">,</span> <span class="n">recon</span> <span class="o">=</span> <span class="n">pca_reduce</span><span class="p">(</span><span class="n">tabulasa_left</span><span class="p">,</span> <span class="n">n_components</span><span class="p">,</span> <span class="n">block_size</span><span class="p">)</span>
<span class="n">raw_pca_estimate</span> <span class="o">=</span> <span class="n">raw_estimate</span><span class="p">(</span><span class="n">transformed</span><span class="p">,</span> <span class="n">pca</span><span class="p">)</span>
<span class="n">bz2_pca_estimate</span> <span class="o">=</span> <span class="n">bz2_estimate</span><span class="p">(</span><span class="n">transformed</span><span class="p">,</span> <span class="n">pca</span><span class="p">)</span>
<span class="n">raw_size</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">recon</span><span class="o">.</span><span class="n">tobytes</span><span class="p">())</span> <span class="o">/</span> <span class="p">(</span><span class="mi">2</span><span class="o">**</span><span class="mi">20</span><span class="p">)</span>
<span class="k">return</span> <span class="n">raw_size</span><span class="p">,</span> <span class="n">raw_pca_estimate</span><span class="p">,</span> <span class="n">bz2_pca_estimate</span>
<span class="n">pca_compression_results</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([</span>
<span class="n">build_estimates</span><span class="p">(</span><span class="n">tabulasa_left</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">bs</span><span class="p">)</span>
<span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">bs</span> <span class="ow">in</span> <span class="n">compression_attempts</span>
<span class="p">])</span>
<span class="n">pca_compression_results</span><span class="o">.</span><span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;Raw&quot;</span><span class="p">,</span> <span class="s2">&quot;PCA&quot;</span><span class="p">,</span> <span class="s2">&quot;PCA w/ BZ2&quot;</span><span class="p">]</span>
<span class="n">pca_compression_results</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="n">compression_attempts</span>
<span class="n">pca_compression_results</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt output_prompt">Out[6]:</div>
<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Raw</th>
<th>PCA</th>
<th>PCA w/ BZ2</th>
</tr>
</thead>
<tbody>
<tr>
<th>(1, 1)</th>
<td>69.054298</td>
<td>138.108597</td>
<td>16.431797</td>
</tr>
<tr>
<th>(1, 2)</th>
<td>69.054306</td>
<td>69.054306</td>
<td>32.981380</td>
</tr>
<tr>
<th>(1, 4)</th>
<td>69.054321</td>
<td>34.527161</td>
<td>16.715032</td>
</tr>
<tr>
<th>(4, 32)</th>
<td>69.054443</td>
<td>17.263611</td>
<td>8.481735</td>
</tr>
<tr>
<th>(16, 256)</th>
<td>69.054688</td>
<td>8.631836</td>
<td>4.274846</td>
</tr>
<tr>
<th>(32, 256)</th>
<td>69.054688</td>
<td>17.263672</td>
<td>8.542909</td>
</tr>
<tr>
<th>(64, 256)</th>
<td>69.054688</td>
<td>34.527344</td>
<td>17.097543</td>
</tr>
<tr>
<th>(128, 1024)</th>
<td>69.054688</td>
<td>17.263672</td>
<td>9.430644</td>
</tr>
<tr>
<th>(256, 1024)</th>
<td>69.054688</td>
<td>34.527344</td>
<td>18.870387</td>
</tr>
<tr>
<th>(512, 1024)</th>
<td>69.054688</td>
<td>69.054688</td>
<td>37.800940</td>
</tr>
<tr>
<th>(128, 2048)</th>
<td>69.062500</td>
<td>8.632812</td>
<td>6.185015</td>
</tr>
<tr>
<th>(256, 2048)</th>
<td>69.062500</td>
<td>17.265625</td>
<td>12.366942</td>
</tr>
<tr>
<th>(512, 2048)</th>
<td>69.062500</td>
<td>34.531250</td>
<td>24.736506</td>
</tr>
<tr>
<th>(1024, 2048)</th>
<td>69.062500</td>
<td>69.062500</td>
<td>49.517493</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>As we can see, there are a couple of instances where we do nearly 20 times better on storage space than the uncompressed file. Let's here what that sounds like:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">reconstructed</span> <span class="o">=</span> <span class="n">pca_reduce</span><span class="p">(</span><span class="n">tabulasa_left</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">256</span><span class="p">)</span>
<span class="n">Audio</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">reconstructed</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">end</span><span class="p">],</span> <span class="n">rate</span><span class="o">=</span><span class="n">samplerate</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt output_prompt">Out[7]:</div>
<div class="output_html rendered_html output_subarea output_execute_result">
<audio controls="controls" >
<source src="data:audio/wav;base64,UklGRnR1DQBXQVZFZm10IBAAAAABA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nkRgRB6D4kObQ0YDOkKygmhCLAHrAbDBRgFlQRBBFQErwQZBZgFKQbwBs0HpwikCcsK7AsGDQUOzA5fD7wPzw+/D5EPKQ+VDv0NVQ2oDAMMZAvYCocKRwr3CYoJWgmKCaUJswnQCQEKGAocChkKFwoRCgQK3QmcCToJvQhICNQHMAc+BicFCwTnAqsBhwB1/zb+Q/2R/Nr7Tfvz+rz6n/qr+gX7nPtZ/Dv9IP78/uL/BQEFAtECnwN2BHAFTgbvBo8HGQh0CLUI3Aj7CBoJBAnRCJAIFwiMB+MGGAZfBXMEiQPNAhYCcQEFAcMAewA0AP3/8//O/7n/pf9s/17/Tf/8/qX+PP7d/Y39Av1s/Jv70fpi+vH5hflD+Qv57fgT+W75CfrR+rf7w/ze/R3/dwC3AfACNwRrBX4GZAcoCLcILQmKCagJnAlDCZoIrgHmAaMBAgH3/3X+7PxK+1v5Ovfw9NTyAPFE78ztS+w665TqBOq/6anp0+lL6trqmOtc7DftSu4z7+jvvPCT8W3yNPOs8xL0aPSZ9JT0L/SF8/bySfJ18ePwOPCj72zvFO8V73bv3++o8HXxTfJQ82L0ovXD9uT39Pjq+QT77vuY/Hn9SP7T/i7/L/8O/9L+PP6r/Q39Ffwq+xz6/vhE+Kz3//Zd9oT1u/Q19HjzuPLw8Tnx6PCw8HXwOfAG8BfwVPBw8HjwZ/CZ8AHxTvFy8YXxgvGY8aTxkfGb8YnxW/Ey8SHxJvET8c7wuvDC8ODwL/Gg8Q3ycfK08hnzcvOt87XzWfPe8kDyRfEr8P7unu1A7NrqS+nR52bm
Your browser does not support the audio element.
</audio>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>It sounds incredibly poor though. Let's try something that's a bit more realistic:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">reconstructed</span> <span class="o">=</span> <span class="n">pca_reduce</span><span class="p">(</span><span class="n">tabulasa_left</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="n">Audio</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">reconstructed</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">end</span><span class="p">],</span> <span class="n">rate</span><span class="o">=</span><span class="n">samplerate</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt output_prompt">Out[8]:</div>
<div class="output_html rendered_html output_subarea output_execute_result">
<audio controls="controls" >
<source src="data:audio/wav;base64,UklGRnR1DQBXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YVB1DQCAFMkUyRSAFPwNLg4uDvwNsRL0EvMSsRIpIJsgmyAqIFAfvh+/H1AfXBSlFKUUXRQnBDcENwQnBPMA9wD3APMAxwTZBNkExwSxB80HzQexB5AQyxDLEJAQVxamFqYWVxbdFzIYMhjdF18VqxWrFV8VpgW7BbsFpgUF+vL58vkF+nIGigaKBnIGSxSTFJMUTBTAHSkeKR7AHdIWIxcjF9IWExJTElMSExLnDRkOGQ7nDd8MDQ0NDd8MLA5eDl4OLA6eC8gLyAueC8wQCBEIEcwQoRj4GPgYoRjdGjwbPBveGkkWmBaYFkkW8A4mDyYP8Q7NBuYG5gbNBjkHVAdUBzkHJQxQDFAMJQyrB8cHxwerB/X+8/7z/vX+JAMwAzADJANnAnACcAJmAoL+fv59/oH+Hwc5BzkHHweuDuMO4w6uDtgPERARENgPiwenB6cHiwdbA2gDaANbAyb/JP8k/yX/sQO/A78DsQMDDC4MLgwDDGcPng+eD2cPzQXiBeIFzQVX+0j7R/tW+zr2GfYZ9jn2VPtF+0X7VPv4Awg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jfwl/DL8Df2FvYW9jf25P3e/d795P1u/23/bf9u/zn6Jvom+jn6Mvwm/Cb8Mvzb/dT91P3b/bX4nfic+LX44fW/9b/14fUI8dXw1fAI8RDryOrI6g/rEPLh8eHxEPKz95j3l/ez9331WfVZ9Xz1O/EI8QjxOvEI7cfsx+wH7dbum+6b7tXusvSM9Iz0svTD96j3qPfD98T5sPmv+cT5DfTl8+XzDfS84VThU+G84fDZ
Your browser does not support the audio element.
</audio>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>And just out of curiosity, we can try something that has the same ratio of components to block size. This should be close to an apples-to-apples comparison.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">reconstructed</span> <span class="o">=</span> <span class="n">pca_reduce</span><span class="p">(</span><span class="n">tabulasa_left</span><span class="p">,</span> <span class="mi">64</span><span class="p">,</span> <span class="mi">256</span><span class="p">)</span>
<span class="n">Audio</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">reconstructed</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">end</span><span class="p">],</span> <span class="n">rate</span><span class="o">=</span><span class="n">samplerate</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt output_prompt">Out[9]:</div>
<div class="output_html rendered_html output_subarea output_execute_result">
<audio controls="controls" >
<source src="data:audio/wav;base64,UklGRnR1DQBXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YVB1DQBoFy4UbBFVD74N1gxnDBwM9gxsD/oSKBfUGnUd7R5KH7Ie4B35HGMb9BhEFZwQEgwbCL4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JmQteDccOmg/uD8MPNw/YDpUOjg7fDrMPfxDGEHoQOA8vDYwKiQfhBL4CgwE6AV4BpAFMAlADdAT9BZoHTQnsCosMsQ0yDtoNHA12DJcLdwq4CAwGCAMCALT8G/pI+H/3jvio+rX83f6iAIYBjQFhANr+fv1y/Jr7JfvK+mD6E/oc+ur6Gf1UAKkEWglEDToQ+RF3EkYRlA76ClIHUwSaAisCOAIVArABtgBq/3j+Of5U/14BQQMkBUgG9gWuBHsC2f8g/Uz7Tvvk/In/pgKzBN0ESgMjAI381/hR9kr1uPU69yP5jfss/sP/wgBVAfwA9P8U/pn7vvkQ+WL5uPqO/Kv+JAFlAzQFHgeOCDgJgAmxCdQJ6QmoCQYJQQNnAX3+S/sa+Vf4HvnS+k78Qf1q/R79R/x2+mD3H/Pc7jTrG+ir5SDkY+Oh4xvl4+eq6/LvYfSA+Pv7Zv5P/+T+2Pxh+Vj1o/EB76ftTe0i7m7vxvDh8ZbyRPNh9Jv12fWS9bb0WfNg8q/xhfFN8rzzzfUa+Av66PuM/Q7/FgAbAMz/Bf/f/bX8W/sO+pT5Ffpd+x/9Zf48/z3/7/02/Jn6cPmi+AT4k/cW90/2XvVw9BXzbvFz737tPOwW7Brt7e6D8ST0fPY++BL5HfmW+EH34vVV9d70OPRc8y3yffBH72buk+167frtb+9R8bXyqPOY9Jv1lPaX97n4bPl/+YX5hvnk+Tb6bvlh9yb06+/w6q7lqeDr3Ajb
Your browser does not support the audio element.
</audio>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>The smaller block size definitely has better high-end response, but I personally think the larger block size sounds better overall.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Conclusions">Conclusions<a class="anchor-link" href="#Conclusions">&#182;</a></h2><p>So, what do I think about audio compression using PCA?</p>
<p>Strangely enough, it actually works pretty well relative to what I expected. That said, it's a terrible idea in general.</p>
<p>First off, you don't really save any space. The component matrix needed to actually run the PCA algorithm takes up a lot of space on its own, so it's very difficult to save space without sacrificing a huge amount of audio quality. And even then, codecs like AAC sound very nice even at bitrates that this PCA method could only dream of.</p>
<p>Second, there's the issue of audio streaming. PCA relies on two components: the datastream, and a matrix used to reconstruct the original signal. While it is easy to stream the data, you can't stream that matrix. And even if you divided the stream up into small blocks to give you a small matrix, you must guarantee that the matrix arrives; if you don't have that matrix, the data stream will make no sense whatsoever.</p>
<p>All said, this was an interesting experiment. It's really cool seeing PCA used for signal analysis where I haven't seen it applied before, but I don't think it will lead to any practical results. Look forward to more signal processing stuff in the future!</p>
</div>
</div>
</div></p>
<div class="comments">
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'bradleespeice';
var disqus_identifier = 'audio-compression-using-pca.html';
var disqus_url = 'https://bspeice.github.io/audio-compression-using-pca.html';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the comments.</noscript>
</div>
2016-11-01 16:19:21 -04:00
</div>
<!-- /Content -->
<!-- Footer -->
<div class="footer gradient-2">
<div class="container footer-container ">
<div class="row">
<div class="col-xs-4 col-sm-3 col-md-3 col-lg-3">
<div class="footer-title"></div>
<ul class="list-unstyled">
2018-01-16 20:28:29 -05:00
<li><a href="https://bspeice.github.io/feeds/all.atom.xml" type="application/atom+xml" rel="alternate"></a></li>
2016-11-01 16:19:21 -04:00
</ul>
</div>
<div class="col-xs-4 col-sm-3 col-md-3 col-lg-3">
<div class="footer-title"></div>
<ul class="list-unstyled">
<li><a href="https://github.com/bspeice" target="_blank">Github</a></li>
<li><a href="https://www.linkedin.com/in/bradleespeice" target="_blank">LinkedIn</a></li>
</ul>
</div>
<div class="col-xs-4 col-sm-3 col-md-3 col-lg-3">
</div>
<div class="col-xs-12 col-sm-3 col-md-3 col-lg-3">
<p class="pull-right text-right">
<small><em>Proudly powered by <a href="http://docs.getpelican.com/" target="_blank">pelican</a></em></small><br/>
<small><em>Theme and code by <a href="https://github.com/molivier" target="_blank">molivier</a></em></small><br/>
<small></small>
</p>
</div>
</div>
</div>
</div>
<!-- /Footer -->
</body>
</html>