Error (( vector must be the same length))
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Melika Eft
le 11 Mar 2023
Commenté : Melika Eft
le 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 commentaires
Réponse acceptée
aakash dewangan
le 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');
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Filter Analysis dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!