Index exceeds matrix dimensions

5 Ansichten (letzte 30 Tage)
lakshmi priya
lakshmi priya am 27 Feb. 2019
Bearbeitet: KSSV am 27 Feb. 2019
% MatLab Code:
% Author: Minh Anh Nguyen (minhanhnguyen@q.com)
% Expects a .wav soundfile as input.
% usage: Soundfunction('myfile.wav')
% This function will read in two wav files, perform fft, and compare these
files. it
% will display on the screen whether or not the file are identifcal.
% this function is also perfom autocorrelation
% This beginning part just defines the function to be used in MatLab: takes a
% "wav" sound file as an input, and spits out a graph.
function [Soundfunction] = Soundfunction( file1, file2 );
clf % Clears the graphic screen.
close all;% Close all figures (except those of imtool.)
clc;% Clear the command window.
fontSize = 20;
fontSize1 = 14;
% Reads in the sound files, into a big array called y1 and y2.
%y = wavread( file );
[y1, fs1]= audioread( file1 );
[y2, fs2]= audioread( file2 );
% Normalize y1; that is, scale all values to its maximum. Note how simple it
is
% to do this in MatLab.
yn1 = y1/max(abs(y1));
yn2 = y1/max(abs(y1));
sound1 = y1;
sound2 = y2;
%% Do not modify here
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% time of sound
N = length(sound1); % number of points to analyze
ls = size (sound1); % find the length of the data per second
nbits = 2^(length(sound1));
t1 = (0:1:length(sound1)-1)/fs1; %% time1
fx = fs1*(0:N/2-1)/N; %Prepare freq data for plot
T1 = 1/fs1 % period between each sample
N2 = length(sound2);
ls2 = size (sound2); % find the length of the data per second
t2 = (0:1:length(sound2)-1)/fs2;
fx2 = fs2*(0:N2/2-1)/N2; %Prepare freq data for plot
T2 = 1/fs2 % period between each sample
%%%%% cut signal for comparing
% cut signal to same length
voice1 = y1(fs1*1 : fs1*9);
voice2 = y2(fs2*1 : fs2*9);
%% find new length
N2x = length(voice2);
N1x = length(voice1);
% find new time
t1x = (0:1:length(voice1)-1)/fs1;
t2x = (0:1:length(voice2)-1)/fs2;
%% find new frequency
f2x = fs2*(0:N2x/2-1)/N2x;
f1x = fs1*(0:N1x/2-1)/N1x;
%% fft of cut signal
NFFT1x = 2 ^ nextpow2(N1x);
Y1x = fft(voice1, NFFT1x)/ N1x;
f1xx = (fs1/ 2 * linspace(0, 1, NFFT1x / 2+1))'; % Vector containing
frequencies in Hz
STFFT1x = ( 2 * abs(Y1x(1: NFFT1x / 2+1))); % Vector containing corresponding
amplitudes
NFFT2x = 2 ^ nextpow2(N2x);
Y2x = fft(voice2, NFFT2x) / N2x;
f2xx = (fs2 / 2 * linspace(0, 1, NFFT2x / 2+1))'; % Vector containing
frequencies in Hz
STFFT2x = ( 2 * abs(Y2x(1: NFFT2x / 2+1))); % Vector containing corresponding
amplitudes
%% plot for the cut signal
%% plot for the cut signal
figure; subplot (3,2,1); plot(t1x, voice1);
str1=sprintf('Plot Sound1 with sampling rate = %d Hz and number sample = %d',
fs1, N1x);
title(str1);
xlabel('time (sec)'); ylabel('relative signal strength'); grid on;
subplot (3,2,2); plot(t2x, voice2);
str2=sprintf('Plot Sound2 with sampling rate = %d Hz and number sample = %d',
fs2, N2x);
title(str2); xlabel('time (sec)','Fontsize', fontSize); ylabel('relative signal strength','Fontsize', fontSize); grid on;
%% fft of cut signal
subplot (3,2,3); plot(f1xx, STFFT1x); title ('Single-sided Power spectrum for Sound1 with same length','Fontsize', fontSize1);
ylabel ('Magnitude [dB]','Fontsize', fontSize); xlabel ('frequency [Hz]','Fontsize', fontSize); grid on;
subplot (3,2,4); plot(f2xx, STFFT2x); title ('Single-sided Power spectrum for Sound2 with same length','Fontsize', fontSize1);
ylabel ('Magnitude [dB]','Fontsize', fontSize); xlabel ('frequency [Hz]','Fontsize', fontSize); grid on;
%% corlation
[C1, lag1] = xcorr(abs((fft(voice1))),abs((fft(voice2)) ));
figure, plot(lag1/fs1,C1);
ylabel('Amplitude'); grid on
title('Cross-correlation between Sound1 and sound2 files')
%% calculate mean
% Calculate the MSE
D= voice1 - voice2;
MSE=mean(D.^2);
%msgbox(strcat('MSE value is= ',mat2str(),' MSE'));
msgbox(strcat('MSE value is= ',mat2str(MSE)));
if MSE ==0;
msgbox('no error. Signal are the same');
disp('Signal are the same');
else
disp('Signal are not the same');
msgbox('Test error. Please check your set up');
end
  1 Kommentar
Walter Roberson
Walter Roberson am 27 Feb. 2019
Do we get a hint as to which line the problem is happening on, or what the sizes of the files are?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

KSSV
KSSV am 27 Feb. 2019
Bearbeitet: KSSV am 27 Feb. 2019
This error happens when you try to extract more number of elements from array that present in the array.
A = rand(10,1) ;
A(1) % no error
A(10) % no error
A(11) % error
In the above A has ten elements, if you extract elements more than 10 by index...it throws error. The same thing is happeing in your code. Try to run your code in debug mode and check the dimensions of the arrays at the line where error is occuring.

Kategorien

Mehr zu Measurements and Spatial Audio finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by