Rough 'n Ready Audio Frequency extraction with a PIC


Closed Thread
Results 1 to 34 of 34

Hybrid View

  1. #1
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Well that sure does look clean! For your rough and ready approach, how about a simple count command like this:

    COUNT PORTB.1,100,W1

    That will count pulses during a 100ms period. Other way to do it for more accuracy is interrupts. But as you say, you just are going to do a lookup table.

    Edit:
    For the accuracy you need between D and D# etc, you would need to sample for 1 second using this method. If that's too long, you would have to go with interrupts, and measure time between pulses. Another quick and dirty try might be pulsin? Not sure how well that would work on an analog signal though. Maybe you could average a few...
    Last edited by ScaleRobotics; - 7th March 2010 at 17:37.

  2. #2


    Did you find this post helpful? Yes | No

    Default

    Here's another approach.
    You can use timer 1 as a frequency counter. Not sure if will be accurate enough? You did not say which pic you are using? Timer 1 external input is normally on portc. There should be a way to route the comparator output to tmr1? You will have to play with the pause statement to calibrate the output ie "pause 97" to compensate for the delays in pbp.


    CCP1CON = %00000101 ' Capture every rising edge
    T1CON = %00000011 ' No prescale/Osc off/Sync on/External source/TMR1 on

    Freq var word ' 100ms count result

    LOOP:

    TMR1H = 0 ' Clear Timer1 high 8-bits
    TMR1L = 0 ' Clear Timer1 low 8-bits
    T1CON.0 = 1 ' Start 16-bit timer
    Pause 100 ' Capture 100 mS of Input
    T1CON.0 = 0 ' Stop 16-bit Timer
    Freq.BYTE0 = TMR1L ' Read Low 8-bits
    Freq.BYTE1 = TMR1H ' Read High 8-bits

    GoTo LOOP

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Wink

    Hi, Hank

    Tenaja perfectly told what I was thinking to ... except it exists a CMOS version of the 567 ... much less consuming.

    now, if you measure a nice filtered sinewave ... without distortion. you must have calculated a very, very, very nice filter ... ( I'm not talking about harmonics, but other very close frequencies that can be mixed with ...).


    using the comparator section to create an interrupt , or better the CCP capture feature ... will give you the info with less than a cycle delay ...

    You call that " fun " ... small gambler you are !!!

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Guys...thanks for your input - I'll take time later to consider them all in detail.

    In the meantime, a bit more info.

    I need to capture the frequency soon after the string is plucked but not too soon else too many complex harmonics are still embedded in the signal. I'd imagine coming in to make my 'sample' about 300ms after the pluck (I have a preset PIC threshold that when breached assumes a note has been plucked, which will allow me to pause 300ms before sampling).

    I can't imagine going past 500ms after the note has been plucked (else the note will have decayed too much) so this only gives a maximum window of about 200ms to glean what the frequency is.

    If it helps, I'm using a 16F690.

    Acetronics - just seen your post - I'm using a sustainer which thankfully drops out all harmonics and leaves a relatively pure fundamental ( http://www.youtube.com/user/hankmcsp.../0/u6gm5fY1wM0 ie a nice sinewave!) Wrt to sustain - some frquencies are quite tricky to sustain, so I need to know when they're plucked to trigger special handling
    Last edited by HankMcSpank; - 7th March 2010 at 19:05.

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Got to laugh - 16f690 ...absolute mare for a noobesque personage like meself..

    I wouldn't mind but I've tackled so much with that PIC variant...but comparators? WTF?! The datasheet has ma heed buzzin.

    I've decided a nice easy entry into the world of comparators is now going to be with a simpler 12F683. Once I nail the concepts with that, then I shall take a few swigs of stiff whisky & get out the 16F690 datasheet again - were microchip having a laugh with that on?

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I've thought about doing something like that too,

    And I think without external hardware and using the comparator, the DFT idea Josuetas was working with a while back might have the best chance at detecting the notes.
    It may even be able to detect multiple notes at the same time (chords).

    I played with the spreadsheet, but didn't try any hardware.

    And whether the resolution will be tight enough to distinguish 9/8, 10/9 and 16/15ths steps, I don't know either.
    It was originally for DTMF tones.

    Might be worth a look though.
    http://www.picbasic.co.uk/forum/show...2606#post42606

    And the original AppNote (AN257) ...
    http://www.microchip.com/stellent/id...pnote=en024294

    <br>
    Last edited by Darrel Taylor; - 7th March 2010 at 23:45.
    DT

  7. #7
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Thanks Darrel....I'll check it out later.

    So here's the ludicrously simple 'get me up & running' setup I have (lifted/edited fromthe Microchip Tips' n trick sheet)



    I put a sawtooth from a sig gen on the comparator input, but get nowt on the output

    Now like I say, I always struggle with the registers - and even then, it's been a while - so try not to laugh...

    Code:
    @MyConfig = _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON  
    @MyConfig = MyConfig & _MCLRE_OFF 
    
    DEFINE OSC 4           ' Internal 4MHz
    
    CMCON0 = %00000001   	'CIN pins are configured as analog, COUT pin configured as Comparator output?
    ADCON0 = %00000000	'ADC disabled?
    ANSEL = %00001111	'All Analogue inputs enabled?
    TRISIO = %111011	'GPIO2 output (Comparator Out)
    
    ' PIN# NAME     USE & CONNECTION
    '  1   Vdd      +5VDC power supply
    '  2   GPIO.5   Debug Port (PicKit2 needs switching from here to pin Pin 7 to allow programming)
    '  3   GPIO.4   
    '  4   GPIO.3   
    '  5   GPIO.2   Comparator Out (GP2)
    '  6   GPIO.1   CIN-  Signal In  (GP1)
    '  7   GPIO.0   CIN+  trigger level (GP0)
    '  8   Vss      Power supply ground
    '-------------------------------------------------------------------------



    EDIT Ok, spotted it..

    Code:
    ANSEL = %00001011		'All Analogue inputs except AN2 enabled
    & here we have an output...



    Now...what to do with the output?!!!
    Last edited by HankMcSpank; - 8th March 2010 at 00:05.

  8. #8
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by HankMcSpank View Post
    Now...what to do with the output?!!!
    On the rising edge of your square wave, start a timer running. When the next rising edge comes along, grab the timer count so you can calculate the period of the square wave, and reset the timer for the next go around.

    If your waveform is symmetrical you could start the timer on the rising edge and stop on the falling edge and get your frequency in 1/2 cycle.
    Maybe...


    steve

Similar Threads

  1. Measuring change of frequency with PIC
    By Aussie in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 19th July 2007, 01:47
  2. PIC Audio
    By toofastdave in forum mel PIC BASIC Pro
    Replies: 28
    Last Post: - 27th June 2007, 13:49
  3. Pic driven digital audio delay
    By skimask in forum Off Topic
    Replies: 12
    Last Post: - 19th April 2007, 20:42
  4. Audio Encoding and playback in a PIC
    By Rob in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 24th March 2005, 08:56
  5. Frequency Counter using PIC and PicBasic
    By PICtron in forum mel PIC BASIC Pro
    Replies: 31
    Last Post: - 28th January 2005, 06:20

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts