INDEX EXCEEDS THE NUMBER OF ARRAY ELEMENTS.

% Load the signal data from the text file using the load command with the -ascii option
x = load('Lab7data1.txt', '-ascii');
% Plot the signal
%figure;
%plot(x);
%xlabel('Sample');
%ylabel('Amplitude');
%title('Original Signal');
%grid on;
% Calculate the length of the signal in samples
signal_length = length(x);
num_periods = 100;
% Estimate the number of samples per period
samples_per_period = signal_length / num_periods;
% Check Nyquist condition
if samples_per_period >= 2
disp('Nyquist sampling rate is satisfied.');
else
disp('Nyquist sampling rate is not satisfied.');
end
% Calculate the current sampling rate
current_sampling_rate = 1 / samples_per_period;
% Estimate the down-sampling factor needed to reduce the sampling rate to
% the Nyquist
downsampling_factor = ceil(current_sampling_rate / 2);
% Down-sample the signal
downsampled_signal = x(1:downsampling_factor:end);
% Define the x-values for the down-sampled signal
downsampled_indices = 1:downsampling_factor:signal_length;
% Make sure the lengths match
if length(downsampled_indices) ~= length(downsampled_signal)
downsampled_indices = downsampled_indices(1:length(downsampled_signal));
end
% Interpolate the down-sampled signal to up-sample it
up_sampled_signal = interp1(downsampled_indices, downsampled_signal, 1:signal_length,'linear');
% Plot both the original and down-sampled signals
figure;
plot(1:signal_length, x, 'b', 'DisplayName', 'Original Signal');
hold on;
plot(downsampled_signal, 'ro', 'DisplayName', 'Down-sampled Signal');
plot(up_sampled_signal, 'g', 'DisplayName', 'Up-sampled Signal (Linear Interpolation)');
hold off;
xlabel('Sample');
ylabel('Amplitude');
title('Original vs Down-sampled Signal vs Up-sampled Signal');
legend('Location', 'best');
grid on;

3 commentaires

Voss
Voss le 25 Avr 2024
Please upload 'Lab7data1.txt' using the paperclip button.
Kiet Ho
Kiet Ho le 25 Avr 2024
Déplacé(e) : Dyuman Joshi le 25 Avr 2024
Here is the data. Thanks.
Voss
Voss le 25 Avr 2024
It's better to use size rather than length.

Connectez-vous pour commenter.

 Réponse acceptée

Voss
Voss le 25 Avr 2024
Modifié(e) : Voss le 25 Avr 2024
My guess is that x is a matrix but your code is assuming it's a vector.
Example:
x = rand(2,232);
signal_length = length(x);
num_periods = 100;
% Estimate the number of samples per period
samples_per_period = signal_length / num_periods;
% Check Nyquist condition
if samples_per_period >= 2
disp('Nyquist sampling rate is satisfied.');
else
disp('Nyquist sampling rate is not satisfied.');
end
Nyquist sampling rate is satisfied.
% Calculate the current sampling rate
current_sampling_rate = 1 / samples_per_period;
% Estimate the down-sampling factor needed to reduce the sampling rate to
% the Nyquist
downsampling_factor = ceil(current_sampling_rate / 2);
% Down-sample the signal
downsampled_signal = x(1:downsampling_factor:end);
% Define the x-values for the down-sampled signal
downsampled_indices = 1:downsampling_factor:signal_length;
% Make sure the lengths match
if length(downsampled_indices) ~= length(downsampled_signal)
whos downsampled_indices downsampled_signal
disp('error happens here')
downsampled_indices = downsampled_indices(1:length(downsampled_signal))
end
Name Size Bytes Class Attributes downsampled_indices 1x232 1856 double downsampled_signal 1x464 3712 double
error happens here
Index exceeds the number of array elements. Index must not exceed 232.

3 commentaires

Thank you for the data file.
x is a matrix:
x = load('Lab7data1.txt', '-ascii')
x = 8000x2
-0.5000 0.0807 -0.4999 0.0413 -0.4998 0.0020 -0.4996 -0.0370 -0.4995 -0.0767 -0.4994 -0.1157 -0.4993 -0.1547 -0.4991 -0.1937 -0.4990 -0.2321 -0.4989 -0.2704
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
which means that this line
% Down-sample the signal
downsampled_signal = x(1:downsampling_factor:end);
should be
% Down-sample the signal
downsampled_signal = x(1:downsampling_factor:end,:);
See the difference below.
signal_length = length(x);
num_periods = 100;
% Estimate the number of samples per period
samples_per_period = signal_length / num_periods;
% Check Nyquist condition
if samples_per_period >= 2
disp('Nyquist sampling rate is satisfied.');
else
disp('Nyquist sampling rate is not satisfied.');
end
Nyquist sampling rate is satisfied.
% Calculate the current sampling rate
current_sampling_rate = 1 / samples_per_period;
% Estimate the down-sampling factor needed to reduce the sampling rate to
% the Nyquist
downsampling_factor = ceil(current_sampling_rate / 2);
% Down-sample the signal
downsampled_signal = x(1:downsampling_factor:end)
downsampled_signal = 1x16000
-0.5000 -0.4999 -0.4998 -0.4996 -0.4995 -0.4994 -0.4993 -0.4991 -0.4990 -0.4989 -0.4988 -0.4986 -0.4985 -0.4984 -0.4983 -0.4981 -0.4980 -0.4979 -0.4978 -0.4976 -0.4975 -0.4974 -0.4973 -0.4971 -0.4970 -0.4969 -0.4968 -0.4966 -0.4965 -0.4964
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
downsampled_signal = x(1:downsampling_factor:end,:)
downsampled_signal = 8000x2
-0.5000 0.0807 -0.4999 0.0413 -0.4998 0.0020 -0.4996 -0.0370 -0.4995 -0.0767 -0.4994 -0.1157 -0.4993 -0.1547 -0.4991 -0.1937 -0.4990 -0.2321 -0.4989 -0.2704
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Define the x-values for the down-sampled signal
downsampled_indices = 1:downsampling_factor:signal_length;
% Make sure the lengths match
if length(downsampled_indices) ~= length(downsampled_signal)
downsampled_indices = downsampled_indices(1:length(downsampled_signal))
end
% Interpolate the down-sampled signal to up-sample it
up_sampled_signal = interp1(downsampled_indices, downsampled_signal, 1:signal_length,'linear');
% Plot both the original and down-sampled signals
figure;
plot(1:signal_length, x, 'b', 'DisplayName', 'Original Signal');
hold on;
plot(downsampled_signal, 'ro', 'DisplayName', 'Down-sampled Signal');
plot(up_sampled_signal, 'g', 'DisplayName', 'Up-sampled Signal (Linear Interpolation)');
hold off;
xlabel('Sample');
ylabel('Amplitude');
title('Original vs Down-sampled Signal vs Up-sampled Signal');
legend('Location', 'best');
grid on;
Kiet Ho
Kiet Ho le 25 Avr 2024
It works. Thank you so much for your help.
Voss
Voss le 25 Avr 2024
Modifié(e) : Voss le 25 Avr 2024
You're welcome! Any questions, let me know.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by