{"id":1097,"date":"2017-09-27T20:50:43","date_gmt":"2017-09-27T18:50:43","guid":{"rendered":"http:\/\/www.fabienm.eu\/wordpress\/?p=1097"},"modified":"2017-11-12T15:53:39","modified_gmt":"2017-11-12T13:53:39","slug":"analog-input-with-usb-soundcard-on-linux-part2","status":"publish","type":"post","link":"http:\/\/www.fabienm.eu\/wordpress\/2017\/09\/27\/analog-input-with-usb-soundcard-on-linux-part2\/","title":{"rendered":"Analog input with usb soundcard on Linux [Part2]"},"content":{"rendered":"<p><a href=\"http:\/\/www.fabienm.eu\/wordpress\/?p=1084\">[&lt;&lt; part1]<\/a><\/p>\n<p>Ok, we <a href=\"http:\/\/www.fabienm.eu\/wordpress\/?p=1084\">seen on a first article<\/a> how to record signal from the input of an usb sound card. But our objective is not to just \u00abear the sound of current\u00bb but our objective is to make a measurement.<\/p>\n<p><strong>Displaying waves with python<\/strong><\/p>\n<p>For quick and dirty displaying we can use the opensource sound software <a href=\"http:\/\/audacity.fr\/\">audacity<\/a> . But for more computation, and for the end application I used python with the following script found on stackoverflow:<\/p>\n<pre>import matplotlib.pyplot as plt\r\nimport numpy as np\r\nimport wave\r\nimport sys\r\n\r\nspf = wave.open('foobar.wav','r')\r\n\r\n#Extract Raw Audio from Wav File\r\nsignal = spf.readframes(-1)\r\nsignal = np.fromstring(signal, 'Int16')\r\n\r\nplt.figure(1)\r\nplt.title('Signal Wave...')\r\nplt.plot(signal)\r\nplt.show()\r\n<\/pre>\n<p><strong>Saturation<\/strong><\/p>\n<p>There is a problem with sound card <a href=\"https:\/\/en.wikipedia.org\/wiki\/Line_level\">Line in<\/a> : its voltage level limit <a href=\"https:\/\/electronics.stackexchange.com\/questions\/76010\/what-is-the-typical-max-voltage-for-sound-card-line-in\">is low<\/a> ~400mVpp depend to volume input control. Then recorded signal is dramatically saturated by the sound card. As we can see on the following measurement curves<\/p>\n<div id=\"attachment_1106\" style=\"width: 822px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1106\" class=\"size-full wp-image-1106\" src=\"http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/toaster_satured.png\" alt=\"\" width=\"812\" height=\"612\" srcset=\"http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/toaster_satured.png 812w, http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/toaster_satured-300x226.png 300w, http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/toaster_satured-768x579.png 768w\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" \/><p id=\"caption-attachment-1106\" class=\"wp-caption-text\">toaster current curve : saturated<\/p><\/div>\n<div id=\"attachment_1105\" style=\"width: 822px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1105\" class=\"size-full wp-image-1105\" src=\"http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/kettle_toaster_satured.png\" alt=\"\" width=\"812\" height=\"612\" srcset=\"http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/kettle_toaster_satured.png 812w, http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/kettle_toaster_satured-300x226.png 300w, http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/kettle_toaster_satured-768x579.png 768w\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" \/><p id=\"caption-attachment-1105\" class=\"wp-caption-text\">Kettle and toaster current curve : saturated<\/p><\/div>\n<p>To solve this problem, I simply soldered resistor divider to divide by ~10 the input signal level. Then I get good signal entry.<\/p>\n<div id=\"attachment_1118\" style=\"width: 970px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1118\" class=\"size-full wp-image-1118\" src=\"http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/voltage_dividor.png\" alt=\"\" width=\"960\" height=\"320\" srcset=\"http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/voltage_dividor.png 960w, http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/voltage_dividor-300x100.png 300w, http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/voltage_dividor-768x256.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><p id=\"caption-attachment-1118\" class=\"wp-caption-text\">Voltage dividor<\/p><\/div>\n<p>To unsure that signal level will not saturate, don&rsquo;t forget to set input sound level at maximum with alsamixer :<\/p>\n<pre>$ alsamixer<\/pre>\n<p>We can then obtain a not saturated signal :<\/p>\n<div id=\"attachment_1109\" style=\"width: 822px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1109\" class=\"size-full wp-image-1109\" src=\"http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/toaster_kettle_notsat.png\" alt=\"\" width=\"812\" height=\"612\" srcset=\"http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/toaster_kettle_notsat.png 812w, http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/toaster_kettle_notsat-300x226.png 300w, http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/toaster_kettle_notsat-768x579.png 768w\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" \/><p id=\"caption-attachment-1109\" class=\"wp-caption-text\">Toaster plus kettle current curve not saturated<\/p><\/div>\n<p><strong>Plugging it on raspberrypi and electric distribution board.<\/strong><\/p>\n<p>In my house, the electric distribution board and internet box are under the same cupboard then it&rsquo;s easy to connect current probe + raspberrypi + usb sound card and it&rsquo;s power (tricks: to power the raspberrypi you can use the usb plug from your box instead of power wall adapt).<\/p>\n<div id=\"attachment_1111\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1111\" class=\"size-full wp-image-1111\" src=\"http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/electric_current_plug_house.jpeg\" alt=\"\" width=\"600\" height=\"450\" srcset=\"http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/electric_current_plug_house.jpeg 600w, http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/electric_current_plug_house-300x225.jpeg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><p id=\"caption-attachment-1111\" class=\"wp-caption-text\">Wire plug in my electric cupboard<\/p><\/div>\n<p><strong>Visualize the sound of your house electric consumption<\/strong><\/p>\n<p>I can now make measurement from the raspberrypi then download wave on computer to see the my house electric consumption.<\/p>\n<pre>ssh pi@192.168.0.111\r\narecord -d 10 -f cd -D plughw:1 -t wav foobar.wav\r\n<\/pre>\n<p>This above command give is not stopping after ten seconds like on my laptop and give me multiple files of 44Bytes. I don&rsquo;t know why ?<br \/>\nOnce it recorded we can download it with scp on laptop :<\/p>\n<pre>$ scp pi@192.168.0.111:~\/foobar-* .\r\n<\/pre>\n<p>Then display it with my little python script :<\/p>\n<div id=\"attachment_1113\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1113\" class=\"size-large wp-image-1113\" src=\"http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/maison_rasp_allumage_cuisson-1024x529.png\" alt=\"\" width=\"640\" height=\"331\" srcset=\"http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/maison_rasp_allumage_cuisson-1024x529.png 1024w, http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/maison_rasp_allumage_cuisson-300x155.png 300w, http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/09\/maison_rasp_allumage_cuisson-768x397.png 768w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><p id=\"caption-attachment-1113\" class=\"wp-caption-text\">Electric consumption before and after power on the inductive hot plate for boiling water.<\/p><\/div>\n<p>&nbsp;<\/p>\n<p>In a futur article we will see how to calculate the RMS current with a deamon and save it in raspberrypi to see the consumption history.<\/p>\n<p><a href=\"http:\/\/www.fabienm.eu\/wordpress\/?p=1121\">[ Part3 &gt;&gt;]<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[&lt;&lt; part1] Ok, we seen on a first article how to record signal from the input of an usb sound card. But our objective is not to just \u00abear the sound of current\u00bb but our objective is to make a &hellip; <a href=\"http:\/\/www.fabienm.eu\/wordpress\/2017\/09\/27\/analog-input-with-usb-soundcard-on-linux-part2\/\">Continuer la lecture <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[106,10],"tags":[178,166,176,83,175,174,19,177,173],"class_list":["post-1097","post","type-post","status-publish","format-standard","hentry","category-embarque","category-informatique","tag-carte-son","tag-consommation","tag-courant","tag-electricite","tag-electronique","tag-embarque","tag-linux","tag-oscilloscope","tag-raspberrypi"],"_links":{"self":[{"href":"http:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/posts\/1097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/comments?post=1097"}],"version-history":[{"count":15,"href":"http:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/posts\/1097\/revisions"}],"predecessor-version":[{"id":1140,"href":"http:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/posts\/1097\/revisions\/1140"}],"wp:attachment":[{"href":"http:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/media?parent=1097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/categories?post=1097"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/tags?post=1097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}