Warning: Integer operands are required for colon operator when used as index

조회 수: 9 (최근 30일)
I am using fft functions and have twice in my code this warning message, bu no clue why I get the warning:
N = 2048*16;
sizeData = size(data,2);
padding = (N-sizeData)/2;
cInput = zeros([1 N]); % <-- Warning
cInput(padding+1:(padding+sizeData)) = data;
cInput = fftshift(cInput);
% transform
cfourier = fftshift(fft(cInput));
% iFFt
cOutput = ifft(ifftshift(cfourier));
cOutput = ifftshift(cOutput); % <-- Warning
AmpIFourier = abs(cOutput(padding+1:(padding+sizeData)));
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 12월 8일
Haldun Hannis posted a question in the Answer section but deleted it while I was composing the reply. That reply follows:
(NFFT was constructed with 2^nextpow2 so is a power of 2)
WINDOW = hamming(NFFT/8+1); % Windowing function
NOVERLAP=length(WINDOW)*0.5;
provided that the data is not very small, NFFT will be exactly divisible by 8 so NFFT/8 would be an even integer unless NFFT is 8 or less (not impossible, should be tested for.) With NFFT/8 being even, NFFT/8+1 will be odd. So you are asking for an odd-size hamming filter to be created.
You then take the length of that, which will be odd. And you multiply by 0.5. With it being odd, you will not get an integer result.
You then pass that non-integer value as the overlap size, into a function that uses it something vaguely like
data(start:start+overlap-1)
and that is a problem because overlap is not an integer
You should be using floor() or ceil() to force integer

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

채택된 답변

Image Analyst
Image Analyst 2012년 2월 20일
(N-sizeData)/2 can be a fractional number, e.g. "something point 5" so try
padding = floor((N-sizeData)/2);
or better yet, just use the padarray() function to do your padding.
  댓글 수: 5
Jan
Jan 2012년 2월 21일
@Matthias: I've never seen a bug in Matlab, which leads to a wrong line number for warnings. While I assume IA and Oleg point to the correct solution, there must be an additional problem.
Hannetjie
Hannetjie 2015년 3월 6일
@Jan Simon: It is now 3 years later so my apologies if this has since been touched upon in newer posts... But I came across this same anomaly (and this is the first and only time I've seen it). When running my code through, I get
Warning: Integer operands are required for colon operator when used as index > In tdnoise3 at 73
The warning in my case makes no sense at line nr 73. However, if I step through the exact same code, I get
Warning: Integer operands are required for colon operator when used as index > In tdnoise3 at 81
Which does make sense and was where the fix was required.
But OK, has no bearing on the topic of this post.

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

추가 답변 (2개)

Oleg Komarov
Oleg Komarov 2012년 2월 20일
Perfectly fine
idx = 2.5:9;
But when you use to index a position:
A = rand(10,1);
A(idx)
There's no such a thing as position number 2.5

Jan
Jan 2012년 2월 20일
N = 2048*16;
cInput = zeros([1 N]); % <-- Warning
No, I do not get a warning in this code. Are you sure, that this is the original code?

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by