help optimising code


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2004
    Posts
    74

    Default help optimising code

    I have written a routine that measures frequency on an input and then looks at an upper and lower limit for that tone to come up with a standard used frequency. I have 33 of these frequencies and at the moment I am using if then statements but there are a lot of them. I have broken the tone set in half , then in half again etc so that I can find the tone in a minimal time. This uses a large amount of code and thought there must be an eiser way to do this. Any help would be appreciated. Below is a code snippet

    tonehigher26:
    IF w1 < 4508 Then tonehigher30 ' tone is between > 225.7
    IF w1 > 4507 Then tone26to29 ' tone is between 192,8 and 218.1
    GoTo loop
    tone18to25:
    IF w1 < 6060 Then tone22to25 'tone Is between 167.9 and 186.2
    IF w1 > 6059 Then tone18to21 'tone is between 146.2 and 162.2
    GoTo loop
    tone26to29:
    IF w1 < 4830 Then tone28to29
    IF w1 > 4829 Then tone26to27
    GoTo loop
    tone22to25:
    IF w1 < 5657 Then tone24to25
    IF w1 > 5656 Then tone22to23
    GoTo loop
    tone18to21:
    IF w1 < 6494 Then tone20to21
    IF w1 > 6493 Then tone18to19
    GoTo loop
    tone28to29:
    IF w1 < 4666 Then tone=29
    IF w1 > 4665 Then tone=28

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Just a thought, have not tested.

    Code:
    cnt      var word
    testF    var word
    w1       var word
    
    T_C:
        for cnt = 1 to 33  
        testF = testF+? '  frequency step
        if w1 = testF then ST_P
        pause 100
        next
    RETURN
    
    ST_P:
    '    DO SOMETHING
    '   print w1
        goto T_C
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default Just a thought

    Hi,

    I don't know if your frequencies are evenly spaced. If they are then you might divide it to a manageable factor and use the lookup function. BTW you cannot use a Return with a Goto.
    Regards

    Sougata

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sougata View Post
    Hi,
    BTW you cannot use a Return with a Goto.
    OOPPS' GOSUB

    Like I said "not tested, just a thought"
    Dave
    Always wear safety glasses while programming.

  5. #5
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Have you tried the 'Select Case' statement yet? don't know if it'll save space...ya never know...

  6. #6
    Join Date
    Mar 2004
    Posts
    74


    Did you find this post helpful? Yes | No

    Default

    No the tones are not eveny spaced. Here are the required tones and associated value of the w1 variable.

    CTCSS W1

    1 67 14925
    2 71.9 13908
    3 77 12987
    4 82.5 12121
    5 88.5 11299
    6 94.8 10549
    7 100 10000
    8 103.5 9662
    9 107.2 9328
    10 110.9 9017
    11 114.8 8711
    12 118.8 8418
    13 123 8130
    14 127.3 7855
    15 131.8 7587
    16 136.5 7326
    17 141.3 7077
    18 146.2 6840
    19 151.4 6605
    20 156.7 6382
    21 162.2 6165
    22 167.9 5956
    23 173.8 5754
    24 179.9 5559
    25 186.2 5371
    26 192.8 5187
    27 203.5 4914
    28 210.7 4746
    29 218.1 4585
    30 225.7 4431
    31 233.6 4281
    32 241.8 4136
    33 250.3 3995

  7. #7
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    lookdown2 w0,>[14925,12987,12121,11299,10549,10000,9662,9328,9017 ,8711,8418,8130,7855,7587,7326,7077,6840,6605,6382 ,6165,5956,5754,5559,5371,5187,4914,4746,4585,4431 ,4281,4136,3995],b0

    branchl b0 , [ t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, t20, t21, t22, t23, t24, t25, t26, t27, t28, t29, t30, t31, t32, t33 ]

    t1: do whatever with tone 1
    t2: do whatever with tone 2
    and so on and so on...

    I'll let you know right now, this bit of code will still eat up a bunch of code space...

  8. #8
    Join Date
    Mar 2004
    Posts
    74


    Did you find this post helpful? Yes | No

    Default

    this would be good if the tone was extremely presice. I have allowed for variances up to half way between tones. can this also be done with the above example?

  9. #9
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrenmac View Post
    this would be good if the tone was extremely presice. I have allowed for variances up to half way between tones. can this also be done with the above example?
    Probably not, 'cause that sounds like it involves 'fuzzy logic', the ability to change parameters in the middle of what you're doing, or something like that.
    Maybe an extra if/then in each chunk of tone code checking for the 'exactness' of the tone?

    You do understand the concept of the example above right?
    If the value is above 14925 it'll jump to t1...and you're done...
    if the value is 14925 or below, it'll skip the first one and try the second one...
    If the value is above 12987 (but below or equal to 14925 'cause it checked it during the first check), it'll jump to t2...and you're done....
    ...and so on and so on and so on...
    And you don't have to use the BRANCHL statement. Once you've got your index variable into B0, you can use the SELECT CASE statement to handle the individual blocks of code instead of a bunch of goto's.

  10. #10
    Join Date
    Mar 2004
    Posts
    74


    Did you find this post helpful? Yes | No

    Default

    thanks Skimask,
    I did'nt see the ">" symbol.
    Yes it does make sense and should work. I'll give it a go and let you know

  11. #11
    Join Date
    Mar 2004
    Posts
    74


    Did you find this post helpful? Yes | No

    Default

    Thanks Skimask
    I modified my code to use lookdown2 and works great. Not using branch but sending the lookdown2 index out serialy. My code droped from approx 1500 words with only 1/2 of the tones covered to less than 500 words for the entire tone set.

    Thanks again

Similar Threads

  1. Reading in Manchester code
    By brid0030 in forum Code Examples
    Replies: 0
    Last Post: - 10th March 2009, 21:55
  2. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  5. Re-Writing IF-THEN-AND-ENDIF code?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th August 2006, 17:23

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