DSPZ6
DSPZ6
FIGURE
ZUBAIR KHAN 02-133222-048 BEE 6-B
x---------------x---------------x
QUESTION NO. 02
MATLAB CODE
% Sampling frequency and time vector
Fs = 1000;
n = 0:1023;
t = n/Fs;
% Generate three sinusoids: 10 Hz, 80 Hz, 130 Hz
x1 = sin(2*pi*10*t);
x2 = sin(2*pi*80*t);
x3 = sin(2*pi*130*t);
% Combined input signal
x = x1 + x2 + x3;
% Design Notch filter to remove 80 Hz
w = 2*pi*80/Fs;
b = [1 -2*cos(w) 1];
a = [1 -0.9*2*cos(w) 0.81];
% Apply normal filter (with phase delay)
y_filter = filter(b,a,x);
% Apply filtfilt (zero-phase filtering)
y_filtfilt = filtfilt(b,a,x);
ZUBAIR KHAN 02-133222-048 BEE 6-B
% --- Time Domain Comparison ---
figure;
subplot(3,1,1);
plot(t,x);
title('Input Signal (Time Domain)');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid on;
subplot(3,1,2);
plot(t,y_filter);
title('Output using filter() (Time Domain)');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid on;
subplot(3,1,3);
plot(t,y_filtfilt);
title('Output using filtfilt() (Time Domain)');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid on;
% --- Frequency Domain Comparison ---
NFFT = 1024;
f = (-NFFT/2:NFFT/2-1)*(Fs/NFFT);
X = fftshift(fft(x,NFFT));
Y_filter = fftshift(fft(y_filter,NFFT));
Y_filtfilt = fftshift(fft(y_filtfilt,NFFT));
figure;
subplot(3,1,1);
plot(f, abs(X));
title('Input Signal Spectrum (Frequency Domain)');
xlabel('Frequency (Hz)');
ylabel('|X(f)|');
grid on;
subplot(3,1,2);
plot(f, abs(Y_filter));
title('Filtered Signal Spectrum using filter()');
xlabel('Frequency (Hz)');
ylabel('|Y(f)|');
grid on;
ZUBAIR KHAN 02-133222-048 BEE 6-B
subplot(3,1,3);
plot(f, abs(Y_filtfilt));
title('Filtered Signal Spectrum using filtfilt()');
xlabel('Frequency (Hz)');
ylabel('|Y(f)|');
grid on;
FIGURE