Index exceeds the number of array elements. Index must not exceed 10.
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Working on a code to create a spectral analysis FFT test file, based on a given equation.
There are two scripts I am using to do this, one that generates a simulated test file, and another that reads it in and uses it for the rest of the program.
The first of which is:
clc; clear;
% Data File Generator
A = 5;
B = 6;
C = 7;
D = 3;
omega1 = 200 * 2 * pi;
omega2 = 400 * 2 * pi;
omega3 = 900 * 2 * pi;
dt = 1 / 10000;
t = 0:dt:1-dt;
X = A * cos(omega1 * t) + B * sin(omega2 * t) + C * cos(omega3 * t) + D;
% Saving the data to a file
dlmwrite('Simulated_Data_File.txt', X, 'delimiter', '\n');
And the second being:
% Data File Reader
% Reading the data from the file
Data_File = dlmread('Simulated_Data_File.txt');
% Splitting the data into 100 arrays of 1000 points each
Data_File = reshape(Data_File, 1000, []);
% Finding the mean of each array
mean_of_arrays = mean(Data_File, 1);
% Subtracting the mean of each array from each element
Data_File_with_Mean_Subtracted = Data_File - mean_of_arrays;
% Checking that the mean of each array is zero
if mean(mean(Data_File_with_Mean_Subtracted)) == 0
disp('The mean of each array is zero');
else
disp('The mean of each array is not zero');
end
% Finding the FFT of the elements of each array
FFT_array = fft(Data_File_with_Mean_Subtracted, [], 2);
% Find the average of the elements of the 100 arrays
average = sum(FFT_array,1) / 100;
% Take the first 500 points for single-side of the spectrum
single_side = average(1:500);
% Plot the first 500 points
figure;
plot(single_side);
xlabel('Frequency (Hz)');
ylabel('Variance of the signal');
title('Single-side of the spectrum');
% Check the variance of the data
variance = var(single_side);
fprintf('The variance of the data is: %f\n', variance);
% Check if the variance of the data is equal to the variance of the original data
original_variance = var(Data_File_with_Mean_Subtracted(:));
fprintf('The variance of the original data is: %f\n', original_variance);
if variance == original_variance
fprintf('The variance of the data is equal to the variance of the original data\n');
else
fprintf('The variance of the data is NOT equal to the variance of the original data\n');
end
% Check if the frequencies with non-zero values correspond to the frequencies used to generate the function
frequencies = [200, 400, 900];
for i = 1:length(frequencies)
if single_side(frequencies(i)) ~= 0
fprintf('The frequency %d Hz is present in the data\n', frequencies(i));
else
fprintf('The frequency %d Hz is NOT present in the data\n', frequencies(i));
end
end
However, once I reach the single_side line, I receive this error message:
Index exceeds the number of array elements. Index must not exceed 10.
Error in Read_Data_File (line 29)
single_side = average(1:500);
I am not super proficient with MATLAB, so if someone could be of assistance as to what the problem is (and an appropriate solution that would fit), I would appreciate it.
0 commentaires
Réponses (1)
Voss
le 13 Fév 2023
The size of FFT_array is 1000-by-10. Therefore, average, which is calculated by summing FFT_array along its first dimension and dividing by 100, is of size 1-by-10.
FFT_array = rand(1000,10); % random values
average = sum(FFT_array,1) / 100
Then single_side is defined as the first 500 elements of average, but obviously you can't take up to the 500th element of a vector with only 10 elements, so you get that error.
Maybe you meant to sum FFT_array along its second dimension and divide by 10 (the number of columns)? (That'd be consistent with plotting single_side and labelling the x-axis as Frequency, which you do next.)
FFT_array = rand(1000,10); % random values
average = sum(FFT_array,2) / 10
If that's the case, you can use mean instead of using sum and then dividing. (Note that in the original you're dividing by 100 instead of 1000, so that average is really 10 times the average row value of FFT_array - I'm not sure if this is intentional or a mistake.)
average = mean(FFT_array,2)
2 commentaires
Voss
le 13 Fév 2023
"Index exceeds the number of array elements. Index must not exceed 500."
This error happens because single_side is defined to be the first 500 elements of average
single_side = average(1:500);
but later you attempt to use the 900th element of single_side
frequencies = [200, 400, 900];
for i = 1:length(frequencies)
% this gives the error, when i is 3, since that's
% attempting to reference single_side(900):
if single_side(frequencies(i)) ~= 0
% ...
% ...
end
end
As for questions about the actual signal-processing task at hand, someone else would probably be better suited to answer those. You may want to start a new question thread about that specifically, since a new (and unanswered) question would be more likely to be seen by more people, and it really is a different question than, "why do I get these errors", which I've answered.
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!