Coil Inductance and Q Calculator

User projects written in or related to FreeBASIC.
Post Reply
k6sti
Posts: 19
Joined: Feb 14, 2019 14:31

Coil Inductance and Q Calculator

Post by k6sti »

I used FreeBASIC to update a program I had written for DOS in the 1990s. The program calculates inductance and Q for a solenoid made of solid or Litz wire. It accounts for wire and form material as well as leads. I used several sources to obtain the most accurate methods, some rather esoteric. The result was many pages of dense equations. I validated the program by measuring a couple dozen test coils with an HP 4342A Q Meter. Average inductance accuracy was 2% and average Q accuracy 7%.

I used QB mode to ease program conversion. I still had to rewrite some incompatible code. I created a console program since input and output are numeric, but I use all three mouse buttons and the wheel. I added assembler code to greatly speed ^ and EXP(). An optimizer adjusts coil dimensions to maximize Q. Speed matters since the optimizer may do thousands of coil evaluations. I added hyperbolic sine/cosine assembler code to take advantage of the fsincos instruction. It yields both sine and cosine in about half the time FB requires for separate SIN() and COS(). I also added my own random number generator. The one in FB worked fine, but I reduced the EXE file size by 2.5k with seven lines of assembler code.

http://ham-radio.com/k6sti/coil.zip

Brian
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Coil Inductance and Q Calculator

Post by counting_pine »

Hi k6sti, thanks for sharing your project. Are you willing to provide the code for it? This prevents people having to run an untrusted executable just to see what it does.
What kinds of problems did you run into when porting the code? Was written in QBasic previously?
k6sti
Posts: 19
Joined: Feb 14, 2019 14:31

Re: Coil Inductance and Q Calculator

Post by k6sti »

I originally wrote the program for Microsoft BASIC PDS 7.1. For FB I had to rename the many variables that used the same name as an array. There may have been other incompatibilities, but they were easy to fix. The main problem was the inadequacy of the FB documentation. It took me months to find all the answers I needed. Some were buried in multipage examples, which I normally don't read. Searching this forum yielded other answers. Still others I had to discover by examining assembler code. I was able to make some things work only by making lucky guesses.

Brian
Last edited by k6sti on Oct 15, 2019 23:07, edited 3 times in total.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Coil Inductance and Q Calculator

Post by fxm »

k6sti wrote:The main problem was the inadequacy of the FB documentation.....
It starts strong for your second post on the forum, me who on the contrary tries to share as much as possible my some knowledge on FB, including through its documentation.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Coil Inductance and Q Calculator

Post by counting_pine »

Probably the single most helpful page for this kind of situation is LangQB, although it's more geared towards QB than PDS.
Do you have any suggestions for things to add there from your experiences?
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Coil Inductance and Q Calculator

Post by caseih »

k6sti wrote:The main problem was the inadequacy of the FB documentation. It took me months to find all the answers I needed. Some were buried in multipage examples, which I normally don't read. Searching this forum yielded other answers. Still others I had to discover by examining assembler code. I was able to make some things work only by making lucky guesses.
You probably could have saved a few months of frustrations by coming to the forum with questions much earlier. The wiki documentation for FreeBASIC I find to be incredibly good. Better than many other languages. At least as good as the old PowerBASIC printed manuals I used years ago, or the fantastic built-in help in QB 4.5. If there is something lacking, you are welcome to contribute improvements and suggestions. That's the beauty of an open source, volunteer project.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Coil Inductance and Q Calculator

Post by badidea »

I assume you did find the online documentation: https://www.freebasic.net/wiki/wikka.php?wakka=DocToc
and the off-line versions: https://sourceforge.net/projects/fbc/fi ... mentation/ ?
k6sti
Posts: 19
Joined: Feb 14, 2019 14:31

Re: Coil Inductance and Q Calculator

Post by k6sti »

I used this documentation:

https://www.freebasic.net/wiki/wikka.php?wakka=DocToc

I did not write down what I had trouble with. I'll try to make a list of things as they occur to me.

Here's another INT() replacement if you have SSE 4.1. It runs almost 60 times as fast as FB INT() and returns a 32-bit integer.

Code: Select all

;	DECLARE FUNCTION intt& (BYVAL a)
;	Faster replacement for INT. Returns 32-bit integer. Requires SSE 4.1.

_INTT@4:
	roundss   xmm0,[esp+4],1
	cvtss2si  eax,xmm0
	retn      4
Brian
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Coil Inductance and Q Calculator

Post by dodicat »

Better to use the .chm file IMO.
Much easier.
Anyway, I ran a test on your asm.
parameter in single, 32 bit integer out.
must use the 32 bit compiler.

Code: Select all

 

Function intt Naked Cdecl(Byval a As Single) As Integer
    Asm
        roundss   xmm0,[esp+4],1
        cvtss2si  eax,xmm0
        ret 
    End Asm
End Function



Dim As Single x
Dim As Double t,t1,t2
For x =-500 To 590 Step .3077
    If Int(x)<>intt(x) Then Print Int(x),intt(x) ,x
Next
Print "comparison check done"

Dim As Double i
Dim As Single limit=1000002.7
Dim As Single n
For z As Long=1 To 5
    i=z   
    t=Timer
    For n =1 To limit Step .1
        i+=Int(n)
    Next
    t1= Timer-t
    Print "Int", t1,i
    Sleep 50
    i=z
    t=Timer
    For n =1 To limit Step .1
        i+=intt(n)
    Next
    t2 =Timer-t
    Print "Intt",t2,i,"speed ratio ";t1/t2
    Print
Next z
Sleep



 
My results (default 32 bit compiler)

Code: Select all

comparison check done
Int            0.5094819400166344          4359575087424
Intt           0.05134917334613931         4359575087424              speed ratio  9.921911236667256

Int            0.1881160506406729          4359575087425
Intt           0.05960829402160783         4359575087425              speed ratio  3.155870399050196

Int            0.1816431667090512          4359575087426
Intt           0.06202074019599735         4359575087426              speed ratio  2.928748772346545

Int            0.1810022430608953          4359575087427
Intt           0.06382511305166361         4359575087427              speed ratio  2.835909478364449

Int            0.1801573736138877          4359575087428
Intt           0.04968509877748062         4359575087428              speed ratio  3.625984008218227

  
k6sti
Posts: 19
Joined: Feb 14, 2019 14:31

Re: Coil Inductance and Q Calculator

Post by k6sti »

What is a .chm file?

I used this test program:

Code: Select all

	DEFINT I-N
	DECLARE FUNCTION intt& (BYVAL a)
	a = 123456.7
	t = TIMER
	FOR i& = 1 TO 10000000
	NEXT i&
	t1 = TIMER - t
	__SLEEP 100
	t = TIMER
	FOR i& = 1 TO 10000000
	  j& = INT(a)
	NEXT i&
	t2 = TIMER - t
	__SLEEP 100
	t = TIMER
	FOR i& = 1 TO 10000000
	  j& = intt(a)
	next i&
	t3 = TIMER - t
	PRINT (t2 - t1) / (t3 - t1)
The result varies, but is mostly 50-60. Why do you suppose our timings are so different? My data is constant - is that it? I'm using an Intel Core 2 with WinXP.

Brian

Edit - I put a = i& in each loop and get about 20. With a= i& + .7 I get about 12. Using a as the FOR variable, from -1000000 to 1000000 with .0417 increment, it's back to a large ratio. Time for INT() loop is about 0.85 seconds.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Coil Inductance and Q Calculator

Post by dodicat »

The .chm help file, from the home page / downloalds/documenataion
https://sourceforge.net/projects/fbc/fi ... mentation/
For win 10, right click the .chm, properties then unblock.
I use fbide, you can set a path to it in view- settings -freebasic.
If you have this set up then can put your cursor on a keyword and press the f1 key.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Coil Inductance and Q Calculator

Post by badidea »

The .chm file contains the same information as the wiki, but more convenient searching + off-line.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Coil Inductance and Q Calculator

Post by dodicat »

Timer measurements should always be double, even in lang "qb"
Here are my #lang "qb" results:

Code: Select all

#lang "qb"

   
  
    FUNCTION intt naked cdecl(byval a as single) as integer<32>
__asm
   roundss   xmm0,[esp+4],1
   cvtss2si  eax,xmm0
   ret 
   end __asm
end function

print __sizeof(intt(0!))

print

DEFINT I-N
DEFDBL t

print "Test loop"
for s as single=-500 to 500 step .01
    if int(s) <> intt(s) then print s
next s
print "test loop finished"
print
dim as integer c
rem get an average for an empty loop
for c =1 to 50
    t=timer
    FOR i& = 1 TO 10000000
    NEXT i&
    t1+=timer-t
next c
t1=t1/50
print "Empty loop time ~ ";t1
print


for c=1 to 10
   
   a = 123456.7
  
   
   t = TIMER
   FOR i& = 1 TO 10000000
     j& = INT(a)
   NEXT i&
   t2 = TIMER - t
   print t2,j&,"INT"
   __SLEEP 100
   
   t = TIMER
   FOR i& = 1 TO 10000000
     j& = intt(a)
   next i&
   t3 = TIMER - t
   print t3,j&,"intt"
   
   PRINT (t2 - t1) / (t3 - t1),"   speed factor"
   print
next c
#print __typeof(t3)

   sleep 

Code: Select all

  4

Test loop
test loop finished

Empty loop time ~  0.02701678275513927

 0.1926329711271961          123456       INT
 0.02991604344882859         123456       intt
 57.12359317413086             speed factor

 0.1922377405749245          123456       INT
 0.03164855620524776         123456       intt
 35.67120879280386             speed factor

 0.193845353650147           123456       INT
 0.03193907632977755         123456       intt
 33.89244634951857             speed factor

 0.1925919082120799          123456       INT
 0.03265357101386712         123456       intt
 29.37401900817699             speed factor

 0.1937916296604598          123456       INT
 0.03260395333571609         123456       intt
 29.84960714911678             speed factor

 0.1938936025692826          123456       INT
 0.03262140506922862         123456       intt
 29.77485554996903             speed factor

 0.1924392910473429          123456       INT
 0.03273056398579399         123456       intt
 28.95149492330993             speed factor

 0.1934070070472274          123456       INT
 0.03162939352497318         123456       intt
 36.07289506850792             speed factor

 0.194090020172986           123456       INT
 0.03103877194929083         123456       intt
 41.53995183795887             speed factor

 0.1924779586280181          123456       INT
 0.03098162605924415         123456       intt
 41.7320845193488              speed factor

  
Post Reply