I need help in fixing this code as I keep getting errors

7 views (last 30 days)
Danny
Danny on 19 Mar 2025 at 17:12
Answered: Fangjun Jiang on 19 Mar 2025 at 18:26
% Define filter specifications
fs = 1000; % Sampling frequency in Hz
t = 0:1/fs:1; % Time vector (1 second)
fc_low = 50; % Cutoff frequency for low-pass filter in Hz
fc_high = 100; % Cutoff frequency for high-pass filter in Hz
fc_bandpass_low = 30; % Lower cutoff for band-pass filter in Hz
fc_bandpass_high = 150; % Upper cutoff for band-pass filter in Hz
order = 4; % Filter order
% Create a mixed signal (voltage output) with multiple frequency components
signal = sin(2*pi*10*t) + sin(2*pi*200*t) + 0.5*randn(size(t)); % 10 Hz, 200 Hz signal with noise
% Apply Low-Pass Filter
Wn_low = fc_low / (fs / 2); % Normalize the cutoff frequency
[b_low, a_low] = butter(order, Wn_low, 'low');
filtered_signal_low = filter(b_low, a_low, signal);
% Apply High-Pass Filter
Wn_high = fc_high / (fs / 2); % Normalize the cutoff frequency
[b_high, a_high] = butter(order, Wn_high, 'high');
filtered_signal_high = filter(b_high, a_high, signal);
% Apply Band-Pass Filter
Wn_bandpass = [fc_bandpass_low, fc_bandpass_high] / (fs / 2); % Normalize band-pass range
[b_bandpass, a_bandpass] = butter(order, Wn_bandpass, 'bandpass');
filtered_signal_bandpass = filter(b_bandpass, a_bandpass, signal);
% Plot the original signal and the filtered signals
figure;
% Original signal plot
subplot(4, 1, 1);
plot(t, signal);
title('Original Signal (Voltage Output)');
xlabel('Time [s]');
ylabel('Voltage [V]');
% Low-pass filtered signal plot
subplot(4, 1, 2);
plot(t, filtered_signal_low);
title('Low-Pass Filtered Signal (f < 50 Hz)');
xlabel('Time [s]');
ylabel('Voltage [V]');
% High-pass filtered signal plot
subplot(4, 1, 3);
plot(t, filtered_signal_high);
title('High-Pass Filtered Signal (f > 100 Hz)');
xlabel('Time [s]');
ylabel('Voltage [V]');
% Band-pass filtered signal plot
subplot(4, 1, 4);
plot(t, filtered_signal_bandpass);
title('Band-Pass Filtered Signal (30 Hz < f < 150 Hz)');
xlabel('Time [s]');
ylabel('Voltage [V]');
% Magnitude Spectrum
figure;
% Compute and plot the magnitude spectrum (FFT) of the filtered signals
N = length(signal) % Number of points for FFT
N = 1001
f = fs * (0:(N/2)) / N % Frequency vector for plotting
f = 1×501
0 0.9990 1.9980 2.9970 3.9960 4.9950 5.9940 6.9930 7.9920 8.9910 9.9900 10.9890 11.9880 12.9870 13.9860 14.9850 15.9840 16.9830 17.9820 18.9810 19.9800 20.9790 21.9780 22.9770 23.9760 24.9750 25.9740 26.9730 27.9720 28.9710
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Original signal magnitude spectrum
subplot(4, 1, 1);
Y = fft(signal);
plot(f, abs(Y(1:N/2+1)));
Warning: Integer operands are required for colon operator when used as index.
title('Magnitude Spectrum of Original Signal');
xlabel('Frequency [Hz]');
ylabel('Magnitude');
% Low-pass filtered signal magnitude spectrum
subplot(4, 1, 2);
Y_low = fft(filtered_signal_low);
%plot(f, abs(Y_low(1:N/2+1)));
plot(f, abs(Y_low(1:floor(N/2)+1)))
title('Magnitude Spectrum of Low-Pass Filtered Signal');
xlabel('Frequency [Hz]');
ylabel('Magnitude');
% High-pass filtered signal magnitude spectrum
subplot(4, 1, 3);
Y_high = fft(filtered_signal_high);
plot(f, abs(Y_high(1:N/2+1)));
Warning: Integer operands are required for colon operator when used as index.
title('Magnitude Spectrum of High-Pass Filtered Signal');
xlabel('Frequency [Hz]');
ylabel('Magnitude');
% Band-pass filtered signal magnitude spectrum
subplot(4, 1, 4);
Y_bandpass = fft(filtered_signal_bandpass);
plot(f, abs(Y_bandpass(1:N/2+1)));
Warning: Integer operands are required for colon operator when used as index.
title('Magnitude Spectrum of Band-Pass Filtered Signal');
xlabel('Frequency [Hz]');
ylabel('Magnitude');

Answers (1)

Fangjun Jiang
Fangjun Jiang on 19 Mar 2025 at 18:26
No error but warnings.
The cause is that N/2 is not an integer because in your case, N is 1001.
It is surprise that in the example below, y(index) does not give warning but y(1:N/2+1) does, where index is equal to 1:N/2+1
But anyway, the plots show up and the warning is not critical.
N=11;
index=1:N/2+1
index = 1×6
1 2 3 4 5 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y=rand(1,20);
y(index)
ans = 1×6
0.0055 0.9275 0.7893 0.7231 0.6331 0.9171
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure
plot(y(index))
figure
plot(y(1:N/2+1))
Warning: Integer operands are required for colon operator when used as index.

Categories

Find more on Signal Processing Toolbox 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!