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

📄 debpsk.m

📁 此代码仿真了一个语音输入输出系统
💻 M
字号:
% DE-BPSK   Undoes the BPSK algorithm using a correlation algorithm.
%   This component takes a (filtered) BPSK carrier signal and
%   determines the transmitted binary sequence.

%   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 debpsk_pb sssa_base done_text_fontsize debpsk_done_dims srrc_2_pb

% Globalize the appropriate data created by this component.
global rec_filtered_trimmed ref_cycle first_product bin_sequence_received

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

% Get the length of the received/filtered sinusoid.
temp = size(rec_carrier_filtered);
rec_carrier_filtered_length = temp(1, 1);
disp('Filtered signal length calculated');

% Remove unnecessary data from the beginning of the filtered
% waveform (for now, looks to be 13 cells worth) and set the first and
% last values of the array equal to 0.
disp('Trimming filtered signal');
rec_filtered_trimmed = rec_carrier_filtered(13: rec_carrier_filtered_length-13, :); % trim beginning and end of waveform
rec_filtered_trimmed(1, 1) = 0;
temp = size(rec_filtered_trimmed);
rec_filtered_trimmed_length = temp(1, 1);
rec_filtered_trimmed(rec_filtered_trimmed_length, 1) = 0;
disp('Filtered signal trimmed');

% The cascaded square root raised cosine filters cause an upsampling factor
% of 2, and, therefore, there are 80 data points per cycle in the 
% received/trimmed (TR) signal.
DP_PER_TR_CYCLE = 80;

% Calculate the total number of bits expected.
total_bits_expected = (rec_filtered_trimmed_length - 1) / DP_PER_TR_CYCLE;

% Allocate memory for the received binary sequence.
bin_sequence_received = ones(total_bits_expected, 1);

% Create a reference cycle from sinusoid_data.  This cycle will be the basis
% for comparison when determining 1s and 0s, and it represents a binary 1.
ref_cycle = sinusoid_data(:, 1:41)'; % arbitrarily choose first cycle

% Next take each cycle of rec_filtered_trimmed, downsample it, and multiply
% it with the reference cycle.  If the product is positive (all datapoints
% are above 0), then then the reference cycle and the received cycle in
% question are in phase and a binary 1 has been detected.  If the product
% is negative, then a binary 0 has been detected.
cycle_start = 1; % starting index of current received cycle
cycle_end = 81; % ending index of current received cycle
Z_TOLERANCE = -10e-015; % rather than checking 0, will check that product values are larger than this tolerance
for a = 1: total_bits_expected % this loop runs until the received binary sequence array is filled
    % Get the current received/filtered/trimmed cycle in question.
    current_rec_cycle = rec_filtered_trimmed(cycle_start:cycle_end, :);

    % Downsample the current cycle (each cycle is now considered to have 41 or 
    % 81 data points (ie transition points are considered shared, so they are 
    % double-counted as ending points and beginning points).
    current_rec_cycle_ds = ones(41, 1); % downsampled cycle will contain 41 samples
    counter = 1; % index into current_rec_cycle
    for i = 1:41
        current_rec_cycle_ds(i, :) = current_rec_cycle(counter, :); 
        counter = counter + 2; % skip next original cell
    end

    % Compare the reference cycle to the current downsampled cycle by
    % multiplying the two.
    product = ref_cycle .* current_rec_cycle_ds;
    
    % Save the first product for plotting in the user interface.
    if cycle_start == 1
        first_product = product;
    end
    
    % If the product is positive (ie all data points are greater than OR equal to 0),
    % then current_rec_cycle_ds represents a binary 1.  If not, then it represents a
    % 0 (which, in the array, will be convyed by a -1).  Because the "0's"
    % of the product array tend to be VERY slightly negative (as opposed to being
    % exactly equal to 0), they will be ignored.
    if (product(1:40, :) >= Z_TOLERANCE)
        bin_sequence_received(a, 1) = 1;
    else
        bin_sequence_received(a, 1) = -1;
    end
    
    % Update cycle_start and cycle_end.  The next cycle will start where
    % the previous cycle ended and end 80 data points later.  This will
    % double-count transition points as both end points and beginning
    % points and will assign 81 data points to each cycle (as opposed to
    % 80).
    cycle_start = cycle_end;
    cycle_end = cycle_end + 80;
end

% Replace -1s in bin_sequence_received with 0s, and the BPSK to binary
% sequence conversion is complete.
disp('Replacing -1s with 0s in binary array')
for r = 1: total_bits_expected
    if bin_sequence_received(r, 1) == -1
        bin_sequence_received(r, 1) = 0;
    end
end
disp('-1s replaced with 0s in binary array');
disp('Received binary quantized data recovered');
disp(' '); % blank line

%------------------------------BIT-CHECKING CODE------------------------------%

% % The following code will run a bit-checking routine on the transmitted/
% % received data.  It is not necessary to 3SA.
% 
% % Report.
% disp(['Total bits sent = ' num2str(total_bits)]);
% disp(['Total bits expected = ' num2str(total_bits_expected)]);
% disp(['Total transitions identified = ' num2str(total_bits_expected-1)]);
% disp(' '); % blank line
% 
% % Success rate calculation and report.
% match_table = ones(total_bits_expected, 2);
% match_table(:, 1) = [1:total_bits_expected]'; % number bits
% matched = 0; % number of bits that match up
% for z = 1: total_bits_expected % traverse the sent and received binary sequences
%     if bin_sequence(z, 1) == bin_sequence_received(z, 1) % if they match, tally up
%         matched = matched + 1;
%     end
%     match_table(z, 2) = bin_sequence(z, 1) == bin_sequence_received(z, 1); % put 1 or 0, depending on match
% end
% match_table_beg = match_table(1:1000, :); % beginning
% % match_table_mid = match_table(63000:64000, :); % middle
% % match_table_end = match_table(127000:128000, :); % end
% disp(['Bit match rate = ' num2str((matched / total_bits_expected) * 100) '%']);
% disp(' '); % blank line
% 
% % Failure table.
% failure_table_basic = ones(total_bits_expected - matched, 2);
% failure_table_detailed = ones(total_bits_expected - matched, 5);
% index = 1; % index into failure_table
% for z = 1: total_bits_expected
%     if match_table(z, 2) == 0 % if a failure is found in match_table, put it in failure_table's
%         failure_table_detailed(index, 1) = match_table(z, 1); % number of failed bit
%         failure_table_basic(index, 1) = match_table(z, 1);
%         failure_table_detailed(index, 2) = bin_sequence(z, 1); % what it should be
%         failure_table_detailed(index, 3) = bin_sequence_received(z, 1); % what it is
%         failure_table_basic(index, 2) = bin_sequence_received(z, 1);
%         failure_table_detailed(index, 4) = transition_array(z-1, 2); % transition type
%         failure_table_detailed(index, 5) = bin_sequence_received(z-1, 1); % previous bit
%         index = index + 1;
%     end
% end
% 
% % Random bit comparison (compares five sent bits to corresponding five received bits).
% random_bit_index = randint(1, 1, [1, total_bits_expected]);
% equality_result = (bin_sequence(random_bit_index, 1) == bin_sequence_received(random_bit_index, 1));
% disp(['Bit: ' num2str(random_bit_index) '; Result of equality test: ' num2str(equality_result)]);
% random_bit_index = randint(1, 1, [1, total_bits_expected]);
% equality_result = (bin_sequence(random_bit_index, 1) == bin_sequence_received(random_bit_index, 1));
% disp(['Bit: ' num2str(random_bit_index) '; Result of equality test: ' num2str(equality_result)]);
% random_bit_index = randint(1, 1, [1, total_bits_expected]);
% equality_result = (bin_sequence(random_bit_index, 1) == bin_sequence_received(random_bit_index, 1));
% disp(['Bit: ' num2str(random_bit_index) '; Result of equality test: ' num2str(equality_result)]);
% random_bit_index = randint(1, 1, [1, total_bits_expected]);
% equality_result = (bin_sequence(random_bit_index, 1) == bin_sequence_received(random_bit_index, 1));
% disp(['Bit: ' num2str(random_bit_index) '; Result of equality test: ' num2str(equality_result)]);
% random_bit_index = randint(1, 1, [1, total_bits_expected]);
% equality_result = (bin_sequence(random_bit_index, 1) == bin_sequence_received(random_bit_index, 1));
% disp(['Bit: ' num2str(random_bit_index) '; Result of equality test: ' num2str(equality_result)]);
% disp(' ');
% 
% % % Code to plot comparisons.
% % subplot(2, 1, 1)
% % plot(carrier(1, 1:250))
% % title('Original BPSK Signal');
% % subplot(2, 1, 2)
% % plot(rec_filtered_trimmed(1:1000, 1))
% % title(['Trimmed Received/Filtered BPSK Signal (Filter Parameters: Fd = ' num2str(Fd) ' Hz, Fs = ' num2str(Fs) ' Hz, R = ' num2str(R) ')']);
% % 
% % % Plot received vs. trimmed.
% % subplot(2, 1, 1)
% % plot(rec_carrier_filtered(1:500, 1))
% % title(['Original Received/Filtered BPSK Signal (Filter Parameters: Fd = ' num2str(Fd) ' Hz, Fs = ' num2str(Fs) ' Hz, R = ' num2str(R) ')']);
% % subplot(2, 1, 2)
% % plot(rec_filtered_trimmed(1:500, 1))
% % title(['Trimmed Received/Filtered BPSK Signal (Filter Parameters: Fd = '
% % num2str(Fd) ' Hz, Fs = ' num2str(Fs) ' Hz, R = ' num2str(R) ')']);

%------------------------------BIT-CHECKING CODE------------------------------%



% Free memory.
clear DP_PER_TR_CYCLE Fd Fs Z_TOLERANCE a counter current_rec_cycle current_rec_cycle_ds;
clear cycle_end cycle_start equality_result failure_table_basic failure_table_detailed i;
clear index match_table match_table_beg matched product r random_bit_index;
clear temp z rec_carrier_filtered_length rec_filtered_trimmed_length;

% Change button color and update text when component is finished.
set(debpsk_pb, 'backgroundcolor', 'white');
set(debpsk_pb, 'foregroundcolor', 'black');
debpsk_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', debpsk_done_dims); 

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

% Continue on.
dac;

⌨️ 快捷键说明

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