I need help in fixing this code as I keep getting errors
7 views (last 30 days)
Show older comments
% 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
f = fs * (0:(N/2)) / N % Frequency vector for plotting
% Original signal magnitude spectrum
subplot(4, 1, 1);
Y = fft(signal);
plot(f, abs(Y(1:N/2+1)));
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)));
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)));
title('Magnitude Spectrum of Band-Pass Filtered Signal');
xlabel('Frequency [Hz]');
ylabel('Magnitude');
0 Comments
Answers (1)
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
y=rand(1,20);
y(index)
figure
plot(y(index))
figure
plot(y(1:N/2+1))
0 Comments
See Also
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!