<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
<channel>
  <title>Linux Format forums</title>
  <link>http://www.linuxformat.com/forums/index.php</link>
  <description>Help, discussion, magazine feedback and more</description>
  <language>english</language>
  <copyright>(c) Copyright Mon May 20, 2013 5:43 am by Linux Format forums</copyright>
  <managingEditor>webmaster@linuxformat.com</managingEditor>
  <webMaster>webmaster@linuxformat.com</webMaster>
  <pubDate>Mon May 20, 2013 5:43 am</pubDate>
  <lastBuildDate>Mon May 20, 2013 5:43 am</lastBuildDate>
  <docs>http://backend.userland.com/rss</docs>
  <generator>phpBB2 RSS Syndication Mod by Lucas</generator>
  <ttl>1</ttl>

  <image>
    <title>Linux Format forums</title>
    <url></url>
    <link>http://www.linuxformat.com/forums/</link>
    <description>Help, discussion, magazine feedback and more</description>
  </image>

                                      <item>
                                        <title>A very unusual Python program...</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=102553#102553</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=58561'&gt;Bazza&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Tue Nov 01, 2011 11:16 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Hi all...&lt;br /&gt;
&lt;br /&gt;
The code just about says it all, for Python 3.x.x...&lt;br /&gt;
&lt;br /&gt;
It is a &amp;lt;50 Hz to &amp;gt;3500 Hz audio frequency counter with the ability&lt;br /&gt;
to go to an even much greater range with simple home brew electronics...&lt;br /&gt;
&lt;br /&gt;
Intended for the youngster to understand as well as the experts.&lt;br /&gt;
&lt;br /&gt;
Be aware of word wrapping etc...&lt;br /&gt;
&lt;br /&gt;
Enjoy finding simple solutions to often very difficult problems... ;o)&lt;br /&gt;
&lt;br /&gt;
Bazza...&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;# Freq_Counter3x.py...&lt;br /&gt;
#&lt;br /&gt;
# A DEMO audio frequency counter using standard text mode Python.&lt;br /&gt;
# &amp;#40;C&amp;#41;2011, B.Walker, G0LCU. Issued as Public Domain EXCLUSIVELY to LXF.&lt;br /&gt;
#&lt;br /&gt;
# &amp;quot;/dev/dsp&amp;quot; is required for this to work. If it doesn't exist install&lt;br /&gt;
# &amp;quot;oss-compat&amp;quot; from your distro's repository.&lt;br /&gt;
# Written in such a way that youngsters can understand what is going on.&lt;br /&gt;
#&lt;br /&gt;
# Tested on PCLinuxOS 2009 and Debian 6.0.0 using Python versions&lt;br /&gt;
# 3.0.1, 3.1.3 and 3.2.2.&lt;br /&gt;
#&lt;br /&gt;
# It will display from &amp;lt;50 Hz to &amp;gt;3500 Hz to withing 0.1% accuracy.&lt;br /&gt;
# With a very simple external hardware add-on AND sine wave, &amp;lt;10 Hz to&lt;br /&gt;
# &amp;gt;35 KHz is well within this DEMOs scope; however for this DEMO only a&lt;br /&gt;
# simple connecting cable is needed. This is yet another ultra simple&lt;br /&gt;
# piece of audio test gear that can be made and understood by a 10 year&lt;br /&gt;
# old...&lt;br /&gt;
# See my other DEMO uploads of very basic test gear using the sound card&lt;br /&gt;
# as the prime mover&amp;#58;-&lt;br /&gt;
#&lt;br /&gt;
# http&amp;#58;//code.activestate.com/recipes/users/4177147/&lt;br /&gt;
#&lt;br /&gt;
# ENJOY...&lt;br /&gt;
#&lt;br /&gt;
# Bzzza, G0LCU...&lt;br /&gt;
&lt;br /&gt;
def main&amp;#40;&amp;#41;&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;# Make variables global; my choice... ;o&amp;#41;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;global record&lt;br /&gt;
&amp;nbsp; &amp;nbsp;global n&lt;br /&gt;
&amp;nbsp; &amp;nbsp;global freq&lt;br /&gt;
&amp;nbsp; &amp;nbsp;# Set the startup values...&lt;br /&gt;
&amp;nbsp; &amp;nbsp;freq=0&lt;br /&gt;
&amp;nbsp; &amp;nbsp;record=&amp;quot;&amp;quot;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;n=0&lt;br /&gt;
&amp;nbsp; &amp;nbsp;# Connect a 3.2mm jack plugs stereo cable from the audio output to&lt;br /&gt;
&amp;nbsp; &amp;nbsp;# the microphone input; or from a sine/square generator into the&lt;br /&gt;
&amp;nbsp; &amp;nbsp;# microphone input.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;#&lt;br /&gt;
&amp;nbsp; &amp;nbsp;# Using the 1 KHz Audio Function Generator, &amp;#40;found elsewhere on this site&amp;#41;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp;# from another Python shell start up this code and generate a square,&lt;br /&gt;
&amp;nbsp; &amp;nbsp;# triangle or sine wave.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;# Enter a continuous loop.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;# Grab a file from my Laptop`s, Notebook`s and Netbook`s microphone socket.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;while 1&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# Do a 1 second recorded burst...&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;audio=open&amp;#40;'/dev/dsp', 'rb'&amp;#41;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# &amp;quot;record&amp;quot; is the string to be counted...&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;record=audio.read&amp;#40;8000&amp;#41;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;audio.close&amp;#40;&amp;#41;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# Enter another loop to do the count...&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;n=0&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;freq=0&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;while 1&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# Assume a square wave &amp;quot;mark to space&amp;quot; ratio of 1 to 1 is used,&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# then wait until a &amp;quot;space&amp;quot; is found.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# This ensures that the loop cycles when NO input is&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# applied to the microphone socket.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# Exit this loop when &amp;quot;mark&amp;quot; is found or n=8000...&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;while record&amp;#91;n&amp;#93;&amp;lt;=127&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;n=n+1&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# Ensure as soon as n=8000 occurs it drops out of the loop.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;if n&amp;gt;=8000&amp;#58; break&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# Ensure as soon as n=8000 occurs it drops completely out of this loop.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;if n&amp;gt;=8000&amp;#58; break&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# Now the &amp;quot;mark&amp;quot; can loop until a &amp;quot;space&amp;quot; is found again and the whole&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# can cycle until n=8000...&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;while record&amp;#91;n&amp;#93;&amp;gt;=128&amp;#58;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;n=n+1&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# Ensure as soon as n=8000 occurs it drops out of the loop.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;if n&amp;gt;=8000&amp;#58; break&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# Ensure as soon as n=8000 occurs it drops completely out of this loop.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;if n&amp;gt;=8000&amp;#58; break&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# This will become the frequency of a symmertical waveform&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# when the above loops are finally exited...&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;freq=freq+1&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# An ultra simple clear screen line...&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# This line is not needed for the demo but added for fullness...&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;print&amp;#40;&amp;quot;\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n&amp;quot;&amp;#41;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# Now display the value in the same place on screen every time.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# This assumes a 24 or 25 line Python Shell window. Just modify to&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;# suit your particular Shell that you use...&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;print&amp;#40;&amp;quot;\nA simple 50 Hz to 3500 Hz audio frequency counter.\n&amp;quot;&amp;#41;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;print&amp;#40;&amp;quot;&amp;#40;C&amp;#41;2011, B.Walker, G0LCU. Issued as Public Domain.\n&amp;quot;&amp;#41;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;print&amp;#40;&amp;quot;Accuracy is within 0.1% of the displayed frequency...\n&amp;quot;&amp;#41;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;print&amp;#40;&amp;quot;\n\nFrequency is&amp;quot;, freq, &amp;quot;Hz...\n\n\n\n\n\n\n\n\n\n\n\n\n&amp;quot;&amp;#41;&lt;br /&gt;
main&amp;#40;&amp;#41;&lt;br /&gt;
# End of Freq_Counter.py DEMO.&lt;br /&gt;
# Enjoy finding simple solutions to often very difficult problems.&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=102553#102553</comments>
                                        <author>Bazza</author>
                                        <pubDate>Tue Nov 01, 2011 11:16 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=102553#102553</guid>
                                      </item></channel></rss>