Index exceeds the number of array elements. Index must not exceed 10.

7 次查看(过去 30 天)
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.

回答(1 个)

Voss
Voss 2023-2-13
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
average = 1×10
5.0296 5.0690 5.0601 4.8939 5.0951 4.8645 4.9973 5.0340 4.8509 5.0330
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
average = 1000×1
0.5162 0.5691 0.4062 0.5127 0.4855 0.4890 0.3378 0.6189 0.4772 0.6183
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)
average = 1000×1
0.5162 0.5691 0.4062 0.5127 0.4855 0.4890 0.3378 0.6189 0.4772 0.6183
  2 个评论
Brandon
Brandon 2023-2-13
If I replace the average function you described as I did here, while keeping the FFT_array as it was (due to having a "Data_File_With_Mean_Subtracted" defined and utilized), I can get the code to go through furthur (it goes through the for loop twice), but then it comes across this error, being:
Index exceeds the number of array elements. Index must not exceed 500.
Error in test (line 74)
if single_side(frequencies(i)) ~= 0
The other issue that then arises, is that it generates a bit of the plot (the second image), though it is supposed to more closely resemble the first image (titled Test File), I believe, as "this figure shows the plot of the FFT (variance vs frequency). You can see the values for the variance for the 200, 400, and 900 Hz frequencies."
(If needed, I can contact you privately to give a more in-depth description of what the actual overall assignment is requiring, to perhaps give a better idea of what is necessary / what I am doing wrong? If not that is also fine.)
% 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 = mean(FFT_array,2);
% 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
Voss
Voss 2023-2-13
"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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by