Shop OBEX P1 Docs P2 Docs Learn Events
Frequency Detection - Basic Stamp II — Parallax Forums

Frequency Detection - Basic Stamp II

PRPROG01PRPROG01 Posts: 28
edited 2013-01-08 13:21 in BASIC Stamp
I built a simple Op amp circuit to amp a signal (guitar) pass it thru a Schmitt trigger and input to the Basic Stamp. I have 2 programs, both used the PULSIN instruction. None give an accurate frequency measurement. Is the Basic Stamp II able to measure frequencies?

One of the programs is Jon Williams SHOWFREQ.BS2.

Thanks,
PRPROG

Comments

  • playinmyblues_nsccplayinmyblues_nscc Posts: 38
    edited 2012-12-28 07:55
    Where did you get the SHOWFREQ.BS2 program? It is hard to analyze why programs do not work if you do not provide schematics and the code.

    Here are some suggestions to help you on your way:
    1) Use an oscilloscope if you have one. USB scopes are convenient and the lower end ones do a decent job. For projects with signals in the audible frequency range, low end scopes are more than enough. Still, do your research. You can also download free software to use your soundcard as the hardware interface for an oscilloscope. I have never built one and YOU HAVE TO BE VERY CAUTIOUS HOW YOU MAKE IT AND HOW MUCH VOLTAGE IS SUPPLIED BY THE SIGNALS YOU USE. A soundcard is quite limited in how much applied votlage it will take before it is damaged. So your research before building one. I recommend the USB or standalone oscilloscopes.
    2) Get small sections of your program working before you attempt to get the whole thing working at once. Troubleshooting is much harder with 100 lines of code compared to 10 or 15.
    3) Use DEBUG statements to help you determine what kinds of numbers you are getting from different parts of the program.
    4) You can also write bits of code that will do things such as light up LEDs dependent of different conditions. Later, you can comment out the code or delete it when it is not needed.
  • PRPROG01PRPROG01 Posts: 28
    edited 2012-12-28 09:54
    Attach is the source for the showfreq code.
    It read pin 0
  • playinmyblues_nsccplayinmyblues_nscc Posts: 38
    edited 2012-12-28 16:43
    The PULSIN instruction is found on page 343 of the "Basic Stamp Syntax and Reference Manual." Table 5.83 shows that the instruction will allow measurement of pulses with a maximum width of 131.07 ms for the BS2. BS2e, BS2sx BS2p, BS2pe, and BS2px may or may have different values that can be measured. I checked the general range of frequencies that can be expected from a guitar or at least what I think is the correct range for a standard tuned guitar, E A D G B e.

    These frequencies are looked up from a project I am working on called the Luna Mod Looper Basic Stamp 2 Version which can be found in the Projects section and on Instructables. The point is that the BS2 uses integer math. Any calculations have the numbers to the right of the decimal point truncated. The numbers I used were those I used in the program. If you want fairly accurate frequencies, check here: http://www.phy.mtu.edu/~suits/notefreqs.html

    Here is a relevant equation: f = 1 / T, where
    f = frequency and T = period (in seconds or milliseconds, etc.)

    Low E, E2, f = 82 Hz, T = 12.2 ms
    C5, f = 523 Hz, T = 1.91 ms

    These frequencies should fall under the maximum pulse width of 131.07 ms (and greater than minimum of 2 us?). Given that, how far off are your frequencies? Putting the equations from the program into a spreadsheet would be a good idea. Then enter in a range of values for the equations. In the next column use a round down function to truncate the values. Now you have what the true values from the equation and the truncated values. Compare the spreadsheet values to the ones obtained from BS2. Make sure to take note what the expected frequencies are for the notes you are playing.

    The other thing that comes to mind about inaccuracies with such a method where your frequency source is a guitar is that the peaks might be throwing off your input from the Schmitt trigger. What are the threshold values, minimum and maximum for changing from one state to the next? The last time I measured my electric guitar signal it had a lot or irregular waveforms and if I recall correctly, that was even for single notes being played. You could tell where the general maximums and minimums were but it was far from a regular sine wave. An oscilloscope would really be an asset to you.
  • PRPROG01PRPROG01 Posts: 28
    edited 2012-12-28 18:56
    playinmyblues_nscc thanks a lot for the detail information.

    I re-start the basic idea but instead of PULSIN I change to COUNT . Since I am using a Schmitt trigger., COUNT seems to be more accurate.

    The key line in the code is:
    COUNT 0,100,F
    F = F * 10

    I get 440 when I play note A on my keyboard.

    I notice that the COUNT give me a 440 count ..but then the numbers start to decrease gradually. I think that is a normal behaviour, so the question is how do I get a sample of the Maximum value, which instruction/Command give me the Maximum value. (at first I though MAX was the command but that will limit to a certain value).

    Also how do I translate this formula to PBASIC:

    MidiNote = (12 * Log2 (F/440)) + 69

    I intend to calculate the corresponding MIDI note by getting the frequency (F). What is the equivalent of Log2 in PBASIC?

    Thanks,
    PRPROG
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-12-29 12:11
    As counter-intuitive as it may seem, it the the MIN operator that you should use to capture the maximum value.
    Fmax = 0
       FOR idx = 1 to 10   '  the sampling period
           COUNT 0,100,F
           Fmax = Fmax MIN F  ' hold the maximum value
       NEXT
               F = Fmax * 10
    

    I have a series of articles on how to calculate log2 in PBASIC, here.
  • playinmyblues_nsccplayinmyblues_nscc Posts: 38
    edited 2012-12-29 12:58
    @ Tracy Allen: Thanks for posting that info for the original person. I have a project which requires similar curves and have yet to examine the possibilities.
  • playinmyblues_nsccplayinmyblues_nscc Posts: 38
    edited 2012-12-29 13:02
    What references are you using for your MIDI information? I am very interested in producing a MIDI capable device at some point. From what I see on most magazine and web articles on the subject, people are using software programs to handle the details once the mcu has processed the things like your pots or guitar signals that allow for analog input. Currently, I am reading the MIDI spec and a book called "MIDI Power."
  • playinmyblues_nsccplayinmyblues_nscc Posts: 38
    edited 2012-12-29 13:26
    For my Luna Mod project I was searching the Web for sound related ideas for the BS2 and came up with a Nuts and Volts column (#67) available on the Parallax website. If you are looking for something else besides the "What's a Microcontroller" PDF or the "StampWorks" then the Nuts and Volts columns are very good. You get the explanation of the program, schematics and the program itself. At some point, it is a good idea to explore the whole Parallax website just to see what else is there.

    Here is a link to the Resources where you can find the Nuts and Volts section: http://www.parallax.com/Resources/ResourcesHome/tabid/473/Default.aspx
  • PRPROG01PRPROG01 Posts: 28
    edited 2013-01-08 09:57
    As counter-intuitive as it may seem, it the the MIN operator that you should use to capture the maximum value.
    Fmax = 0
       FOR idx = 1 to 10   '  the sampling period
           COUNT 0,100,F
           Fmax = Fmax MIN F  ' hold the maximum value
       NEXT
               F = Fmax * 10
    

    I have a series of articles on how to calculate log2 in PBASIC, here.

    Thanks a lot. But I have to admit, that I read your article on to calculate log2 in PBASIC and have no idea ho to implement it to get this ► MidiNote = (12 * Log2 (F/440)) + 69

    Can you help me to create such formula in PBASIC? I appreciate your help.

    Thanks,PRPROG
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2013-01-08 13:21
    Hi, I'll give it a whirl. You'll have to teach me about MIDI codes.

    The MIDI code is an integer, right, with A440 = 69, and 12 integer values per octave? Then to find the code for A880:
    MIDI = 12 * log2(880/440) + 69
    = 12 * 1 + 69
    = 81
    And by the same means A55 is three octaves down:
    MIDI = 12 * log2(55/440) + 69
    = 12 * log2(2-3) + 69
    = 12 * -3 + 69
    = 33
    Frequency=55 Hz is pretty low, unless you have a pipe organ!

    To find the log, first you will find the octave of your F, by steps of 12. Once having the octave, the last part to get the code within the octave will be a bit of calculation and rounding off. OK so far?
Sign In or Register to comment.