Index in position 2 is invalid. Array indices must be positive integers or logical values.

3 ビュー (過去 30 日間)
Leonidas Daedalus
Leonidas Daedalus 2018 年 12 月 13 日
回答済み: jibrahim 2020 年 1 月 9 日
I'm trying this code from matlab with the given dataset and it works fine. But when I added my own dataset it gives me an error.
My table is 3x2
Label Count
_________ _____
emergency 46
neutral 46
unknown 12
Index in position 2 is invalid. Array indices must be positive integers or logical values.
Error in speechSpectrograms (line 38)
X(:,ind,1,i) = spec;
MAIN CODE
%% Load Speech Commands Data Set
datafolder = fullfile('D:\Users\Hannah\Desktop\dataset');
ads = audioDatastore(datafolder, ...
'IncludeSubfolders',true, ...
'FileExtensions','.wav', ...
'LabelSource','foldernames')
ads0 = copy(ads);
%% Choose Words to Recognize
commands = categorical(["emergency","neutral"]);
isCommand = ismember(ads.Labels,commands);
isUnknown = ~ismember(ads.Labels,[commands,"_background_noise_"]);
includeFraction = 0.2;
mask = rand(numel(ads.Labels),1) < includeFraction;
isUnknown = isUnknown & mask;
ads.Labels(isUnknown) = categorical("unknown");
ads = subset(ads,isCommand|isUnknown);
countEachLabel(ads)
%% Split Data into Training, Validation, and Test Sets
[adsTrain,adsValidation,adsTest] = splitData(ads,datafolder);
%% Compute Speech Spectrograms
segmentDuration = 1;
frameDuration = 0.025;
hopDuration = 0.010;
numBands = 40;
% Compute the spectrograms for the training, validation, and test sets
epsil = 1e-6;
XTrain = speechSpectrograms(adsTrain,segmentDuration,frameDuration,hopDuration,numBands);
XTrain = log10(XTrain + epsil);
XValidation = speechSpectrograms(adsValidation,segmentDuration,frameDuration,hopDuration,numBands);
XValidation = log10(XValidation + epsil);
XTest = speechSpectrograms(adsTest,segmentDuration,frameDuration,hopDuration,numBands);
XTest = log10(XTest + epsil);
YTrain = adsTrain.Labels;
YValidation = adsValidation.Labels;
YTest = adsTest.Labels;
speechSpectogram code
% speechSpectrograms(ads,segmentDuration,frameDuration,hopDuration,numBands)
% computes speech spectrograms for the files in the datastore ads.
% segmentDuration is the total duration of the speech clips (in seconds),
% frameDuration the duration of each spectrogram frame, hopDuration the
% time shift between each spectrogram frame, and numBands the number of
% frequency bands.
function X = speechSpectrograms(ads,segmentDuration,frameDuration,hopDuration,numBands)
disp("Computing speech spectrograms...");
numHops = ceil((segmentDuration - frameDuration)/hopDuration);
numFiles = length(ads.Files);
X = zeros([numBands,numHops,1,numFiles],'single');
for i = 1:numFiles
[x,info] = read(ads);
fs = info.SampleRate;
frameLength = round(frameDuration*fs);
hopLength = round(hopDuration*fs);
spec = auditorySpectrogram(x,fs, ...
'WindowLength',frameLength, ...
'OverlapLength',frameLength - hopLength, ...
'NumBands',numBands, ...
'Range',[50,7000], ...
'WindowType','Hann', ...
'WarpType','Bark', ...
'SumExponent',2);
% If the spectrogram is less wide than numHops, then put spectrogram in
% the middle of X.
w = size(spec,2);
left = floor((numHops-w)/2)+1;
ind = left:left+w-1;
X(:,ind,1,i) = spec;
if mod(i,46) == 0
disp("Processed " + i + " files out of " + numFiles)
end
end
disp("...done");
end
  1 件のコメント
dpb
dpb 2018 年 12 月 13 日
Set a breakpoint at the beginning sequence of the code that calculates ind and step through and see where your logic fails (or you get an unexpected result for one or more of the intermediates).
What does ind contain before the offending statement when it fails? Whatever it is, it isn't a valid suscripting expression by the error message but we have no way on earth to be able to tell what it is going to be just from the code itself.

サインインしてコメントする。

回答 (2 件)

abu
abu 2019 年 2 月 7 日
Comment following code in SpeechSpectrogrmas.m
% If the spectrogram is less wide than numHops, then put spectrogram in
% the middle of X.
w = size(spec,2);
left = floor((numHops-w)/2)+1
ind = left:left+w-1
X(:,ind,1,i) = spec;
The code breaks when numHops exceeds spectrogram width. Missing an 'if' condition here.
  1 件のコメント
asd asd
asd asd 2020 年 1 月 4 日
What mean: "if" condition? Can you write a code? I dont know how use condition if in this example.

サインインしてコメントする。


jibrahim
jibrahim 2020 年 1 月 9 日
Hi Leonidas,
I suspect the function speechSpectrograms cannot handle the case where the variable x is longer than segmentDuration. You will see this error if some of your audio signals are longer than the specified segmentDuration (in your case, one second). Either make segmentDuration longer (if your signals are indeed longer than 1 second), or adjust the length of x in speechSpectrograms, for example like this:
[x,info] = read(ads);
if numel(x) > segmentDuration
x = x(1:segmentDuration,:);
end
The right thing to do depends on how long your signals are, and where the useful information resides in your signals.

カテゴリ

Help Center および File ExchangeAI for Audio についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by