-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhex2dec_octave
67 lines (57 loc) · 1.89 KB
/
hex2dec_octave
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
#Convert data from Altera hex-file to decimal
function use_help() {
echo -e "Format isn't correct!
\rUse \033[1mhex2dec_vram VRAM.hex conv 0 0 12\033[0m to convert format to x0_0 file.
\rUse \033[1mhex2dec_vram VRAM.hex fft 0 0 12\033[0m to convert format and open data and FFT in Octave session.
\r\$3 - 32-bit signal select 0 to 7
\r\$4 - 16 bit sel from 32-bit signal
\r\$5 - real bit-width of signal"
exit
}
if [ "$#" -lt 5 ]
then
use_help
fi
tmp_file="/tmp/vr"
function data_file_prepare() {
#$1 - data filename
#$2 - 32-bit signal 0 to 7
#$3 - 16 bit sel from 32-bit signal
#$4 - real bit-width of signal
local hex_bits="$4"
local max_bits=`echo "2^$hex_bits" | bc`
local thr_bits=`echo "$max_bits/2-1" | bc`
if [ "$3" = 0 ]
then
local bits_sel="10,11,12,13"
else
local bits_sel="6,7,8,9"
fi
cut $1 -c$bits_sel | head -n -1 > $tmp_file
local full_length=`wc -l $tmp_file | cut -d " " -f1`
octave -q --eval "x = hex2dec(textread(\"$tmp_file\", \"%s\"));
for k=1:$full_length if(x(k)>$thr_bits) x(k)=x(k)-$max_bits; end; end
file_id = fopen(\"x$2_$3\", 'w');
fprintf(file_id,\"%i\\n\",x);
fclose(file_id);"
rm $tmp_file
echo "Decimal $4-bit data writed to x$2_$3 file, $full_length samples."
}
#Use octave for format conversion
case $2 in
conv)
data_file_prepare $1 $3 $4 $5
;;
fft)
data_file_prepare $1 $3 $4 $5
octave -q --persist --eval "x = load(\"x$3_$4\");
x_absfft = abs(fft(x));
f=20*log10(x_absfft/max(x_absfft));
freq=(1:length(f)/2)*100/(length(f)/2);
disp(\"To see data type plot(x)\");
disp(\"To see FFT type plot(freq,f(1:length(f)/2))\");"
;;
*) use_help
;;
esac