{"id":5041,"date":"2016-09-12T00:48:55","date_gmt":"2016-09-12T00:48:55","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=5041"},"modified":"2016-09-12T00:48:55","modified_gmt":"2016-09-12T00:48:55","slug":"compute-signal-noise-ratio-audio-files-python","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/compute-signal-noise-ratio-audio-files-python\/","title":{"rendered":"Compute the Signal To Noise ratio in audio files in Python"},"content":{"rendered":"<p>Computing the &#8220;signal to noise&#8221; ratio of an audio file is pretty simple if it&#8217;s already a wav file &#8211; if not, I suggest you <a href=\"https:\/\/www.garysieling.com\/blog\/processing-mp3s-python\">convert it to one<\/a> first.<\/p>\n<p>If you&#8217;re doing a lot of these, this can take up a lot of disk space &#8211; I&#8217;m doing <a href=\"https:\/\/www.findlectures.com\/\">audio lectures<\/a>, which are on average 30mb mp3s. I&#8217;ve found it helpful to think about trying to write scripts that you can ctrl-c and re-run.<\/p>\n<p>One minor note here is that audio files are typically one or two channels (left-right), so you can potentially have two values for signal-to-noise. You should be able to add these together without issue, if you wish to get one value.<\/p>\n<p>This does [1] on the wavfile data, as [0] has the sample rate.<\/p>\n<pre lang=\"python\">\nimport scipy.io.wavfile as wavfile\nimport numpy\nimport os.path\n\ndef snr(file):\n  if (os.path.isfile(file)):\n    data = wavfile.read(fileWav)[1]\n    singleChannel = data\n    try:\n      singleChannel = numpy.sum(data, axis=1)\n    except:\n      # was mono after all\n      pass\n      \n    norm = singleChannel \/ (max(numpy.amax(singleChannel), -1 * numpy.amin(singleChannel)))\n    return stats.signaltonoise(norm)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How to compute the signal-to-noise ratio in audio files in Python<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12],"tags":[178,447],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5041"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=5041"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5041\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=5041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=5041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=5041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}