Question: why won't this code decode a voice code? do I need to plug the decoded digits into an array and decode the voice code that

why won't this code decode a voice code? do I need to plug the decoded digits into an array and decode the voice code that way? here is the matlab code; clear
clc
% Voice-Code Breaker
clc;
clear;
% Load the corrupted voice code file
[voiceCode, fs]= audioread('VoiceCode (5).wav');
% Define parameters
digit_samples =16000; % Number of samples per digit
num_samples = length(voiceCode);
num_digits = floor(num_samples / digit_samples); % Calculate the number of digits
fprintf('Number of digits in the voice code: %d
', num_digits);
% Preallocate storage for digit segments
digit_segments = cell(num_digits, 1);
for i =1:num_digits
start_idx =(i -1)* digit_samples +1;
end_idx = i * digit_samples;
digit_segments{i}= voiceCode(start_idx:end_idx); % Extract each digit's segment
end
% Generate synthetic templates for digits 0 to 9
templates = cell(10,1);
for i =0:9
t =(0:digit_samples -1)/ fs; % Time vector
freq =500+100* i; % Frequency corresponding to each digit
templates{i +1}= sin(2* pi * freq * t)'; % Generate sine wave template
end
% Initialize result storage
decoded_digits = zeros(1, num_digits);
inner_products = zeros(num_digits, 10);
% Decode each digit
for digit_idx =1:num_digits
% Compute inner products with all templates
for template_idx =1:10
inner_products(digit_idx, template_idx)= abs(dot(digit_segments{digit_idx}, templates{template_idx}));
end
% Identify the digit with the maximum inner product
[~, max_idx]= max(inner_products(digit_idx, :));
decoded_digits(digit_idx)= max_idx -1; % Convert index to digit
end
% Output results
fprintf('Decoded Voice Code: %s
', num2str(decoded_digits));
disp('Magnitudes of Inner Products:');
disp(inner_products);
% Plot the first digit and its corresponding template as separate figures
% Smooth the graphs using a moving average filter
window_size =50; % You can adjust the window size for smoothing
smooth_digit = movmean(digit_segments{1}, window_size);
smooth_template = movmean(templates{decoded_digits(1)+1}, window_size);
% First digit samples
figure;
plot(smooth_digit(1:1000),'b');
title('First 1000 samples of the first digit (from the voice code)');
xlabel('Sample Index');
ylabel('Amplitude');
grid on;
% Corresponding template samples
figure;
plot(smooth_template(1:1000),'r');
title('First 1000 samples of the corresponding template digit');
xlabel('Sample Index');
ylabel('Amplitude');
grid on;
% Reconstruct the decoded voice code using the original digit segments
reconstructed_voice_code =[];
for i =1:num_digits
reconstructed_voice_code =[reconstructed_voice_code; digit_segments{i}]; % Concatenate original voice segments
end
% Smooth the reconstructed voice code for better playback
reconstructed_voice_code_smooth = movmean(reconstructed_voice_code, window_size);
% Play the reconstructed voice code
player = audioplayer(reconstructed_voice_code_smooth, fs);
disp('Playing the reconstructed voice code...');
play(player); this was the output; Number of digits in the voice code: 5 Decoded Voice Code: 99651 Magnitudes of Inner Products: 24.801482.895215.371545.30225.280818.967020.628562.939247.515694.903533.879716.888825.413813.815656.535810.7122118.071748.489998.8843162.808842.339430.79795.115516.827511.827342.925796.405428.873431.756769.924820.601058.902029.885451.867087.8516120.488662.692963.175588.451742.269133.7335114.555546.05258.069878.58485.143134.807526.59318.871855.3149
Playing the reconstructed voice code...

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!