Error (( vector must be the same length))
1 view (last 30 days)
Show older comments
Melika Eft
on 11 Mar 2023
Commented: Melika Eft
on 11 Mar 2023
Hello I have this code and it gives that error at plot(t,x_bp)could you please help me thank you indeed
% Generate a lowpass signal fs = 1000; % sampling frequency (Hz) t = 0:1/fs:1; % time vector (s) f1 = 10; % frequency of the signal (Hz) x = sin(2*pi*f1*t); % lowpass signal
% Perform FFT and IFFT X = fft(x); X_bp = [zeros(1,100), X(101:401), zeros(1,499)]; % create bandpass frequency domain signal x_bp = ifft(X_bp);
% Create a bandpass filter fc = [5 15]; % bandpass cutoff frequencies (Hz) order = 50; % filter order b = fir1(order, fc/(fs/2)); % FIR filter coefficients
% Apply the bandpass filter x_bp_filt = filtfilt(b, 1, x_bp);
% Plot the results subplot(2,2,1); plot(t, x); title('Lowpass Signal'); xlabel('Time (s)'); ylabel('Amplitude');
subplot(2,2,2); plot(t, x_bp); title('Bandpass Signal (Frequency Domain)'); xlabel('Time (s)'); ylabel('Amplitude');
subplot(2,2,3:4); freqz(b); title('Filter Frequency Response');
0 Comments
Accepted Answer
aakash dewangan
on 11 Mar 2023
length of t and x_bp are not same. Theymust have same length to plot the graph using plot command.
You may use maximum available points to plot by modifying your code as
% Generate a lowpass signal
fs = 1000; % sampling frequency (Hz)
t = 0:1/fs:1; % time vector (s)
f1 = 10; % frequency of the signal (Hz)
x = sin(2*pi*f1*t); % lowpass signal
% Perform FFT and IFFT
X = fft(x);
X_bp = [zeros(1,100), X(101:401), zeros(1,499)]; % create bandpass frequency domain signal
x_bp = ifft(X_bp); % Create a bandpass filter
fc = [5 15]; % bandpass cutoff frequencies (Hz)
order = 50; % filter order
b = fir1(order, fc/(fs/2)); % FIR filter coefficients
% Apply the bandpass filter
x_bp_filt = filtfilt(b, 1, x_bp);
% Plot the results
subplot(2,2,1); plot(t, x); title('Lowpass Signal'); xlabel('Time (s)'); ylabel('Amplitude');
subplot(2,2,2); plot(t(1:900), x_bp(1:900)); title('Bandpass Signal (Frequency Domain)'); xlabel('Time (s)'); ylabel('Amplitude');
subplot(2,2,3:4); freqz(b); title('Filter Frequency Response');
More Answers (0)
See Also
Categories
Find more on Filter Analysis 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!