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 

Starter shell Frequency Counter for the Scope...

 
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: 1476
Location: Loughborough

PostPosted: Sat Jul 13, 2013 6:53 pm    Post subject: Starter shell Frequency Counter for the Scope... Reply with quote

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:

#!/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...
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