Attempting to extract formant frequencies of various speakers, storing them in a Matrix. Error displays "Index exceeds the number of array elements. Index must not exceed 3."

4 次查看(过去 30 天)
I am attempting to extract Formant frequencies through LPC analysis with hopes of creating a database of various speaker's formant frequencies and possibly creating a Speaker Identification program based on that database.
It is an undergraduate project. So nothing too fancy. For now I only have 4 speakers with 5 speech signals each. I hope to expand this once I get the code right!
Code:
clc;
clear;
close all;
cd('D:\project\speech_signals')
% Define the number of speakers and speech signals per speaker
num_speakers = 4;
num_speeches_per_speaker = 5;
% Cell arrays to store speech signals and sampling frequencies for each speaker
speeches = cell(num_speakers, num_speeches_per_speaker);
Fs = cell(num_speakers, num_speeches_per_speaker);
% Read speech signals for each speaker
for speaker = 1:num_speakers
for speech = 1:num_speeches_per_speaker
filename = sprintf('speaker%d_speech%d.wav', speaker, speech);
[speeches{speaker, speech}, Fs{speaker, speech}] = audioread (filename);
end
end
% Define analysis parameters
window_size = round(0.03 * Fs{1, 1}); % window size of 30 ms
hop_size = round(0.01 * Fs{1, 1}); % hop size of 10 ms
n_formants = 4; % number of formants to extract
% Initialize formant matrices for each speaker
formants = cell(num_speakers, num_speeches_per_speaker);
% Loop through speech signals for each speaker
for speaker = 1:num_speakers
for speech = 1:num_speeches_per_speaker
speech_signal = speeches{speaker, speech};
speech_length = length(speech_signal);
% Calculate the number of analysis windows
num_windows = floor((speech_length - window_size) / hop_size) + 1;
% Initialize formant matrix for the current speech signal
temp_formants = zeros(num_windows, n_formants);
% Loop through speech signal frames
for i = 1:hop_size:(speech_length - window_size + 1)
frame = speech_signal(i:i+window_size-1);
lpc_coeffs = lpc(frame, n_formants+1);
polynomial = poly(lpc_coeffs);
roots_coeffs = roots(polynomial);
roots_coeffs = roots_coeffs(abs(roots_coeffs) < 1);
angles = angle(roots_coeffs);
formant_freqs = -Fs{speaker, speech} * angles / (2 * pi);
formant_freqs = sort(formant_freqs, 'descend');
temp_formants(i:i+hop_size-1, :) = repmat(formant_freqs(1:n_formants)', hop_size, 1);
end
% Remove trailing zeros from the temporary formant matrix
temp_formants = temp_formants(1:i+hop_size-1, :);
% Store the temporary formant matrix in the main formants cell array
formants{speaker, speech} = temp_formants;
end
end
I included the code up to that point. It's standard audioread mostly, of each speech signal of each speaker.
Followed by a couple of loops for extracting the Formant Frequencies of each speaker.
However at the end of the inner loop, I attempt to save my extracted values to a cell array, and there I encounter an error.
Problematic Line : temp_formants(i:i+hop_size-1, :) = repmat(formant_freqs(1:n_formants)', hop_size, 1);
Error Display: " Index exceeds the number of array elements. Index must not exceed 3.
Error in Speaker_Identification (line 58)
temp_formants(i:i+hop_size-1, :) = repmat(formant_freqs(1:n_formants)', hop_size, 1);"
I am confused because the size of the two arrays are the same. If I alter the size of the rows of either a different error pops up telling me their dimensions are not compatible. Thus the problem is no the cell arrays size. What is it's problem with the index? It seems correct to me but I am no expert.

回答(1 个)

Manoj Mirge
Manoj Mirge 2023-8-17
This error is encountered when the array gets indexed using the index which is greater than its length. The error states that the array has size of 3 and its index cannot exceed 3.
The “temp_formants” array in the code has size of row as “floor((speech_length - window_size) / hop_size) + 1”. In the inner “for” loop you can have maximum value of variablei asspeech_length - window_size + 1” and the array’s row gets indexed using the “ivariable. The value of variable “i” is greater than the size of row of array that is why the error gets encountered.
As a workaround you may remove the colon operator of the array. Please refer to the below code for the same:
temp_formants(i:i+hop_size-1) = repmat(formant_freqs(1:n_formants)', hop_size, 1);
Hope this helps.

类别

Help CenterFile Exchange 中查找有关 Digital Filter Analysis 的更多信息

产品


版本

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by