Shop OBEX P1 Docs P2 Docs Learn Events
Microphone BPM Counter — Parallax Forums

Microphone BPM Counter

domidomi Posts: 3
edited 2009-06-30 15:23 in General Discussion
Hi,
i'm experimenting with the basic stamp and tried to implement a BPM counter·via CPU.count function for a Microphone attached on P15.

To get a counter for 1 second i tried to count within a loop 4·times to 250ms· (value 28802) because of a Maximum Integer value of 32,767.

Now i got 2 problems:
first, it's not counting exactly 1 second ( counter a little bit to slow, someone knows how?)
second, it sometimes counts 1 beat as 2 beats(beat time to long?)


Thanks for help [noparse]:)[/noparse]

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-06-30 10:29
    Description of CPU.count()

    · /**
    ·· * Count the number of rising or falling edge transitions that occur during
    ·· * a set period of time.
    ·· * If the pin is an output then it will be changed to be an input and will remain as
    ·· * an input when the counting is complete.
    ·· * <p>
    ·· * To ensure reliable detection the input signal must maintain a value for at least
    ·· * 8.68us. This means that a square wave must have a period of at least 17.36us and
    ·· * a frequency less than 57.6kHz.
    ·· *
    ·· * @param timeout the amount of time to count transitions over. Measured in
    ·· *··············· 8.68us units. The value of 32,767 allows a maximum measurement
    ·· *··············· time of 284.4ms.
    ·· * @param portPin···· the I/O pin to measure transitions on.
    ·· * @param edge··· the type of edge to count. Pass true to count rising edges,
    ·· *··············· or false to count falling edges.
    ·· * @return the number of rising edges or falling edges that occured on the pin
    ·· *········ during the timeout period.
    ·· */

    The maximum time to count rising or falling edges is 0.284 seconds.
    So you used a loop of 4 times 250 msec.
    Counter too slow (so the real time is longer than 4*250msec)
    due to processing time of your loop.
    But the counts are measured in 1 second, only there are gaps between
    the measurements, which introduces the extra time.

    I think the error of 1 in beats is also due to those gaps in measurement.
    Can't you just measure 1 period of 250msec and then multiply the counts by 4
    to get counts per second?

    regards peter
  • domidomi Posts: 3
    edited 2009-06-30 10:32
    would be to inexact :/
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-06-30 12:31
    Instead of a for-loop, use 4 consecutive CPU.count() calls.
    That probably gives smaller gaps and better timing.
    And use static int to save results.
    After the calls you can add the static ints.

    regards peter
  • domidomi Posts: 3
    edited 2009-06-30 14:36
    thanks, you also got an idea because of the 1 beat counted as 2 or 3?

    regards dominik
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-06-30 15:23
    You can try using alternatively count rising and falling edges.
    Chances are that if a rising count is 1 up, a falling count is 1 down,
    but added the error is cancelled out.

    static int res1 = CPU.count(...,true); //count rising edges for 0.25sec
    static int res2 = CPU.count(...,false); //count falling edges for 0.25sec
    static int res3 = CPU.count(...,true); //count rising edges for 0.25sec
    static int res4 = CPU.count(...,false); //count falling edges for 0.25sec
    int result = res1+res2+res3+res4;


    regards peter
Sign In or Register to comment.