Starter shell Frequency Counter for the Scope...

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Starter shell Frequency Counter for the Scope...

Postby Bazza » Sat Jul 13, 2013 5:53 pm

This is the first attempt at the frequency counter for the AudioScope project as proof of concept...

It is SSLLOOWW but works a treat.

I am now working on speeding up the code by an order of magnitude using a different method completely...

It is in DEMO mode ATM and will read 1000Hz but the commented out lines for SOX or /dev/dsp can be used for real grabs...

Ensure not to comment out the line
> /tmp/sinewave.raw
as this zeros the file for appending...

Watch for wordwrapping etc...

Enjoy...
Code: Select all
#!/bin/bash

# Set the startup values...
data="?"
freq=0
number=""
subscript=0
waveform=0

# An initial screen...
clear
printf "\nA simple 50 Hz to 3500 Hz audio frequency counter.\n"
printf "(C)2013, B.Walker, G0LCU. Issued as Public Domain.\n"
printf "Accuracy is within 0.1 percent of the displayed frequency...\n"
printf "\nFrequency is "$freq"Hz...\n\n"

while true
do
   # This is a demo mode so that there is no need to access HW.
   > /tmp/sinewave.raw
   # This is the binary byte data list for the crude sinewave.
   data="\\x80\\x26\\x00\\x26\\x7F\\xD9\\xFF\\xD9"
   # Generate an 8000 byte file...
   for waveform in {0..999}
   do
           printf "$data" >> /tmp/sinewave.raw
   done
   # These two modes are for various *NIX flavours using the sound sources as required.
   # dd if=/dev/dsp of=/tmp/sinewave.raw bs=8000 count=1
   # /Users/barrywalker/Downloads/sox-14.4.0/sox -q -V0 -d -t raw -r 8000 -b 8 -c 1 -e unsigned-integer -> /tmp/sinewave.raw trim 0 00:01
   subscript=0
   freq=0
   number=`hexdump -n1 -s$subscript -v -e '1/1 "%u"' /tmp/sinewave.raw`
   while true
   do
      # 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 [ $number -le 127 ]
      do
         number=`hexdump -n1 -s$subscript -v -e '1/1 "%u"' /tmp/sinewave.raw`
         subscript=$[ ( $subscript + 1 ) ]
         # Ensure as soon as subscript >= 8000 occurs it drops out of the loop.
         if [ $subscript -ge 8000 ]
         then
            break
         fi
      done
      # Ensure as soon as subscript >= 8000 occurs it drops completely out of this loop.
      if [ $subscript -ge 8000 ]
      then
         break
      fi
      # Now the "mark" can loop until a "space" is found again and the whole
      # can cycle until subscript >= 8000...
      while [ $number -ge 128 ]
      do
         number=`hexdump -n1 -s$subscript -v -e '1/1 "%u"' /tmp/sinewave.raw`
         subscript=$[ ( $subscript + 1 ) ]
         # Ensure as soon as subscript >= 8000 occurs it drops out of the loop.
         if [ $subscript -ge 8000 ]
         then
            break
         fi
      done
      # Ensure as soon as subscript >= 8000 occurs it drops completely out of this loop.
      if [ $subscript -ge 8000 ]
      then
         break
      fi
      # "freq" will become the frequency of a symmetrical waveform
      # when the above loops are finally exited, subscript >= 8000...
      # Tick up the freq(uency) per "mark to space" cycle.
      freq=$[ ( $freq + 1 ) ]
      done
   clear
   printf "\nA simple 50 Hz to 3500 Hz audio frequency counter.\n"
   printf "(C)2013, B.Walker, G0LCU. Issued as Public Domain.\n"
   printf "Accuracy is within 0.1 percent of the displayed frequency...\n"
   printf "\nFrequency is "$freq"Hz...\n\n"

done
# End of Freq_Counter.sh DEMO.
# Enjoy finding simple solutions to often very difficult problems.
73...

Bazza, G0LCU...

Team AMIGA...
User avatar
Bazza
LXF regular
 
Posts: 1476
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron