# How to apply a filter to a signal?

114 views (last 30 days)
KibreabG on 17 Nov 2021
Commented: Star Strider on 19 Nov 2021
I have an input signal. I want to filter out the contents of that input at frequencies 60, 120, and 180Hz (there are unwanted interferences at those frequencies). To achieve this, I created zeros and poles at (what I understand to be) locations on the pole-zero plot that would filter the input signal. I am wondering if I have done that part correctly and how I can filter the input signal. Using the subplot command, I want to plot the original signal on the top panel, and the filtered signal on the bottom panel. The following is the code so far, but I either get errors or unexpected results depending on what I do to the number of poles on my transfer function.
k = 1;
z = [cos(pi/3)+sin(pi/3)*1i;
cos(2*pi/3)+sin(2*pi/3)*1i;
-1];
p = ;%if I change this to "p = [1; -2; 1];", I would no longer get an error but an unexpected result instead
[b,a] = zp2tf(z,p,k);
[h,t1] = impz(b,a);
zplane(z,p);
pause
% create system
myFilter = tf(b,a);
% apply filter to time domain signal
[y_out, time] = lsim(myFilter,y,t);%y is the input signal
% plot for check
plot(time,[y; y_out']);

Star Strider on 17 Nov 2021
I do not understand the reason that the Control System Toolbox is being used for signal processing.
I would instead use the Signal Processing Toolbox bandstop function, with the 'ImpulseResponse','iir' name-value pair to design an efficient elliptical bandstop filter. It will be necessary to design three different filters, and run them in series.
Another option is to use a FIR filter to reject all of them at once. That would go something like this —
Fs = 1000; % Use Correct Sampling Frequency (Must Be Greater Than 370 Hz)
fcomb = [[55 59 61 64], [55 59 61 64]+60, [55 59 61 64]+120];
mags = [[1 0 1], [0 1], [0 1]];
dev = [[0.5 0.1 0.5], [0.1 0.5], [0.1 0.5]];
[n,Wn,beta,ftype] = kaiserord(fcomb,mags,dev,Fs);
hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'noscale');
figure
freqz(hh, 1, 2^20, Fs)
set(subplot(2,1,1), 'XLim', [0 200]) % Zoom X-Axis
set(subplot(2,1,2), 'XLim', [0 200]) % Zoom X-Axis This filter has a length of 281, so the signal length must be at least twice that for it to work. Use the filtfilt function to do the actual filtering.
.
Star Strider on 19 Nov 2021
As always, my pleasure!
.