New error correction method.

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

New error correction method.

Post by mrminecrafttnt »

It uses the simple Repeatation code but i advanced it.. and i make it very strong so that more than 50% of corrupted date are possible to correct :)

Code: Select all

function corrected_data(x() as integer) as integer
    dim as ulongint errc_data,divisor
    for i as integer = lbound(x) to ubound(x) - 4
        if x(i) = x(i+1) and x(i) = x(i+2) and x(i) = x(i+4) then 
            errc_data += x(i) 
            divisor+=1 
            PRINT "GOOD DATA @ ";I 
        end if
        
    next
    return errc_data / divisor
end function

sub generate_correctable_data (value as integer,v_data() as integer)
    for i as integer = lbound(v_data) to ubound(v_data)
        v_data(i)=value
    next
end sub


dim as integer example(0 to 99)

'geratation of correctable_data_arrey
generate_correctable_data (512,example())

'now we corrupt some data of the arrey 
randomize timer
for i as integer = 0 to 52 
    example(int(rnd*ubound(example)))=int(rnd*99999999)
next

'new we will output the correct data..
print corrected_data(example())
sleep
'and voila :)
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: New error correction method.

Post by Tourist Trap »

mrminecrafttnt wrote:It uses the simple Repeatation code but i advanced it.. and i make it very strong so that more than 50% of corrupted date are possible to correct
I'm curious. What's the principle of that?
martin123
Posts: 1
Joined: Apr 01, 2017 21:35

Re: New error correction method.

Post by martin123 »

I little confused regarding this method to be applied! If it is possible for you share some visuals to make it more clear!
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Re: New error correction method.

Post by mrminecrafttnt »

Here i added some variables for you to play around with, and i released the visual wish :)

Code: Select all

#define add_errors 'enable me to add errors into the data - engine have to correct this an will be a little bit slower..

const error_level = 2 ' (2 - 8) ,higher levels generates more errors)
const pixel_package_size = 512' (4 - 1512) ' lower values decreases the correction change but needs less power and memory (faster)
'                                            higher values incrases the correction change but needs more power and memory (slower)

function corrected_data(x() as integer) as integer
    dim as ulongint errc_data,divisor
    for i as integer = lbound(x) to ubound(x) - 4
        if x(i) = x(i+1) and x(i) = x(i+2) and x(i) = x(i+4) then
            errc_data += x(i)
            divisor+=1
           ' PRINT "GOOD DATA @ ";I
        end if
       
    next
    return errc_data / divisor
end function

sub generate_correctable_data (value as integer,v_data() as integer)
    for i as integer = lbound(v_data) to ubound(v_data)
        v_data(i)=value 
            
            #ifdef add_errors
            v_data(i) += int(rnd*(1+error_level))
            #endif
    next
end sub



type correctable_pixel
    dim as uinteger c(pixel_package_size)
    declare sub set_pixel_color (col as uinteger)
end type

sub correctable_pixel.set_pixel_color(col as uinteger)
    generate_correctable_data col,this.c()
end sub

dim shared as correctable_pixel image(640,480)
print len(image),640*480*pixel_package_size
screenres 640,480
dim as integer pixel_color
dim as integer corrected_color
for x as integer = 0 to 640
    pixel_color+=1
    for y as integer = 0 to 480
        image(x,y).set_pixel_color pixel_color
        corrected_color = corrected_data(image(x,y).c())
        pset (x,y),corrected_color
        if inkey <> "" then exit for    
    next
next
sleep
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: New error correction method.

Post by srvaldez »

MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: New error correction method.

Post by MrSwiss »

Just some remarks on your (Main) code:
  • Avoid Global variables, if at all possible.
    Use correct variable Type (e.g. Color = ULong).
    Use correct upper bounds, on graphic screen dimensions.
    Add more *white space* for better readability.
    etc.
The corrected and annotated version below:

Code: Select all

' main code
dim shared as correctable_pixel image(640, 480) ' :-/ avoid GLOBALS if at all possible
print SizeOf(image), 640 * 480 * pixel_package_size ' to console

screenres 640, 480, 32                      ' graphics screen

dim as ULong pixel_color, corrected_color   ' color is ULong = UINT32 always!!!

' uinteger is faster (as loop counter) than integer (exception: down to 0 loop)
for x as UInteger = 0 to 640 - 1            ' <-- 0 to 639 !
    pixel_color+=1
    for y as UInteger = 0 to 480 - 1        ' <-- 0 to 479 !
        image(x, y).set_pixel_color pixel_color
        corrected_color = corrected_data(image(x, y).c())
        pset (x, y), corrected_color
        if Len(InKey()) then exit For, For  ' <-- simplified & corrected !
    Next
    if Len(InKey()) then exit For           ' <-- added
Next

Sleep
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Re: New error correction method.

Post by mrminecrafttnt »

Thanks!
Here is a new Version with an optimized "corrected_data" function :)

Code: Select all

#define add_errors 'enable me to add errors into the data - engine have to correct this an will be a little bit slower..
const error_level = 3 ' (2 - 8) ,higher levels generates more errors)
const pixel_package_size = 128' (4 - 1512) ' lower values decreases the correction change but needs less power and memory (faster)
'                                            higher values incrases the correction change but needs more power and memory (slower)

function corrected_data_old(x() as integer) as integer
    dim as ulongint errc_data,divisor
    for i as integer = lbound(x) to ubound(x) - 4
        if x(i) = x(i+1) and x(i) = x(i+2) and x(i) = x(i+4) then
            errc_data += x(i)
            divisor+=1
           ' PRINT "GOOD DATA @ ";I
        end if
       
    next
    return errc_data / divisor
end function


function corrected_data_new(x() as integer) as integer
    dim as ulongint errc_data,divisor
    for i as integer = lbound(x) to ubound(x) - 3 step 2
        if ((x(i) + x(i+1)) = (x(i+2) + x(i+3))) then
            errc_data += x(i)
            divisor+=1
            'PRINT "GOOD DATA @ ";I
        end if
       
    next
    return errc_data / divisor
end function


sub generate_correctable_data (value as integer,v_data() as integer)
    for i as integer = lbound(v_data) to ubound(v_data)
        v_data(i)=value
           
            #ifdef add_errors
            v_data(i) += int(rnd*(1+error_level))
            #endif
    next
end sub



type correctable_pixel
    dim as uinteger c(pixel_package_size)
    declare sub set_pixel_color (col as uinteger)
end type

sub correctable_pixel.set_pixel_color(col as uinteger)
    generate_correctable_data col,this.c()
end sub

' main code
dim shared as correctable_pixel image(640, 480) ' :-/ avoid GLOBALS if at all possible
print SizeOf(image), 640 * 480 * pixel_package_size ' to console

screenres 640, 480, 32                      ' graphics screen

dim as ULong pixel_color, corrected_color   ' color is ULong = UINT32 always!!!

' uinteger is faster (as loop counter) than integer (exception: down to 0 loop)
for x as UInteger = 0 to 640 - 1            ' <-- 0 to 639 !
    pixel_color+=1
    for y as UInteger = 0 to 480 - 1        ' <-- 0 to 479 !
        image(x, y).set_pixel_color pixel_color
        corrected_color = corrected_data_old(image(x, y).c())
        pset (x, y), corrected_color
        if Len(InKey()) then exit For, For  ' <-- simplified & corrected !
    Next
    if Len(InKey()) then exit For           ' <-- added
Next
Print "Old Algorythm - press a key"
Sleep
cls
'dim shared as correctable_pixel image(640,480)

'dim as integer pixel_color
'dim as integer corrected_color
for x as integer = 0 to 640
    pixel_color+=1
    for y as integer = 0 to 480
        image(x,y).set_pixel_color pixel_color
        corrected_color = corrected_data_new(image(x,y).c())
        pset (x,y),corrected_color

    next
            
next
print "New Algorythm - press a key"
sleep
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: New error correction method.

Post by Tourist Trap »

srvaldez wrote:perhaps you may find Repair damaged image pixel using Laplace Interpolation of interest
Impressive!
BasicCoder2
Posts: 3908
Joined: Jan 01, 2009 7:03
Location: Australia

Re: New error correction method.

Post by BasicCoder2 »

This reminds me of the old salt and pepper removal routine.
On line 48 you will have to insert a 640x480 32bit image of your own.
bload "finches.bmp",image1 'replace with own bitmap image
Also maybe replace the uinteger with ulong.

Code: Select all

screenres 640,480,32
color rgb(0,0,0),rgb(255,255,255):cls
dim as any ptr image1,image2,image3
image1 = imagecreate(640,480,0)
image2 = imagecreate(640,480,0)
image3 = imagecreate(640,480,0)

sub medianFilter(image2 as any ptr,image1 as any ptr)
    dim as uinteger v,r,g,b
    dim as integer ii
    dim as uinteger mList(10),cList(10)
    for j as integer = 1 to 478
        for i as integer = 1 to 638
            ii = 0
            for bb as integer = -1 to 1
                for aa as integer = -1 to 1                
                    v = point(i+aa,j+bb,image1)
                    cList(ii) = v
                    r = v shr 16 and 255
                    g = v shr 8 and 255
                    b = v and 255
                    v = (r+g+b)\3
                    mList(ii)=v
                    ii = ii + 1
                next aa
            next bb
            'sort list
            dim as integer swapFlag
            do
                swapFlag = 0
                for k as integer = 0 to 7
                    if mList(k)>mList(k+1) then
                        swap mList(k),mList(k+1)
                        swap cList(k),cList(k+1)
                        swapFlag = 1
                    end if
                next k
            loop until swapFlag = 0
            v = cList(4)
            r = v shr 16 and 255
            g = v shr 8 and 255
            b = v and 255
            pset image2,(i,j),rgb(r,g,b)
        next i
    next j
end sub

bload "finches.bmp",image1

'add salt and pepper noise
dim as uinteger r,g,b,v
dim as integer x,y
for i as integer = 0 to 10000
    x = int(rnd(1)*640)
    y = int(rnd(1)*480)
    r = int(rnd(1)*256)
    g = int(rnd(1)*256)
    b = int(rnd(1)*256)
    pset image1,(x,y),rgb(r,g,b)
next i
put (0,0),image1,pset         'display the image
medianFilter(image2,image1)   'filter the image

sleep

put (0,0),image2,pset         'display the result

sleep
Post Reply