-
Notifications
You must be signed in to change notification settings - Fork 2
/
GD_stochasticQ.m
55 lines (47 loc) · 1.08 KB
/
GD_stochasticQ.m
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
function [quantized,number_of_bits_toSend]=GD_stochasticQ(current,prev,bitsToSend)
b=bitsToSend;
tau=1/(2^b-1);
if(b==1)
R=1E-12;
for i=1:length(current)
temp=abs(prev(i)-current(i));
if(temp > R)
R=temp;
end
end
R=R/2;
number_of_bits_toSend =32+32*length(current);
for i=1:length(current)
Q(i)=(current(i)-prev(i)+R)/(2*tau*R);
p=(ceil(Q(i))-Q(i));%/(ceil(Q(i))-floor(Q(i)));
temp=rand;
if(temp <=p)
Q(i)=ceil(Q(i));
else
Q(i)=floor(Q(i));
end
quantized(i)=2*tau*Q(i)*R-R;
number_of_bits_toSend = number_of_bits_toSend + b;
end
else
R=1E-12;
for i=1:length(current)
temp=abs(prev(i)-current(i));
if(temp > R)
R=temp;
end
end
number_of_bits_toSend =32+32*length(current);
for i=1:length(current)
Q(i)=(current(i)-prev(i)+R)/(2*tau*R);
p=(ceil(Q(i))-Q(i));%/(ceil(Q(i))-floor(Q(i)));
temp=rand;
if(temp <=p)
Q(i)=ceil(Q(i));
else
Q(i)=floor(Q(i));
end
quantized(i)=2*tau*Q(i)*R-R;
number_of_bits_toSend = number_of_bits_toSend + b;
end
end