Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

A very unusual Python program...

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
Bazza
LXF regular


Joined: Sat Mar 21, 2009 11:16 am
Posts: 1474
Location: Loughborough

PostPosted: Tue Nov 01, 2011 11:16 pm    Post subject: A very unusual Python program... Reply with quote

Hi all...

The code just about says it all, for Python 3.x.x...

It is a <50 Hz to >3500 Hz audio frequency counter with the ability
to go to an even much greater range with simple home brew electronics...

Intended for the youngster to understand as well as the experts.

Be aware of word wrapping etc...

Enjoy finding simple solutions to often very difficult problems... ;o)

Bazza...

Code:
# Freq_Counter3x.py...
#
# A DEMO audio frequency counter using standard text mode Python.
# (C)2011, B.Walker, G0LCU. Issued as Public Domain EXCLUSIVELY to LXF.
#
# "/dev/dsp" is required for this to work. If it doesn't exist install
# "oss-compat" from your distro's repository.
# Written in such a way that youngsters can understand what is going on.
#
# Tested on PCLinuxOS 2009 and Debian 6.0.0 using Python versions
# 3.0.1, 3.1.3 and 3.2.2.
#
# It will display from <50 Hz to >3500 Hz to withing 0.1% accuracy.
# With a very simple external hardware add-on AND sine wave, <10 Hz to
# >35 KHz is well within this DEMOs scope; however for this DEMO only a
# simple connecting cable is needed. This is yet another ultra simple
# piece of audio test gear that can be made and understood by a 10 year
# old...
# See my other DEMO uploads of very basic test gear using the sound card
# as the prime mover:-
#
# http://code.activestate.com/recipes/users/4177147/
#
# ENJOY...
#
# Bzzza, G0LCU...

def main():
   # Make variables global; my choice... ;o)
   global record
   global n
   global freq
   # Set the startup values...
   freq=0
   record=""
   n=0
   # Connect a 3.2mm jack plugs stereo cable from the audio output to
   # the microphone input; or from a sine/square generator into the
   # microphone input.
   #
   # Using the 1 KHz Audio Function Generator, (found elsewhere on this site),
   # from another Python shell start up this code and generate a square,
   # triangle or sine wave.
   # Enter a continuous loop.
   # Grab a file from my Laptop`s, Notebook`s and Netbook`s microphone socket.
   while 1:
      # Do a 1 second recorded burst...
      audio=open('/dev/dsp', 'rb')
      # "record" is the string to be counted...
      record=audio.read(8000)
      audio.close()
      # Enter another loop to do the count...
      n=0
      freq=0
      while 1:
         # Assume a square wave "mark to space" ratio of 1 to 1 is used,
         # then wait until a "space" is found.
         # This ensures that the loop cycles when NO input is
         # applied to the microphone socket.
         # Exit this loop when "mark" is found or n=8000...
         while record[n]<=127:
            n=n+1
            # Ensure as soon as n=8000 occurs it drops out of the loop.
            if n>=8000: break
         # Ensure as soon as n=8000 occurs it drops completely out of this loop.
         if n>=8000: break
         # Now the "mark" can loop until a "space" is found again and the whole
         # can cycle until n=8000...
         while record[n]>=128:
            n=n+1
            # Ensure as soon as n=8000 occurs it drops out of the loop.
            if n>=8000: break
         # Ensure as soon as n=8000 occurs it drops completely out of this loop.
         if n>=8000: break
         # This will become the frequency of a symmertical waveform
         # when the above loops are finally exited...
         freq=freq+1
      # An ultra simple clear screen line...
      # This line is not needed for the demo but added for fullness...
      print("\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")
      # Now display the value in the same place on screen every time.
      # This assumes a 24 or 25 line Python Shell window. Just modify to
      # suit your particular Shell that you use...
      print("\nA simple 50 Hz to 3500 Hz audio frequency counter.\n")
      print("(C)2011, B.Walker, G0LCU. Issued as Public Domain.\n")
      print("Accuracy is within 0.1% of the displayed frequency...\n")
      print("\n\nFrequency is", freq, "Hz...\n\n\n\n\n\n\n\n\n\n\n\n\n")
main()
# End of Freq_Counter.py DEMO.
# Enjoy finding simple solutions to often very difficult problems.

_________________
73...

Bazza, G0LCU...

Team AMIGA...
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast