-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsamp_rec.m
88 lines (80 loc) · 2.2 KB
/
samp_rec.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function samp_rec(fs);
% Signal Construction
N = 100;
t = 1 : N;
sig = 2*sin(2*pi*400*t/8000);
figure
plot(sig);
TITLE ('The Signal in Time Domain');
grid on;
p_max = 2;
p_min = -2;
% Sampling the Signal
b = 8000/fs; % sampling interval
samp = zeros(1,N); % constructing a zero array
for (p = 1 : b : N)
samp(p) = sig(p);
end
figure
stem(samp);
TITLE ('The Sampled Signal')
grid on;
% Quantizing the Sampled Signal
no = N/b; % no. of samples
z = ceil(5*(no)); % no. of bits required
quan = zeros(1,z); % constructing a zero array
c = (abs(p_max) + abs(p_min))/32 % size of quantized level
u = p_min;
w = 1;
quant = zeros(1,N);
for (q = 1 : b : N)
for (r = 1 : 32)
y = r;
if (samp(q)>=u & samp(q)<=(u+c))
for (v = (w+4) : -1 : w) % this loop is for converting decimal into binary
quan(v) = mod(y,2);
y = (y/2)- (mod(y,2)/2);
end
quant(q) = r;
end
u = u + c;
end
u = p_min;
w = w + 5;
end
quan
% Writing the Quantized Signal
fid = fopen ('quant.bin','w');
count = fwrite (fid,quant,'int8');
% Reading the Quantized Signal
fid = fopen ('quant.bin','r');
[quant_r,count] = fread (fid,inf,'int8');
% Reconstructing the Sample of the Quantized Signal
for (n = 1 : N)
u = p_min;
for (ab = 1 : 32)
if (quant_r(n)==ab)
quant_r(n) = u;
end
u = u + c;
end
end
figure
stem (quant_r);
TITLE ('The Reconstructed Sample of Quantized Signal')
grid on;
% Reconstructing the Original Signal
for (c1 = 1 : b : (N-b))
if (b~=1)
xyz = (quant_r(c1+b)-quant_r(c1))/b;
for (c2 = (c1+1) : (c1+b-1))
quant_r(c2) = quant_r(c2-1) + xyz;
end
end
end
figure
quant_r = smooth (quant_r);
plot (quant_r);
TITLE ('The Reconstructed Signal')
grid on;
end