1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Kiet Ho am 25 Apr. 2024
Bearbeitet: Voss am 25 Apr. 2024
Akzeptierte Antwort: Voss
In MATLAB Online öffnen
% 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 Kommentare 1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Voss am 25 Apr. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2111716-index-exceeds-the-number-of-array-elements#comment_3143576
Please upload 'Lab7data1.txt' using the paperclip button.
Kiet Ho am 25 Apr. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2111716-index-exceeds-the-number-of-array-elements#comment_3143651
Verschoben: Dyuman Joshi am 25 Apr. 2024
- Lab7data1.txt
Here is the data. Thanks.
Voss am 25 Apr. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2111716-index-exceeds-the-number-of-array-elements#comment_3143681
It's better to use size rather than length.
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Akzeptierte Antwort
Voss am 25 Apr. 2024
Bearbeitet: Voss am 25 Apr. 2024
In MATLAB Online öffnen
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 Kommentare 1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Voss am 25 Apr. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2111716-index-exceeds-the-number-of-array-elements#comment_3143676
In MATLAB Online öffnen
- Lab7data1.txt
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 am 25 Apr. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2111716-index-exceeds-the-number-of-array-elements#comment_3143876
It works. Thank you so much for your help.
Voss am 25 Apr. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2111716-index-exceeds-the-number-of-array-elements#comment_3143881
Bearbeitet: Voss am 25 Apr. 2024
You're welcome! Any questions, let me know.
Melden Sie sich an, um zu kommentieren.
Weitere Antworten (0)
Melden Sie sich an, um diese Frage zu beantworten.
Siehe auch
Kategorien
Signal ProcessingSignal Processing ToolboxDigital and Analog FiltersMultirate Signal Processing
Mehr zu Multirate Signal Processing finden Sie in Help Center und File Exchange
Tags
- array element
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Es ist ein Fehler aufgetreten
Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asien-Pazifik
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Kontakt zu Ihrer lokalen Niederlassung