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 次查看(过去 30 天)
%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

采纳的回答

Voss
Voss 2021-12-19
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 个)

类别

Help CenterFile Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by