I'm not sure how to fix this error Warning: Integer operands are required for colon operator when used as index. > In demo (line 147)
2 views (last 30 days)
Show older comments
%convolution
y1 = conv(x,h1)*Ts; %multiply by interval Ts to get integral
Nsamps = length(y1); %Nsamps is the length of array y1
t = (Ts)*(1:Nsamps); %Prepare time data for plot
figure
plot(t,y1);
xlim([1.5 1.51]); %only plot t within xlim
xlabel('Time (s)');
ylabel('Amplitude');
title('Convolution with 200Hz Cutoff');
%Do Fourier Transform of the 200Hz Cutoff signal
y_fft = abs(fft(y1 * 5)/Fs); %Normalize the signal & Retain Magnitude
y_fft = (y_fft(1:Nsamps/2)); %Discard Half of Points
f = Fs*(0:Nsamps/2-1)/Nsamps; %Prepare freq data for plot
Accepted Answer
Voss
on 19 Dec 2021
This is probably due to the fact that Nsamps is odd, so that Nsamps/2 is not an integer, so that this line:
y_fft = (y_fft(1:Nsamps/2)); %Discard Half of Points
produces the warning. It's just a warning (not an error), so you don't need to 'fix' it, but you can avoid it by only using integers with the colon operator when used as indexes. For instance:
if rem(Nsamps,2) == 1
y_fft = y_fft(1:(Nsamps-1)/2);
else
y_fft = y_fft(1:Nsamps/2);
end
or, equivalently:
y_fft = y_fft(1:(Nsamps-rem(Nsamps,2))/2);
0 Comments
More Answers (0)
See Also
Categories
Find more on Fourier Analysis and Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!