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

Voss
Voss 2024년 4월 25일
Please upload 'Lab7data1.txt' using the paperclip button.
Kiet Ho
Kiet Ho 2024년 4월 25일
이동: Dyuman Joshi 2024년 4월 25일
Here is the data. Thanks.
Voss
Voss 2024년 4월 25일
It's better to use size rather than length.

댓글을 달려면 로그인하십시오.

 채택된 답변

Voss
Voss 2024년 4월 25일
편집: Voss 2024년 4월 25일
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

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 2024년 4월 25일
It works. Thank you so much for your help.
Voss
Voss 2024년 4월 25일
편집: Voss 2024년 4월 25일
You're welcome! Any questions, let me know.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

질문:

2024년 4월 25일

편집:

2024년 4월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by