-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantizer.m
71 lines (56 loc) · 2.93 KB
/
quantizer.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
% Quantization Mode
% 0 = Mid rise
% 1 = Mid tread
function [quantized_signal, mean_sqr_q_error, bit_stream, mp_max, mp_min, R] = quantizer(input_signal, t, L, quantization_mode)
mp_max = max(input_signal); %output, to be used by the decoder
mp_min = min(input_signal); %...
delta = (mp_max-mp_min)/L; %step size
if(quantization_mode == 0) %mid rise
%Initializing quantization levels
min_q_level = mp_min + delta/2;
max_q_level = mp_max - delta/2;
quantization_levels = min_q_level : delta : max_q_level;
%clipping the input signal at the boundries of the quantization
%levels === mapping the values outside the boundries of the
%quantization levels into these boundries
modified_input_signal = max(min_q_level, min(input_signal,max_q_level));
%Mid rise to mid tread => so that we can map the amplitudes between
%the middle threshold, in case it = zero (signal amplitudes centered around 0),
% and the first quantization level under that threshold
% to -ve values instead of +ve values (having correct mapping)
modified_input_signal = modified_input_signal + delta/2;
index = quantization(modified_input_signal, delta);
elseif (quantization_mode == 1) %Mid tread
%Initializing quantization levels
min_q_level = mp_min+delta;
max_q_level = mp_max;
quantization_levels = min_q_level : delta : max_q_level;
%clipping the input signal at the boundries of the quantization levels
modified_input_signal = max(min_q_level, min(input_signal,max_q_level));
index = quantization(modified_input_signal, delta);
else
error('Not valid!');
end
quantized_signal = quantization_levels(index); %the actual quantized signal
%figure of input signal and the quantized signal
nexttile
plot(t, input_signal);
hold on
stairs(t,quantized_signal);
legend('Input Signal', 'Quantized Signal');
xlabel('t[sec]');
ylabel('Amplitude');
title('input signal vs. quantized signal');
%the mean square quantization error
q_error = quantized_signal - input_signal;
mean_sqr_q_error = mean(q_error.^2);
%A stream of bits representing the quantized signal
% here we map the quantization levels indices into binary values
% represinting the bit stream (since the indices are positive intger, easy to be mapped into binary numbers and vice versa)
index = index-1; %for 0 to be included in the binary representation of the indices
R = ceil(log2(L)); %bit frame size %ciel is used in case L is not binary weighted
bit_stream = int2bit(index,R);
bit_stream = reshape(bit_stream, 1, numel(bit_stream));
display('Bit stream (first 20 bits):');
display(bit_stream(1:20));
end