Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
lab-integrador
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
comina
lab-integrador
Commits
282532e6
Commit
282532e6
authored
2 years ago
by
Gonzalo Belcredi
Browse files
Options
Downloads
Patches
Plain Diff
Added automated bit sync and BER
parent
f15b35a0
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
bit2image.py
+88
-17
88 additions, 17 deletions
bit2image.py
with
88 additions
and
17 deletions
bit2image.py
+
88
−
17
View file @
282532e6
...
...
@@ -24,18 +24,67 @@
from
PIL
import
Image
import
numpy
as
np
import
math
import
pickle
filename_bin
=
'
dat/received_241022_2.dat
'
DECODE_IMAGE
=
True
filename_bin
=
'
dat/recepcion.dat
'
img_height
=
800
img_width
=
549
sync_start
=
[
0
,
128
,
255
]
*
10
sync_size
=
30
array_rx
=
np
.
fromfile
(
filename_bin
,
dtype
=
'
uint8
'
)
columnas
=
img_height
+
sync_size
filas
=
int
(
len
(
array_rx
)
/
columnas
)
DECODE_IMAGE
=
True
def
sync_bits
(
array_bytes
):
print
(
'
\n
Performing bit synchronization...
'
,
end
=
""
)
nr_shift
=
0
corr
=
correlate_preamble
(
array_bytes
)
for
i
in
range
(
1
,
7
):
array_bytes_shifted
=
shift_bits_bytes
(
array_bytes
,
shift_nr_bits
=
i
,
nr_bytes
=
len
(
array_bytes
))
corr_i
=
correlate_preamble
(
array_bytes_shifted
)
if
corr_i
>
corr
:
nr_shift
=
i
corr
=
corr_i
print
(
"
Shifting {} bits.
"
.
format
(
nr_shift
),
end
=
""
)
print
(
'
Completed!
'
)
return
shift_bits_bytes
(
array_bytes
,
shift_nr_bits
=
nr_shift
,
nr_bytes
=
len
(
array_bytes
))
def
correlate_preamble
(
array_bytes
):
sync_start
=
np
.
array
([
0
,
128
,
255
]
*
10
,
dtype
=
np
.
float32
)
return
np
.
max
(
np
.
correlate
(
array_bytes
.
astype
(
np
.
float32
)
-
127.0
,
sync_start
-
127.0
,
mode
=
'
valid
'
))
def
shift_bits_bytes
(
array_bytes
,
shift_nr_bits
,
nr_bytes
):
bits
=
[
format
(
x
,
'
08b
'
)
for
x
in
array_bytes
[
0
:
nr_bytes
]]
array_bytes_sync
=
np
.
zeros
(
len
(
bits
)
-
1
,
dtype
=
'
uint8
'
)
for
i
in
range
(
len
(
bits
)
-
1
):
# Shift bits and get new bytes
array_bytes_sync
[
i
]
=
int
(
bits
[
i
][
shift_nr_bits
:]
+
bits
[
i
+
1
][
0
:
shift_nr_bits
],
2
)
return
array_bytes_sync
def
get_BER
(
image_bytes
):
filename_baseline_bin
=
'
dat/imagen_bin.dat
'
array_baseline_bytes
=
np
.
fromfile
(
filename_baseline_bin
,
dtype
=
'
uint8
'
)
array_received_bytes
=
image_bytes
.
flatten
()
total_bits
=
8
*
len
(
array_baseline_bytes
)
bit_errors
=
0
for
i
in
range
(
len
(
array_baseline_bytes
)):
bits_baseline
=
format
(
array_baseline_bytes
[
i
],
'
08b
'
)
bits_received
=
format
(
array_received_bytes
[
i
],
'
08b
'
)
errors
=
sum
(
bits_baseline
!=
bits_received
for
bits_baseline
,
bits_received
in
zip
(
bits_baseline
,
bits_received
))
bit_errors
+=
errors
return
bit_errors
/
total_bits
def
get_PSNR
(
image1
,
image2
):
value
=
0
...
...
@@ -92,9 +141,16 @@ def get_sync_peaks(signal, sync):
return
peaks
'''
Visualización de señal en recepción
'''
array_rx
=
array_rx
[
0
:
filas
*
columnas
]
array_bytes
=
np
.
fromfile
(
filename_bin
,
dtype
=
'
uint8
'
)
array_bytes_sync
=
sync_bits
(
array_bytes
)
columnas
=
img_height
+
sync_size
filas
=
int
(
len
(
array_bytes_sync
)
/
columnas
)
'''
Visualización de señal en recepción
'''
array_rx
=
array_bytes_sync
[
0
:
filas
*
columnas
]
image_rx
=
np
.
reshape
(
array_rx
,(
filas
,
columnas
))
image
=
Image
.
fromarray
(
image_rx
.
astype
(
'
uint8
'
))
image
.
save
(
'
images/imagen_recibida.png
'
)
...
...
@@ -102,29 +158,44 @@ image.show()
if
DECODE_IMAGE
:
print
(
'
\n
Decoding image...
'
,
end
=
""
)
peaks
=
get_sync_peaks
(
array_rx
,
sync_start
)
nr_images
=
len
(
peaks
)
print
(
"
Se identificaron
"
,
nr_images
,
"
imágenes.
"
)
print
(
"
Indices de comienzo de imagen:
"
,
peaks
)
print
(
'
Completed!
'
)
nr_images
=
len
(
peaks
)
-
1
print
(
"
\n
Se identificaron
"
,
nr_images
,
"
imágenes.
"
)
for
i
in
range
(
0
,
nr_images
):
if
(
peaks
[
i
]
+
img_width
*
columnas
)
<
len
(
array_rx
):
array
=
array_rx
[
peaks
[
i
]:
peaks
[
i
]
+
img_width
*
columnas
]
matrix
=
np
.
reshape
(
array
,(
img_width
,
columnas
))
with
open
(
'
images/imagen_recibida_
'
+
str
(
i
)
+
'
.pkl
'
,
'
wb
'
)
as
handle
:
pickle
.
dump
(
matrix
,
handle
,
protocol
=
pickle
.
HIGHEST_PROTOCOL
)
'''
Quito el frame de sincronización para recuperar la imagen original
'''
matrix_deframed
=
matrix
[:,
30
:]
image
=
Image
.
fromarray
(
matrix_deframed
.
astype
(
'
uint8
'
))
image
.
save
(
'
images/imagen_recibida_
'
+
str
(
i
+
1
)
+
'
.png
'
)
image
.
save
(
'
images/imagen_recibida_
'
+
str
(
i
)
+
'
.png
'
)
image
.
show
()
'''
Calculo MSE y PSNR
'''
img
=
Image
.
open
(
'
images/image.png
'
)
img_rx
=
Image
.
open
(
'
images/imagen_recibida_1.png
'
)
#Seleccionar aquí la imagen correspondiente
img
.
load
()
for
i
in
range
(
0
,
nr_images
):
img_rx
=
Image
.
open
(
'
images/imagen_recibida_
'
+
str
(
i
)
+
'
.png
'
)
#Seleccionar aquí la imagen correspondiente
img_rx
.
load
()
MSE
,
PSNR
=
get_PSNR
(
img
,
img_rx
)
print
(
"
\n
Image
"
,
i
,
"
:
\n
"
)
print
(
"
MSE:
"
,
MSE
)
print
(
"
PSNR (dB):
"
,
PSNR
)
try
:
with
open
(
'
images/imagen_recibida_
'
+
str
(
i
)
+
'
.pkl
'
,
'
rb
'
)
as
handle
:
matrix_deframed
=
pickle
.
load
(
handle
)
BER
=
get_BER
(
matrix_deframed
)
print
(
"
BER:
"
,
BER
)
except
:
print
(
'
I/O error.
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment