Index in position 1 is invalid. Array indices must be positive integers or logical values.

61 Ansichten (letzte 30 Tage)
I am a python programmer and new to Matlab. Therefore, facing some basic issues. Please help me out with this. I am pasting my code here.
Error Message:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in spectrum_uncompressed (line 30)
X_per_bartlett = Px(x_sig_with_noise_updated, 1);
load('lowpasssignal.mat')
%check for mean
mn = dsp.Mean;
x_sig_with_noise_mean = mn(x_sig_and_noise);
%new signal definition with mean
x_sig_with_noise_updated = x_sig_and_noise - x_sig_with_noise_mean;
%Periodogram on x to compute PSD
N = length(x_sig_with_noise_updated);
X_per = periodo(x_sig_with_noise_updated, N);
fs = 2*pi; %sampling of 2*pi becuase signal is complex valued
freq_0 = 0:fs/length(x_sig_with_noise_updated):fs - fs/N;
figure()
plot(freq_0/pi, X_per)
hold on
grid on
title('Periodogram Using FFT')
xlabel('Normalized Frequency (\times\pi rad/sample)')
ylabel('Power/Frequency')
%Bartlett's method
X_per_bartlett = Px(x_sig_with_noise_updated, 1, 1, N);
freq_1 = 0:fs/length(X_per_bartlett):fs;
freq_1_updated = freq_1(1:length(freq_1)-1);
plot(freq_1_updated/pi, X_per_bartlett)
hold on
grid on
title('Periodogram Using Bartlett Avg')
xlabel('Normalized Frequency (\times\pi rad/sample)')
ylabel('Power/Frequency')
The functions are here:
%periodo
function perx = periodo(x, N)
x_fft = fft(x);
perx = (1/(2 * pi * N)) * abs(x_fft) .^ 2
end
%Bartlett
function per_x = Px(x, win, n1, n2)
x = x(:);
if nargin == 2
n1 = 1; n2 = length(x);
display(n1, n2);
end;
N = n2 - n1 + 1
w = ones(N, 1);
if (win == 1) w = bartlett(N);
elseif (win == 2) w = hamming(N);
end;
xw = x(n1:n2).*w/norm(w);
per_x = N * periodogram(xw);
  5 Kommentare

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 13 Nov. 2018
somehow you have aa variable in your workspace that is named Px so matlab thinks you are indexing instead of calling a function .
  3 Kommentare
Walter Roberson
Walter Roberson am 13 Nov. 2018
Bearbeitet: Walter Roberson am 13 Nov. 2018
Your code is not a function, so if you were to do
Px = 'hello';
before running the code then Px would be considered to be a variable instead of a function. You might have assigned something to Px in an earlier version of the code and not cleared your variables since then. Using functions instead of scripts protects against this kind of accidental use of left-over variables.
Also you are using load() without assigning the output to a variable. If you happened to have a variable named Px in the .mat file, the stored value would become active. It is recommended that you assign load() to a variable and access the structure, something like
filestruct = load('lowpasssignal.mat');
dsp = filestruct.dsp; %pull out the specific variable you need from the structure.
Evelyn Cooper
Evelyn Cooper am 13 Feb. 2020
Thank you so much! This helped me figure out/debug why tf wasn't working for me.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

madhan ravi
madhan ravi am 13 Nov. 2018
Bearbeitet: madhan ravi am 13 Nov. 2018
x=1:5 %an example to make you understand
x(1)
x(0) %this will throw the same error as you have

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by