Quantization of a standard Cosine signal
    조회 수: 11 (최근 30일)
  
       이전 댓글 표시
    
Hello, could someone please explain the function of the folllowing ( Z1 to Z4 )lines in the MATLAB script along with the summary of what the code does. A slightly detailed explanation is much appreciated. 
Thank you in advance.
close all; clear all; clc
A = 4; fm = 1; M = 20;
t = 0:(1/(M*fm)):1;
xs = A*cos(2*pi*fm*t);  %sampled cosine signal
%Quantization
L = 10;
Length_xs = length(xs);
xq = zeros(1, Length_xs);
L_level = zeros(1, Length_xs);
for loop = 1:Length_xs
     for index = 1:L
       if (A-index*(2*A)/L <=xs(loop)) && (A-(index-1)*(2*A)/L > xs(loop)) 
          xq(loop) = A - index*(2*A)/L + A/L;
          L_level(loop) = L-(index-1); %-------------------------------------Z1
       end
     end
     if xs(loop) == A
        xq(loop) = A - A/L;
        L_level(loop) = L;  %-----------------------------------------Z2
     end
end
%xq is the quantized cosine signal
NN = ceil(log2(L));  %---------------------------------------------Z3
encoded = dec2bin(L_level, NN) %-----------------------------------Z4
plot(t, xs, 'r-')
hold on
plot(t, xq, 'bo')
xlabel('time')
ylabel('signals')
legend('Original signal','quantized signal')
댓글 수: 0
답변 (1개)
  Robert Brown
      
 2021년 3월 30일
        
      편집: John D'Errico
      
      
 2021년 4월 4일
  
      %% Description of Z1
First, we recognize that loop is equal to sample number.
So L_level(loop) is the quantization level of the current sample.
Because MATLAB indexes from 1 to n, instead of 0 to n-1, we would never
reach level 10 quantization unless we subtract 1 from the index.  
The level L=10-(index-1), for index=1 = 10.
Also, they are inverting the levels, bacause the test is "amplitude-index...
In other words, the first index is a test of Level 10, so index=1 gives
Level=10-(index-1) = 10-(1-1) = Level_10.
To continue, index 10 = (Level=10)-(index-1) = (Level=10) - (index=10 - 1)
= 10 - (10-1) = 10-9 = 1.
Therefore, indexes 1:10 test for Levels 10:1 (this could have been programmed
better, without this inverse relationship)
%% Description of Z2
Because the index loop (for index = 1:L) tests for A-index being less than xs,
and also tests whether A-(index-1) is greater than xs, it won't catch the
case where A-(index=1 -1) = A-0 = A is equal to the max amplitude A.
Therefore, the code developer followed up with a test of.... if xs(loop) = A...
then xq(loop) = A - A/L = 4 - 4/10 = 3.6
Note that this test is unnecessary if the code developer changes the second
half of the first test to be (A-(index-1)*(2*A)/L >= xs(loop)
Maybe to be clear, the L_levels are...  Level 10 >= 3.2(to 4), Level 9 >=2.4(to 3.5999999...), 
Level 8 >= 1.6 to 2.4-, Level 7 >= 0.8.., Level 6 >= 0.0-0.799999999, Level 5 >=
-0.8(to 0-), Level 4 >= -1.6(to just under -0.8), Level 3 >= -2.4..., Level
2 >= -3.2..., Level 1 >= -4 to just under -3.2)
Note that the L_levels are not the final answer.  Eventually the final
answer will be the midpoint of the L_levels for each bin.... So Level 10 =
3.2 to 4.0, and will eventually be encoded 3.6, the midpoint of the band.
%% Description of Z3
Because we have 10 levels, and want to express them in binary...
log2 of a number tells what power of 2 = 10
NN = ceil(log2(L))
NN = ceil(log2(10))
since log2(10) = 3.3219
ceil(log2(10) = ceil(3.3219), and ceil rounds up to next integer
so NN = 4(bits needed) when this statement is finished, 
implying 4 bits to hold 1:10 in binary representation.
Level    Binary(encoded)
1           '0001
2           '0010
3           '0011
4           '0100
5           '0101
6           '0110
7           '0111
8           '1000
9           '1001
10          '1010
%% Description of Z4
The dec2bin function converts the base_10 level numbers to base_2 (binary).
In addition, since NN was specified, we will fully pupulate 4 bit binary
text representatons of the 10 levels.
'encoded' contains the binary representation of the level at each of the 21
signal samples.
Level    Binary(encoded)
1           '0001
2           '0010
3           '0011
4           '0100
5           '0101
6           '0110
7           '0111
8           '1000
9           '1001
10          '1010
댓글 수: 3
  John D'Errico
      
      
 2021년 4월 4일
				
      편집: John D'Errico
      
      
 2021년 4월 4일
  
			I just merged all of your answers into one. 
Note there is NO need to enclose all of your comments in %{ and }%. That does nothing in Answers. So I deleted those lines in the merge.
Remember that you edit any response of yours by clicking on the icon to the top right of it that looks like a pencil. It is between the flag icon and the trash can icon.
  Robert Brown
      
 2021년 4월 5일
				Thanks for the edit pencil advice...
I first wrote the comments into a MATLAB code copy of the example, hence the comment % marks in the answers.  They did nothing here, but they did something (commented those answer comments) in the code.  I'm not ADDING % comment markers to my answers here, they were already added when I was writing the answers into the code while debugging this code question.  When I extracted the answers from the code and pasted here, the % markers tagged along...
You MVP's sure are particular about your answers.  Perhaps I'll just leave the answering to the MVPs.  I don't seem to be getting any thanks for my attempts, just complaints that my answers aren't up to "standards".  Disappointing...
참고 항목
카테고리
				Help Center 및 File Exchange에서 Quantizers에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


