Error (( vector must be the same length))

4 visualizaciones (últimos 30 días)
Melika Eft
Melika Eft el 11 de Mzo. de 2023
Comentada: Melika Eft el 11 de Mzo. de 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');

Respuesta aceptada

aakash dewangan
aakash dewangan el 11 de Mzo. de 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');
  1 comentario
Melika Eft
Melika Eft el 11 de Mzo. de 2023
Thanks for your attention it was very helpful

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Frequency Transformations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by