⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcm_encode.m

📁 i hope use every file i put here to evry one and thank >
💻 M
字号:
function [code,num_array] = pcm_encode(data)

global H

% get BITS
s = get(H.edit20,'String');
BITS = st2de(s);
BITS = round(BITS);

% find max and min of data
max_data = 1;
min_data = -1;

% find max and min of data
if max(data) > 1;
    max_data = 255;
    min_data = 0;
end
    
% computer delta, center of data and number of steps
no_of_steps = 2^BITS;
delta = (max_data - min_data)/no_of_steps;
center = min_data + ((max_data - min_data)/2);

% initialize num_array(holds steps), bin_array(representative binary values), and lim_array(holds limit values)
num_array = [min_data + delta/2];
bin_array = de2bi(0,BITS);
lim_array = [min_data min_data + delta];

% create num_array, bin_array, and lim_array based on number of bits to be used
for j = 2:no_of_steps,
   num_array(j) = num_array(j - 1) + delta;
   bin_array(j,:) = de2bi(j - 1, BITS);
   lim_array(j + 1) = lim_array(j) + delta;
end

% initialize code array
code = 0;

% initialize code array index
code_index = 1;

% quantize very first data point (will be need for giant for loop following)
for k = 1:no_of_steps,
   if data(1) >= lim_array(k) & data(1) < lim_array(k + 1)
      code(code_index:code_index + (BITS - 1)) = bin_array(k,:);
   end
   if data(1) == max_data
      code(code_index:code_index + (BITS - 1)) = bin_array(no_of_steps,:);
   end
end

% increate code index
code_index = code_index + BITS;

% initialize found flag to indicate whether or not a data point has been quantized
found = 0;

% giant for loop to quantize every data point in data
for j = 2:length(data),
   
   % if previous data point was positive, the loop tries to quantize the current point to a positive number
   % if a level is not found, it quantizes it to a negative number
   if data(j - 1) >= 0
      
      % try to quatize to positive number
      for k = no_of_steps/2 + 1:no_of_steps,
         
         % actual quantizing loop
         if data(j) >= lim_array(k) & data(j) < lim_array(k + 1)
         	code(code_index:code_index + (BITS - 1)) = bin_array(k,:);
            found = 1;
         end
         
         % problem was found when quantizes the max of data with the less than constraint, 
         % this loop fixes that by directly quantizing the max level
      	if data(j) == max_data
         	code(code_index:code_index + (BITS - 1)) = bin_array(no_of_steps,:);
            found = 1;
      	end
      end
      
      % try to quatize to negative number if positive number was not found
      if found == 0
         for k = 1:no_of_steps/2,
            
            % actual quantizing loop
      		if data(j) >= lim_array(k) & data(j) < lim_array(k + 1)
         		code(code_index:code_index + (BITS - 1)) = bin_array(k,:);
            	found = 1;
      		end
         end
      end
   end
   
   % if previous data point was negative, the loop tries to quantize the current point to a negative number
   % if a level is not found, it quantizes it to a positive number
   if data(j - 1) <= 0    
      
      % try to quantize to negative number
      for k = 1:no_of_steps/2,
         
         % actual quantizing loop
      	if data(j) >= lim_array(k) & data(j) < lim_array(k + 1)
         	code(code_index:code_index + (BITS - 1)) = bin_array(k,:);
            found = 1;
      	end
      end
      
   % try to quatize to positive number if negative number was not found
   if found == 0
      for k = no_of_steps/2 + 1:no_of_steps,
         
         % actual quantizing loop
      	if data(j) >= lim_array(k) & data(j) < lim_array(k + 1)
         	code(code_index:code_index + (BITS - 1)) = bin_array(k,:);
           	found = 1;
         end
         
         % problem was found when quantizes the max of data with the less than constraint, 
         % this loop fixes that by directly quantizing the max level
      	if data(j) == max_data
         	code(code_index:code_index + (BITS - 1)) = bin_array(no_of_steps,:);
            found = 1;
      	end
      end
   end
end

	% increase code index and reset found flag
   code_index = code_index + BITS;
   found = 0;
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -