{"id":443,"date":"2012-09-03T02:30:55","date_gmt":"2012-09-03T02:30:55","guid":{"rendered":"http:\/\/garysieling.com\/blog\/?p=443"},"modified":"2012-09-03T02:30:55","modified_gmt":"2012-09-03T02:30:55","slug":"detecting-pitches-in-music-with-r","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/detecting-pitches-in-music-with-r\/","title":{"rendered":"Detecting Pitches in music with R"},"content":{"rendered":"<p>In a previous post, I described a method to <a href=\"http:\/\/garysieling.com\/blog\/how-to-find-pitches-in-music\">detect a chord using a Fourier transform<\/a> in Java\/Scala. I&#8217;ve re-implemented the same in R, detailed below.<\/p>\n<p>This will generate an audio file containing the C-Major chord:<\/p>\n<pre>library(sound)\n\nc&lt;-261.63\ne&lt;-164.81\ng&lt;-196\n\nlen&lt;-1\ncData&lt;-Sine(c,len)\neData&lt;-Sine(e,len)\ngData&lt;-Sine(g,len)\n\naudio&lt;-normalize(cData+eData+gData)\n\nsaveSample(audio, \"out\\\\ceg.wav\", overwrite=TRUE)<\/pre>\n<p>And a series of helper functions:<\/p>\n<pre>magnitude&lt;-function(x) { sqrt(Re(x) * Re(x) + Im(x) * Im(x)) }\nmaxPitch&lt;-audio$rate\/2-1\n\nfrq&lt;-c(16.35,17.32,18.35,19.45,20.60,21.83,23.12,24.50,25.96,27.50,29.14,30.87)\nnoteNames&lt;-c(\"C\", \"C#0\/Db0\", \"D0\", \"D#0\/Eb0\", \"E0\", \"F0\", \"F#0\/Gb0\", \"G0\", \"G#0\/Ab0\", \"A0\", \"A#0\/Bb0\", \"B0\")\n\nlookup&lt;-data.frame(noteNames,frq)<\/pre>\n<p>A function to find the middle frequency in each FFT bucket:<\/p>\n<pre>idxFrq&lt;-function(idx) { (idx - .5 ) }<\/pre>\n<p>And a function to find the closest note in the lowest octave:<\/p>\n<pre>noteDist&lt;-function(note1, note2) { abs(log(note1)\/log(2) - log(note2)\/log(2)) %% 1 }<\/pre>\n<p>Now to do the actual FFT:<\/p>\n<pre>fftdata&lt;-fft(audio$sound)\nhalf&lt;-fftdata[1:maxPitch]\nindexes&lt;-c(1:maxPitch)\nmagnitudes&lt;-magnitude(half)<\/pre>\n<p>Now, from each bucket, find the note that is closest to each bucket:<\/p>\n<pre>closest&lt;-function(x){ subset(lookup, select=c(noteNames,frq),subset=(min(noteDist(frq, x)) == noteDist(frq, x)))$noteNames }\n\nnoteList&lt;-mapply(closest, indexes-0.5)\nmag&lt;-data.frame(noteList, magnitudes)\nbarplot(by(mag$magnitudes, mag$noteList, sum))<\/pre>\n<p>This results in the following image, which has peaks at C, E, and G, as expected.<\/p>\n<p><a href=\"http:\/\/172.104.26.128\/wp-content\/uploads\/2012\/09\/fft-chord.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-447\" title=\"fft-chord\" src=\"http:\/\/garysieling.com\/blog\/wp-content\/uploads\/2012\/09\/fft-chord-297x300.png\" alt=\"\" width=\"297\" height=\"300\" srcset=\"https:\/\/www.garysieling.com\/blog\/wp-content\/uploads\/2012\/09\/fft-chord-297x300.png 297w, https:\/\/www.garysieling.com\/blog\/wp-content\/uploads\/2012\/09\/fft-chord.png 586w\" sizes=\"(max-width: 297px) 100vw, 297px\" \/><\/a><\/p>\n<p>The calculation is very crude &#8211; all frequencies are mapped to a note evenly, which isn&#8217;t really correct.<\/p>\n<p>This can be adjusted by de-emphasizing the fft buckets between notes:<\/p>\n<pre>scale&lt;-function(x){ .5 + .5 * sin(pi * x*2 + pi\/2) }\nnote&lt;-function(x) { 12 * log(x\/440)\/log(2) + 49 }\nscaleData&lt;-scale(note(idxFrq(indexes)))\nscaledMagnitudes&lt;-scaleData*magnitudes\nscaledMag&lt;-data.frame(noteList, scaledMagnitudes)\n<\/pre>\n<p>This looks better &#8211; the three highest are the notes in the chord-<\/p>\n<p><a href=\"http:\/\/172.104.26.128\/wp-content\/uploads\/2012\/09\/fft-chord2.png\"><img decoding=\"async\" class=\"alignnone size-medium wp-image-454\" title=\"fft-chord2\" src=\"http:\/\/garysieling.com\/blog\/wp-content\/uploads\/2012\/09\/fft-chord2-300x278.png\" alt=\"\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post, I described a method to detect a chord using a Fourier transform in Java\/Scala. I&#8217;ve re-implemented the same in R, detailed below. This will generate an audio file containing the C-Major chord: library(sound) c&lt;-261.63 e&lt;-164.81 g&lt;-196 len&lt;-1 cData&lt;-Sine(c,len) eData&lt;-Sine(e,len) gData&lt;-Sine(g,len) audio&lt;-normalize(cData+eData+gData) saveSample(audio, &#8220;out\\\\ceg.wav&#8221;, overwrite=TRUE) And a series of helper functions: magnitude&lt;-function(x) &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/detecting-pitches-in-music-with-r\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Detecting Pitches in music with R&#8221;<\/span><\/a><\/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":[6,27],"tags":[109,178,373,450,532],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/443"}],"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=443"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/443\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}