Error (( vector must be the same length))

조회 수: 1 (최근 30일)
Melika Eft
Melika Eft 2023년 3월 11일
댓글: Melika Eft 2023년 3월 11일
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');

채택된 답변

aakash dewangan
aakash dewangan 2023년 3월 11일
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');
  댓글 수: 1
Melika Eft
Melika Eft 2023년 3월 11일
Thanks for your attention it was very helpful

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Filter Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by