Squares

General FreeBASIC programming questions.
Locked
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

This formula barely compresses , like .9% to 2% after 40 loops.

It gives you a little insight ... the "1100" combination , hardly ever happens, in real data..

How would you write an analyzer function , to see which combinations of bits are the mostly likely to occur???
given:
00
01
10
11
What combinations occur most and which ones the least??

Code: Select all

Declare Function   compress_loop( chrs as string ) as string
Declare Function decompress_loop( chrs as string ) as string
'=============================================================================
'coderjeff's code.
'======================================================================================
const MAX_WORDS = 4
dim shared words( 0 to MAX_WORDS-1 ) as string

sub init_words()
   words(0) = "00"
   words(1) = "11"
   words(2) = "0001"
   words(3) = "1100"
end sub

function string_to_bits( byref s as const string ) as string
   dim ret as string = ""

   for i as integer = 0 to len(s)-1
      ret &= bin( s[i], 8 )
   next

   function = ret
end function

function bits_to_string( byref s as const string ) as string
   dim ret as string = ""
   '' assume input length is multiple of 8
   assert( (len(s) mod 8) = 0 )

   for i as integer = 1 to len(s) step 8
      ret &= chr( cubyte( "&b" & mid( s, i, 8 ) ) )
   next

   function = ret
end function

function encode( byref s as const string ) as string
   dim bits as string = ""
   '' loop through every byte in 's'
   for i as ulongint = 0 to len(s) - 1
      dim b as ubyte = s[i]
      '' look at each pair of bits, most significant to least significant
      for j as integer = 0 to 7 step 2
         '' look up word from the dictionary
         bits &= words( cint( "&b" & mid( bin( b, 8 ), j+1, 2 ) ) )
      next
   next
   
   '' pad bits to multiple of 8
   bits &= string( (8 - (len(bits) mod 8) mod 8), "0" )

   function = bits_to_string( bits )
end function

function decode( byref s as const string, byval n as integer ) as string
   dim bits as string = string_to_bits( s )

   dim i as integer = 1, count as integer = 0
   dim ret as string = ""
   '' loop through entire bit string   
   while i <= len(bits) and (count < n*8 )
      '' check for words
      for j as integer = 0 to MAX_WORDS-1
         if( mid( bits, i, len(words(j)) ) = words(j) ) then
            ret &= bin( j, 2 )
            i += len(words(j))
            count += 2
            continue while
         end if            
      next
      print "bad input for decode"
      exit while
   wend
   
   function = bits_to_string( ret )
end function

init_words()

'=============================================================================
'=============================================================================



Namespace Zlibrary

#inclib "zlib"
Extern "C"
    Declare Function compressBound(Byval sourceLen As Ulong) As Ulong
    Declare Function uncompress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
    Declare Function compress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
End Extern

Function getpassedinfo(text As String,Byref passed_length As Integer) As String
    Dim As String var1,var2
    Dim As Integer pst
    #macro splice(stri,char,var1,var2)
    pst=Instr(stri,char)
    var1="":var2="" 
    If pst<>0 Then
        var1=Mid(stri,1,pst-1)
        var2=Mid(stri,pst+1)
    Else
        var1=stri
    End If
    #endmacro
    splice(text,"|",var1,var2)
    text=var2
    passed_length=Valint(var1)
    Return text
End Function


'=================   UNPACK ===============
Function unpack(file As String) As String
    Dim As Integer passed_length
    Dim As String text=getpassedinfo(file,passed_length)
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength =passed_length
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr  destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=uncompress(destination,@destinationlength, source, stringlength)
    If mistake<>0 Then Print "There was an error":Sleep:End
    Dim As String uncompressed
    uncompressed=String(destinationlength,0)
    For i As Integer = 0 To destinationlength- 1
        uncompressed[i]=(destination[i])
    Next
    Deallocate destination
    Return uncompressed
End Function

'===================  PACK ============
Function pack(file As String) As String
    Dim As String text=file
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength = compressBound(stringlength)
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=compress(destination, @destinationlength, source, stringlength)
    If mistake <>0 Then Print "There was an error"
    Dim As String compressed
    compressed=String(destinationlength,0)
    For n As Integer=0 To destinationlength-1
        compressed[n]=destination[n]
    Next n
    compressed=stringlength &"|"+compressed
    Deallocate destination
    Return compressed
End Function

End Namespace


'==================================================================
'==================================================================
'test zipper 
'==================================================================
'==================================================================
screen 19

Dim Shared As String s

Randomize

s=""
dim as string check=""
dim as string compare=""
dim as longint length = 0
dim as double compression = 0 
dim as longint loops = 0
do
    
    loops+=1
    
    'one time run , create initial string 
    if loops = 1 then
        For n As Long = 1 To 8000
            s+=chr(Int(Rnd*256))'+48)
        Next
        compare =  s
        length = len(s)
    else
        'modify compression to make further compression possible 
        
        s = compress_loop(s)
        
    end if
    check = s
    compression = (100 - ( 100 / ( length / len(check) ) ))
    
    Print "original string"
    Print Len(s)
    Print
   
    Dim As String compressed=Zlibrary.pack(s)
    s = compressed
    
    Print "packed string "
    Print Len(compressed)
    Print
    
    Dim As String uncompressed=Zlibrary.unpack(compressed)
   
    Print "Retrieve"
    Print Len(uncompressed)
    Print
    'Print "compression ratio  "; 100 - ( 100 / ( Len(s) / len(compressed) ) ) ; "%"
    Print "compression ratio  "; 100 - ( 100 / ( length / len(s) ) ) ; "%"
    Print Iif(uncompressed=check,"OK","ERROR")
    Print "-------------------------------"
    
    'sleep 1000
    
    'if loops > 2 and (100 - ( 100 / ( length / len(s) ) )) < compression then exit do
    
    print "press a key for next compression."
    print
    print "press esc to exit."
    sleep
    
    if inkey = chr(27) then exit do
    
loop until loops = 40

print "Press a key to decompress."  
sleep

s = str(loops) + "_" + s ' save as an output file...

'==================================================================
'decompress
'==================================================================
dim as longint dec = instr(1,s,"_")
dim as longint count = val(left(s,dec-1))
dim as string comp = mid(s,dec+1)
dim as string val1
dim as string outs
for a as longint = count to 2 step -1
    s = Zlibrary.unpack(comp)
    outs = decompress_loop(s)
    comp = outs
next

comp = Zlibrary.unpack(comp)

print
print "input = "; length , "output = " ; len(comp) , "compression ratio  "; 100 - ( 100 / ( length / len(s) ) ) ; "%"
print
if comp = compare then print "Decompression successful." else print "ERROR"
print
Print "!!~~Done~~!!"

Sleep
end
'===============================================================================
'===============================================================================
'begin functions
'===============================================================================
'===============================================================================
Function compress_loop( chrs as string ) as string
   
   
    dim as string final_out =  encode( chrs )

       
    return final_out
       
end function
'===============================================================================
'============================================================================
Function decompress_loop( chrs as string ) as string
        
    
    dim as string final_out = decode( chrs , len(chrs) )
    
    return final_out

end function

albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

sub init_words()
words(0) = "00"
words(1) = "10"
words(2) = "0100"
words(3) = "100"
end sub

compresses 76% after 40 loops

combinations:

00 00
00 10
00 0100
00 100

10 00
10 10
10 0100
10 100

0100 00
0100 10
0100 0100
0100 100

100 00
100 10
100 0100
100 100

I might be mistaken but i don't see any combos that equal other combos..
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

Now on to solving Tesla's "Death Ray"

He said , he could bring down a whole squadron of enemy aircraft , with some sort of ray...

That ray might have been sonic?
I don't see , light or lasers or plasma , bringing down aircraft..

Maybe a sonic "Earth Quake Machine" like i described earlier. call it a "EQM"

With speakers you could make aircraft wobble like rubber , from a distance ...At the right pulse rate.
Maybe the wobbling would bring the planes down???

Maybe you could use light or radar to pulse the aircraft to bring them down...
Photons or microwaves hitting the aircraft would cause waves in the planes.
if you pulsed them at the right rate you could cause the planes to start wobbling like rubber?

It doesn't take much force to amplify a waveform in an object. just pulsing in time with the waves... even a small amount..

But then you should be able to make a wine glass , wobble like rubber, by pulsing a light bulb in time with the waves traversing it..
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

words(0) = "00"
words(1) = "10"
words(2) = "0100"
words(3) = "100"

You step through by 2's if you run into a "01" you know it's a "0100"

If you run into a "10" its either a "10" or "10 00 " or "10 0100" or a "100?"

Should be pretty simple..
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

How would you convert the speed of sound into inches a second??

460 MPH / 3600 would give you miles a second.
Divide that by 5280 that would give you feet per second..
Divide that by 12 that should give you inches per second...

So for a wine glass , 4 inches in diameter.. The pulse rate would be 460 / 3600 / 5280 / 12 * 4 / 2
That should be the pulse rate of the speaker or light bulb against the wine glass.. To get it wobbling like rubber.

I worked it out in the calculator , the above formula came out to 0.000004033 pulses per second. That's got to be wrong!!!

I'm not German , so i use English measurements , instead of meters..
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Re: Squares

Post by integer »

albert wrote:How would you convert the speed of sound into inches a second??
...
Divide MULTIPLY that by 5280 that would give you feet per second..
Divide MULTIPLY that by 12 that should give you inches per second...
...
@Albert, that chinese pencil needs to be inverted. Pencils from India do not make that type of mistake. Just my opinion.
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Post by Richard »

Albert wrote:How would you convert the speed of sound into inches a second??
460 MPH
But the speed of sound in air is actually above 760 mph. In glass the speed of sound is 10,150. mph.

If you need the speed of sound in inches per second, just multiply the speed in metres/second by 39.37 which is the number of inches in a metre.

Typical examples of speed of sound are; in air 350 m/s. In water it is 1500 m/s. In glass it is 4540 m/s, and in aluminium 6320 m/s. In concrete it is in the range 3200 to 3700 m/s, while in steel it ranges from 4880 to 5050 m/s. So there are two distinct speeds of sound in steel reinforced concrete, that in the steel bars and that in the concrete. For that reason, reinforced concrete sounds dead and does not ring, so you cannot destroy it by building up a sympathetic oscillation.

Bricks and mortar have different speeds of sound so you cannot ring a brick building. But you can remove mortar from bricks just by hitting the brick so the sound follows and shears the bond.

There is a rock called a phonolite that has the same speed of sound in all it's constituent minerals, so you can ring a phonolite boulder by hitting it with a pick.

I use the SI metre, not the customary USA meter. Like scientists world wide, I use “System International” = SI, MKSA.
Velocity is measured in metre / second, at 20°C, not furlongs per fortnight at 68 degrees fahrenheit. Only US engineers still use long and short ton, Fahrenheit or the British thermal unit = Btu; Everyone else, including the British, use the SI units of tonne, Celsius, joule and watt. That way we never need conversion factors in equations or for units.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Whatever happened to Sandy Kidd's anti gravity machine.

” After Sandy Kidd moved to Australia a second prototype was tested
in Melbourne for three days under the supervision of specialist engineers.
Placed in a sealed wooden box, it was suspended from a cord attached
to an overhead beam fitted with sensitive measuring instruments.
Powered by a model aircraft engine, the entire device lost weight
as the vertical thrust overcame the force of gravity. “People in the lab
were clearly shaken,” said Kidd. The Sandy Kidd Device produces lift
without reacting on air, water or a solid surface and therefore appears
to be defying Newton’s Third Law of Motion that states that every action
must have an equal and opposite reaction.”

I believe he is still working on the project.

Most day to day people here still use the old units.
Pounds ounces, feet inches, M.P.H, knots, even pecks and bushels in granaries.
And of course pints of beer.
S.I. is for international collaboration (like here)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Squares

Post by MrSwiss »

I have to disagree with what's been said about US engineers. I'm used to work with
some of them (in degrees C, meters e.t.c.) without any problems, in a really
strictly regulated environment (EPA 40, CFR 1065), where all units are given in SI
conforming ways.
(Btw. there was even a Presidential Decree in the US, that stated that SI must be
adopted in science/engineering.)

Only the people "in the streets" are refusing to adapt from their "old ways", not
unlike some here, refusing to realize that the 32-bit days are gone ...
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

The speed of sound varies with altitude..

I think at sea level it's like 360 MPH , Test vehicles at the "Bonneville Salt Flats" hit 360 mph.. They called it Mach 1

At 10,000 feet its 460 MPH , The WWII airplanes "Mustangs" were limited to that 460 MPH..

At 30,000 feet its like 1,700 MPH


@Richard

Look up the "Tacoma Narrows Bridge" on Google...
The wind pulsing in just the right gusts per second, had the whole steel and concrete bridge swaying like rubber...
So steel reinforced concrete can be made to wobble like rubber...

I don't think the earth quake machine would work in sand or soil.?? maybe it would ,
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

Material Speed of Sound
Air at 20 oC 343 m/s
Lead 1210 m/s
Gold 3240 m/s
Glass 4540 m/s

Glass is 4540 , 4540 * 100 = 454,000 centimeters a second...
That comes out to like 20,000 - 30,000 thousand hertz... That's to high.. Should be just a few pulses per second...

The 20 - 30 kilohertz would be the resonant frequency of glass.. Were not looking for the resonance.

Like a car woofer can make windows and walls vibrate.. just a low frequency makes it wobble...

The Earth Quake frequency , should be under 100 Hz.. like 1 to 100 pulses per second for the wine glass..
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

Back in WWII the governments experimented with Extremely Low Frequency weapons.. ELF

At 7 hertz , it makes all the enemy soldiers get sick to their stomachs.
But they could never find a way to protect their own soldiers from the waves.

Sound travels out 360 - 360 , so if you use an ELF weapon , you get the effects of it as well as the enemy.
They couldn't find a way to isolate their own soldiers , from the waves.


But the earth quake machine works at low pulse rates..Like a real earth quake..

==========================================================
From the internet.
==========================================================
Human beings can detect sounds in the frequency range 20-20,000 Hertz. If a P wave refracts out of the rock surface into the air, and it has a frequency in the audible range, it will be heard as a rumble. Most earthquake waves have a frequency of less than 20 Hz, so the waves themselves are usually not heard.

Earth quakes are under 20Hz

You start the tamper , tamping at 1 tap per second , and then gradually increase that , till the floor is wobbling like rubber..
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

You put a wine glass on a table and set a speaker next to it....

You start the speaker pulsing once a second.

You gradually increase that to , 1 pulse every 9 10th's of a second..
You keep increasing the pulses by 1 10th of a second interval , till the glass is wobbling like rubber.

I think waves travel through all materials at the same rate..
if you tap any object , the wave will traverse the object in the same time as any other object , of the same dimensions.
I think all objects are the same...

When a car woofer , wobbles a building , the waves travel through the glass at the same speed as the concrete.

If you take a wineglass made of glass , and wineglasses made of other substances. gold , steel , clay , rubber , aluminum.
They would all wobble like rubber at the same pulse rate. Given they all have the same dimensions.


Maybe that pulse rate , is the speed of gravity???? Maybe anti-gravity can be accomplished with sound pulses??
A speaker pulsing at the speed of gravity??

Like when you slap the water in a swimming pool , the wave travels at a certain visible rate, not the speed of sound in water.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

I think that speed of waves in water.. Is the same for all other objects...

If you tap a bowl of water , the wave traveling to the outside of the bowl , is a certain speed...
I think that certain speed is the same for all objects...

The only thing left to consider is the dimensions of the object..

The waves in a 4 inch object , would be twice as fast as waves in an 8 inch object.

You would pulse the 8 inch wineglass , at half the frequency , as the 4 inch wineglass.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Squares

Post by badidea »

I suggest you try it. Get some wine glasses, buy a audio set (amplifier + speaker/woofer) and a wave generator (e.g. second hand) or write a freeBASIC program that outputs sounds via audio output to the amplifier. And let us know the result. Without testing your ideas, you are doing philosophy instead of science.
Locked