# MATLAB Designed Filter Does Not Filter

I designed a FIR Kaiser windowed low pass digital filter with filter design app, and had MATLAB generate the code for it and return the filter object:

function Hd = filter_lp_100
%FILTER_LP_100 Returns a discrete-time filter object.

% MATLAB Code
% Generated by MATLAB(R) 9.2 and the DSP System Toolbox 9.4.
% Generated on: 04-Aug-2017 17:09:08
% FIR Window Lowpass filter designed using the FIR1 function.
% All frequency values are in Hz.

Fs = 500;                % Sampling Frequency
Fpass = 100;             % Passband Frequency
Fstop = 110;             % Stopband Frequency
Dpass = 0.057501127785;  % Passband Ripple
Dstop = 0.0000000001;    % Stopband Attenuation
flag  = 'scale';         % Sampling Flag

% Calculate the order from the parameters using KAISERORD.
[N,Wn,BETA,TYPE] = kaiserord([Fpass Fstop]/(Fs/2), [1 0], [Dstop Dpass]);

% Calculate the coefficients using the FIR1 function.
b  = fir1(N, Wn, TYPE, kaiser(N+1, BETA), flag);
Hd = dfilt.dffir(b);


But this filter does not attenuate the input signal, it even amplifies it! I expected that PSD for the output becomes smaller, or at most equal to input PSD(for the pass band) and that's not happening in any point. Please see the plots. Blue is input signal power spectrum density, Red is output PSD, and yellow is filter response.

I tried different filters, none of them is performing. My code is very simple, I copied the part that does filtering and plotting filter response:

%% FILTER %%%%%%%%%%%%%%%%%%%%%%%%%%
coef=filter_lp_100;  % MATLAB filter generated function above
out_signal=filter(coef.numerator,1,in_signal);

%%% plotting filter response. f is physical frequencies, fs is sampling rate:
h=freqz(coef,f,fs);
plot(f,10*log10(abs(h)))


Do you see anything wrong with this?

• This looks totally right. Can you describe what you think is wrong with this? what kind of effect did you expect? Notice that your input signal isn't zero-mean and your filter output truthfully reflects that. From my perspective, everything is fine with that filter, aside from you plotting things a bit too small :) – Marcus Müller Aug 7 '17 at 21:11
• @MarcusMüller I reposted with a larger image:) I expected that PSD for the output becomes smaller, or at most equal to input PSD(for the flat part) and that's not happening it's higher than input in all points. Also I don't understand why the tail of output PSD(red) suddenly flattens and stays at -90dB and higher than input. – doubleE Aug 7 '17 at 21:38
• @MarcusMüller and I forgot to mention, I wanted to filter high frequency noise. I zoomed into baseline of input signal which is noisy, and compared it to output baseline, output is still noisy with high-frequency noise.. – doubleE Aug 7 '17 at 21:48
• That might very well be an effect of aliasing and numerical accuracy; it's at -90dB, so I simply ignored it. It's not about the periodicity of how often the pulses occur, but about their individual frequency content, which I roughly estimated by saying "pulse bandwidth ≈ 1/ pulse length". – Marcus Müller Aug 7 '17 at 22:04
• I'm assuming you want the ecg so the DC component isn't a feature of interest. Your spectrum seems to be a victim of leakage – user28715 Aug 7 '17 at 22:45