Index in position 1 is invalid. Array indices must be positive integers or logical values.
조회 수: 57 (최근 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
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
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.
Evelyn Cooper
2020년 2월 13일
Thank you so much! This helped me figure out/debug why tf wasn't working for me.
추가 답변 (1개)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!