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

📄 adc.m

📁 此代码仿真了一个语音输入输出系统
💻 M
字号:
% ADC   Performs analog to digital conversion on the spread voice 
%   signal after processing it through a Mu-law compander.

%   Completeed: July 24, 2005

%   Terrence Irving
%   2005 NSF REU in SDR
%   Stevens Institute of Technology
%   Hoboken, NJ USA



% Globalize object handles that this component must access.
global adc_pb sssa_base done_text_fontsize adc_done_dims spread_pb

% Change previous button's color (MATLAB 7 precaution).
set(spread_pb, 'backgroundcolor', 'white');
set(spread_pb, 'foregroundcolor', 'black');

% Apply Mu-law algorithm (the quantization technique follows an example given 
% in MATLAB Help under "Communications Toolbox: Using the Communications 
% Toolbox: Companding a Signal").
MU = 255; % value for mu, according to the north american and japanese standards
voice_data_spread = voice_data_spread'; % turn input column vector into a row vector
V1 = max(voice_data_spread); % get maximum value of input signal
INT_FACTOR = 10; % integer "conversion"/normalization factor--the larger it is, the more significant digits that are considered
voice_data_spread_compressed = compand(voice_data_spread, MU, V1, 'mu/compressor'); % compress the unchanged input signal
voice_data_spread_compressed_normalized = floor(voice_data_spread_compressed*INT_FACTOR); % get integer approximation of compressed signal
partition = [min(voice_data_spread_compressed_normalized): 1 : max(voice_data_spread_compressed_normalized)]; % partition range is based on an integer approximation of compressed signal (multiplied by 1000 to get integers)
codebook = [min(voice_data_spread_compressed_normalized): 1 : max(voice_data_spread_compressed_normalized) + 1]; % codebook is same as partition with one extra element
[index, quantized_data] = quantiz(voice_data_spread_compressed_normalized, partition, codebook); % quantize the normalized array
clear index; % the quantization index is not needed

% Change quantized_data to a column vector.
quantized_data = quantized_data';

% Only keep the left channel, as with the original signal.
quantized_data = quantized_data(:, 1);

% Store the number of rows in quantized_data.
temp = size(quantized_data);
qd_rows = temp(1);

% Allocate an array to hold 1s and -1s based on the polarity of each
% quantized_data value.
polarity_array = ones(qd_rows, 1);

% Change 1s in polarity_array to -1s as appropriate, based on the negative
% values in quantized_data.
for i = 1: qd_rows
    if quantized_data(i, 1) < 0 % if it is a negative value...
        polarity_array(i, 1) = 0; % note this in polarity_array
    end
end

% Now that quantized_data's values' signs have been recorded, remove
% polarity from quantized_data so that it can be encoded with a basic
% integer-to-binary conversion algorithm.
quantized_data_signless = abs(quantized_data);

% Know from tests that max(abs(quantized_data)) will be small enough to be
% represented by four bits.
BITS_NEEDED = 4;

% Allocate memory to store the encoded data, which consists of the quantized_data
% and polarity_array arrays (+ qd_rows term accounts for polarity_array
% data, which will also be encoded).
encoded_data = ones(BITS_NEEDED * qd_rows + qd_rows, 1);

% Fill encoded_data with the binary equivalences of the values in 
% quantized_data.  up_counter is incremented by the number of bits in
% each binary equivalence (ie BITS_NEEDED) each time one such equivalence
% is copied into encoded_data.  up_counter tells i where to begin, and i
% is an index into encoded_data.  The idea is that i starts at up_counter
% (for example, i starts at 1 the first time through) and counts up to
% the row in encoded_data where the last bit of the current binary word
% will end (continuing with the example, the first such last row in
% encoded_data will be row 8, which, for the first iteration of j, is
% equal to BITS_NEEDED*j).  j is a row index into quantized_data. down_
% counter starts at BITS_NEEDED and is decremented by 1 each time one bit
% of the current binary words (plural because both values in a quantized_
% data row are converted and copied with each iteration of j) is gotten
% by bitget and copied into encoded_data.  For example, if BITS_NEEDED is
% 8, then down_counter counts from 8 to 1 each time j is incremented.
disp('Encoding quantized data');
up_counter = 1;
for j = 1: qd_rows
    down_counter = BITS_NEEDED;
    for i = up_counter: BITS_NEEDED*j
        encoded_data(i, 1) = bitget(quantized_data_signless(j, 1), down_counter);
        down_counter = down_counter-1;    
    end
    up_counter = up_counter+BITS_NEEDED;
end
disp('Quantized data encoded');

% Now fill the rest of encoded_data with polarity_array.
disp('Placing quantized data polarity information at end of encoded data array');
j = 1; % will manually traverse polarity_array
for i = (BITS_NEEDED*qd_rows)+1: BITS_NEEDED*qd_rows+qd_rows % ie start at cell 32001, end at 40000
        encoded_data(i, 1) = polarity_array(j, 1);
        j = j + 1; % increment polarity_array index
end
disp('Polarity information placed at end of encoded data array');
disp('Binary sequence created');
disp(' '); % blank line

% Free memory.
clear BITS_NEEDED down_counter ed_rows filt1;
clear i j temp trans_den trans_num;
clear up_counter V1 codebook partition;
clear polarity_array quantized_data quantized_data_signless;
clear voice_data_spread_compressed_normalized;

% Change button color and update text when component is finished.
set(adc_pb, 'backgroundcolor', 'white');
set(adc_pb, 'foregroundcolor', 'black');
adc_done = uicontrol(sssa_base,...
    'style', 'text',...
    'fontsize', done_text_fontsize,...
    'foregroundcolor', 'white',...
    'backgroundcolor', [.5 0 0],... % matches background image color
    'horizontalalignment', 'center',...
    'string', 'DONE',...
    'position', adc_done_dims);

% Pause for one second before continuing, giving the button time to update.
pause(1); 

% Continue on.
bpsk;

⌨️ 快捷键说明

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