Welcome, guest | Sign In | My Account | Store | Cart

Hi experts...

I think that this is a first... ;o)

Another kids level project to do for yourselves...

This is yet another Python DEMO to show the power of the sound card using Linux as an audio frequency counter.

It is a kids level project that uses the sound card as an input through the microphone socket to measure up to >3500Hz from a symmetrical waveform, sine square or triangle. See the comments inside the code for more details...

The only external hardware required is a test lead; oh my, this is technical stuff... ;o)

This is for Python 2.x.x, (probably even down to 1.5.2) but it would be just as easy to make it work on 3.x.x. I've already done it......

......But I'll let the big guns do that for you...

Enjoy finding simple solutions to often VERY difficult problems...

Be aware of word wrapping etc...

Bazza, G0LCU...

Python, 114 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Freq_Counter2x.py...
#
# A DEMO audio frequency counter using standard text mode Python.
# (C)2011, B.Walker, G0LCU. Issued as Public Domain.
#
# "/dev/dsp" is required for this to work. If it doesn't exist install
# "oss-compat" from your distro's repository.
# Ensure thet the sound is set up from your distro's audio mixing SW.
#
# 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
# 2.5.2, 2.6.6 and 2.7.2. It would probably work down to Python 1.5.2
# but has not been tested to that level.
#
# It will display from <50 Hz to >3500 Hz to withing 0.1% accuracy.
# With a very simple external hardware add-on <30 Hz to >35 KHz is well
# within this DEMOs scope; however for this DEMO only a simple connecting
# cable is needed. Also with a simple code change it is possible to make
# the accuracy to within 0.01% and <5Hz bottom limit.
#
# 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/
#
# This is even easier in Python 3.x.x but I'll let the big guns do that for you...
# I HAVE already done it, uploaded it elsewhere and issued it as Public Domain...
#
# This DEMO just requires a virgin text mode Python install and NO imports
# are required either. Banging the metal without special imports or libraries
# and having some functional use too! Good eh!... ;o)
#
# 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 plug(s) stereo cable from the earphone output
	# to the microphone input; or from a sine/square generator into the
	# microphone input.
	#
	# Using the 1 KHz Audio Function Generator, found here......
	# http://code.activestate.com/recipes/577592-simple-1khz-audio-function-generator-using-standar/?in=user-4177147
	# ......from another Python shell start up this code and generate a sine,
	# square or triangle wave symmetrical waveform.
	#
	# Do NOT exceed 2 Volts peak to peak from an external signal generator!
	#
	# Enter a continuous loop.
	# Grab a file from my Laptop`s, Notebook`s or Netbook`s microphone socket.
	while 1:
		# Do a 1 second recorded burst...
		audio=file('/dev/dsp', 'rb')
		# "record" is the "binary 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.
			# (For those that don't know.)
			#
			#                  +------+      +---
			# Square wave:-    | Mark |Space |
			#               ---+      +------+
			#
			# 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 ord(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 ord(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
			# "freq" will become the frequency of a symmetrical waveform
			# when the above loops are finally exited, n>=8000...
			# Tick up the freq(uency) per "mark to space" cycle.
			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.

This is yet another kids level piece of test equipment that with careful use of the code can be easily made much more accurate and sophisticated.

You don't need a Maths or Physics Doctorate to do these things for yourselves, just someone to lay down the ideas so that you can expand and make them much better...

Good luck guys and gals, and......

Enjoy finding simple solutions to often VERY difficult problems...

Bazza, G0LCU...

1 comment

Barry Walker (author) 5 years, 5 months ago  # | flag

To the person who voted this up on an unknown date to me...

Many thanks.