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

55 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2018-11-13
somehow you have aa variable in your workspace that is named Px so matlab thinks you are indexing instead of calling a function .
  3 个评论
Walter Roberson
Walter Roberson 2018-11-13
编辑:Walter Roberson 2018-11-13
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.

请先登录,再进行评论。

更多回答(1 个)

madhan ravi
madhan ravi 2018-11-13
编辑:madhan ravi 2018-11-13
x=1:5 %an example to make you understand
x(1)
x(0) %this will throw the same error as you have

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by