PDA

View Full Version : help optimising code



Darrenmac
- 21st March 2007, 03:26
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

mackrackit
- 21st March 2007, 11:21
Just a thought, have not tested.



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

sougata
- 21st March 2007, 11:29
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.

mackrackit
- 21st March 2007, 11:40
Hi,
BTW you cannot use a Return with a Goto.

OOPPS' GOSUB

Like I said "not tested, just a thought":)

skimask
- 21st March 2007, 15:55
Have you tried the 'Select Case' statement yet? don't know if it'll save space...ya never know...

Darrenmac
- 21st March 2007, 20:25
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

skimask
- 21st March 2007, 23:49
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...

Darrenmac
- 22nd March 2007, 03:39
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?

skimask
- 22nd March 2007, 03:56
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.

Darrenmac
- 22nd March 2007, 07:40
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

Darrenmac
- 26th March 2007, 10:27
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