• Status: Solved
• Priority: Medium
• Security: Public
• Views: 192

# Urgent but Simple....I think. Aid in manipulating these two code segments.

hi....this first section of code draws a 70mm length of a 8 - 12 Ghz sine wave. The frequency is changed using a scroll bar. I'm not sure how it works...I'm new to programming, but it does what it needs to.

Private Sub DrawWave()

Me.PicWave.Cls
' number of sine waves per box = 70mm/wavelength(mm), ie 2.33 for 10ghz

Dim NumWaves As Single
Dim Wavelength As Double
Dim GHZFreq As Double
Dim x As Single
Dim y As Double
Dim Freq As Single

GHZFreq = scrGunDiode.Value / GHZFactor
Wavelength = 300 / GHZFreq
NumWaves = WIDTHmm / Wavelength

For x = 0 To WIDTHmm Step 0.1

y = Sin(x * NumWaves * (360 / WIDTHmm) * PI / 180) * AMPLITUDE
Me.PicWave.PSet (x, y), vbRed

Next x

Me.lblGunDiodeHz = CStr(Val(scrGunDiode.Value / GHZFactor)) & " GHz"

End Sub

The next section of code is designed to convert the 8 - 12 GHz signal, convert it into the corresponding wavelength. It then changes the original wavelength to the wavelength it changes to when it passes througha waveguide, called ComputeWaveGuideWavelength.
Public Function ComputeWavelength(dblFrequency As Double) As Double

ComputeWavelength = 3 * 10 ^ 8 / dblFrequency

End Function

Public Function ComputeWaveGuideWavelength(dblFrequency As Double) As Double

Dim dblOrigWaveLength As Double

dblOrigWaveLength = ComputeWavelength(dblFrequency)

ComputeWaveGuideWavelength = dblOrigWaveLength / Sqr(1 - ((dblOrigWaveLength / 0.04572) ^ 2))

End Function

these pieces of code were given to me by two different experts and hence they use different variable names. What I'm trying to do is instead of drawing the original wavelength is to draw the wavelength generated using ComputeWaveGuideWavelength. The second snippet of code is in a code module, not in the form module. How and what would I change to achieve this???

Thanks, Nick
0
nickclaridge
• 9
• 5
• 4
1 Solution

Author Commented:
The scroll bar should still be able to change the original wavelength. This should then cause ComputeWaveGuideWavelength to calculate the new waveguide wavelength which it passes to the DrawWave function. Not being too familiar with programming I'm not sure how to do this.
0

Commented:
If I understand what you are trying to do, try inserting this line:

Wavelength = ComputeWaveGuideWavelength(Wavelength)

after the original line that begins "Wavelength = "

It also looks like the Sqr function call in ComputeWaveGuideWavelength will fail if the calculation (1 - ((dblOrigWaveLength / 0.04572) ^ 2)) yields a negative number. It is possible you will never have a problem that, but if you receive a run-time error 5 : "Invalid procedure call or argument", that may well be the problem. I don't know if taking the absolute value (Abs()) of the result will yield a valid result.

0

Author Commented:
I did receive a run-time error 5 and it pointed me to the line with the sqr function. How do I take the absolute value of the result. Could you show me please.
Thanks
Nick
0

Author Commented:
No that line should not produce a negative value but it still produces a run-time 5 error. Do you know why? The value produced by 1 - ((dblOrigWaveLength / 0.04572) ^ 2)) is less than 1.
0

Commented:
Abs(1 - ((dblOrigWaveLength / 0.04572) ^ 2)))

Less than 1 shouldn't matter.
0

Author Commented:
Hi again. If the rin-time error 5 is produced its not because its square rooting a negative number.
0

Commented:
What values are you using for AMPLITUDE and GHZFactor? Is there a specific range you are restricting the scroll bar scrGunDiode to?
0

Commented:
What values are you using for AMPLITUDE and GHZFactor? Is there a specific range you are restricting the scroll bar scrGunDiode to?
0

Commented:
The line should end up looking like this:

ComputeWaveGuideWavelength = dblOrigWaveLength / Sqr(Abs(1 - ((dblOrigWaveLength / 0.04572) ^ 2)))
0

Author Commented:
The scroll bar is restricted to values between 8 * 10^9 and 12 * 10^9. It should never produce a value less than 1.
0

Commented:
if you are absolutely sure that the sqrt argument is SUPPOSED to be between 0 and 1, then make sure that you're using the right constant UNITS in your expression.

Remember that your units for wavelength here is mm, and make sure that constant isn't for meters, if it is, convert it.

The fact that I can get a positive number for the expression if I multiply the originalwavelength by 1000 suggests that the millimeter/meter effect is in play...

0

Commented:
and technically, your scroll bar is limited between 8*GHZfactor and 12*GHZFactor (ie 800 and 1200) and your values from it converted into GHz frequencies in the calculations...

In case thats coming into play...
0

Commented:
I meant divided by 1000
0

Author Commented:
Should the scroll bar used to change the original wavelength use the same variable name in both parts of code? i.e.scrGunDiode.Value? This is the initial value of the frequency. Should this be tied into the second snippet of code? I hope this makes sense. Is it this that is causing the run-time 5 error?
0

Author Commented:
If I put in the line with the Abs() function it doesn't draw a wave at all but a series of dots on the graph.
0

Commented:
Nick

I had planned to make the code before more generic, allowing for plotting multiple waves.  And because you were refering to making computations on those waves, I've included a phase component too.

I posted a new code in your most recent PAQ for you, taking this code a step further.  I put in the 1000 factor correction hoping that that was correct, as it does do something...

0

Author Commented:
GPrentice00,
Thanks for that. So it will allow me to display the waveguide wavelength in the picture box....is that right. i'll stick it in and see what happens.

Nick
0

Author Commented:
Thanks again my friend. You really have been helpful. Lets hope I can get to grips with all this code you have given me.

Nick
0

## Featured Post

• 9
• 5
• 4
Tackle projects and never again get stuck behind a technical roadblock.